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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/// The maximum value of an EO char (1-byte encoded integer type)
pub const CHAR_MAX: i32 = 253;
/// The maximum value of an EO short (2-byte encoded integer type)
pub const SHORT_MAX: i32 = CHAR_MAX * CHAR_MAX;
/// The maximum value of an EO three (3-byte encoded integer type)
pub const THREE_MAX: i32 = CHAR_MAX * CHAR_MAX * CHAR_MAX;
/// The maximum value of an EO int (4-byte encoded integer type)
pub const INT_MAX: i64 = CHAR_MAX as i64 * CHAR_MAX as i64 * CHAR_MAX as i64 * CHAR_MAX as i64;
/// Returns an encoded byte array from `number`
///
/// EO uses a maximum of four bytes to represent a number
/// in a data stream.
///
/// The value is spread across the four bytes based on if the
/// value is greater than the defined maximum for each amount of bytes
/// see [CHAR_MAX], [SHORT_MAX], [THREE_MAX]
///
/// The four bytes are initialized with a value of 254.
/// This is used later in [decode_number] for translating to a 0 value.
///
/// Bytes 4, 3, and 2 are set as follows if `number` is greater than or equal to
/// the corresponding `MAX` constant.
///
/// `bytes[x] = (number / MAX_x) + 1`
///
/// the number is then set to be the remainder of the division as follows
///
/// `number %= MAX_x`
///
/// Byte 1 is simply the remaining `number` plus one.
///
/// `bytes[0] = number + 1`
///
/// # Examples
///
/// ## Number less than CHAR_MAX
/// ```
/// use eolib::data::encode_number;
///
/// let result = encode_number(42).unwrap();
/// assert_eq!(result, [43, 254, 254, 254]);
/// ```
/// since 42 is less than CHAR_MAX it is simply incremented by 1
/// and the remaining bytes are set to 254
///
/// ## Number less than SHORT_MAX
/// ```
/// use eolib::data::encode_number;
/// let result = encode_number(533).unwrap();
/// assert_eq!(result, [28, 3, 254, 254]);
/// ```
///
/// since 533 is grater than CHAR_MAX byte 2 is set to
///
/// `(533 / CHAR_MAX) + 1 // 3`
///
/// byte 1 is set to the the remainder + 1
///
/// `(533 % CHAR_MAX) + 1 // 28`
///
/// and the remaining bytes are set to 254
///
/// ## Number less than THREE_MAX
/// ```
/// use eolib::data::encode_number;
/// let result = encode_number(888888).unwrap();
/// assert_eq!(result, [100, 225, 14, 254]);
/// ```
///
/// since 888888 is grater than SHORT_MAX byte 3 is set to
///
/// `(888888 / SHORT_MAX) + 1 // 14`
///
/// byte 2 is set to
///
/// `((888888 % SHORT_MAX) / CHAR_MAX) + 1 // 225`
///
/// byte 1 is set to the the remainder + 1
///
/// `(888888 % SHORT_MAX % CHAR_MAX) + 1 // 100`
///
/// and the last byte is set to 254
///
/// ## Number less than MAX4
/// ```
/// use eolib::data::encode_number;
/// let result = encode_number(18994242).unwrap();
/// assert_eq!(result, [15, 189, 44, 2]);
/// ```
///
/// since 18994242 is grater than THREE_MAX byte 4 is set to
///
/// `(18994242 / THREE_MAX) + 1 // 2`
///
/// byte 3 is set to
///
/// `((18994242 % THREE_MAX) / SHORT_MAX) + 1 // 44`
///
/// byte 2 is set to
///
/// `((18994242 % THREE_MAX % SHORT_MAX) / CHAR_MAX) + 1 // 189`
///
/// byte 1 is set to the the remainder + 1
///
/// `(18994242 % THREE_MAX % SHORT_MAX % CHAR_MAX) + 1 // 15`
/// Returns a decoded number from an EO Byte array
///
/// EO uses a maximum of four bytes to represent a number
/// in a data stream.
///
/// You can provide any number of [u8]s in `bytes`
/// but only the first four are used.
///
/// If you provide less than four than the remaining bytes default to 254
///
/// The byte array is iterated over and any byte of 254 is changed to 1, and
/// each byte is decremented by 1.
///
/// The result is then calculated like so
///
/// `(b4 * THREE_MAX) + (b3 * SHORT_MAX) + (b2 * CHAR_MAX) + b1`
///
/// # Examples
/// ```
/// use eolib::data::decode_number;
/// let result = decode_number(&[43, 254, 254, 254]);
/// assert_eq!(result, 42);
/// ```
///
/// * bytes with `254` are swapped to `1`
/// `[43, 1, 1, 1]`
/// * bytes are decremented by 1
/// `[42, 0, 0, 0]`
/// * bytes are multiplied by MAX's and summed
/// `(0 * THREE_MAX) + (0 * SHORT_MAX) + (0 * CHAR_MAX) + 42 == 42`
///
/// Decodes a string in place
///
/// This is used for map names and sign text in map files
///
/// # Examples
///
/// ```
/// use eolib::data::decode_string;
///
/// let mut buf = [0x69, 0x36, 0x5E, 0x49];
/// decode_string(&mut buf);
///
/// let name = String::from_utf8_lossy(&buf).to_string();
/// assert_eq!(name, "Void");
/// ````
/// Encodes a string in place
///
/// This is used for map names and sign text in map files
///
/// # Examples
///
/// ```
/// use eolib::data::encode_string;
///
/// let mut buf = b"Void".to_vec();
/// encode_string(&mut buf);
///
/// assert_eq!(buf, [0x69, 0x36, 0x5E, 0x49]);
/// ````
pub use ;
pub use ;
pub use ;