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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
use std::{
io::Write,
ops::{BitAnd, Shr},
};
use bytes::BufMut;
use crate::{Error, Result};
/// A trait that provides methods for writing basic primitive types.
pub trait Writer {
/// Writes a single byte.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_u8(&mut self, value: u8) -> Result<()>;
/// Implement this method for more efficient writing of multiple bytes.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_buf(&mut self, buf: &[u8]) -> Result<()> {
for b in buf {
self.write_u8(*b)?;
}
Ok(())
}
/// Writes presence map.
/// # Errors
/// Returns error if bitmap is ill-formed or writer can't handle more bytes.
fn write_presence_map(&mut self, bitmap: u64, size: u8) -> Result<()> {
if size == 0 {
self.write_u8(0x80)?;
return Ok(());
}
if !size.is_multiple_of(7) {
return Err(Error::Runtime(
"write_presence_map: size must be multiple of 7".to_string(),
));
}
let trailing_bits = bitmap.trailing_zeros() as usize;
// Only if all 7 bits are 0 we treat byte as trailing
let trailing_bytes = trailing_bits / 7;
let bitmap = bitmap >> (trailing_bytes * 7);
// Skipping useless bytes.
let len = usize::from(size / 7).saturating_sub(trailing_bytes);
if len == 0 {
self.write_u8(0x80)?;
return Ok(());
}
// For u64 there's only 10 bytes we can write (if number is u64::MAX) and meaning_bits is 64.
// Then 64.div_ceil(7) is 10;
let buf = encode_number(bitmap, len);
self.write_buf(&buf[..len])
}
/// Writes non-nullable 64bit unsigned integer.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_uint(&mut self, value: u64) -> Result<()> {
// If number is zero we only have to add last byte marker.
if value == 0 {
return self.write_u8(0x80);
}
// Calculating position of the last 1 in number.
let meaning_bits = u64::BITS - value.leading_zeros();
// Since we can write only 7 bits (8 bit is the last bit marker), getting number of bytes to write.
// 1..=7 => 1 byte
// 8..=15 => 2 byte
// etc
let bytes_to_write = meaning_bits.div_ceil(7) as usize;
let buf = encode_number(value, bytes_to_write);
self.write_buf(&buf[..bytes_to_write])
}
/// Writes nullable 64bit unsigned integer.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_uint_nullable(&mut self, value: Option<u64>) -> Result<()> {
match value {
None => self.write_uint(0),
Some(v) => self.write_uint(v + 1),
}
}
/// Writes non-nullable 64bit signed integer.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_int(&mut self, value: i64) -> Result<()> {
// If number contains only meaningless_bits, just write it with last byte marker.
if value == 0 || value == -1 {
return self.write_u8(value as u8 | 0x80);
}
let is_pos = value >= 0;
// For posititive numbers we ignore 0 in the MSB since we only search for last 1
// For negatitive number logic is reversed.
let useless_bits = if is_pos {
value.leading_zeros()
} else {
value.leading_ones()
};
// Calculating position of the last meaning sign in number.
let meaning_bits = i64::BITS - useless_bits;
// Since we can write only 7 bits (8 bit is the last bit marker), getting number of bytes to write.
// 1..=7 => 1 byte
// 8..=15 => 2 byte
// etc
let bytes_to_write = meaning_bits.div_ceil(7) as usize;
// Additional byte is required if the last signed bit position is divisible by 7.
let additional_byte = usize::from(
(value.unbounded_shr(bytes_to_write as u32 * 7 - 1)) & 0x1 == i64::from(is_pos),
);
let bytes_to_write = bytes_to_write + additional_byte;
let buf = encode_number(value, bytes_to_write);
self.write_buf(&buf[..bytes_to_write])
}
/// Writes nullable 64bit signed integer.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_int_nullable(&mut self, value: Option<i64>) -> Result<()> {
match value {
None => self.write_int(0),
Some(v) if v >= 0 => self.write_int(v + 1),
Some(v) => self.write_int(v),
}
}
/// Writes non-nullable ASCII encoded bytes.
/// # Errors
/// Returns error if string is not ASCII encoded or writer can't handle more bytes.
fn write_ascii_string(&mut self, value: &str) -> Result<()> {
self.write_ascii_str(value, &[0x80])
}
/// Writes nullable ASCII encoded bytes.
/// # Errors
/// Returns error if string is not ASCII encoded or writer can't handle more bytes.
fn write_ascii_string_nullable(&mut self, value: Option<&str>) -> Result<()> {
match value {
None => self.write_u8(0x80),
Some(s) => self.write_ascii_str(s, &[0x00, 0x80]),
}
}
/// Writes non-nullable ASCII encoded bytes.
/// # Errors
/// Returns error if string is not ASCII encoded or writer can't handle more bytes.
fn write_ascii_str(&mut self, value: &str, empty: &[u8]) -> Result<()> {
// Checking is string contains only ASCII chars.
// If so we can just use them as slice of bytes with only last byte changed.
if !value.is_ascii() {
return Err(Error::Runtime(
"write_ascii_string: invalid ASCII char".to_string(),
));
}
// Splitting up last bytes since it have to be marked.
// If string is empty we should write only empty.
let [buf @ .., last_byte] = value.as_bytes() else {
return self.write_buf(empty);
};
self.write_buf(buf)?;
self.write_u8(*last_byte | 0x80)
}
/// Writes non-nullable Unicode encoded string.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_unicode_string(&mut self, value: &str) -> Result<()> {
self.write_bytes(value.as_bytes())
}
/// Writes nullable Unicode encoded string.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_unicode_string_nullable(&mut self, value: Option<&str>) -> Result<()> {
match value {
None => self.write_bytes_nullable(None),
Some(s) => self.write_bytes_nullable(Some(s.as_bytes())),
}
}
/// Writes non-nullable bytes.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_bytes(&mut self, value: &[u8]) -> Result<()> {
self.write_uint(value.len() as u64)?;
self.write_buf(value)
}
/// Writes nullable bytes.
/// # Errors
/// Returns error if writer can't handle more bytes.
fn write_bytes_nullable(&mut self, value: Option<&[u8]>) -> Result<()> {
match value {
None => self.write_uint_nullable(None),
Some(b) => {
if b.is_empty() {
self.write_uint_nullable(Some(0))
} else {
self.write_uint_nullable(Some(b.len() as u64))?;
self.write_buf(b)
}
}
}
}
}
trait ToByte {
fn to_byte(&self) -> u8;
}
impl ToByte for i64 {
fn to_byte(&self) -> u8 {
*self as u8
}
}
impl ToByte for u64 {
fn to_byte(&self) -> u8 {
*self as u8
}
}
fn encode_number<T>(number: T, len: usize) -> [u8; 10]
where
T: ToByte + Shr<usize, Output = T> + BitAnd<Output = T> + From<u8> + Copy,
{
// For 64 bit number there's only 10 bytes we can write (if number is T::MAX) and meaning_bits is 64.
// Then 64.div_ceil(7) is 10;
let mut buf = [0; 10];
let values = (0..len).map(|i| {
// Writing in reversed order because most signed bits have to be written first.
let offset_bits_index = len - i - 1;
let shifted_bitmap = number >> (offset_bits_index * 7);
(shifted_bitmap & T::from(0x7f)).to_byte()
});
for (buf, value) in buf.iter_mut().zip(values) {
*buf = value;
}
// set stop bit
buf[len - 1] |= 0x80;
buf
}
impl Writer for bytes::BytesMut {
fn write_u8(&mut self, value: u8) -> Result<()> {
self.put_u8(value);
Ok(())
}
fn write_buf(&mut self, buf: &[u8]) -> Result<()> {
self.put(buf);
Ok(())
}
}
/// Wrapper around `std::io::Write` that implements [`fastlib::Writer`][crate::encoder::writer::Writer].
pub(crate) struct StreamWriter<'a> {
stream: &'a mut dyn Write,
}
impl<'a> StreamWriter<'a> {
pub fn new(stream: &'a mut dyn Write) -> Self {
Self { stream }
}
}
impl Writer for StreamWriter<'_> {
fn write_u8(&mut self, value: u8) -> Result<()> {
self.stream.write_all(&[value])?;
Ok(())
}
fn write_buf(&mut self, buf: &[u8]) -> Result<()> {
self.stream.write_all(buf)?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn write_presence_map() {
struct TestCase {
pmap: (u64, u8),
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
pmap: (0b0, 7),
value: vec![0x80],
},
TestCase {
pmap: (0b1, 7),
value: vec![0x81],
},
TestCase {
pmap: (0b11110001111, 14),
value: vec![0x0f, 0x8f],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_presence_map(tc.pmap.0, tc.pmap.1).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
#[test]
fn write_uint() {
struct TestCase {
input: u64,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
input: 0,
value: vec![0x80],
},
TestCase {
input: 1,
value: vec![0x81],
},
TestCase {
input: 942755,
value: vec![0x39, 0x45, 0xa3],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_uint(tc.input).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
#[test]
fn write_uint_nullable() {
struct TestCase {
input: Option<u64>,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
input: None,
value: vec![0x80],
},
TestCase {
input: Some(0),
value: vec![0x81],
},
TestCase {
input: Some(942755),
value: vec![0x39, 0x45, 0xa4],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_uint_nullable(tc.input).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
#[test]
fn write_int() {
struct TestCase {
input: i64,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
// Mandatory Positive Number
TestCase {
input: 942755,
value: vec![0x39, 0x45, 0xa3],
},
// Mandatory Negative Number
TestCase {
input: -7942755,
value: vec![0x7c, 0x1b, 0x1b, 0x9d],
},
// Mandatory Positive Number with sign-bit extension
TestCase {
input: 8193,
value: vec![0x00, 0x40, 0x81],
},
// Mandatory Negative Number with sign-bit extension
TestCase {
input: -8193,
value: vec![0x7f, 0x3f, 0xff],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_int(tc.input).unwrap();
assert_eq!(buf.to_vec(), tc.value, "Invalid encoder for {}", tc.input);
}
}
#[test]
fn write_int_nullable() {
struct TestCase {
input: Option<i64>,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
input: None,
value: vec![0x80],
},
// Optional Positive Number
TestCase {
input: Some(942755),
value: vec![0x39, 0x45, 0xa4],
},
// Optional Negative Number
TestCase {
input: Some(-942755),
value: vec![0x46, 0x3a, 0xdd],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_int_nullable(tc.input).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
#[test]
fn write_ascii_string() {
struct TestCase {
input: &'static str,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
input: "",
value: vec![0x80],
},
TestCase {
input: "ABC",
value: vec![0x41, 0x42, 0xc3],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_ascii_string(tc.input).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
#[test]
fn write_ascii_string_nullable() {
struct TestCase {
input: Option<&'static str>,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
input: None,
value: vec![0x80],
},
TestCase {
input: Some(""),
value: vec![0x00, 0x80],
},
TestCase {
input: Some("ABC"),
value: vec![0x41, 0x42, 0xc3],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_ascii_string_nullable(tc.input).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
#[test]
fn write_unicode_string() {
struct TestCase {
input: &'static str,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
input: "",
value: vec![0x80],
},
TestCase {
input: "ABC",
value: vec![0x83, 0x41, 0x42, 0x43],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_unicode_string(tc.input).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
#[test]
fn write_unicode_string_nullable() {
struct TestCase {
input: Option<&'static str>,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
input: None,
value: vec![0x80],
},
TestCase {
input: Some(""),
value: vec![0x81],
},
TestCase {
input: Some("ABC"),
value: vec![0x84, 0x41, 0x42, 0x43],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_unicode_string_nullable(tc.input).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
#[test]
fn write_bytes() {
struct TestCase {
input: Vec<u8>,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
input: vec![],
value: vec![0x80],
},
TestCase {
input: vec![0x41, 0x42, 0x43],
value: vec![0x83, 0x41, 0x42, 0x43],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_bytes(&tc.input).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
#[test]
fn write_bytes_nullable() {
struct TestCase {
input: Option<Vec<u8>>,
value: Vec<u8>,
}
let test_cases: Vec<TestCase> = vec![
TestCase {
input: None,
value: vec![0x80],
},
TestCase {
input: Some(vec![]),
value: vec![0x81],
},
TestCase {
input: Some(vec![0x41, 0x42, 0x43]),
value: vec![0x84, 0x41, 0x42, 0x43],
},
];
for tc in test_cases {
let mut buf = bytes::BytesMut::new();
buf.write_bytes_nullable(tc.input.as_deref()).unwrap();
assert_eq!(buf.to_vec(), tc.value);
}
}
}