tracepoint_decode 0.4.1

Rust API for decoding tracepoints
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

use core::fmt;
use core::str;

use crate::filters;

#[inline]
fn char_from_validated_u32(valid_ch32: u32) -> char {
    debug_assert!(!(0xD800..=0xDFFF).contains(&valid_ch32) && valid_ch32 < 0x110000);
    return unsafe { char::from_u32_unchecked(valid_ch32) };
}

#[inline]
fn str_from_validated_utf8(valid_utf8: &[u8]) -> &str {
    debug_assert!(core::str::from_utf8(valid_utf8).is_ok());
    return unsafe { str::from_utf8_unchecked(valid_utf8) };
}

// **** write_latin1_to

/// Writes a Latin-1-encoded string to a filter.
pub fn write_latin1_to<F: filters::Filter>(bytes: &[u8], filter: &mut F) -> fmt::Result {
    let len = bytes.len();
    let mut written_pos = 0;
    for pos in 0..len {
        let b = bytes[pos];
        if b <= 0x7F {
            // ASCII. Continue.
            continue;
        }

        // bytes[pos..] is not ASCII.

        // Flush the ASCII, if any.
        if written_pos < pos {
            // Validated: substring contains only ASCII.
            filter.write_str(str_from_validated_utf8(&bytes[written_pos..pos]))?;
        }

        filter.write_non_ascii(b as char)?;
        written_pos = pos + 1;
    }

    // Write any remaining ASCII. (Common case: the entire string.)
    return if written_pos < len {
        // Validated: substring contains only ASCII.
        filter.write_str(str_from_validated_utf8(&bytes[written_pos..]))
    } else {
        Ok(())
    };
}

// **** write_utf8_with_latin1_fallback_to

/// Writes a UTF-8-encoded string to a filter. If the string contains any invalid UTF-8 sequences,
/// the sequences are treated as Latin-1.
pub fn write_utf8_with_latin1_fallback_to<F: filters::Filter>(
    bytes: &[u8],
    filter: &mut F,
) -> fmt::Result {
    let len = bytes.len();
    let mut written_pos = 0;
    let mut pos = 0;
    while pos < len {
        // If this is valid UTF-8, update pos and continue to next iteration.
        // If this is not valid UTF-8, fall-through to the Latin1 case.
        let b0 = bytes[pos];

        if b0 <= 0x7F {
            // 0x00..0x7F: Valid UTF-8. Continue.
            pos += 1;
            continue;
        } else if b0 <= 0xBF {
            // Invalid lead byte. Fall-through.
        } else if b0 <= 0xDF {
            if len - pos >= 2 {
                let b1 = bytes[pos + 1];
                if 0x80 == (b1 & 0xC0) {
                    let ch = ((b0 & 0x1F) as u32) << 6 | ((b1 & 0x3F) as u32);
                    if 0x80 <= ch {
                        // Valid 2-byte UTF-8. Continue.
                        pos += 2;
                        continue;
                    }
                }
            }
        } else if b0 <= 0xEF {
            if len - pos >= 3 {
                let b1 = bytes[pos + 1];
                let b2 = bytes[pos + 2];
                if 0x80 == (b1 & 0xC0) && 0x80 == (b2 & 0xC0) {
                    let ch = ((b0 & 0x0F) as u32) << 12
                        | ((b1 & 0x3F) as u32) << 6
                        | ((b2 & 0x3F) as u32);
                    if 0x800 <= ch && !(0xD800..=0xDFFF).contains(&ch) {
                        // Valid 3-byte UTF-8. Continue.
                        pos += 3;
                        continue;
                    }
                }
            }
        } else if b0 <= 0xF4 {
            #[allow(clippy::collapsible_if)]
            // The symmetry seems helpful in understanding this code.
            if len - pos >= 4 {
                let b1 = bytes[pos + 1];
                let b2 = bytes[pos + 2];
                let b3 = bytes[pos + 3];
                if 0x80 == (b1 & 0xC0) && 0x80 == (b2 & 0xC0) && 0x80 == (b3 & 0xC0) {
                    let ch = ((b0 & 0x07) as u32) << 18
                        | ((b1 & 0x3F) as u32) << 12
                        | ((b2 & 0x3F) as u32) << 6
                        | ((b3 & 0x3F) as u32);
                    if (0x10000..=0x10FFFF).contains(&ch) {
                        // Valid 4-byte UTF-8. Continue.
                        pos += 4;
                        continue;
                    }
                }
            }
        }

        // bytes[pos..] is not valid UTF-8.

        // Flush the valid UTF-8, if any.
        if written_pos < pos {
            // Validated: substring contains only valid UTF-8.
            filter.write_str(str_from_validated_utf8(&bytes[written_pos..pos]))?;
        }

        // Treat bytes[pos] as Latin1 and move forward.
        filter.write_non_ascii(b0 as char)?;
        written_pos = pos + 1;
        pos = written_pos;
    }

    // Write any remaining valid UTF-8. (Common case: the entire string.)
    return if written_pos < len {
        filter.write_str(str_from_validated_utf8(&bytes[written_pos..]))
    } else {
        Ok(())
    };
}

// **** write_utf16_be_to, write_utf16_le_to

fn write_utf16_to<const BIG_ENDIAN: bool, F: filters::Filter>(
    bytes: &[u8],
    filter: &mut F,
) -> fmt::Result {
    let len = bytes.len();
    let mut pos = 0;
    while len - pos >= 2 {
        let high = if BIG_ENDIAN {
            u16::from_be_bytes(bytes[pos..pos + 2].try_into().unwrap())
        } else {
            u16::from_le_bytes(bytes[pos..pos + 2].try_into().unwrap())
        };
        pos += 2; // Consume the first code unit.

        let ch;
        if high <= 0x7F {
            // ASCII
            filter.write_ascii(high as u8)?;
            continue;
        } else if !(0xD800..=0xDFFF).contains(&high) {
            // Not ASCII, not a surrogate.
            ch = char_from_validated_u32(high as u32);
        } else if high >= 0xDC00 || len - pos < 2 {
            // Invalid or unpaired high surrogate.
            ch = char::REPLACEMENT_CHARACTER;
        } else {
            // Valid high surrogate. Possibly valid low surrogate.
            let low = if BIG_ENDIAN {
                u16::from_be_bytes(bytes[pos..pos + 2].try_into().unwrap())
            } else {
                u16::from_le_bytes(bytes[pos..pos + 2].try_into().unwrap())
            };

            ch = if !(0xDC00..=0xDFFF).contains(&low) {
                char::REPLACEMENT_CHARACTER // Unpaired high surrogate.
            } else {
                pos += 2; // Consume the second code unit.
                char_from_validated_u32(
                    (((high as u32 - 0xD800) << 10) | (low as u32 - 0xDC00)) + 0x10000,
                )
            };
        }

        filter.write_non_ascii(ch)?;
    }

    return Ok(());
}

/// Writes a UTF-16BE-encoded string to a filter. Invalid code units are replaced with the
/// replacement character.
pub fn write_utf16be_to<F: filters::Filter>(bytes: &[u8], filter: &mut F) -> fmt::Result {
    return write_utf16_to::<true, F>(bytes, filter);
}

/// Writes a UTF-16LE-encoded string to a filter. Invalid code units are replaced with the
/// replacement character.
pub fn write_utf16le_to<F: filters::Filter>(bytes: &[u8], filter: &mut F) -> fmt::Result {
    return write_utf16_to::<false, F>(bytes, filter);
}

// **** write_utf32_be_to, write_utf32_le_to

fn write_utf32_to<const BIG_ENDIAN: bool, F: filters::Filter>(
    bytes: &[u8],
    filter: &mut F,
) -> fmt::Result {
    let len = bytes.len();
    let mut pos = 0;
    while len - pos >= 4 {
        let ch32 = if BIG_ENDIAN {
            u32::from_be_bytes(bytes[pos..pos + 4].try_into().unwrap())
        } else {
            u32::from_le_bytes(bytes[pos..pos + 4].try_into().unwrap())
        };
        pos += 4; // Consume.

        let ch = char::from_u32(ch32).unwrap_or(char::REPLACEMENT_CHARACTER);
        filter.write_non_ascii(ch)?;
    }

    return Ok(());
}

/// Writes a UTF-32BE-encoded string to a filter. Invalid code units are replaced with the
/// replacement character.
pub fn write_utf32be_to<F: filters::Filter>(bytes: &[u8], filter: &mut F) -> fmt::Result {
    return write_utf32_to::<true, F>(bytes, filter);
}

/// Writes a UTF-32LE-encoded string to a filter. Invalid code units are replaced with the
/// replacement character.
pub fn write_utf32le_to<F: filters::Filter>(bytes: &[u8], filter: &mut F) -> fmt::Result {
    return write_utf32_to::<false, F>(bytes, filter);
}

#[cfg(test)]
mod tests {
    extern crate alloc;
    use alloc::string;

    use super::*;

    #[test]
    fn test_write_latin1_to() {
        fn check(expected: &str, input: &[u8]) {
            let mut actual = string::String::new();
            let mut actual_writer = filters::WriteFilter::new(&mut actual);
            write_latin1_to(input, &mut actual_writer).unwrap();
            assert_eq!(expected, actual);
        }

        check("", b"");
        check("A", b"A");
        check("A\u{80}", b"A\x80");
        check("\u{80}", b"\x80");
        check("\u{80}A", b"\x80A");
        check("Hello, world!", b"Hello, world!");
        check("Hello, \u{80}world!", b"Hello, \x80world!");
        check("Hello, \u{FF}", b"Hello, \xFF");
    }

    #[test]
    fn test_write_utf8_with_latin1_fallback_to() {
        fn check(expected: &str, input: &[u8]) {
            let mut actual = string::String::new();
            let mut actual_writer = filters::WriteFilter::new(&mut actual);
            write_utf8_with_latin1_fallback_to(input, &mut actual_writer).unwrap();
            assert_eq!(expected, actual);
        }

        fn check_u32(i: u32) {
            let mut buf4 = [0u8; 4];

            if let Some(ch) = char::from_u32(i) {
                // utf-8 encoding of valid Unicode code point should pass through unchanged.
                let s = ch.encode_utf8(&mut buf4[..]);
                let sb = s.as_bytes();

                check(s, sb);
            } else {
                // Invalid Unicode code points. Encode as UTF-8 anyway, then verify that
                // it is recognized as invalid and treated as Latin-1.
                debug_assert!(i >= 0x800); // There are no invalid code points below 0x800.
                debug_assert!(i <= 0x1FFFFF); // Should not be called after 0x1FFFFF.
                let len;
                if i <= 0xFFFF {
                    buf4[0] = (0xE0 | ((i >> 12) & 0x0F)) as u8;
                    buf4[1] = (0x80 | ((i >> 6) & 0x3F)) as u8;
                    buf4[2] = (0x80 | (i & 0x3F)) as u8;
                    len = 3;
                } else {
                    buf4[0] = (0xF0 | ((i >> 18) & 0x07)) as u8;
                    buf4[1] = (0x80 | ((i >> 12) & 0x3F)) as u8;
                    buf4[2] = (0x80 | ((i >> 6) & 0x3F)) as u8;
                    buf4[3] = (0x80 | (i & 0x3F)) as u8;
                    len = 4;
                }

                let mut str = string::String::new();
                for j in 0..len {
                    str.push(char::from_u32(buf4[j] as u32).unwrap());
                }
                check(&str, &buf4[..len]);
            }
        }

        // Check handling of valid and invalid code points that have been encoded into utf-8.
        for i in 0x0..0xFFFF {
            check_u32(i);
        }
        for i in (0x0..0x1F0000).step_by(0x10000) {
            check_u32(i);
            check_u32(i | 0xFFFF);
        }

        // Valid utf-8 should pass through unchanged.
        let valid_strings = [
            "",
            "Hello, world!",
            "\u{0}\u{7F}\u{80}\u{7FF}\u{800}\u{D7FF}\u{E000}\u{FFFF}\u{10000}\u{10FFFF}",
        ];
        for valid_string in valid_strings.iter() {
            check(valid_string, valid_string.as_bytes());
        }
    }

    #[test]
    fn test_write_utf16_to() {
        fn check(expected: &str, input_big_endian: &mut [u8]) {
            let mut actual = string::String::new();

            let mut actual_writer = filters::WriteFilter::new(&mut actual);
            write_utf16_to::<true, _>(input_big_endian, &mut actual_writer).unwrap();
            assert_eq!(expected, actual);

            for i in 0..(input_big_endian.len() / 2) {
                input_big_endian.swap(i * 2, i * 2 + 1);
            }

            actual.clear();
            let mut actual_writer = filters::WriteFilter::new(&mut actual);
            write_utf16_to::<false, _>(&input_big_endian, &mut actual_writer).unwrap();
            assert_eq!(expected, actual);
        }

        // Note: input is big-endian.
        check("", &mut []); // Empty.
        check("", &mut [99]); // Odd length should be ignored.
        check("0", &mut [0x00, 0x30]); // ASCII.
        check("0", &mut [0x00, 0x30, 99]); // Odd length should be ignored.
        check("\u{FF}", &mut [0x00, 0xFF]); // Non-ASCII.
        check("\u{100}", &mut [0x01, 0x00]); // Non-ASCII.
        check("\u{D7FF}", &mut [0xD7, 0xFF]); // Non-ASCII.
        check("\u{10000}", &mut [0xD8, 0x00, 0xDC, 0x00]); // Valid surrogate pair.
        check("\u{103FF}", &mut [0xD8, 0x00, 0xDF, 0xFF]); // Valid surrogate pair.
        check("\u{10FC00}", &mut [0xDB, 0xFF, 0xDC, 0x00]); // Valid surrogate pair.
        check("\u{10FFFF}", &mut [0xDB, 0xFF, 0xDF, 0xFF]); // Valid surrogate pair.

        check("\u{FFFD}", &mut [0xD8, 0x00]); // Unpaired high surrogate.
        check("\u{FFFD}0", &mut [0xDB, 0xFF, 0x00, 0x30]); // Unpaired high surrogate followed by '0'.
        check("\u{FFFD}", &mut [0xDC, 0x00]); // Unpaired low surrogate.
        check("\u{FFFD}0", &mut [0xDF, 0xFF, 0x00, 0x30]); // Unpaired low surrogate followed by '0'.
    }

    #[test]
    fn test_write_utf32_to() {
        fn check(expected: &str, input_big_endian: &mut [u8]) {
            let mut actual = string::String::new();

            let mut actual_writer = filters::WriteFilter::new(&mut actual);
            write_utf32_to::<true, _>(input_big_endian, &mut actual_writer).unwrap();
            assert_eq!(expected, actual);

            for i in 0..(input_big_endian.len() / 4) {
                input_big_endian.swap(i * 4, i * 4 + 3);
                input_big_endian.swap(i * 4 + 1, i * 4 + 2);
            }

            actual.clear();
            let mut actual_writer = filters::WriteFilter::new(&mut actual);
            write_utf32_to::<false, _>(&input_big_endian, &mut actual_writer).unwrap();
            assert_eq!(expected, actual);
        }

        // Note: input is big-endian.
        check("", &mut []); // Empty.
        check("", &mut [99]); // Odd length should be ignored.
        check("", &mut [0, 99]); // Odd length should be ignored.
        check("", &mut [0, 0, 99]); // Odd length should be ignored.
        check("0", &mut [0x00, 0x00, 0x00, 0x30]); // ASCII.
        check("0", &mut [0x00, 0x00, 0x00, 0x30, 99]); // Odd length should be ignored.
        check("0", &mut [0x00, 0x00, 0x00, 0x30, 0, 99]); // Odd length should be ignored.
        check("0", &mut [0x00, 0x00, 0x00, 0x30, 0, 0, 99]); // Odd length should be ignored.
        check("\u{FF}", &mut [0x00, 0x00, 0x00, 0xFF]); // Non-ASCII.
        check("\u{100}", &mut [0x00, 0x00, 0x01, 0x00]);
        check("\u{D7FF}", &mut [0x00, 0x00, 0xD7, 0xFF]);
        check("\u{10000}", &mut [0x00, 0x01, 0x00, 0x00]);
        check("\u{10FFFF}", &mut [0x00, 0x10, 0xFF, 0xFF]); // Limit

        // Invalid char handling.
        check("\u{FFFD}", &mut [0x00, 0x11, 0x00, 0x00]);
        check(
            "\u{FFFD}0",
            &mut [0x00, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30],
        );
        check("\u{FFFD}", &mut [0x00, 0x00, 0xDC, 0x00]);
        check(
            "\u{FFFD}0",
            &mut [0x01, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x30],
        );
    }
}