Coverage Report

Created: 2024-02-25 07:20

/src/libphdi/libuna/libuna_byte_stream.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Byte stream functions
3
 *
4
 * Copyright (C) 2008-2024, 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_byte_stream.h"
26
#include "libuna_definitions.h"
27
#include "libuna_libcerror.h"
28
#include "libuna_types.h"
29
#include "libuna_unicode_character.h"
30
31
/* Determines the size of a byte stream from an UTF-8 string
32
 * Returns 1 if successful or -1 on error
33
 */
34
int libuna_byte_stream_size_from_utf8(
35
     const libuna_utf8_character_t *utf8_string,
36
     size_t utf8_string_size,
37
     int codepage,
38
     size_t *byte_stream_size,
39
     libcerror_error_t **error )
40
0
{
41
0
  static char *function                        = "libuna_byte_stream_size_from_utf8";
42
0
  size_t utf8_string_index                     = 0;
43
0
  libuna_unicode_character_t unicode_character = 0;
44
45
0
  if( utf8_string == NULL )
46
0
  {
47
0
    libcerror_error_set(
48
0
     error,
49
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
50
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
51
0
     "%s: invalid UTF-8 string.",
52
0
     function );
53
54
0
    return( -1 );
55
0
  }
56
0
  if( utf8_string_size > (size_t) SSIZE_MAX )
57
0
  {
58
0
    libcerror_error_set(
59
0
     error,
60
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
61
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
62
0
     "%s: invalid UTF-8 string size value exceeds maximum.",
63
0
     function );
64
65
0
    return( -1 );
66
0
  }
67
0
  if( byte_stream_size == NULL )
68
0
  {
69
0
    libcerror_error_set(
70
0
     error,
71
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
72
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
73
0
     "%s: invalid byte stream size.",
74
0
     function );
75
76
0
    return( -1 );
77
0
  }
78
0
  *byte_stream_size = 0;
79
80
0
  while( utf8_string_index < utf8_string_size )
81
0
  {
82
    /* Convert the UTF-8 character bytes into an Unicode character
83
     */
84
0
    if( libuna_unicode_character_copy_from_utf8(
85
0
         &unicode_character,
86
0
         utf8_string,
87
0
         utf8_string_size,
88
0
         &utf8_string_index,
89
0
         error ) != 1 )
90
0
    {
91
0
      libcerror_error_set(
92
0
       error,
93
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
94
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
95
0
       "%s: unable to copy Unicode character from UTF-8.",
96
0
       function );
97
98
0
      return( -1 );
99
0
    }
100
    /* Determine how many byte stream character bytes are required
101
     */
102
0
    if( libuna_unicode_character_size_to_byte_stream(
103
0
         unicode_character,
104
0
         codepage,
105
0
         byte_stream_size,
106
0
         error ) != 1 )
107
0
    {
108
0
      libcerror_error_set(
109
0
       error,
110
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
111
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
112
0
       "%s: unable to unable to determine size of Unicode character in byte stream.",
113
0
       function );
114
115
0
      return( -1 );
116
0
    }
117
0
    if( unicode_character == 0 )
118
0
    {
119
0
      break;
120
0
    }
121
0
  }
122
0
  return( 1 );
123
0
}
124
125
/* Copies a byte stream from an UTF-8 string
126
 * Returns 1 if successful or -1 on error
127
 */
128
int libuna_byte_stream_copy_from_utf8(
129
     uint8_t *byte_stream,
130
     size_t byte_stream_size,
131
     int codepage,
132
     const libuna_utf8_character_t *utf8_string,
133
     size_t utf8_string_size,
134
     libcerror_error_t **error )
135
0
{
136
0
  static char *function                        = "libuna_byte_stream_copy_from_utf8";
137
0
  size_t byte_stream_index                     = 0;
138
0
  size_t utf8_string_index                     = 0;
139
0
  libuna_unicode_character_t unicode_character = 0;
140
141
0
  if( byte_stream == NULL )
142
0
  {
143
0
    libcerror_error_set(
144
0
     error,
145
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
146
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
147
0
     "%s: invalid byte stream.",
148
0
     function );
149
150
0
    return( -1 );
151
0
  }
152
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
153
0
  {
154
0
    libcerror_error_set(
155
0
     error,
156
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
157
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
158
0
     "%s: invalid byte stream size value exceeds maximum.",
159
0
     function );
160
161
0
    return( -1 );
162
0
  }
163
0
  if( utf8_string == NULL )
164
0
  {
165
0
    libcerror_error_set(
166
0
     error,
167
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
168
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
169
0
     "%s: invalid UTF-8 string.",
170
0
     function );
171
172
0
    return( -1 );
173
0
  }
174
0
  if( utf8_string_size > (size_t) SSIZE_MAX )
175
0
  {
176
0
    libcerror_error_set(
177
0
     error,
178
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
179
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
180
0
     "%s: invalid UTF-8 string size value exceeds maximum.",
181
0
     function );
182
183
0
    return( -1 );
184
0
  }
185
0
  while( utf8_string_index < utf8_string_size )
186
0
  {
187
    /* Convert the UTF-8 string bytes into an Unicode character
188
     */
189
0
    if( libuna_unicode_character_copy_from_utf8(
190
0
         &unicode_character,
191
0
         utf8_string,
192
0
         utf8_string_size,
193
0
         &utf8_string_index,
194
0
         error ) != 1 )
195
0
    {
196
0
      libcerror_error_set(
197
0
       error,
198
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
199
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
200
0
       "%s: unable to copy Unicode character from UTF-8 string.",
201
0
       function );
202
203
0
      return( -1 );
204
0
    }
205
    /* Convert the Unicode character into a byte stream
206
     */
207
0
    if( libuna_unicode_character_copy_to_byte_stream(
208
0
         unicode_character,
209
0
         byte_stream,
210
0
         byte_stream_size,
211
0
         &byte_stream_index,
212
0
         codepage,
213
0
         error ) != 1 )
214
0
    {
215
0
      libcerror_error_set(
216
0
       error,
217
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
218
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
219
0
       "%s: unable to copy Unicode character to byte stream.",
220
0
       function );
221
222
0
      return( -1 );
223
0
    }
224
0
    if( unicode_character == 0 )
225
0
    {
226
0
      break;
227
0
    }
228
0
  }
229
0
  return( 1 );
230
0
}
231
232
/* Determines the size of a byte stream from an UTF-16 string
233
 * Returns 1 if successful or -1 on error
234
 */
235
int libuna_byte_stream_size_from_utf16(
236
     const libuna_utf16_character_t *utf16_string,
237
     size_t utf16_string_size,
238
     int codepage,
239
     size_t *byte_stream_size,
240
     libcerror_error_t **error )
241
0
{
242
0
  static char *function                        = "libuna_byte_stream_size_from_utf16";
243
0
  size_t utf16_string_index                    = 0;
244
0
  libuna_unicode_character_t unicode_character = 0;
245
246
0
  if( utf16_string == NULL )
247
0
  {
248
0
    libcerror_error_set(
249
0
     error,
250
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
251
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
252
0
     "%s: invalid UTF-16 string.",
253
0
     function );
254
255
0
    return( -1 );
256
0
  }
257
0
  if( utf16_string_size > (size_t) SSIZE_MAX )
258
0
  {
259
0
    libcerror_error_set(
260
0
     error,
261
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
262
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
263
0
     "%s: invalid UTF-16 string size value exceeds maximum.",
264
0
     function );
265
266
0
    return( -1 );
267
0
  }
268
0
  if( byte_stream_size == NULL )
269
0
  {
270
0
    libcerror_error_set(
271
0
     error,
272
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
273
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
274
0
     "%s: invalid byte stream size.",
275
0
     function );
276
277
0
    return( -1 );
278
0
  }
279
0
  *byte_stream_size = 0;
280
281
0
  while( utf16_string_index < utf16_string_size )
282
0
  {
283
    /* Convert the UTF-16 character bytes into an Unicode character
284
     */
285
0
    if( libuna_unicode_character_copy_from_utf16(
286
0
         &unicode_character,
287
0
         utf16_string,
288
0
         utf16_string_size,
289
0
         &utf16_string_index,
290
0
         error ) != 1 )
291
0
    {
292
0
      libcerror_error_set(
293
0
       error,
294
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
295
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
296
0
       "%s: unable to copy Unicode character from UTF-16.",
297
0
       function );
298
299
0
      return( -1 );
300
0
    }
301
    /* Determine how many byte stream character bytes are required
302
     */
303
0
    if( libuna_unicode_character_size_to_byte_stream(
304
0
         unicode_character,
305
0
         codepage,
306
0
         byte_stream_size,
307
0
         error ) != 1 )
308
0
    {
309
0
      libcerror_error_set(
310
0
       error,
311
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
312
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
313
0
       "%s: unable to unable to determine size of Unicode character in byte stream.",
314
0
       function );
315
316
0
      return( -1 );
317
0
    }
318
0
    if( unicode_character == 0 )
319
0
    {
320
0
      break;
321
0
    }
322
0
  }
323
0
  return( 1 );
324
0
}
325
326
/* Copies a byte stream from an UTF-16 string
327
 * Returns 1 if successful or -1 on error
328
 */
329
int libuna_byte_stream_copy_from_utf16(
330
     uint8_t *byte_stream,
331
     size_t byte_stream_size,
332
     int codepage,
333
     const libuna_utf16_character_t *utf16_string,
334
     size_t utf16_string_size,
335
     libcerror_error_t **error )
336
0
{
337
0
  static char *function                        = "libuna_byte_stream_copy_from_utf16";
338
0
  size_t byte_stream_index                     = 0;
339
0
  size_t utf16_string_index                    = 0;
340
0
  libuna_unicode_character_t unicode_character = 0;
341
342
0
  if( byte_stream == NULL )
343
0
  {
344
0
    libcerror_error_set(
345
0
     error,
346
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
347
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
348
0
     "%s: invalid byte stream.",
349
0
     function );
350
351
0
    return( -1 );
352
0
  }
353
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
354
0
  {
355
0
    libcerror_error_set(
356
0
     error,
357
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
358
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
359
0
     "%s: invalid byte stream size value exceeds maximum.",
360
0
     function );
361
362
0
    return( -1 );
363
0
  }
364
0
  if( utf16_string == NULL )
365
0
  {
366
0
    libcerror_error_set(
367
0
     error,
368
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
369
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
370
0
     "%s: invalid UTF-16 string.",
371
0
     function );
372
373
0
    return( -1 );
374
0
  }
375
0
  if( utf16_string_size > (size_t) SSIZE_MAX )
376
0
  {
377
0
    libcerror_error_set(
378
0
     error,
379
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
380
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
381
0
     "%s: invalid UTF-16 string size value exceeds maximum.",
382
0
     function );
383
384
0
    return( -1 );
385
0
  }
386
0
  while( utf16_string_index < utf16_string_size )
387
0
  {
388
    /* Convert the UTF-16 string bytes into an Unicode character
389
     */
390
0
    if( libuna_unicode_character_copy_from_utf16(
391
0
         &unicode_character,
392
0
         utf16_string,
393
0
         utf16_string_size,
394
0
         &utf16_string_index,
395
0
         error ) != 1 )
396
0
    {
397
0
      libcerror_error_set(
398
0
       error,
399
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
400
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
401
0
       "%s: unable to copy Unicode character from UTF-16 string.",
402
0
       function );
403
404
0
      return( -1 );
405
0
    }
406
    /* Convert the Unicode character into a byte stream
407
     */
408
0
    if( libuna_unicode_character_copy_to_byte_stream(
409
0
         unicode_character,
410
0
         byte_stream,
411
0
         byte_stream_size,
412
0
         &byte_stream_index,
413
0
         codepage,
414
0
         error ) != 1 )
415
0
    {
416
0
      libcerror_error_set(
417
0
       error,
418
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
419
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
420
0
       "%s: unable to copy Unicode character to byte stream.",
421
0
       function );
422
423
0
      return( -1 );
424
0
    }
425
0
    if( unicode_character == 0 )
426
0
    {
427
0
      break;
428
0
    }
429
0
  }
430
0
  return( 1 );
431
0
}
432
433
/* Determines the size of a byte stream from an UTF-32 string
434
 * Returns 1 if successful or -1 on error
435
 */
436
int libuna_byte_stream_size_from_utf32(
437
     const libuna_utf32_character_t *utf32_string,
438
     size_t utf32_string_size,
439
     int codepage,
440
     size_t *byte_stream_size,
441
     libcerror_error_t **error )
442
0
{
443
0
  static char *function                        = "libuna_byte_stream_size_from_utf32";
444
0
  size_t utf32_string_index                    = 0;
445
0
  libuna_unicode_character_t unicode_character = 0;
446
447
0
  if( utf32_string == NULL )
448
0
  {
449
0
    libcerror_error_set(
450
0
     error,
451
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
452
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
453
0
     "%s: invalid UTF-32 string.",
454
0
     function );
455
456
0
    return( -1 );
457
0
  }
458
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
459
0
  {
460
0
    libcerror_error_set(
461
0
     error,
462
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
463
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
464
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
465
0
     function );
466
467
0
    return( -1 );
468
0
  }
469
0
  if( byte_stream_size == NULL )
470
0
  {
471
0
    libcerror_error_set(
472
0
     error,
473
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
474
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
475
0
     "%s: invalid byte stream size.",
476
0
     function );
477
478
0
    return( -1 );
479
0
  }
480
0
  *byte_stream_size = 0;
481
482
0
  while( utf32_string_index < utf32_string_size )
483
0
  {
484
    /* Convert the UTF-32 character bytes into an Unicode character
485
     */
486
0
    if( libuna_unicode_character_copy_from_utf32(
487
0
         &unicode_character,
488
0
         utf32_string,
489
0
         utf32_string_size,
490
0
         &utf32_string_index,
491
0
         error ) != 1 )
492
0
    {
493
0
      libcerror_error_set(
494
0
       error,
495
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
496
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
497
0
       "%s: unable to copy Unicode character from UTF-32.",
498
0
       function );
499
500
0
      return( -1 );
501
0
    }
502
    /* Determine how many byte stream character bytes are required
503
     */
504
0
    if( libuna_unicode_character_size_to_byte_stream(
505
0
         unicode_character,
506
0
         codepage,
507
0
         byte_stream_size,
508
0
         error ) != 1 )
509
0
    {
510
0
      libcerror_error_set(
511
0
       error,
512
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
513
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
514
0
       "%s: unable to unable to determine size of Unicode character in byte stream.",
515
0
       function );
516
517
0
      return( -1 );
518
0
    }
519
0
    if( unicode_character == 0 )
520
0
    {
521
0
      break;
522
0
    }
523
0
  }
524
0
  return( 1 );
525
0
}
526
527
/* Copies a byte stream from an UTF-32 string
528
 * Returns 1 if successful or -1 on error
529
 */
530
int libuna_byte_stream_copy_from_utf32(
531
     uint8_t *byte_stream,
532
     size_t byte_stream_size,
533
     int codepage,
534
     const libuna_utf32_character_t *utf32_string,
535
     size_t utf32_string_size,
536
     libcerror_error_t **error )
537
0
{
538
0
  static char *function                        = "libuna_byte_stream_copy_from_utf32";
539
0
  size_t byte_stream_index                     = 0;
540
0
  size_t utf32_string_index                    = 0;
541
0
  libuna_unicode_character_t unicode_character = 0;
542
543
0
  if( byte_stream == NULL )
544
0
  {
545
0
    libcerror_error_set(
546
0
     error,
547
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
548
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
549
0
     "%s: invalid byte stream.",
550
0
     function );
551
552
0
    return( -1 );
553
0
  }
554
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
555
0
  {
556
0
    libcerror_error_set(
557
0
     error,
558
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
559
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
560
0
     "%s: invalid byte stream size value exceeds maximum.",
561
0
     function );
562
563
0
    return( -1 );
564
0
  }
565
0
  if( utf32_string == NULL )
566
0
  {
567
0
    libcerror_error_set(
568
0
     error,
569
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
570
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
571
0
     "%s: invalid UTF-32 string.",
572
0
     function );
573
574
0
    return( -1 );
575
0
  }
576
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
577
0
  {
578
0
    libcerror_error_set(
579
0
     error,
580
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
581
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
582
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
583
0
     function );
584
585
0
    return( -1 );
586
0
  }
587
0
  while( utf32_string_index < utf32_string_size )
588
0
  {
589
    /* Convert the UTF-32 string bytes into an Unicode character
590
     */
591
0
    if( libuna_unicode_character_copy_from_utf32(
592
0
         &unicode_character,
593
0
         utf32_string,
594
0
         utf32_string_size,
595
0
         &utf32_string_index,
596
0
         error ) != 1 )
597
0
    {
598
0
      libcerror_error_set(
599
0
       error,
600
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
601
0
       LIBCERROR_CONVERSION_ERROR_INPUT_FAILED,
602
0
       "%s: unable to copy Unicode character from UTF-32 string.",
603
0
       function );
604
605
0
      return( -1 );
606
0
    }
607
    /* Convert the Unicode character into a byte stream
608
     */
609
0
    if( libuna_unicode_character_copy_to_byte_stream(
610
0
         unicode_character,
611
0
         byte_stream,
612
0
         byte_stream_size,
613
0
         &byte_stream_index,
614
0
         codepage,
615
0
         error ) != 1 )
616
0
    {
617
0
      libcerror_error_set(
618
0
       error,
619
0
       LIBCERROR_ERROR_DOMAIN_CONVERSION,
620
0
       LIBCERROR_CONVERSION_ERROR_OUTPUT_FAILED,
621
0
       "%s: unable to copy Unicode character to byte stream.",
622
0
       function );
623
624
0
      return( -1 );
625
0
    }
626
0
    if( unicode_character == 0 )
627
0
    {
628
0
      break;
629
0
    }
630
0
  }
631
0
  return( 1 );
632
0
}
633