Coverage Report

Created: 2026-12-01 07:31

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