/src/libewf/libuna/libuna_utf16_string.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * UTF-16 string functions |
3 | | * |
4 | | * Copyright (C) 2008-2022, Joachim Metz <joachim.metz@gmail.com> |
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_definitions.h" |
26 | | #include "libuna_libcerror.h" |
27 | | #include "libuna_scsu.h" |
28 | | #include "libuna_types.h" |
29 | | #include "libuna_unicode_character.h" |
30 | | #include "libuna_utf16_string.h" |
31 | | |
32 | | /* Determines the size of an UTF-16 string from a byte stream |
33 | | * Returns 1 if successful or -1 on error |
34 | | */ |
35 | | int libuna_utf16_string_size_from_byte_stream( |
36 | | const uint8_t *byte_stream, |
37 | | size_t byte_stream_size, |
38 | | int codepage, |
39 | | size_t *utf16_string_size, |
40 | | libcerror_error_t **error ) |
41 | 0 | { |
42 | 0 | static char *function = "libuna_utf16_string_size_from_byte_stream"; |
43 | 0 | size_t byte_stream_index = 0; |
44 | 0 | libuna_unicode_character_t unicode_character = 0; |
45 | |
|
46 | 0 | if( byte_stream == NULL ) |
47 | 0 | { |
48 | 0 | libcerror_error_set( |
49 | 0 | error, |
50 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
51 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
52 | 0 | "%s: invalid byte stream.", |
53 | 0 | function ); |
54 | |
|
55 | 0 | return( -1 ); |
56 | 0 | } |
57 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
58 | 0 | { |
59 | 0 | libcerror_error_set( |
60 | 0 | error, |
61 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
62 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
63 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
64 | 0 | function ); |
65 | |
|
66 | 0 | return( -1 ); |
67 | 0 | } |
68 | 0 | if( utf16_string_size == NULL ) |
69 | 0 | { |
70 | 0 | libcerror_error_set( |
71 | 0 | error, |
72 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
73 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
74 | 0 | "%s: invalid UTF-16 string size.", |
75 | 0 | function ); |
76 | |
|
77 | 0 | return( -1 ); |
78 | 0 | } |
79 | 0 | *utf16_string_size = 0; |
80 | |
|
81 | 0 | if( byte_stream_size == 0 ) |
82 | 0 | { |
83 | 0 | return( 1 ); |
84 | 0 | } |
85 | 0 | while( byte_stream_index < byte_stream_size ) |
86 | 0 | { |
87 | | /* Convert the byte stream bytes into an Unicode character |
88 | | */ |
89 | 0 | if( libuna_unicode_character_copy_from_byte_stream( |
90 | 0 | &unicode_character, |
91 | 0 | byte_stream, |
92 | 0 | byte_stream_size, |
93 | 0 | &byte_stream_index, |
94 | 0 | codepage, |
95 | 0 | error ) != 1 ) |
96 | 0 | { |
97 | 0 | libcerror_error_set( |
98 | 0 | error, |
99 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
100 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
101 | 0 | "%s: unable to copy Unicode character from byte stream.", |
102 | 0 | function ); |
103 | |
|
104 | 0 | return( -1 ); |
105 | 0 | } |
106 | | /* Determine how many UTF-16 character byte words are required |
107 | | */ |
108 | 0 | if( libuna_unicode_character_size_to_utf16( |
109 | 0 | unicode_character, |
110 | 0 | utf16_string_size, |
111 | 0 | error ) != 1 ) |
112 | 0 | { |
113 | 0 | libcerror_error_set( |
114 | 0 | error, |
115 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
116 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
117 | 0 | "%s: unable to unable to determine size of Unicode character in UTF-16.", |
118 | 0 | function ); |
119 | |
|
120 | 0 | return( -1 ); |
121 | 0 | } |
122 | 0 | if( unicode_character == 0 ) |
123 | 0 | { |
124 | 0 | break; |
125 | 0 | } |
126 | 0 | } |
127 | | /* Check if the string is terminated with an end-of-string character |
128 | | */ |
129 | 0 | if( unicode_character != 0 ) |
130 | 0 | { |
131 | 0 | *utf16_string_size += 1; |
132 | 0 | } |
133 | 0 | return( 1 ); |
134 | 0 | } |
135 | | |
136 | | /* Copies an UTF-16 string from a byte stream |
137 | | * Returns 1 if successful or -1 on error |
138 | | */ |
139 | | int libuna_utf16_string_copy_from_byte_stream( |
140 | | libuna_utf16_character_t *utf16_string, |
141 | | size_t utf16_string_size, |
142 | | const uint8_t *byte_stream, |
143 | | size_t byte_stream_size, |
144 | | int codepage, |
145 | | libcerror_error_t **error ) |
146 | 0 | { |
147 | 0 | static char *function = "libuna_utf16_string_copy_from_byte_stream"; |
148 | 0 | size_t utf16_string_index = 0; |
149 | |
|
150 | 0 | if( libuna_utf16_string_with_index_copy_from_byte_stream( |
151 | 0 | utf16_string, |
152 | 0 | utf16_string_size, |
153 | 0 | &utf16_string_index, |
154 | 0 | byte_stream, |
155 | 0 | byte_stream_size, |
156 | 0 | codepage, |
157 | 0 | error ) != 1 ) |
158 | 0 | { |
159 | 0 | libcerror_error_set( |
160 | 0 | error, |
161 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
162 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
163 | 0 | "%s: unable to copy byte stream to UTF-16 string.", |
164 | 0 | function ); |
165 | |
|
166 | 0 | return( -1 ); |
167 | 0 | } |
168 | 0 | return( 1 ); |
169 | 0 | } |
170 | | |
171 | | /* Copies an UTF-16 string from a byte stream |
172 | | * Returns 1 if successful or -1 on error |
173 | | */ |
174 | | int libuna_utf16_string_with_index_copy_from_byte_stream( |
175 | | libuna_utf16_character_t *utf16_string, |
176 | | size_t utf16_string_size, |
177 | | size_t *utf16_string_index, |
178 | | const uint8_t *byte_stream, |
179 | | size_t byte_stream_size, |
180 | | int codepage, |
181 | | libcerror_error_t **error ) |
182 | 0 | { |
183 | 0 | static char *function = "libuna_utf16_string_with_index_copy_from_byte_stream"; |
184 | 0 | size_t byte_stream_index = 0; |
185 | 0 | libuna_unicode_character_t unicode_character = 0; |
186 | |
|
187 | 0 | if( utf16_string == NULL ) |
188 | 0 | { |
189 | 0 | libcerror_error_set( |
190 | 0 | error, |
191 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
192 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
193 | 0 | "%s: invalid UTF-16 string.", |
194 | 0 | function ); |
195 | |
|
196 | 0 | return( -1 ); |
197 | 0 | } |
198 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
199 | 0 | { |
200 | 0 | libcerror_error_set( |
201 | 0 | error, |
202 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
203 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
204 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
205 | 0 | function ); |
206 | |
|
207 | 0 | return( -1 ); |
208 | 0 | } |
209 | 0 | if( utf16_string_index == NULL ) |
210 | 0 | { |
211 | 0 | libcerror_error_set( |
212 | 0 | error, |
213 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
214 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
215 | 0 | "%s: invalid UTF-16 string index.", |
216 | 0 | function ); |
217 | |
|
218 | 0 | return( -1 ); |
219 | 0 | } |
220 | 0 | if( byte_stream == NULL ) |
221 | 0 | { |
222 | 0 | libcerror_error_set( |
223 | 0 | error, |
224 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
225 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
226 | 0 | "%s: invalid byte stream.", |
227 | 0 | function ); |
228 | |
|
229 | 0 | return( -1 ); |
230 | 0 | } |
231 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
232 | 0 | { |
233 | 0 | libcerror_error_set( |
234 | 0 | error, |
235 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
236 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
237 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
238 | 0 | function ); |
239 | |
|
240 | 0 | return( -1 ); |
241 | 0 | } |
242 | 0 | if( byte_stream_size == 0 ) |
243 | 0 | { |
244 | 0 | libcerror_error_set( |
245 | 0 | error, |
246 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
247 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
248 | 0 | "%s: missing byte stream value.", |
249 | 0 | function ); |
250 | |
|
251 | 0 | return( -1 ); |
252 | 0 | } |
253 | 0 | while( byte_stream_index < byte_stream_size ) |
254 | 0 | { |
255 | | /* Convert the byte stream bytes into an Unicode character |
256 | | */ |
257 | 0 | if( libuna_unicode_character_copy_from_byte_stream( |
258 | 0 | &unicode_character, |
259 | 0 | byte_stream, |
260 | 0 | byte_stream_size, |
261 | 0 | &byte_stream_index, |
262 | 0 | codepage, |
263 | 0 | error ) != 1 ) |
264 | 0 | { |
265 | 0 | libcerror_error_set( |
266 | 0 | error, |
267 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
268 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
269 | 0 | "%s: unable to copy Unicode character from byte stream.", |
270 | 0 | function ); |
271 | |
|
272 | 0 | return( -1 ); |
273 | 0 | } |
274 | | /* Convert the Unicode character into UTF-16 character byte words |
275 | | */ |
276 | 0 | if( libuna_unicode_character_copy_to_utf16( |
277 | 0 | unicode_character, |
278 | 0 | utf16_string, |
279 | 0 | utf16_string_size, |
280 | 0 | utf16_string_index, |
281 | 0 | error ) != 1 ) |
282 | 0 | { |
283 | 0 | libcerror_error_set( |
284 | 0 | error, |
285 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
286 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
287 | 0 | "%s: unable to copy Unicode character to UTF-16.", |
288 | 0 | function ); |
289 | |
|
290 | 0 | return( -1 ); |
291 | 0 | } |
292 | 0 | if( unicode_character == 0 ) |
293 | 0 | { |
294 | 0 | break; |
295 | 0 | } |
296 | 0 | } |
297 | | /* Check if the string is terminated with an end-of-string character |
298 | | */ |
299 | 0 | if( unicode_character != 0 ) |
300 | 0 | { |
301 | 0 | if( *utf16_string_index >= utf16_string_size ) |
302 | 0 | { |
303 | 0 | libcerror_error_set( |
304 | 0 | error, |
305 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
306 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
307 | 0 | "%s: UTF-16 string too small.", |
308 | 0 | function ); |
309 | |
|
310 | 0 | return( -1 ); |
311 | 0 | } |
312 | 0 | utf16_string[ *utf16_string_index ] = 0; |
313 | |
|
314 | 0 | *utf16_string_index += 1; |
315 | 0 | } |
316 | 0 | return( 1 ); |
317 | 0 | } |
318 | | |
319 | | /* Compares an UTF-16 string with a byte stream |
320 | | * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error |
321 | | */ |
322 | | int libuna_utf16_string_compare_with_byte_stream( |
323 | | const libuna_utf16_character_t *utf16_string, |
324 | | size_t utf16_string_size, |
325 | | const uint8_t *byte_stream, |
326 | | size_t byte_stream_size, |
327 | | int codepage, |
328 | | libcerror_error_t **error ) |
329 | 0 | { |
330 | 0 | static char *function = "libuna_utf16_string_compare_with_byte_stream"; |
331 | 0 | size_t byte_stream_index = 0; |
332 | 0 | size_t utf16_string_index = 0; |
333 | 0 | libuna_unicode_character_t utf16_unicode_character = 0; |
334 | 0 | libuna_unicode_character_t byte_stream_unicode_character = 0; |
335 | |
|
336 | 0 | if( utf16_string == NULL ) |
337 | 0 | { |
338 | 0 | libcerror_error_set( |
339 | 0 | error, |
340 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
341 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
342 | 0 | "%s: invalid UTF-16 string.", |
343 | 0 | function ); |
344 | |
|
345 | 0 | return( -1 ); |
346 | 0 | } |
347 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
348 | 0 | { |
349 | 0 | libcerror_error_set( |
350 | 0 | error, |
351 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
352 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
353 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
354 | 0 | function ); |
355 | |
|
356 | 0 | return( -1 ); |
357 | 0 | } |
358 | 0 | if( byte_stream == NULL ) |
359 | 0 | { |
360 | 0 | libcerror_error_set( |
361 | 0 | error, |
362 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
363 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
364 | 0 | "%s: invalid byte stream.", |
365 | 0 | function ); |
366 | |
|
367 | 0 | return( -1 ); |
368 | 0 | } |
369 | 0 | if( byte_stream_size > (size_t) SSIZE_MAX ) |
370 | 0 | { |
371 | 0 | libcerror_error_set( |
372 | 0 | error, |
373 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
374 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
375 | 0 | "%s: invalid byte stream size value exceeds maximum.", |
376 | 0 | function ); |
377 | |
|
378 | 0 | return( -1 ); |
379 | 0 | } |
380 | 0 | if( byte_stream_size == 0 ) |
381 | 0 | { |
382 | 0 | libcerror_error_set( |
383 | 0 | error, |
384 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
385 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
386 | 0 | "%s: missing byte stream value.", |
387 | 0 | function ); |
388 | |
|
389 | 0 | return( -1 ); |
390 | 0 | } |
391 | 0 | if( ( utf16_string_size >= 1 ) |
392 | 0 | && ( utf16_string[ utf16_string_size - 1 ] == 0 ) ) |
393 | 0 | { |
394 | 0 | utf16_string_size -= 1; |
395 | 0 | } |
396 | | /* Check if the byte stream is terminated with zero bytes |
397 | | */ |
398 | 0 | if( ( byte_stream_size >= 1 ) |
399 | 0 | && ( byte_stream[ byte_stream_size - 1 ] == 0 ) ) |
400 | 0 | { |
401 | 0 | byte_stream_size -= 1; |
402 | 0 | } |
403 | 0 | while( ( utf16_string_index < utf16_string_size ) |
404 | 0 | && ( byte_stream_index < byte_stream_size ) ) |
405 | 0 | { |
406 | | /* Convert the UTF-16 character bytes into an Unicode character |
407 | | */ |
408 | 0 | if( libuna_unicode_character_copy_from_utf16( |
409 | 0 | &utf16_unicode_character, |
410 | 0 | utf16_string, |
411 | 0 | utf16_string_size, |
412 | 0 | &utf16_string_index, |
413 | 0 | error ) != 1 ) |
414 | 0 | { |
415 | 0 | libcerror_error_set( |
416 | 0 | error, |
417 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
418 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
419 | 0 | "%s: unable to copy Unicode character from UTF-16.", |
420 | 0 | function ); |
421 | |
|
422 | 0 | return( -1 ); |
423 | 0 | } |
424 | | /* Convert the byte stream bytes into an Unicode character |
425 | | */ |
426 | 0 | if( libuna_unicode_character_copy_from_byte_stream( |
427 | 0 | &byte_stream_unicode_character, |
428 | 0 | byte_stream, |
429 | 0 | byte_stream_size, |
430 | 0 | &byte_stream_index, |
431 | 0 | codepage, |
432 | 0 | error ) != 1 ) |
433 | 0 | { |
434 | 0 | libcerror_error_set( |
435 | 0 | error, |
436 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
437 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
438 | 0 | "%s: unable to copy Unicode character from byte stream.", |
439 | 0 | function ); |
440 | |
|
441 | 0 | return( -1 ); |
442 | 0 | } |
443 | 0 | if( utf16_unicode_character < byte_stream_unicode_character ) |
444 | 0 | { |
445 | 0 | return( LIBUNA_COMPARE_LESS ); |
446 | 0 | } |
447 | 0 | else if( utf16_unicode_character > byte_stream_unicode_character ) |
448 | 0 | { |
449 | 0 | return( LIBUNA_COMPARE_GREATER ); |
450 | 0 | } |
451 | 0 | } |
452 | | /* Check if both strings were entirely processed |
453 | | */ |
454 | 0 | if( utf16_string_index < utf16_string_size ) |
455 | 0 | { |
456 | 0 | return( LIBUNA_COMPARE_GREATER ); |
457 | 0 | } |
458 | 0 | else if( byte_stream_index < byte_stream_size ) |
459 | 0 | { |
460 | 0 | return( LIBUNA_COMPARE_LESS ); |
461 | 0 | } |
462 | 0 | return( LIBUNA_COMPARE_EQUAL ); |
463 | 0 | } |
464 | | |
465 | | /* Determines the size of an UTF-16 string from an UTF-7 stream |
466 | | * Returns 1 if successful or -1 on error |
467 | | */ |
468 | | int libuna_utf16_string_size_from_utf7_stream( |
469 | | const uint8_t *utf7_stream, |
470 | | size_t utf7_stream_size, |
471 | | size_t *utf16_string_size, |
472 | | libcerror_error_t **error ) |
473 | 0 | { |
474 | 0 | static char *function = "libuna_utf16_string_size_from_utf7_stream"; |
475 | 0 | size_t utf7_stream_index = 0; |
476 | 0 | libuna_unicode_character_t unicode_character = 0; |
477 | 0 | uint32_t utf7_stream_base64_data = 0; |
478 | |
|
479 | 0 | if( utf7_stream == NULL ) |
480 | 0 | { |
481 | 0 | libcerror_error_set( |
482 | 0 | error, |
483 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
484 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
485 | 0 | "%s: invalid UTF-7 stream.", |
486 | 0 | function ); |
487 | |
|
488 | 0 | return( -1 ); |
489 | 0 | } |
490 | 0 | if( utf7_stream_size > (size_t) SSIZE_MAX ) |
491 | 0 | { |
492 | 0 | libcerror_error_set( |
493 | 0 | error, |
494 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
495 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
496 | 0 | "%s: invalid UTF-7 stream size value exceeds maximum.", |
497 | 0 | function ); |
498 | |
|
499 | 0 | return( -1 ); |
500 | 0 | } |
501 | 0 | if( utf16_string_size == NULL ) |
502 | 0 | { |
503 | 0 | libcerror_error_set( |
504 | 0 | error, |
505 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
506 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
507 | 0 | "%s: invalid UTF-16 string size.", |
508 | 0 | function ); |
509 | |
|
510 | 0 | return( -1 ); |
511 | 0 | } |
512 | 0 | *utf16_string_size = 0; |
513 | |
|
514 | 0 | if( utf7_stream_size == 0 ) |
515 | 0 | { |
516 | 0 | return( 1 ); |
517 | 0 | } |
518 | 0 | while( utf7_stream_index < utf7_stream_size ) |
519 | 0 | { |
520 | | /* Convert the UTF-7 stream bytes into an Unicode character |
521 | | */ |
522 | 0 | if( libuna_unicode_character_copy_from_utf7_stream( |
523 | 0 | &unicode_character, |
524 | 0 | utf7_stream, |
525 | 0 | utf7_stream_size, |
526 | 0 | &utf7_stream_index, |
527 | 0 | &utf7_stream_base64_data, |
528 | 0 | error ) != 1 ) |
529 | 0 | { |
530 | 0 | libcerror_error_set( |
531 | 0 | error, |
532 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
533 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
534 | 0 | "%s: unable to copy Unicode character from UTF-7 stream.", |
535 | 0 | function ); |
536 | |
|
537 | 0 | return( -1 ); |
538 | 0 | } |
539 | | /* Determine how many UTF-16 character bytes are required |
540 | | */ |
541 | 0 | if( libuna_unicode_character_size_to_utf16( |
542 | 0 | unicode_character, |
543 | 0 | utf16_string_size, |
544 | 0 | error ) != 1 ) |
545 | 0 | { |
546 | 0 | libcerror_error_set( |
547 | 0 | error, |
548 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
549 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
550 | 0 | "%s: unable to unable to determine size of Unicode character in UTF-16.", |
551 | 0 | function ); |
552 | |
|
553 | 0 | return( -1 ); |
554 | 0 | } |
555 | 0 | if( unicode_character == 0 ) |
556 | 0 | { |
557 | 0 | break; |
558 | 0 | } |
559 | 0 | } |
560 | | /* Check if the string is terminated with an end-of-string character |
561 | | */ |
562 | 0 | if( unicode_character != 0 ) |
563 | 0 | { |
564 | 0 | *utf16_string_size += 1; |
565 | 0 | } |
566 | 0 | return( 1 ); |
567 | 0 | } |
568 | | |
569 | | /* Copies an UTF-16 string from an UTF-7 stream |
570 | | * Returns 1 if successful or -1 on error |
571 | | */ |
572 | | int libuna_utf16_string_copy_from_utf7_stream( |
573 | | libuna_utf16_character_t *utf16_string, |
574 | | size_t utf16_string_size, |
575 | | const uint8_t *utf7_stream, |
576 | | size_t utf7_stream_size, |
577 | | libcerror_error_t **error ) |
578 | 0 | { |
579 | 0 | static char *function = "libuna_utf16_string_copy_from_utf7_stream"; |
580 | 0 | size_t utf16_string_index = 0; |
581 | |
|
582 | 0 | if( libuna_utf16_string_with_index_copy_from_utf7_stream( |
583 | 0 | utf16_string, |
584 | 0 | utf16_string_size, |
585 | 0 | &utf16_string_index, |
586 | 0 | utf7_stream, |
587 | 0 | utf7_stream_size, |
588 | 0 | error ) != 1 ) |
589 | 0 | { |
590 | 0 | libcerror_error_set( |
591 | 0 | error, |
592 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
593 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
594 | 0 | "%s: unable to UTF-7 stream to UTF-16 string.", |
595 | 0 | function ); |
596 | |
|
597 | 0 | return( -1 ); |
598 | 0 | } |
599 | 0 | return( 1 ); |
600 | 0 | } |
601 | | |
602 | | /* Copies an UTF-16 string from an UTF-7 stream |
603 | | * Returns 1 if successful or -1 on error |
604 | | */ |
605 | | int libuna_utf16_string_with_index_copy_from_utf7_stream( |
606 | | libuna_utf16_character_t *utf16_string, |
607 | | size_t utf16_string_size, |
608 | | size_t *utf16_string_index, |
609 | | const uint8_t *utf7_stream, |
610 | | size_t utf7_stream_size, |
611 | | libcerror_error_t **error ) |
612 | 0 | { |
613 | 0 | static char *function = "libuna_utf16_string_with_index_copy_from_utf7_stream"; |
614 | 0 | size_t utf7_stream_index = 0; |
615 | 0 | libuna_unicode_character_t unicode_character = 0; |
616 | 0 | uint32_t utf7_stream_base64_data = 0; |
617 | |
|
618 | 0 | if( utf16_string == NULL ) |
619 | 0 | { |
620 | 0 | libcerror_error_set( |
621 | 0 | error, |
622 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
623 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
624 | 0 | "%s: invalid UTF-16 string.", |
625 | 0 | function ); |
626 | |
|
627 | 0 | return( -1 ); |
628 | 0 | } |
629 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
630 | 0 | { |
631 | 0 | libcerror_error_set( |
632 | 0 | error, |
633 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
634 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
635 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
636 | 0 | function ); |
637 | |
|
638 | 0 | return( -1 ); |
639 | 0 | } |
640 | 0 | if( utf16_string_index == NULL ) |
641 | 0 | { |
642 | 0 | libcerror_error_set( |
643 | 0 | error, |
644 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
645 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
646 | 0 | "%s: invalid UTF-16 string index.", |
647 | 0 | function ); |
648 | |
|
649 | 0 | return( -1 ); |
650 | 0 | } |
651 | 0 | if( utf7_stream == NULL ) |
652 | 0 | { |
653 | 0 | libcerror_error_set( |
654 | 0 | error, |
655 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
656 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
657 | 0 | "%s: invalid UTF-7 stream.", |
658 | 0 | function ); |
659 | |
|
660 | 0 | return( -1 ); |
661 | 0 | } |
662 | 0 | if( utf7_stream_size > (size_t) SSIZE_MAX ) |
663 | 0 | { |
664 | 0 | libcerror_error_set( |
665 | 0 | error, |
666 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
667 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
668 | 0 | "%s: invalid UTF-7 stream size value exceeds maximum.", |
669 | 0 | function ); |
670 | |
|
671 | 0 | return( -1 ); |
672 | 0 | } |
673 | 0 | if( utf7_stream_size == 0 ) |
674 | 0 | { |
675 | 0 | libcerror_error_set( |
676 | 0 | error, |
677 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
678 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
679 | 0 | "%s: missing UTF-7 stream value.", |
680 | 0 | function ); |
681 | |
|
682 | 0 | return( -1 ); |
683 | 0 | } |
684 | 0 | while( utf7_stream_index < utf7_stream_size ) |
685 | 0 | { |
686 | | /* Convert the UTF-7 stream bytes into an Unicode character |
687 | | */ |
688 | 0 | if( libuna_unicode_character_copy_from_utf7_stream( |
689 | 0 | &unicode_character, |
690 | 0 | utf7_stream, |
691 | 0 | utf7_stream_size, |
692 | 0 | &utf7_stream_index, |
693 | 0 | &utf7_stream_base64_data, |
694 | 0 | error ) != 1 ) |
695 | 0 | { |
696 | 0 | libcerror_error_set( |
697 | 0 | error, |
698 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
699 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
700 | 0 | "%s: unable to copy Unicode character from UTF-7 stream.", |
701 | 0 | function ); |
702 | |
|
703 | 0 | return( -1 ); |
704 | 0 | } |
705 | | /* Convert the Unicode character into UTF-16 character bytes |
706 | | */ |
707 | 0 | if( libuna_unicode_character_copy_to_utf16( |
708 | 0 | unicode_character, |
709 | 0 | utf16_string, |
710 | 0 | utf16_string_size, |
711 | 0 | utf16_string_index, |
712 | 0 | error ) != 1 ) |
713 | 0 | { |
714 | 0 | libcerror_error_set( |
715 | 0 | error, |
716 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
717 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
718 | 0 | "%s: unable to copy Unicode character to UTF-16.", |
719 | 0 | function ); |
720 | |
|
721 | 0 | return( -1 ); |
722 | 0 | } |
723 | 0 | if( unicode_character == 0 ) |
724 | 0 | { |
725 | 0 | break; |
726 | 0 | } |
727 | 0 | } |
728 | | /* Check if the string is terminated with an end-of-string character |
729 | | */ |
730 | 0 | if( unicode_character != 0 ) |
731 | 0 | { |
732 | 0 | if( *utf16_string_index >= utf16_string_size ) |
733 | 0 | { |
734 | 0 | libcerror_error_set( |
735 | 0 | error, |
736 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
737 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
738 | 0 | "%s: UTF-16 string too small.", |
739 | 0 | function ); |
740 | |
|
741 | 0 | return( -1 ); |
742 | 0 | } |
743 | 0 | utf16_string[ *utf16_string_index ] = 0; |
744 | |
|
745 | 0 | *utf16_string_index += 1; |
746 | 0 | } |
747 | 0 | return( 1 ); |
748 | 0 | } |
749 | | |
750 | | /* Compares an UTF-16 string with an UTF-7 stream |
751 | | * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error |
752 | | */ |
753 | | int libuna_utf16_string_compare_with_utf7_stream( |
754 | | const libuna_utf16_character_t *utf16_string, |
755 | | size_t utf16_string_size, |
756 | | const uint8_t *utf7_stream, |
757 | | size_t utf7_stream_size, |
758 | | libcerror_error_t **error ) |
759 | 0 | { |
760 | 0 | static char *function = "libuna_utf16_string_compare_with_utf7_stream"; |
761 | 0 | size_t utf16_string_index = 0; |
762 | 0 | size_t utf7_stream_index = 0; |
763 | 0 | libuna_unicode_character_t utf16_unicode_character = 0; |
764 | 0 | libuna_unicode_character_t utf7_stream_unicode_character = 0; |
765 | 0 | uint32_t utf7_stream_base64_data = 0; |
766 | |
|
767 | 0 | if( utf16_string == NULL ) |
768 | 0 | { |
769 | 0 | libcerror_error_set( |
770 | 0 | error, |
771 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
772 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
773 | 0 | "%s: invalid UTF-16 string.", |
774 | 0 | function ); |
775 | |
|
776 | 0 | return( -1 ); |
777 | 0 | } |
778 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
779 | 0 | { |
780 | 0 | libcerror_error_set( |
781 | 0 | error, |
782 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
783 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
784 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
785 | 0 | function ); |
786 | |
|
787 | 0 | return( -1 ); |
788 | 0 | } |
789 | 0 | if( utf7_stream == NULL ) |
790 | 0 | { |
791 | 0 | libcerror_error_set( |
792 | 0 | error, |
793 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
794 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
795 | 0 | "%s: invalid UTF-7 stream.", |
796 | 0 | function ); |
797 | |
|
798 | 0 | return( -1 ); |
799 | 0 | } |
800 | 0 | if( utf7_stream_size > (size_t) SSIZE_MAX ) |
801 | 0 | { |
802 | 0 | libcerror_error_set( |
803 | 0 | error, |
804 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
805 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
806 | 0 | "%s: invalid UTF-7 stream size value exceeds maximum.", |
807 | 0 | function ); |
808 | |
|
809 | 0 | return( -1 ); |
810 | 0 | } |
811 | 0 | if( utf7_stream_size == 0 ) |
812 | 0 | { |
813 | 0 | libcerror_error_set( |
814 | 0 | error, |
815 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
816 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
817 | 0 | "%s: missing UTF-7 stream value.", |
818 | 0 | function ); |
819 | |
|
820 | 0 | return( -1 ); |
821 | 0 | } |
822 | 0 | if( ( utf16_string_size >= 1 ) |
823 | 0 | && ( utf16_string[ utf16_string_size - 1 ] == 0 ) ) |
824 | 0 | { |
825 | 0 | utf16_string_size -= 1; |
826 | 0 | } |
827 | | /* Check if the UTF-7 stream is terminated with zero bytes |
828 | | */ |
829 | 0 | if( ( utf7_stream_size >= 1 ) |
830 | 0 | && ( utf7_stream[ utf7_stream_size - 1 ] == 0 ) ) |
831 | 0 | { |
832 | 0 | utf7_stream_size -= 1; |
833 | 0 | } |
834 | 0 | while( ( utf16_string_index < utf16_string_size ) |
835 | 0 | && ( utf7_stream_index < utf7_stream_size ) ) |
836 | 0 | { |
837 | | /* Convert the UTF-16 character bytes into an Unicode character |
838 | | */ |
839 | 0 | if( libuna_unicode_character_copy_from_utf16( |
840 | 0 | &utf16_unicode_character, |
841 | 0 | utf16_string, |
842 | 0 | utf16_string_size, |
843 | 0 | &utf16_string_index, |
844 | 0 | error ) != 1 ) |
845 | 0 | { |
846 | 0 | libcerror_error_set( |
847 | 0 | error, |
848 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
849 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
850 | 0 | "%s: unable to copy Unicode character from UTF-16.", |
851 | 0 | function ); |
852 | |
|
853 | 0 | return( -1 ); |
854 | 0 | } |
855 | | /* Convert the UTF-7 character bytes into an Unicode character |
856 | | */ |
857 | 0 | if( libuna_unicode_character_copy_from_utf7_stream( |
858 | 0 | &utf7_stream_unicode_character, |
859 | 0 | utf7_stream, |
860 | 0 | utf7_stream_size, |
861 | 0 | &utf7_stream_index, |
862 | 0 | &utf7_stream_base64_data, |
863 | 0 | error ) != 1 ) |
864 | 0 | { |
865 | 0 | libcerror_error_set( |
866 | 0 | error, |
867 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
868 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
869 | 0 | "%s: unable to copy Unicode character from UTF-7 stream.", |
870 | 0 | function ); |
871 | |
|
872 | 0 | return( -1 ); |
873 | 0 | } |
874 | 0 | if( utf16_unicode_character < utf7_stream_unicode_character ) |
875 | 0 | { |
876 | 0 | return( LIBUNA_COMPARE_LESS ); |
877 | 0 | } |
878 | 0 | else if( utf16_unicode_character > utf7_stream_unicode_character ) |
879 | 0 | { |
880 | 0 | return( LIBUNA_COMPARE_GREATER ); |
881 | 0 | } |
882 | 0 | } |
883 | | /* Check if both strings were entirely processed |
884 | | */ |
885 | 0 | if( utf16_string_index < utf16_string_size ) |
886 | 0 | { |
887 | 0 | return( LIBUNA_COMPARE_GREATER ); |
888 | 0 | } |
889 | 0 | else if( utf7_stream_index < utf7_stream_size ) |
890 | 0 | { |
891 | 0 | return( LIBUNA_COMPARE_LESS ); |
892 | 0 | } |
893 | 0 | return( LIBUNA_COMPARE_EQUAL ); |
894 | 0 | } |
895 | | |
896 | | /* Determines the size of an UTF-16 string from an UTF-8 string |
897 | | * Returns 1 if successful or -1 on error |
898 | | */ |
899 | | int libuna_utf16_string_size_from_utf8( |
900 | | const libuna_utf8_character_t *utf8_string, |
901 | | size_t utf8_string_size, |
902 | | size_t *utf16_string_size, |
903 | | libcerror_error_t **error ) |
904 | 0 | { |
905 | 0 | static char *function = "libuna_utf16_string_size_from_utf8"; |
906 | 0 | size_t utf8_string_index = 0; |
907 | 0 | libuna_unicode_character_t unicode_character = 0; |
908 | |
|
909 | 0 | if( utf8_string == NULL ) |
910 | 0 | { |
911 | 0 | libcerror_error_set( |
912 | 0 | error, |
913 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
914 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
915 | 0 | "%s: invalid UTF-8 string.", |
916 | 0 | function ); |
917 | |
|
918 | 0 | return( -1 ); |
919 | 0 | } |
920 | 0 | if( utf8_string_size > (size_t) SSIZE_MAX ) |
921 | 0 | { |
922 | 0 | libcerror_error_set( |
923 | 0 | error, |
924 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
925 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
926 | 0 | "%s: invalid UTF-8 string size value exceeds maximum.", |
927 | 0 | function ); |
928 | |
|
929 | 0 | return( -1 ); |
930 | 0 | } |
931 | 0 | if( utf16_string_size == NULL ) |
932 | 0 | { |
933 | 0 | libcerror_error_set( |
934 | 0 | error, |
935 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
936 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
937 | 0 | "%s: invalid UTF-16 string size.", |
938 | 0 | function ); |
939 | |
|
940 | 0 | return( -1 ); |
941 | 0 | } |
942 | 0 | *utf16_string_size = 0; |
943 | |
|
944 | 0 | if( utf8_string_size == 0 ) |
945 | 0 | { |
946 | 0 | return( 1 ); |
947 | 0 | } |
948 | 0 | while( utf8_string_index < utf8_string_size ) |
949 | 0 | { |
950 | | /* Convert the UTF-8 character bytes into an Unicode character |
951 | | */ |
952 | 0 | if( libuna_unicode_character_copy_from_utf8( |
953 | 0 | &unicode_character, |
954 | 0 | utf8_string, |
955 | 0 | utf8_string_size, |
956 | 0 | &utf8_string_index, |
957 | 0 | error ) != 1 ) |
958 | 0 | { |
959 | 0 | libcerror_error_set( |
960 | 0 | error, |
961 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
962 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
963 | 0 | "%s: unable to copy Unicode character from UTF-8.", |
964 | 0 | function ); |
965 | |
|
966 | 0 | return( -1 ); |
967 | 0 | } |
968 | | /* Determine how many UTF-16 character bytes are required |
969 | | */ |
970 | 0 | if( libuna_unicode_character_size_to_utf16( |
971 | 0 | unicode_character, |
972 | 0 | utf16_string_size, |
973 | 0 | error ) != 1 ) |
974 | 0 | { |
975 | 0 | libcerror_error_set( |
976 | 0 | error, |
977 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
978 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
979 | 0 | "%s: unable to unable to determine size of Unicode character in UTF-16.", |
980 | 0 | function ); |
981 | |
|
982 | 0 | return( -1 ); |
983 | 0 | } |
984 | 0 | if( unicode_character == 0 ) |
985 | 0 | { |
986 | 0 | break; |
987 | 0 | } |
988 | 0 | } |
989 | | /* Check if the string is terminated with an end-of-string character |
990 | | */ |
991 | 0 | if( unicode_character != 0 ) |
992 | 0 | { |
993 | 0 | *utf16_string_size += 1; |
994 | 0 | } |
995 | 0 | return( 1 ); |
996 | 0 | } |
997 | | |
998 | | /* Copies an UTF-16 string from an UTF-8 string |
999 | | * Returns 1 if successful or -1 on error |
1000 | | */ |
1001 | | int libuna_utf16_string_copy_from_utf8( |
1002 | | libuna_utf16_character_t *utf16_string, |
1003 | | size_t utf16_string_size, |
1004 | | const libuna_utf8_character_t *utf8_string, |
1005 | | size_t utf8_string_size, |
1006 | | libcerror_error_t **error ) |
1007 | 0 | { |
1008 | 0 | static char *function = "libuna_utf16_string_copy_from_utf8"; |
1009 | 0 | size_t utf16_string_index = 0; |
1010 | |
|
1011 | 0 | if( libuna_utf16_string_with_index_copy_from_utf8( |
1012 | 0 | utf16_string, |
1013 | 0 | utf16_string_size, |
1014 | 0 | &utf16_string_index, |
1015 | 0 | utf8_string, |
1016 | 0 | utf8_string_size, |
1017 | 0 | error ) != 1 ) |
1018 | 0 | { |
1019 | 0 | libcerror_error_set( |
1020 | 0 | error, |
1021 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1022 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
1023 | 0 | "%s: unable to copy UTF-8 string to UTF-16 string.", |
1024 | 0 | function ); |
1025 | |
|
1026 | 0 | return( -1 ); |
1027 | 0 | } |
1028 | 0 | return( 1 ); |
1029 | 0 | } |
1030 | | |
1031 | | /* Copies an UTF-16 string from an UTF-8 string |
1032 | | * Returns 1 if successful or -1 on error |
1033 | | */ |
1034 | | int libuna_utf16_string_with_index_copy_from_utf8( |
1035 | | libuna_utf16_character_t *utf16_string, |
1036 | | size_t utf16_string_size, |
1037 | | size_t *utf16_string_index, |
1038 | | const libuna_utf8_character_t *utf8_string, |
1039 | | size_t utf8_string_size, |
1040 | | libcerror_error_t **error ) |
1041 | 0 | { |
1042 | 0 | static char *function = "libuna_utf16_string_with_index_copy_from_utf8"; |
1043 | 0 | size_t utf8_string_index = 0; |
1044 | 0 | libuna_unicode_character_t unicode_character = 0; |
1045 | |
|
1046 | 0 | if( utf16_string == NULL ) |
1047 | 0 | { |
1048 | 0 | libcerror_error_set( |
1049 | 0 | error, |
1050 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1051 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1052 | 0 | "%s: invalid UTF-16 string.", |
1053 | 0 | function ); |
1054 | |
|
1055 | 0 | return( -1 ); |
1056 | 0 | } |
1057 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
1058 | 0 | { |
1059 | 0 | libcerror_error_set( |
1060 | 0 | error, |
1061 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1062 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1063 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
1064 | 0 | function ); |
1065 | |
|
1066 | 0 | return( -1 ); |
1067 | 0 | } |
1068 | 0 | if( utf16_string_index == NULL ) |
1069 | 0 | { |
1070 | 0 | libcerror_error_set( |
1071 | 0 | error, |
1072 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1073 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1074 | 0 | "%s: invalid UTF-16 string index.", |
1075 | 0 | function ); |
1076 | |
|
1077 | 0 | return( -1 ); |
1078 | 0 | } |
1079 | 0 | if( utf8_string == NULL ) |
1080 | 0 | { |
1081 | 0 | libcerror_error_set( |
1082 | 0 | error, |
1083 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1084 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1085 | 0 | "%s: invalid UTF-8 string.", |
1086 | 0 | function ); |
1087 | |
|
1088 | 0 | return( -1 ); |
1089 | 0 | } |
1090 | 0 | if( utf8_string_size > (size_t) SSIZE_MAX ) |
1091 | 0 | { |
1092 | 0 | libcerror_error_set( |
1093 | 0 | error, |
1094 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1095 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1096 | 0 | "%s: invalid UTF-8 string size value exceeds maximum.", |
1097 | 0 | function ); |
1098 | |
|
1099 | 0 | return( -1 ); |
1100 | 0 | } |
1101 | 0 | if( utf8_string_size == 0 ) |
1102 | 0 | { |
1103 | 0 | libcerror_error_set( |
1104 | 0 | error, |
1105 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1106 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
1107 | 0 | "%s: missing UTF-8 string value.", |
1108 | 0 | function ); |
1109 | |
|
1110 | 0 | return( -1 ); |
1111 | 0 | } |
1112 | 0 | while( utf8_string_index < utf8_string_size ) |
1113 | 0 | { |
1114 | | /* Convert the UTF-8 character bytes into an Unicode character |
1115 | | */ |
1116 | 0 | if( libuna_unicode_character_copy_from_utf8( |
1117 | 0 | &unicode_character, |
1118 | 0 | utf8_string, |
1119 | 0 | utf8_string_size, |
1120 | 0 | &utf8_string_index, |
1121 | 0 | error ) != 1 ) |
1122 | 0 | { |
1123 | 0 | libcerror_error_set( |
1124 | 0 | error, |
1125 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1126 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
1127 | 0 | "%s: unable to copy Unicode character from UTF-8.", |
1128 | 0 | function ); |
1129 | |
|
1130 | 0 | return( -1 ); |
1131 | 0 | } |
1132 | | /* Convert the Unicode character into UTF-16 character bytes |
1133 | | */ |
1134 | 0 | if( libuna_unicode_character_copy_to_utf16( |
1135 | 0 | unicode_character, |
1136 | 0 | utf16_string, |
1137 | 0 | utf16_string_size, |
1138 | 0 | utf16_string_index, |
1139 | 0 | error ) != 1 ) |
1140 | 0 | { |
1141 | 0 | libcerror_error_set( |
1142 | 0 | error, |
1143 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1144 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
1145 | 0 | "%s: unable to copy Unicode character to UTF-16.", |
1146 | 0 | function ); |
1147 | |
|
1148 | 0 | return( -1 ); |
1149 | 0 | } |
1150 | 0 | if( unicode_character == 0 ) |
1151 | 0 | { |
1152 | 0 | break; |
1153 | 0 | } |
1154 | 0 | } |
1155 | | /* Check if the string is terminated with an end-of-string character |
1156 | | */ |
1157 | 0 | if( unicode_character != 0 ) |
1158 | 0 | { |
1159 | 0 | if( *utf16_string_index >= utf16_string_size ) |
1160 | 0 | { |
1161 | 0 | libcerror_error_set( |
1162 | 0 | error, |
1163 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1164 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1165 | 0 | "%s: UTF-16 string too small.", |
1166 | 0 | function ); |
1167 | |
|
1168 | 0 | return( -1 ); |
1169 | 0 | } |
1170 | 0 | utf16_string[ *utf16_string_index ] = 0; |
1171 | |
|
1172 | 0 | *utf16_string_index += 1; |
1173 | 0 | } |
1174 | 0 | return( 1 ); |
1175 | 0 | } |
1176 | | |
1177 | | /* Determines the size of an UTF-16 string from an UTF-8 stream |
1178 | | * Returns 1 if successful or -1 on error |
1179 | | */ |
1180 | | int libuna_utf16_string_size_from_utf8_stream( |
1181 | | const uint8_t *utf8_stream, |
1182 | | size_t utf8_stream_size, |
1183 | | size_t *utf16_string_size, |
1184 | | libcerror_error_t **error ) |
1185 | 0 | { |
1186 | 0 | static char *function = "libuna_utf16_string_size_from_utf8_stream"; |
1187 | 0 | size_t utf8_stream_index = 0; |
1188 | 0 | libuna_unicode_character_t unicode_character = 0; |
1189 | |
|
1190 | 0 | if( utf8_stream == NULL ) |
1191 | 0 | { |
1192 | 0 | libcerror_error_set( |
1193 | 0 | error, |
1194 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1195 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1196 | 0 | "%s: invalid UTF-8 stream.", |
1197 | 0 | function ); |
1198 | |
|
1199 | 0 | return( -1 ); |
1200 | 0 | } |
1201 | 0 | if( utf8_stream_size > (size_t) SSIZE_MAX ) |
1202 | 0 | { |
1203 | 0 | libcerror_error_set( |
1204 | 0 | error, |
1205 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1206 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1207 | 0 | "%s: invalid UTF-8 stream size value exceeds maximum.", |
1208 | 0 | function ); |
1209 | |
|
1210 | 0 | return( -1 ); |
1211 | 0 | } |
1212 | 0 | if( utf16_string_size == NULL ) |
1213 | 0 | { |
1214 | 0 | libcerror_error_set( |
1215 | 0 | error, |
1216 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1217 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1218 | 0 | "%s: invalid UTF-16 string size.", |
1219 | 0 | function ); |
1220 | |
|
1221 | 0 | return( -1 ); |
1222 | 0 | } |
1223 | 0 | *utf16_string_size = 0; |
1224 | |
|
1225 | 0 | if( utf8_stream_size == 0 ) |
1226 | 0 | { |
1227 | 0 | return( 1 ); |
1228 | 0 | } |
1229 | | /* Check if UTF-8 stream starts with a byte order mark (BOM) |
1230 | | */ |
1231 | 0 | if( utf8_stream_size >= 3 ) |
1232 | 0 | { |
1233 | 0 | if( ( utf8_stream[ 0 ] == 0x0ef ) |
1234 | 0 | && ( utf8_stream[ 1 ] == 0x0bb ) |
1235 | 0 | && ( utf8_stream[ 2 ] == 0x0bf ) ) |
1236 | 0 | { |
1237 | 0 | utf8_stream_index += 3; |
1238 | 0 | } |
1239 | 0 | } |
1240 | 0 | while( utf8_stream_index < utf8_stream_size ) |
1241 | 0 | { |
1242 | | /* Convert the UTF-8 stream bytes into an Unicode character |
1243 | | */ |
1244 | 0 | if( libuna_unicode_character_copy_from_utf8( |
1245 | 0 | &unicode_character, |
1246 | 0 | utf8_stream, |
1247 | 0 | utf8_stream_size, |
1248 | 0 | &utf8_stream_index, |
1249 | 0 | error ) != 1 ) |
1250 | 0 | { |
1251 | 0 | libcerror_error_set( |
1252 | 0 | error, |
1253 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1254 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
1255 | 0 | "%s: unable to copy Unicode character from UTF-8 stream.", |
1256 | 0 | function ); |
1257 | |
|
1258 | 0 | return( -1 ); |
1259 | 0 | } |
1260 | | /* Determine how many UTF-16 character bytes are required |
1261 | | */ |
1262 | 0 | if( libuna_unicode_character_size_to_utf16( |
1263 | 0 | unicode_character, |
1264 | 0 | utf16_string_size, |
1265 | 0 | error ) != 1 ) |
1266 | 0 | { |
1267 | 0 | libcerror_error_set( |
1268 | 0 | error, |
1269 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1270 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
1271 | 0 | "%s: unable to unable to determine size of Unicode character in UTF-16.", |
1272 | 0 | function ); |
1273 | |
|
1274 | 0 | return( -1 ); |
1275 | 0 | } |
1276 | 0 | if( unicode_character == 0 ) |
1277 | 0 | { |
1278 | 0 | break; |
1279 | 0 | } |
1280 | 0 | } |
1281 | | /* Check if the string is terminated with an end-of-string character |
1282 | | */ |
1283 | 0 | if( unicode_character != 0 ) |
1284 | 0 | { |
1285 | 0 | *utf16_string_size += 1; |
1286 | 0 | } |
1287 | 0 | return( 1 ); |
1288 | 0 | } |
1289 | | |
1290 | | /* Copies an UTF-16 string from an UTF-8 stream |
1291 | | * Returns 1 if successful or -1 on error |
1292 | | */ |
1293 | | int libuna_utf16_string_copy_from_utf8_stream( |
1294 | | libuna_utf16_character_t *utf16_string, |
1295 | | size_t utf16_string_size, |
1296 | | const uint8_t *utf8_stream, |
1297 | | size_t utf8_stream_size, |
1298 | | libcerror_error_t **error ) |
1299 | 0 | { |
1300 | 0 | static char *function = "libuna_utf16_string_copy_from_utf8_stream"; |
1301 | 0 | size_t utf16_string_index = 0; |
1302 | |
|
1303 | 0 | if( libuna_utf16_string_with_index_copy_from_utf8_stream( |
1304 | 0 | utf16_string, |
1305 | 0 | utf16_string_size, |
1306 | 0 | &utf16_string_index, |
1307 | 0 | utf8_stream, |
1308 | 0 | utf8_stream_size, |
1309 | 0 | error ) != 1 ) |
1310 | 0 | { |
1311 | 0 | libcerror_error_set( |
1312 | 0 | error, |
1313 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1314 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
1315 | 0 | "%s: unable to UTF-8 stream to UTF-16 string.", |
1316 | 0 | function ); |
1317 | |
|
1318 | 0 | return( -1 ); |
1319 | 0 | } |
1320 | 0 | return( 1 ); |
1321 | 0 | } |
1322 | | |
1323 | | /* Copies an UTF-16 string from an UTF-8 stream |
1324 | | * Returns 1 if successful or -1 on error |
1325 | | */ |
1326 | | int libuna_utf16_string_with_index_copy_from_utf8_stream( |
1327 | | libuna_utf16_character_t *utf16_string, |
1328 | | size_t utf16_string_size, |
1329 | | size_t *utf16_string_index, |
1330 | | const uint8_t *utf8_stream, |
1331 | | size_t utf8_stream_size, |
1332 | | libcerror_error_t **error ) |
1333 | 0 | { |
1334 | 0 | static char *function = "libuna_utf16_string_with_index_copy_from_utf8_stream"; |
1335 | 0 | size_t utf8_stream_index = 0; |
1336 | 0 | libuna_unicode_character_t unicode_character = 0; |
1337 | |
|
1338 | 0 | if( utf16_string == NULL ) |
1339 | 0 | { |
1340 | 0 | libcerror_error_set( |
1341 | 0 | error, |
1342 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1343 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1344 | 0 | "%s: invalid UTF-16 string.", |
1345 | 0 | function ); |
1346 | |
|
1347 | 0 | return( -1 ); |
1348 | 0 | } |
1349 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
1350 | 0 | { |
1351 | 0 | libcerror_error_set( |
1352 | 0 | error, |
1353 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1354 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1355 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
1356 | 0 | function ); |
1357 | |
|
1358 | 0 | return( -1 ); |
1359 | 0 | } |
1360 | 0 | if( utf16_string_index == NULL ) |
1361 | 0 | { |
1362 | 0 | libcerror_error_set( |
1363 | 0 | error, |
1364 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1365 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1366 | 0 | "%s: invalid UTF-16 string index.", |
1367 | 0 | function ); |
1368 | |
|
1369 | 0 | return( -1 ); |
1370 | 0 | } |
1371 | 0 | if( utf8_stream == NULL ) |
1372 | 0 | { |
1373 | 0 | libcerror_error_set( |
1374 | 0 | error, |
1375 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1376 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1377 | 0 | "%s: invalid UTF-8 stream.", |
1378 | 0 | function ); |
1379 | |
|
1380 | 0 | return( -1 ); |
1381 | 0 | } |
1382 | 0 | if( utf8_stream_size > (size_t) SSIZE_MAX ) |
1383 | 0 | { |
1384 | 0 | libcerror_error_set( |
1385 | 0 | error, |
1386 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1387 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1388 | 0 | "%s: invalid UTF-8 stream size value exceeds maximum.", |
1389 | 0 | function ); |
1390 | |
|
1391 | 0 | return( -1 ); |
1392 | 0 | } |
1393 | 0 | if( utf8_stream_size == 0 ) |
1394 | 0 | { |
1395 | 0 | libcerror_error_set( |
1396 | 0 | error, |
1397 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1398 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
1399 | 0 | "%s: missing UTF-8 stream value.", |
1400 | 0 | function ); |
1401 | |
|
1402 | 0 | return( -1 ); |
1403 | 0 | } |
1404 | | /* Check if UTF-8 stream starts with a byte order mark (BOM) |
1405 | | */ |
1406 | 0 | if( utf8_stream_size >= 3 ) |
1407 | 0 | { |
1408 | 0 | if( ( utf8_stream[ 0 ] == 0x0ef ) |
1409 | 0 | && ( utf8_stream[ 1 ] == 0x0bb ) |
1410 | 0 | && ( utf8_stream[ 2 ] == 0x0bf ) ) |
1411 | 0 | { |
1412 | 0 | utf8_stream_index += 3; |
1413 | 0 | } |
1414 | 0 | } |
1415 | 0 | while( utf8_stream_index < utf8_stream_size ) |
1416 | 0 | { |
1417 | | /* Convert the UTF-8 stream bytes into an Unicode character |
1418 | | */ |
1419 | 0 | if( libuna_unicode_character_copy_from_utf8( |
1420 | 0 | &unicode_character, |
1421 | 0 | utf8_stream, |
1422 | 0 | utf8_stream_size, |
1423 | 0 | &utf8_stream_index, |
1424 | 0 | error ) != 1 ) |
1425 | 0 | { |
1426 | 0 | libcerror_error_set( |
1427 | 0 | error, |
1428 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1429 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
1430 | 0 | "%s: unable to copy Unicode character from UTF-8 stream.", |
1431 | 0 | function ); |
1432 | |
|
1433 | 0 | return( -1 ); |
1434 | 0 | } |
1435 | | /* Convert the Unicode character into UTF-16 character bytes |
1436 | | */ |
1437 | 0 | if( libuna_unicode_character_copy_to_utf16( |
1438 | 0 | unicode_character, |
1439 | 0 | utf16_string, |
1440 | 0 | utf16_string_size, |
1441 | 0 | utf16_string_index, |
1442 | 0 | error ) != 1 ) |
1443 | 0 | { |
1444 | 0 | libcerror_error_set( |
1445 | 0 | error, |
1446 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1447 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
1448 | 0 | "%s: unable to copy Unicode character to UTF-16.", |
1449 | 0 | function ); |
1450 | |
|
1451 | 0 | return( -1 ); |
1452 | 0 | } |
1453 | 0 | if( unicode_character == 0 ) |
1454 | 0 | { |
1455 | 0 | break; |
1456 | 0 | } |
1457 | 0 | } |
1458 | | /* Check if the string is terminated with an end-of-string character |
1459 | | */ |
1460 | 0 | if( unicode_character != 0 ) |
1461 | 0 | { |
1462 | 0 | if( *utf16_string_index >= utf16_string_size ) |
1463 | 0 | { |
1464 | 0 | libcerror_error_set( |
1465 | 0 | error, |
1466 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1467 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1468 | 0 | "%s: UTF-16 string too small.", |
1469 | 0 | function ); |
1470 | |
|
1471 | 0 | return( -1 ); |
1472 | 0 | } |
1473 | 0 | utf16_string[ *utf16_string_index ] = 0; |
1474 | |
|
1475 | 0 | *utf16_string_index += 1; |
1476 | 0 | } |
1477 | 0 | return( 1 ); |
1478 | 0 | } |
1479 | | |
1480 | | /* Compares an UTF-16 string with an UTF-8 stream |
1481 | | * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error |
1482 | | */ |
1483 | | int libuna_utf16_string_compare_with_utf8_stream( |
1484 | | const libuna_utf16_character_t *utf16_string, |
1485 | | size_t utf16_string_size, |
1486 | | const uint8_t *utf8_stream, |
1487 | | size_t utf8_stream_size, |
1488 | | libcerror_error_t **error ) |
1489 | 0 | { |
1490 | 0 | static char *function = "libuna_utf16_string_compare_with_utf8_stream"; |
1491 | 0 | size_t utf16_string_index = 0; |
1492 | 0 | size_t utf8_stream_index = 0; |
1493 | 0 | libuna_unicode_character_t utf16_unicode_character = 0; |
1494 | 0 | libuna_unicode_character_t utf8_stream_unicode_character = 0; |
1495 | |
|
1496 | 0 | if( utf16_string == NULL ) |
1497 | 0 | { |
1498 | 0 | libcerror_error_set( |
1499 | 0 | error, |
1500 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1501 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1502 | 0 | "%s: invalid UTF-16 string.", |
1503 | 0 | function ); |
1504 | |
|
1505 | 0 | return( -1 ); |
1506 | 0 | } |
1507 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
1508 | 0 | { |
1509 | 0 | libcerror_error_set( |
1510 | 0 | error, |
1511 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1512 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1513 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
1514 | 0 | function ); |
1515 | |
|
1516 | 0 | return( -1 ); |
1517 | 0 | } |
1518 | 0 | if( utf8_stream == NULL ) |
1519 | 0 | { |
1520 | 0 | libcerror_error_set( |
1521 | 0 | error, |
1522 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1523 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1524 | 0 | "%s: invalid UTF-8 stream.", |
1525 | 0 | function ); |
1526 | |
|
1527 | 0 | return( -1 ); |
1528 | 0 | } |
1529 | 0 | if( utf8_stream_size > (size_t) SSIZE_MAX ) |
1530 | 0 | { |
1531 | 0 | libcerror_error_set( |
1532 | 0 | error, |
1533 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1534 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1535 | 0 | "%s: invalid UTF-8 stream size value exceeds maximum.", |
1536 | 0 | function ); |
1537 | |
|
1538 | 0 | return( -1 ); |
1539 | 0 | } |
1540 | 0 | if( utf8_stream_size == 0 ) |
1541 | 0 | { |
1542 | 0 | libcerror_error_set( |
1543 | 0 | error, |
1544 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1545 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
1546 | 0 | "%s: missing UTF-8 stream value.", |
1547 | 0 | function ); |
1548 | |
|
1549 | 0 | return( -1 ); |
1550 | 0 | } |
1551 | | /* Check if UTF-8 stream starts with a byte order mark (BOM) |
1552 | | */ |
1553 | 0 | if( utf8_stream_size >= 3 ) |
1554 | 0 | { |
1555 | 0 | if( ( utf8_stream[ 0 ] == 0x0ef ) |
1556 | 0 | && ( utf8_stream[ 1 ] == 0x0bb ) |
1557 | 0 | && ( utf8_stream[ 2 ] == 0x0bf ) ) |
1558 | 0 | { |
1559 | 0 | utf8_stream_index += 3; |
1560 | 0 | } |
1561 | 0 | } |
1562 | 0 | if( ( utf16_string_size >= 1 ) |
1563 | 0 | && ( utf16_string[ utf16_string_size - 1 ] == 0 ) ) |
1564 | 0 | { |
1565 | 0 | utf16_string_size -= 1; |
1566 | 0 | } |
1567 | | /* Check if the UTF-8 stream is terminated with zero bytes |
1568 | | */ |
1569 | 0 | if( ( utf8_stream_size >= 1 ) |
1570 | 0 | && ( utf8_stream[ utf8_stream_size - 1 ] == 0 ) ) |
1571 | 0 | { |
1572 | 0 | utf8_stream_size -= 1; |
1573 | 0 | } |
1574 | 0 | while( ( utf16_string_index < utf16_string_size ) |
1575 | 0 | && ( utf8_stream_index < utf8_stream_size ) ) |
1576 | 0 | { |
1577 | | /* Convert the UTF-16 character bytes into an Unicode character |
1578 | | */ |
1579 | 0 | if( libuna_unicode_character_copy_from_utf16( |
1580 | 0 | &utf16_unicode_character, |
1581 | 0 | utf16_string, |
1582 | 0 | utf16_string_size, |
1583 | 0 | &utf16_string_index, |
1584 | 0 | error ) != 1 ) |
1585 | 0 | { |
1586 | 0 | libcerror_error_set( |
1587 | 0 | error, |
1588 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1589 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
1590 | 0 | "%s: unable to copy Unicode character from UTF-16.", |
1591 | 0 | function ); |
1592 | |
|
1593 | 0 | return( -1 ); |
1594 | 0 | } |
1595 | | /* Convert the UTF-8 character bytes into an Unicode character |
1596 | | */ |
1597 | 0 | if( libuna_unicode_character_copy_from_utf8( |
1598 | 0 | &utf8_stream_unicode_character, |
1599 | 0 | utf8_stream, |
1600 | 0 | utf8_stream_size, |
1601 | 0 | &utf8_stream_index, |
1602 | 0 | error ) != 1 ) |
1603 | 0 | { |
1604 | 0 | libcerror_error_set( |
1605 | 0 | error, |
1606 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1607 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
1608 | 0 | "%s: unable to copy Unicode character from UTF-8 stream.", |
1609 | 0 | function ); |
1610 | |
|
1611 | 0 | return( -1 ); |
1612 | 0 | } |
1613 | 0 | if( utf16_unicode_character < utf8_stream_unicode_character ) |
1614 | 0 | { |
1615 | 0 | return( LIBUNA_COMPARE_LESS ); |
1616 | 0 | } |
1617 | 0 | else if( utf16_unicode_character > utf8_stream_unicode_character ) |
1618 | 0 | { |
1619 | 0 | return( LIBUNA_COMPARE_GREATER ); |
1620 | 0 | } |
1621 | 0 | } |
1622 | | /* Check if both strings were entirely processed |
1623 | | */ |
1624 | 0 | if( utf16_string_index < utf16_string_size ) |
1625 | 0 | { |
1626 | 0 | return( LIBUNA_COMPARE_GREATER ); |
1627 | 0 | } |
1628 | 0 | else if( utf8_stream_index < utf8_stream_size ) |
1629 | 0 | { |
1630 | 0 | return( LIBUNA_COMPARE_LESS ); |
1631 | 0 | } |
1632 | 0 | return( LIBUNA_COMPARE_EQUAL ); |
1633 | 0 | } |
1634 | | |
1635 | | /* Determines the size of an UTF-16 string from an UTF-16 stream |
1636 | | * Returns 1 if successful or -1 on error |
1637 | | */ |
1638 | | int libuna_utf16_string_size_from_utf16_stream( |
1639 | | const uint8_t *utf16_stream, |
1640 | | size_t utf16_stream_size, |
1641 | | int byte_order, |
1642 | | size_t *utf16_string_size, |
1643 | | libcerror_error_t **error ) |
1644 | 87 | { |
1645 | 87 | static char *function = "libuna_utf16_string_size_from_utf16_stream"; |
1646 | 87 | size_t utf16_stream_index = 0; |
1647 | 87 | libuna_unicode_character_t unicode_character = 0; |
1648 | 87 | int read_byte_order = 0; |
1649 | | |
1650 | 87 | if( utf16_stream == NULL ) |
1651 | 0 | { |
1652 | 0 | libcerror_error_set( |
1653 | 0 | error, |
1654 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1655 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1656 | 0 | "%s: invalid UTF-16 stream.", |
1657 | 0 | function ); |
1658 | |
|
1659 | 0 | return( -1 ); |
1660 | 0 | } |
1661 | 87 | if( utf16_stream_size > (size_t) SSIZE_MAX ) |
1662 | 0 | { |
1663 | 0 | libcerror_error_set( |
1664 | 0 | error, |
1665 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1666 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1667 | 0 | "%s: invalid UTF-16 stream size value exceeds maximum.", |
1668 | 0 | function ); |
1669 | |
|
1670 | 0 | return( -1 ); |
1671 | 0 | } |
1672 | 87 | if( ( utf16_stream_size % 2 ) != 0 ) |
1673 | 0 | { |
1674 | 0 | libcerror_error_set( |
1675 | 0 | error, |
1676 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1677 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1678 | 0 | "%s: missing UTF-16 stream value.", |
1679 | 0 | function ); |
1680 | |
|
1681 | 0 | return( -1 ); |
1682 | 0 | } |
1683 | 87 | if( utf16_string_size == NULL ) |
1684 | 0 | { |
1685 | 0 | libcerror_error_set( |
1686 | 0 | error, |
1687 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1688 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1689 | 0 | "%s: invalid UTF-16 string size.", |
1690 | 0 | function ); |
1691 | |
|
1692 | 0 | return( -1 ); |
1693 | 0 | } |
1694 | 87 | *utf16_string_size = 0; |
1695 | | |
1696 | 87 | if( utf16_stream_size == 0 ) |
1697 | 0 | { |
1698 | 0 | return( 1 ); |
1699 | 0 | } |
1700 | | /* Check if UTF-16 stream is in big or little endian |
1701 | | */ |
1702 | 87 | if( utf16_stream_size >= 2 ) |
1703 | 87 | { |
1704 | 87 | if( ( utf16_stream[ 0 ] == 0x0ff ) |
1705 | 87 | && ( utf16_stream[ 1 ] == 0x0fe ) ) |
1706 | 1 | { |
1707 | 1 | read_byte_order = LIBUNA_ENDIAN_LITTLE; |
1708 | 1 | utf16_stream_index = 2; |
1709 | 1 | } |
1710 | 86 | else if( ( utf16_stream[ 0 ] == 0x0fe ) |
1711 | 86 | && ( utf16_stream[ 1 ] == 0x0ff ) ) |
1712 | 2 | { |
1713 | 2 | read_byte_order = LIBUNA_ENDIAN_BIG; |
1714 | 2 | utf16_stream_index = 2; |
1715 | 2 | } |
1716 | 87 | if( byte_order == 0 ) |
1717 | 0 | { |
1718 | 0 | byte_order = read_byte_order; |
1719 | 0 | } |
1720 | 87 | } |
1721 | 87 | if( ( byte_order != LIBUNA_ENDIAN_BIG ) |
1722 | 87 | && ( byte_order != LIBUNA_ENDIAN_LITTLE ) ) |
1723 | 0 | { |
1724 | 0 | libcerror_error_set( |
1725 | 0 | error, |
1726 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1727 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
1728 | 0 | "%s: unsupported byte order.", |
1729 | 0 | function ); |
1730 | |
|
1731 | 0 | return( -1 ); |
1732 | 0 | } |
1733 | 1.56k | while( ( utf16_stream_index + 1 ) < utf16_stream_size ) |
1734 | 1.55k | { |
1735 | | /* Convert the UTF-16 stream bytes into an Unicode character |
1736 | | */ |
1737 | 1.55k | if( libuna_unicode_character_copy_from_utf16_stream( |
1738 | 1.55k | &unicode_character, |
1739 | 1.55k | utf16_stream, |
1740 | 1.55k | utf16_stream_size, |
1741 | 1.55k | &utf16_stream_index, |
1742 | 1.55k | byte_order, |
1743 | 1.55k | error ) != 1 ) |
1744 | 7 | { |
1745 | 7 | libcerror_error_set( |
1746 | 7 | error, |
1747 | 7 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1748 | 7 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
1749 | 7 | "%s: unable to copy Unicode character from UTF-16 stream.", |
1750 | 7 | function ); |
1751 | | |
1752 | 7 | return( -1 ); |
1753 | 7 | } |
1754 | | /* Determine how many UTF-16 character bytes are required |
1755 | | */ |
1756 | 1.54k | if( libuna_unicode_character_size_to_utf16( |
1757 | 1.54k | unicode_character, |
1758 | 1.54k | utf16_string_size, |
1759 | 1.54k | error ) != 1 ) |
1760 | 0 | { |
1761 | 0 | libcerror_error_set( |
1762 | 0 | error, |
1763 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1764 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
1765 | 0 | "%s: unable to unable to determine size of Unicode character in UTF-16.", |
1766 | 0 | function ); |
1767 | |
|
1768 | 0 | return( -1 ); |
1769 | 0 | } |
1770 | 1.54k | if( unicode_character == 0 ) |
1771 | 66 | { |
1772 | 66 | break; |
1773 | 66 | } |
1774 | 1.54k | } |
1775 | | /* Check if the string is terminated with an end-of-string character |
1776 | | */ |
1777 | 80 | if( unicode_character != 0 ) |
1778 | 14 | { |
1779 | 14 | *utf16_string_size += 1; |
1780 | 14 | } |
1781 | 80 | return( 1 ); |
1782 | 87 | } |
1783 | | |
1784 | | /* Copies an UTF-16 string from an UTF-16 stream |
1785 | | * Returns 1 if successful or -1 on error |
1786 | | */ |
1787 | | int libuna_utf16_string_copy_from_utf16_stream( |
1788 | | libuna_utf16_character_t *utf16_string, |
1789 | | size_t utf16_string_size, |
1790 | | const uint8_t *utf16_stream, |
1791 | | size_t utf16_stream_size, |
1792 | | int byte_order, |
1793 | | libcerror_error_t **error ) |
1794 | 0 | { |
1795 | 0 | static char *function = "libuna_utf16_string_copy_from_utf16_stream"; |
1796 | 0 | size_t utf16_string_index = 0; |
1797 | |
|
1798 | 0 | if( libuna_utf16_string_with_index_copy_from_utf16_stream( |
1799 | 0 | utf16_string, |
1800 | 0 | utf16_string_size, |
1801 | 0 | &utf16_string_index, |
1802 | 0 | utf16_stream, |
1803 | 0 | utf16_stream_size, |
1804 | 0 | byte_order, |
1805 | 0 | error ) != 1 ) |
1806 | 0 | { |
1807 | 0 | libcerror_error_set( |
1808 | 0 | error, |
1809 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
1810 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
1811 | 0 | "%s: unable to copy UTF-16 stream to UTF-16 string.", |
1812 | 0 | function ); |
1813 | |
|
1814 | 0 | return( -1 ); |
1815 | 0 | } |
1816 | 0 | return( 1 ); |
1817 | 0 | } |
1818 | | |
1819 | | /* Copies an UTF-16 string from an UTF-16 stream |
1820 | | * Returns 1 if successful or -1 on error |
1821 | | */ |
1822 | | int libuna_utf16_string_with_index_copy_from_utf16_stream( |
1823 | | libuna_utf16_character_t *utf16_string, |
1824 | | size_t utf16_string_size, |
1825 | | size_t *utf16_string_index, |
1826 | | const uint8_t *utf16_stream, |
1827 | | size_t utf16_stream_size, |
1828 | | int byte_order, |
1829 | | libcerror_error_t **error ) |
1830 | 0 | { |
1831 | 0 | static char *function = "libuna_utf16_string_with_index_copy_from_utf16_stream"; |
1832 | 0 | size_t utf16_stream_index = 0; |
1833 | 0 | libuna_unicode_character_t unicode_character = 0; |
1834 | 0 | int read_byte_order = 0; |
1835 | |
|
1836 | 0 | if( utf16_string == NULL ) |
1837 | 0 | { |
1838 | 0 | libcerror_error_set( |
1839 | 0 | error, |
1840 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1841 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1842 | 0 | "%s: invalid UTF-16 string.", |
1843 | 0 | function ); |
1844 | |
|
1845 | 0 | return( -1 ); |
1846 | 0 | } |
1847 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
1848 | 0 | { |
1849 | 0 | libcerror_error_set( |
1850 | 0 | error, |
1851 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1852 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1853 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
1854 | 0 | function ); |
1855 | |
|
1856 | 0 | return( -1 ); |
1857 | 0 | } |
1858 | 0 | if( utf16_string_index == NULL ) |
1859 | 0 | { |
1860 | 0 | libcerror_error_set( |
1861 | 0 | error, |
1862 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1863 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1864 | 0 | "%s: invalid UTF-16 string index.", |
1865 | 0 | function ); |
1866 | |
|
1867 | 0 | return( -1 ); |
1868 | 0 | } |
1869 | 0 | if( utf16_stream == NULL ) |
1870 | 0 | { |
1871 | 0 | libcerror_error_set( |
1872 | 0 | error, |
1873 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1874 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
1875 | 0 | "%s: invalid UTF-16 stream.", |
1876 | 0 | function ); |
1877 | |
|
1878 | 0 | return( -1 ); |
1879 | 0 | } |
1880 | 0 | if( utf16_stream_size > (size_t) SSIZE_MAX ) |
1881 | 0 | { |
1882 | 0 | libcerror_error_set( |
1883 | 0 | error, |
1884 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1885 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
1886 | 0 | "%s: invalid UTF-16 stream size value exceeds maximum.", |
1887 | 0 | function ); |
1888 | |
|
1889 | 0 | return( -1 ); |
1890 | 0 | } |
1891 | 0 | if( ( utf16_stream_size == 0 ) |
1892 | 0 | || ( ( utf16_stream_size % 2 ) != 0 ) ) |
1893 | 0 | { |
1894 | 0 | libcerror_error_set( |
1895 | 0 | error, |
1896 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1897 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1898 | 0 | "%s: missing UTF-16 stream value.", |
1899 | 0 | function ); |
1900 | |
|
1901 | 0 | return( -1 ); |
1902 | 0 | } |
1903 | | /* Check if UTF-16 stream is in big or little endian |
1904 | | */ |
1905 | 0 | if( utf16_stream_size >= 2 ) |
1906 | 0 | { |
1907 | 0 | if( ( utf16_stream[ 0 ] == 0x0ff ) |
1908 | 0 | && ( utf16_stream[ 1 ] == 0x0fe ) ) |
1909 | 0 | { |
1910 | 0 | read_byte_order = LIBUNA_ENDIAN_LITTLE; |
1911 | 0 | utf16_stream_index = 2; |
1912 | 0 | } |
1913 | 0 | else if( ( utf16_stream[ 0 ] == 0x0fe ) |
1914 | 0 | && ( utf16_stream[ 1 ] == 0x0ff ) ) |
1915 | 0 | { |
1916 | 0 | read_byte_order = LIBUNA_ENDIAN_BIG; |
1917 | 0 | utf16_stream_index = 2; |
1918 | 0 | } |
1919 | 0 | if( byte_order == 0 ) |
1920 | 0 | { |
1921 | 0 | byte_order = read_byte_order; |
1922 | 0 | } |
1923 | 0 | } |
1924 | 0 | if( ( byte_order != LIBUNA_ENDIAN_BIG ) |
1925 | 0 | && ( byte_order != LIBUNA_ENDIAN_LITTLE ) ) |
1926 | 0 | { |
1927 | 0 | libcerror_error_set( |
1928 | 0 | error, |
1929 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1930 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
1931 | 0 | "%s: unsupported byte order.", |
1932 | 0 | function ); |
1933 | |
|
1934 | 0 | return( -1 ); |
1935 | 0 | } |
1936 | 0 | while( ( utf16_stream_index + 1 ) < utf16_stream_size ) |
1937 | 0 | { |
1938 | | /* Convert the UTF-16 stream bytes into an Unicode character |
1939 | | */ |
1940 | 0 | if( libuna_unicode_character_copy_from_utf16_stream( |
1941 | 0 | &unicode_character, |
1942 | 0 | utf16_stream, |
1943 | 0 | utf16_stream_size, |
1944 | 0 | &utf16_stream_index, |
1945 | 0 | byte_order, |
1946 | 0 | error ) != 1 ) |
1947 | 0 | { |
1948 | 0 | libcerror_error_set( |
1949 | 0 | error, |
1950 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1951 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
1952 | 0 | "%s: unable to copy Unicode character from UTF-16 stream.", |
1953 | 0 | function ); |
1954 | |
|
1955 | 0 | return( -1 ); |
1956 | 0 | } |
1957 | | /* Convert the Unicode character into UTF-16 character bytes |
1958 | | */ |
1959 | 0 | if( libuna_unicode_character_copy_to_utf16( |
1960 | 0 | unicode_character, |
1961 | 0 | utf16_string, |
1962 | 0 | utf16_string_size, |
1963 | 0 | utf16_string_index, |
1964 | 0 | error ) != 1 ) |
1965 | 0 | { |
1966 | 0 | libcerror_error_set( |
1967 | 0 | error, |
1968 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
1969 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
1970 | 0 | "%s: unable to copy Unicode character to UTF-16.", |
1971 | 0 | function ); |
1972 | |
|
1973 | 0 | return( -1 ); |
1974 | 0 | } |
1975 | 0 | if( unicode_character == 0 ) |
1976 | 0 | { |
1977 | 0 | break; |
1978 | 0 | } |
1979 | 0 | } |
1980 | | /* Check if the string is terminated with an end-of-string character |
1981 | | */ |
1982 | 0 | if( unicode_character != 0 ) |
1983 | 0 | { |
1984 | 0 | if( *utf16_string_index >= utf16_string_size ) |
1985 | 0 | { |
1986 | 0 | libcerror_error_set( |
1987 | 0 | error, |
1988 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
1989 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
1990 | 0 | "%s: UTF-16 string too small.", |
1991 | 0 | function ); |
1992 | |
|
1993 | 0 | return( -1 ); |
1994 | 0 | } |
1995 | 0 | utf16_string[ *utf16_string_index ] = 0; |
1996 | |
|
1997 | 0 | *utf16_string_index += 1; |
1998 | 0 | } |
1999 | 0 | return( 1 ); |
2000 | 0 | } |
2001 | | |
2002 | | /* Compares an UTF-16 string with an UTF-16 stream |
2003 | | * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error |
2004 | | */ |
2005 | | int libuna_utf16_string_compare_with_utf16_stream( |
2006 | | const libuna_utf16_character_t *utf16_string, |
2007 | | size_t utf16_string_size, |
2008 | | const uint8_t *utf16_stream, |
2009 | | size_t utf16_stream_size, |
2010 | | int byte_order, |
2011 | | libcerror_error_t **error ) |
2012 | 0 | { |
2013 | 0 | static char *function = "libuna_utf16_string_compare_with_utf16_stream"; |
2014 | 0 | size_t utf16_stream_index = 0; |
2015 | 0 | size_t utf16_string_index = 0; |
2016 | 0 | libuna_unicode_character_t utf16_unicode_character = 0; |
2017 | 0 | libuna_unicode_character_t utf16_stream_unicode_character = 0; |
2018 | 0 | int read_byte_order = 0; |
2019 | |
|
2020 | 0 | if( utf16_string == NULL ) |
2021 | 0 | { |
2022 | 0 | libcerror_error_set( |
2023 | 0 | error, |
2024 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2025 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2026 | 0 | "%s: invalid UTF-16 string.", |
2027 | 0 | function ); |
2028 | |
|
2029 | 0 | return( -1 ); |
2030 | 0 | } |
2031 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
2032 | 0 | { |
2033 | 0 | libcerror_error_set( |
2034 | 0 | error, |
2035 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2036 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2037 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
2038 | 0 | function ); |
2039 | |
|
2040 | 0 | return( -1 ); |
2041 | 0 | } |
2042 | 0 | if( utf16_stream == NULL ) |
2043 | 0 | { |
2044 | 0 | libcerror_error_set( |
2045 | 0 | error, |
2046 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2047 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2048 | 0 | "%s: invalid UTF-16 stream.", |
2049 | 0 | function ); |
2050 | |
|
2051 | 0 | return( -1 ); |
2052 | 0 | } |
2053 | 0 | if( utf16_stream_size > (size_t) SSIZE_MAX ) |
2054 | 0 | { |
2055 | 0 | libcerror_error_set( |
2056 | 0 | error, |
2057 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2058 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2059 | 0 | "%s: invalid UTF-16 stream size value exceeds maximum.", |
2060 | 0 | function ); |
2061 | |
|
2062 | 0 | return( -1 ); |
2063 | 0 | } |
2064 | 0 | if( ( utf16_stream_size == 0 ) |
2065 | 0 | || ( ( utf16_stream_size % 2 ) != 0 ) ) |
2066 | 0 | { |
2067 | 0 | libcerror_error_set( |
2068 | 0 | error, |
2069 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2070 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
2071 | 0 | "%s: missing UTF-16 stream value.", |
2072 | 0 | function ); |
2073 | |
|
2074 | 0 | return( -1 ); |
2075 | 0 | } |
2076 | | /* Check if UTF-16 stream is in big or little endian |
2077 | | */ |
2078 | 0 | if( utf16_stream_size >= 2 ) |
2079 | 0 | { |
2080 | 0 | if( ( utf16_stream[ 0 ] == 0xfe ) |
2081 | 0 | && ( utf16_stream[ 1 ] == 0xff ) ) |
2082 | 0 | { |
2083 | 0 | read_byte_order = LIBUNA_ENDIAN_BIG; |
2084 | 0 | utf16_stream_index = 2; |
2085 | 0 | } |
2086 | 0 | else if( ( utf16_stream[ 0 ] == 0xff ) |
2087 | 0 | && ( utf16_stream[ 1 ] == 0xfe ) ) |
2088 | 0 | { |
2089 | 0 | read_byte_order = LIBUNA_ENDIAN_LITTLE; |
2090 | 0 | utf16_stream_index = 2; |
2091 | 0 | } |
2092 | 0 | if( byte_order == 0 ) |
2093 | 0 | { |
2094 | 0 | byte_order = read_byte_order; |
2095 | 0 | } |
2096 | 0 | } |
2097 | 0 | if( ( byte_order != LIBUNA_ENDIAN_BIG ) |
2098 | 0 | && ( byte_order != LIBUNA_ENDIAN_LITTLE ) ) |
2099 | 0 | { |
2100 | 0 | libcerror_error_set( |
2101 | 0 | error, |
2102 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2103 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
2104 | 0 | "%s: unsupported byte order.", |
2105 | 0 | function ); |
2106 | |
|
2107 | 0 | return( -1 ); |
2108 | 0 | } |
2109 | 0 | if( ( utf16_string_size >= 1 ) |
2110 | 0 | && ( utf16_string[ utf16_string_size - 1 ] == 0 ) ) |
2111 | 0 | { |
2112 | 0 | utf16_string_size -= 1; |
2113 | 0 | } |
2114 | | /* Check if the UTF-16 stream is terminated with zero bytes |
2115 | | */ |
2116 | 0 | if( ( utf16_stream_size >= 2 ) |
2117 | 0 | && ( utf16_stream[ utf16_stream_size - 2 ] == 0 ) |
2118 | 0 | && ( utf16_stream[ utf16_stream_size - 1 ] == 0 ) ) |
2119 | 0 | { |
2120 | 0 | utf16_stream_size -= 2; |
2121 | 0 | } |
2122 | 0 | while( ( utf16_string_index < utf16_string_size ) |
2123 | 0 | && ( utf16_stream_index < utf16_stream_size ) ) |
2124 | 0 | { |
2125 | | /* Convert the UTF-16 character bytes into an Unicode character |
2126 | | */ |
2127 | 0 | if( libuna_unicode_character_copy_from_utf16( |
2128 | 0 | &utf16_unicode_character, |
2129 | 0 | utf16_string, |
2130 | 0 | utf16_string_size, |
2131 | 0 | &utf16_string_index, |
2132 | 0 | error ) != 1 ) |
2133 | 0 | { |
2134 | 0 | libcerror_error_set( |
2135 | 0 | error, |
2136 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2137 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
2138 | 0 | "%s: unable to copy Unicode character from UTF-16.", |
2139 | 0 | function ); |
2140 | |
|
2141 | 0 | return( -1 ); |
2142 | 0 | } |
2143 | | /* Convert the UTF-16 stream bytes into an Unicode character |
2144 | | */ |
2145 | 0 | if( libuna_unicode_character_copy_from_utf16_stream( |
2146 | 0 | &utf16_stream_unicode_character, |
2147 | 0 | utf16_stream, |
2148 | 0 | utf16_stream_size, |
2149 | 0 | &utf16_stream_index, |
2150 | 0 | byte_order, |
2151 | 0 | error ) != 1 ) |
2152 | 0 | { |
2153 | 0 | libcerror_error_set( |
2154 | 0 | error, |
2155 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2156 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
2157 | 0 | "%s: unable to copy Unicode character from UTF-16 stream.", |
2158 | 0 | function ); |
2159 | |
|
2160 | 0 | return( -1 ); |
2161 | 0 | } |
2162 | 0 | if( utf16_unicode_character < utf16_stream_unicode_character ) |
2163 | 0 | { |
2164 | 0 | return( LIBUNA_COMPARE_LESS ); |
2165 | 0 | } |
2166 | 0 | else if( utf16_unicode_character > utf16_stream_unicode_character ) |
2167 | 0 | { |
2168 | 0 | return( LIBUNA_COMPARE_GREATER ); |
2169 | 0 | } |
2170 | 0 | } |
2171 | | /* Check if both strings were entirely processed |
2172 | | */ |
2173 | 0 | if( utf16_string_index < utf16_string_size ) |
2174 | 0 | { |
2175 | 0 | return( LIBUNA_COMPARE_GREATER ); |
2176 | 0 | } |
2177 | 0 | else if( utf16_stream_index < utf16_stream_size ) |
2178 | 0 | { |
2179 | 0 | return( LIBUNA_COMPARE_LESS ); |
2180 | 0 | } |
2181 | 0 | return( LIBUNA_COMPARE_EQUAL ); |
2182 | 0 | } |
2183 | | |
2184 | | /* Determines the size of an UTF-16 string from an UTF-32 string |
2185 | | * Returns 1 if successful or -1 on error |
2186 | | */ |
2187 | | int libuna_utf16_string_size_from_utf32( |
2188 | | const libuna_utf32_character_t *utf32_string, |
2189 | | size_t utf32_string_size, |
2190 | | size_t *utf16_string_size, |
2191 | | libcerror_error_t **error ) |
2192 | 0 | { |
2193 | 0 | static char *function = "libuna_utf16_string_size_from_utf32"; |
2194 | 0 | size_t utf32_string_index = 0; |
2195 | 0 | libuna_unicode_character_t unicode_character = 0; |
2196 | |
|
2197 | 0 | if( utf32_string == NULL ) |
2198 | 0 | { |
2199 | 0 | libcerror_error_set( |
2200 | 0 | error, |
2201 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2202 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2203 | 0 | "%s: invalid UTF-32 string.", |
2204 | 0 | function ); |
2205 | |
|
2206 | 0 | return( -1 ); |
2207 | 0 | } |
2208 | 0 | if( utf32_string_size > (size_t) SSIZE_MAX ) |
2209 | 0 | { |
2210 | 0 | libcerror_error_set( |
2211 | 0 | error, |
2212 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2213 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2214 | 0 | "%s: invalid UTF-32 string size value exceeds maximum.", |
2215 | 0 | function ); |
2216 | |
|
2217 | 0 | return( -1 ); |
2218 | 0 | } |
2219 | 0 | if( utf16_string_size == NULL ) |
2220 | 0 | { |
2221 | 0 | libcerror_error_set( |
2222 | 0 | error, |
2223 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2224 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2225 | 0 | "%s: invalid UTF-16 string size.", |
2226 | 0 | function ); |
2227 | |
|
2228 | 0 | return( -1 ); |
2229 | 0 | } |
2230 | 0 | *utf16_string_size = 0; |
2231 | |
|
2232 | 0 | if( utf32_string_size == 0 ) |
2233 | 0 | { |
2234 | 0 | return( 1 ); |
2235 | 0 | } |
2236 | 0 | while( utf32_string_index < utf32_string_size ) |
2237 | 0 | { |
2238 | | /* Convert the UTF-32 character bytes into an Unicode character |
2239 | | */ |
2240 | 0 | if( libuna_unicode_character_copy_from_utf32( |
2241 | 0 | &unicode_character, |
2242 | 0 | utf32_string, |
2243 | 0 | utf32_string_size, |
2244 | 0 | &utf32_string_index, |
2245 | 0 | error ) != 1 ) |
2246 | 0 | { |
2247 | 0 | libcerror_error_set( |
2248 | 0 | error, |
2249 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2250 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
2251 | 0 | "%s: unable to copy Unicode character from UTF-32.", |
2252 | 0 | function ); |
2253 | |
|
2254 | 0 | return( -1 ); |
2255 | 0 | } |
2256 | | /* Determine how many UTF-16 character bytes are required |
2257 | | */ |
2258 | 0 | if( libuna_unicode_character_size_to_utf16( |
2259 | 0 | unicode_character, |
2260 | 0 | utf16_string_size, |
2261 | 0 | error ) != 1 ) |
2262 | 0 | { |
2263 | 0 | libcerror_error_set( |
2264 | 0 | error, |
2265 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2266 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
2267 | 0 | "%s: unable to unable to determine size of Unicode character in UTF-16.", |
2268 | 0 | function ); |
2269 | |
|
2270 | 0 | return( -1 ); |
2271 | 0 | } |
2272 | 0 | if( unicode_character == 0 ) |
2273 | 0 | { |
2274 | 0 | break; |
2275 | 0 | } |
2276 | 0 | } |
2277 | | /* Check if the string is terminated with an end-of-string character |
2278 | | */ |
2279 | 0 | if( unicode_character != 0 ) |
2280 | 0 | { |
2281 | 0 | *utf16_string_size += 1; |
2282 | 0 | } |
2283 | 0 | return( 1 ); |
2284 | 0 | } |
2285 | | |
2286 | | /* Copies an UTF-16 string from an UTF-32 string |
2287 | | * Returns 1 if successful or -1 on error |
2288 | | */ |
2289 | | int libuna_utf16_string_copy_from_utf32( |
2290 | | libuna_utf16_character_t *utf16_string, |
2291 | | size_t utf16_string_size, |
2292 | | const libuna_utf32_character_t *utf32_string, |
2293 | | size_t utf32_string_size, |
2294 | | libcerror_error_t **error ) |
2295 | 0 | { |
2296 | 0 | static char *function = "libuna_utf16_string_copy_from_utf32"; |
2297 | 0 | size_t utf16_string_index = 0; |
2298 | |
|
2299 | 0 | if( libuna_utf16_string_with_index_copy_from_utf32( |
2300 | 0 | utf16_string, |
2301 | 0 | utf16_string_size, |
2302 | 0 | &utf16_string_index, |
2303 | 0 | utf32_string, |
2304 | 0 | utf32_string_size, |
2305 | 0 | error ) != 1 ) |
2306 | 0 | { |
2307 | 0 | libcerror_error_set( |
2308 | 0 | error, |
2309 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
2310 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
2311 | 0 | "%s: unable to copy UTF-32 string to UTF-16 string.", |
2312 | 0 | function ); |
2313 | |
|
2314 | 0 | return( -1 ); |
2315 | 0 | } |
2316 | 0 | return( 1 ); |
2317 | 0 | } |
2318 | | |
2319 | | /* Copies an UTF-16 string from an UTF-32 string |
2320 | | * Returns 1 if successful or -1 on error |
2321 | | */ |
2322 | | int libuna_utf16_string_with_index_copy_from_utf32( |
2323 | | libuna_utf16_character_t *utf16_string, |
2324 | | size_t utf16_string_size, |
2325 | | size_t *utf16_string_index, |
2326 | | const libuna_utf32_character_t *utf32_string, |
2327 | | size_t utf32_string_size, |
2328 | | libcerror_error_t **error ) |
2329 | 0 | { |
2330 | 0 | static char *function = "libuna_utf16_string_with_index_copy_from_utf32"; |
2331 | 0 | size_t utf32_string_index = 0; |
2332 | 0 | libuna_unicode_character_t unicode_character = 0; |
2333 | |
|
2334 | 0 | if( utf16_string == NULL ) |
2335 | 0 | { |
2336 | 0 | libcerror_error_set( |
2337 | 0 | error, |
2338 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2339 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2340 | 0 | "%s: invalid UTF-16 string.", |
2341 | 0 | function ); |
2342 | |
|
2343 | 0 | return( -1 ); |
2344 | 0 | } |
2345 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
2346 | 0 | { |
2347 | 0 | libcerror_error_set( |
2348 | 0 | error, |
2349 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2350 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2351 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
2352 | 0 | function ); |
2353 | |
|
2354 | 0 | return( -1 ); |
2355 | 0 | } |
2356 | 0 | if( utf16_string_index == NULL ) |
2357 | 0 | { |
2358 | 0 | libcerror_error_set( |
2359 | 0 | error, |
2360 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2361 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2362 | 0 | "%s: invalid UTF-16 string index.", |
2363 | 0 | function ); |
2364 | |
|
2365 | 0 | return( -1 ); |
2366 | 0 | } |
2367 | 0 | if( utf32_string == NULL ) |
2368 | 0 | { |
2369 | 0 | libcerror_error_set( |
2370 | 0 | error, |
2371 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2372 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2373 | 0 | "%s: invalid UTF-32 string.", |
2374 | 0 | function ); |
2375 | |
|
2376 | 0 | return( -1 ); |
2377 | 0 | } |
2378 | 0 | if( utf32_string_size > (size_t) SSIZE_MAX ) |
2379 | 0 | { |
2380 | 0 | libcerror_error_set( |
2381 | 0 | error, |
2382 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2383 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2384 | 0 | "%s: invalid UTF-32 string size value exceeds maximum.", |
2385 | 0 | function ); |
2386 | |
|
2387 | 0 | return( -1 ); |
2388 | 0 | } |
2389 | 0 | if( utf32_string_size == 0 ) |
2390 | 0 | { |
2391 | 0 | libcerror_error_set( |
2392 | 0 | error, |
2393 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2394 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
2395 | 0 | "%s: missing UTF-32 string value.", |
2396 | 0 | function ); |
2397 | |
|
2398 | 0 | return( -1 ); |
2399 | 0 | } |
2400 | 0 | while( utf32_string_index < utf32_string_size ) |
2401 | 0 | { |
2402 | | /* Convert the UTF-32 character bytes into an Unicode character |
2403 | | */ |
2404 | 0 | if( libuna_unicode_character_copy_from_utf32( |
2405 | 0 | &unicode_character, |
2406 | 0 | utf32_string, |
2407 | 0 | utf32_string_size, |
2408 | 0 | &utf32_string_index, |
2409 | 0 | error ) != 1 ) |
2410 | 0 | { |
2411 | 0 | libcerror_error_set( |
2412 | 0 | error, |
2413 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2414 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
2415 | 0 | "%s: unable to copy Unicode character from UTF-32.", |
2416 | 0 | function ); |
2417 | |
|
2418 | 0 | return( -1 ); |
2419 | 0 | } |
2420 | | /* Convert the Unicode character into UTF-16 character bytes |
2421 | | */ |
2422 | 0 | if( libuna_unicode_character_copy_to_utf16( |
2423 | 0 | unicode_character, |
2424 | 0 | utf16_string, |
2425 | 0 | utf16_string_size, |
2426 | 0 | utf16_string_index, |
2427 | 0 | error ) != 1 ) |
2428 | 0 | { |
2429 | 0 | libcerror_error_set( |
2430 | 0 | error, |
2431 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2432 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
2433 | 0 | "%s: unable to copy Unicode character to UTF-16.", |
2434 | 0 | function ); |
2435 | |
|
2436 | 0 | return( -1 ); |
2437 | 0 | } |
2438 | 0 | if( unicode_character == 0 ) |
2439 | 0 | { |
2440 | 0 | break; |
2441 | 0 | } |
2442 | 0 | } |
2443 | | /* Check if the string is terminated with an end-of-string character |
2444 | | */ |
2445 | 0 | if( unicode_character != 0 ) |
2446 | 0 | { |
2447 | 0 | if( *utf16_string_index >= utf16_string_size ) |
2448 | 0 | { |
2449 | 0 | libcerror_error_set( |
2450 | 0 | error, |
2451 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2452 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
2453 | 0 | "%s: UTF-16 string too small.", |
2454 | 0 | function ); |
2455 | |
|
2456 | 0 | return( -1 ); |
2457 | 0 | } |
2458 | 0 | utf16_string[ *utf16_string_index ] = 0; |
2459 | |
|
2460 | 0 | *utf16_string_index += 1; |
2461 | 0 | } |
2462 | 0 | return( 1 ); |
2463 | 0 | } |
2464 | | |
2465 | | /* Compares an UTF-16 string with an UTF-32 string |
2466 | | * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error |
2467 | | */ |
2468 | | int libuna_utf16_string_compare_with_utf32( |
2469 | | const libuna_utf16_character_t *utf16_string, |
2470 | | size_t utf16_string_size, |
2471 | | const libuna_utf32_character_t *utf32_string, |
2472 | | size_t utf32_string_size, |
2473 | | libcerror_error_t **error ) |
2474 | 0 | { |
2475 | 0 | static char *function = "libuna_utf16_string_compare_with_utf32"; |
2476 | 0 | size_t utf16_string_index = 0; |
2477 | 0 | size_t utf32_string_index = 0; |
2478 | 0 | libuna_unicode_character_t utf16_unicode_character = 0; |
2479 | 0 | libuna_unicode_character_t utf32_unicode_character = 0; |
2480 | |
|
2481 | 0 | if( utf16_string == NULL ) |
2482 | 0 | { |
2483 | 0 | libcerror_error_set( |
2484 | 0 | error, |
2485 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2486 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2487 | 0 | "%s: invalid UTF-16 string.", |
2488 | 0 | function ); |
2489 | |
|
2490 | 0 | return( -1 ); |
2491 | 0 | } |
2492 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
2493 | 0 | { |
2494 | 0 | libcerror_error_set( |
2495 | 0 | error, |
2496 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2497 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2498 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
2499 | 0 | function ); |
2500 | |
|
2501 | 0 | return( -1 ); |
2502 | 0 | } |
2503 | 0 | if( utf32_string == NULL ) |
2504 | 0 | { |
2505 | 0 | libcerror_error_set( |
2506 | 0 | error, |
2507 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2508 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2509 | 0 | "%s: invalid UTF-32 string.", |
2510 | 0 | function ); |
2511 | |
|
2512 | 0 | return( -1 ); |
2513 | 0 | } |
2514 | 0 | if( utf32_string_size > (size_t) SSIZE_MAX ) |
2515 | 0 | { |
2516 | 0 | libcerror_error_set( |
2517 | 0 | error, |
2518 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2519 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2520 | 0 | "%s: invalid UTF-32 string size value exceeds maximum.", |
2521 | 0 | function ); |
2522 | |
|
2523 | 0 | return( -1 ); |
2524 | 0 | } |
2525 | 0 | if( utf32_string_size == 0 ) |
2526 | 0 | { |
2527 | 0 | libcerror_error_set( |
2528 | 0 | error, |
2529 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2530 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
2531 | 0 | "%s: missing UTF-32 string value.", |
2532 | 0 | function ); |
2533 | |
|
2534 | 0 | return( -1 ); |
2535 | 0 | } |
2536 | 0 | if( ( utf16_string_size >= 1 ) |
2537 | 0 | && ( utf16_string[ utf16_string_size - 1 ] == 0 ) ) |
2538 | 0 | { |
2539 | 0 | utf16_string_size -= 1; |
2540 | 0 | } |
2541 | 0 | if( ( utf32_string_size >= 1 ) |
2542 | 0 | && ( utf32_string[ utf32_string_size - 1 ] == 0 ) ) |
2543 | 0 | { |
2544 | 0 | utf32_string_size -= 1; |
2545 | 0 | } |
2546 | 0 | while( ( utf16_string_index < utf16_string_size ) |
2547 | 0 | && ( utf32_string_index < utf32_string_size ) ) |
2548 | 0 | { |
2549 | | /* Convert the UTF-16 character bytes into an Unicode character |
2550 | | */ |
2551 | 0 | if( libuna_unicode_character_copy_from_utf16( |
2552 | 0 | &utf16_unicode_character, |
2553 | 0 | utf16_string, |
2554 | 0 | utf16_string_size, |
2555 | 0 | &utf16_string_index, |
2556 | 0 | error ) != 1 ) |
2557 | 0 | { |
2558 | 0 | libcerror_error_set( |
2559 | 0 | error, |
2560 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2561 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
2562 | 0 | "%s: unable to copy Unicode character from UTF-16.", |
2563 | 0 | function ); |
2564 | |
|
2565 | 0 | return( -1 ); |
2566 | 0 | } |
2567 | | /* Convert the UTF-32 character bytes into an Unicode character |
2568 | | */ |
2569 | 0 | if( libuna_unicode_character_copy_from_utf32( |
2570 | 0 | &utf32_unicode_character, |
2571 | 0 | utf32_string, |
2572 | 0 | utf32_string_size, |
2573 | 0 | &utf32_string_index, |
2574 | 0 | error ) != 1 ) |
2575 | 0 | { |
2576 | 0 | libcerror_error_set( |
2577 | 0 | error, |
2578 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2579 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
2580 | 0 | "%s: unable to copy Unicode character from UTF-32.", |
2581 | 0 | function ); |
2582 | |
|
2583 | 0 | return( -1 ); |
2584 | 0 | } |
2585 | 0 | if( utf16_unicode_character < utf32_unicode_character ) |
2586 | 0 | { |
2587 | 0 | return( LIBUNA_COMPARE_LESS ); |
2588 | 0 | } |
2589 | 0 | else if( utf16_unicode_character > utf32_unicode_character ) |
2590 | 0 | { |
2591 | 0 | return( LIBUNA_COMPARE_GREATER ); |
2592 | 0 | } |
2593 | 0 | } |
2594 | | /* Check if both strings were entirely processed |
2595 | | */ |
2596 | 0 | if( utf16_string_index < utf16_string_size ) |
2597 | 0 | { |
2598 | 0 | return( LIBUNA_COMPARE_GREATER ); |
2599 | 0 | } |
2600 | 0 | else if( utf32_string_index < utf32_string_size ) |
2601 | 0 | { |
2602 | 0 | return( LIBUNA_COMPARE_LESS ); |
2603 | 0 | } |
2604 | 0 | return( LIBUNA_COMPARE_EQUAL ); |
2605 | 0 | } |
2606 | | |
2607 | | /* Determines the size of an UTF-16 string from an UTF-32 stream |
2608 | | * Returns 1 if successful or -1 on error |
2609 | | */ |
2610 | | int libuna_utf16_string_size_from_utf32_stream( |
2611 | | const uint8_t *utf32_stream, |
2612 | | size_t utf32_stream_size, |
2613 | | int byte_order, |
2614 | | size_t *utf16_string_size, |
2615 | | libcerror_error_t **error ) |
2616 | 0 | { |
2617 | 0 | static char *function = "libuna_utf16_string_size_from_utf32_stream"; |
2618 | 0 | size_t utf32_stream_index = 0; |
2619 | 0 | libuna_unicode_character_t unicode_character = 0; |
2620 | 0 | int read_byte_order = 0; |
2621 | |
|
2622 | 0 | if( utf32_stream == NULL ) |
2623 | 0 | { |
2624 | 0 | libcerror_error_set( |
2625 | 0 | error, |
2626 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2627 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2628 | 0 | "%s: invalid UTF-32 stream.", |
2629 | 0 | function ); |
2630 | |
|
2631 | 0 | return( -1 ); |
2632 | 0 | } |
2633 | 0 | if( utf32_stream_size > (size_t) SSIZE_MAX ) |
2634 | 0 | { |
2635 | 0 | libcerror_error_set( |
2636 | 0 | error, |
2637 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2638 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2639 | 0 | "%s: invalid UTF-32 stream size value exceeds maximum.", |
2640 | 0 | function ); |
2641 | |
|
2642 | 0 | return( -1 ); |
2643 | 0 | } |
2644 | 0 | if( ( utf32_stream_size % 4 ) != 0 ) |
2645 | 0 | { |
2646 | 0 | libcerror_error_set( |
2647 | 0 | error, |
2648 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2649 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
2650 | 0 | "%s: missing UTF-32 stream value.", |
2651 | 0 | function ); |
2652 | |
|
2653 | 0 | return( -1 ); |
2654 | 0 | } |
2655 | 0 | if( utf16_string_size == NULL ) |
2656 | 0 | { |
2657 | 0 | libcerror_error_set( |
2658 | 0 | error, |
2659 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2660 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2661 | 0 | "%s: invalid UTF-16 string size.", |
2662 | 0 | function ); |
2663 | |
|
2664 | 0 | return( -1 ); |
2665 | 0 | } |
2666 | 0 | *utf16_string_size = 0; |
2667 | |
|
2668 | 0 | if( utf32_stream_size == 0 ) |
2669 | 0 | { |
2670 | 0 | return( 1 ); |
2671 | 0 | } |
2672 | | /* Check if UTF-32 stream is in big or little endian |
2673 | | */ |
2674 | 0 | if( utf32_stream_size >= 4 ) |
2675 | 0 | { |
2676 | 0 | if( ( utf32_stream[ 0 ] == 0x00 ) |
2677 | 0 | && ( utf32_stream[ 1 ] == 0x00 ) |
2678 | 0 | && ( utf32_stream[ 2 ] == 0xfe ) |
2679 | 0 | && ( utf32_stream[ 3 ] == 0xff ) ) |
2680 | 0 | { |
2681 | 0 | read_byte_order = LIBUNA_ENDIAN_BIG; |
2682 | 0 | utf32_stream_index = 4; |
2683 | 0 | } |
2684 | 0 | else if( ( utf32_stream[ 0 ] == 0xff ) |
2685 | 0 | && ( utf32_stream[ 1 ] == 0xfe ) |
2686 | 0 | && ( utf32_stream[ 2 ] == 0x00 ) |
2687 | 0 | && ( utf32_stream[ 3 ] == 0x00 ) ) |
2688 | 0 | { |
2689 | 0 | read_byte_order = LIBUNA_ENDIAN_LITTLE; |
2690 | 0 | utf32_stream_index = 4; |
2691 | 0 | } |
2692 | 0 | if( byte_order == 0 ) |
2693 | 0 | { |
2694 | 0 | byte_order = read_byte_order; |
2695 | 0 | } |
2696 | 0 | } |
2697 | 0 | if( ( byte_order != LIBUNA_ENDIAN_BIG ) |
2698 | 0 | && ( byte_order != LIBUNA_ENDIAN_LITTLE ) ) |
2699 | 0 | { |
2700 | 0 | libcerror_error_set( |
2701 | 0 | error, |
2702 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2703 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
2704 | 0 | "%s: unsupported byte order.", |
2705 | 0 | function ); |
2706 | |
|
2707 | 0 | return( -1 ); |
2708 | 0 | } |
2709 | 0 | while( ( utf32_stream_index + 3 ) < utf32_stream_size ) |
2710 | 0 | { |
2711 | | /* Convert the UTF-32 stream bytes into an Unicode character |
2712 | | */ |
2713 | 0 | if( libuna_unicode_character_copy_from_utf32_stream( |
2714 | 0 | &unicode_character, |
2715 | 0 | utf32_stream, |
2716 | 0 | utf32_stream_size, |
2717 | 0 | &utf32_stream_index, |
2718 | 0 | byte_order, |
2719 | 0 | error ) != 1 ) |
2720 | 0 | { |
2721 | 0 | libcerror_error_set( |
2722 | 0 | error, |
2723 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2724 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
2725 | 0 | "%s: unable to copy Unicode character from UTF-32 stream.", |
2726 | 0 | function ); |
2727 | |
|
2728 | 0 | return( -1 ); |
2729 | 0 | } |
2730 | | /* Determine how many UTF-16 character bytes are required |
2731 | | */ |
2732 | 0 | if( libuna_unicode_character_size_to_utf16( |
2733 | 0 | unicode_character, |
2734 | 0 | utf16_string_size, |
2735 | 0 | error ) != 1 ) |
2736 | 0 | { |
2737 | 0 | libcerror_error_set( |
2738 | 0 | error, |
2739 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2740 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
2741 | 0 | "%s: unable to unable to determine size of Unicode character in UTF-16.", |
2742 | 0 | function ); |
2743 | |
|
2744 | 0 | return( -1 ); |
2745 | 0 | } |
2746 | 0 | if( unicode_character == 0 ) |
2747 | 0 | { |
2748 | 0 | break; |
2749 | 0 | } |
2750 | 0 | } |
2751 | | /* Check if the string is terminated with an end-of-string character |
2752 | | */ |
2753 | 0 | if( unicode_character != 0 ) |
2754 | 0 | { |
2755 | 0 | *utf16_string_size += 1; |
2756 | 0 | } |
2757 | 0 | return( 1 ); |
2758 | 0 | } |
2759 | | |
2760 | | /* Copies an UTF-16 string from an UTF-32 stream |
2761 | | * Returns 1 if successful or -1 on error |
2762 | | */ |
2763 | | int libuna_utf16_string_copy_from_utf32_stream( |
2764 | | libuna_utf16_character_t *utf16_string, |
2765 | | size_t utf16_string_size, |
2766 | | const uint8_t *utf32_stream, |
2767 | | size_t utf32_stream_size, |
2768 | | int byte_order, |
2769 | | libcerror_error_t **error ) |
2770 | 0 | { |
2771 | 0 | static char *function = "libuna_utf16_string_copy_from_utf32_stream"; |
2772 | 0 | size_t utf16_string_index = 0; |
2773 | |
|
2774 | 0 | if( libuna_utf16_string_with_index_copy_from_utf32_stream( |
2775 | 0 | utf16_string, |
2776 | 0 | utf16_string_size, |
2777 | 0 | &utf16_string_index, |
2778 | 0 | utf32_stream, |
2779 | 0 | utf32_stream_size, |
2780 | 0 | byte_order, |
2781 | 0 | error ) != 1 ) |
2782 | 0 | { |
2783 | 0 | libcerror_error_set( |
2784 | 0 | error, |
2785 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
2786 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
2787 | 0 | "%s: unable to copy UTF-32 stream to UTF-16 string.", |
2788 | 0 | function ); |
2789 | |
|
2790 | 0 | return( -1 ); |
2791 | 0 | } |
2792 | 0 | return( 1 ); |
2793 | 0 | } |
2794 | | |
2795 | | /* Copies an UTF-16 string from an UTF-32 stream |
2796 | | * Returns 1 if successful or -1 on error |
2797 | | */ |
2798 | | int libuna_utf16_string_with_index_copy_from_utf32_stream( |
2799 | | libuna_utf16_character_t *utf16_string, |
2800 | | size_t utf16_string_size, |
2801 | | size_t *utf16_string_index, |
2802 | | const uint8_t *utf32_stream, |
2803 | | size_t utf32_stream_size, |
2804 | | int byte_order, |
2805 | | libcerror_error_t **error ) |
2806 | 0 | { |
2807 | 0 | static char *function = "libuna_utf16_string_with_index_copy_from_utf32_stream"; |
2808 | 0 | size_t utf32_stream_index = 0; |
2809 | 0 | libuna_unicode_character_t unicode_character = 0; |
2810 | 0 | int read_byte_order = 0; |
2811 | |
|
2812 | 0 | if( utf16_string == NULL ) |
2813 | 0 | { |
2814 | 0 | libcerror_error_set( |
2815 | 0 | error, |
2816 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2817 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2818 | 0 | "%s: invalid UTF-16 string.", |
2819 | 0 | function ); |
2820 | |
|
2821 | 0 | return( -1 ); |
2822 | 0 | } |
2823 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
2824 | 0 | { |
2825 | 0 | libcerror_error_set( |
2826 | 0 | error, |
2827 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2828 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2829 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
2830 | 0 | function ); |
2831 | |
|
2832 | 0 | return( -1 ); |
2833 | 0 | } |
2834 | 0 | if( utf16_string_index == NULL ) |
2835 | 0 | { |
2836 | 0 | libcerror_error_set( |
2837 | 0 | error, |
2838 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2839 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2840 | 0 | "%s: invalid UTF-16 string index.", |
2841 | 0 | function ); |
2842 | |
|
2843 | 0 | return( -1 ); |
2844 | 0 | } |
2845 | 0 | if( utf32_stream == NULL ) |
2846 | 0 | { |
2847 | 0 | libcerror_error_set( |
2848 | 0 | error, |
2849 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2850 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
2851 | 0 | "%s: invalid UTF-32 stream.", |
2852 | 0 | function ); |
2853 | |
|
2854 | 0 | return( -1 ); |
2855 | 0 | } |
2856 | 0 | if( utf32_stream_size > (size_t) SSIZE_MAX ) |
2857 | 0 | { |
2858 | 0 | libcerror_error_set( |
2859 | 0 | error, |
2860 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2861 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
2862 | 0 | "%s: invalid UTF-32 stream size value exceeds maximum.", |
2863 | 0 | function ); |
2864 | |
|
2865 | 0 | return( -1 ); |
2866 | 0 | } |
2867 | 0 | if( ( utf32_stream_size == 0 ) |
2868 | 0 | || ( ( utf32_stream_size % 4 ) != 0 ) ) |
2869 | 0 | { |
2870 | 0 | libcerror_error_set( |
2871 | 0 | error, |
2872 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2873 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
2874 | 0 | "%s: missing UTF-32 stream value.", |
2875 | 0 | function ); |
2876 | |
|
2877 | 0 | return( -1 ); |
2878 | 0 | } |
2879 | | /* Check if UTF-32 stream is in big or little endian |
2880 | | */ |
2881 | 0 | if( utf32_stream_size >=4 ) |
2882 | 0 | { |
2883 | 0 | if( ( utf32_stream[ 0 ] == 0x00 ) |
2884 | 0 | && ( utf32_stream[ 1 ] == 0x00 ) |
2885 | 0 | && ( utf32_stream[ 2 ] == 0xfe ) |
2886 | 0 | && ( utf32_stream[ 3 ] == 0xff ) ) |
2887 | 0 | { |
2888 | 0 | read_byte_order = LIBUNA_ENDIAN_BIG; |
2889 | 0 | utf32_stream_index = 4; |
2890 | 0 | } |
2891 | 0 | else if( ( utf32_stream[ 0 ] == 0xff ) |
2892 | 0 | && ( utf32_stream[ 1 ] == 0xfe ) |
2893 | 0 | && ( utf32_stream[ 2 ] == 0x00 ) |
2894 | 0 | && ( utf32_stream[ 3 ] == 0x00 ) ) |
2895 | 0 | { |
2896 | 0 | read_byte_order = LIBUNA_ENDIAN_LITTLE; |
2897 | 0 | utf32_stream_index = 4; |
2898 | 0 | } |
2899 | 0 | if( byte_order == 0 ) |
2900 | 0 | { |
2901 | 0 | byte_order = read_byte_order; |
2902 | 0 | } |
2903 | 0 | } |
2904 | 0 | if( ( byte_order != LIBUNA_ENDIAN_BIG ) |
2905 | 0 | && ( byte_order != LIBUNA_ENDIAN_LITTLE ) ) |
2906 | 0 | { |
2907 | 0 | libcerror_error_set( |
2908 | 0 | error, |
2909 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2910 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
2911 | 0 | "%s: unsupported byte order.", |
2912 | 0 | function ); |
2913 | |
|
2914 | 0 | return( -1 ); |
2915 | 0 | } |
2916 | 0 | while( ( utf32_stream_index + 3 ) < utf32_stream_size ) |
2917 | 0 | { |
2918 | | /* Convert the UTF-32 stream bytes into an Unicode character |
2919 | | */ |
2920 | 0 | if( libuna_unicode_character_copy_from_utf32_stream( |
2921 | 0 | &unicode_character, |
2922 | 0 | utf32_stream, |
2923 | 0 | utf32_stream_size, |
2924 | 0 | &utf32_stream_index, |
2925 | 0 | byte_order, |
2926 | 0 | error ) != 1 ) |
2927 | 0 | { |
2928 | 0 | libcerror_error_set( |
2929 | 0 | error, |
2930 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2931 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
2932 | 0 | "%s: unable to copy Unicode character from UTF-32 stream.", |
2933 | 0 | function ); |
2934 | |
|
2935 | 0 | return( -1 ); |
2936 | 0 | } |
2937 | | /* Convert the Unicode character into UTF-16 character bytes |
2938 | | */ |
2939 | 0 | if( libuna_unicode_character_copy_to_utf16( |
2940 | 0 | unicode_character, |
2941 | 0 | utf16_string, |
2942 | 0 | utf16_string_size, |
2943 | 0 | utf16_string_index, |
2944 | 0 | error ) != 1 ) |
2945 | 0 | { |
2946 | 0 | libcerror_error_set( |
2947 | 0 | error, |
2948 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
2949 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
2950 | 0 | "%s: unable to copy Unicode character to UTF-16.", |
2951 | 0 | function ); |
2952 | |
|
2953 | 0 | return( -1 ); |
2954 | 0 | } |
2955 | 0 | if( unicode_character == 0 ) |
2956 | 0 | { |
2957 | 0 | break; |
2958 | 0 | } |
2959 | 0 | } |
2960 | | /* Check if the string is terminated with an end-of-string character |
2961 | | */ |
2962 | 0 | if( unicode_character != 0 ) |
2963 | 0 | { |
2964 | 0 | if( *utf16_string_index >= utf16_string_size ) |
2965 | 0 | { |
2966 | 0 | libcerror_error_set( |
2967 | 0 | error, |
2968 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
2969 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
2970 | 0 | "%s: UTF-16 string too small.", |
2971 | 0 | function ); |
2972 | |
|
2973 | 0 | return( -1 ); |
2974 | 0 | } |
2975 | 0 | utf16_string[ *utf16_string_index ] = 0; |
2976 | |
|
2977 | 0 | *utf16_string_index += 1; |
2978 | 0 | } |
2979 | 0 | return( 1 ); |
2980 | 0 | } |
2981 | | |
2982 | | /* Compares an UTF-16 string with an UTF-32 stream |
2983 | | * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error |
2984 | | */ |
2985 | | int libuna_utf16_string_compare_with_utf32_stream( |
2986 | | const libuna_utf16_character_t *utf16_string, |
2987 | | size_t utf16_string_size, |
2988 | | const uint8_t *utf32_stream, |
2989 | | size_t utf32_stream_size, |
2990 | | int byte_order, |
2991 | | libcerror_error_t **error ) |
2992 | 0 | { |
2993 | 0 | static char *function = "libuna_utf16_string_compare_with_utf32_stream"; |
2994 | 0 | size_t utf16_string_index = 0; |
2995 | 0 | size_t utf32_stream_index = 0; |
2996 | 0 | libuna_unicode_character_t utf16_unicode_character = 0; |
2997 | 0 | libuna_unicode_character_t utf32_stream_unicode_character = 0; |
2998 | 0 | int read_byte_order = 0; |
2999 | |
|
3000 | 0 | if( utf16_string == NULL ) |
3001 | 0 | { |
3002 | 0 | libcerror_error_set( |
3003 | 0 | error, |
3004 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3005 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
3006 | 0 | "%s: invalid UTF-16 string.", |
3007 | 0 | function ); |
3008 | |
|
3009 | 0 | return( -1 ); |
3010 | 0 | } |
3011 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
3012 | 0 | { |
3013 | 0 | libcerror_error_set( |
3014 | 0 | error, |
3015 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3016 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
3017 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
3018 | 0 | function ); |
3019 | |
|
3020 | 0 | return( -1 ); |
3021 | 0 | } |
3022 | 0 | if( utf32_stream == NULL ) |
3023 | 0 | { |
3024 | 0 | libcerror_error_set( |
3025 | 0 | error, |
3026 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3027 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
3028 | 0 | "%s: invalid UTF-32 stream.", |
3029 | 0 | function ); |
3030 | |
|
3031 | 0 | return( -1 ); |
3032 | 0 | } |
3033 | 0 | if( utf32_stream_size > (size_t) SSIZE_MAX ) |
3034 | 0 | { |
3035 | 0 | libcerror_error_set( |
3036 | 0 | error, |
3037 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3038 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
3039 | 0 | "%s: invalid UTF-32 stream size value exceeds maximum.", |
3040 | 0 | function ); |
3041 | |
|
3042 | 0 | return( -1 ); |
3043 | 0 | } |
3044 | 0 | if( ( utf32_stream_size == 0 ) |
3045 | 0 | || ( ( utf32_stream_size % 4 ) != 0 ) ) |
3046 | 0 | { |
3047 | 0 | libcerror_error_set( |
3048 | 0 | error, |
3049 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3050 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3051 | 0 | "%s: missing UTF-32 stream value.", |
3052 | 0 | function ); |
3053 | |
|
3054 | 0 | return( -1 ); |
3055 | 0 | } |
3056 | | /* Check if UTF-32 stream is in big or little endian |
3057 | | */ |
3058 | 0 | if( utf32_stream_size >= 4 ) |
3059 | 0 | { |
3060 | 0 | if( ( utf32_stream[ 0 ] == 0x00 ) |
3061 | 0 | && ( utf32_stream[ 1 ] == 0x00 ) |
3062 | 0 | && ( utf32_stream[ 2 ] == 0xfe ) |
3063 | 0 | && ( utf32_stream[ 3 ] == 0xff ) ) |
3064 | 0 | { |
3065 | 0 | read_byte_order = LIBUNA_ENDIAN_BIG; |
3066 | 0 | utf32_stream_index = 4; |
3067 | 0 | } |
3068 | 0 | else if( ( utf32_stream[ 0 ] == 0xff ) |
3069 | 0 | && ( utf32_stream[ 1 ] == 0xfe ) |
3070 | 0 | && ( utf32_stream[ 2 ] == 0x00 ) |
3071 | 0 | && ( utf32_stream[ 3 ] == 0x00 ) ) |
3072 | 0 | { |
3073 | 0 | read_byte_order = LIBUNA_ENDIAN_LITTLE; |
3074 | 0 | utf32_stream_index = 4; |
3075 | 0 | } |
3076 | 0 | if( byte_order == 0 ) |
3077 | 0 | { |
3078 | 0 | byte_order = read_byte_order; |
3079 | 0 | } |
3080 | 0 | } |
3081 | 0 | if( ( byte_order != LIBUNA_ENDIAN_BIG ) |
3082 | 0 | && ( byte_order != LIBUNA_ENDIAN_LITTLE ) ) |
3083 | 0 | { |
3084 | 0 | libcerror_error_set( |
3085 | 0 | error, |
3086 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3087 | 0 | LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE, |
3088 | 0 | "%s: unsupported byte order.", |
3089 | 0 | function ); |
3090 | |
|
3091 | 0 | return( -1 ); |
3092 | 0 | } |
3093 | 0 | if( ( utf16_string_size >= 1 ) |
3094 | 0 | && ( utf16_string[ utf16_string_size - 1 ] == 0 ) ) |
3095 | 0 | { |
3096 | 0 | utf16_string_size -= 1; |
3097 | 0 | } |
3098 | | /* Check if the UTF-32 stream is terminated with zero bytes |
3099 | | */ |
3100 | 0 | if( ( utf32_stream_size >= 4 ) |
3101 | 0 | && ( utf32_stream[ utf32_stream_size - 4 ] == 0 ) |
3102 | 0 | && ( utf32_stream[ utf32_stream_size - 3 ] == 0 ) |
3103 | 0 | && ( utf32_stream[ utf32_stream_size - 2 ] == 0 ) |
3104 | 0 | && ( utf32_stream[ utf32_stream_size - 1 ] == 0 ) ) |
3105 | 0 | { |
3106 | 0 | utf32_stream_size -= 1; |
3107 | 0 | } |
3108 | 0 | while( ( utf16_string_index < utf16_string_size ) |
3109 | 0 | && ( utf32_stream_index < utf32_stream_size ) ) |
3110 | 0 | { |
3111 | | /* Convert the UTF-16 character bytes into an Unicode character |
3112 | | */ |
3113 | 0 | if( libuna_unicode_character_copy_from_utf16( |
3114 | 0 | &utf16_unicode_character, |
3115 | 0 | utf16_string, |
3116 | 0 | utf16_string_size, |
3117 | 0 | &utf16_string_index, |
3118 | 0 | error ) != 1 ) |
3119 | 0 | { |
3120 | 0 | libcerror_error_set( |
3121 | 0 | error, |
3122 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
3123 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
3124 | 0 | "%s: unable to copy Unicode character from UTF-16.", |
3125 | 0 | function ); |
3126 | |
|
3127 | 0 | return( -1 ); |
3128 | 0 | } |
3129 | | /* Convert the UTF-32 stream bytes into an Unicode character |
3130 | | */ |
3131 | 0 | if( libuna_unicode_character_copy_from_utf32_stream( |
3132 | 0 | &utf32_stream_unicode_character, |
3133 | 0 | utf32_stream, |
3134 | 0 | utf32_stream_size, |
3135 | 0 | &utf32_stream_index, |
3136 | 0 | byte_order, |
3137 | 0 | error ) != 1 ) |
3138 | 0 | { |
3139 | 0 | libcerror_error_set( |
3140 | 0 | error, |
3141 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
3142 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
3143 | 0 | "%s: unable to copy Unicode character from UTF-32 stream.", |
3144 | 0 | function ); |
3145 | |
|
3146 | 0 | return( -1 ); |
3147 | 0 | } |
3148 | 0 | if( utf16_unicode_character < utf32_stream_unicode_character ) |
3149 | 0 | { |
3150 | 0 | return( LIBUNA_COMPARE_LESS ); |
3151 | 0 | } |
3152 | 0 | else if( utf16_unicode_character > utf32_stream_unicode_character ) |
3153 | 0 | { |
3154 | 0 | return( LIBUNA_COMPARE_GREATER ); |
3155 | 0 | } |
3156 | 0 | } |
3157 | | /* Check if both strings were entirely processed |
3158 | | */ |
3159 | 0 | if( utf16_string_index < utf16_string_size ) |
3160 | 0 | { |
3161 | 0 | return( LIBUNA_COMPARE_GREATER ); |
3162 | 0 | } |
3163 | 0 | else if( utf32_stream_index < utf32_stream_size ) |
3164 | 0 | { |
3165 | 0 | return( LIBUNA_COMPARE_LESS ); |
3166 | 0 | } |
3167 | 0 | return( LIBUNA_COMPARE_EQUAL ); |
3168 | 0 | } |
3169 | | |
3170 | | /* Determines the size of an UTF-16 string from a Standard Compression Scheme for Unicode (SCSU) stream |
3171 | | * Returns 1 if successful or -1 on error |
3172 | | */ |
3173 | | int libuna_utf16_string_size_from_scsu_stream( |
3174 | | const uint8_t *scsu_stream, |
3175 | | size_t scsu_stream_size, |
3176 | | size_t *utf16_string_size, |
3177 | | libcerror_error_t **error ) |
3178 | 0 | { |
3179 | 0 | uint32_t scsu_dynamic_window_positions[ 8 ] = { |
3180 | 0 | 0x0080, 0x00c0, 0x0400, 0x0600, 0x0900, 0x3040, 0x30a0, 0xff00 }; |
3181 | |
|
3182 | 0 | static char *function = "libuna_utf8_string_size_from_scsu_stream"; |
3183 | 0 | libuna_unicode_character_t unicode_character = 0; |
3184 | 0 | size_t scsu_stream_index = 0; |
3185 | 0 | uint32_t scsu_window_position = 0; |
3186 | 0 | uint8_t byte_value1 = 0; |
3187 | 0 | uint8_t byte_value2 = 0; |
3188 | 0 | uint8_t byte_value3 = 0; |
3189 | 0 | uint8_t dynamic_window_position_index = 0; |
3190 | 0 | uint8_t in_unicode_mode = 0; |
3191 | 0 | uint8_t unicode_character_set = 0; |
3192 | |
|
3193 | 0 | if( scsu_stream == NULL ) |
3194 | 0 | { |
3195 | 0 | libcerror_error_set( |
3196 | 0 | error, |
3197 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3198 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
3199 | 0 | "%s: invalid SCSU stream.", |
3200 | 0 | function ); |
3201 | |
|
3202 | 0 | return( -1 ); |
3203 | 0 | } |
3204 | 0 | if( scsu_stream_size > (size_t) SSIZE_MAX ) |
3205 | 0 | { |
3206 | 0 | libcerror_error_set( |
3207 | 0 | error, |
3208 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3209 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
3210 | 0 | "%s: invalid SCSU stream size value exceeds maximum.", |
3211 | 0 | function ); |
3212 | |
|
3213 | 0 | return( -1 ); |
3214 | 0 | } |
3215 | 0 | if( utf16_string_size == NULL ) |
3216 | 0 | { |
3217 | 0 | libcerror_error_set( |
3218 | 0 | error, |
3219 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3220 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
3221 | 0 | "%s: invalid UTF-16 string size.", |
3222 | 0 | function ); |
3223 | |
|
3224 | 0 | return( -1 ); |
3225 | 0 | } |
3226 | 0 | *utf16_string_size = 0; |
3227 | |
|
3228 | 0 | if( scsu_stream_size == 0 ) |
3229 | 0 | { |
3230 | 0 | return( 1 ); |
3231 | 0 | } |
3232 | 0 | while( scsu_stream_index < scsu_stream_size ) |
3233 | 0 | { |
3234 | 0 | unicode_character_set = 0; |
3235 | |
|
3236 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3237 | 0 | { |
3238 | 0 | libcerror_error_set( |
3239 | 0 | error, |
3240 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3241 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3242 | 0 | "%s: SCSU stream too small.", |
3243 | 0 | function ); |
3244 | |
|
3245 | 0 | return( -1 ); |
3246 | 0 | } |
3247 | 0 | byte_value1 = scsu_stream[ scsu_stream_index++ ]; |
3248 | |
|
3249 | 0 | if( in_unicode_mode != 0 ) |
3250 | 0 | { |
3251 | 0 | if( ( byte_value1 <= 0xdf ) |
3252 | 0 | || ( byte_value1 >= 0xf3 ) ) |
3253 | 0 | { |
3254 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3255 | 0 | { |
3256 | 0 | libcerror_error_set( |
3257 | 0 | error, |
3258 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3259 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3260 | 0 | "%s: SCSU stream too small.", |
3261 | 0 | function ); |
3262 | |
|
3263 | 0 | return( -1 ); |
3264 | 0 | } |
3265 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3266 | |
|
3267 | 0 | unicode_character = byte_value1; |
3268 | 0 | unicode_character <<= 8; |
3269 | 0 | unicode_character |= byte_value2; |
3270 | |
|
3271 | 0 | unicode_character_set = 1; |
3272 | 0 | } |
3273 | | /* UCn tags |
3274 | | */ |
3275 | 0 | else if( ( byte_value1 >= 0xe0 ) |
3276 | 0 | && ( byte_value1 <= 0xe7 ) ) |
3277 | 0 | { |
3278 | 0 | dynamic_window_position_index = byte_value1 - 0xe0; |
3279 | |
|
3280 | 0 | in_unicode_mode = 0; |
3281 | 0 | } |
3282 | | /* UDn tags |
3283 | | */ |
3284 | 0 | else if( ( byte_value1 >= 0xe8 ) |
3285 | 0 | && ( byte_value1 <= 0xef ) ) |
3286 | 0 | { |
3287 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3288 | 0 | { |
3289 | 0 | libcerror_error_set( |
3290 | 0 | error, |
3291 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3292 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3293 | 0 | "%s: SCSU stream too small.", |
3294 | 0 | function ); |
3295 | |
|
3296 | 0 | return( -1 ); |
3297 | 0 | } |
3298 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3299 | |
|
3300 | 0 | dynamic_window_position_index = byte_value1 - 0xe8; |
3301 | 0 | scsu_window_position = libuna_scsu_window_offset_table[ byte_value2 ]; |
3302 | |
|
3303 | 0 | scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position; |
3304 | |
|
3305 | 0 | in_unicode_mode = 0; |
3306 | 0 | } |
3307 | | /* UQU tag |
3308 | | */ |
3309 | 0 | else if( byte_value1 == 0xf0 ) |
3310 | 0 | { |
3311 | 0 | if( ( scsu_stream_size < 2 ) |
3312 | 0 | || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) ) |
3313 | 0 | { |
3314 | 0 | libcerror_error_set( |
3315 | 0 | error, |
3316 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3317 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3318 | 0 | "%s: SCSU stream too small.", |
3319 | 0 | function ); |
3320 | |
|
3321 | 0 | return( -1 ); |
3322 | 0 | } |
3323 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3324 | 0 | byte_value3 = scsu_stream[ scsu_stream_index++ ]; |
3325 | |
|
3326 | 0 | unicode_character = byte_value2; |
3327 | 0 | unicode_character <<= 8; |
3328 | 0 | unicode_character |= byte_value3; |
3329 | |
|
3330 | 0 | unicode_character_set = 1; |
3331 | 0 | } |
3332 | | /* UDX tag |
3333 | | */ |
3334 | 0 | else if( byte_value1 == 0xf1 ) |
3335 | 0 | { |
3336 | 0 | if( ( scsu_stream_size < 2 ) |
3337 | 0 | || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) ) |
3338 | 0 | { |
3339 | 0 | libcerror_error_set( |
3340 | 0 | error, |
3341 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3342 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3343 | 0 | "%s: SCSU stream too small.", |
3344 | 0 | function ); |
3345 | |
|
3346 | 0 | return( -1 ); |
3347 | 0 | } |
3348 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3349 | 0 | byte_value3 = scsu_stream[ scsu_stream_index++ ]; |
3350 | |
|
3351 | 0 | dynamic_window_position_index = byte_value2 >> 5; |
3352 | 0 | scsu_window_position = byte_value2 & 0x1f; |
3353 | 0 | scsu_window_position <<= 8; |
3354 | 0 | scsu_window_position |= byte_value3; |
3355 | 0 | scsu_window_position <<= 7; |
3356 | 0 | scsu_window_position += 0x00010000UL; |
3357 | |
|
3358 | 0 | scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position; |
3359 | |
|
3360 | 0 | in_unicode_mode = 0; |
3361 | 0 | } |
3362 | 0 | } |
3363 | 0 | else |
3364 | 0 | { |
3365 | 0 | if( ( byte_value1 == 0x00 ) |
3366 | 0 | || ( byte_value1 == 0x09 ) |
3367 | 0 | || ( byte_value1 == 0x0a ) |
3368 | 0 | || ( byte_value1 == 0x0c ) |
3369 | 0 | || ( byte_value1 == 0x0d ) |
3370 | 0 | || ( ( byte_value1 >= 0x20 ) |
3371 | 0 | && ( byte_value1 <= 0x7f ) ) ) |
3372 | 0 | { |
3373 | 0 | unicode_character = byte_value1; |
3374 | |
|
3375 | 0 | unicode_character_set = 1; |
3376 | 0 | } |
3377 | | /* SQn tags |
3378 | | */ |
3379 | 0 | else if( ( byte_value1 >= 0x01 ) |
3380 | 0 | && ( byte_value1 <= 0x08 ) ) |
3381 | 0 | { |
3382 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3383 | 0 | { |
3384 | 0 | libcerror_error_set( |
3385 | 0 | error, |
3386 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3387 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3388 | 0 | "%s: SCSU stream too small.", |
3389 | 0 | function ); |
3390 | |
|
3391 | 0 | return( -1 ); |
3392 | 0 | } |
3393 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3394 | |
|
3395 | 0 | unicode_character = byte_value2; |
3396 | |
|
3397 | 0 | if( byte_value2 < 0x80 ) |
3398 | 0 | { |
3399 | 0 | unicode_character += libuna_scsu_static_window_positions[ byte_value1 - 0x01 ]; |
3400 | 0 | } |
3401 | 0 | else |
3402 | 0 | { |
3403 | 0 | unicode_character -= 0x80; |
3404 | 0 | unicode_character += scsu_dynamic_window_positions[ byte_value1 - 0x01 ]; |
3405 | 0 | } |
3406 | 0 | unicode_character_set = 1; |
3407 | 0 | } |
3408 | | /* SDX tag |
3409 | | */ |
3410 | 0 | else if( byte_value1 == 0x0b ) |
3411 | 0 | { |
3412 | 0 | if( ( scsu_stream_size < 2 ) |
3413 | 0 | || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) ) |
3414 | 0 | { |
3415 | 0 | libcerror_error_set( |
3416 | 0 | error, |
3417 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3418 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3419 | 0 | "%s: SCSU stream too small.", |
3420 | 0 | function ); |
3421 | |
|
3422 | 0 | return( -1 ); |
3423 | 0 | } |
3424 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3425 | 0 | byte_value3 = scsu_stream[ scsu_stream_index++ ]; |
3426 | |
|
3427 | 0 | dynamic_window_position_index = byte_value2 >> 5; |
3428 | 0 | scsu_window_position = byte_value2 & 0x1f; |
3429 | 0 | scsu_window_position <<= 8; |
3430 | 0 | scsu_window_position |= byte_value3; |
3431 | 0 | scsu_window_position <<= 7; |
3432 | 0 | scsu_window_position += 0x00010000UL; |
3433 | |
|
3434 | 0 | scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position; |
3435 | 0 | } |
3436 | | /* SQU tag |
3437 | | */ |
3438 | 0 | else if( byte_value1 == 0x0e ) |
3439 | 0 | { |
3440 | 0 | if( ( scsu_stream_size < 2 ) |
3441 | 0 | || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) ) |
3442 | 0 | { |
3443 | 0 | libcerror_error_set( |
3444 | 0 | error, |
3445 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3446 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3447 | 0 | "%s: SCSU stream too small.", |
3448 | 0 | function ); |
3449 | |
|
3450 | 0 | return( -1 ); |
3451 | 0 | } |
3452 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3453 | 0 | byte_value3 = scsu_stream[ scsu_stream_index++ ]; |
3454 | |
|
3455 | 0 | unicode_character = byte_value2; |
3456 | 0 | unicode_character <<= 8; |
3457 | 0 | unicode_character |= byte_value3; |
3458 | |
|
3459 | 0 | unicode_character_set = 1; |
3460 | 0 | } |
3461 | | /* SCU tag |
3462 | | */ |
3463 | 0 | else if( byte_value1 == 0x0f ) |
3464 | 0 | { |
3465 | 0 | in_unicode_mode = 1; |
3466 | 0 | } |
3467 | | /* SCn tags |
3468 | | */ |
3469 | 0 | else if( ( byte_value1 >= 0x10 ) |
3470 | 0 | && ( byte_value1 <= 0x17 ) ) |
3471 | 0 | { |
3472 | 0 | dynamic_window_position_index = byte_value1 - 0x10; |
3473 | 0 | } |
3474 | | /* SDn tags |
3475 | | */ |
3476 | 0 | else if( ( byte_value1 >= 0x18 ) |
3477 | 0 | && ( byte_value1 <= 0x1f ) ) |
3478 | 0 | { |
3479 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3480 | 0 | { |
3481 | 0 | libcerror_error_set( |
3482 | 0 | error, |
3483 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3484 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3485 | 0 | "%s: SCSU stream too small.", |
3486 | 0 | function ); |
3487 | |
|
3488 | 0 | return( -1 ); |
3489 | 0 | } |
3490 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3491 | |
|
3492 | 0 | dynamic_window_position_index = byte_value1 - 0x18; |
3493 | 0 | scsu_window_position = libuna_scsu_window_offset_table[ byte_value2 ]; |
3494 | |
|
3495 | 0 | scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position; |
3496 | 0 | } |
3497 | 0 | else if( byte_value1 >= 0x80 ) |
3498 | 0 | { |
3499 | 0 | unicode_character = byte_value1 - 0x80; |
3500 | 0 | unicode_character += scsu_dynamic_window_positions[ dynamic_window_position_index ]; |
3501 | |
|
3502 | 0 | unicode_character_set = 1; |
3503 | 0 | } |
3504 | 0 | } |
3505 | 0 | if( unicode_character_set != 0 ) |
3506 | 0 | { |
3507 | | /* Determine how many UTF-16 character bytes are required |
3508 | | */ |
3509 | 0 | if( libuna_unicode_character_size_to_utf16( |
3510 | 0 | unicode_character, |
3511 | 0 | utf16_string_size, |
3512 | 0 | error ) != 1 ) |
3513 | 0 | { |
3514 | 0 | libcerror_error_set( |
3515 | 0 | error, |
3516 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
3517 | 0 | LIBCERROR_CONVERSION_ERROR_INPUT_FAILED, |
3518 | 0 | "%s: unable to unable to determine size of Unicode character in UTF-16.", |
3519 | 0 | function ); |
3520 | |
|
3521 | 0 | return( -1 ); |
3522 | 0 | } |
3523 | 0 | if( unicode_character == 0 ) |
3524 | 0 | { |
3525 | 0 | break; |
3526 | 0 | } |
3527 | 0 | } |
3528 | 0 | } |
3529 | | /* Check if the string is terminated with an end-of-string character |
3530 | | */ |
3531 | 0 | if( unicode_character != 0 ) |
3532 | 0 | { |
3533 | 0 | *utf16_string_size += 1; |
3534 | 0 | } |
3535 | 0 | return( 1 ); |
3536 | 0 | } |
3537 | | |
3538 | | /* Copies an UTF-16 string from a Standard Compression Scheme for Unicode (SCSU) stream |
3539 | | * Returns 1 if successful or -1 on error |
3540 | | */ |
3541 | | int libuna_utf16_string_copy_from_scsu_stream( |
3542 | | libuna_utf16_character_t *utf16_string, |
3543 | | size_t utf16_string_size, |
3544 | | const uint8_t *scsu_stream, |
3545 | | size_t scsu_stream_size, |
3546 | | libcerror_error_t **error ) |
3547 | 0 | { |
3548 | 0 | static char *function = "libuna_utf16_string_copy_from_scsu_stream"; |
3549 | 0 | size_t utf16_string_index = 0; |
3550 | |
|
3551 | 0 | if( libuna_utf16_string_with_index_copy_from_scsu_stream( |
3552 | 0 | utf16_string, |
3553 | 0 | utf16_string_size, |
3554 | 0 | &utf16_string_index, |
3555 | 0 | scsu_stream, |
3556 | 0 | scsu_stream_size, |
3557 | 0 | error ) != 1 ) |
3558 | 0 | { |
3559 | 0 | libcerror_error_set( |
3560 | 0 | error, |
3561 | 0 | LIBCERROR_ERROR_DOMAIN_RUNTIME, |
3562 | 0 | LIBCERROR_RUNTIME_ERROR_COPY_FAILED, |
3563 | 0 | "%s: unable to SCSU stream to UTF-16 string.", |
3564 | 0 | function ); |
3565 | |
|
3566 | 0 | return( -1 ); |
3567 | 0 | } |
3568 | 0 | return( 1 ); |
3569 | 0 | } |
3570 | | |
3571 | | /* Copies an UTF-16 string from a Standard Compression Scheme for Unicode (SCSU) stream |
3572 | | * Returns 1 if successful or -1 on error |
3573 | | */ |
3574 | | int libuna_utf16_string_with_index_copy_from_scsu_stream( |
3575 | | libuna_utf16_character_t *utf16_string, |
3576 | | size_t utf16_string_size, |
3577 | | size_t *utf16_string_index, |
3578 | | const uint8_t *scsu_stream, |
3579 | | size_t scsu_stream_size, |
3580 | | libcerror_error_t **error ) |
3581 | 0 | { |
3582 | 0 | uint32_t scsu_dynamic_window_positions[ 8 ] = { |
3583 | 0 | 0x0080, 0x00c0, 0x0400, 0x0600, 0x0900, 0x3040, 0x30a0, 0xff00 }; |
3584 | |
|
3585 | 0 | static char *function = "libuna_utf16_string_with_index_copy_from_scsu_stream"; |
3586 | 0 | libuna_unicode_character_t unicode_character = 0; |
3587 | 0 | size_t scsu_stream_index = 0; |
3588 | 0 | uint32_t scsu_window_position = 0; |
3589 | 0 | uint8_t byte_value1 = 0; |
3590 | 0 | uint8_t byte_value2 = 0; |
3591 | 0 | uint8_t byte_value3 = 0; |
3592 | 0 | uint8_t dynamic_window_position_index = 0; |
3593 | 0 | uint8_t in_unicode_mode = 0; |
3594 | 0 | uint8_t unicode_character_set = 0; |
3595 | |
|
3596 | 0 | if( utf16_string == NULL ) |
3597 | 0 | { |
3598 | 0 | libcerror_error_set( |
3599 | 0 | error, |
3600 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3601 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
3602 | 0 | "%s: invalid UTF-16 string.", |
3603 | 0 | function ); |
3604 | |
|
3605 | 0 | return( -1 ); |
3606 | 0 | } |
3607 | 0 | if( utf16_string_size > (size_t) SSIZE_MAX ) |
3608 | 0 | { |
3609 | 0 | libcerror_error_set( |
3610 | 0 | error, |
3611 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3612 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
3613 | 0 | "%s: invalid UTF-16 string size value exceeds maximum.", |
3614 | 0 | function ); |
3615 | |
|
3616 | 0 | return( -1 ); |
3617 | 0 | } |
3618 | 0 | if( utf16_string_index == NULL ) |
3619 | 0 | { |
3620 | 0 | libcerror_error_set( |
3621 | 0 | error, |
3622 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3623 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
3624 | 0 | "%s: invalid UTF-16 string index.", |
3625 | 0 | function ); |
3626 | |
|
3627 | 0 | return( -1 ); |
3628 | 0 | } |
3629 | 0 | if( scsu_stream == NULL ) |
3630 | 0 | { |
3631 | 0 | libcerror_error_set( |
3632 | 0 | error, |
3633 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3634 | 0 | LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE, |
3635 | 0 | "%s: invalid SCSU stream.", |
3636 | 0 | function ); |
3637 | |
|
3638 | 0 | return( -1 ); |
3639 | 0 | } |
3640 | 0 | if( scsu_stream_size > (size_t) SSIZE_MAX ) |
3641 | 0 | { |
3642 | 0 | libcerror_error_set( |
3643 | 0 | error, |
3644 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3645 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM, |
3646 | 0 | "%s: invalid SCSU stream size value exceeds maximum.", |
3647 | 0 | function ); |
3648 | |
|
3649 | 0 | return( -1 ); |
3650 | 0 | } |
3651 | 0 | if( scsu_stream_size == 0 ) |
3652 | 0 | { |
3653 | 0 | libcerror_error_set( |
3654 | 0 | error, |
3655 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3656 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_ZERO_OR_LESS, |
3657 | 0 | "%s: missing SCSU stream value.", |
3658 | 0 | function ); |
3659 | |
|
3660 | 0 | return( -1 ); |
3661 | 0 | } |
3662 | 0 | while( scsu_stream_index < scsu_stream_size ) |
3663 | 0 | { |
3664 | 0 | unicode_character_set = 0; |
3665 | |
|
3666 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3667 | 0 | { |
3668 | 0 | libcerror_error_set( |
3669 | 0 | error, |
3670 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3671 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3672 | 0 | "%s: SCSU stream too small.", |
3673 | 0 | function ); |
3674 | |
|
3675 | 0 | return( -1 ); |
3676 | 0 | } |
3677 | 0 | byte_value1 = scsu_stream[ scsu_stream_index++ ]; |
3678 | |
|
3679 | 0 | if( in_unicode_mode != 0 ) |
3680 | 0 | { |
3681 | 0 | if( ( byte_value1 <= 0xdf ) |
3682 | 0 | || ( byte_value1 >= 0xf3 ) ) |
3683 | 0 | { |
3684 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3685 | 0 | { |
3686 | 0 | libcerror_error_set( |
3687 | 0 | error, |
3688 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3689 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3690 | 0 | "%s: SCSU stream too small.", |
3691 | 0 | function ); |
3692 | |
|
3693 | 0 | return( -1 ); |
3694 | 0 | } |
3695 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3696 | |
|
3697 | 0 | unicode_character = byte_value1; |
3698 | 0 | unicode_character <<= 8; |
3699 | 0 | unicode_character |= byte_value2; |
3700 | |
|
3701 | 0 | unicode_character_set = 1; |
3702 | 0 | } |
3703 | | /* UCn tags |
3704 | | */ |
3705 | 0 | else if( ( byte_value1 >= 0xe0 ) |
3706 | 0 | && ( byte_value1 <= 0xe7 ) ) |
3707 | 0 | { |
3708 | 0 | dynamic_window_position_index = byte_value1 - 0xe0; |
3709 | |
|
3710 | 0 | in_unicode_mode = 0; |
3711 | 0 | } |
3712 | | /* UDn tags |
3713 | | */ |
3714 | 0 | else if( ( byte_value1 >= 0xe8 ) |
3715 | 0 | && ( byte_value1 <= 0xef ) ) |
3716 | 0 | { |
3717 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3718 | 0 | { |
3719 | 0 | libcerror_error_set( |
3720 | 0 | error, |
3721 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3722 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3723 | 0 | "%s: SCSU stream too small.", |
3724 | 0 | function ); |
3725 | |
|
3726 | 0 | return( -1 ); |
3727 | 0 | } |
3728 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3729 | |
|
3730 | 0 | dynamic_window_position_index = byte_value1 - 0xe8; |
3731 | 0 | scsu_window_position = libuna_scsu_window_offset_table[ byte_value2 ]; |
3732 | |
|
3733 | 0 | scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position; |
3734 | |
|
3735 | 0 | in_unicode_mode = 0; |
3736 | 0 | } |
3737 | | /* UQU tag |
3738 | | */ |
3739 | 0 | else if( byte_value1 == 0xf0 ) |
3740 | 0 | { |
3741 | 0 | if( ( scsu_stream_size < 2 ) |
3742 | 0 | || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) ) |
3743 | 0 | { |
3744 | 0 | libcerror_error_set( |
3745 | 0 | error, |
3746 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3747 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3748 | 0 | "%s: SCSU stream too small.", |
3749 | 0 | function ); |
3750 | |
|
3751 | 0 | return( -1 ); |
3752 | 0 | } |
3753 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3754 | 0 | byte_value3 = scsu_stream[ scsu_stream_index++ ]; |
3755 | |
|
3756 | 0 | unicode_character = byte_value2; |
3757 | 0 | unicode_character <<= 8; |
3758 | 0 | unicode_character |= byte_value3; |
3759 | |
|
3760 | 0 | unicode_character_set = 1; |
3761 | 0 | } |
3762 | | /* UDX tag |
3763 | | */ |
3764 | 0 | else if( byte_value1 == 0xf1 ) |
3765 | 0 | { |
3766 | 0 | if( ( scsu_stream_size < 2 ) |
3767 | 0 | || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) ) |
3768 | 0 | { |
3769 | 0 | libcerror_error_set( |
3770 | 0 | error, |
3771 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3772 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3773 | 0 | "%s: SCSU stream too small.", |
3774 | 0 | function ); |
3775 | |
|
3776 | 0 | return( -1 ); |
3777 | 0 | } |
3778 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3779 | 0 | byte_value3 = scsu_stream[ scsu_stream_index++ ]; |
3780 | |
|
3781 | 0 | dynamic_window_position_index = byte_value2 >> 5; |
3782 | 0 | scsu_window_position = byte_value2 & 0x1f; |
3783 | 0 | scsu_window_position <<= 8; |
3784 | 0 | scsu_window_position |= byte_value3; |
3785 | 0 | scsu_window_position <<= 7; |
3786 | 0 | scsu_window_position += 0x00010000UL; |
3787 | |
|
3788 | 0 | scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position; |
3789 | |
|
3790 | 0 | in_unicode_mode = 0; |
3791 | 0 | } |
3792 | 0 | } |
3793 | 0 | else |
3794 | 0 | { |
3795 | 0 | if( ( byte_value1 == 0x00 ) |
3796 | 0 | || ( byte_value1 == 0x09 ) |
3797 | 0 | || ( byte_value1 == 0x0a ) |
3798 | 0 | || ( byte_value1 == 0x0c ) |
3799 | 0 | || ( byte_value1 == 0x0d ) |
3800 | 0 | || ( ( byte_value1 >= 0x20 ) |
3801 | 0 | && ( byte_value1 <= 0x7f ) ) ) |
3802 | 0 | { |
3803 | 0 | unicode_character = byte_value1; |
3804 | |
|
3805 | 0 | unicode_character_set = 1; |
3806 | 0 | } |
3807 | | /* SQn tags |
3808 | | */ |
3809 | 0 | else if( ( byte_value1 >= 0x01 ) |
3810 | 0 | && ( byte_value1 <= 0x08 ) ) |
3811 | 0 | { |
3812 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3813 | 0 | { |
3814 | 0 | libcerror_error_set( |
3815 | 0 | error, |
3816 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3817 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3818 | 0 | "%s: SCSU stream too small.", |
3819 | 0 | function ); |
3820 | |
|
3821 | 0 | return( -1 ); |
3822 | 0 | } |
3823 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3824 | |
|
3825 | 0 | unicode_character = byte_value2; |
3826 | |
|
3827 | 0 | if( byte_value2 < 0x80 ) |
3828 | 0 | { |
3829 | 0 | unicode_character += libuna_scsu_static_window_positions[ byte_value1 - 0x01 ]; |
3830 | 0 | } |
3831 | 0 | else |
3832 | 0 | { |
3833 | 0 | unicode_character -= 0x80; |
3834 | 0 | unicode_character += scsu_dynamic_window_positions[ byte_value1 - 0x01 ]; |
3835 | 0 | } |
3836 | 0 | unicode_character_set = 1; |
3837 | 0 | } |
3838 | | /* SDX tag |
3839 | | */ |
3840 | 0 | else if( byte_value1 == 0x0b ) |
3841 | 0 | { |
3842 | 0 | if( ( scsu_stream_size < 2 ) |
3843 | 0 | || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) ) |
3844 | 0 | { |
3845 | 0 | libcerror_error_set( |
3846 | 0 | error, |
3847 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3848 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3849 | 0 | "%s: SCSU stream too small.", |
3850 | 0 | function ); |
3851 | |
|
3852 | 0 | return( -1 ); |
3853 | 0 | } |
3854 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3855 | 0 | byte_value3 = scsu_stream[ scsu_stream_index++ ]; |
3856 | |
|
3857 | 0 | dynamic_window_position_index = byte_value2 >> 5; |
3858 | 0 | scsu_window_position = byte_value2 & 0x1f; |
3859 | 0 | scsu_window_position <<= 8; |
3860 | 0 | scsu_window_position |= byte_value3; |
3861 | 0 | scsu_window_position <<= 7; |
3862 | 0 | scsu_window_position += 0x00010000UL; |
3863 | |
|
3864 | 0 | scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position; |
3865 | 0 | } |
3866 | | /* SQU tag |
3867 | | */ |
3868 | 0 | else if( byte_value1 == 0x0e ) |
3869 | 0 | { |
3870 | 0 | if( ( scsu_stream_size < 2 ) |
3871 | 0 | || ( scsu_stream_index > ( scsu_stream_size - 2 ) ) ) |
3872 | 0 | { |
3873 | 0 | libcerror_error_set( |
3874 | 0 | error, |
3875 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3876 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3877 | 0 | "%s: SCSU stream too small.", |
3878 | 0 | function ); |
3879 | |
|
3880 | 0 | return( -1 ); |
3881 | 0 | } |
3882 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3883 | 0 | byte_value3 = scsu_stream[ scsu_stream_index++ ]; |
3884 | |
|
3885 | 0 | unicode_character = byte_value2; |
3886 | 0 | unicode_character <<= 8; |
3887 | 0 | unicode_character |= byte_value3; |
3888 | |
|
3889 | 0 | unicode_character_set = 1; |
3890 | 0 | } |
3891 | | /* SCU tag |
3892 | | */ |
3893 | 0 | else if( byte_value1 == 0x0f ) |
3894 | 0 | { |
3895 | 0 | in_unicode_mode = 1; |
3896 | 0 | } |
3897 | | /* SCn tags |
3898 | | */ |
3899 | 0 | else if( ( byte_value1 >= 0x10 ) |
3900 | 0 | && ( byte_value1 <= 0x17 ) ) |
3901 | 0 | { |
3902 | 0 | dynamic_window_position_index = byte_value1 - 0x10; |
3903 | 0 | } |
3904 | | /* SDn tags |
3905 | | */ |
3906 | 0 | else if( ( byte_value1 >= 0x18 ) |
3907 | 0 | && ( byte_value1 <= 0x1f ) ) |
3908 | 0 | { |
3909 | 0 | if( scsu_stream_index >= scsu_stream_size ) |
3910 | 0 | { |
3911 | 0 | libcerror_error_set( |
3912 | 0 | error, |
3913 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3914 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3915 | 0 | "%s: SCSU stream too small.", |
3916 | 0 | function ); |
3917 | |
|
3918 | 0 | return( -1 ); |
3919 | 0 | } |
3920 | 0 | byte_value2 = scsu_stream[ scsu_stream_index++ ]; |
3921 | |
|
3922 | 0 | dynamic_window_position_index = byte_value1 - 0x18; |
3923 | 0 | scsu_window_position = libuna_scsu_window_offset_table[ byte_value2 ]; |
3924 | |
|
3925 | 0 | scsu_dynamic_window_positions[ dynamic_window_position_index ] = scsu_window_position; |
3926 | 0 | } |
3927 | 0 | else if( byte_value1 >= 0x80 ) |
3928 | 0 | { |
3929 | 0 | unicode_character = byte_value1 - 0x80; |
3930 | 0 | unicode_character += scsu_dynamic_window_positions[ dynamic_window_position_index ]; |
3931 | |
|
3932 | 0 | unicode_character_set = 1; |
3933 | 0 | } |
3934 | 0 | } |
3935 | 0 | if( unicode_character_set != 0 ) |
3936 | 0 | { |
3937 | | /* Convert the Unicode character into UTF-16 character bytes |
3938 | | */ |
3939 | 0 | if( libuna_unicode_character_copy_to_utf16( |
3940 | 0 | unicode_character, |
3941 | 0 | utf16_string, |
3942 | 0 | utf16_string_size, |
3943 | 0 | utf16_string_index, |
3944 | 0 | error ) != 1 ) |
3945 | 0 | { |
3946 | 0 | libcerror_error_set( |
3947 | 0 | error, |
3948 | 0 | LIBCERROR_ERROR_DOMAIN_CONVERSION, |
3949 | 0 | LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED, |
3950 | 0 | "%s: unable to copy Unicode character to UTF-16.", |
3951 | 0 | function ); |
3952 | |
|
3953 | 0 | return( -1 ); |
3954 | 0 | } |
3955 | 0 | if( unicode_character == 0 ) |
3956 | 0 | { |
3957 | 0 | break; |
3958 | 0 | } |
3959 | 0 | } |
3960 | 0 | } |
3961 | | /* Check if the string is terminated with an end-of-string character |
3962 | | */ |
3963 | 0 | if( unicode_character != 0 ) |
3964 | 0 | { |
3965 | 0 | if( *utf16_string_index >= utf16_string_size ) |
3966 | 0 | { |
3967 | 0 | libcerror_error_set( |
3968 | 0 | error, |
3969 | 0 | LIBCERROR_ERROR_DOMAIN_ARGUMENTS, |
3970 | 0 | LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL, |
3971 | 0 | "%s: UTF-16 string too small.", |
3972 | 0 | function ); |
3973 | |
|
3974 | 0 | return( -1 ); |
3975 | 0 | } |
3976 | 0 | utf16_string[ *utf16_string_index ] = 0; |
3977 | |
|
3978 | 0 | *utf16_string_index += 1; |
3979 | 0 | } |
3980 | 0 | return( 1 ); |
3981 | 0 | } |
3982 | | |