resb/
binary.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
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
// This file is part of ICU4X. For terms of use, please see the file
// called LICENSE at the top level of the ICU4X source tree
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ).

//! The `binary` module provides a `serde` deserializer for the binary resource
//! bundle format as well as a means of writing resource bundles to the binary
//! file format.

mod deserializer;
mod header;
pub use self::deserializer::from_bytes;

#[cfg(feature = "serialize")]
mod serializer;
#[cfg(feature = "serialize")]
pub use self::serializer::Serializer;

use core::{fmt, slice::SliceIndex};

use self::header::BinHeader;

/// Gets the endianness of a binary resource bundle's data.
pub fn determine_endianness(resb: &[u8]) -> Result<Endianness, BinaryDeserializerError> {
    let header = BinHeader::try_from(resb)?;

    Ok(header.repr_info.endianness)
}

/// The `BinIndex` struct represents details of the written bundle.
///
/// The index is present from [`FormatVersion::V1_1`] on.
#[allow(dead_code)]
struct BinIndex {
    /// The number of 32-bit fields written in the index, including the field
    /// count.
    field_count: u32,

    /// The offset of the end of the key block in 32-bit values from the
    /// beginning of the body.
    keys_end: u32,

    /// The offset of the end of the resources block in 32-bit values from the
    /// beginning of the body.
    resources_end: u32,

    /// The offset of the end of the bundle in 32-bit values from the beginning
    /// of the body.
    ///
    /// In all versions through [`FormatVersion::V3_0`], this is always the same
    /// as `resources_end`.
    bundle_end: u32,

    /// The number of entries in the largest table in the bundle.
    largest_table_entry_count: u32,

    /// Attributes describing resolution of external resources.
    ///
    /// Present from [`FormatVersion::V1_2`] on.
    bundle_attributes: Option<u32>,

    /// The offset of the end of the 16-bit data block in 32-bit values from the
    /// beginning of the body.
    ///
    /// Present from [`FormatVersion::V2_0`] on.
    data_16_bit_end: Option<u32>,

    /// The resource pool bundle checksum.
    ///
    /// Present from [`FormatVersion::V2_0`] on when the bundle either is a pool
    /// bundle or uses a pool bundle for sharing resources.
    pool_checksum: Option<u32>,
}

/// Adds convenience properties to an `enum` represented as a primitive type,
/// including conversions to and from the primitive type.
macro_rules! primitive_enum {
    ($type:ty, $(#[$meta:meta])* $vis:vis enum $name:ident {
        $($(#[$variant_meta:meta])* $variant:ident = $value:expr,)*
    }) => {
        // At a minimum, `$meta` is needed to allow for doc comments on enums
        // created via this macro.
        $(#[$meta])*
        #[repr($type)]
        $vis enum $name {
            // At a minimum, `$variant_meta` is needed to allow for doc comments
            // on variants created via this macro.
            $($(#[$variant_meta])* $variant = $value,)*
        }

        impl From<$name> for $type {
            fn from(v: $name) -> Self {
                v as $type
            }
        }

        impl TryFrom<$type> for $name {
            type Error = BinaryDeserializerError;

            fn try_from(value: $type) -> Result<Self, Self::Error> {
                match value {
                    $(x if x == $name::$variant as $type => Ok($name::$variant),)*
                    _ => Err(BinaryDeserializerError::invalid_data(
                        concat!("unrecognized value for ", stringify!($name))
                    )),
                }
            }
        }
    }
}

primitive_enum!(
    u8,
    /// The endianness used to write a resource bundle.
    #[derive(Clone, Copy, Debug, PartialEq)]
    // The resource bundle format doesn't support any sort of mixed-endian or
    // middle-endian encodings.
    #[allow(clippy::exhaustive_enums)]
    #[allow(missing_docs)]
    pub enum Endianness {
        Little = 0,
        Big = 1,
    }
);

primitive_enum!(
    u8,
    /// A family of character sets used to represent the characters of key strings.
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    enum CharsetFamily {
        /// The ASCII family of character sets, such as ASCII, latin1, and
        /// UTF-8.
        Ascii = 0,

        /// The EBCDIC family of character sets, such as EBCDIC and UTF-EBCDIC.
        ///
        /// The EBCDIC family is currently unsupported by this crate both for
        /// serialization and deserialization of binary bundles.
        Ebcdic = 1,
    }
);

primitive_enum!(
    u16,
    #[derive(Clone, Copy, Debug, Eq, PartialEq)]
    /// The type of a resource representation within a binary resource bundle.
    ///
    /// The representation is distinct from the resource type as presented to
    /// consumers. Some resource types may have multiple possible
    /// representations, depending on the [`FormatVersion`] and—in the case of
    /// collections—number and type of their constituent resources.
    enum ResourceReprType {
        /// A string resource. Not yet supported.
        _String = 0,

        /// A raw binary resource.
        ///
        /// Consists of a 32-bit length value `n` followed by `n` arbitrary
        /// bytes.
        Binary = 1,

        /// A table resource for bundles with fewer than `0x1_0000` keys.
        ///
        /// Consists of a 16-bit length value `n` followed by `n` 16-bit key
        /// offsets from the beginning of the key block, 0 or 16 bits of padding
        /// (in order to align the table representation so far to a 32-bit
        /// boundary), and `n` 32-bit resource descriptors. For details on the
        /// representation of resource descriptors, see [`ResDescriptor`].
        ///
        /// The `i`th entry in the resulting table is a a pair of the `i`th key
        /// and the `i`th resource.
        Table = 2,

        /// An alias resource. Not yet supported.
        _Alias = 3,

        /// A table resource for bundles with `0x1_0000` or more keys. Not yet
        /// supported.
        _Table32 = 4,

        /// A 16-bit table resource. Not yet supported.
        Table16 = 5,

        /// A 16-bit string resource for [`FormatVersion::V2_0`] and later.
        ///
        /// Consists of a UTF-16 string with length marked in one of the
        /// following ways:
        ///
        /// - For strings of length `[1..40]` characters, there is no length
        ///   marker and the string must be null-terminated (i.e., by two `0`
        ///   bytes in a row).
        ///
        /// - For strings of length `n` in the range `(40..0x3ef)`, the string
        ///   is preceded by a single UTF-16 low surrogate composed as
        ///   `0xdc00 & n`.
        ///
        /// - For strings of length `n` in the range `[0x3ef..0x10_0000)`, the
        ///   string is preceded by a length marker consisting of a UTF-16 low
        ///   surrogate followed by a 16-bit value, composed as
        ///   `[0xdfef + (n >> 0x10), n & 0xffff]`.
        ///
        /// - For strings of length `n` in the range `[0x10_0000,
        ///   0x1_0000_0000)`, the string is preceded by a length marker
        ///   consisting of a UTF-16 low surrogate followed by two 16-bit length
        ///   values, composed as `[0xdfff, n >> 0x10, n & 0xffff]`.
        ///
        /// Strings of greater length than those described above may not be
        /// stored in binary bundles.
        ///
        /// These length markers can be reliably detected, as UTF-16 low
        /// surrogates may not legally appear without a preceding high surrogate
        /// in a UTF-16 string.
        StringV2 = 6,

        /// A 28-bit integer resource.
        ///
        /// Consists solely of the resource descriptor with the 28 bits of the
        /// integer in place of an offset.
        ///
        /// The signedness of 28-bit integers is not indicated in the resource
        /// bundle itself. Consumers are expected to explicitly request a signed
        /// or unsigned integer. In order to
        Int = 7,

        /// A general array resource.
        ///
        /// Consists of a 32-bit length value `n` followed by `n` 32-bit
        /// resource descriptors. For more details on the representation of
        /// resource descriptors, see [`ResDescriptor`].
        Array = 8,

        /// A 16-bit array resource.
        ///
        /// Consists of a 16-bit length value `n` followed by `n` 16-bit offsets
        /// from the beginning of the 16-bit data block.
        ///
        /// As of [`FormatVersion::V3_0`], only `StringV2` representations can
        /// be fully stored in the 16-bit data block. As such, only `StringV2`
        /// resources can appear in an `Array16`.
        Array16 = 9,

        /// An integer array resource.
        ///
        /// Consists of a 32-bit length value `n` followed by `n` 32-bit integer
        /// values.
        ///
        /// Note that these are not integer _resources_, but rather full 32-bit
        /// integers.
        IntVector = 14,
    }
);

/// A `FormatVersion` represents a specific binary file format used for
/// representing resource bundles.
///
/// A partial [specification] of each format version is present in the ICU4C
/// source code.
///
/// [specification]: https://github.com/unicode-org/icu/blob/main/icu4c/source/common/uresdata.h
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
// We use `u32` representation to enforce correct sizing for structs containing
// a `FormatVersion`.
//
// Note that order of declaration is important for partial ordering.
#[repr(u32)]
enum FormatVersion {
    V1_0,
    V1_1,
    V1_2,
    V1_3,
    V2_0,
    V3_0,
}

/// The `ResDescriptor` struct represents a typed pointer to a resource body
/// within a binary resource bundle.
///
/// It is represented within the binary bundle as a 4-bit resource type in the
/// most significant nibble of a 32-bit integer with a 28-bit unsigned offset
/// in the remaining bits. The offset is interpreted as a count of 32-bit
/// values from the start of the body.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
struct ResDescriptor {
    resource_type: ResourceReprType,
    value: u32,
}

impl ResDescriptor {
    /// Makes a new resource descriptor with the given type and 28-bit value.
    pub const fn new(resource_type: ResourceReprType, value: u32) -> Self {
        Self {
            resource_type,
            value,
        }
    }

    /// Makes a new resource descriptor with the given type and no body.
    pub const fn _new_empty(resource_type: ResourceReprType) -> Self {
        Self::new(resource_type, 0)
    }

    /// Returns `true` if the described resource is empty.
    pub fn is_empty(&self) -> bool {
        self.value == 0
    }

    /// Gets the offset to the described 16-bit resource in bytes.
    ///
    /// The type of the resource representation is not verified. Consumers are
    /// expected to call the function appropriate to the resource type they are
    /// querying.
    fn value_as_16_bit_offset(&self) -> usize {
        // When the value of a resource descriptor is an offset, it is counted
        // in units dependent on the resource type (16-bit values for 16-bit
        // resources, 32-bit values for 32-bit resources). Translate that into
        // bytes for consumers.
        (self.value as usize) * core::mem::size_of::<u16>()
    }

    /// Gets the offset to the described 32-bit resource in bytes.
    ///
    /// The type of the resource representation is not verified. Consumers are
    /// expected to call the function appropriate to the resource type they are
    /// querying.
    fn value_as_32_bit_offset(&self) -> usize {
        // When the value of a resource descriptor is an offset, it is counted
        // in units dependent on the resource type (16-bit values for 16-bit
        // resources, 32-bit values for 32-bit resources). Translate that into
        // bytes for consumers.
        (self.value as usize) * core::mem::size_of::<u32>()
    }

    /// Gets the value of the resource descriptor as a signed integer.
    ///
    /// The type of the resource representation is not verified. Consumers are
    /// expected to call the function appropriate to the resource type they are
    /// querying.
    fn value_as_signed_int(&self) -> i32 {
        ((self.value as i32) << 4) >> 4
    }

    /// Gets the value of the resource descriptor as an unsigned integer.
    ///
    /// The type of the resource representation is not verified. Consumers are
    /// expected to call the function appropriate to the resource type they are
    /// querying.
    fn value_as_unsigned_int(&self) -> u32 {
        self.value
    }

    /// Gets the resource type of the described resource.
    pub fn resource_type(&self) -> ResourceReprType {
        self.resource_type
    }
}

/// The `Error` type provides basic error handling for deserialization of binary
/// resource bundles.
#[derive(Clone, Copy, Debug)]
pub struct BinaryDeserializerError {
    kind: ErrorKind,
    message: &'static str,
}

impl BinaryDeserializerError {
    fn invalid_data(message: &'static str) -> Self {
        Self {
            kind: ErrorKind::InvalidData,
            message,
        }
    }

    fn resource_type_mismatch(message: &'static str) -> Self {
        Self {
            kind: ErrorKind::ResourceTypeMismatch,
            message,
        }
    }

    fn unsupported_format(message: &'static str) -> Self {
        Self {
            kind: ErrorKind::UnsupportedFormat,
            message,
        }
    }

    fn unknown(message: &'static str) -> Self {
        Self {
            kind: ErrorKind::Unknown,
            message,
        }
    }
}

impl fmt::Display for BinaryDeserializerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let description = match self.kind {
            ErrorKind::InvalidData => "Invalid resource bundle data",
            ErrorKind::ResourceTypeMismatch => "Resource did not match expected data type",
            ErrorKind::UnsupportedFormat => "Unsupported resource bundle format",
            ErrorKind::Unknown => "Unknown error",
        };

        write!(f, "{description}: {}", self.message)
    }
}

#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
enum ErrorKind {
    ResourceTypeMismatch,
    InvalidData,
    UnsupportedFormat,

    Unknown,
}

/// Gets a subset of the given `u8` slice based on the specified index with
/// bounds checking.
///
/// Returns the subslice. Returns an error if the index is not valid for the
/// input.
fn get_subslice<I>(input: &[u8], index: I) -> Result<&[u8], BinaryDeserializerError>
where
    I: SliceIndex<[u8], Output = [u8]>,
{
    input
        .get(index)
        .ok_or(BinaryDeserializerError::invalid_data(
            "unexpected end of input",
        ))
}

/// Reads the first two bytes of the input and interprets them as a `u16` with
/// native endianness.
///
/// Returns the `u16` and a slice containing all input after the interpreted
/// bytes. Returns an error if the input is of insufficient length.
fn read_u16(input: &[u8]) -> Result<(u16, &[u8]), BinaryDeserializerError> {
    // Safe to unwrap at the end of this because `try_into()` for arrays will
    // only fail if the slice is the wrong size.
    #[allow(clippy::unwrap_used)]
    let bytes = get_subslice(input, ..core::mem::size_of::<u16>())?
        .try_into()
        .unwrap();
    let value = u16::from_le_bytes(bytes);

    let rest = get_subslice(input, core::mem::size_of::<u16>()..)?;
    Ok((value, rest))
}