ms_pdb/
encoder.rs

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
//! Support for encoding primitives and blittable types into output buffers.
#![allow(missing_docs)]

use crate::guid::GuidLe;
use bstr::BStr;
use uuid::Uuid;
use zerocopy::{Immutable, IntoBytes};

pub struct Encoder<'a> {
    pub buf: &'a mut Vec<u8>,
}

impl<'a> Encoder<'a> {
    pub fn new(buf: &'a mut Vec<u8>) -> Self {
        Self { buf }
    }

    pub fn len(&self) -> usize {
        self.buf.len()
    }

    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    pub fn u8(&mut self, x: u8) {
        self.buf.push(x);
    }

    pub fn bytes(&mut self, b: &[u8]) {
        self.buf.extend_from_slice(b);
    }

    pub fn u16(&mut self, x: u16) {
        self.bytes(&x.to_le_bytes());
    }

    pub fn u32(&mut self, x: u32) {
        self.bytes(&x.to_le_bytes());
    }

    pub fn t<T: IntoBytes + Immutable>(&mut self, x: &T) {
        self.buf.extend_from_slice(x.as_bytes());
    }

    pub fn strz(&mut self, s: &BStr) {
        self.buf.extend_from_slice(s);
        self.buf.push(0);
    }

    pub fn uuid(&mut self, u: &Uuid) {
        self.t(&GuidLe::from(u));
    }
}