hematite-nbt 0.5.2

A full-featured library for working with Minecraft's Named Binary Tag (NBT) file format, including Serde support.
Documentation
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
use std::fs::File;
use std::io;
use Map;

//use test::Bencher;

use blob::Blob;
use error::Error;
use value::Value;

#[test]
fn nbt_nonempty() {
    let mut nbt = Blob::new();
    nbt.insert("name", "Herobrine").unwrap();
    nbt.insert("health", 100i8).unwrap();
    nbt.insert("food", 20.0f32).unwrap();
    nbt.insert("emeralds", 12345i16).unwrap();
    nbt.insert("timestamp", 1424778774i32).unwrap();

    #[rustfmt::skip]
    let bytes = vec![
        0x0a,
            0x00, 0x00,
            0x08,
                0x00, 0x04,
                0x6e, 0x61, 0x6d, 0x65,
                0x00, 0x09,
                0x48, 0x65, 0x72, 0x6f, 0x62, 0x72, 0x69, 0x6e, 0x65,
            0x01,
                0x00, 0x06,
                0x68, 0x65, 0x61, 0x6c, 0x74, 0x68,
                0x64,
            0x05,
                0x00, 0x04,
                0x66, 0x6f, 0x6f, 0x64,
                0x41, 0xa0, 0x00, 0x00,
            0x02,
                0x00, 0x08,
                0x65, 0x6d, 0x65, 0x72, 0x61, 0x6c, 0x64, 0x73,
                0x30, 0x39,
            0x03,
                0x00, 0x09,
                0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70,
                0x54, 0xec, 0x66, 0x16,
        0x00
    ];

    // Test correct length and contents when field order is preserved
    let mut dst = Vec::new();
    nbt.to_writer(&mut dst).unwrap();
    assert_eq!(&bytes.len(), &dst.len());
    #[cfg(feature = "preserve_order")]
    assert_eq!(&bytes, &dst);

    // When not using the preserve_order feature,
    // we can only test if the decoded bytes match, since the HashMap does
    // not guarantee order (and so encoding is likely to be different, but
    // still correct).
    let mut src = io::Cursor::new(bytes);
    let file = Blob::from_reader(&mut src).unwrap();
    assert_eq!(&file, &nbt);
}

#[test]
fn nbt_empty_nbtfile() {
    let nbt = Blob::new();

    #[rustfmt::skip]
    let bytes = vec![
        0x0a,
            0x00, 0x00,
        0x00
    ];

    // Test encoding.
    let mut dst = Vec::new();
    nbt.to_writer(&mut dst).unwrap();
    assert_eq!(&dst, &bytes);

    // Test decoding.
    let mut src = io::Cursor::new(bytes);
    let file = Blob::from_reader(&mut src).unwrap();
    assert_eq!(&file, &nbt);
}

#[test]
fn nbt_nested_compound() {
    let mut inner = Map::new();
    inner.insert("test".to_string(), Value::Byte(123));
    let mut nbt = Blob::new();
    nbt.insert("inner", Value::Compound(inner)).unwrap();

    #[rustfmt::skip]
    let bytes = vec![
        0x0a,
            0x00, 0x00,
            0x0a,
                0x00, 0x05,
                0x69, 0x6e, 0x6e, 0x65, 0x72,
                0x01,
                0x00, 0x04,
                0x74, 0x65, 0x73, 0x74,
                0x7b,
            0x00,
        0x00
    ];

    // Test encoding.
    let mut dst = Vec::new();
    nbt.to_writer(&mut dst).unwrap();
    assert_eq!(&dst, &bytes);

    // Test decoding.
    let mut src = io::Cursor::new(bytes);
    let file = Blob::from_reader(&mut src).unwrap();
    assert_eq!(&file, &nbt);
}

#[test]
fn nbt_empty_list() {
    let mut nbt = Blob::new();
    nbt.insert("list", Value::List(Vec::new())).unwrap();

    #[rustfmt::skip]
    let bytes = vec![
        0x0a,
            0x00, 0x00,
            0x09,
                0x00, 0x04,
                0x6c, 0x69, 0x73, 0x74,
                0x00,
                0x00, 0x00, 0x00, 0x00,
        0x00
    ];

    // Test encoding.
    let mut dst = Vec::new();
    nbt.to_writer(&mut dst).unwrap();
    assert_eq!(&dst, &bytes);

    // Test decoding.
    let mut src = io::Cursor::new(bytes);
    let file = Blob::from_reader(&mut src).unwrap();
    assert_eq!(&file, &nbt);
}

#[test]
fn nbt_nested_list() {
    let mut nbt = Blob::new();
    let inner_one = Value::List(vec![Value::Short(1), Value::Short(2)]);
    let inner_two = Value::List(vec![Value::Float(0.25), Value::Float(0.75)]);
    nbt.insert("list", Value::List(vec![inner_one, inner_two]))
        .unwrap();

    #[rustfmt::skip]
    let bytes = vec![
        0x0a,
            0x00, 0x00,
            0x09,
                0x00, 0x04,
                0x6c, 0x69, 0x73, 0x74,
                0x09, // Also a list.
                0x00, 0x00, 0x00, 0x02,
                0x02, // First list has type short.
                0x00, 0x00, 0x00, 0x02,
                0x00, 0x01, 0x00, 0x02,
                0x05, // Second list has type float.
                0x00, 0x00, 0x00, 0x02,
                0x3e, 0x80, 0x00, 0x00,
                0x3f, 0x40, 0x00, 0x00,
        0x00
    ];

    // Test encoding.
    let mut dst = Vec::new();
    nbt.to_writer(&mut dst).unwrap();
    assert_eq!(&dst, &bytes);

    // Test decoding.
    let mut src = io::Cursor::new(bytes);
    let file = Blob::from_reader(&mut src).unwrap();
    assert_eq!(&file, &nbt);
}

#[test]
fn nbt_no_root() {
    let bytes = vec![0x00];
    // Will fail, because the root is not a compound.
    assert_eq!(
        Blob::from_reader(&mut io::Cursor::new(&bytes[..])),
        Err(Error::NoRootCompound)
    );
}

#[test]
fn nbt_no_end_tag() {
    #[rustfmt::skip]
    let bytes = vec![
        0x0a,
            0x00, 0x00,
            0x09,
                0x00, 0x04,
                0x6c, 0x69, 0x73, 0x74,
                0x01,
                0x00, 0x00, 0x00, 0x00
    ];

    // Will fail, because there is no end tag.
    assert_eq!(
        Blob::from_reader(&mut io::Cursor::new(&bytes[..])),
        Err(Error::IncompleteNbtValue)
    );
}

#[test]
fn nbt_invalid_id() {
    #[rustfmt::skip]
    let bytes = vec![
        0x0a,
            0x00, 0x00,
            0x0f, // No tag associated with 0x0f.
                0x00, 0x04,
                0x6c, 0x69, 0x73, 0x74,
                0x01,
        0x00
    ];
    assert_eq!(
        Blob::from_reader(&mut io::Cursor::new(&bytes[..])),
        Err(Error::InvalidTypeId(15))
    );
}

#[test]
fn nbt_invalid_list() {
    let mut nbt = Blob::new();
    let mut badlist = Vec::new();
    badlist.push(Value::Byte(1));
    badlist.push(Value::Short(1));
    // Will fail to insert, because the List is heterogeneous.
    assert_eq!(
        nbt.insert("list", Value::List(badlist)),
        Err(Error::HeterogeneousList)
    );
}

#[test]
fn nbt_bad_compression() {
    // These aren't in the zlib or gzip format, so they'll fail.
    let bytes = vec![0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
    assert!(Blob::from_gzip_reader(&mut io::Cursor::new(&bytes[..])).is_err());
    assert!(Blob::from_zlib_reader(&mut io::Cursor::new(&bytes[..])).is_err());
}

#[test]
fn nbt_compression() {
    // Create a non-trivial Blob.
    let mut nbt = Blob::new();
    nbt.insert("name", Value::String("Herobrine".to_string()))
        .unwrap();
    nbt.insert("health", Value::Byte(100)).unwrap();
    nbt.insert("food", Value::Float(20.0)).unwrap();
    nbt.insert("emeralds", Value::Short(12345)).unwrap();
    nbt.insert("timestamp", Value::Int(1424778774)).unwrap();

    // Test zlib encoding/decoding.
    let mut zlib_dst = Vec::new();
    nbt.to_zlib_writer(&mut zlib_dst).unwrap();
    let zlib_file = Blob::from_zlib_reader(&mut io::Cursor::new(zlib_dst)).unwrap();
    assert_eq!(&nbt, &zlib_file);

    // Test gzip encoding/decoding.
    let mut gzip_dst = Vec::new();
    nbt.to_gzip_writer(&mut gzip_dst).unwrap();
    let gz_file = Blob::from_gzip_reader(&mut io::Cursor::new(gzip_dst)).unwrap();
    assert_eq!(&nbt, &gz_file);
}

#[test]
fn nbt_bigtest() {
    let mut bigtest_file = File::open("tests/big1.nbt").unwrap();
    let bigtest = Blob::from_gzip_reader(&mut bigtest_file).unwrap();
    // This is a pretty indirect way of testing correctness.
    let mut dst = Vec::new();
    bigtest.to_writer(&mut dst).unwrap();
    assert_eq!(1544, dst.len());
}

#[test]
fn nbt_arrays() {
    let mut arrays_file = File::open("tests/arrays.nbt").unwrap();
    let arrays = Blob::from_reader(&mut arrays_file).unwrap();
    match &arrays["ia"] {
        &Value::IntArray(ref arr) => assert_eq!(&[-2, -1, 0, 1, 2], &**arr),
        _ => panic!("ia was not TAG_IntArray"),
    }

    match &arrays["ba"] {
        &Value::ByteArray(ref arr) => assert_eq!(&[-2, -1, 0, 1, 2], &**arr),
        _ => panic!("ba was not TAG_ByteArray"),
    }

    match &arrays["la"] {
        &Value::LongArray(ref arr) => assert_eq!(&[-2, -1, 0, 1, 2], &**arr),
        _ => panic!("la was not TAG_LongArray"),
    }
}

#[test]
#[cfg(feature = "serde")]
fn serde_blob() {
    use de::from_reader;
    use ser::to_writer;

    let mut nbt = Blob::new();
    nbt.insert("name", "Herobrine").unwrap();
    nbt.insert("health", 100i8).unwrap();
    nbt.insert("food", 20.0f32).unwrap();
    nbt.insert("emeralds", 12345i16).unwrap();
    nbt.insert("timestamp", 1424778774i32).unwrap();

    #[rustfmt::skip]
    let bytes = vec![
        0x0a,
            0x00, 0x00,
            0x08,
                0x00, 0x04,
                0x6e, 0x61, 0x6d, 0x65,
                0x00, 0x09,
                0x48, 0x65, 0x72, 0x6f, 0x62, 0x72, 0x69, 0x6e, 0x65,
            0x01,
                0x00, 0x06,
                0x68, 0x65, 0x61, 0x6c, 0x74, 0x68,
                0x64,
            0x05,
                0x00, 0x04,
                0x66, 0x6f, 0x6f, 0x64,
                0x41, 0xa0, 0x00, 0x00,
            0x02,
                0x00, 0x08,
                0x65, 0x6d, 0x65, 0x72, 0x61, 0x6c, 0x64, 0x73,
                0x30, 0x39,
            0x03,
                0x00, 0x09,
                0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, 0x6d, 0x70,
                0x54, 0xec, 0x66, 0x16,
        0x00
    ];

    // Roundtrip.

    let mut src = io::Cursor::new(bytes.clone());
    let file: Blob = from_reader(&mut src).unwrap();
    assert_eq!(&file, &nbt);
    let mut dst = Vec::new();
    to_writer(&mut dst, &nbt, None).unwrap();
    // When the preserve_order feature is not enabled,
    // we can only test if the decoded bytes match, since the HashMap does
    // not guarantee order (and so encoding is likely to be different, but
    // still correct).
    assert_eq!(&bytes.len(), &dst.len());
    #[cfg(feature = "preserve_order")]
    assert_eq!(&bytes, &dst);
}

#[test]
fn nbt_modified_utf8() {
    let mut nbt = Blob::new();
    // These strings are taken from the cesu8 documentation.
    nbt.insert("\u{10401}", "\0\0").unwrap();

    #[rustfmt::skip]
    let bytes = vec![
        0x0a,
            0x00, 0x00,
            0x08,
                0x00, 0x06,
                0xed, 0xa0, 0x81, 0xed, 0xb0, 0x81,
                0x00, 0x04,
                0xc0, 0x80, 0xc0, 0x80,
        0x00
    ];

    // Test encoding.
    let mut dst = Vec::new();
    nbt.to_writer(&mut dst).unwrap();
    assert_eq!(&dst, &bytes);

    // Test decoding.
    let mut src = io::Cursor::new(bytes);
    let file = Blob::from_reader(&mut src).unwrap();
    assert_eq!(&file, &nbt);
}

#[test]
fn nbt_sizes() {
    // Arbitarary values, covering most data types
    let mut subtree = Map::<String, Value>::new();
    subtree.insert("name".into(), "Herobrine".into());
    subtree.insert("health".into(), 100i8.into());
    subtree.insert("enormous".into(), 100i64.into());
    subtree.insert("food".into(), 20.0f32.into());
    subtree.insert("emeralds".into(), 12345i16.into());
    subtree.insert("timestamp".into(), 1424778774i32.into());
    subtree.insert(
        "list".into(),
        Value::List(vec![1, 2, 3, 4].into_iter().map(Value::Int).collect()),
    );

    let deeper_sub = Value::Compound(subtree.clone());

    subtree.insert("recursion".into(), deeper_sub);

    let subling_sub = Value::Compound(subtree.clone());
    let orig_compound = Value::Compound(subtree);

    // Here so this test covers every tag type
    let byte_array = Value::ByteArray((-127..127).collect());
    let int_array = Value::IntArray((0..128).collect());
    let long_array = Value::LongArray((0..512).collect());

    // Creating a blob that has weird nested compounds/lists/arrays
    // Intended to cover all possible tags and make sure recursions
    // handle nested types correctly.
    let mut root = Blob::new();
    root.insert("List-C", Value::List(vec![orig_compound, subling_sub]))
        .unwrap();
    root.insert("List-B", byte_array).unwrap();
    root.insert("List-I", int_array).unwrap();
    root.insert("List-L", long_array).unwrap();

    // Write out the blob
    let mut cursor = std::io::Cursor::new(vec![]);
    root.to_writer(&mut cursor).unwrap();

    assert_eq!(cursor.position() as usize, root.len_bytes());
}