Encoder

Struct Encoder 

Source
pub struct Encoder<W: Write> { /* private fields */ }
Expand description

An encoder for MS-NRBF binary streams.

Implementations§

Source§

impl<W: Write> Encoder<W>

Source

pub fn new(writer: W) -> Self

Creates a new encoder from a writer.

Examples found in repository?
examples/round_trip.rs (line 74)
25fn main() -> Result<(), Box<dyn std::error::Error>> {
26    let args: Vec<String> = env::args().collect();
27    if args.len() < 2 {
28        eprintln!("Usage: {} <nrbf_file>", args[0]);
29        std::process::exit(1);
30    }
31
32    let input_path = &args[1];
33    println!("Reading original file: {}", input_path);
34    let file = File::open(input_path)?;
35    let mut original_data = Vec::new();
36    File::open(input_path)?.read_to_end(&mut original_data)?;
37
38    let reader = BufReader::new(file);
39    let mut decoder = Decoder::new(reader);
40
41    let mut records = Vec::new();
42    while let Some(record) = decoder.decode_next()? {
43        let is_end = matches!(record, Record::MessageEnd);
44        records.push(record);
45        if is_end {
46            break;
47        }
48    }
49    println!("Parsed {} records.", records.len());
50
51    // Serialize to JSON
52    let json = serde_json::to_string_pretty(&records)?;
53    let json_path = "output.json";
54    std::fs::write(json_path, &json)?;
55    println!("Saved records to {}", json_path);
56
57    // Serialize to Interleaved JSON
58    let interleaved_json = to_interleaved(&records);
59    let interleaved_json_str = serde_json::to_string_pretty(&interleaved_json)?;
60    let interleaved_path = "interleaved.json";
61    std::fs::write(interleaved_path, &interleaved_json_str)?;
62    println!("Saved interleaved records to {}", interleaved_path);
63
64    // Deserialize from JSON
65    let deserialized_records: Vec<Record> = serde_json::from_str(&json)?;
66    println!(
67        "Deserialized {} records from JSON.",
68        deserialized_records.len()
69    );
70
71    // Encode back to binary
72    let output_path = "reconstructed.meta";
73    let out_file = File::create(output_path)?;
74    let mut encoder = Encoder::new(BufWriter::new(out_file));
75
76    for record in &deserialized_records {
77        encoder.encode(record)?;
78    }
79    // Ensure everything is flushed
80    drop(encoder);
81    println!("Reconstructed binary saved to {}", output_path);
82
83    // Interleaved reconstruction check
84    println!("--- Interleaved Round Trip Check ---");
85    let interleaved_reconstructed_records = from_interleaved(interleaved_json);
86    println!(
87        "Deserialized {} records from Interleaved JSON.",
88        interleaved_reconstructed_records.len()
89    );
90
91    let interleaved_output_path = "reconstructed_interleaved.meta";
92    let int_out_file = File::create(interleaved_output_path)?;
93    let mut int_encoder = Encoder::new(BufWriter::new(int_out_file));
94
95    for record in &interleaved_reconstructed_records {
96        int_encoder.encode(record)?;
97    }
98    drop(int_encoder);
99
100    let mut int_reconstructed_data = Vec::new();
101    File::open(interleaved_output_path)?.read_to_end(&mut int_reconstructed_data)?;
102
103    if original_data == int_reconstructed_data {
104        println!("SUCCESS: Interleaved reconstructed binary is identical to original!");
105    } else {
106        println!("FAILURE: Interleaved reconstructed binary differs from original.");
107        println!(
108            "Original size: {}, Reconstructed size: {}",
109            original_data.len(),
110            int_reconstructed_data.len()
111        );
112        // Find first difference
113        let min_len = std::cmp::min(original_data.len(), int_reconstructed_data.len());
114        for i in 0..min_len {
115            if original_data[i] != int_reconstructed_data[i] {
116                println!(
117                    "First difference at offset 0x{:x}: original 0x{:02x}, reconstructed 0x{:02x}",
118                    i, original_data[i], int_reconstructed_data[i]
119                );
120                break;
121            }
122        }
123    }
124
125    Ok(())
126}
Source

pub fn encode(&mut self, record: &Record) -> Result<()>

Encodes a record and writes it to the stream.

Examples found in repository?
examples/round_trip.rs (line 77)
25fn main() -> Result<(), Box<dyn std::error::Error>> {
26    let args: Vec<String> = env::args().collect();
27    if args.len() < 2 {
28        eprintln!("Usage: {} <nrbf_file>", args[0]);
29        std::process::exit(1);
30    }
31
32    let input_path = &args[1];
33    println!("Reading original file: {}", input_path);
34    let file = File::open(input_path)?;
35    let mut original_data = Vec::new();
36    File::open(input_path)?.read_to_end(&mut original_data)?;
37
38    let reader = BufReader::new(file);
39    let mut decoder = Decoder::new(reader);
40
41    let mut records = Vec::new();
42    while let Some(record) = decoder.decode_next()? {
43        let is_end = matches!(record, Record::MessageEnd);
44        records.push(record);
45        if is_end {
46            break;
47        }
48    }
49    println!("Parsed {} records.", records.len());
50
51    // Serialize to JSON
52    let json = serde_json::to_string_pretty(&records)?;
53    let json_path = "output.json";
54    std::fs::write(json_path, &json)?;
55    println!("Saved records to {}", json_path);
56
57    // Serialize to Interleaved JSON
58    let interleaved_json = to_interleaved(&records);
59    let interleaved_json_str = serde_json::to_string_pretty(&interleaved_json)?;
60    let interleaved_path = "interleaved.json";
61    std::fs::write(interleaved_path, &interleaved_json_str)?;
62    println!("Saved interleaved records to {}", interleaved_path);
63
64    // Deserialize from JSON
65    let deserialized_records: Vec<Record> = serde_json::from_str(&json)?;
66    println!(
67        "Deserialized {} records from JSON.",
68        deserialized_records.len()
69    );
70
71    // Encode back to binary
72    let output_path = "reconstructed.meta";
73    let out_file = File::create(output_path)?;
74    let mut encoder = Encoder::new(BufWriter::new(out_file));
75
76    for record in &deserialized_records {
77        encoder.encode(record)?;
78    }
79    // Ensure everything is flushed
80    drop(encoder);
81    println!("Reconstructed binary saved to {}", output_path);
82
83    // Interleaved reconstruction check
84    println!("--- Interleaved Round Trip Check ---");
85    let interleaved_reconstructed_records = from_interleaved(interleaved_json);
86    println!(
87        "Deserialized {} records from Interleaved JSON.",
88        interleaved_reconstructed_records.len()
89    );
90
91    let interleaved_output_path = "reconstructed_interleaved.meta";
92    let int_out_file = File::create(interleaved_output_path)?;
93    let mut int_encoder = Encoder::new(BufWriter::new(int_out_file));
94
95    for record in &interleaved_reconstructed_records {
96        int_encoder.encode(record)?;
97    }
98    drop(int_encoder);
99
100    let mut int_reconstructed_data = Vec::new();
101    File::open(interleaved_output_path)?.read_to_end(&mut int_reconstructed_data)?;
102
103    if original_data == int_reconstructed_data {
104        println!("SUCCESS: Interleaved reconstructed binary is identical to original!");
105    } else {
106        println!("FAILURE: Interleaved reconstructed binary differs from original.");
107        println!(
108            "Original size: {}, Reconstructed size: {}",
109            original_data.len(),
110            int_reconstructed_data.len()
111        );
112        // Find first difference
113        let min_len = std::cmp::min(original_data.len(), int_reconstructed_data.len());
114        for i in 0..min_len {
115            if original_data[i] != int_reconstructed_data[i] {
116                println!(
117                    "First difference at offset 0x{:x}: original 0x{:02x}, reconstructed 0x{:02x}",
118                    i, original_data[i], int_reconstructed_data[i]
119                );
120                break;
121            }
122        }
123    }
124
125    Ok(())
126}

Auto Trait Implementations§

§

impl<W> Freeze for Encoder<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for Encoder<W>
where W: RefUnwindSafe,

§

impl<W> Send for Encoder<W>
where W: Send,

§

impl<W> Sync for Encoder<W>
where W: Sync,

§

impl<W> Unpin for Encoder<W>
where W: Unpin,

§

impl<W> UnwindSafe for Encoder<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.