gvdb 0.10.0

Implementation of the glib gvdb file format
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
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
#![allow(unused)]

use crate::read::{File, HashItemType, HashTable};
use crate::write::{FileWriter, HashTableBuilder};
use glib::value::ToValue;
pub use matches::assert_matches;
pub use pretty_assertions::{assert_eq, assert_ne, assert_str_eq};
use serde::Deserialize;
use std::borrow::Cow;
use std::cmp::{max, min};
use std::io::{Cursor, Read, Write};
use std::path::{Path, PathBuf};
use std::sync::LazyLock;
use zvariant::DynamicType;

pub(crate) static TEST_FILE_DIR: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("test-data"));
pub(crate) static TEST_FILE_1: LazyLock<PathBuf> =
    LazyLock::new(|| TEST_FILE_DIR.join("test1.gvdb"));
pub(crate) static TEST_FILE_2: LazyLock<PathBuf> =
    LazyLock::new(|| TEST_FILE_DIR.join("test2.gvdb"));
pub(crate) static TEST_FILE_3: LazyLock<PathBuf> =
    LazyLock::new(|| TEST_FILE_DIR.join("test3.gresource"));
pub(crate) static TEST_FILE_4: LazyLock<PathBuf> =
    LazyLock::new(|| TEST_FILE_DIR.join("test4.gvdb"));
pub(crate) static GRESOURCE_DIR: LazyLock<PathBuf> =
    LazyLock::new(|| TEST_FILE_DIR.join("gresource"));
pub(crate) static GRESOURCE_XML: LazyLock<PathBuf> =
    LazyLock::new(|| GRESOURCE_DIR.join("test3.gresource.xml"));

pub(crate) const SIMPLE_FILE_KEY: &str = "test";
pub(crate) const SIMPLE_FILE_VALUE: u32 = 0xabca_bcab_u32;

fn write_byte_row(
    f: &mut dyn std::io::Write,
    offset: usize,
    bytes_per_row: usize,
    bytes: &[u8],
) -> std::io::Result<()> {
    write!(f, "{offset:08X}")?;

    for (index, byte) in bytes.iter().enumerate() {
        if index % 4 == 0 {
            write!(f, " ")?;
        }

        write!(f, " {byte:02X}")?;
    }

    let bytes_per_row = max(bytes_per_row, bytes.len());
    for index in bytes.len()..bytes_per_row {
        if index % 4 == 0 {
            write!(f, " ")?;
        }

        write!(f, "   ")?;
    }

    write!(f, "  ")?;

    for byte in bytes {
        if byte.is_ascii_alphanumeric() || byte.is_ascii_whitespace() || byte.is_ascii_punctuation()
        {
            write!(f, "{}", *byte as char)?;
        } else {
            write!(f, ".")?;
        }
    }

    writeln!(f)
}

fn write_byte_rows(
    f: &mut dyn std::io::Write,
    center_offset: usize,
    additional_rows_top: usize,
    additional_rows_bottom: usize,
    bytes_per_row: usize,
    bytes: &[u8],
) -> std::io::Result<()> {
    let center_row_num = center_offset / bytes_per_row;
    let start_row = center_row_num - min(center_row_num, additional_rows_top);
    // We add 1 because we can add partial rows at the end
    let last_row = min(
        additional_rows_bottom + center_row_num,
        bytes.len() / bytes_per_row + 1,
    );
    let row_count = last_row - start_row;

    for row in 0..row_count {
        let offset_start = (start_row + row) * bytes_per_row;
        let offset_end = min(bytes.len(), offset_start + bytes_per_row);

        write_byte_row(
            f,
            offset_start,
            bytes_per_row,
            &bytes[offset_start..offset_end],
        )?;
    }

    Ok(())
}

pub fn assert_gvariant_eq(a: &[u8], b: &[u8], context: &str) {
    // Decode gvariant using glib, and diff using print()
    let a_var = glib::Variant::from_data::<glib::Variant, _>(a);
    let b_var = glib::Variant::from_data::<glib::Variant, _>(b);

    let a_str = a_var.print(true);
    let b_str = b_var.print(true);

    if a_str != b_str {
        let mut bytes_a: Vec<u8> = Vec::new();
        write_byte_rows(&mut bytes_a, 0, 0, usize::MAX, 16, a);

        let mut bytes_b: Vec<u8> = Vec::new();
        write_byte_rows(&mut bytes_b, 0, 0, usize::MAX, 16, b);

        assert_eq!(
            format!(
                "{}\n{}",
                a_var.print(true).as_str(),
                std::str::from_utf8(&bytes_a).unwrap()
            ),
            format!(
                "{}\n{}",
                b_var.print(true).as_str(),
                std::str::from_utf8(&bytes_b).unwrap()
            ),
            "{}",
            context
        );
    }
}

#[track_caller]
pub fn assert_bytes_eq(a: &[u8], b: &[u8], context: &str) {
    const WIDTH: usize = 16;
    const EXTRA_ROWS_TOP: usize = 8;
    const EXTRA_ROWS_BOTTOM: usize = 4;

    let max_len = max(a.len(), b.len());

    for index in 0..max_len {
        let a_byte = a.get(index);
        let b_byte = b.get(index);

        if a_byte.is_none() || b_byte.is_none() || a_byte.unwrap() != b_byte.unwrap() {
            let mut a_bytes_buf = Vec::new();
            write_byte_rows(
                &mut a_bytes_buf,
                index,
                EXTRA_ROWS_TOP,
                EXTRA_ROWS_BOTTOM,
                WIDTH,
                a,
            )
            .unwrap();
            let str_a = String::from_utf8(a_bytes_buf).unwrap();

            let mut b_bytes_buf = Vec::new();
            write_byte_rows(
                &mut b_bytes_buf,
                index,
                EXTRA_ROWS_TOP,
                EXTRA_ROWS_BOTTOM,
                WIDTH,
                b,
            )
            .unwrap();
            let str_b = String::from_utf8(b_bytes_buf).unwrap();

            assert_str_eq!(str_a, str_b, "{}", context);
        }
    }
}

pub fn byte_compare_gvdb_file(a: &File, b: &File, context: &str) {
    assert_eq!(a.header, b.header);

    let a_hash = a.hash_table().unwrap();
    let b_hash = b.hash_table().unwrap();
    byte_compare_gvdb_hash_table(&a_hash, &b_hash, context);
}

fn byte_compare_file(file: &File, reference_path: &Path) {
    let mut reference_file = std::fs::File::open(reference_path).unwrap();
    let mut reference_data = Vec::new();
    reference_file.read_to_end(&mut reference_data).unwrap();

    assert_bytes_eq(
        &reference_data,
        file.data.as_ref(),
        &format!("Byte comparing with file '{}'", reference_path.display()),
    );
}

pub fn byte_compare_file_1(file: &File) {
    byte_compare_file(file, &TEST_FILE_1);
}

pub fn assert_is_file_1(file: &File) {
    let table = file.hash_table().unwrap();
    let mut names = table.keys();
    assert_eq!(names.len(), 1);
    assert_eq!(&names.next().unwrap().unwrap(), "root_key");

    let value = table.get_value("root_key").unwrap();
    assert_matches!(value, zvariant::Value::Structure(_));
    assert_eq!(value.value_signature(), "(uus)");

    let tuple = zvariant::Structure::try_from(value).unwrap();
    let fields = tuple.into_fields();

    assert_eq!(u32::try_from(&fields[0]), Ok(1234));
    assert_eq!(u32::try_from(&fields[1]), Ok(98765));
    assert_eq!(<&str>::try_from(&fields[2]), Ok("TEST_STRING_VALUE"));
}

pub fn byte_compare_file_2(file: &File) {
    byte_compare_file(file, &TEST_FILE_2);
}

pub fn assert_is_file_2(file: &File) {
    let table = file.hash_table().unwrap();
    let names = table.keys().collect::<Result<Vec<_>, _>>().unwrap();
    assert_eq!(names.len(), 2);
    assert_eq!(names[0], "string");
    assert_eq!(names[1], "table");

    let str_value = table.get_value("string").unwrap();
    assert_matches!(str_value, zvariant::Value::Str(_));
    assert_eq!(<&str>::try_from(&str_value), Ok("test string"));

    let sub_table = table.get_hash_table("table").unwrap();
    let sub_table_names = sub_table.keys().collect::<Result<Vec<_>, _>>().unwrap();
    assert_eq!(sub_table_names.len(), 1);
    assert_eq!(sub_table_names[0], "int");

    let int_value = sub_table.get_value("int").unwrap();
    assert_eq!(u32::try_from(int_value), Ok(42));
}

pub fn byte_compare_file_3(file: &File) {
    let ref_root = File::from_file(&TEST_FILE_3).unwrap();
    byte_compare_gvdb_file(&ref_root, file, "Comparing file 3");
}

pub fn assert_is_file_3(file: &File) {
    let table = file.hash_table().unwrap();
    let mut names = table.keys().collect::<Result<Vec<_>, _>>().unwrap();
    names.sort();
    let reference_names = vec![
        "/",
        "/gvdb/",
        "/gvdb/rs/",
        "/gvdb/rs/test/",
        "/gvdb/rs/test/icons/",
        "/gvdb/rs/test/icons/scalable/",
        "/gvdb/rs/test/icons/scalable/actions/",
        "/gvdb/rs/test/icons/scalable/actions/send-symbolic.svg",
        "/gvdb/rs/test/json/",
        "/gvdb/rs/test/json/test.json",
        "/gvdb/rs/test/online-symbolic.svg",
        "/gvdb/rs/test/test.css",
    ];
    assert_eq!(names, reference_names);

    #[derive(Clone, zvariant::Type, zvariant::OwnedValue, serde::Deserialize)]
    struct GResourceData {
        size: u32,
        flags: u32,
        content: Vec<u8>,
    }

    let svg1: GResourceData = table
        .get::<GResourceData>("/gvdb/rs/test/online-symbolic.svg")
        .unwrap();

    assert_eq!(svg1.size, 1390);
    assert_eq!(svg1.flags, 0);
    assert_eq!(svg1.size as usize, svg1.content.len() - 1);

    // Ensure the last byte is zero because of zero-padding defined in the format
    assert_eq!(svg1.content[svg1.content.len() - 1], 0);
    let svg1_str = std::str::from_utf8(&svg1.content[0..svg1.content.len() - 1]).unwrap();
    assert!(svg1_str.starts_with(
        &(r#"<?xml version="1.0" encoding="UTF-8"?>"#.to_string()
            + "\n\n"
            + r#"<svg xmlns="http://www.w3.org/2000/svg" height="16px""#)
    ));

    let svg2 = table
        .get_value("/gvdb/rs/test/icons/scalable/actions/send-symbolic.svg")
        .unwrap();
    assert_matches!(svg2, zvariant::Value::Structure(_));
    let svg2_fields = zvariant::Structure::try_from(svg2).unwrap().into_fields();

    let svg2_size = u32::try_from(&svg2_fields[0]).unwrap();
    let svg2_flags = u32::try_from(&svg2_fields[1]).unwrap();
    let svg2_content: Vec<u8> = <Vec<u8>>::try_from(svg2_fields[2].try_clone().unwrap()).unwrap();

    assert_eq!(svg2_size, 345);
    assert_eq!(svg2_flags, 1);
    let mut decoder = flate2::read::ZlibDecoder::new(&*svg2_content);
    let mut svg2_data = Vec::new();
    decoder.read_to_end(&mut svg2_data).unwrap();

    // Ensure the last byte is *not* zero and len is not one bigger than specified because
    // compressed data is not zero-padded
    assert_ne!(svg2_data[svg2_data.len() - 1], 0);
    assert_eq!(svg2_size as usize, svg2_data.len());
    let svg2_str = std::str::from_utf8(&svg2_data).unwrap();

    let mut svg2_reference = String::new();
    std::fs::File::open(GRESOURCE_DIR.join("icons/scalable/actions/send-symbolic.svg"))
        .unwrap()
        .read_to_string(&mut svg2_reference)
        .unwrap();
    assert_str_eq!(svg2_str, svg2_reference);

    let json =
        zvariant::Structure::try_from(table.get_value("/gvdb/rs/test/json/test.json").unwrap())
            .unwrap()
            .into_fields();
    let json_size: u32 = (&json[0]).try_into().unwrap();
    let json_flags: u32 = (&json[1]).try_into().unwrap();
    let json_content: Vec<u8> = json[2].try_clone().unwrap().try_into().unwrap();

    // Ensure the last byte is zero because of zero-padding defined in the format
    assert_eq!(json_content[json_content.len() - 1], 0);
    assert_eq!(json_size as usize, json_content.len() - 1);
    let json_str = std::str::from_utf8(&json_content[0..json_content.len() - 1]).unwrap();

    assert_eq!(json_flags, 0);
    assert_str_eq!(
        json_str,
        r#"["test_string",42,{"bool":true}]"#.to_string() + "\n"
    );
}

pub fn byte_compare_file_4(file: &File) {
    let ref_root = File::from_file(&TEST_FILE_4).unwrap();
    byte_compare_gvdb_file(&ref_root, file, "Comparing file 4");
}

pub(crate) fn new_empty_file() -> File<'static> {
    let writer = FileWriter::new();
    let table_builder = HashTableBuilder::new();
    let data = Vec::new();
    let mut cursor = Cursor::new(data);
    writer.write_with_table(table_builder, &mut cursor).unwrap();

    File::from_bytes(Cow::Owned(cursor.into_inner())).unwrap()
}

pub(crate) fn new_simple_file(big_endian: bool) -> File<'static> {
    let writer = if big_endian {
        FileWriter::for_big_endian()
    } else {
        FileWriter::new()
    };

    let mut table_builder = HashTableBuilder::new();
    table_builder
        .insert(SIMPLE_FILE_KEY, SIMPLE_FILE_VALUE)
        .unwrap();
    let data = Vec::new();
    let mut cursor = Cursor::new(data);
    writer.write_with_table(table_builder, &mut cursor).unwrap();

    File::from_bytes(Cow::Owned(cursor.into_inner())).unwrap()
}

#[track_caller]
pub(crate) fn byte_compare_gvdb_hash_table(a: &HashTable, b: &HashTable, context: &str) {
    assert_eq!(a.header, b.header);

    let mut keys_a = a.keys().collect::<Result<Vec<_>, _>>().unwrap();
    let mut keys_b = b.keys().collect::<Result<Vec<_>, _>>().unwrap();
    keys_a.sort();
    keys_b.sort();
    assert_eq!(keys_a, keys_b);

    for key in keys_a {
        let item_a = a.get_hash_item(&key).unwrap();
        let item_b = b.get_hash_item(&key).unwrap();

        let data_a = a.file.dereference(item_a.value_ptr(), 1).unwrap();
        let data_b = b.file.dereference(item_b.value_ptr(), 1).unwrap();

        match item_a.typ().unwrap() {
            HashItemType::Value => {
                assert_gvariant_eq(
                    data_a,
                    data_b,
                    &format!("Comparing gvariant values with key '{key}'"),
                );
                assert_bytes_eq(
                    data_a,
                    data_b,
                    &format!("Comparing values with key '{key}'"),
                );
            }
            HashItemType::HashTable => byte_compare_gvdb_hash_table(
                &a.get_hash_table(&key).expect(context),
                &b.get_hash_table(&key).expect(context),
                &format!("{context}: Comparing hash tables with key '{key}'"),
            ),
            HashItemType::Container => {
                // We don't compare containers, only their length
                if data_a.len() != data_b.len() {
                    // The lengths should not be different. For context we will compare the data
                    assert_bytes_eq(
                        data_a,
                        data_b,
                        &format!("Containers with key '{key}' have different lengths"),
                    );
                }
            }
        }

        assert_eq!(item_a.hash_value(), item_b.hash_value());
        assert_eq!(item_a.key_size(), item_b.key_size());
        assert_eq!(item_a.typ().unwrap(), item_b.typ().unwrap());
        assert_eq!(item_a.value_ptr().size(), item_b.value_ptr().size());
    }
}

#[test]
fn assert_bytes_eq1() {
    assert_bytes_eq(&[1, 2, 3], &[1, 2, 3], "test");
}

#[test]
fn assert_bytes_eq2() {
    // b is exactly 16 bytes long to test "b is too small" panic
    assert_bytes_eq(
        b"help i am stuck in a test case",
        b"help i am stuck in a test case",
        "test",
    );
}

#[test]
#[should_panic]
fn assert_bytes_eq_fail1() {
    assert_bytes_eq(&[1, 2, 4], &[1, 2, 3], "test");
}

#[test]
#[should_panic]
fn assert_bytes_eq_fail2() {
    assert_bytes_eq(&[1, 2, 3, 4], &[1, 2, 3], "test");
}

#[test]
#[should_panic]
fn assert_bytes_eq_fail3() {
    assert_bytes_eq(&[1, 2, 3], &[1, 2, 3, 4], "test");
}

#[test]
#[should_panic]
fn assert_bytes_eq_fail4() {
    // b is exactly 16 bytes long to test "b is too small" panic
    assert_bytes_eq(
        b"help i am stuck in a test case",
        b"help i am stuck in a test cas",
        "test",
    );
}