Debian Codegraph @

libuna/libuna_unicode_character.c

raw symbols SCIP
1 /*
2 * Unicode character functions
3 *
4 * Copyright (C) 2008-2024, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <types.h>
24
25 #include "libuna_base64_stream.h"
26 #include "libuna_codepage_iso_8859_2.h"
27 #include "libuna_codepage_iso_8859_3.h"
28 #include "libuna_codepage_iso_8859_4.h"
29 #include "libuna_codepage_iso_8859_5.h"
30 #include "libuna_codepage_iso_8859_6.h"
31 #include "libuna_codepage_iso_8859_7.h"
32 #include "libuna_codepage_iso_8859_8.h"
33 #include "libuna_codepage_iso_8859_9.h"
34 #include "libuna_codepage_iso_8859_10.h"
35 #include "libuna_codepage_iso_8859_13.h"
36 #include "libuna_codepage_iso_8859_14.h"
37 #include "libuna_codepage_iso_8859_15.h"
38 #include "libuna_codepage_iso_8859_16.h"
39 #include "libuna_codepage_koi8_r.h"
40 #include "libuna_codepage_koi8_u.h"
41 #include "libuna_codepage_mac_arabic.h"
42 #include "libuna_codepage_mac_celtic.h"
43 #include "libuna_codepage_mac_centraleurroman.h"
44 #include "libuna_codepage_mac_croatian.h"
45 #include "libuna_codepage_mac_cyrillic.h"
46 #include "libuna_codepage_mac_dingbats.h"
47 #include "libuna_codepage_mac_farsi.h"
48 #include "libuna_codepage_mac_gaelic.h"
49 #include "libuna_codepage_mac_greek.h"
50 #include "libuna_codepage_mac_icelandic.h"
51 #include "libuna_codepage_mac_inuit.h"
52 #include "libuna_codepage_mac_roman.h"
53 #include "libuna_codepage_mac_romanian.h"
54 #include "libuna_codepage_mac_russian.h"
55 #include "libuna_codepage_mac_symbol.h"
56 #include "libuna_codepage_mac_thai.h"
57 #include "libuna_codepage_mac_turkish.h"
58 #include "libuna_codepage_mac_ukrainian.h"
59 #include "libuna_codepage_windows_874.h"
60 #include "libuna_codepage_windows_932.h"
61 #include "libuna_codepage_windows_936.h"
62 #include "libuna_codepage_windows_949.h"
63 #include "libuna_codepage_windows_950.h"
64 #include "libuna_codepage_windows_1250.h"
65 #include "libuna_codepage_windows_1251.h"
66 #include "libuna_codepage_windows_1252.h"
67 #include "libuna_codepage_windows_1253.h"
68 #include "libuna_codepage_windows_1254.h"
69 #include "libuna_codepage_windows_1255.h"
70 #include "libuna_codepage_windows_1256.h"
71 #include "libuna_codepage_windows_1257.h"
72 #include "libuna_codepage_windows_1258.h"
73 #include "libuna_definitions.h"
74 #include "libuna_libcerror.h"
75 #include "libuna_types.h"
76 #include "libuna_unicode_character.h"
77 #include "libuna_unused.h"
78
79 /* Valid directly encoded characters: A-Z, a-z, 0-9, '\', '(', ')', ',', '-', '.', '/', ':', '?'
80 * Valid directly encoded whitespace: '\t', '\n', '\r', ' '
81 * Valid optional directly encoded characters: '!', '"', '#', '$', '%', '&', '*', ';', '<', '=', '>', '@', '[', ']', '^', '_', '`', '{', '|', '}'
82 */
83 uint8_t libuna_unicode_character_utf7_valid_directly_encoded_character[ 256 ] = {
84 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
86 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1,
87 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
88 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
89 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
90 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
91 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
97 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
98 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
99 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
100
101 /* Valid UTF-7 base64 characters: A-Z, a-z, 0-9, '+' and '/'
102 */
103 uint8_t libuna_unicode_character_utf7_valid_base64_character[ 256 ] = {
104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1,
107 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0,
108 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
109 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
110 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
111 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
116 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
117 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
118 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
119 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
120
121 /* Determines the size of a byte stream character from an Unicode character
122 * Adds the size to the byte stream character size value
123 * Returns 1 if successful, 0 if the byte stream character is valid but not supported since it requires special handling or -1 on error
124 */
125 int libuna_unicode_character_size_to_byte_stream(
126 libuna_unicode_character_t unicode_character,
127 int codepage,
128 size_t *byte_stream_character_size,
129 libcerror_error_t **error )
130 {
131 static char *function = "libuna_unicode_character_size_to_byte_stream";
132 size_t safe_byte_stream_character_size = 0;
133 int result = 1;
134
135 if( byte_stream_character_size == NULL )
136 {
137 libcerror_error_set(
138 error,
139 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
140 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
141 "%s: invalid byte stream character size.",
142 function );
143
144 return( -1 );
145 }
146 safe_byte_stream_character_size = *byte_stream_character_size;
147
148 switch( codepage )
149 {
150 case LIBUNA_CODEPAGE_ASCII:
151 case LIBUNA_CODEPAGE_ISO_8859_1:
152 case LIBUNA_CODEPAGE_ISO_8859_2:
153 case LIBUNA_CODEPAGE_ISO_8859_3:
154 case LIBUNA_CODEPAGE_ISO_8859_4:
155 case LIBUNA_CODEPAGE_ISO_8859_5:
156 case LIBUNA_CODEPAGE_ISO_8859_6:
157 case LIBUNA_CODEPAGE_ISO_8859_7:
158 case LIBUNA_CODEPAGE_ISO_8859_8:
159 case LIBUNA_CODEPAGE_ISO_8859_9:
160 case LIBUNA_CODEPAGE_ISO_8859_10:
161 case LIBUNA_CODEPAGE_ISO_8859_11:
162 case LIBUNA_CODEPAGE_ISO_8859_13:
163 case LIBUNA_CODEPAGE_ISO_8859_14:
164 case LIBUNA_CODEPAGE_ISO_8859_15:
165 case LIBUNA_CODEPAGE_ISO_8859_16:
166 case LIBUNA_CODEPAGE_KOI8_R:
167 case LIBUNA_CODEPAGE_KOI8_U:
168 case LIBUNA_CODEPAGE_MAC_ARABIC:
169 case LIBUNA_CODEPAGE_MAC_CELTIC:
170 case LIBUNA_CODEPAGE_MAC_CENTRALEURROMAN:
171 case LIBUNA_CODEPAGE_MAC_CROATIAN:
172 case LIBUNA_CODEPAGE_MAC_CYRILLIC:
173 case LIBUNA_CODEPAGE_MAC_DINGBATS:
174 case LIBUNA_CODEPAGE_MAC_FARSI:
175 case LIBUNA_CODEPAGE_MAC_GAELIC:
176 case LIBUNA_CODEPAGE_MAC_GREEK:
177 case LIBUNA_CODEPAGE_MAC_ICELANDIC:
178 case LIBUNA_CODEPAGE_MAC_INUIT:
179 case LIBUNA_CODEPAGE_MAC_ROMAN:
180 case LIBUNA_CODEPAGE_MAC_ROMANIAN:
181 case LIBUNA_CODEPAGE_MAC_RUSSIAN:
182 case LIBUNA_CODEPAGE_MAC_TURKISH:
183 case LIBUNA_CODEPAGE_MAC_UKRAINIAN:
184 case LIBUNA_CODEPAGE_WINDOWS_874:
185 case LIBUNA_CODEPAGE_WINDOWS_1250:
186 case LIBUNA_CODEPAGE_WINDOWS_1251:
187 case LIBUNA_CODEPAGE_WINDOWS_1252:
188 case LIBUNA_CODEPAGE_WINDOWS_1253:
189 case LIBUNA_CODEPAGE_WINDOWS_1254:
190 case LIBUNA_CODEPAGE_WINDOWS_1255:
191 case LIBUNA_CODEPAGE_WINDOWS_1256:
192 case LIBUNA_CODEPAGE_WINDOWS_1257:
193 case LIBUNA_CODEPAGE_WINDOWS_1258:
194 safe_byte_stream_character_size += 1;
195 break;
196
197 case LIBUNA_CODEPAGE_MAC_SYMBOL:
198 result = libuna_codepage_mac_symbol_unicode_character_size_to_byte_stream(
199 unicode_character,
200 &safe_byte_stream_character_size,
201 error );
202 break;
203
204 case LIBUNA_CODEPAGE_MAC_THAI:
205 result = libuna_codepage_mac_thai_unicode_character_size_to_byte_stream(
206 unicode_character,
207 &safe_byte_stream_character_size,
208 error );
209 break;
210
211 case LIBUNA_CODEPAGE_WINDOWS_932:
212 result = libuna_codepage_windows_932_unicode_character_size_to_byte_stream(
213 unicode_character,
214 &safe_byte_stream_character_size,
215 error );
216 break;
217
218 case LIBUNA_CODEPAGE_WINDOWS_936:
219 result = libuna_codepage_windows_936_unicode_character_size_to_byte_stream(
220 unicode_character,
221 &safe_byte_stream_character_size,
222 error );
223 break;
224
225 case LIBUNA_CODEPAGE_WINDOWS_949:
226 result = libuna_codepage_windows_949_unicode_character_size_to_byte_stream(
227 unicode_character,
228 &safe_byte_stream_character_size,
229 error );
230 break;
231
232 case LIBUNA_CODEPAGE_WINDOWS_950:
233 result = libuna_codepage_windows_950_unicode_character_size_to_byte_stream(
234 unicode_character,
235 &safe_byte_stream_character_size,
236 error );
237 break;
238
239 default:
240 libcerror_error_set(
241 error,
242 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
243 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
244 "%s: unsupported codepage: %d.",
245 function,
246 codepage );
247
248 return( -1 );
249 }
250 if( result == -1 )
251 {
252 libcerror_error_set(
253 error,
254 LIBCERROR_ERROR_DOMAIN_RUNTIME,
255 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
256 "%s: unable to determine byte stream character size.",
257 function );
258
259 return( -1 );
260 }
261 *byte_stream_character_size = safe_byte_stream_character_size;
262
263 return( result );
264 }
265
266 /* Copies an Unicode character from a byte stream
267 * Returns 1 if successful, 0 if the byte stream character is valid but not supported since it requires special handling or -1 on error
268 */
269 int libuna_unicode_character_copy_from_byte_stream(
270 libuna_unicode_character_t *unicode_character,
271 const uint8_t *byte_stream,
272 size_t byte_stream_size,
273 size_t *byte_stream_index,
274 int codepage,
275 libcerror_error_t **error )
276 {
277 static char *function = "libuna_unicode_character_copy_from_byte_stream";
278 libuna_unicode_character_t safe_unicode_character = 0;
279 size_t safe_byte_stream_index = 0;
280 uint8_t byte_stream_character = 0;
281 int result = 1;
282
283 if( unicode_character == NULL )
284 {
285 libcerror_error_set(
286 error,
287 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
288 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
289 "%s: invalid Unicode character.",
290 function );
291
292 return( -1 );
293 }
294 if( byte_stream == NULL )
295 {
296 libcerror_error_set(
297 error,
298 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
299 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
300 "%s: invalid byte stream.",
301 function );
302
303 return( -1 );
304 }
305 if( byte_stream_size > (size_t) SSIZE_MAX )
306 {
307 libcerror_error_set(
308 error,
309 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
310 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
311 "%s: invalid byte stream size value exceeds maximum.",
312 function );
313
314 return( -1 );
315 }
316 if( byte_stream_index == NULL )
317 {
318 libcerror_error_set(
319 error,
320 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
321 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
322 "%s: invalid byte stream index.",
323 function );
324
325 return( -1 );
326 }
327 safe_byte_stream_index = *byte_stream_index;
328
329 if( safe_byte_stream_index >= byte_stream_size )
330 {
331 libcerror_error_set(
332 error,
333 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
334 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
335 "%s: byte stream too small.",
336 function );
337
338 return( -1 );
339 }
340 byte_stream_character = byte_stream[ safe_byte_stream_index ];
341
342 switch( codepage )
343 {
344 case LIBUNA_CODEPAGE_ASCII:
345 if( byte_stream_character < 0x80 )
346 {
347 safe_unicode_character = byte_stream_character;
348 }
349 else
350 {
351 safe_unicode_character = 0xfffd;
352 }
353 safe_byte_stream_index += 1;
354
355 break;
356
357 case LIBUNA_CODEPAGE_ISO_8859_1:
358 safe_unicode_character = byte_stream_character;
359
360 safe_byte_stream_index += 1;
361
362 break;
363
364 case LIBUNA_CODEPAGE_ISO_8859_2:
365 if( byte_stream_character < 0xa0 )
366 {
367 safe_unicode_character = byte_stream_character;
368 }
369 else
370 {
371 byte_stream_character -= 0xa0;
372
373 safe_unicode_character = libuna_codepage_iso_8859_2_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
374 }
375 safe_byte_stream_index += 1;
376
377 break;
378
379 case LIBUNA_CODEPAGE_ISO_8859_3:
380 if( byte_stream_character < 0xa0 )
381 {
382 safe_unicode_character = byte_stream_character;
383 }
384 else
385 {
386 byte_stream_character -= 0xa0;
387
388 safe_unicode_character = libuna_codepage_iso_8859_3_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
389 }
390 safe_byte_stream_index += 1;
391
392 break;
393
394 case LIBUNA_CODEPAGE_ISO_8859_4:
395 if( byte_stream_character < 0xa0 )
396 {
397 safe_unicode_character = byte_stream_character;
398 }
399 else
400 {
401 byte_stream_character -= 0xa0;
402
403 safe_unicode_character = libuna_codepage_iso_8859_4_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
404 }
405 safe_byte_stream_index += 1;
406
407 break;
408
409 case LIBUNA_CODEPAGE_ISO_8859_5:
410 if( byte_stream_character < 0xa0 )
411 {
412 safe_unicode_character = byte_stream_character;
413 }
414 else
415 {
416 byte_stream_character -= 0xa0;
417
418 safe_unicode_character = libuna_codepage_iso_8859_5_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
419 }
420 safe_byte_stream_index += 1;
421
422 break;
423
424 case LIBUNA_CODEPAGE_ISO_8859_6:
425 if( byte_stream_character < 0xa0 )
426 {
427 safe_unicode_character = byte_stream_character;
428 }
429 else
430 {
431 byte_stream_character -= 0xa0;
432
433 safe_unicode_character = libuna_codepage_iso_8859_6_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
434 }
435 safe_byte_stream_index += 1;
436
437 break;
438
439 case LIBUNA_CODEPAGE_ISO_8859_7:
440 if( byte_stream_character < 0xa0 )
441 {
442 safe_unicode_character = byte_stream_character;
443 }
444 else
445 {
446 byte_stream_character -= 0xa0;
447
448 safe_unicode_character = libuna_codepage_iso_8859_7_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
449 }
450 safe_byte_stream_index += 1;
451
452 break;
453
454 case LIBUNA_CODEPAGE_ISO_8859_8:
455 if( byte_stream_character < 0xa0 )
456 {
457 safe_unicode_character = byte_stream_character;
458 }
459 else
460 {
461 byte_stream_character -= 0xa0;
462
463 safe_unicode_character = libuna_codepage_iso_8859_8_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
464 }
465 safe_byte_stream_index += 1;
466
467 break;
468
469 case LIBUNA_CODEPAGE_ISO_8859_9:
470 if( byte_stream_character < 0xd0 )
471 {
472 safe_unicode_character = byte_stream_character;
473 }
474 else
475 {
476 byte_stream_character -= 0xd0;
477
478 safe_unicode_character = libuna_codepage_iso_8859_9_byte_stream_to_unicode_base_0xd0[ byte_stream_character ];
479 }
480 safe_byte_stream_index += 1;
481
482 break;
483
484 case LIBUNA_CODEPAGE_ISO_8859_10:
485 if( byte_stream_character < 0xa0 )
486 {
487 safe_unicode_character = byte_stream_character;
488 }
489 else
490 {
491 byte_stream_character -= 0xa0;
492
493 safe_unicode_character = libuna_codepage_iso_8859_10_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
494 }
495 safe_byte_stream_index += 1;
496
497 break;
498
499 case LIBUNA_CODEPAGE_ISO_8859_11:
500 if( byte_stream_character < 0xa1 )
501 {
502 safe_unicode_character = byte_stream_character;
503 }
504 else if( byte_stream_character < 0xdb )
505 {
506 safe_unicode_character = byte_stream_character + 0x0d60;
507 }
508 else if( byte_stream_character < 0xdf )
509 {
510 safe_unicode_character = 0xfffd;
511 }
512 else if( byte_stream_character < 0xfc )
513 {
514 safe_unicode_character = byte_stream_character + 0x0d60;
515 }
516 else
517 {
518 safe_unicode_character = 0xfffd;
519 }
520 safe_byte_stream_index += 1;
521
522 break;
523
524 case LIBUNA_CODEPAGE_ISO_8859_13:
525 if( byte_stream_character < 0xa0 )
526 {
527 safe_unicode_character = byte_stream_character;
528 }
529 else
530 {
531 byte_stream_character -= 0xa0;
532
533 safe_unicode_character = libuna_codepage_iso_8859_13_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
534 }
535 safe_byte_stream_index += 1;
536
537 break;
538
539 case LIBUNA_CODEPAGE_ISO_8859_14:
540 if( byte_stream_character < 0xa0 )
541 {
542 safe_unicode_character = byte_stream_character;
543 }
544 else
545 {
546 byte_stream_character -= 0xa0;
547
548 safe_unicode_character = libuna_codepage_iso_8859_14_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
549 }
550 safe_byte_stream_index += 1;
551
552 break;
553
554 case LIBUNA_CODEPAGE_ISO_8859_15:
555 if( ( byte_stream_character >= 0xa0 )
556 && ( byte_stream_character < 0xc0 ) )
557 {
558 byte_stream_character -= 0xa0;
559
560 safe_unicode_character = libuna_codepage_iso_8859_15_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
561 }
562 else
563 {
564 safe_unicode_character = byte_stream_character;
565 }
566 safe_byte_stream_index += 1;
567
568 break;
569
570 case LIBUNA_CODEPAGE_ISO_8859_16:
571 if( byte_stream_character < 0xa0 )
572 {
573 safe_unicode_character = byte_stream_character;
574 }
575 else
576 {
577 byte_stream_character -= 0xa0;
578
579 safe_unicode_character = libuna_codepage_iso_8859_16_byte_stream_to_unicode_base_0xa0[ byte_stream_character ];
580 }
581 safe_byte_stream_index += 1;
582
583 break;
584
585 case LIBUNA_CODEPAGE_KOI8_R:
586 result = libuna_codepage_koi8_r_copy_from_byte_stream(
587 &safe_unicode_character,
588 byte_stream,
589 byte_stream_size,
590 &safe_byte_stream_index,
591 error );
592 break;
593
594 case LIBUNA_CODEPAGE_KOI8_U:
595 result = libuna_codepage_koi8_u_copy_from_byte_stream(
596 &safe_unicode_character,
597 byte_stream,
598 byte_stream_size,
599 &safe_byte_stream_index,
600 error );
601 break;
602
603 case LIBUNA_CODEPAGE_MAC_ARABIC:
604 result = libuna_codepage_mac_arabic_copy_from_byte_stream(
605 &safe_unicode_character,
606 byte_stream,
607 byte_stream_size,
608 &safe_byte_stream_index,
609 error );
610 break;
611
612 case LIBUNA_CODEPAGE_MAC_CELTIC:
613 result = libuna_codepage_mac_celtic_copy_from_byte_stream(
614 &safe_unicode_character,
615 byte_stream,
616 byte_stream_size,
617 &safe_byte_stream_index,
618 error );
619 break;
620
621 case LIBUNA_CODEPAGE_MAC_CENTRALEURROMAN:
622 result = libuna_codepage_mac_centraleurroman_copy_from_byte_stream(
623 &safe_unicode_character,
624 byte_stream,
625 byte_stream_size,
626 &safe_byte_stream_index,
627 error );
628 break;
629
630 case LIBUNA_CODEPAGE_MAC_CROATIAN:
631 result = libuna_codepage_mac_croatian_copy_from_byte_stream(
632 &safe_unicode_character,
633 byte_stream,
634 byte_stream_size,
635 &safe_byte_stream_index,
636 error );
637 break;
638
639 case LIBUNA_CODEPAGE_MAC_CYRILLIC:
640 result = libuna_codepage_mac_cyrillic_copy_from_byte_stream(
641 &safe_unicode_character,
642 byte_stream,
643 byte_stream_size,
644 &safe_byte_stream_index,
645 error );
646 break;
647
648 case LIBUNA_CODEPAGE_MAC_DINGBATS:
649 result = libuna_codepage_mac_dingbats_copy_from_byte_stream(
650 &safe_unicode_character,
651 byte_stream,
652 byte_stream_size,
653 &safe_byte_stream_index,
654 error );
655 break;
656
657 case LIBUNA_CODEPAGE_MAC_FARSI:
658 result = libuna_codepage_mac_farsi_copy_from_byte_stream(
659 &safe_unicode_character,
660 byte_stream,
661 byte_stream_size,
662 &safe_byte_stream_index,
663 error );
664 break;
665
666 case LIBUNA_CODEPAGE_MAC_GAELIC:
667 result = libuna_codepage_mac_gaelic_copy_from_byte_stream(
668 &safe_unicode_character,
669 byte_stream,
670 byte_stream_size,
671 &safe_byte_stream_index,
672 error );
673 break;
674
675 case LIBUNA_CODEPAGE_MAC_GREEK:
676 result = libuna_codepage_mac_greek_copy_from_byte_stream(
677 &safe_unicode_character,
678 byte_stream,
679 byte_stream_size,
680 &safe_byte_stream_index,
681 error );
682 break;
683
684 case LIBUNA_CODEPAGE_MAC_ICELANDIC:
685 result = libuna_codepage_mac_icelandic_copy_from_byte_stream(
686 &safe_unicode_character,
687 byte_stream,
688 byte_stream_size,
689 &safe_byte_stream_index,
690 error );
691 break;
692
693 case LIBUNA_CODEPAGE_MAC_INUIT:
694 result = libuna_codepage_mac_inuit_copy_from_byte_stream(
695 &safe_unicode_character,
696 byte_stream,
697 byte_stream_size,
698 &safe_byte_stream_index,
699 error );
700 break;
701
702 case LIBUNA_CODEPAGE_MAC_ROMAN:
703 result = libuna_codepage_mac_roman_copy_from_byte_stream(
704 &safe_unicode_character,
705 byte_stream,
706 byte_stream_size,
707 &safe_byte_stream_index,
708 error );
709 break;
710
711 case LIBUNA_CODEPAGE_MAC_ROMANIAN:
712 result = libuna_codepage_mac_romanian_copy_from_byte_stream(
713 &safe_unicode_character,
714 byte_stream,
715 byte_stream_size,
716 &safe_byte_stream_index,
717 error );
718 break;
719
720 case LIBUNA_CODEPAGE_MAC_RUSSIAN:
721 result = libuna_codepage_mac_russian_copy_from_byte_stream(
722 &safe_unicode_character,
723 byte_stream,
724 byte_stream_size,
725 &safe_byte_stream_index,
726 error );
727 break;
728
729 case LIBUNA_CODEPAGE_MAC_SYMBOL:
730 result = libuna_codepage_mac_symbol_copy_from_byte_stream(
731 &safe_unicode_character,
732 byte_stream,
733 byte_stream_size,
734 &safe_byte_stream_index,
735 error );
736 break;
737
738 case LIBUNA_CODEPAGE_MAC_THAI:
739 result = libuna_codepage_mac_thai_copy_from_byte_stream(
740 &safe_unicode_character,
741 byte_stream,
742 byte_stream_size,
743 &safe_byte_stream_index,
744 error );
745 break;
746
747 case LIBUNA_CODEPAGE_MAC_TURKISH:
748 result = libuna_codepage_mac_turkish_copy_from_byte_stream(
749 &safe_unicode_character,
750 byte_stream,
751 byte_stream_size,
752 &safe_byte_stream_index,
753 error );
754 break;
755
756 case LIBUNA_CODEPAGE_MAC_UKRAINIAN:
757 result = libuna_codepage_mac_ukrainian_copy_from_byte_stream(
758 &safe_unicode_character,
759 byte_stream,
760 byte_stream_size,
761 &safe_byte_stream_index,
762 error );
763 break;
764
765 case LIBUNA_CODEPAGE_WINDOWS_874:
766 result = libuna_codepage_windows_874_copy_from_byte_stream(
767 &safe_unicode_character,
768 byte_stream,
769 byte_stream_size,
770 &safe_byte_stream_index,
771 error );
772 break;
773
774 case LIBUNA_CODEPAGE_WINDOWS_932:
775 result = libuna_codepage_windows_932_copy_from_byte_stream(
776 &safe_unicode_character,
777 byte_stream,
778 byte_stream_size,
779 &safe_byte_stream_index,
780 error );
781 break;
782
783 case LIBUNA_CODEPAGE_WINDOWS_936:
784 result = libuna_codepage_windows_936_copy_from_byte_stream(
785 &safe_unicode_character,
786 byte_stream,
787 byte_stream_size,
788 &safe_byte_stream_index,
789 error );
790 break;
791
792 case LIBUNA_CODEPAGE_WINDOWS_949:
793 result = libuna_codepage_windows_949_copy_from_byte_stream(
794 &safe_unicode_character,
795 byte_stream,
796 byte_stream_size,
797 &safe_byte_stream_index,
798 error );
799 break;
800
801 case LIBUNA_CODEPAGE_WINDOWS_950:
802 result = libuna_codepage_windows_950_copy_from_byte_stream(
803 &safe_unicode_character,
804 byte_stream,
805 byte_stream_size,
806 &safe_byte_stream_index,
807 error );
808 break;
809
810 case LIBUNA_CODEPAGE_WINDOWS_1250:
811 result = libuna_codepage_windows_1250_copy_from_byte_stream(
812 &safe_unicode_character,
813 byte_stream,
814 byte_stream_size,
815 &safe_byte_stream_index,
816 error );
817 break;
818
819 case LIBUNA_CODEPAGE_WINDOWS_1251:
820 result = libuna_codepage_windows_1251_copy_from_byte_stream(
821 &safe_unicode_character,
822 byte_stream,
823 byte_stream_size,
824 &safe_byte_stream_index,
825 error );
826 break;
827
828 case LIBUNA_CODEPAGE_WINDOWS_1252:
829 result = libuna_codepage_windows_1252_copy_from_byte_stream(
830 &safe_unicode_character,
831 byte_stream,
832 byte_stream_size,
833 &safe_byte_stream_index,
834 error );
835 break;
836
837 case LIBUNA_CODEPAGE_WINDOWS_1253:
838 result = libuna_codepage_windows_1253_copy_from_byte_stream(
839 &safe_unicode_character,
840 byte_stream,
841 byte_stream_size,
842 &safe_byte_stream_index,
843 error );
844 break;
845
846 case LIBUNA_CODEPAGE_WINDOWS_1254:
847 result = libuna_codepage_windows_1254_copy_from_byte_stream(
848 &safe_unicode_character,
849 byte_stream,
850 byte_stream_size,
851 &safe_byte_stream_index,
852 error );
853 break;
854
855 case LIBUNA_CODEPAGE_WINDOWS_1255:
856 result = libuna_codepage_windows_1255_copy_from_byte_stream(
857 &safe_unicode_character,
858 byte_stream,
859 byte_stream_size,
860 &safe_byte_stream_index,
861 error );
862 break;
863
864 case LIBUNA_CODEPAGE_WINDOWS_1256:
865 result = libuna_codepage_windows_1256_copy_from_byte_stream(
866 &safe_unicode_character,
867 byte_stream,
868 byte_stream_size,
869 &safe_byte_stream_index,
870 error );
871 break;
872
873 case LIBUNA_CODEPAGE_WINDOWS_1257:
874 result = libuna_codepage_windows_1257_copy_from_byte_stream(
875 &safe_unicode_character,
876 byte_stream,
877 byte_stream_size,
878 &safe_byte_stream_index,
879 error );
880 break;
881
882 case LIBUNA_CODEPAGE_WINDOWS_1258:
883 result = libuna_codepage_windows_1258_copy_from_byte_stream(
884 &safe_unicode_character,
885 byte_stream,
886 byte_stream_size,
887 &safe_byte_stream_index,
888 error );
889 break;
890
891 default:
892 libcerror_error_set(
893 error,
894 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
895 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
896 "%s: unsupported codepage: %d.",
897 function,
898 codepage );
899
900 return( -1 );
901 }
902 if( result == -1 )
903 {
904 libcerror_error_set(
905 error,
906 LIBCERROR_ERROR_DOMAIN_RUNTIME,
907 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
908 "%s: unable to copy Unicode character from byte stream.",
909 function );
910
911 return( -1 );
912 }
913 *unicode_character = safe_unicode_character;
914 *byte_stream_index = safe_byte_stream_index;
915
916 return( result );
917 }
918
919 /* Copies an Unicode character to a byte stream
920 * Returns 1 if successful, 0 if the Unicode character is valid but not supported since it requires special handling or -1 on error
921 */
922 int libuna_unicode_character_copy_to_byte_stream(
923 libuna_unicode_character_t unicode_character,
924 uint8_t *byte_stream,
925 size_t byte_stream_size,
926 size_t *byte_stream_index,
927 int codepage,
928 libcerror_error_t **error )
929 {
930 static char *function = "libuna_unicode_character_copy_to_byte_stream";
931 size_t safe_byte_stream_index = 0;
932 int result = 1;
933
934 if( byte_stream == NULL )
935 {
936 libcerror_error_set(
937 error,
938 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
939 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
940 "%s: invalid byte stream.",
941 function );
942
943 return( -1 );
944 }
945 if( byte_stream_size > (size_t) SSIZE_MAX )
946 {
947 libcerror_error_set(
948 error,
949 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
950 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
951 "%s: invalid byte stream size value exceeds maximum.",
952 function );
953
954 return( -1 );
955 }
956 if( byte_stream_index == NULL )
957 {
958 libcerror_error_set(
959 error,
960 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
961 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
962 "%s: invalid byte stream index.",
963 function );
964
965 return( -1 );
966 }
967 safe_byte_stream_index = *byte_stream_index;
968
969 if( safe_byte_stream_index >= byte_stream_size )
970 {
971 libcerror_error_set(
972 error,
973 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
974 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
975 "%s: byte stream too small.",
976 function );
977
978 return( -1 );
979 }
980 switch( codepage )
981 {
982 case LIBUNA_CODEPAGE_ASCII:
983 if( unicode_character < 0x0080 )
984 {
985 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
986 }
987 else
988 {
989 byte_stream[ safe_byte_stream_index ] = 0x1a;
990 }
991 safe_byte_stream_index += 1;
992
993 break;
994
995 case LIBUNA_CODEPAGE_ISO_8859_1:
996 if( unicode_character < 0x0100 )
997 {
998 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
999 }
1000 else
1001 {
1002 byte_stream[ safe_byte_stream_index ] = 0x1a;
1003 }
1004 safe_byte_stream_index += 1;
1005
1006 break;
1007
1008 case LIBUNA_CODEPAGE_ISO_8859_2:
1009 if( unicode_character < 0x00a0 )
1010 {
1011 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1012 }
1013 else if( ( unicode_character >= 0x00a0 )
1014 && ( unicode_character < 0x0120 ) )
1015 {
1016 unicode_character -= 0x00a0;
1017
1018 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_2_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
1019 }
1020 else if( ( unicode_character >= 0x0138 )
1021 && ( unicode_character < 0x0180 ) )
1022 {
1023 unicode_character -= 0x0138;
1024
1025 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_2_unicode_to_byte_stream_base_0x0138[ unicode_character ];
1026 }
1027 else if( ( unicode_character >= 0x02d8 )
1028 && ( unicode_character < 0x02e0 ) )
1029 {
1030 unicode_character -= 0x02d8;
1031
1032 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_2_unicode_to_byte_stream_base_0x02d8[ unicode_character ];
1033 }
1034 else if( unicode_character == 0x02c7 )
1035 {
1036 byte_stream[ safe_byte_stream_index ] = 0xb7;
1037 }
1038 else
1039 {
1040 byte_stream[ safe_byte_stream_index ] = 0x1a;
1041 }
1042 safe_byte_stream_index += 1;
1043
1044 break;
1045
1046 case LIBUNA_CODEPAGE_ISO_8859_3:
1047 if( unicode_character < 0x00a0 )
1048 {
1049 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1050 }
1051 else if( ( unicode_character >= 0x00a0 )
1052 && ( unicode_character < 0x0100 ) )
1053 {
1054 unicode_character -= 0x00a0;
1055
1056 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_3_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
1057 }
1058 else if( ( unicode_character >= 0x0108 )
1059 && ( unicode_character < 0x0110 ) )
1060 {
1061 unicode_character -= 0x0108;
1062
1063 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_3_unicode_to_byte_stream_base_0x0108[ unicode_character ];
1064 }
1065 else if( ( unicode_character >= 0x0118 )
1066 && ( unicode_character < 0x0128 ) )
1067 {
1068 unicode_character -= 0x0118;
1069
1070 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_3_unicode_to_byte_stream_base_0x0118[ unicode_character ];
1071 }
1072 else if( ( unicode_character >= 0x0130 )
1073 && ( unicode_character < 0x0138 ) )
1074 {
1075 unicode_character -= 0x0130;
1076
1077 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_3_unicode_to_byte_stream_base_0x0130[ unicode_character ];
1078 }
1079 else if( ( unicode_character >= 0x0158 )
1080 && ( unicode_character < 0x0160 ) )
1081 {
1082 unicode_character -= 0x0158;
1083
1084 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_3_unicode_to_byte_stream_base_0x0158[ unicode_character ];
1085 }
1086 else switch( unicode_character )
1087 {
1088 case 0x016c:
1089 byte_stream[ safe_byte_stream_index ] = 0xdd;
1090 break;
1091
1092 case 0x016d:
1093 byte_stream[ safe_byte_stream_index ] = 0xfd;
1094 break;
1095
1096 case 0x017b:
1097 byte_stream[ safe_byte_stream_index ] = 0xaf;
1098 break;
1099
1100 case 0x017c:
1101 byte_stream[ safe_byte_stream_index ] = 0xbf;
1102 break;
1103
1104 case 0x02d8:
1105 byte_stream[ safe_byte_stream_index ] = 0xa2;
1106 break;
1107
1108 case 0x02d9:
1109 byte_stream[ safe_byte_stream_index ] = 0xff;
1110 break;
1111
1112 default:
1113 byte_stream[ safe_byte_stream_index ] = 0x1a;
1114 break;
1115 }
1116 safe_byte_stream_index += 1;
1117
1118 break;
1119
1120 case LIBUNA_CODEPAGE_ISO_8859_4:
1121 if( unicode_character < 0x00a0 )
1122 {
1123 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1124 }
1125 else if( ( unicode_character >= 0x00a0 )
1126 && ( unicode_character < 0x0158 ) )
1127 {
1128 unicode_character -= 0x00a0;
1129
1130 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_4_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
1131 }
1132 else if( ( unicode_character >= 0x0160 )
1133 && ( unicode_character < 0x0180 ) )
1134 {
1135 unicode_character -= 0x0160;
1136
1137 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_4_unicode_to_byte_stream_base_0x0160[ unicode_character ];
1138 }
1139 else switch( unicode_character )
1140 {
1141 case 0x02c7:
1142 byte_stream[ safe_byte_stream_index ] = 0xb7;
1143 break;
1144
1145 case 0x02d9:
1146 byte_stream[ safe_byte_stream_index ] = 0xff;
1147 break;
1148
1149 case 0x02db:
1150 byte_stream[ safe_byte_stream_index ] = 0xb2;
1151 break;
1152
1153 default:
1154 byte_stream[ safe_byte_stream_index ] = 0x1a;
1155 break;
1156 }
1157 safe_byte_stream_index += 1;
1158
1159 break;
1160
1161 case LIBUNA_CODEPAGE_ISO_8859_5:
1162 if( unicode_character < 0x00a1 )
1163 {
1164 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1165 }
1166 else if( ( unicode_character >= 0x0400 )
1167 && ( unicode_character < 0x0460 ) )
1168 {
1169 unicode_character -= 0x0400;
1170
1171 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_5_unicode_to_byte_stream_base_0x0400[ unicode_character ];
1172 }
1173 else switch( unicode_character )
1174 {
1175 case 0x00a7:
1176 byte_stream[ safe_byte_stream_index ] = 0xfd;
1177 break;
1178
1179 case 0x00ad:
1180 byte_stream[ safe_byte_stream_index ] = 0xad;
1181 break;
1182
1183 case 0x2116:
1184 byte_stream[ safe_byte_stream_index ] = 0xf0;
1185 break;
1186
1187 default:
1188 byte_stream[ safe_byte_stream_index ] = 0x1a;
1189 break;
1190 }
1191 safe_byte_stream_index += 1;
1192
1193 break;
1194
1195 case LIBUNA_CODEPAGE_ISO_8859_6:
1196 if( unicode_character < 0x00a1 )
1197 {
1198 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1199 }
1200 else if( ( unicode_character >= 0x0618 )
1201 && ( unicode_character < 0x658 ) )
1202 {
1203 unicode_character -= 0x0618;
1204
1205 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_6_unicode_to_byte_stream_base_0x0618[ unicode_character ];
1206 }
1207 else switch( unicode_character )
1208 {
1209 case 0x00a4:
1210 byte_stream[ safe_byte_stream_index ] = 0xa4;
1211 break;
1212
1213 case 0x00ad:
1214 byte_stream[ safe_byte_stream_index ] = 0xad;
1215 break;
1216
1217 case 0x060c:
1218 byte_stream[ safe_byte_stream_index ] = 0xac;
1219 break;
1220
1221 default:
1222 byte_stream[ safe_byte_stream_index ] = 0x1a;
1223 break;
1224 }
1225 safe_byte_stream_index += 1;
1226
1227 break;
1228
1229 case LIBUNA_CODEPAGE_ISO_8859_7:
1230 if( unicode_character < 0x00a0 )
1231 {
1232 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1233 }
1234 else if( ( unicode_character >= 0x00a0 )
1235 && ( unicode_character < 0x00b8 ) )
1236 {
1237 unicode_character -= 0x00a0;
1238
1239 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_7_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
1240 }
1241 else if( ( unicode_character >= 0x0380 )
1242 && ( unicode_character < 0x03d0 ) )
1243 {
1244 unicode_character -= 0x0380;
1245
1246 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_7_unicode_to_byte_stream_base_0x0380[ unicode_character ];
1247 }
1248 else switch( unicode_character )
1249 {
1250 case 0x00bb:
1251 byte_stream[ safe_byte_stream_index ] = 0xbb;
1252 break;
1253
1254 case 0x00bd:
1255 byte_stream[ safe_byte_stream_index ] = 0xbd;
1256 break;
1257
1258 case 0x037a:
1259 byte_stream[ safe_byte_stream_index ] = 0xaa;
1260 break;
1261
1262 case 0x2015:
1263 byte_stream[ safe_byte_stream_index ] = 0xaf;
1264 break;
1265
1266 case 0x2018:
1267 byte_stream[ safe_byte_stream_index ] = 0xa1;
1268 break;
1269
1270 case 0x2019:
1271 byte_stream[ safe_byte_stream_index ] = 0xa2;
1272 break;
1273
1274 case 0x20ac:
1275 byte_stream[ safe_byte_stream_index ] = 0xa4;
1276 break;
1277
1278 case 0x20af:
1279 byte_stream[ safe_byte_stream_index ] = 0xa5;
1280 break;
1281
1282 default:
1283 byte_stream[ safe_byte_stream_index ] = 0x1a;
1284 break;
1285 }
1286 safe_byte_stream_index += 1;
1287
1288 break;
1289
1290 case LIBUNA_CODEPAGE_ISO_8859_8:
1291 if( unicode_character < 0x00a0 )
1292 {
1293 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1294 }
1295 else if( ( unicode_character >= 0x00a0 )
1296 && ( unicode_character < 0x00c0 ) )
1297 {
1298 unicode_character -= 0x00a0;
1299
1300 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_8_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
1301 }
1302 else if( ( unicode_character >= 0x05d0 )
1303 && ( unicode_character < 0x05f0 ) )
1304 {
1305 unicode_character -= 0x05d0;
1306
1307 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_8_unicode_to_byte_stream_base_0x05d0[ unicode_character ];
1308 }
1309 else switch( unicode_character )
1310 {
1311 case 0x00d7:
1312 byte_stream[ safe_byte_stream_index ] = 0xaa;
1313 break;
1314
1315 case 0x00f7:
1316 byte_stream[ safe_byte_stream_index ] = 0xba;
1317 break;
1318
1319 case 0x200e:
1320 byte_stream[ safe_byte_stream_index ] = 0xfd;
1321 break;
1322
1323 case 0x200f:
1324 byte_stream[ safe_byte_stream_index ] = 0xfe;
1325 break;
1326
1327 case 0x2017:
1328 byte_stream[ safe_byte_stream_index ] = 0xdf;
1329 break;
1330
1331 default:
1332 byte_stream[ safe_byte_stream_index ] = 0x1a;
1333 break;
1334 }
1335 safe_byte_stream_index += 1;
1336
1337 break;
1338
1339 case LIBUNA_CODEPAGE_ISO_8859_9:
1340 if( unicode_character < 0x00d0 )
1341 {
1342 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1343 }
1344 else if( ( unicode_character >= 0x00d0 )
1345 && ( unicode_character < 0x0100 ) )
1346 {
1347 unicode_character -= 0x00d0;
1348
1349 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_9_unicode_to_byte_stream_base_0x00d0[ unicode_character ];
1350 }
1351 else switch( unicode_character )
1352 {
1353 case 0x011e:
1354 byte_stream[ safe_byte_stream_index ] = 0xd0;
1355 break;
1356
1357 case 0x011f:
1358 byte_stream[ safe_byte_stream_index ] = 0xf0;
1359 break;
1360
1361 case 0x0130:
1362 byte_stream[ safe_byte_stream_index ] = 0xdd;
1363 break;
1364
1365 case 0x0131:
1366 byte_stream[ safe_byte_stream_index ] = 0xfd;
1367 break;
1368
1369 case 0x015e:
1370 byte_stream[ safe_byte_stream_index ] = 0xde;
1371 break;
1372
1373 case 0x015f:
1374 byte_stream[ safe_byte_stream_index ] = 0xfe;
1375 break;
1376
1377 default:
1378 byte_stream[ safe_byte_stream_index ] = 0x1a;
1379 break;
1380 }
1381 safe_byte_stream_index += 1;
1382
1383 break;
1384
1385 case LIBUNA_CODEPAGE_ISO_8859_10:
1386 if( unicode_character < 0x00a1 )
1387 {
1388 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1389 }
1390 else if( ( unicode_character >= 0x00c0 )
1391 && ( unicode_character < 0x0150 ) )
1392 {
1393 unicode_character -= 0x00c0;
1394
1395 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_10_unicode_to_byte_stream_base_0x00c0[ unicode_character ];
1396 }
1397 else if( ( unicode_character >= 0x0160 )
1398 && ( unicode_character < 0x0170 ) )
1399 {
1400 unicode_character -= 0x0160;
1401
1402 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_10_unicode_to_byte_stream_base_0x0160[ unicode_character ];
1403 }
1404 else switch( unicode_character )
1405 {
1406 case 0x00a7:
1407 byte_stream[ safe_byte_stream_index ] = 0xa7;
1408 break;
1409
1410 case 0x00ad:
1411 byte_stream[ safe_byte_stream_index ] = 0xad;
1412 break;
1413
1414 case 0x00b0:
1415 byte_stream[ safe_byte_stream_index ] = 0xb0;
1416 break;
1417
1418 case 0x00b7:
1419 byte_stream[ safe_byte_stream_index ] = 0xb7;
1420 break;
1421
1422 case 0x0172:
1423 byte_stream[ safe_byte_stream_index ] = 0xd9;
1424 break;
1425
1426 case 0x0173:
1427 byte_stream[ safe_byte_stream_index ] = 0xf9;
1428 break;
1429
1430 case 0x017d:
1431 byte_stream[ safe_byte_stream_index ] = 0xac;
1432 break;
1433
1434 case 0x017e:
1435 byte_stream[ safe_byte_stream_index ] = 0xbc;
1436 break;
1437
1438 case 0x2015:
1439 byte_stream[ safe_byte_stream_index ] = 0xbd;
1440 break;
1441
1442 default:
1443 byte_stream[ safe_byte_stream_index ] = 0x1a;
1444 break;
1445 }
1446 safe_byte_stream_index += 1;
1447
1448 break;
1449
1450 case LIBUNA_CODEPAGE_ISO_8859_11:
1451 if( unicode_character < 0x00a1 )
1452 {
1453 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1454 }
1455 else if( ( unicode_character >= 0x0e01 )
1456 && ( unicode_character < 0x0e3b ) )
1457 {
1458 byte_stream[ safe_byte_stream_index ] = (uint8_t) ( unicode_character - 0x0d60 );
1459 }
1460 else if( ( unicode_character >= 0x0e3f )
1461 && ( unicode_character < 0x0e5c ) )
1462 {
1463 byte_stream[ safe_byte_stream_index ] = (uint8_t) ( unicode_character - 0x0d60 );
1464 }
1465 else
1466 {
1467 byte_stream[ safe_byte_stream_index ] = 0x1a;
1468 }
1469 safe_byte_stream_index += 1;
1470
1471 break;
1472
1473 case LIBUNA_CODEPAGE_ISO_8859_13:
1474 if( unicode_character < 0x00a0 )
1475 {
1476 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1477 }
1478 else if( ( unicode_character >= 0x00a0 )
1479 && ( unicode_character < 0x0180 ) )
1480 {
1481 unicode_character -= 0x00a0;
1482
1483 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_13_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
1484 }
1485 else if( ( unicode_character >= 0x2018 )
1486 && ( unicode_character < 0x2020 ) )
1487 {
1488 unicode_character -= 0x2018;
1489
1490 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_13_unicode_to_byte_stream_base_0x2018[ unicode_character ];
1491 }
1492 else
1493 {
1494 byte_stream[ safe_byte_stream_index ] = 0x1a;
1495 }
1496 safe_byte_stream_index += 1;
1497
1498 break;
1499
1500 case LIBUNA_CODEPAGE_ISO_8859_14:
1501 if( unicode_character < 0x00a1 )
1502 {
1503 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1504 }
1505 else if( ( unicode_character >= 0x00c0 )
1506 && ( unicode_character < 0x0100 ) )
1507 {
1508 unicode_character -= 0x00c0;
1509
1510 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_14_unicode_to_byte_stream_base_0x00c0[ unicode_character ];
1511 }
1512 else if( ( unicode_character >= 0x0170 )
1513 && ( unicode_character < 0x0178 ) )
1514 {
1515 unicode_character -= 0x0170;
1516
1517 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_14_unicode_to_byte_stream_base_0x0170[ unicode_character ];
1518 }
1519 else if( ( unicode_character >= 0x1e80 )
1520 && ( unicode_character < 0x1e88 ) )
1521 {
1522 unicode_character -= 0x1e80;
1523
1524 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_14_unicode_to_byte_stream_base_0x1e80[ unicode_character ];
1525 }
1526 else switch( unicode_character )
1527 {
1528 case 0x00a3:
1529 byte_stream[ safe_byte_stream_index ] = 0xa3;
1530 break;
1531
1532 case 0x00a7:
1533 byte_stream[ safe_byte_stream_index ] = 0xa7;
1534 break;
1535
1536 case 0x00a9:
1537 byte_stream[ safe_byte_stream_index ] = 0xa9;
1538 break;
1539
1540 case 0x00ad:
1541 byte_stream[ safe_byte_stream_index ] = 0xad;
1542 break;
1543
1544 case 0x00ae:
1545 byte_stream[ safe_byte_stream_index ] = 0xae;
1546 break;
1547
1548 case 0x00b6:
1549 byte_stream[ safe_byte_stream_index ] = 0xb6;
1550 break;
1551
1552 case 0x010a:
1553 byte_stream[ safe_byte_stream_index ] = 0xa4;
1554 break;
1555
1556 case 0x010b:
1557 byte_stream[ safe_byte_stream_index ] = 0xa5;
1558 break;
1559
1560 case 0x0120:
1561 byte_stream[ safe_byte_stream_index ] = 0xb2;
1562 break;
1563
1564 case 0x0121:
1565 byte_stream[ safe_byte_stream_index ] = 0xb3;
1566 break;
1567
1568 case 0x0178:
1569 byte_stream[ safe_byte_stream_index ] = 0xaf;
1570 break;
1571
1572 case 0x1e02:
1573 byte_stream[ safe_byte_stream_index ] = 0xa1;
1574 break;
1575
1576 case 0x1e03:
1577 byte_stream[ safe_byte_stream_index ] = 0xa2;
1578 break;
1579
1580 case 0x1e0a:
1581 byte_stream[ safe_byte_stream_index ] = 0xa6;
1582 break;
1583
1584 case 0x1e0b:
1585 byte_stream[ safe_byte_stream_index ] = 0xab;
1586 break;
1587
1588 case 0x1e1e:
1589 byte_stream[ safe_byte_stream_index ] = 0xb0;
1590 break;
1591
1592 case 0x1e1f:
1593 byte_stream[ safe_byte_stream_index ] = 0xb1;
1594 break;
1595
1596 case 0x1e40:
1597 byte_stream[ safe_byte_stream_index ] = 0xb4;
1598 break;
1599
1600 case 0x1e41:
1601 byte_stream[ safe_byte_stream_index ] = 0xb5;
1602 break;
1603
1604 case 0x1e56:
1605 byte_stream[ safe_byte_stream_index ] = 0xb7;
1606 break;
1607
1608 case 0x1e57:
1609 byte_stream[ safe_byte_stream_index ] = 0xb9;
1610 break;
1611
1612 case 0x1e60:
1613 byte_stream[ safe_byte_stream_index ] = 0xbb;
1614 break;
1615
1616 case 0x1e61:
1617 byte_stream[ safe_byte_stream_index ] = 0xbf;
1618 break;
1619
1620 case 0x1e6a:
1621 byte_stream[ safe_byte_stream_index ] = 0xd7;
1622 break;
1623
1624 case 0x1e6b:
1625 byte_stream[ safe_byte_stream_index ] = 0xf7;
1626 break;
1627
1628 case 0x1ef2:
1629 byte_stream[ safe_byte_stream_index ] = 0xac;
1630 break;
1631
1632 case 0x1ef3:
1633 byte_stream[ safe_byte_stream_index ] = 0xbc;
1634 break;
1635
1636 default:
1637 byte_stream[ safe_byte_stream_index ] = 0x1a;
1638 break;
1639 }
1640 safe_byte_stream_index += 1;
1641
1642 break;
1643
1644 case LIBUNA_CODEPAGE_ISO_8859_15:
1645 if( unicode_character < 0x00a0 )
1646 {
1647 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1648 }
1649 else if( ( unicode_character >= 0x00a0 )
1650 && ( unicode_character < 0x00c0 ) )
1651 {
1652 unicode_character -= 0x00a0;
1653
1654 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_15_unicode_to_byte_stream_base_0x00a0[ unicode_character ];
1655 }
1656 else if( unicode_character < 0x0100 )
1657 {
1658 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1659 }
1660 else switch( unicode_character )
1661 {
1662 case 0x0152:
1663 byte_stream[ safe_byte_stream_index ] = 0xbc;
1664 break;
1665
1666 case 0x0153:
1667 byte_stream[ safe_byte_stream_index ] = 0xbd;
1668 break;
1669
1670 case 0x0160:
1671 byte_stream[ safe_byte_stream_index ] = 0xa6;
1672 break;
1673
1674 case 0x0161:
1675 byte_stream[ safe_byte_stream_index ] = 0xa8;
1676 break;
1677
1678 case 0x0178:
1679 byte_stream[ safe_byte_stream_index ] = 0xbe;
1680 break;
1681
1682 case 0x017d:
1683 byte_stream[ safe_byte_stream_index ] = 0xb4;
1684 break;
1685
1686 case 0x017e:
1687 byte_stream[ safe_byte_stream_index ] = 0xb8;
1688 break;
1689
1690 case 0x20ac:
1691 byte_stream[ safe_byte_stream_index ] = 0xa4;
1692 break;
1693
1694 default:
1695 byte_stream[ safe_byte_stream_index ] = 0x1a;
1696 break;
1697 }
1698 safe_byte_stream_index += 1;
1699
1700 break;
1701
1702 case LIBUNA_CODEPAGE_ISO_8859_16:
1703 if( unicode_character < 0x00a1 )
1704 {
1705 byte_stream[ safe_byte_stream_index ] = (uint8_t) unicode_character;
1706 }
1707 else if( ( unicode_character >= 0x00a8 )
1708 && ( unicode_character < 0x0108 ) )
1709 {
1710 unicode_character -= 0x00a8;
1711
1712 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_16_unicode_to_byte_stream_base_0x00a8[ unicode_character ];
1713 }
1714 else if( ( unicode_character >= 0x0140 )
1715 && ( unicode_character < 0x0148 ) )
1716 {
1717 unicode_character -= 0x0140;
1718
1719 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_16_unicode_to_byte_stream_base_0x0140[ unicode_character ];
1720 }
1721 else if( ( unicode_character >= 0x0150 )
1722 && ( unicode_character < 0x0158 ) )
1723 {
1724 unicode_character -= 0x0150;
1725
1726 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_16_unicode_to_byte_stream_base_0x0150[ unicode_character ];
1727 }
1728 else if( ( unicode_character >= 0x0178 )
1729 && ( unicode_character < 0x0180 ) )
1730 {
1731 unicode_character -= 0x0178;
1732
1733 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_16_unicode_to_byte_stream_base_0x0178[ unicode_character ];
1734 }
1735 else if( ( unicode_character >= 0x0218 )
1736 && ( unicode_character < 0x0220 ) )
1737 {
1738 unicode_character -= 0x0218;
1739
1740 byte_stream[ safe_byte_stream_index ] = libuna_codepage_iso_8859_16_unicode_to_byte_stream_base_0x0218[ unicode_character ];
1741 }
1742 else switch( unicode_character )
1743 {
1744 case 0x00a7:
1745 byte_stream[ safe_byte_stream_index ] = 0xa7;
1746 break;
1747
1748 case 0x010c:
1749 byte_stream[ safe_byte_stream_index ] = 0xb2;
1750 break;
1751
1752 case 0x010d:
1753 byte_stream[ safe_byte_stream_index ] = 0xb9;
1754 break;
1755
1756 case 0x0110:
1757 byte_stream[ safe_byte_stream_index ] = 0xd0;
1758 break;
1759
1760 case 0x0111:
1761 byte_stream[ safe_byte_stream_index ] = 0xf0;
1762 break;
1763
1764 case 0x0118:
1765 byte_stream[ safe_byte_stream_index ] = 0xdd;
1766 break;
1767
1768 case 0x0119:
1769 byte_stream[ safe_byte_stream_index ] = 0xfd;
1770 break;
1771
1772 case 0x015a:
1773 byte_stream[ safe_byte_stream_index ] = 0xd7;
1774 break;
1775
1776 case 0x015b:
1777 byte_stream[ safe_byte_stream_index ] = 0xf7;
1778 break;
1779
1780 case 0x0160:
1781 byte_stream[ safe_byte_stream_index ] = 0xa6;
1782 break;
1783
1784 case 0x0161:
1785 byte_stream[ safe_byte_stream_index ] = 0xa8;
1786 break;
1787
1788 case 0x0170:
1789 byte_stream[ safe_byte_stream_index ] = 0xd8;
1790 break;
1791
1792 case 0x0171:
1793 byte_stream[ safe_byte_stream_index ] = 0xf8;
1794 break;
1795
1796 case 0x201d:
1797 byte_stream[ safe_byte_stream_index ] = 0xb5;
1798 break;
1799
1800 case 0x201e:
1801 byte_stream[ safe_byte_stream_index ] = 0xa5;
1802 break;
1803
1804 case 0x20ac:
1805 byte_stream[ safe_byte_stream_index ] = 0xa4;
1806 break;
1807
1808 default:
1809 byte_stream[ safe_byte_stream_index ] = 0x1a;
1810 break;
1811 }
1812 safe_byte_stream_index += 1;
1813
1814 break;
1815
1816 case LIBUNA_CODEPAGE_KOI8_R:
1817 result = libuna_codepage_koi8_r_copy_to_byte_stream(
1818 unicode_character,
1819 byte_stream,
1820 byte_stream_size,
1821 &safe_byte_stream_index,
1822 error );
1823 break;
1824
1825 case LIBUNA_CODEPAGE_KOI8_U:
1826 result = libuna_codepage_koi8_u_copy_to_byte_stream(
1827 unicode_character,
1828 byte_stream,
1829 byte_stream_size,
1830 &safe_byte_stream_index,
1831 error );
1832 break;
1833
1834 case LIBUNA_CODEPAGE_MAC_ARABIC:
1835 result = libuna_codepage_mac_arabic_copy_to_byte_stream(
1836 unicode_character,
1837 byte_stream,
1838 byte_stream_size,
1839 &safe_byte_stream_index,
1840 error );
1841 break;
1842
1843 case LIBUNA_CODEPAGE_MAC_CELTIC:
1844 result = libuna_codepage_mac_celtic_copy_to_byte_stream(
1845 unicode_character,
1846 byte_stream,
1847 byte_stream_size,
1848 &safe_byte_stream_index,
1849 error );
1850 break;
1851
1852 case LIBUNA_CODEPAGE_MAC_CENTRALEURROMAN:
1853 result = libuna_codepage_mac_centraleurroman_copy_to_byte_stream(
1854 unicode_character,
1855 byte_stream,
1856 byte_stream_size,
1857 &safe_byte_stream_index,
1858 error );
1859 break;
1860
1861 case LIBUNA_CODEPAGE_MAC_CROATIAN:
1862 result = libuna_codepage_mac_croatian_copy_to_byte_stream(
1863 unicode_character,
1864 byte_stream,
1865 byte_stream_size,
1866 &safe_byte_stream_index,
1867 error );
1868 break;
1869
1870 case LIBUNA_CODEPAGE_MAC_CYRILLIC:
1871 result = libuna_codepage_mac_cyrillic_copy_to_byte_stream(
1872 unicode_character,
1873 byte_stream,
1874 byte_stream_size,
1875 &safe_byte_stream_index,
1876 error );
1877 break;
1878
1879 case LIBUNA_CODEPAGE_MAC_DINGBATS:
1880 result = libuna_codepage_mac_dingbats_copy_to_byte_stream(
1881 unicode_character,
1882 byte_stream,
1883 byte_stream_size,
1884 &safe_byte_stream_index,
1885 error );
1886 break;
1887
1888 case LIBUNA_CODEPAGE_MAC_FARSI:
1889 result = libuna_codepage_mac_farsi_copy_to_byte_stream(
1890 unicode_character,
1891 byte_stream,
1892 byte_stream_size,
1893 &safe_byte_stream_index,
1894 error );
1895 break;
1896
1897 case LIBUNA_CODEPAGE_MAC_GAELIC:
1898 result = libuna_codepage_mac_gaelic_copy_to_byte_stream(
1899 unicode_character,
1900 byte_stream,
1901 byte_stream_size,
1902 &safe_byte_stream_index,
1903 error );
1904 break;
1905
1906 case LIBUNA_CODEPAGE_MAC_GREEK:
1907 result = libuna_codepage_mac_greek_copy_to_byte_stream(
1908 unicode_character,
1909 byte_stream,
1910 byte_stream_size,
1911 &safe_byte_stream_index,
1912 error );
1913 break;
1914
1915 case LIBUNA_CODEPAGE_MAC_ICELANDIC:
1916 result = libuna_codepage_mac_icelandic_copy_to_byte_stream(
1917 unicode_character,
1918 byte_stream,
1919 byte_stream_size,
1920 &safe_byte_stream_index,
1921 error );
1922 break;
1923
1924 case LIBUNA_CODEPAGE_MAC_INUIT:
1925 result = libuna_codepage_mac_inuit_copy_to_byte_stream(
1926 unicode_character,
1927 byte_stream,
1928 byte_stream_size,
1929 &safe_byte_stream_index,
1930 error );
1931 break;
1932
1933 case LIBUNA_CODEPAGE_MAC_ROMAN:
1934 result = libuna_codepage_mac_roman_copy_to_byte_stream(
1935 unicode_character,
1936 byte_stream,
1937 byte_stream_size,
1938 &safe_byte_stream_index,
1939 error );
1940 break;
1941
1942 case LIBUNA_CODEPAGE_MAC_ROMANIAN:
1943 result = libuna_codepage_mac_romanian_copy_to_byte_stream(
1944 unicode_character,
1945 byte_stream,
1946 byte_stream_size,
1947 &safe_byte_stream_index,
1948 error );
1949 break;
1950
1951 case LIBUNA_CODEPAGE_MAC_RUSSIAN:
1952 result = libuna_codepage_mac_russian_copy_to_byte_stream(
1953 unicode_character,
1954 byte_stream,
1955 byte_stream_size,
1956 &safe_byte_stream_index,
1957 error );
1958 break;
1959
1960 case LIBUNA_CODEPAGE_MAC_SYMBOL:
1961 result = libuna_codepage_mac_symbol_copy_to_byte_stream(
1962 unicode_character,
1963 byte_stream,
1964 byte_stream_size,
1965 &safe_byte_stream_index,
1966 error );
1967 break;
1968
1969 case LIBUNA_CODEPAGE_MAC_THAI:
1970 result = libuna_codepage_mac_thai_copy_to_byte_stream(
1971 unicode_character,
1972 byte_stream,
1973 byte_stream_size,
1974 &safe_byte_stream_index,
1975 error );
1976 break;
1977
1978 case LIBUNA_CODEPAGE_MAC_TURKISH:
1979 result = libuna_codepage_mac_turkish_copy_to_byte_stream(
1980 unicode_character,
1981 byte_stream,
1982 byte_stream_size,
1983 &safe_byte_stream_index,
1984 error );
1985 break;
1986
1987 case LIBUNA_CODEPAGE_MAC_UKRAINIAN:
1988 result = libuna_codepage_mac_ukrainian_copy_to_byte_stream(
1989 unicode_character,
1990 byte_stream,
1991 byte_stream_size,
1992 &safe_byte_stream_index,
1993 error );
1994 break;
1995
1996 case LIBUNA_CODEPAGE_WINDOWS_874:
1997 result = libuna_codepage_windows_874_copy_to_byte_stream(
1998 unicode_character,
1999 byte_stream,
2000 byte_stream_size,
2001 &safe_byte_stream_index,
2002 error );
2003 break;
2004
2005 case LIBUNA_CODEPAGE_WINDOWS_932:
2006 result = libuna_codepage_windows_932_copy_to_byte_stream(
2007 unicode_character,
2008 byte_stream,
2009 byte_stream_size,
2010 &safe_byte_stream_index,
2011 error );
2012 break;
2013
2014 case LIBUNA_CODEPAGE_WINDOWS_936:
2015 result = libuna_codepage_windows_936_copy_to_byte_stream(
2016 unicode_character,
2017 byte_stream,
2018 byte_stream_size,
2019 &safe_byte_stream_index,
2020 error );
2021 break;
2022
2023 case LIBUNA_CODEPAGE_WINDOWS_949:
2024 result = libuna_codepage_windows_949_copy_to_byte_stream(
2025 unicode_character,
2026 byte_stream,
2027 byte_stream_size,
2028 &safe_byte_stream_index,
2029 error );
2030 break;
2031
2032 case LIBUNA_CODEPAGE_WINDOWS_950:
2033 result = libuna_codepage_windows_950_copy_to_byte_stream(
2034 unicode_character,
2035 byte_stream,
2036 byte_stream_size,
2037 &safe_byte_stream_index,
2038 error );
2039 break;
2040
2041 case LIBUNA_CODEPAGE_WINDOWS_1250:
2042 result = libuna_codepage_windows_1250_copy_to_byte_stream(
2043 unicode_character,
2044 byte_stream,
2045 byte_stream_size,
2046 &safe_byte_stream_index,
2047 error );
2048 break;
2049
2050 case LIBUNA_CODEPAGE_WINDOWS_1251:
2051 result = libuna_codepage_windows_1251_copy_to_byte_stream(
2052 unicode_character,
2053 byte_stream,
2054 byte_stream_size,
2055 &safe_byte_stream_index,
2056 error );
2057 break;
2058
2059 case LIBUNA_CODEPAGE_WINDOWS_1252:
2060 result = libuna_codepage_windows_1252_copy_to_byte_stream(
2061 unicode_character,
2062 byte_stream,
2063 byte_stream_size,
2064 &safe_byte_stream_index,
2065 error );
2066 break;
2067
2068 case LIBUNA_CODEPAGE_WINDOWS_1253:
2069 result = libuna_codepage_windows_1253_copy_to_byte_stream(
2070 unicode_character,
2071 byte_stream,
2072 byte_stream_size,
2073 &safe_byte_stream_index,
2074 error );
2075 break;
2076
2077 case LIBUNA_CODEPAGE_WINDOWS_1254:
2078 result = libuna_codepage_windows_1254_copy_to_byte_stream(
2079 unicode_character,
2080 byte_stream,
2081 byte_stream_size,
2082 &safe_byte_stream_index,
2083 error );
2084 break;
2085
2086 case LIBUNA_CODEPAGE_WINDOWS_1255:
2087 result = libuna_codepage_windows_1255_copy_to_byte_stream(
2088 unicode_character,
2089 byte_stream,
2090 byte_stream_size,
2091 &safe_byte_stream_index,
2092 error );
2093 break;
2094
2095 case LIBUNA_CODEPAGE_WINDOWS_1256:
2096 result = libuna_codepage_windows_1256_copy_to_byte_stream(
2097 unicode_character,
2098 byte_stream,
2099 byte_stream_size,
2100 &safe_byte_stream_index,
2101 error );
2102 break;
2103
2104 case LIBUNA_CODEPAGE_WINDOWS_1257:
2105 result = libuna_codepage_windows_1257_copy_to_byte_stream(
2106 unicode_character,
2107 byte_stream,
2108 byte_stream_size,
2109 &safe_byte_stream_index,
2110 error );
2111 break;
2112
2113 case LIBUNA_CODEPAGE_WINDOWS_1258:
2114 result = libuna_codepage_windows_1258_copy_to_byte_stream(
2115 unicode_character,
2116 byte_stream,
2117 byte_stream_size,
2118 &safe_byte_stream_index,
2119 error );
2120 break;
2121
2122 default:
2123 libcerror_error_set(
2124 error,
2125 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2126 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2127 "%s: unsupported codepage: %d.",
2128 function,
2129 codepage );
2130
2131 return( -1 );
2132 }
2133 if( result == -1 )
2134 {
2135 libcerror_error_set(
2136 error,
2137 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2138 LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
2139 "%s: unable to copy Unicode character to byte stream.",
2140 function );
2141
2142 return( -1 );
2143 }
2144 *byte_stream_index = safe_byte_stream_index;
2145
2146 return( result );
2147 }
2148
2149 /* Determines the size of an UCS-2 character from an Unicode character
2150 * Adds the size to the UCS-2 character size value
2151 * Returns 1 if successful or -1 on error
2152 */
2153 int libuna_unicode_character_size_to_ucs2(
2154 libuna_unicode_character_t unicode_character,
2155 size_t *ucs2_character_size,
2156 libcerror_error_t **error )
2157 {
2158 static char *function = "libuna_unicode_character_size_to_ucs2";
2159
2160 if( ucs2_character_size == NULL )
2161 {
2162 libcerror_error_set(
2163 error,
2164 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2165 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2166 "%s: invalid UCS-2 character size.",
2167 function );
2168
2169 return( -1 );
2170 }
2171 /* Determine if the Unicode character is valid
2172 * UCS-2 with surrogate pairs supports upto 0x10ffff characters
2173 */
2174 if( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX )
2175 {
2176 libcerror_error_set(
2177 error,
2178 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2179 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2180 "%s: unsupported Unicode character.",
2181 function );
2182
2183 return( -1 );
2184 }
2185 if( unicode_character > LIBUNA_UNICODE_BASIC_MULTILINGUAL_PLANE_MAX )
2186 {
2187 *ucs2_character_size += 2;
2188 }
2189 else
2190 {
2191 *ucs2_character_size += 1;
2192 }
2193 return( 1 );
2194 }
2195
2196 /* Copies an Unicode character from an UCS-2 string
2197 * Returns 1 if successful or -1 on error
2198 */
2199 int libuna_unicode_character_copy_from_ucs2(
2200 libuna_unicode_character_t *unicode_character,
2201 const libuna_utf16_character_t *ucs2_string,
2202 size_t ucs2_string_size,
2203 size_t *ucs2_string_index,
2204 libcerror_error_t **error )
2205 {
2206 static char *function = "libuna_unicode_character_copy_from_ucs2";
2207 libuna_utf16_character_t ucs2_surrogate = 0;
2208 libuna_unicode_character_t safe_unicode_character = 0;
2209 size_t safe_ucs2_string_index = 0;
2210
2211 if( unicode_character == NULL )
2212 {
2213 libcerror_error_set(
2214 error,
2215 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2216 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2217 "%s: invalid Unicode character.",
2218 function );
2219
2220 return( -1 );
2221 }
2222 if( ucs2_string == NULL )
2223 {
2224 libcerror_error_set(
2225 error,
2226 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2227 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2228 "%s: invalid UCS-2 string.",
2229 function );
2230
2231 return( -1 );
2232 }
2233 if( ucs2_string_size > (size_t) SSIZE_MAX )
2234 {
2235 libcerror_error_set(
2236 error,
2237 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2238 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2239 "%s: invalid UCS-2 string size value exceeds maximum.",
2240 function );
2241
2242 return( -1 );
2243 }
2244 if( ucs2_string_index == NULL )
2245 {
2246 libcerror_error_set(
2247 error,
2248 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2249 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2250 "%s: invalid UCS-2 string index.",
2251 function );
2252
2253 return( -1 );
2254 }
2255 safe_ucs2_string_index = *ucs2_string_index;
2256
2257 if( safe_ucs2_string_index >= ucs2_string_size )
2258 {
2259 libcerror_error_set(
2260 error,
2261 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2262 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2263 "%s: UCS-2 string too small.",
2264 function );
2265
2266 return( -1 );
2267 }
2268 safe_unicode_character = ucs2_string[ safe_ucs2_string_index ];
2269 safe_ucs2_string_index += 1;
2270
2271 /* Determine if the UCS-2 character is within the high surrogate range
2272 */
2273 if( ( safe_unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
2274 && ( safe_unicode_character <= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_END ) )
2275 {
2276 if( safe_ucs2_string_index >= ucs2_string_size )
2277 {
2278 libcerror_error_set(
2279 error,
2280 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2281 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
2282 "%s: missing surrogate UCS-2 character bytes.",
2283 function );
2284
2285 return( -1 );
2286 }
2287 ucs2_surrogate = ucs2_string[ safe_ucs2_string_index ];
2288
2289 /* Determine if the UCS-2 character is within the low surrogate range
2290 */
2291 if( ( ucs2_surrogate >= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START )
2292 && ( ucs2_surrogate <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
2293 {
2294 safe_unicode_character -= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START;
2295 safe_unicode_character <<= 10;
2296 safe_unicode_character += ucs2_surrogate - LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START;
2297 safe_unicode_character += 0x010000;
2298
2299 safe_ucs2_string_index += 1;
2300 }
2301 }
2302 /* Determine if the Unicode character is valid
2303 * UCS-2 with surrogate pairs supports upto 0x10ffff characters
2304 */
2305 if( safe_unicode_character > LIBUNA_UNICODE_CHARACTER_MAX )
2306 {
2307 libcerror_error_set(
2308 error,
2309 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2310 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2311 "%s: unsupported Unicode character.",
2312 function );
2313
2314 return( -1 );
2315 }
2316 *unicode_character = safe_unicode_character;
2317 *ucs2_string_index = safe_ucs2_string_index;
2318
2319 return( 1 );
2320 }
2321
2322 /* Copies an Unicode character into a UCS-2 string
2323 * Returns 1 if successful or -1 on error
2324 */
2325 int libuna_unicode_character_copy_to_ucs2(
2326 libuna_unicode_character_t unicode_character,
2327 libuna_utf16_character_t *ucs2_string,
2328 size_t ucs2_string_size,
2329 size_t *ucs2_string_index,
2330 libcerror_error_t **error )
2331 {
2332 static char *function = "libuna_unicode_character_copy_to_ucs2";
2333 size_t safe_ucs2_string_index = 0;
2334
2335 if( ucs2_string == NULL )
2336 {
2337 libcerror_error_set(
2338 error,
2339 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2340 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2341 "%s: invalid UCS-2 string.",
2342 function );
2343
2344 return( -1 );
2345 }
2346 if( ucs2_string_size > (size_t) SSIZE_MAX )
2347 {
2348 libcerror_error_set(
2349 error,
2350 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2351 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2352 "%s: invalid UCS-2 string size value exceeds maximum.",
2353 function );
2354
2355 return( -1 );
2356 }
2357 if( ucs2_string_index == NULL )
2358 {
2359 libcerror_error_set(
2360 error,
2361 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2362 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2363 "%s: invalid UCS-2 string index.",
2364 function );
2365
2366 return( -1 );
2367 }
2368 safe_ucs2_string_index = *ucs2_string_index;
2369
2370 if( safe_ucs2_string_index >= ucs2_string_size )
2371 {
2372 libcerror_error_set(
2373 error,
2374 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2375 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2376 "%s: UCS-2 string too small.",
2377 function );
2378
2379 return( -1 );
2380 }
2381 /* Determine if the Unicode character is valid
2382 */
2383 if( unicode_character > LIBUNA_UCS_CHARACTER_MAX )
2384 {
2385 libcerror_error_set(
2386 error,
2387 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2388 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2389 "%s: unsupported Unicode character.",
2390 function );
2391
2392 return( -1 );
2393 }
2394 if( unicode_character <= LIBUNA_UNICODE_BASIC_MULTILINGUAL_PLANE_MAX )
2395 {
2396 ucs2_string[ safe_ucs2_string_index++ ] = (libuna_utf16_character_t) unicode_character;
2397 }
2398 else
2399 {
2400 if( ( ucs2_string_size < 2 )
2401 || ( safe_ucs2_string_index > ( ucs2_string_size - 2 ) ) )
2402 {
2403 libcerror_error_set(
2404 error,
2405 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2406 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2407 "%s: UCS-2 string too small.",
2408 function );
2409
2410 return( -1 );
2411 }
2412 unicode_character -= 0x010000;
2413 ucs2_string[ safe_ucs2_string_index++ ] = (libuna_utf16_character_t) ( ( unicode_character >> 10 ) + LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START );
2414 ucs2_string[ safe_ucs2_string_index++ ] = (libuna_utf16_character_t) ( ( unicode_character & 0x03ff ) + LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START );
2415 }
2416 *ucs2_string_index = safe_ucs2_string_index;
2417
2418 return( 1 );
2419 }
2420
2421 /* Determines the size of an UCS-4 character from an Unicode character
2422 * Adds the size to the UCS-4 character size value
2423 * Returns 1 if successful or -1 on error
2424 */
2425 int libuna_unicode_character_size_to_ucs4(
2426 libuna_unicode_character_t unicode_character,
2427 size_t *ucs4_character_size,
2428 libcerror_error_t **error )
2429 {
2430 static char *function = "libuna_unicode_character_size_to_ucs4";
2431
2432 LIBUNA_UNREFERENCED_PARAMETER( unicode_character )
2433
2434 if( ucs4_character_size == NULL )
2435 {
2436 libcerror_error_set(
2437 error,
2438 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2439 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2440 "%s: invalid UCS-4 character size.",
2441 function );
2442
2443 return( -1 );
2444 }
2445 if( unicode_character > LIBUNA_UCS_CHARACTER_MAX )
2446 {
2447 libcerror_error_set(
2448 error,
2449 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2450 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2451 "%s: unsupported Unicode character.",
2452 function );
2453
2454 return( -1 );
2455 }
2456 *ucs4_character_size += 1;
2457
2458 return( 1 );
2459 }
2460
2461 /* Copies an Unicode character from an UCS-4 string
2462 * Returns 1 if successful or -1 on error
2463 */
2464 int libuna_unicode_character_copy_from_ucs4(
2465 libuna_unicode_character_t *unicode_character,
2466 const libuna_utf32_character_t *ucs4_string,
2467 size_t ucs4_string_size,
2468 size_t *ucs4_string_index,
2469 libcerror_error_t **error )
2470 {
2471 static char *function = "libuna_unicode_character_copy_from_ucs4";
2472 libuna_unicode_character_t safe_unicode_character = 0;
2473 size_t safe_ucs4_string_index = 0;
2474
2475 if( unicode_character == NULL )
2476 {
2477 libcerror_error_set(
2478 error,
2479 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2480 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2481 "%s: invalid Unicode character.",
2482 function );
2483
2484 return( -1 );
2485 }
2486 if( ucs4_string == NULL )
2487 {
2488 libcerror_error_set(
2489 error,
2490 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2491 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2492 "%s: invalid UCS-4 string.",
2493 function );
2494
2495 return( -1 );
2496 }
2497 if( ucs4_string_size > (size_t) SSIZE_MAX )
2498 {
2499 libcerror_error_set(
2500 error,
2501 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2502 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2503 "%s: invalid UCS-4 string size value exceeds maximum.",
2504 function );
2505
2506 return( -1 );
2507 }
2508 if( ucs4_string_index == NULL )
2509 {
2510 libcerror_error_set(
2511 error,
2512 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2513 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2514 "%s: invalid UCS-4 string index.",
2515 function );
2516
2517 return( -1 );
2518 }
2519 safe_ucs4_string_index = *ucs4_string_index;
2520
2521 if( safe_ucs4_string_index >= ucs4_string_size )
2522 {
2523 libcerror_error_set(
2524 error,
2525 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2526 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2527 "%s: UCS-4 string too small.",
2528 function );
2529
2530 return( -1 );
2531 }
2532 safe_unicode_character = ucs4_string[ safe_ucs4_string_index ];
2533
2534 /* Determine if the Unicode character is valid
2535 */
2536 if( safe_unicode_character > LIBUNA_UNICODE_CHARACTER_MAX )
2537 {
2538 libcerror_error_set(
2539 error,
2540 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2541 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2542 "%s: unsupported Unicode character.",
2543 function );
2544
2545 return( -1 );
2546 }
2547 *unicode_character = safe_unicode_character;
2548 *ucs4_string_index = safe_ucs4_string_index + 1;
2549
2550 return( 1 );
2551 }
2552
2553 /* Copies an Unicode character into a UCS-4 string
2554 * Returns 1 if successful or -1 on error
2555 */
2556 int libuna_unicode_character_copy_to_ucs4(
2557 libuna_unicode_character_t unicode_character,
2558 libuna_utf32_character_t *ucs4_string,
2559 size_t ucs4_string_size,
2560 size_t *ucs4_string_index,
2561 libcerror_error_t **error )
2562 {
2563 static char *function = "libuna_unicode_character_copy_to_ucs4";
2564 size_t safe_ucs4_string_index = 0;
2565
2566 if( ucs4_string == NULL )
2567 {
2568 libcerror_error_set(
2569 error,
2570 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2571 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2572 "%s: invalid UCS-4 string.",
2573 function );
2574
2575 return( -1 );
2576 }
2577 if( ucs4_string_size > (size_t) SSIZE_MAX )
2578 {
2579 libcerror_error_set(
2580 error,
2581 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2582 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2583 "%s: invalid UCS-4 string size value exceeds maximum.",
2584 function );
2585
2586 return( -1 );
2587 }
2588 if( ucs4_string_index == NULL )
2589 {
2590 libcerror_error_set(
2591 error,
2592 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2593 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2594 "%s: invalid UCS-4 string index.",
2595 function );
2596
2597 return( -1 );
2598 }
2599 safe_ucs4_string_index = *ucs4_string_index;
2600
2601 if( safe_ucs4_string_index >= ucs4_string_size )
2602 {
2603 libcerror_error_set(
2604 error,
2605 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2606 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2607 "%s: UCS-4 string too small.",
2608 function );
2609
2610 return( -1 );
2611 }
2612 /* Determine if the Unicode character is valid
2613 */
2614 if( unicode_character > LIBUNA_UCS_CHARACTER_MAX )
2615 {
2616 libcerror_error_set(
2617 error,
2618 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2619 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2620 "%s: unsupported Unicode character.",
2621 function );
2622
2623 return( -1 );
2624 }
2625 ucs4_string[ safe_ucs4_string_index ] = (libuna_utf32_character_t) unicode_character;
2626
2627 *ucs4_string_index = safe_ucs4_string_index + 1;
2628
2629 return( 1 );
2630 }
2631
2632 /* Determines the size of an UTF-7 stream character from an Unicode character
2633 * Adds the size to the UTF-7 stream character size value
2634 * Returns 1 if successful or -1 on error
2635 */
2636 int libuna_unicode_character_size_to_utf7_stream(
2637 libuna_unicode_character_t unicode_character,
2638 size_t *utf7_stream_character_size,
2639 uint32_t *utf7_stream_base64_data,
2640 libcerror_error_t **error )
2641 {
2642 static char *function = "libuna_unicode_character_size_to_utf7_stream";
2643 libuna_utf16_character_t utf16_surrogate = 0;
2644 size_t safe_utf7_stream_character_size = 0;
2645 uint32_t base64_triplet = 0;
2646 uint32_t safe_utf7_stream_base64_data = 0;
2647 uint8_t base64_encode_character = 0;
2648 uint8_t byte_bit_shift = 0;
2649 uint8_t current_byte = 0;
2650 uint8_t number_of_bytes = 0;
2651
2652 if( utf7_stream_character_size == NULL )
2653 {
2654 libcerror_error_set(
2655 error,
2656 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2657 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2658 "%s: invalid UTF-7 stream character size.",
2659 function );
2660
2661 return( -1 );
2662 }
2663 if( utf7_stream_base64_data == NULL )
2664 {
2665 libcerror_error_set(
2666 error,
2667 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2668 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2669 "%s: invalid UTF-7 stream base64 data.",
2670 function );
2671
2672 return( -1 );
2673 }
2674 safe_utf7_stream_character_size = *utf7_stream_character_size;
2675 safe_utf7_stream_base64_data = *utf7_stream_base64_data;
2676
2677 /* Determine if the Unicode character is valid
2678 */
2679 if( ( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
2680 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
2681 || ( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
2682 {
2683 libcerror_error_set(
2684 error,
2685 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2686 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
2687 "%s: unsupported Unicode character.",
2688 function );
2689
2690 return( -1 );
2691 }
2692 /* The + character must be escaped
2693 */
2694 if( unicode_character == (libuna_unicode_character_t) '+' )
2695 {
2696 }
2697 /* Allow for the end of string character
2698 */
2699 else if( unicode_character == 0 )
2700 {
2701 }
2702 else if( ( unicode_character >= 256 )
2703 || ( libuna_unicode_character_utf7_valid_directly_encoded_character[ (uint8_t) unicode_character ] == 0 ) )
2704 {
2705 base64_encode_character = 1;
2706 }
2707 if( base64_encode_character == 0 )
2708 {
2709 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) != 0 )
2710 {
2711 safe_utf7_stream_base64_data = 0;
2712 }
2713 safe_utf7_stream_character_size += 1;
2714
2715 /* The + character must be escaped
2716 */
2717 if( unicode_character == (libuna_unicode_character_t) '+' )
2718 {
2719 safe_utf7_stream_character_size += 1;
2720 }
2721 }
2722 else
2723 {
2724 /* Escape the base64 encoded characters with a +
2725 */
2726 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) == 0 )
2727 {
2728 safe_utf7_stream_character_size += 1;
2729 }
2730 /* Otherwise continue the previously base64 encoded characters
2731 */
2732 else
2733 {
2734 base64_triplet = safe_utf7_stream_base64_data & 0x00ffffff;
2735 number_of_bytes = ( safe_utf7_stream_base64_data >> 24 ) & 0x03;
2736 current_byte = ( safe_utf7_stream_base64_data >> 28 ) & 0x03;
2737
2738 if( number_of_bytes > 0 )
2739 {
2740 if( safe_utf7_stream_character_size < (size_t) ( number_of_bytes + 1 ) )
2741 {
2742 libcerror_error_set(
2743 error,
2744 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2745 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2746 "%s: invalid UTF-7 stream character size value out of bounds.",
2747 function );
2748
2749 return( -1 );
2750 }
2751 /* Correct the size for the last partial base64 stream
2752 */
2753 safe_utf7_stream_character_size -= number_of_bytes + 1;
2754 }
2755 if( safe_utf7_stream_character_size < 1 )
2756 {
2757 libcerror_error_set(
2758 error,
2759 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2760 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2761 "%s: invalid UTF-7 stream character size value out of bounds.",
2762 function );
2763
2764 return( -1 );
2765 }
2766 /* Correct the size for the base64 stream termination character
2767 */
2768 safe_utf7_stream_character_size -= 1;
2769 }
2770 safe_utf7_stream_base64_data = LIBUNA_UTF7_IS_BASE64_ENCODED;
2771
2772 if( unicode_character > LIBUNA_UNICODE_BASIC_MULTILINGUAL_PLANE_MAX )
2773 {
2774 unicode_character -= 0x010000;
2775
2776 utf16_surrogate = (libuna_utf16_character_t) ( ( unicode_character >> 10 ) + LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START );
2777
2778 byte_bit_shift = 16 - ( current_byte * 8 );
2779 base64_triplet += (uint32_t) ( ( utf16_surrogate >> 8 ) & 0xff ) << byte_bit_shift;
2780 current_byte += 1;
2781 number_of_bytes += 1;
2782
2783 if( number_of_bytes == 3 )
2784 {
2785 safe_utf7_stream_character_size += 4;
2786 number_of_bytes = 0;
2787 current_byte = 0;
2788 base64_triplet = 0;
2789 }
2790 byte_bit_shift = 16 - ( current_byte * 8 );
2791 base64_triplet += (uint32_t) ( utf16_surrogate & 0xff ) << byte_bit_shift;
2792 current_byte += 1;
2793 number_of_bytes += 1;
2794
2795 if( number_of_bytes == 3 )
2796 {
2797 safe_utf7_stream_character_size += 4;
2798 number_of_bytes = 0;
2799 current_byte = 0;
2800 base64_triplet = 0;
2801 }
2802 unicode_character = (libuna_utf16_character_t) ( ( unicode_character & 0x03ff ) + LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START );
2803 }
2804 byte_bit_shift = 16 - ( current_byte * 8 );
2805 base64_triplet += (uint32_t) ( ( unicode_character >> 8 ) & 0xff ) << byte_bit_shift;
2806 current_byte += 1;
2807 number_of_bytes += 1;
2808
2809 if( number_of_bytes == 3 )
2810 {
2811 safe_utf7_stream_character_size += 4;
2812 number_of_bytes = 0;
2813 current_byte = 0;
2814 base64_triplet = 0;
2815 }
2816 byte_bit_shift = 16 - ( current_byte * 8 );
2817 base64_triplet += (uint32_t) ( unicode_character & 0xff ) << byte_bit_shift;
2818 current_byte += 1;
2819 number_of_bytes += 1;
2820
2821 if( number_of_bytes == 3 )
2822 {
2823 safe_utf7_stream_character_size += 4;
2824 number_of_bytes = 0;
2825 current_byte = 0;
2826 base64_triplet = 0;
2827 }
2828 /* Terminate the base64 encoded characters
2829 */
2830 if( number_of_bytes > 0 )
2831 {
2832 safe_utf7_stream_character_size += number_of_bytes + 1;
2833 }
2834 safe_utf7_stream_character_size += 1;
2835 }
2836 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) != 0 )
2837 {
2838 safe_utf7_stream_base64_data = LIBUNA_UTF7_IS_BASE64_ENCODED;
2839 safe_utf7_stream_base64_data |= (uint32_t) current_byte << 28;
2840 safe_utf7_stream_base64_data |= (uint32_t) number_of_bytes << 24;
2841 safe_utf7_stream_base64_data |= base64_triplet & 0x00ffffff;
2842 }
2843 *utf7_stream_character_size = safe_utf7_stream_character_size;
2844 *utf7_stream_base64_data = safe_utf7_stream_base64_data;
2845
2846 return( 1 );
2847 }
2848
2849 /* Copies an Unicode character from an UTF-7 stream
2850 * The bits of the base64 data contain:
2851 * 0 - 23 the base64 triplet
2852 * 24 - 25 the number of bytes in the triplet
2853 * 26 - 27 unused
2854 * 28 - 29 the current byte
2855 * 30 unused
2856 * 31 flag to indicate the current UTF-7 characters are (modified) base64 encoded
2857 *
2858 * Returns 1 if successful or -1 on error
2859 */
2860 int libuna_unicode_character_copy_from_utf7_stream(
2861 libuna_unicode_character_t *unicode_character,
2862 const uint8_t *utf7_stream,
2863 size_t utf7_stream_size,
2864 size_t *utf7_stream_index,
2865 uint32_t *utf7_stream_base64_data,
2866 libcerror_error_t **error )
2867 {
2868 static char *function = "libuna_unicode_character_copy_from_utf7_stream";
2869 libuna_unicode_character_t safe_unicode_character = 0;
2870 libuna_utf16_character_t utf16_surrogate = 0;
2871 size_t safe_utf7_stream_index = 0;
2872 uint32_t base64_triplet = 0;
2873 uint32_t safe_utf7_stream_base64_data = 0;
2874 uint8_t byte_bit_shift = 0;
2875 uint8_t current_byte = 0;
2876 uint8_t number_of_bytes = 0;
2877 uint8_t padding_size = 0;
2878 uint8_t utf7_character_value = 0;
2879
2880 if( unicode_character == NULL )
2881 {
2882 libcerror_error_set(
2883 error,
2884 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2885 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2886 "%s: invalid Unicode character.",
2887 function );
2888
2889 return( -1 );
2890 }
2891 if( utf7_stream == NULL )
2892 {
2893 libcerror_error_set(
2894 error,
2895 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2896 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2897 "%s: invalid UTF-7 stream.",
2898 function );
2899
2900 return( -1 );
2901 }
2902 if( utf7_stream_size > (size_t) SSIZE_MAX )
2903 {
2904 libcerror_error_set(
2905 error,
2906 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2907 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
2908 "%s: invalid UTF-7 stream size value exceeds maximum.",
2909 function );
2910
2911 return( -1 );
2912 }
2913 if( utf7_stream_index == NULL )
2914 {
2915 libcerror_error_set(
2916 error,
2917 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2918 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2919 "%s: invalid UTF-7 stream index.",
2920 function );
2921
2922 return( -1 );
2923 }
2924 if( utf7_stream_base64_data == NULL )
2925 {
2926 libcerror_error_set(
2927 error,
2928 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2929 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
2930 "%s: invalid UTF-7 base64 data.",
2931 function );
2932
2933 return( -1 );
2934 }
2935 safe_utf7_stream_index = *utf7_stream_index;
2936 safe_utf7_stream_base64_data = *utf7_stream_base64_data;
2937
2938 if( safe_utf7_stream_index >= utf7_stream_size )
2939 {
2940 libcerror_error_set(
2941 error,
2942 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
2943 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
2944 "%s: UTF-7 stream too small.",
2945 function );
2946
2947 return( -1 );
2948 }
2949 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) != 0 )
2950 {
2951 base64_triplet = safe_utf7_stream_base64_data & 0x00ffffff;
2952 number_of_bytes = ( safe_utf7_stream_base64_data >> 24 ) & 0x03;
2953 current_byte = ( safe_utf7_stream_base64_data >> 28 ) & 0x03;
2954
2955 if( current_byte >= number_of_bytes )
2956 {
2957 if( safe_utf7_stream_index >= utf7_stream_size )
2958 {
2959 libcerror_error_set(
2960 error,
2961 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2962 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2963 "%s: invalid UTF-7 stream character size value out of bounds.",
2964 function );
2965
2966 return( -1 );
2967 }
2968 utf7_character_value = utf7_stream[ safe_utf7_stream_index ];
2969
2970 /* Any character not in the modified base64 alphabet terminates the base64 encoded sequence
2971 */
2972 if( libuna_unicode_character_utf7_valid_base64_character[ utf7_character_value ] == 0 )
2973 {
2974 safe_utf7_stream_base64_data = 0;
2975 }
2976 }
2977 }
2978 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) == 0 )
2979 {
2980 if( safe_utf7_stream_index >= utf7_stream_size )
2981 {
2982 libcerror_error_set(
2983 error,
2984 LIBCERROR_ERROR_DOMAIN_RUNTIME,
2985 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
2986 "%s: invalid UTF-7 stream character size value out of bounds.",
2987 function );
2988
2989 return( -1 );
2990 }
2991 utf7_character_value = utf7_stream[ safe_utf7_stream_index ];
2992
2993 /* Determine if the character is modified base64 encoded
2994 * or a + character
2995 */
2996 if( utf7_character_value == (uint8_t) '+' )
2997 {
2998 if( ( safe_utf7_stream_index + 1 ) >= utf7_stream_size )
2999 {
3000 libcerror_error_set(
3001 error,
3002 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3003 LIBCERROR_RUNTIME_ERROR_VALUE_OUT_OF_BOUNDS,
3004 "%s: invalid UTF-7 stream character size value out of bounds.",
3005 function );
3006
3007 return( -1 );
3008 }
3009 if( utf7_stream[ safe_utf7_stream_index + 1 ] != (uint8_t) '-' )
3010 {
3011 safe_utf7_stream_base64_data = LIBUNA_UTF7_IS_BASE64_ENCODED;
3012
3013 safe_utf7_stream_index++;
3014 }
3015 }
3016 /* Allow for the end of string character
3017 */
3018 else if( utf7_character_value == 0 )
3019 {
3020 }
3021 else if( libuna_unicode_character_utf7_valid_directly_encoded_character[ utf7_character_value ] == 0 )
3022 {
3023 libcerror_error_set(
3024 error,
3025 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3026 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3027 "%s: invalid directly encoded UTF-7 character byte: 0x%02" PRIx8 ".",
3028 function,
3029 utf7_character_value );
3030
3031 return( -1 );
3032 }
3033 }
3034 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) == 0 )
3035 {
3036 safe_unicode_character = utf7_stream[ safe_utf7_stream_index++ ];
3037
3038 if( ( safe_unicode_character == (libuna_unicode_character_t) '+' )
3039 && ( utf7_stream[ safe_utf7_stream_index ] == (uint8_t) '-' ) )
3040 {
3041 safe_utf7_stream_index++;
3042 }
3043 }
3044 else if( ( number_of_bytes == 0 )
3045 || ( current_byte >= number_of_bytes ) )
3046 {
3047 if( libuna_base64_triplet_copy_from_base64_stream(
3048 &base64_triplet,
3049 utf7_stream,
3050 utf7_stream_size - 1,
3051 &safe_utf7_stream_index,
3052 &padding_size,
3053 LIBUNA_BASE64_VARIANT_UTF7,
3054 error ) != 1 )
3055 {
3056 libcerror_error_set(
3057 error,
3058 LIBCERROR_ERROR_DOMAIN_CONVERSION,
3059 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
3060 "%s: unable to copy base64 encoded UTF-7 characters.",
3061 function );
3062
3063 return( -1 );
3064 }
3065 if( padding_size > 2 )
3066 {
3067 libcerror_error_set(
3068 error,
3069 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3070 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3071 "%s: unsupported padding in base64 encoded UTF-7 characters.",
3072 function );
3073
3074 return( -1 );
3075 }
3076 number_of_bytes = 3 - padding_size;
3077 current_byte = 0;
3078 }
3079 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) != 0 )
3080 {
3081 byte_bit_shift = 16 - ( current_byte * 8 );
3082 safe_unicode_character = ( ( base64_triplet >> byte_bit_shift ) & 0x000000ffUL ) << 8;
3083 current_byte += 1;
3084
3085 if( current_byte >= number_of_bytes )
3086 {
3087 if( libuna_base64_triplet_copy_from_base64_stream(
3088 &base64_triplet,
3089 utf7_stream,
3090 utf7_stream_size - 1,
3091 &safe_utf7_stream_index,
3092 &padding_size,
3093 LIBUNA_BASE64_VARIANT_UTF7,
3094 error ) != 1 )
3095 {
3096 libcerror_error_set(
3097 error,
3098 LIBCERROR_ERROR_DOMAIN_CONVERSION,
3099 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
3100 "%s: unable to copy base64 encoded UTF-7 characters.",
3101 function );
3102
3103 return( -1 );
3104 }
3105 if( padding_size > 2 )
3106 {
3107 libcerror_error_set(
3108 error,
3109 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3110 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3111 "%s: unsupported padding in base64 encoded UTF-7 characters.",
3112 function );
3113
3114 return( -1 );
3115 }
3116 number_of_bytes = 3 - padding_size;
3117 current_byte = 0;
3118 }
3119 byte_bit_shift = 16 - ( current_byte * 8 );
3120 safe_unicode_character += ( base64_triplet >> byte_bit_shift ) & 0x000000ffUL;
3121 current_byte += 1;
3122
3123 if( ( safe_unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
3124 && ( safe_unicode_character <= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_END ) )
3125 {
3126 if( current_byte >= number_of_bytes )
3127 {
3128 if( libuna_base64_triplet_copy_from_base64_stream(
3129 &base64_triplet,
3130 utf7_stream,
3131 utf7_stream_size - 1,
3132 &safe_utf7_stream_index,
3133 &padding_size,
3134 LIBUNA_BASE64_VARIANT_UTF7,
3135 error ) != 1 )
3136 {
3137 libcerror_error_set(
3138 error,
3139 LIBCERROR_ERROR_DOMAIN_CONVERSION,
3140 LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
3141 "%s: unable to copy base64 encoded UTF-7 characters.",
3142 function );
3143
3144 return( -1 );
3145 }
3146 if( padding_size > 2 )
3147 {
3148 libcerror_error_set(
3149 error,
3150 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3151 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3152 "%s: unsupported padding in base64 encoded UTF-7 characters.",
3153 function );
3154
3155 return( -1 );
3156 }
3157 number_of_bytes = 3 - padding_size;
3158 current_byte = 0;
3159 }
3160 byte_bit_shift = 16 - ( current_byte * 8 );
3161 utf16_surrogate = ( ( base64_triplet >> byte_bit_shift ) & 0x000000ffUL ) << 8;
3162 current_byte += 1;
3163
3164 if( current_byte >= number_of_bytes )
3165 {
3166 if( libuna_base64_triplet_copy_from_base64_stream(
3167 &base64_triplet,
3168 utf7_stream,
3169 utf7_stream_size - 1,
3170 &safe_utf7_stream_index,
3171 &padding_size,
3172 LIBUNA_BASE64_VARIANT_UTF7,
3173 error ) != 1 )
3174 {
3175 libcerror_error_set(
3176 error,
3177 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3178 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
3179 "%s: unable to retrieve base64 encoded UTF-7 characters.",
3180 function );
3181
3182 return( -1 );
3183 }
3184 if( padding_size > 2 )
3185 {
3186 libcerror_error_set(
3187 error,
3188 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3189 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3190 "%s: unsupported padding in base64 encoded UTF-7 characters.",
3191 function );
3192
3193 return( -1 );
3194 }
3195 number_of_bytes = 3 - padding_size;
3196 current_byte = 0;
3197 }
3198 byte_bit_shift = 16 - ( current_byte * 8 );
3199 utf16_surrogate += ( base64_triplet >> byte_bit_shift ) & 0x000000ffUL;
3200 current_byte += 1;
3201
3202 /* Determine if the UTF-16 character is within the low surrogate range
3203 */
3204 if( ( utf16_surrogate >= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START )
3205 && ( utf16_surrogate <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
3206 {
3207 safe_unicode_character -= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START;
3208 safe_unicode_character <<= 10;
3209 safe_unicode_character += utf16_surrogate - LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START;
3210 safe_unicode_character += 0x010000;
3211 }
3212 else
3213 {
3214 libcerror_error_set(
3215 error,
3216 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3217 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
3218 "%s: unsupported low surrogate UTF-16 character.",
3219 function );
3220
3221 return( -1 );
3222 }
3223 }
3224 if( safe_utf7_stream_index >= utf7_stream_size )
3225 {
3226 libcerror_error_set(
3227 error,
3228 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3229 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3230 "%s: UTF-7 stream too small.",
3231 function );
3232
3233 return( -1 );
3234 }
3235 if( ( current_byte >= number_of_bytes )
3236 && ( utf7_stream[ safe_utf7_stream_index ] == (uint8_t) '-' ) )
3237 {
3238 safe_utf7_stream_base64_data = 0;
3239
3240 safe_utf7_stream_index++;
3241 }
3242 }
3243 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) != 0 )
3244 {
3245 safe_utf7_stream_base64_data = LIBUNA_UTF7_IS_BASE64_ENCODED;
3246 safe_utf7_stream_base64_data |= (uint32_t) current_byte << 28;
3247 safe_utf7_stream_base64_data |= (uint32_t) number_of_bytes << 24;
3248 safe_utf7_stream_base64_data |= base64_triplet & 0x00ffffff;
3249 }
3250 *unicode_character = safe_unicode_character;
3251 *utf7_stream_index = safe_utf7_stream_index;
3252 *utf7_stream_base64_data = safe_utf7_stream_base64_data;
3253
3254 return( 1 );
3255 }
3256
3257 /* Copies an Unicode character into a UTF-7 stream
3258 * The bits of the base64 data contain:
3259 * 0 - 23 the base64 triplet
3260 * 24 - 25 the number of bytes in the triplet
3261 * 26 - 27 unused
3262 * 28 - 29 the current byte
3263 * 30 unused
3264 * 31 flag to indicate the current UTF-7 characters are (modified) base64 encoded
3265 *
3266 * Returns 1 if successful or -1 on error
3267 */
3268 int libuna_unicode_character_copy_to_utf7_stream(
3269 libuna_unicode_character_t unicode_character,
3270 uint8_t *utf7_stream,
3271 size_t utf7_stream_size,
3272 size_t *utf7_stream_index,
3273 uint32_t *utf7_stream_base64_data,
3274 libcerror_error_t **error )
3275 {
3276 static char *function = "libuna_unicode_character_copy_to_utf7_stream";
3277 libuna_utf16_character_t utf16_surrogate = 0;
3278 size_t safe_utf7_stream_index = 0;
3279 uint32_t base64_triplet = 0;
3280 uint32_t safe_utf7_stream_base64_data = 0;
3281 uint8_t base64_encode_character = 0;
3282 uint8_t byte_bit_shift = 0;
3283 uint8_t current_byte = 0;
3284 uint8_t number_of_bytes = 0;
3285
3286 if( utf7_stream == NULL )
3287 {
3288 libcerror_error_set(
3289 error,
3290 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3291 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3292 "%s: invalid UTF-7 stream.",
3293 function );
3294
3295 return( -1 );
3296 }
3297 if( utf7_stream_size > (size_t) SSIZE_MAX )
3298 {
3299 libcerror_error_set(
3300 error,
3301 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3302 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3303 "%s: invalid UTF-7 stream size value exceeds maximum.",
3304 function );
3305
3306 return( -1 );
3307 }
3308 if( utf7_stream_index == NULL )
3309 {
3310 libcerror_error_set(
3311 error,
3312 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3313 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3314 "%s: invalid UTF-7 stream index.",
3315 function );
3316
3317 return( -1 );
3318 }
3319 if( utf7_stream_base64_data == NULL )
3320 {
3321 libcerror_error_set(
3322 error,
3323 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3324 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3325 "%s: invalid UTF-7 stream base64 data.",
3326 function );
3327
3328 return( -1 );
3329 }
3330 safe_utf7_stream_index = *utf7_stream_index;
3331 safe_utf7_stream_base64_data = *utf7_stream_base64_data;
3332
3333 /* Determine if the Unicode character is valid
3334 */
3335 if( ( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
3336 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
3337 || ( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
3338 {
3339 libcerror_error_set(
3340 error,
3341 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3342 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
3343 "%s: unsupported Unicode character.",
3344 function );
3345
3346 return( -1 );
3347 }
3348 /* A-Z is not a continous range on an EBCDIC based system
3349 * it consists of the ranges: A-I, J-R, S-Z
3350 */
3351 if( ( unicode_character >= 0x41 )
3352 && ( unicode_character <= 0x49 ) )
3353 {
3354 unicode_character = ( unicode_character - 0x41 ) + (libuna_unicode_character_t) 'A';
3355 }
3356 else if( ( unicode_character >= 0x4a )
3357 && ( unicode_character <= 0x52 ) )
3358 {
3359 unicode_character = ( unicode_character - 0x4a ) + (libuna_unicode_character_t) 'J';
3360 }
3361 else if( ( unicode_character >= 0x53 )
3362 && ( unicode_character <= 0x5a ) )
3363 {
3364 unicode_character = ( unicode_character - 0x53 ) + (libuna_unicode_character_t) 'S';
3365 }
3366 /* a-z is not a continous range on an EBCDIC based system
3367 * it consists of the ranges: a-i, j-r, s-z
3368 */
3369 else if( ( unicode_character >= 0x61 )
3370 && ( unicode_character <= 0x69 ) )
3371 {
3372 unicode_character = ( unicode_character - 0x61 ) + (libuna_unicode_character_t) 'a';
3373 }
3374 else if( ( unicode_character >= 0x6a )
3375 && ( unicode_character <= 0x72 ) )
3376 {
3377 unicode_character = ( unicode_character - 0x6a ) + (libuna_unicode_character_t) 'j';
3378 }
3379 else if( ( unicode_character >= 0x73 )
3380 && ( unicode_character <= 0x7a ) )
3381 {
3382 unicode_character = ( unicode_character - 0x73 ) + (libuna_unicode_character_t) 's';
3383 }
3384 /* 0-9
3385 */
3386 else if( ( unicode_character >= 0x30 )
3387 && ( unicode_character <= 0x39 ) )
3388 {
3389 unicode_character = ( unicode_character - 0x30 ) + (libuna_unicode_character_t) '0';
3390 }
3391 /* The + character must be escaped
3392 */
3393 else if( unicode_character == (libuna_unicode_character_t) '+' )
3394 {
3395 }
3396 /* Allow for the end of string character
3397 */
3398 else if( unicode_character == 0 )
3399 {
3400 }
3401 else if( ( unicode_character >= 256 )
3402 || ( libuna_unicode_character_utf7_valid_directly_encoded_character[ (uint8_t) unicode_character ] == 0 ) )
3403 {
3404 base64_encode_character = 1;
3405 }
3406 if( base64_encode_character == 0 )
3407 {
3408 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) != 0 )
3409 {
3410 safe_utf7_stream_base64_data = 0;
3411 }
3412 if( safe_utf7_stream_index >= utf7_stream_size )
3413 {
3414 libcerror_error_set(
3415 error,
3416 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3417 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3418 "%s: UTF-7 stream too small.",
3419 function );
3420
3421 return( -1 );
3422 }
3423 utf7_stream[ safe_utf7_stream_index++ ] = (uint8_t) unicode_character;
3424
3425 /* The + character must be escaped
3426 */
3427 if( unicode_character == (libuna_unicode_character_t) '+' )
3428 {
3429 if( safe_utf7_stream_index >= utf7_stream_size )
3430 {
3431 libcerror_error_set(
3432 error,
3433 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3434 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3435 "%s: UTF-7 stream too small.",
3436 function );
3437
3438 return( -1 );
3439 }
3440 utf7_stream[ safe_utf7_stream_index++ ] = (uint8_t) '-';
3441 }
3442 }
3443 else
3444 {
3445 /* Escape the base64 encoded chracters with a +
3446 */
3447 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) == 0 )
3448 {
3449 if( safe_utf7_stream_index >= utf7_stream_size )
3450 {
3451 libcerror_error_set(
3452 error,
3453 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3454 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3455 "%s: UTF-7 stream too small.",
3456 function );
3457
3458 return( -1 );
3459 }
3460 utf7_stream[ safe_utf7_stream_index++ ] = (uint8_t) '+';
3461 }
3462 /* Otherwise continue the previously base64 encoded characters
3463 */
3464 else
3465 {
3466 base64_triplet = safe_utf7_stream_base64_data & 0x00ffffff;
3467 number_of_bytes = ( safe_utf7_stream_base64_data >> 24 ) & 0x03;
3468 current_byte = ( safe_utf7_stream_base64_data >> 28 ) & 0x03;
3469
3470 if( number_of_bytes > 0 )
3471 {
3472 /* Correct the index for the last partial base64 stream
3473 */
3474 safe_utf7_stream_index -= number_of_bytes + 1;
3475 }
3476 /* Correct the index for the base64 stream termination character
3477 */
3478 safe_utf7_stream_index -= 1;
3479 }
3480 safe_utf7_stream_base64_data = LIBUNA_UTF7_IS_BASE64_ENCODED;
3481
3482 if( unicode_character > LIBUNA_UNICODE_BASIC_MULTILINGUAL_PLANE_MAX )
3483 {
3484 unicode_character -= 0x010000;
3485
3486 utf16_surrogate = (libuna_utf16_character_t) ( ( unicode_character >> 10 )
3487 + LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START );
3488
3489 byte_bit_shift = 16 - ( current_byte * 8 );
3490 base64_triplet += (uint32_t) ( ( utf16_surrogate >> 8 ) & 0xff ) << byte_bit_shift;
3491 current_byte += 1;
3492 number_of_bytes += 1;
3493
3494 if( number_of_bytes == 3 )
3495 {
3496 if( libuna_base64_triplet_copy_to_base64_stream(
3497 base64_triplet,
3498 utf7_stream,
3499 utf7_stream_size,
3500 &safe_utf7_stream_index,
3501 0,
3502 LIBUNA_BASE64_VARIANT_UTF7,
3503 error ) != 1 )
3504 {
3505 libcerror_error_set(
3506 error,
3507 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3508 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3509 "%s: unable to set base64 encoded UTF-7 characters.",
3510 function );
3511
3512 return( -1 );
3513 }
3514 number_of_bytes = 0;
3515 current_byte = 0;
3516 base64_triplet = 0;
3517 }
3518 byte_bit_shift = 16 - ( current_byte * 8 );
3519 base64_triplet += (uint32_t) ( utf16_surrogate & 0xff ) << byte_bit_shift;
3520 current_byte += 1;
3521 number_of_bytes += 1;
3522
3523 if( number_of_bytes == 3 )
3524 {
3525 if( libuna_base64_triplet_copy_to_base64_stream(
3526 base64_triplet,
3527 utf7_stream,
3528 utf7_stream_size,
3529 &safe_utf7_stream_index,
3530 0,
3531 LIBUNA_BASE64_VARIANT_UTF7,
3532 error ) != 1 )
3533 {
3534 libcerror_error_set(
3535 error,
3536 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3537 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3538 "%s: unable to set base64 encoded UTF-7 characters.",
3539 function );
3540
3541 return( -1 );
3542 }
3543 number_of_bytes = 0;
3544 current_byte = 0;
3545 base64_triplet = 0;
3546 }
3547 unicode_character = (libuna_utf16_character_t) ( ( unicode_character & 0x03ff )
3548 + LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START );
3549 }
3550 byte_bit_shift = 16 - ( current_byte * 8 );
3551 base64_triplet += (uint32_t) ( ( unicode_character >> 8 ) & 0xff ) << byte_bit_shift;
3552 current_byte += 1;
3553 number_of_bytes += 1;
3554
3555 if( number_of_bytes == 3 )
3556 {
3557 if( libuna_base64_triplet_copy_to_base64_stream(
3558 base64_triplet,
3559 utf7_stream,
3560 utf7_stream_size,
3561 &safe_utf7_stream_index,
3562 0,
3563 LIBUNA_BASE64_VARIANT_UTF7,
3564 error ) != 1 )
3565 {
3566 libcerror_error_set(
3567 error,
3568 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3569 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3570 "%s: unable to set base64 encoded UTF-7 characters.",
3571 function );
3572
3573 return( -1 );
3574 }
3575 number_of_bytes = 0;
3576 current_byte = 0;
3577 base64_triplet = 0;
3578 }
3579 byte_bit_shift = 16 - ( current_byte * 8 );
3580 base64_triplet += (uint32_t) ( unicode_character & 0xff ) << byte_bit_shift;
3581 current_byte += 1;
3582 number_of_bytes += 1;
3583
3584 if( number_of_bytes == 3 )
3585 {
3586 if( libuna_base64_triplet_copy_to_base64_stream(
3587 base64_triplet,
3588 utf7_stream,
3589 utf7_stream_size,
3590 &safe_utf7_stream_index,
3591 0,
3592 LIBUNA_BASE64_VARIANT_UTF7,
3593 error ) != 1 )
3594 {
3595 libcerror_error_set(
3596 error,
3597 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3598 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3599 "%s: unable to set base64 encoded UTF-7 characters.",
3600 function );
3601
3602 return( -1 );
3603 }
3604 number_of_bytes = 0;
3605 current_byte = 0;
3606 base64_triplet = 0;
3607 }
3608 /* Terminate the base64 encoded characters
3609 */
3610 if( number_of_bytes > 0 )
3611 {
3612 if( libuna_base64_triplet_copy_to_base64_stream(
3613 base64_triplet,
3614 utf7_stream,
3615 utf7_stream_size,
3616 &safe_utf7_stream_index,
3617 3 - number_of_bytes,
3618 LIBUNA_BASE64_VARIANT_UTF7,
3619 error ) != 1 )
3620 {
3621 libcerror_error_set(
3622 error,
3623 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3624 LIBCERROR_RUNTIME_ERROR_SET_FAILED,
3625 "%s: unable to set base64 encoded UTF-7 characters.",
3626 function );
3627
3628 return( -1 );
3629 }
3630 }
3631 if( safe_utf7_stream_index >= utf7_stream_size )
3632 {
3633 libcerror_error_set(
3634 error,
3635 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3636 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3637 "%s: UTF-7 stream too small.",
3638 function );
3639
3640 return( -1 );
3641 }
3642 utf7_stream[ safe_utf7_stream_index++ ] = (uint8_t) '-';
3643 }
3644 if( ( safe_utf7_stream_base64_data & LIBUNA_UTF7_IS_BASE64_ENCODED ) != 0 )
3645 {
3646 safe_utf7_stream_base64_data = LIBUNA_UTF7_IS_BASE64_ENCODED;
3647 safe_utf7_stream_base64_data |= (uint32_t) current_byte << 28;
3648 safe_utf7_stream_base64_data |= (uint32_t) number_of_bytes << 24;
3649 safe_utf7_stream_base64_data |= base64_triplet & 0x00ffffff;
3650 }
3651 *utf7_stream_index = safe_utf7_stream_index;
3652 *utf7_stream_base64_data = safe_utf7_stream_base64_data;
3653
3654 return( 1 );
3655 }
3656
3657 /* Determines the size of an UTF-8 character from an Unicode character
3658 * This function supports upto U+10FFFF (4 byte UTF-8 characters)
3659 * Adds the size to the UTF-8 character size value
3660 * Returns 1 if successful or -1 on error
3661 */
3662 int libuna_unicode_character_size_to_utf8(
3663 libuna_unicode_character_t unicode_character,
3664 size_t *utf8_character_size,
3665 libcerror_error_t **error )
3666 {
3667 static char *function = "libuna_unicode_character_size_to_utf8";
3668 size_t safe_utf8_character_size = 0;
3669
3670 if( utf8_character_size == NULL )
3671 {
3672 libcerror_error_set(
3673 error,
3674 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3675 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3676 "%s: invalid UTF-8 character size.",
3677 function );
3678
3679 return( -1 );
3680 }
3681 /* Determine if the Unicode character is valid
3682 */
3683 if( ( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
3684 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
3685 || ( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
3686 {
3687 libcerror_error_set(
3688 error,
3689 LIBCERROR_ERROR_DOMAIN_RUNTIME,
3690 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
3691 "%s: unsupported Unicode character.",
3692 function );
3693
3694 return( -1 );
3695 }
3696 /* RFC 3629 limits the UTF-8 character to consist of a maximum of 4 bytes
3697 * while its predecessor RFC 2279 allowed up to 6 bytes
3698 */
3699 if( unicode_character < 0x00000080UL )
3700 {
3701 safe_utf8_character_size += 1;
3702 }
3703 else if( unicode_character < 0x00000800UL )
3704 {
3705 safe_utf8_character_size += 2;
3706 }
3707 else if( unicode_character < 0x00010000UL )
3708 {
3709 safe_utf8_character_size += 3;
3710 }
3711 else
3712 {
3713 safe_utf8_character_size += 4;
3714 }
3715 *utf8_character_size += safe_utf8_character_size;
3716
3717 return( 1 );
3718 }
3719
3720 /* Copies an Unicode character from an UTF-8 string
3721 * This function supports upto U+10FFFF (4 byte UTF-8 characters)
3722 * Returns 1 if successful or -1 on error
3723 */
3724 int libuna_unicode_character_copy_from_utf8(
3725 libuna_unicode_character_t *unicode_character,
3726 const libuna_utf8_character_t *utf8_string,
3727 size_t utf8_string_size,
3728 size_t *utf8_string_index,
3729 libcerror_error_t **error )
3730 {
3731 static char *function = "libuna_unicode_character_copy_from_utf8";
3732 libuna_unicode_character_t safe_unicode_character = 0;
3733 size_t safe_utf8_string_index = 0;
3734 uint8_t byte_value1 = 0;
3735 uint8_t byte_value2 = 0;
3736 uint8_t byte_value3 = 0;
3737 uint8_t byte_value4 = 0;
3738 uint8_t utf8_character_additional_bytes = 0;
3739 int result = 0;
3740
3741 if( unicode_character == NULL )
3742 {
3743 libcerror_error_set(
3744 error,
3745 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3746 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3747 "%s: invalid Unicode character.",
3748 function );
3749
3750 return( -1 );
3751 }
3752 if( utf8_string == NULL )
3753 {
3754 libcerror_error_set(
3755 error,
3756 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3757 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3758 "%s: invalid UTF-8 string.",
3759 function );
3760
3761 return( -1 );
3762 }
3763 if( utf8_string_size > (size_t) SSIZE_MAX )
3764 {
3765 libcerror_error_set(
3766 error,
3767 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3768 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
3769 "%s: invalid UTF-8 string size value exceeds maximum.",
3770 function );
3771
3772 return( -1 );
3773 }
3774 if( utf8_string_index == NULL )
3775 {
3776 libcerror_error_set(
3777 error,
3778 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3779 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
3780 "%s: invalid UTF-8 string index.",
3781 function );
3782
3783 return( -1 );
3784 }
3785 safe_utf8_string_index = *utf8_string_index;
3786
3787 if( safe_utf8_string_index >= utf8_string_size )
3788 {
3789 libcerror_error_set(
3790 error,
3791 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3792 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3793 "%s: UTF-8 string too small.",
3794 function );
3795
3796 return( -1 );
3797 }
3798 /* Determine the number of additional bytes of the UTF-8 character
3799 */
3800 byte_value1 = utf8_string[ safe_utf8_string_index ];
3801
3802 /* Determine the UTF-8 character and make sure it is valid
3803 * RFC 3629 limits the UTF-8 character to consist of a maximum of 4 bytes
3804 * while its predecessor RFC 2279 allowed up to 6 bytes
3805 */
3806 if( byte_value1 > 0xf4 )
3807 {
3808 libcerror_error_set(
3809 error,
3810 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3811 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3812 "%s: invalid 1st UTF-8 character byte: 0x%02" PRIx8 ".",
3813 function,
3814 byte_value1 );
3815
3816 return( -1 );
3817 }
3818 if( byte_value1 < 0xc0 )
3819 {
3820 utf8_character_additional_bytes = 0;
3821 }
3822 else if( byte_value1 < 0xe0 )
3823 {
3824 utf8_character_additional_bytes = 1;
3825 }
3826 else if( byte_value1 < 0xf0 )
3827 {
3828 utf8_character_additional_bytes = 2;
3829 }
3830 else
3831 {
3832 utf8_character_additional_bytes = 3;
3833 }
3834 if( ( ( (size_t) utf8_character_additional_bytes + 1 ) > utf8_string_size )
3835 || ( safe_utf8_string_index > ( utf8_string_size - ( utf8_character_additional_bytes + 1 ) ) ) )
3836 {
3837 libcerror_error_set(
3838 error,
3839 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3840 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
3841 "%s: missing UTF-8 character bytes.",
3842 function );
3843
3844 return( -1 );
3845 }
3846 safe_unicode_character = byte_value1;
3847
3848 if( utf8_character_additional_bytes == 0 )
3849 {
3850 if( byte_value1 >= 0x80 )
3851 {
3852 libcerror_error_set(
3853 error,
3854 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3855 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3856 "%s: invalid 1st UTF-8 character byte: 0x%02" PRIx8 ".",
3857 function,
3858 byte_value1 );
3859
3860 return( -1 );
3861 }
3862 }
3863 if( utf8_character_additional_bytes >= 1 )
3864 {
3865 byte_value2 = utf8_string[ safe_utf8_string_index + 1 ];
3866
3867 if( ( byte_value2 < 0x80 )
3868 || ( byte_value2 > 0xbf ) )
3869 {
3870 libcerror_error_set(
3871 error,
3872 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3873 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3874 "%s: invalid 2nd UTF-8 character byte: 0x%02" PRIx8 ".",
3875 function,
3876 byte_value2 );
3877
3878 return( -1 );
3879 }
3880 result = 1;
3881
3882 switch( byte_value1 )
3883 {
3884 case 0xe0:
3885 if( ( byte_value2 < 0xa0 )
3886 || ( byte_value2 > 0xbf ) )
3887 {
3888 result = 0;
3889 }
3890 break;
3891
3892 case 0xed:
3893 if( ( byte_value2 < 0x80 )
3894 || ( byte_value2 > 0x9f ) )
3895 {
3896 result = 0;
3897 }
3898 break;
3899
3900 case 0xf0:
3901 if( ( byte_value2 < 0x90 )
3902 || ( byte_value2 > 0xbf ) )
3903 {
3904 result = 0;
3905 }
3906 break;
3907
3908 case 0xf4:
3909 if( ( byte_value2 < 0x80 )
3910 || ( byte_value2 > 0xbf ) )
3911 {
3912 result = 0;
3913 }
3914 break;
3915
3916 default:
3917 break;
3918 }
3919 if( result == 0 )
3920 {
3921 libcerror_error_set(
3922 error,
3923 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3924 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3925 "%s: invalid 1st and 2nd UTF-8 character byte pair: 0x%02" PRIx8 " 0x%02" PRIx8 ".",
3926 function,
3927 byte_value1,
3928 byte_value2 );
3929
3930 return( -1 );
3931 }
3932 safe_unicode_character <<= 6;
3933 safe_unicode_character += byte_value2;
3934
3935 if( utf8_character_additional_bytes == 1 )
3936 {
3937 safe_unicode_character -= 0x03080;
3938 }
3939 }
3940 if( utf8_character_additional_bytes >= 2 )
3941 {
3942 byte_value3 = utf8_string[ safe_utf8_string_index + 2 ];
3943
3944 if( ( byte_value3 < 0x80 )
3945 || ( byte_value3 > 0xbf ) )
3946 {
3947 libcerror_error_set(
3948 error,
3949 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3950 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3951 "%s: invalid 3rd UTF-8 character byte: 0x%02" PRIx8 ".",
3952 function,
3953 byte_value3 );
3954
3955 return( -1 );
3956 }
3957 result = 1;
3958
3959 switch( byte_value2 )
3960 {
3961 case 0xe0:
3962 if( ( byte_value2 < 0xa0 )
3963 || ( byte_value2 > 0xbf ) )
3964 {
3965 result = 0;
3966 }
3967 break;
3968
3969 case 0xed:
3970 if( ( byte_value2 < 0x80 )
3971 || ( byte_value2 > 0x9f ) )
3972 {
3973 result = 0;
3974 }
3975 break;
3976
3977 default:
3978 break;
3979 }
3980 if( result == 0 )
3981 {
3982 libcerror_error_set(
3983 error,
3984 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
3985 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
3986 "%s: invalid 2nd and 3rd UTF-8 character byte pair: 0x%02" PRIx8 " 0x%02" PRIx8 ".",
3987 function,
3988 byte_value2,
3989 byte_value3 );
3990
3991 return( -1 );
3992 }
3993 safe_unicode_character <<= 6;
3994 safe_unicode_character += byte_value3;
3995
3996 if( utf8_character_additional_bytes == 2 )
3997 {
3998 safe_unicode_character -= 0x0e2080;
3999 }
4000 }
4001 if( utf8_character_additional_bytes >= 3 )
4002 {
4003 byte_value4 = utf8_string[ safe_utf8_string_index + 3 ];
4004
4005 if( ( byte_value4 < 0x80 )
4006 || ( byte_value4 > 0xbf ) )
4007 {
4008 libcerror_error_set(
4009 error,
4010 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4011 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
4012 "%s: invalid 4th UTF-8 character byte: 0x%02" PRIx8 ".",
4013 function,
4014 byte_value4 );
4015
4016 return( -1 );
4017 }
4018 safe_unicode_character <<= 6;
4019 safe_unicode_character += byte_value4;
4020
4021 if( utf8_character_additional_bytes == 3 )
4022 {
4023 safe_unicode_character -= 0x03c82080;
4024 }
4025 }
4026 /* Determine if the Unicode character is valid
4027 */
4028 if( ( ( safe_unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
4029 && ( safe_unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
4030 || ( safe_unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
4031 {
4032 libcerror_error_set(
4033 error,
4034 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4035 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4036 "%s: unsupported Unicode character.",
4037 function );
4038
4039 return( -1 );
4040 }
4041 *unicode_character = safe_unicode_character;
4042 *utf8_string_index = safe_utf8_string_index + 1 + utf8_character_additional_bytes;
4043
4044 return( 1 );
4045 }
4046
4047 /* Copies an Unicode character into a UTF-8 string
4048 * This function supports upto U+10FFFF (4 byte UTF-8 characters)
4049 * Returns 1 if successful or -1 on error
4050 */
4051 int libuna_unicode_character_copy_to_utf8(
4052 libuna_unicode_character_t unicode_character,
4053 libuna_utf8_character_t *utf8_string,
4054 size_t utf8_string_size,
4055 size_t *utf8_string_index,
4056 libcerror_error_t **error )
4057 {
4058 static char *function = "libuna_unicode_character_copy_to_utf8";
4059 size_t safe_utf8_string_index = 0;
4060 size_t utf8_character_iterator = 0;
4061 uint8_t utf8_character_additional_bytes = 0;
4062 uint8_t utf8_first_character_mark = 0;
4063
4064 if( utf8_string == NULL )
4065 {
4066 libcerror_error_set(
4067 error,
4068 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4069 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4070 "%s: invalid UTF-8 string.",
4071 function );
4072
4073 return( -1 );
4074 }
4075 if( utf8_string_size > (size_t) SSIZE_MAX )
4076 {
4077 libcerror_error_set(
4078 error,
4079 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4080 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
4081 "%s: invalid UTF-8 string size value exceeds maximum.",
4082 function );
4083
4084 return( -1 );
4085 }
4086 if( utf8_string_index == NULL )
4087 {
4088 libcerror_error_set(
4089 error,
4090 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4091 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4092 "%s: invalid UTF-8 string index.",
4093 function );
4094
4095 return( -1 );
4096 }
4097 safe_utf8_string_index = *utf8_string_index;
4098
4099 if( safe_utf8_string_index >= utf8_string_size )
4100 {
4101 libcerror_error_set(
4102 error,
4103 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4104 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
4105 "%s: UTF-8 string too small.",
4106 function );
4107
4108 return( -1 );
4109 }
4110 /* Determine if the Unicode character is valid
4111 */
4112 if( ( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
4113 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
4114 || ( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
4115 {
4116 libcerror_error_set(
4117 error,
4118 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4119 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4120 "%s: unsupported Unicode character.",
4121 function );
4122
4123 return( -1 );
4124 }
4125 /* Determine how many UTF-8 character bytes are required
4126 */
4127 if( unicode_character < 0x080 )
4128 {
4129 utf8_character_additional_bytes = 0;
4130 utf8_first_character_mark = 0;
4131 }
4132 else if( unicode_character < 0x0800 )
4133 {
4134 utf8_character_additional_bytes = 1;
4135 utf8_first_character_mark = 0x0c0;
4136 }
4137 else if( unicode_character < 0x010000 )
4138 {
4139 utf8_character_additional_bytes = 2;
4140 utf8_first_character_mark = 0x0e0;
4141 }
4142 else
4143 {
4144 utf8_character_additional_bytes = 3;
4145 utf8_first_character_mark = 0x0f0;
4146 }
4147 /* Convert Unicode character into UTF-8 character bytes
4148 */
4149 if( ( utf8_character_additional_bytes > utf8_string_size )
4150 || ( safe_utf8_string_index >= ( utf8_string_size - utf8_character_additional_bytes ) ) )
4151 {
4152 libcerror_error_set(
4153 error,
4154 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4155 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
4156 "%s: UTF-8 string too small.",
4157 function );
4158
4159 return( -1 );
4160 }
4161 for( utf8_character_iterator = safe_utf8_string_index + utf8_character_additional_bytes;
4162 utf8_character_iterator > safe_utf8_string_index;
4163 utf8_character_iterator-- )
4164 {
4165 utf8_string[ utf8_character_iterator ] = (libuna_utf8_character_t) ( ( unicode_character & 0x0bf ) | 0x080 );
4166
4167 unicode_character >>= 6;
4168 }
4169 utf8_string[ safe_utf8_string_index ] = (libuna_utf8_character_t) ( unicode_character | utf8_first_character_mark );
4170
4171 *utf8_string_index = safe_utf8_string_index + 1 + utf8_character_additional_bytes;
4172
4173 return( 1 );
4174 }
4175
4176 /* Determines the size of an UTF-8 character from an Unicode character
4177 * This function supports upto U+7FFFFFF (6 byte UTF-8 characters)
4178 * Adds the size to the UTF-8 character size value
4179 * Returns 1 if successful or -1 on error
4180 */
4181 int libuna_unicode_character_size_to_utf8_rfc2279(
4182 libuna_unicode_character_t unicode_character,
4183 size_t *utf8_character_size,
4184 libcerror_error_t **error )
4185 {
4186 static char *function = "libuna_unicode_character_size_to_utf8_rfc2279";
4187 size_t safe_utf8_character_size = 0;
4188
4189 if( utf8_character_size == NULL )
4190 {
4191 libcerror_error_set(
4192 error,
4193 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4194 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4195 "%s: invalid UTF-8 character size.",
4196 function );
4197
4198 return( -1 );
4199 }
4200 if( unicode_character > LIBUNA_UCS_CHARACTER_MAX )
4201 {
4202 libcerror_error_set(
4203 error,
4204 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4205 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4206 "%s: unsupported Unicode character.",
4207 function );
4208
4209 return( -1 );
4210 }
4211 /* RFC 3629 limits the UTF-8 character to consist of a maximum of 4 bytes
4212 * while its predecessor RFC 2279 allowed up to 6 bytes
4213 */
4214 if( unicode_character < 0x00000080UL )
4215 {
4216 safe_utf8_character_size += 1;
4217 }
4218 else if( unicode_character < 0x00000800UL )
4219 {
4220 safe_utf8_character_size += 2;
4221 }
4222 else if( unicode_character < 0x00010000UL )
4223 {
4224 safe_utf8_character_size += 3;
4225 }
4226 else if( unicode_character < 0x00200000UL )
4227 {
4228 safe_utf8_character_size += 4;
4229 }
4230 else if( unicode_character < 0x04000000UL )
4231 {
4232 safe_utf8_character_size += 5;
4233 }
4234 else
4235 {
4236 safe_utf8_character_size += 6;
4237 }
4238 *utf8_character_size += safe_utf8_character_size;
4239
4240 return( 1 );
4241 }
4242
4243 /* Copies an Unicode character from an UTF-8 string
4244 * This function supports upto U+7FFFFFF (6 byte UTF-8 characters)
4245 * Returns 1 if successful or -1 on error
4246 */
4247 int libuna_unicode_character_copy_from_utf8_rfc2279(
4248 libuna_unicode_character_t *unicode_character,
4249 const libuna_utf8_character_t *utf8_string,
4250 size_t utf8_string_size,
4251 size_t *utf8_string_index,
4252 libcerror_error_t **error )
4253 {
4254 static char *function = "libuna_unicode_character_copy_from_utf8_rfc2279";
4255 libuna_unicode_character_t safe_unicode_character = 0;
4256 size_t safe_utf8_string_index = 0;
4257 uint8_t byte_value1 = 0;
4258 uint8_t byte_value2 = 0;
4259 uint8_t byte_value3 = 0;
4260 uint8_t byte_value4 = 0;
4261 uint8_t byte_value5 = 0;
4262 uint8_t byte_value6 = 0;
4263 uint8_t utf8_character_additional_bytes = 0;
4264
4265 if( unicode_character == NULL )
4266 {
4267 libcerror_error_set(
4268 error,
4269 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4270 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4271 "%s: invalid Unicode character.",
4272 function );
4273
4274 return( -1 );
4275 }
4276 if( utf8_string == NULL )
4277 {
4278 libcerror_error_set(
4279 error,
4280 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4281 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4282 "%s: invalid UTF-8 string.",
4283 function );
4284
4285 return( -1 );
4286 }
4287 if( utf8_string_size > (size_t) SSIZE_MAX )
4288 {
4289 libcerror_error_set(
4290 error,
4291 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4292 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
4293 "%s: invalid UTF-8 string size value exceeds maximum.",
4294 function );
4295
4296 return( -1 );
4297 }
4298 if( utf8_string_index == NULL )
4299 {
4300 libcerror_error_set(
4301 error,
4302 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4303 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4304 "%s: invalid UTF-8 string index.",
4305 function );
4306
4307 return( -1 );
4308 }
4309 safe_utf8_string_index = *utf8_string_index;
4310
4311 if( safe_utf8_string_index >= utf8_string_size )
4312 {
4313 libcerror_error_set(
4314 error,
4315 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4316 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
4317 "%s: UTF-8 string too small.",
4318 function );
4319
4320 return( -1 );
4321 }
4322 /* Determine the number of additional bytes of the UTF-8 character
4323 */
4324 byte_value1 = utf8_string[ safe_utf8_string_index ];
4325
4326 /* Determine the UTF-8 character and make sure it is valid
4327 * RFC 3629 limits the UTF-8 character to consist of a maximum of 4 bytes
4328 * while its predecessor RFC 2279 allowed up to 6 bytes
4329 */
4330 if( byte_value1 > 0xfd )
4331 {
4332 libcerror_error_set(
4333 error,
4334 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4335 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
4336 "%s: invalid 1st UTF-8 character byte: 0x%02" PRIx8 ".",
4337 function,
4338 byte_value1 );
4339
4340 return( -1 );
4341 }
4342 if( byte_value1 < 0xc0 )
4343 {
4344 utf8_character_additional_bytes = 0;
4345 }
4346 else if( byte_value1 < 0xe0 )
4347 {
4348 utf8_character_additional_bytes = 1;
4349 }
4350 else if( byte_value1 < 0xf0 )
4351 {
4352 utf8_character_additional_bytes = 2;
4353 }
4354 else if( byte_value1 < 0xf8 )
4355 {
4356 utf8_character_additional_bytes = 3;
4357 }
4358 else if( byte_value1 < 0xfc )
4359 {
4360 utf8_character_additional_bytes = 4;
4361 }
4362 else
4363 {
4364 utf8_character_additional_bytes = 5;
4365 }
4366 if( ( ( (size_t) utf8_character_additional_bytes + 1 ) > utf8_string_size )
4367 || ( safe_utf8_string_index > ( utf8_string_size - ( utf8_character_additional_bytes + 1 ) ) ) )
4368 {
4369 libcerror_error_set(
4370 error,
4371 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4372 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
4373 "%s: missing UTF-8 character bytes.",
4374 function );
4375
4376 return( -1 );
4377 }
4378 /* Determine the UTF-8 character and make sure it is valid
4379 * RFC 3629 limits the UTF-8 character to consist of a maximum of 4 bytes
4380 * while its predecessor RFC 2279 allowed up to 6 bytes
4381 */
4382 safe_unicode_character = byte_value1;
4383
4384 if( utf8_character_additional_bytes == 0 )
4385 {
4386 if( byte_value1 >= 0x80 )
4387 {
4388 libcerror_error_set(
4389 error,
4390 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4391 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
4392 "%s: invalid 1st UTF-8 character byte: 0x%02" PRIx8 ".",
4393 function,
4394 byte_value1 );
4395
4396 return( -1 );
4397 }
4398 }
4399 if( utf8_character_additional_bytes >= 1 )
4400 {
4401 byte_value2 = utf8_string[ safe_utf8_string_index + 1 ];
4402
4403 if( ( byte_value2 < 0x80 )
4404 || ( byte_value2 > 0xbf ) )
4405 {
4406 libcerror_error_set(
4407 error,
4408 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4409 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
4410 "%s: invalid 2nd UTF-8 character byte: 0x%02" PRIx8 ".",
4411 function,
4412 byte_value2 );
4413
4414 return( -1 );
4415 }
4416 safe_unicode_character <<= 6;
4417 safe_unicode_character += byte_value2;
4418
4419 if( utf8_character_additional_bytes == 1 )
4420 {
4421 safe_unicode_character -= 0x03080;
4422 }
4423 }
4424 if( utf8_character_additional_bytes >= 2 )
4425 {
4426 byte_value3 = utf8_string[ safe_utf8_string_index + 2 ];
4427
4428 if( ( byte_value3 < 0x80 )
4429 || ( byte_value3 > 0xbf ) )
4430 {
4431 libcerror_error_set(
4432 error,
4433 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4434 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
4435 "%s: invalid 3rd UTF-8 character byte: 0x%02" PRIx8 ".",
4436 function,
4437 byte_value3 );
4438
4439 return( -1 );
4440 }
4441 safe_unicode_character <<= 6;
4442 safe_unicode_character += byte_value3;
4443
4444 if( utf8_character_additional_bytes == 2 )
4445 {
4446 safe_unicode_character -= 0x0e2080;
4447 }
4448 }
4449 if( utf8_character_additional_bytes >= 3 )
4450 {
4451 byte_value4 = utf8_string[ safe_utf8_string_index + 3 ];
4452
4453 if( ( byte_value4 < 0x80 )
4454 || ( byte_value4 > 0xbf ) )
4455 {
4456 libcerror_error_set(
4457 error,
4458 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4459 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
4460 "%s: invalid 4th UTF-8 character byte: 0x%02" PRIx8 ".",
4461 function,
4462 byte_value4 );
4463
4464 return( -1 );
4465 }
4466 safe_unicode_character <<= 6;
4467 safe_unicode_character += byte_value4;
4468
4469 if( utf8_character_additional_bytes == 3 )
4470 {
4471 safe_unicode_character -= 0x03c82080;
4472 }
4473 }
4474 if( utf8_character_additional_bytes >= 4 )
4475 {
4476 byte_value5 = utf8_string[ safe_utf8_string_index + 4 ];
4477
4478 if( ( byte_value5 < 0x80 )
4479 || ( byte_value5 > 0xbf ) )
4480 {
4481 libcerror_error_set(
4482 error,
4483 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4484 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
4485 "%s: invalid 5th UTF-8 character byte: 0x%02" PRIx8 ".",
4486 function,
4487 byte_value5 );
4488
4489 return( -1 );
4490 }
4491 safe_unicode_character <<= 6;
4492 safe_unicode_character += byte_value5;
4493
4494 if( utf8_character_additional_bytes == 4 )
4495 {
4496 safe_unicode_character -= 0x0fa082080;
4497 }
4498 }
4499 if( utf8_character_additional_bytes == 5 )
4500 {
4501 byte_value6 = utf8_string[ safe_utf8_string_index + 5 ];
4502
4503 if( ( byte_value6 < 0x80 )
4504 || ( byte_value6 > 0xbf ) )
4505 {
4506 libcerror_error_set(
4507 error,
4508 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4509 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
4510 "%s: invalid 6th UTF-8 character byte: 0x%02" PRIx8 ".",
4511 function,
4512 byte_value6 );
4513
4514 return( -1 );
4515 }
4516 safe_unicode_character <<= 6;
4517 safe_unicode_character += byte_value6;
4518 safe_unicode_character -= 0x082082080;
4519 }
4520 /* Determine if the Unicode character is valid
4521 */
4522 if( safe_unicode_character > LIBUNA_UCS_CHARACTER_MAX )
4523 {
4524 libcerror_error_set(
4525 error,
4526 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4527 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4528 "%s: unsupported Unicode character.",
4529 function );
4530
4531 return( -1 );
4532 }
4533 *unicode_character = safe_unicode_character;
4534 *utf8_string_index = safe_utf8_string_index + 1 + utf8_character_additional_bytes;
4535
4536 return( 1 );
4537 }
4538
4539 /* Copies an Unicode character into a UTF-8 string
4540 * This function supports upto U+7FFFFFF (6 byte UTF-8 characters)
4541 * Returns 1 if successful or -1 on error
4542 */
4543 int libuna_unicode_character_copy_to_utf8_rfc2279(
4544 libuna_unicode_character_t unicode_character,
4545 libuna_utf8_character_t *utf8_string,
4546 size_t utf8_string_size,
4547 size_t *utf8_string_index,
4548 libcerror_error_t **error )
4549 {
4550 static char *function = "libuna_unicode_character_copy_to_utf8_rfc2279";
4551 size_t safe_utf8_string_index = 0;
4552 size_t utf8_character_iterator = 0;
4553 uint8_t utf8_character_additional_bytes = 0;
4554 uint8_t utf8_first_character_mark = 0;
4555
4556 if( utf8_string == NULL )
4557 {
4558 libcerror_error_set(
4559 error,
4560 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4561 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4562 "%s: invalid UTF-8 string.",
4563 function );
4564
4565 return( -1 );
4566 }
4567 if( utf8_string_size > (size_t) SSIZE_MAX )
4568 {
4569 libcerror_error_set(
4570 error,
4571 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4572 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
4573 "%s: invalid UTF-8 string size value exceeds maximum.",
4574 function );
4575
4576 return( -1 );
4577 }
4578 if( utf8_string_index == NULL )
4579 {
4580 libcerror_error_set(
4581 error,
4582 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4583 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4584 "%s: invalid UTF-8 string index.",
4585 function );
4586
4587 return( -1 );
4588 }
4589 safe_utf8_string_index = *utf8_string_index;
4590
4591 if( safe_utf8_string_index >= utf8_string_size )
4592 {
4593 libcerror_error_set(
4594 error,
4595 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4596 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
4597 "%s: UTF-8 string too small.",
4598 function );
4599
4600 return( -1 );
4601 }
4602 /* Determine if the Unicode character is valid
4603 */
4604 if( unicode_character > LIBUNA_UCS_CHARACTER_MAX )
4605 {
4606 libcerror_error_set(
4607 error,
4608 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4609 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4610 "%s: unsupported Unicode character.",
4611 function );
4612
4613 return( -1 );
4614 }
4615 /* Determine how many UTF-8 character bytes are required
4616 */
4617 if( unicode_character < 0x080 )
4618 {
4619 utf8_character_additional_bytes = 0;
4620 utf8_first_character_mark = 0;
4621 }
4622 else if( unicode_character < 0x0800 )
4623 {
4624 utf8_character_additional_bytes = 1;
4625 utf8_first_character_mark = 0x0c0;
4626 }
4627 else if( unicode_character < 0x010000 )
4628 {
4629 utf8_character_additional_bytes = 2;
4630 utf8_first_character_mark = 0x0e0;
4631 }
4632 else if( unicode_character < 0x0200000 )
4633 {
4634 utf8_character_additional_bytes = 3;
4635 utf8_first_character_mark = 0x0f0;
4636 }
4637 else if( unicode_character < 0x0400000 )
4638 {
4639 utf8_character_additional_bytes = 4;
4640 utf8_first_character_mark = 0x0f8;
4641 }
4642 else
4643 {
4644 utf8_character_additional_bytes = 5;
4645 utf8_first_character_mark = 0x0fc;
4646 }
4647 /* Convert Unicode character into UTF-8 character bytes
4648 */
4649 if( ( utf8_character_additional_bytes > utf8_string_size )
4650 || ( safe_utf8_string_index >= ( utf8_string_size - utf8_character_additional_bytes ) ) )
4651 {
4652 libcerror_error_set(
4653 error,
4654 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4655 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
4656 "%s: UTF-8 string too small.",
4657 function );
4658
4659 return( -1 );
4660 }
4661 for( utf8_character_iterator = safe_utf8_string_index + utf8_character_additional_bytes;
4662 utf8_character_iterator > safe_utf8_string_index;
4663 utf8_character_iterator-- )
4664 {
4665 utf8_string[ utf8_character_iterator ] = (libuna_utf8_character_t) ( ( unicode_character & 0x0bf ) | 0x080 );
4666
4667 unicode_character >>= 6;
4668 }
4669 utf8_string[ safe_utf8_string_index ] = (libuna_utf8_character_t) ( unicode_character | utf8_first_character_mark );
4670
4671 *utf8_string_index = safe_utf8_string_index + 1 + utf8_character_additional_bytes;
4672
4673 return( 1 );
4674 }
4675
4676 /* Determines the size of an UTF-16 character from an Unicode character
4677 * Adds the size to the UTF-16 character size value
4678 * Returns 1 if successful or -1 on error
4679 */
4680 int libuna_unicode_character_size_to_utf16(
4681 libuna_unicode_character_t unicode_character,
4682 size_t *utf16_character_size,
4683 libcerror_error_t **error )
4684 {
4685 static char *function = "libuna_unicode_character_size_to_utf16";
4686
4687 if( utf16_character_size == NULL )
4688 {
4689 libcerror_error_set(
4690 error,
4691 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4692 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4693 "%s: invalid UTF-16 character size.",
4694 function );
4695
4696 return( -1 );
4697 }
4698 if( ( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
4699 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
4700 || ( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
4701 {
4702 libcerror_error_set(
4703 error,
4704 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4705 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4706 "%s: unsupported Unicode character.",
4707 function );
4708
4709 return( -1 );
4710 }
4711 if( unicode_character > LIBUNA_UNICODE_BASIC_MULTILINGUAL_PLANE_MAX )
4712 {
4713 *utf16_character_size += 2;
4714 }
4715 else
4716 {
4717 *utf16_character_size += 1;
4718 }
4719 return( 1 );
4720 }
4721
4722 /* Copies an Unicode character from an UTF-16 string
4723 * Returns 1 if successful or -1 on error
4724 */
4725 int libuna_unicode_character_copy_from_utf16(
4726 libuna_unicode_character_t *unicode_character,
4727 const libuna_utf16_character_t *utf16_string,
4728 size_t utf16_string_size,
4729 size_t *utf16_string_index,
4730 libcerror_error_t **error )
4731 {
4732 static char *function = "libuna_unicode_character_copy_from_utf16";
4733 libuna_unicode_character_t safe_unicode_character = 0;
4734 libuna_utf16_character_t utf16_surrogate = 0;
4735 size_t safe_utf16_string_index = 0;
4736
4737 if( unicode_character == NULL )
4738 {
4739 libcerror_error_set(
4740 error,
4741 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4742 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4743 "%s: invalid Unicode character.",
4744 function );
4745
4746 return( -1 );
4747 }
4748 if( utf16_string == NULL )
4749 {
4750 libcerror_error_set(
4751 error,
4752 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4753 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4754 "%s: invalid UTF-16 string.",
4755 function );
4756
4757 return( -1 );
4758 }
4759 if( utf16_string_size > (size_t) SSIZE_MAX )
4760 {
4761 libcerror_error_set(
4762 error,
4763 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4764 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
4765 "%s: invalid UTF-16 string size value exceeds maximum.",
4766 function );
4767
4768 return( -1 );
4769 }
4770 if( utf16_string_index == NULL )
4771 {
4772 libcerror_error_set(
4773 error,
4774 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4775 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4776 "%s: invalid UTF-16 string index.",
4777 function );
4778
4779 return( -1 );
4780 }
4781 safe_utf16_string_index = *utf16_string_index;
4782
4783 if( safe_utf16_string_index >= utf16_string_size )
4784 {
4785 libcerror_error_set(
4786 error,
4787 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4788 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
4789 "%s: UTF-16 string too small.",
4790 function );
4791
4792 return( -1 );
4793 }
4794 safe_unicode_character = utf16_string[ safe_utf16_string_index ];
4795 safe_utf16_string_index += 1;
4796
4797 /* Determine if the UTF-16 character is within the high surrogate range
4798 */
4799 if( ( safe_unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
4800 && ( safe_unicode_character <= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_END ) )
4801 {
4802 if( safe_utf16_string_index >= utf16_string_size )
4803 {
4804 libcerror_error_set(
4805 error,
4806 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4807 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
4808 "%s: missing surrogate UTF-16 character bytes.",
4809 function );
4810
4811 return( -1 );
4812 }
4813 utf16_surrogate = utf16_string[ safe_utf16_string_index ];
4814 safe_utf16_string_index += 1;
4815
4816 /* Determine if the UTF-16 character is within the low surrogate range
4817 */
4818 if( ( utf16_surrogate >= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START )
4819 && ( utf16_surrogate <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
4820 {
4821 safe_unicode_character -= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START;
4822 safe_unicode_character <<= 10;
4823 safe_unicode_character += utf16_surrogate - LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START;
4824 safe_unicode_character += 0x010000;
4825 }
4826 else
4827 {
4828 libcerror_error_set(
4829 error,
4830 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4831 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4832 "%s: unsupported low surrogate UTF-16 character.",
4833 function );
4834
4835 return( -1 );
4836 }
4837 }
4838 /* Determine if the Unicode character is valid
4839 */
4840 if( ( ( safe_unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
4841 && ( safe_unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
4842 || ( safe_unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
4843 {
4844 libcerror_error_set(
4845 error,
4846 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4847 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4848 "%s: unsupported Unicode character.",
4849 function );
4850
4851 return( -1 );
4852 }
4853 *unicode_character = safe_unicode_character;
4854 *utf16_string_index = safe_utf16_string_index;
4855
4856 return( 1 );
4857 }
4858
4859 /* Copies an Unicode character into a UTF-16 string
4860 * Returns 1 if successful or -1 on error
4861 */
4862 int libuna_unicode_character_copy_to_utf16(
4863 libuna_unicode_character_t unicode_character,
4864 libuna_utf16_character_t *utf16_string,
4865 size_t utf16_string_size,
4866 size_t *utf16_string_index,
4867 libcerror_error_t **error )
4868 {
4869 static char *function = "libuna_unicode_character_copy_to_utf16";
4870 size_t safe_utf16_string_index = 0;
4871
4872 if( utf16_string == NULL )
4873 {
4874 libcerror_error_set(
4875 error,
4876 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4877 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4878 "%s: invalid UTF-16 string.",
4879 function );
4880
4881 return( -1 );
4882 }
4883 if( utf16_string_size > (size_t) SSIZE_MAX )
4884 {
4885 libcerror_error_set(
4886 error,
4887 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4888 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
4889 "%s: invalid UTF-16 string size value exceeds maximum.",
4890 function );
4891
4892 return( -1 );
4893 }
4894 if( utf16_string_index == NULL )
4895 {
4896 libcerror_error_set(
4897 error,
4898 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4899 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4900 "%s: invalid UTF-16 string index.",
4901 function );
4902
4903 return( -1 );
4904 }
4905 safe_utf16_string_index = *utf16_string_index;
4906
4907 if( safe_utf16_string_index >= utf16_string_size )
4908 {
4909 libcerror_error_set(
4910 error,
4911 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4912 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
4913 "%s: UTF-16 string too small.",
4914 function );
4915
4916 return( -1 );
4917 }
4918 /* Determine if the Unicode character is valid
4919 */
4920 if( ( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
4921 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
4922 || ( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
4923 {
4924 libcerror_error_set(
4925 error,
4926 LIBCERROR_ERROR_DOMAIN_RUNTIME,
4927 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
4928 "%s: unsupported Unicode character.",
4929 function );
4930
4931 return( -1 );
4932 }
4933 if( unicode_character <= LIBUNA_UNICODE_BASIC_MULTILINGUAL_PLANE_MAX )
4934 {
4935 utf16_string[ safe_utf16_string_index++ ] = (libuna_utf16_character_t) unicode_character;
4936 }
4937 else
4938 {
4939 if( ( utf16_string_size < 2 )
4940 || ( safe_utf16_string_index > ( utf16_string_size - 2 ) ) )
4941 {
4942 libcerror_error_set(
4943 error,
4944 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4945 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
4946 "%s: UTF-16 string too small.",
4947 function );
4948
4949 return( -1 );
4950 }
4951 unicode_character -= 0x010000;
4952 utf16_string[ safe_utf16_string_index++ ] = (libuna_utf16_character_t) ( ( unicode_character >> 10 ) + LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START );
4953 utf16_string[ safe_utf16_string_index++ ] = (libuna_utf16_character_t) ( ( unicode_character & 0x03ff ) + LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START );
4954 }
4955 *utf16_string_index = safe_utf16_string_index;
4956
4957 return( 1 );
4958 }
4959
4960 /* Copies an Unicode character from an UTF-16 stream
4961 * Returns 1 if successful or -1 on error
4962 */
4963 int libuna_unicode_character_copy_from_utf16_stream(
4964 libuna_unicode_character_t *unicode_character,
4965 const uint8_t *utf16_stream,
4966 size_t utf16_stream_size,
4967 size_t *utf16_stream_index,
4968 int byte_order,
4969 libcerror_error_t **error )
4970 {
4971 static char *function = "libuna_unicode_character_copy_from_utf16_stream";
4972 libuna_unicode_character_t safe_unicode_character = 0;
4973 libuna_utf16_character_t utf16_surrogate = 0;
4974 size_t safe_utf16_stream_index = 0;
4975 int byte_order_without_flags = 0;
4976
4977 if( unicode_character == NULL )
4978 {
4979 libcerror_error_set(
4980 error,
4981 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4982 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4983 "%s: invalid Unicode character.",
4984 function );
4985
4986 return( -1 );
4987 }
4988 if( utf16_stream == NULL )
4989 {
4990 libcerror_error_set(
4991 error,
4992 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
4993 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
4994 "%s: invalid UTF-16 stream.",
4995 function );
4996
4997 return( -1 );
4998 }
4999 if( utf16_stream_size > (size_t) SSIZE_MAX )
5000 {
5001 libcerror_error_set(
5002 error,
5003 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5004 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
5005 "%s: invalid UTF-16 stream size value exceeds maximum.",
5006 function );
5007
5008 return( -1 );
5009 }
5010 if( utf16_stream_index == NULL )
5011 {
5012 libcerror_error_set(
5013 error,
5014 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5015 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5016 "%s: invalid UTF-16 stream index.",
5017 function );
5018
5019 return( -1 );
5020 }
5021 byte_order_without_flags = byte_order & 0xff;
5022
5023 if( ( byte_order_without_flags != LIBUNA_ENDIAN_BIG )
5024 && ( byte_order_without_flags != LIBUNA_ENDIAN_LITTLE ) )
5025 {
5026 libcerror_error_set(
5027 error,
5028 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5029 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
5030 "%s: unsupported byte order.",
5031 function );
5032
5033 return( -1 );
5034 }
5035 safe_utf16_stream_index = *utf16_stream_index;
5036
5037 if( ( utf16_stream_size < 2 )
5038 || ( safe_utf16_stream_index > ( utf16_stream_size - 2 ) ) )
5039 {
5040 libcerror_error_set(
5041 error,
5042 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5043 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
5044 "%s: UTF-16 stream too small.",
5045 function );
5046
5047 return( -1 );
5048 }
5049 if( byte_order_without_flags == LIBUNA_ENDIAN_BIG )
5050 {
5051 safe_unicode_character = utf16_stream[ safe_utf16_stream_index ];
5052 safe_unicode_character <<= 8;
5053 safe_unicode_character += utf16_stream[ safe_utf16_stream_index + 1 ];
5054 }
5055 else if( byte_order_without_flags == LIBUNA_ENDIAN_LITTLE )
5056 {
5057 safe_unicode_character = utf16_stream[ safe_utf16_stream_index + 1 ];
5058 safe_unicode_character <<= 8;
5059 safe_unicode_character += utf16_stream[ safe_utf16_stream_index ];
5060 }
5061 safe_utf16_stream_index += 2;
5062
5063 /* Determine if the Unicode character is valid
5064 */
5065 if( ( safe_unicode_character >= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START )
5066 && ( safe_unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
5067 {
5068 if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
5069 {
5070 libcerror_error_set(
5071 error,
5072 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5073 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5074 "%s: unsupported UTF-16 character.",
5075 function );
5076
5077 return( -1 );
5078 }
5079 }
5080 /* Determine if the UTF-16 character is within the high surrogate range
5081 */
5082 if( ( safe_unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
5083 && ( safe_unicode_character <= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_END ) )
5084 {
5085 if( safe_utf16_stream_index > ( utf16_stream_size - 2 ) )
5086 {
5087 if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
5088 {
5089 libcerror_error_set(
5090 error,
5091 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5092 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5093 "%s: missing surrogate UTF-16 character bytes.",
5094 function );
5095
5096 return( -1 );
5097 }
5098 }
5099 else
5100 {
5101 if( byte_order_without_flags == LIBUNA_ENDIAN_BIG )
5102 {
5103 utf16_surrogate = utf16_stream[ safe_utf16_stream_index ];
5104 utf16_surrogate <<= 8;
5105 utf16_surrogate += utf16_stream[ safe_utf16_stream_index + 1 ];
5106 }
5107 else if( byte_order_without_flags == LIBUNA_ENDIAN_LITTLE )
5108 {
5109 utf16_surrogate = utf16_stream[ safe_utf16_stream_index + 1 ];
5110 utf16_surrogate <<= 8;
5111 utf16_surrogate += utf16_stream[ safe_utf16_stream_index ];
5112 }
5113 /* Determine if the UTF-16 character is within the low surrogate range
5114 */
5115 if( ( utf16_surrogate >= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START )
5116 && ( utf16_surrogate <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
5117 {
5118 safe_unicode_character -= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START;
5119 safe_unicode_character <<= 10;
5120 safe_unicode_character += utf16_surrogate - LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START;
5121 safe_unicode_character += 0x010000;
5122
5123 safe_utf16_stream_index += 2;
5124 }
5125 else if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
5126 {
5127 libcerror_error_set(
5128 error,
5129 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5130 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5131 "%s: unsupported low surrogate UTF-16 character.",
5132 function );
5133
5134 return( -1 );
5135 }
5136 }
5137 }
5138 if( safe_unicode_character > LIBUNA_UNICODE_CHARACTER_MAX )
5139 {
5140 libcerror_error_set(
5141 error,
5142 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5143 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5144 "%s: unsupported Unicode character.",
5145 function );
5146
5147 return( -1 );
5148 }
5149 *unicode_character = safe_unicode_character;
5150 *utf16_stream_index = safe_utf16_stream_index;
5151
5152 return( 1 );
5153 }
5154
5155 /* Copies an Unicode character to an UTF-16 stream
5156 * Returns 1 if successful or -1 on error
5157 */
5158 int libuna_unicode_character_copy_to_utf16_stream(
5159 libuna_unicode_character_t unicode_character,
5160 uint8_t *utf16_stream,
5161 size_t utf16_stream_size,
5162 size_t *utf16_stream_index,
5163 int byte_order,
5164 libcerror_error_t **error )
5165 {
5166 static char *function = "libuna_unicode_character_copy_to_utf16_stream";
5167 libuna_utf16_character_t utf16_surrogate = 0;
5168 size_t safe_utf16_stream_index = 0;
5169 int byte_order_without_flags = 0;
5170
5171 if( utf16_stream == NULL )
5172 {
5173 libcerror_error_set(
5174 error,
5175 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5176 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5177 "%s: invalid UTF-16 stream.",
5178 function );
5179
5180 return( -1 );
5181 }
5182 if( utf16_stream_size > (size_t) SSIZE_MAX )
5183 {
5184 libcerror_error_set(
5185 error,
5186 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5187 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
5188 "%s: invalid UTF-16 stream size value exceeds maximum.",
5189 function );
5190
5191 return( -1 );
5192 }
5193 if( utf16_stream_index == NULL )
5194 {
5195 libcerror_error_set(
5196 error,
5197 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5198 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5199 "%s: invalid UTF-16 stream index.",
5200 function );
5201
5202 return( -1 );
5203 }
5204 byte_order_without_flags = byte_order & 0xff;
5205
5206 if( ( byte_order_without_flags != LIBUNA_ENDIAN_BIG )
5207 && ( byte_order_without_flags != LIBUNA_ENDIAN_LITTLE ) )
5208 {
5209 libcerror_error_set(
5210 error,
5211 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5212 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
5213 "%s: unsupported byte order.",
5214 function );
5215
5216 return( -1 );
5217 }
5218 safe_utf16_stream_index = *utf16_stream_index;
5219
5220 /* Determine if the Unicode character is valid
5221 */
5222 if( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX )
5223 {
5224 libcerror_error_set(
5225 error,
5226 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5227 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5228 "%s: unsupported Unicode character.",
5229 function );
5230
5231 return( -1 );
5232 }
5233 if( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
5234 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
5235 {
5236 if( ( byte_order & LIBUNA_UTF16_STREAM_ALLOW_UNPAIRED_SURROGATE ) == 0 )
5237 {
5238 libcerror_error_set(
5239 error,
5240 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5241 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5242 "%s: unsupported Unicode character.",
5243 function );
5244
5245 return( -1 );
5246 }
5247 }
5248 if( unicode_character <= LIBUNA_UNICODE_BASIC_MULTILINGUAL_PLANE_MAX )
5249 {
5250 if( ( utf16_stream_size < 2 )
5251 || ( safe_utf16_stream_index > ( utf16_stream_size - 2 ) ) )
5252 {
5253 libcerror_error_set(
5254 error,
5255 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5256 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
5257 "%s: UTF-16 stream too small.",
5258 function );
5259
5260 return( -1 );
5261 }
5262 if( byte_order == LIBUNA_ENDIAN_BIG )
5263 {
5264 utf16_stream[ safe_utf16_stream_index + 1 ] = (uint8_t) ( unicode_character & 0xff );
5265 unicode_character >>= 8;
5266 utf16_stream[ safe_utf16_stream_index ] = (uint8_t) ( unicode_character & 0xff );
5267 }
5268 else if( byte_order == LIBUNA_ENDIAN_LITTLE )
5269 {
5270 utf16_stream[ safe_utf16_stream_index ] = (uint8_t) ( unicode_character & 0xff );
5271 unicode_character >>= 8;
5272 utf16_stream[ safe_utf16_stream_index + 1 ] = (uint8_t) ( unicode_character & 0xff );
5273 }
5274 safe_utf16_stream_index += 2;
5275 }
5276 else
5277 {
5278 if( ( utf16_stream_size < 4 )
5279 || ( safe_utf16_stream_index > ( utf16_stream_size - 4 ) ) )
5280 {
5281 libcerror_error_set(
5282 error,
5283 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5284 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
5285 "%s: UTF-16 stream too small.",
5286 function );
5287
5288 return( -1 );
5289 }
5290 unicode_character -= 0x010000;
5291
5292 utf16_surrogate = (libuna_utf16_character_t) ( ( unicode_character >> 10 ) + LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START );
5293
5294 if( byte_order == LIBUNA_ENDIAN_BIG )
5295 {
5296 utf16_stream[ safe_utf16_stream_index + 1 ] = (uint8_t) ( utf16_surrogate & 0xff );
5297 utf16_surrogate >>= 8;
5298 utf16_stream[ safe_utf16_stream_index ] = (uint8_t) ( utf16_surrogate & 0xff );
5299 }
5300 else if( byte_order == LIBUNA_ENDIAN_LITTLE )
5301 {
5302 utf16_stream[ safe_utf16_stream_index ] = (uint8_t) ( utf16_surrogate & 0xff );
5303 utf16_surrogate >>= 8;
5304 utf16_stream[ safe_utf16_stream_index + 1 ] = (uint8_t) ( utf16_surrogate & 0xff );
5305 }
5306 safe_utf16_stream_index += 2;
5307
5308 utf16_surrogate = (libuna_utf16_character_t) ( ( unicode_character & 0x03ff ) + LIBUNA_UNICODE_SURROGATE_LOW_RANGE_START );
5309
5310 if( byte_order == LIBUNA_ENDIAN_BIG )
5311 {
5312 utf16_stream[ safe_utf16_stream_index + 1 ] = (uint8_t) ( utf16_surrogate & 0xff );
5313 utf16_surrogate >>= 8;
5314 utf16_stream[ safe_utf16_stream_index ] = (uint8_t) ( utf16_surrogate & 0xff );
5315 }
5316 else if( byte_order == LIBUNA_ENDIAN_LITTLE )
5317 {
5318 utf16_stream[ safe_utf16_stream_index ] = (uint8_t) ( utf16_surrogate & 0xff );
5319 utf16_surrogate >>= 8;
5320 utf16_stream[ safe_utf16_stream_index + 1 ] = (uint8_t) ( utf16_surrogate & 0xff );
5321 }
5322 safe_utf16_stream_index += 2;
5323 }
5324 *utf16_stream_index = safe_utf16_stream_index;
5325
5326 return( 1 );
5327 }
5328
5329 /* Determines the size of an UTF-32 character from an Unicode character
5330 * Adds the size to the UTF-32 character size value
5331 * Returns 1 if successful or -1 on error
5332 */
5333 int libuna_unicode_character_size_to_utf32(
5334 libuna_unicode_character_t unicode_character,
5335 size_t *utf32_character_size,
5336 libcerror_error_t **error )
5337 {
5338 static char *function = "libuna_unicode_character_size_to_utf32";
5339
5340 LIBUNA_UNREFERENCED_PARAMETER( unicode_character )
5341
5342 if( utf32_character_size == NULL )
5343 {
5344 libcerror_error_set(
5345 error,
5346 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5347 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5348 "%s: invalid UTF-32 character size.",
5349 function );
5350
5351 return( -1 );
5352 }
5353 if( ( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
5354 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
5355 || ( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
5356 {
5357 libcerror_error_set(
5358 error,
5359 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5360 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5361 "%s: unsupported Unicode character.",
5362 function );
5363
5364 return( -1 );
5365 }
5366 *utf32_character_size += 1;
5367
5368 return( 1 );
5369 }
5370
5371 /* Copies an Unicode character from an UTF-32 string
5372 * Returns 1 if successful or -1 on error
5373 */
5374 int libuna_unicode_character_copy_from_utf32(
5375 libuna_unicode_character_t *unicode_character,
5376 const libuna_utf32_character_t *utf32_string,
5377 size_t utf32_string_size,
5378 size_t *utf32_string_index,
5379 libcerror_error_t **error )
5380 {
5381 static char *function = "libuna_unicode_character_copy_from_utf32";
5382 libuna_unicode_character_t safe_unicode_character = 0;
5383 size_t safe_utf32_string_index = 0;
5384
5385 if( unicode_character == NULL )
5386 {
5387 libcerror_error_set(
5388 error,
5389 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5390 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5391 "%s: invalid Unicode character.",
5392 function );
5393
5394 return( -1 );
5395 }
5396 if( utf32_string == NULL )
5397 {
5398 libcerror_error_set(
5399 error,
5400 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5401 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5402 "%s: invalid UTF-32 string.",
5403 function );
5404
5405 return( -1 );
5406 }
5407 if( utf32_string_size > (size_t) SSIZE_MAX )
5408 {
5409 libcerror_error_set(
5410 error,
5411 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5412 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
5413 "%s: invalid UTF-32 string size value exceeds maximum.",
5414 function );
5415
5416 return( -1 );
5417 }
5418 if( utf32_string_index == NULL )
5419 {
5420 libcerror_error_set(
5421 error,
5422 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5423 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5424 "%s: invalid UTF-32 string index.",
5425 function );
5426
5427 return( -1 );
5428 }
5429 safe_utf32_string_index = *utf32_string_index;
5430
5431 if( safe_utf32_string_index >= utf32_string_size )
5432 {
5433 libcerror_error_set(
5434 error,
5435 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5436 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
5437 "%s: UTF-32 string too small.",
5438 function );
5439
5440 return( -1 );
5441 }
5442 safe_unicode_character = utf32_string[ safe_utf32_string_index ];
5443
5444 /* Determine if the Unicode character is valid
5445 */
5446 if( ( ( safe_unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
5447 && ( safe_unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
5448 || ( safe_unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
5449 {
5450 libcerror_error_set(
5451 error,
5452 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5453 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5454 "%s: unsupported Unicode character.",
5455 function );
5456
5457 return( -1 );
5458 }
5459 *unicode_character = safe_unicode_character;
5460 *utf32_string_index = safe_utf32_string_index + 1;
5461
5462 return( 1 );
5463 }
5464
5465 /* Copies an Unicode character into a UTF-32 string
5466 * Returns 1 if successful or -1 on error
5467 */
5468 int libuna_unicode_character_copy_to_utf32(
5469 libuna_unicode_character_t unicode_character,
5470 libuna_utf32_character_t *utf32_string,
5471 size_t utf32_string_size,
5472 size_t *utf32_string_index,
5473 libcerror_error_t **error )
5474 {
5475 static char *function = "libuna_unicode_character_copy_to_utf32";
5476 size_t safe_utf32_string_index = 0;
5477
5478 if( utf32_string == NULL )
5479 {
5480 libcerror_error_set(
5481 error,
5482 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5483 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5484 "%s: invalid UTF-32 string.",
5485 function );
5486
5487 return( -1 );
5488 }
5489 if( utf32_string_size > (size_t) SSIZE_MAX )
5490 {
5491 libcerror_error_set(
5492 error,
5493 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5494 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
5495 "%s: invalid UTF-32 string size value exceeds maximum.",
5496 function );
5497
5498 return( -1 );
5499 }
5500 if( utf32_string_index == NULL )
5501 {
5502 libcerror_error_set(
5503 error,
5504 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5505 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5506 "%s: invalid UTF-32 string index.",
5507 function );
5508
5509 return( -1 );
5510 }
5511 safe_utf32_string_index = *utf32_string_index;
5512
5513 if( safe_utf32_string_index >= utf32_string_size )
5514 {
5515 libcerror_error_set(
5516 error,
5517 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5518 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
5519 "%s: UTF-32 string too small.",
5520 function );
5521
5522 return( -1 );
5523 }
5524 /* Determine if the Unicode character is valid
5525 */
5526 if( ( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
5527 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
5528 || ( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
5529 {
5530 libcerror_error_set(
5531 error,
5532 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5533 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5534 "%s: unsupported Unicode character.",
5535 function );
5536
5537 return( -1 );
5538 }
5539 utf32_string[ safe_utf32_string_index ] = (libuna_utf32_character_t) unicode_character;
5540
5541 *utf32_string_index = safe_utf32_string_index + 1;
5542
5543 return( 1 );
5544 }
5545
5546 /* Copies an Unicode character from an UTF-32 stream
5547 * Returns 1 if successful or -1 on error
5548 */
5549 int libuna_unicode_character_copy_from_utf32_stream(
5550 libuna_unicode_character_t *unicode_character,
5551 const uint8_t *utf32_stream,
5552 size_t utf32_stream_size,
5553 size_t *utf32_stream_index,
5554 int byte_order,
5555 libcerror_error_t **error )
5556 {
5557 static char *function = "libuna_unicode_character_copy_from_utf32_stream";
5558 libuna_unicode_character_t safe_unicode_character = 0;
5559 size_t safe_utf32_stream_index = 0;
5560
5561 if( unicode_character == NULL )
5562 {
5563 libcerror_error_set(
5564 error,
5565 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5566 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5567 "%s: invalid Unicode character.",
5568 function );
5569
5570 return( -1 );
5571 }
5572 if( utf32_stream == NULL )
5573 {
5574 libcerror_error_set(
5575 error,
5576 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5577 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5578 "%s: invalid UTF-32 stream.",
5579 function );
5580
5581 return( -1 );
5582 }
5583 if( utf32_stream_size > (size_t) SSIZE_MAX )
5584 {
5585 libcerror_error_set(
5586 error,
5587 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5588 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
5589 "%s: invalid UTF-32 stream size value exceeds maximum.",
5590 function );
5591
5592 return( -1 );
5593 }
5594 if( utf32_stream_index == NULL )
5595 {
5596 libcerror_error_set(
5597 error,
5598 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5599 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5600 "%s: invalid UTF-32 stream index.",
5601 function );
5602
5603 return( -1 );
5604 }
5605 safe_utf32_stream_index = *utf32_stream_index;
5606
5607 if( ( utf32_stream_size < 4 )
5608 || ( safe_utf32_stream_index > ( utf32_stream_size - 4 ) ) )
5609 {
5610 libcerror_error_set(
5611 error,
5612 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5613 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
5614 "%s: UTF-32 stream too small.",
5615 function );
5616
5617 return( -1 );
5618 }
5619 if( ( byte_order != LIBUNA_ENDIAN_BIG )
5620 && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
5621 {
5622 libcerror_error_set(
5623 error,
5624 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5625 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
5626 "%s: unsupported byte order.",
5627 function );
5628
5629 return( -1 );
5630 }
5631 if( byte_order == LIBUNA_ENDIAN_BIG )
5632 {
5633 safe_unicode_character = utf32_stream[ safe_utf32_stream_index ];
5634 safe_unicode_character <<= 8;
5635 safe_unicode_character += utf32_stream[ safe_utf32_stream_index + 1 ];
5636 safe_unicode_character <<= 8;
5637 safe_unicode_character += utf32_stream[ safe_utf32_stream_index + 2 ];
5638 safe_unicode_character <<= 8;
5639 safe_unicode_character += utf32_stream[ safe_utf32_stream_index + 3 ];
5640 }
5641 else if( byte_order == LIBUNA_ENDIAN_LITTLE )
5642 {
5643 safe_unicode_character = utf32_stream[ safe_utf32_stream_index + 3 ];
5644 safe_unicode_character <<= 8;
5645 safe_unicode_character += utf32_stream[ safe_utf32_stream_index + 2 ];
5646 safe_unicode_character <<= 8;
5647 safe_unicode_character += utf32_stream[ safe_utf32_stream_index + 1 ];
5648 safe_unicode_character <<= 8;
5649 safe_unicode_character += utf32_stream[ safe_utf32_stream_index ];
5650 }
5651 /* Determine if the Unicode character is valid
5652 */
5653 if( ( ( safe_unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
5654 && ( safe_unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
5655 || ( safe_unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
5656 {
5657 libcerror_error_set(
5658 error,
5659 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5660 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5661 "%s: unsupported Unicode character.",
5662 function );
5663
5664 return( -1 );
5665 }
5666 *unicode_character = safe_unicode_character;
5667 *utf32_stream_index = safe_utf32_stream_index + 4;
5668
5669 return( 1 );
5670 }
5671
5672 /* Copies an Unicode character to an UTF-32 stream
5673 * Returns 1 if successful or -1 on error
5674 */
5675 int libuna_unicode_character_copy_to_utf32_stream(
5676 libuna_unicode_character_t unicode_character,
5677 uint8_t *utf32_stream,
5678 size_t utf32_stream_size,
5679 size_t *utf32_stream_index,
5680 int byte_order,
5681 libcerror_error_t **error )
5682 {
5683 static char *function = "libuna_unicode_character_copy_to_utf32_stream";
5684 size_t safe_utf32_stream_index = 0;
5685
5686 if( utf32_stream == NULL )
5687 {
5688 libcerror_error_set(
5689 error,
5690 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5691 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5692 "%s: invalid UTF-32 stream.",
5693 function );
5694
5695 return( -1 );
5696 }
5697 if( utf32_stream_size > (size_t) SSIZE_MAX )
5698 {
5699 libcerror_error_set(
5700 error,
5701 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5702 LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
5703 "%s: invalid UTF-32 stream size value exceeds maximum.",
5704 function );
5705
5706 return( -1 );
5707 }
5708 if( utf32_stream_index == NULL )
5709 {
5710 libcerror_error_set(
5711 error,
5712 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5713 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
5714 "%s: invalid UTF-32 stream index.",
5715 function );
5716
5717 return( -1 );
5718 }
5719 safe_utf32_stream_index = *utf32_stream_index;
5720
5721 if( ( utf32_stream_size < 4 )
5722 || ( safe_utf32_stream_index > ( utf32_stream_size - 4 ) ) )
5723 {
5724 libcerror_error_set(
5725 error,
5726 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5727 LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
5728 "%s: UTF-32 stream too small.",
5729 function );
5730
5731 return( -1 );
5732 }
5733 if( ( byte_order != LIBUNA_ENDIAN_BIG )
5734 && ( byte_order != LIBUNA_ENDIAN_LITTLE ) )
5735 {
5736 libcerror_error_set(
5737 error,
5738 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
5739 LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
5740 "%s: unsupported byte order.",
5741 function );
5742
5743 return( -1 );
5744 }
5745 /* Determine if the Unicode character is valid
5746 */
5747 if( ( ( unicode_character >= LIBUNA_UNICODE_SURROGATE_HIGH_RANGE_START )
5748 && ( unicode_character <= LIBUNA_UNICODE_SURROGATE_LOW_RANGE_END ) )
5749 || ( unicode_character > LIBUNA_UNICODE_CHARACTER_MAX ) )
5750 {
5751 libcerror_error_set(
5752 error,
5753 LIBCERROR_ERROR_DOMAIN_RUNTIME,
5754 LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
5755 "%s: unsupported Unicode character.",
5756 function );
5757
5758 return( -1 );
5759 }
5760 if( byte_order == LIBUNA_ENDIAN_BIG )
5761 {
5762 utf32_stream[ safe_utf32_stream_index + 3 ] = (uint8_t) ( unicode_character & 0xff );
5763 unicode_character >>= 8;
5764 utf32_stream[ safe_utf32_stream_index + 2 ] = (uint8_t) ( unicode_character & 0xff );
5765 unicode_character >>= 8;
5766 utf32_stream[ safe_utf32_stream_index + 1 ] = (uint8_t) ( unicode_character & 0xff );
5767 unicode_character >>= 8;
5768 utf32_stream[ safe_utf32_stream_index ] = (uint8_t) ( unicode_character & 0xff );
5769 }
5770 else if( byte_order == LIBUNA_ENDIAN_LITTLE )
5771 {
5772 utf32_stream[ safe_utf32_stream_index ] = (uint8_t) ( unicode_character & 0xff );
5773 unicode_character >>= 8;
5774 utf32_stream[ safe_utf32_stream_index + 1 ] = (uint8_t) ( unicode_character & 0xff );
5775 unicode_character >>= 8;
5776 utf32_stream[ safe_utf32_stream_index + 2 ] = (uint8_t) ( unicode_character & 0xff );
5777 unicode_character >>= 8;
5778 utf32_stream[ safe_utf32_stream_index + 3 ] = (uint8_t) ( unicode_character & 0xff );
5779 }
5780 *utf32_stream_index = safe_utf32_stream_index + 4;
5781
5782 return( 1 );
5783 }
5784