Debian Codegraph @

native/common/jp_encoding.cpp

raw symbols SCIP
1 /*****************************************************************************
2 Licensed under the Apache License, Version 2.0 (the "License");
3 you may not use this file except in compliance with the License.
4 You may obtain a copy of the License at
5
6 http://www.apache.org/licenses/LICENSE-2.0
7
8 Unless required by applicable law or agreed to in writing, software
9 distributed under the License is distributed on an "AS IS" BASIS,
10 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 See the License for the specific language governing permissions and
12 limitations under the License.
13
14 See NOTICE file for details.
15 *****************************************************************************/
16 #include "jp_encoding.h"
17
18 // These encoders handle all the codes expected to be passed between
19 // Java and Python assuming they both generate compliant codings. However,
20 // this code does not handle miscodings very well. The current behavior
21 // is to simply terminate the string at the bad code without producing
22 // a warning. I can add errors, but I am not sure how I would test it
23 // as both java and python will produce an error if I tried to force a
24 // bad encoding on them. Thus I am going to leave this with the truncation
25 // behavior for now.
26 //
27 // Examples of bad encodings:
28 // - non-utf8 such as extended ascii passed from python
29 // - encoded forbidden utf passed from python
30 // - truncated surrogate codes passed from java
31 //
32 // The secondary problem is that string conversion is part of exception
33 // handling. And the last thing we want to do is throw an exception
34 // while trying to convert an exception string while reporting. That
35 // would completely redirect the user. Thus truncation seems like
36 // a sane policy unless we can verify all the potential edge cases.
37 //
38 // Alternatively we could make them a bit more permissive and
39 // try to automatically correct bad encodings by recognizing
40 // extend ASCII, etc. But this is potentially complex and
41 // has the downside that we can't test it currently.
42
43 JPEncoding::~JPEncoding()
44 = default;
45
46 // char* to stream from
47 // https://stackoverflow.com/questions/7781898/get-an-istream-from-a-char
48
49 struct membuf : std::streambuf
50 {
51
52 membuf(char* begin, char* end)
53 {
54 setg(begin, begin, end);
55 }
56 } ;
57
58 // Convert a string from one encoding to another.
59 // Currently we use this to transcribe from utf-8 to java-utf-8 and back.
60 // It could do other encodings, but would need to be generalized to
61 // a template to handle wider character sets.
62
63 std::string transcribe(const char* in, size_t len,
64 const JPEncoding& sourceEncoding,
65 const JPEncoding& targetEncoding)
66 {
67 // ASCII bypass
68 bool ascii = true;
69 for (size_t i = 0; i < len; ++i)
70 {
71 if (in[i]&0x80 || in[i] == 0)
72 {
73 ascii = false;
74 break;
75 }
76 }
77 if (ascii)
78 {
79 return {in, len};
80 }
81
82 // Convert input to istream source
83 membuf sbuf(const_cast<char*> (in), const_cast<char*> (in + len));
84 std::istream inStream(&sbuf);
85
86 // Create an output stream with reserve
87 std::string out;
88 out.reserve(len + 8);
89 std::ostringstream outStream(out);
90
91 while (!inStream.eof())
92 {
93 unsigned int coding = sourceEncoding.fetch(inStream);
94 if (coding == (unsigned int) - 1)
95 {
96 if (inStream.eof())
97 break;
98 // Truncate bad strings for now.
99 return outStream.str();
100 }
101
102 targetEncoding.encode(outStream, coding);
103 }
104 return outStream.str();
105 }
106
107 //**************************************************************
108
109 // Encode a 21 bit code point as UTF-8
110 //
111 // There are 4 encodings used
112 //
113 // Range Encoding
114 // 0x000000-0x00007F
115 // 0xxxxxxx
116 // bits 6543210
117 //
118 // 0x000080-0x0007FF
119 // 110xxxxx 10xxxxxx
120 // 1
121 // bits 09876 543210
122 //
123 // 0x000800-0x000FFFF (excluding 0xD800-0xDFFF)
124 // 1110xxxx 10xxxxxx 10xxxxxx
125 // 1111 11
126 // bits 5432 109876 543210
127 //
128 // 0x100000-0x10FFFF
129 // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
130 // 211 111111 11
131 // bits 098 765432 109876 543210
132 //
133 // The encoding process is simply to add the constant portion encoding
134 // the byte position, then shifting to get the lowest bits, the masking
135 // off the required bits.
136 //
137
138 void JPEncodingUTF8::encode(std::ostream& out, unsigned int c) const
139 {
140 if (c < 0x80)
141 {
142 // encode 1
143 out.put(char(c & 0xff));
144 } else if (c < 0x800)
145 {
146 // encode 2
147 out.put(char(0xc0 + ((c >> 6)&0x1f)));
148 out.put(char(0x80 + ((c >> 0)&0x3f)));
149 } else if (c < 0x10000) // Note 0xd800-0xdfff are not valid codes
150 {
151 // encode 3
152 out.put(char(0xe0 + ((c >> 12)&0x0f)));
153 out.put(char(0x80 + ((c >> 6)&0x3f)));
154 out.put(char(0x80 + ((c >> 0)&0x3f)));
155 } else if (c < 0x110000)
156 {
157 // encode 4
158 out.put(char(0xf0 + ((c >> 18)&0x07)));
159 out.put(char(0x80 + ((c >> 12)&0x3f)));
160 out.put(char(0x80 + ((c >> 6)&0x3f)));
161 out.put(char(0x80 + ((c >> 0)&0x3f)));
162 }
163 }
164
165 // Retrieve a 21 unicode code point from a stream of bytes.
166 //
167 // This is the reverse of the process of encoding.
168 // We must first locate the position encoding bits from the
169 // top of the byte, then pull off the encoded bits. The
170 // final code point is the sum of each set of encoded bits.
171 //
172
173 unsigned int JPEncodingUTF8::fetch(std::istream& in) const
174 {
175 unsigned int c0 = in.get();
176 if (in.eof()) return -1;
177
178 // 1 byte code
179 if ((c0 & 0x80) == 0)
180 return c0;
181
182 unsigned int c1 = in.get();
183 if (in.eof()) return -1;
184
185 // 2 byte code
186 if ((c0 & 0xe0) == 0xc0)
187 {
188 if ((c1 & 0xc0) == 0x80)
189 return ((c0 & 0x1f) << 6) + (c1 & 0x3f);
190 else
191 return -1; // bad 2 byte format
192 }
193
194 unsigned int c2 = in.get();
195 if (in.eof()) return -1;
196
197 // 3 byte code
198 if ((c0 & 0xf0) == 0xe0)
199 {
200 if ((c1 & 0xc0) == 0x80 && (c2 & 0xc0) == 0x80)
201 return ((c0 & 0xf) << 12) + ((c1 & 0x3f) << 6) + (c2 & 0x3f);
202 return -1;
203 }
204
205 unsigned int c3 = in.get();
206 if (in.eof()) return -1;
207
208 // 4 byte code
209 if ((c0 & 0xf8) == 0xf0)
210 {
211 if ((c1 & 0xc0) == 0x80 && (c2 & 0xc0) == 0x80 && (c3 & 0xc0) == 0x80)
212 return ((c0 & 0xf) << 18) + ((c1 & 0x3f) << 12) + ((c2 & 0x3f) << 6) + (c3 & 0x3f);
213 return -1;
214 }
215 return -1;
216 }
217
218
219 // Encode a 21 code point into a Java UTF char stream.
220 //
221 // Java uses 16 bit characters to represent a string, but this is not
222 // sufficient to hold all 21 bits of valid unicode chars. Thus to
223 // encode the upper bits java uses UTF-16 encoding internally. Unfortunately,
224 // when java converts the 16 bit string into 8 bit encoding, it does not
225 // decode the string first. As a result we receive not UTF-8 but rather
226 // doublely encoded UTF-16/UTF-8. Further UTF-16 encoding requires
227 // not just simple byte packing but also an offset of the bits. Thus this
228 // process becomes a real mess. Also a special code point is required for
229 // embedded nulls.
230 //
231 // There are 4 encodings used
232 //
233 // Range Encoding
234 // 0x000001-0x00007F
235 // 0xxxxxxx
236 // bits 6543210
237 //
238 // 0x000080-0x0007FF, special code 0
239 // 110xxxxx 10xxxxxx
240 // 1
241 // bits 09876 543210
242 //
243 // 0x000800-0x000D7FF or 0x00DFFF-0x000FFFF
244 // 1110xxxx 10xxxxxx 10xxxxxx
245 // 1111 11
246 // bits 5432 109876 543210
247 //
248 // 0x100000-0x10FFFF (called surgate codes in UTF-16)
249 // (doublely encoded 6 byte code point)
250 // first subtract 0x10000 which reduces the range by 2 bits
251 // 11101101 1010xxxx 10xxxxxx 11101101 1011xxxx 10xxxxxx
252 // 1111 111111
253 // bits 9876 543210 9876 543210
254 //
255 // The encoding process is simply to add the constant portion encoding
256 // the byte position, then shifting to get the lowest bits, the masking
257 // off the required bits. The exception being the surgate codes which
258 // require an offset then a bit pack.
259 //
260 // 4 byte unicode or coding a 3 point in the invalid range followed by anything other
261 // than 3 byte encoding (in the invalid range) is a coding error.
262 //
263
264 void JPEncodingJavaUTF8::encode(std::ostream& out, unsigned int c) const
265 {
266 if (c == 0)
267 {
268 // encode 0 as 2
269 out.put(char(0xc0));
270 out.put(char(0x80));
271 } else if (c < 0x80)
272 {
273 // encode 1
274 out.put(char(c & 0xff));
275 } else if (c < 0x800)
276 {
277 // encode 2
278 out.put(char(0xc0 + ((c >> 6)&0x1f)));
279 out.put(char(0x80 + ((c >> 0)&0x3f)));
280 } else if (c < 0xd800 || (c >= 0xe000 && c < 0x10000))
281 {
282 // encode 3
283 out.put(char(0xe0 + char((c >> 12)&0x0f)));
284 out.put(char(0x80 + char((c >> 6)&0x3f)));
285 out.put(char(0x80 + char((c >> 0)&0x3f)));
286 } else if (c < 0x110000)
287 {
288 c = c - 0x10000;
289
290 // encode 6
291 out.put(char(0xed));
292 out.put(char(0xa0 + ((c >> 16)&0xf)));
293 out.put(char(0x80 + ((c >> 10)&0x3f)));
294 out.put(char(0xed));
295 out.put(char(0xb0 + ((c >> 6)&0xf)));
296 out.put(char(0x80 + ((c >> 0)&0x3f)));
297 }
298 }
299
300 // Decoding is the reverse of encoding, but there
301 // is a special gotcha because the position encoding bits
302 // are not unique. Both the 3 and 6 points codes share
303 // the position encoding 0xED for the first byte.
304 //
305 // The unicde packing solves this by removing a portion of
306 // the encoding such that 0xD800-0xDFFF are not valid unicode code
307 // points. Thus these invalid code points are used to represent
308 // the upper and lower encoding for the extended unicode
309 // code points.
310 //
311 // Thus the procedure is decode the top 3 and check to see if
312 // it is in a valid range. If it isn't then decode the
313 // second set of 3. So long as the bytes are coded through the
314 // java encoder it will be valid. However, a user could
315 // manually encode a string with an invalid coding.
316 //
317
318 unsigned int JPEncodingJavaUTF8::fetch(std::istream& in) const
319 {
320 unsigned int out = 0;
321 unsigned int c0 = in.get();
322 if (in.eof()) return -1;
323
324 if ((c0 & 0x80) == 0)
325 return c0;
326
327 unsigned int c1 = in.get();
328 if (in.eof()) return -1;
329
330 if ((c0 & 0xe0) == 0xc0)
331 {
332 if ((c1 & 0xc0) == 0x80)
333 return ((c0 & 0x1f) << 6) + (c1 & 0x3f);
334 else
335 return -1; // bad 2 byte format
336 }
337
338 unsigned int c2 = in.get();
339 if (in.eof()) return -1;
340
341 if ((c0 & 0xf0) == 0xe0 && (c1 & 0xc0) == 0x80 && (c2 & 0xc0) == 0x80)
342 {
343 out = ((c0 & 0xf) << 12) + ((c1 & 0x3f) << 6) + (c2 & 0x3f);
344 // 0xD800-0xDF00 are surrogate codes for 6 byte encoding
345 // High code is 0xD800-0xDBFF, Low code is 0xDC00-0xDFFF
346
347 // Plain old code if between 0x0000-0xD7FF, 0xE000-0xFFFF
348 if ((out & 0xf800) != 0xd800)
349 return out;
350 } else
351 {
352 return -1;
353 }
354
355 // Surrogate code should be followed by 3 byte utf8 code
356 unsigned int next = in.peek();
357 if (next == (unsigned int) (-1) || (next & 0xf0) != 0xe0)
358 return out; // unpaired surrogate error.
359
360 // Grab the low word for surrogate code
361 c0 = in.get();
362
363 // Note: we should technically check to see what the first byte is here.
364 // for a valid code point it must be 0xED. But if the user has
365 // manually encoded an invalid string there is not much we can
366 // do as we already are pulling bytes for the next character assuming
367 // it was a valid coding. We don't really want to throw an exception
368 // because this routine can happen at any point that string is being
369 // passed, including reporting of an exception.
370 //
371 // Manually encoding invalid code points and asking them to be
372 // converted is undefined behavior, but I don't know the path
373 // to nethack.
374
375 c1 = in.get();
376 c2 = in.get();
377 next = ((c0 & 0xf) << 12) + ((c1 & 0x3f) << 6) + (c2 & 0x3f);
378 if (in.eof())
379 return -1;
380 unsigned int q1 = (out & 0x3ff);
381 unsigned int q2 = (next & 0x3ff);
382 return 0x10000 + (q1 << 10) + q2;
383 }
384