brotli/enc/
utf8_util.rs

1#![allow(dead_code)]
2static kMinUTF8Ratio: super::util::floatX = 0.75 as super::util::floatX;
3
4fn BrotliParseAsUTF8(symbol: &mut i32, input: &[u8], size: usize) -> usize {
5    if input[0] as i32 & 0x80i32 == 0i32 {
6        *symbol = input[0] as i32;
7        if *symbol > 0i32 {
8            return 1usize;
9        }
10    }
11    if size > 1u32 as usize
12        && (input[0] as i32 & 0xe0i32 == 0xc0i32)
13        && (input[1] as i32 & 0xc0i32 == 0x80i32)
14    {
15        *symbol = (input[0] as i32 & 0x1fi32) << 6 | input[1] as i32 & 0x3fi32;
16        if *symbol > 0x7fi32 {
17            return 2usize;
18        }
19    }
20    if size > 2u32 as usize
21        && (input[0] as i32 & 0xf0i32 == 0xe0i32)
22        && (input[1] as i32 & 0xc0i32 == 0x80i32)
23        && (input[2] as i32 & 0xc0i32 == 0x80i32)
24    {
25        *symbol = (input[0] as i32 & 0xfi32) << 12
26            | (input[1] as i32 & 0x3fi32) << 6
27            | input[2] as i32 & 0x3fi32;
28        if *symbol > 0x7ffi32 {
29            return 3usize;
30        }
31    }
32    if size > 3u32 as usize
33        && (input[0] as i32 & 0xf8i32 == 0xf0i32)
34        && (input[1] as i32 & 0xc0i32 == 0x80i32)
35        && (input[2] as i32 & 0xc0i32 == 0x80i32)
36        && (input[3] as i32 & 0xc0i32 == 0x80i32)
37    {
38        *symbol = (input[0] as i32 & 0x7i32) << 18
39            | (input[1] as i32 & 0x3fi32) << 12
40            | (input[2] as i32 & 0x3fi32) << 6
41            | input[3] as i32 & 0x3fi32;
42        if *symbol > 0xffffi32 && (*symbol <= 0x10ffffi32) {
43            return 4usize;
44        }
45    }
46    *symbol = 0x110000i32 | input[0] as i32;
47    1usize
48}
49
50pub fn BrotliIsMostlyUTF8(
51    data: &[u8],
52    pos: usize,
53    mask: usize,
54    length: usize,
55    min_fraction: super::util::floatX,
56) -> i32 {
57    let mut size_utf8: usize = 0usize;
58    let mut i: usize = 0usize;
59    while i < length {
60        let mut symbol: i32 = 0;
61        let bytes_read: usize = BrotliParseAsUTF8(
62            &mut symbol,
63            &data[(pos.wrapping_add(i) & mask)..],
64            length.wrapping_sub(i),
65        );
66        i = i.wrapping_add(bytes_read);
67        if symbol < 0x110000i32 {
68            size_utf8 = size_utf8.wrapping_add(bytes_read);
69        }
70    }
71    if size_utf8 as (super::util::floatX) > min_fraction * length as (super::util::floatX) {
72        1i32
73    } else {
74        0i32
75    }
76}