ctb_formats_perl/
unicode.rs1#[allow(
23 unused_imports,
24 clippy::wildcard_imports,
25 reason = "Standard workspace module prelude"
26)]
27use crate::utilities::*;
28
29pub fn perl_utf8_decode(data: &[u8]) -> Result<String> {
30 let mut s = String::new();
31 let mut i = 0usize;
32 while i < data.len() {
33 let b1 = *data.get(i).context("Index out of bounds")?;
34 if b1 <= 0x7f {
35 s.push(char::from(b1));
36 i = i.saturating_add(1);
37 } else if (0x80..=0xbf).contains(&b1) {
38 s.push('\u{FFFD}');
39 i = i.saturating_add(1);
40 } else {
41 let n = match b1 {
42 0xc0..=0xdf => 1,
43 0xe0..=0xef => 2,
44 0xf0..=0xf7 => 3,
45 0xf8..=0xfb => 4,
46 0xfc..=0xfd => 5,
47 0xfe => 6,
48 0xff => 12,
49 _ => unreachable!(),
50 };
51
52 let mut k = 0usize;
53 for offset in 1..=n {
54 let next_idx = i.saturating_add(offset);
55 if next_idx >= data.len() {
56 break;
57 }
58 let next_byte =
59 *data.get(next_idx).context("Index out of bounds")?;
60 if (0x80..=0xbf).contains(&next_byte) {
61 k = offset;
62 } else {
63 break;
64 }
65 }
66
67 if k > 0 {
68 let mut cp_valid = false;
69 let mut cp = 0u32;
70 if k == n {
71 if n == 1 {
72 let b2 = *data
73 .get(i.saturating_add(1))
74 .context("Index out of bounds")?;
75 cp = ((u32::from(b1) & 0x1f) << 6)
76 | (u32::from(b2) & 0x3f);
77 if cp > 0x7f {
78 cp_valid = true;
79 }
80 } else if n == 2 {
81 let b2 = *data
82 .get(i.saturating_add(1))
83 .context("Index out of bounds")?;
84 let b3 = *data
85 .get(i.saturating_add(2))
86 .context("Index out of bounds")?;
87 cp = ((u32::from(b1) & 0x0f) << 12)
88 | ((u32::from(b2) & 0x3f) << 6)
89 | (u32::from(b3) & 0x3f);
90 if cp > 0x7ff && !(0xd800..=0xdfff).contains(&cp) {
91 cp_valid = true;
92 }
93 } else if n == 3 {
94 let b2 = *data
95 .get(i.saturating_add(1))
96 .context("Index out of bounds")?;
97 let b3 = *data
98 .get(i.saturating_add(2))
99 .context("Index out of bounds")?;
100 let b4 = *data
101 .get(i.saturating_add(3))
102 .context("Index out of bounds")?;
103 cp = ((u32::from(b1) & 0x07) << 18)
104 | ((u32::from(b2) & 0x3f) << 12)
105 | ((u32::from(b3) & 0x3f) << 6)
106 | (u32::from(b4) & 0x3f);
107 if cp > 0xffff && cp <= 0x10ffff {
108 cp_valid = true;
109 }
110 }
111 }
112
113 if cp_valid {
114 if let Some(c) = std::char::from_u32(cp) {
115 s.push(c);
116 } else {
117 s.push('\u{FFFD}');
118 }
119 } else {
120 s.push('\u{FFFD}');
121 }
122
123 i = i.saturating_add(k.saturating_add(1));
124 } else {
125 s.push('\u{FFFD}');
126 i = i.saturating_add(1);
127 }
128 }
129 }
130 Ok(s)
131}
132
133#[cfg(test)]
134#[allow(
135 clippy::panic,
136 clippy::expect_used,
137 clippy::unwrap_used,
138 clippy::unwrap_in_result,
139 clippy::panic_in_result_fn,
140 clippy::indexing_slicing,
141 clippy::arithmetic_side_effects,
142 reason = "Standard repository test boilerplate"
143)]
144mod tests {
145 use super::*;
146
147 #[crate::ctb_test]
148 fn test_perl_utf8_decode_valid() {
149 assert_eq!(perl_utf8_decode(b"hello world").unwrap(), "hello world");
150 assert_eq!(perl_utf8_decode(b"").unwrap(), "");
151 }
152
153 #[crate::ctb_test]
154 fn test_perl_utf8_decode_invalid_continuation() {
155 assert_eq!(perl_utf8_decode(b"\xc1\x92").unwrap(), "\u{FFFD}");
156 assert_eq!(perl_utf8_decode(b"\xc0\x80").unwrap(), "\u{FFFD}");
157 }
158
159 #[crate::ctb_test]
160 fn test_perl_utf8_decode_greedy() {
161 let input = b"\xff\xff\xff\xff\xff\xff\xff\xff\x9f";
162 assert_eq!(perl_utf8_decode(input).unwrap(), "\u{FFFD}".repeat(8));
163
164 assert_eq!(perl_utf8_decode(b"\xff\x9f").unwrap().chars().count(), 1);
165 assert_eq!(
166 perl_utf8_decode(b"\xff\x9f\x9f").unwrap().chars().count(),
167 1
168 );
169
170 let mut input12 = vec![0xff];
171 input12.extend(std::iter::repeat_n(0x9f, 12));
172 assert_eq!(perl_utf8_decode(&input12).unwrap().chars().count(), 1);
173
174 let mut input13 = vec![0xff];
175 input13.extend(std::iter::repeat_n(0x9f, 13));
176 assert_eq!(perl_utf8_decode(&input13).unwrap().chars().count(), 2);
177
178 let mut input5 = vec![0xfc];
179 input5.extend(std::iter::repeat_n(0x9f, 5));
180 assert_eq!(perl_utf8_decode(&input5).unwrap().chars().count(), 1);
181
182 let mut input6 = vec![0xfc];
183 input6.extend(std::iter::repeat_n(0x9f, 6));
184 assert_eq!(perl_utf8_decode(&input6).unwrap().chars().count(), 2);
185
186 assert_eq!(perl_utf8_decode(b"\xffA").unwrap().chars().count(), 2);
187 assert_eq!(perl_utf8_decode(b"\xff\xff").unwrap().chars().count(), 2);
188 assert_eq!(perl_utf8_decode(b"\xe0\xff").unwrap().chars().count(), 2);
189 }
190}