1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use ;
use SeekFrom;
/// Parses bytes from a reader, checking for the JPEG header `0xFFD8` and reading until the end JPEG marker `0xFFD9` is encountered.
///
/// This function first checks if the initial two bytes match `0xFFD8`. If so, it continuously reads bytes from the given reader and checks
/// for the occurrence of the byte sequence `0xFFD9`. Once this sequence is found, the function stops reading and returns the accumulated bytes
/// (excluding `0xFFD9`). If the initial bytes do not match `0xFFD8`, an error is returned.
///
/// # Parameters
///
/// * `reader`: A mutable reference to an object implementing the `Read` and `Seek` traits.
/// This reader is used to fetch the bytes.
///
/// # Returns
///
/// Returns a `BinResult<Vec<u8>>` which is Ok containing a vector of bytes read until (but
/// not including) `0xFFD9`, or an error (`binrw::Error`) if an issue occurs during reading or
/// if the initial bytes are not `0xFFD8`.
///
/// # Errors
///
/// This function will return an error if there is a problem reading from the reader or if the initial bytes do not match the JPEG header.
/// Seeks through a byte stream to find the start of a JPEG record or an end marker.
///
/// This function reads through bytes from the provided reader two at a time. It looks for
/// a JPEG record, indicated by the byte sequence `0xFF, 0xD8`, or an end marker, indicated
/// by a single `0xFF` byte. If a JPEG record is found, the reader's position is set to the
/// start of this record. If an end marker is found, the reading stops, and the reader's
/// position is at the end marker.
///
/// # Parameters
///
/// * `reader`: A mutable reference to an object implementing the `Read` and `Seek` traits.
///
/// # Returns
///
/// Returns a `BinResult<Vec<u8>>` which is Ok containing a vector of bytes read until either
/// a JPEG record start or an end marker was found. Returns an error (`binrw::Error`) if an
/// issue occurs during reading.
///
/// # Errors
///
/// This function will return an error in the event of a reading failure from the reader.
///
/// Reads a 16-bit unsigned integer from a given reader.
///
/// This function attempts to read 2 bytes from the specified reader and convert them into a `u16`. It can adjust its behavior based on the `from_u8` flag.
///
/// # Parameters
/// - `from_u8`: A boolean flag indicating how to read the bytes.
/// - If `true`, only one byte is read from the reader, and it's treated as the lower part of the `u16`.
/// - If `false`, two bytes are read and directly converted to `u16`.
///
/// # Returns
/// This function returns a `BinResult<u16>`. On successful read and conversion, it returns `Ok(u16)`.
/// If there is any error in reading from the reader, it returns an error encapsulated in the `BinResult`.
///
/// # Errors
/// This function will return an error if the reader fails to provide the necessary number of bytes (1 or 2, based on `from_u8`).
/// ```
/// Extracts and aligns a subset of bits from a given byte.
///
/// This function selects bits from the `byte` parameter based on the `mask` parameter.
/// Bits in `byte` that correspond to `1` bits in `mask` are extracted and shifted
/// towards the least significant bit (LSB) position. Bits in positions where `mask` has `0` bits
/// are ignored. The result is a byte containing only the extracted bits, aligned starting
/// from the LSB.
///
/// # Examples
///
/// ```
/// let byte = 0b10101100;
/// let mask = 0b11100000;
/// assert_eq!(sub_byte_field(byte, mask), 0b00000101);
/// ```
///
/// In this example, `sub_byte_field` takes `byte` (0b10101100) and `mask` (0b11100000).
/// The three high-order bits (101) of `byte` are selected and then shifted right by 5 positions
/// to align at the LSB, resulting in 0b00000101.
///
/// # Parameters
///
/// * `byte`: The byte from which to extract bits.
/// * `mask`: A mask to specify which bits to extract. Bits corresponding to `1` bits in the mask
/// are extracted, and `0` bits in the mask are ignored.
///
/// # Returns
///
/// Returns an `u8` byte containing the extracted and LSB-aligned bits from the original `byte`.
///
/// # Notes
///
/// The function assumes that the mask has contiguous `1` bits starting from the most significant bit
/// (MSB). If the mask's `1` bits are not contiguous or do not start from the MSB, the behavior
/// might not be as expected.
/// Ensures that a given slice of bytes has a minimum specified length, padding it with zeros if necessary.
///
/// # Parameters
///
/// - `input`: A slice of bytes (`&[u8]`) to check and possibly extend.
/// - `min_length`: The minimum length desired for the output vector. If `input` is shorter than this,
/// it will be padded with zeros until it reaches `min_length`.
///
/// # Returns
///
/// A `Vec<u8>` that is at least `min_length` bytes long. If the input slice was already at least
/// `min_length` bytes long, this will be a direct copy of `input`. Otherwise, it will be `input`
/// followed by enough zeros to reach `min_length`.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// let bytes = b"Hello, world!";
/// let padded_bytes = pad_with_zeros(&bytes[..], 16);
/// assert_eq!(padded_bytes, b"Hello, world!\x00\x00\x00");
/// ```
/// Adds a new message to the original message with a separator if the new message is not empty.
///
/// # Arguments
///
/// * `original_message` - The original message to which the new message will be appended.
/// * `message` - The new message to be added. If this message is empty, the original message will remain unchanged.
///
/// # Returns
///
/// A `String` containing the original message followed by the new message separated by "; " if the new message is not empty,
/// otherwise the original message.
///
///