Coverage Report

Created: 2023-06-07 06:53

/src/libewf/libewf/libewf_serialized_string.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Serialized (file) object functions
3
 *
4
 * Copyright (C) 2006-2023, 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 <memory.h>
24
#include <types.h>
25
26
#include "libewf_libcerror.h"
27
#include "libewf_libuna.h"
28
#include "libewf_serialized_string.h"
29
30
/* Creates a serialized string
31
 * Make sure the value serialized_string is referencing, is set to NULL
32
 * Returns 1 if successful or -1 on error
33
 */
34
int libewf_serialized_string_initialize(
35
     libewf_serialized_string_t **serialized_string,
36
     libcerror_error_t **error )
37
0
{
38
0
  static char *function = "libewf_serialized_string_initialize";
39
40
0
  if( serialized_string == NULL )
41
0
  {
42
0
    libcerror_error_set(
43
0
     error,
44
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
45
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
46
0
     "%s: invalid serialized string.",
47
0
     function );
48
49
0
    return( -1 );
50
0
  }
51
0
  if( *serialized_string != NULL )
52
0
  {
53
0
    libcerror_error_set(
54
0
     error,
55
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
56
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
57
0
     "%s: invalid serialized string value already set.",
58
0
     function );
59
60
0
    return( -1 );
61
0
  }
62
0
  *serialized_string = memory_allocate_structure(
63
0
                        libewf_serialized_string_t );
64
65
0
  if( *serialized_string == NULL )
66
0
  {
67
0
    libcerror_error_set(
68
0
     error,
69
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
70
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
71
0
     "%s: unable to create serialized string.",
72
0
     function );
73
74
0
    goto on_error;
75
0
  }
76
0
  if( memory_set(
77
0
       *serialized_string,
78
0
       0,
79
0
       sizeof( libewf_serialized_string_t ) ) == NULL )
80
0
  {
81
0
    libcerror_error_set(
82
0
     error,
83
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
84
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
85
0
     "%s: unable to clear serialized string.",
86
0
     function );
87
88
0
    goto on_error;
89
0
  }
90
0
  return( 1 );
91
92
0
on_error:
93
0
  if( *serialized_string != NULL )
94
0
  {
95
0
    memory_free(
96
0
     *serialized_string );
97
98
0
    *serialized_string = NULL;
99
0
  }
100
0
  return( -1 );
101
0
}
102
103
/* Frees a serialized string
104
 * Returns 1 if successful or -1 on error
105
 */
106
int libewf_serialized_string_free(
107
     libewf_serialized_string_t **serialized_string,
108
     libcerror_error_t **error )
109
0
{
110
0
  static char *function = "libewf_serialized_string_free";
111
112
0
  if( serialized_string == NULL )
113
0
  {
114
0
    libcerror_error_set(
115
0
     error,
116
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
117
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
118
0
     "%s: invalid serialized string.",
119
0
     function );
120
121
0
    return( -1 );
122
0
  }
123
0
  if( *serialized_string != NULL )
124
0
  {
125
0
    if( ( *serialized_string )->data != NULL )
126
0
    {
127
0
      memory_free(
128
0
       ( *serialized_string )->data );
129
0
    }
130
0
    memory_free(
131
0
     *serialized_string );
132
133
0
    *serialized_string = NULL;
134
0
  }
135
0
  return( 1 );
136
0
}
137
138
/* Clones the serialized string
139
 * Returns 1 if successful or -1 on error
140
 */
141
int libewf_serialized_string_clone(
142
     libewf_serialized_string_t **destination_serialized_string,
143
     libewf_serialized_string_t *source_serialized_string,
144
     libcerror_error_t **error )
145
0
{
146
0
  static char *function = "libewf_serialized_string_clone";
147
148
0
  if( destination_serialized_string == NULL )
149
0
  {
150
0
    libcerror_error_set(
151
0
     error,
152
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
153
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
154
0
     "%s: invalid destination serialized string.",
155
0
     function );
156
157
0
    return( -1 );
158
0
  }
159
0
  if( *destination_serialized_string != NULL )
160
0
  {
161
0
    libcerror_error_set(
162
0
     error,
163
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
164
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
165
0
     "%s: invalid destination serialized string value already set.",
166
0
     function );
167
168
0
    return( -1 );
169
0
  }
170
0
  if( source_serialized_string == NULL )
171
0
  {
172
0
    *destination_serialized_string = NULL;
173
174
0
    return( 1 );
175
0
  }
176
0
  *destination_serialized_string = memory_allocate_structure(
177
0
                                    libewf_serialized_string_t );
178
179
0
  if( *destination_serialized_string == NULL )
180
0
  {
181
0
    libcerror_error_set(
182
0
     error,
183
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
184
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
185
0
     "%s: unable to create destination serialized string.",
186
0
     function );
187
188
0
    goto on_error;
189
0
  }
190
0
  if( memory_copy(
191
0
       *destination_serialized_string,
192
0
       source_serialized_string,
193
0
       sizeof( libewf_serialized_string_t ) ) == NULL )
194
0
  {
195
0
    libcerror_error_set(
196
0
     error,
197
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
198
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
199
0
     "%s: unable to copy source to destination serialized string.",
200
0
     function );
201
202
0
    memory_free(
203
0
     *destination_serialized_string );
204
205
0
    *destination_serialized_string = NULL;
206
207
0
    return( -1 );
208
0
  }
209
0
  ( *destination_serialized_string )->data = NULL;
210
211
0
  if( source_serialized_string->data != NULL )
212
0
  {
213
0
    ( *destination_serialized_string )->data = (uint8_t *) memory_allocate(
214
0
                                                            sizeof( uint8_t ) * source_serialized_string->data_size );
215
216
0
    if( ( *destination_serialized_string )->data == NULL )
217
0
    {
218
0
      libcerror_error_set(
219
0
       error,
220
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
221
0
       LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
222
0
       "%s: unable to create destination data.",
223
0
       function );
224
225
0
      goto on_error;
226
0
    }
227
0
    if( memory_copy(
228
0
         ( *destination_serialized_string )->data,
229
0
         source_serialized_string->data,
230
0
         source_serialized_string->data_size ) == NULL )
231
0
    {
232
0
      libcerror_error_set(
233
0
       error,
234
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
235
0
       LIBCERROR_MEMORY_ERROR_COPY_FAILED,
236
0
       "%s: unable to copy source to destination data.",
237
0
       function );
238
239
0
      goto on_error;
240
0
    }
241
0
    ( *destination_serialized_string )->data_size = source_serialized_string->data_size;
242
0
  }
243
0
  return( 1 );
244
245
0
on_error:
246
0
  if( *destination_serialized_string != NULL )
247
0
  {
248
0
    if( ( *destination_serialized_string )->data != NULL )
249
0
    {
250
0
      memory_free(
251
0
       ( *destination_serialized_string )->data );
252
0
    }
253
0
    memory_free(
254
0
     *destination_serialized_string );
255
256
0
    *destination_serialized_string = NULL;
257
0
  }
258
0
  return( -1 );
259
0
}
260
261
/* Reads a serialized string
262
 * Returns 1 if successful or -1 on error
263
 */
264
int libewf_serialized_string_read_data(
265
     libewf_serialized_string_t *serialized_string,
266
     const uint8_t *data,
267
     size_t data_size,
268
     libcerror_error_t **error )
269
0
{
270
0
  static char *function = "libewf_serialized_string_read_data";
271
272
0
  if( serialized_string == NULL )
273
0
  {
274
0
    libcerror_error_set(
275
0
     error,
276
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
277
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
278
0
     "%s: invalid serialized string.",
279
0
     function );
280
281
0
    return( -1 );
282
0
  }
283
0
  if( serialized_string->data != NULL )
284
0
  {
285
0
    libcerror_error_set(
286
0
     error,
287
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
288
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
289
0
     "%s: invalid serialized string - data value already set.",
290
0
     function );
291
292
0
    return( -1 );
293
0
  }
294
0
  if( data == 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 data.",
301
0
     function );
302
303
0
    return( -1 );
304
0
  }
305
0
  if( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE )
306
0
  {
307
0
    libcerror_error_set(
308
0
     error,
309
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
310
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
311
0
     "%s: invalid data size value out of bounds.",
312
0
     function );
313
314
0
    return( -1 );
315
0
  }
316
0
  if( ( data_size >= 1 )
317
0
   && ( data[ data_size - 1 ] == 0 ) )
318
0
  {
319
0
    data_size -= 1;
320
0
  }
321
0
  serialized_string->data = (uint8_t *) memory_allocate(
322
0
                                         sizeof( uint8_t ) * ( data_size + 1 ) );
323
324
0
  if( serialized_string->data == NULL )
325
0
  {
326
0
    libcerror_error_set(
327
0
     error,
328
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
329
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
330
0
     "%s: unable to create data.",
331
0
     function );
332
333
0
    goto on_error;
334
0
  }
335
0
  serialized_string->data_size = data_size + 1;
336
337
0
  if( memory_copy(
338
0
       serialized_string->data,
339
0
       data,
340
0
       data_size ) == NULL )
341
0
  {
342
0
    libcerror_error_set(
343
0
     error,
344
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
345
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
346
0
     "%s: unable to copy data to string.",
347
0
     function );
348
349
0
    goto on_error;
350
0
  }
351
0
  serialized_string->data[ data_size ] = 0;
352
353
0
  return( 1 );
354
355
0
on_error:
356
0
  if( serialized_string->data != NULL )
357
0
  {
358
0
    memory_free(
359
0
     serialized_string->data );
360
361
0
    serialized_string->data = NULL;
362
0
  }
363
0
  serialized_string->data_size = 0;
364
365
0
  return( -1 );
366
0
}
367
368
/* Reads a hexadecimal serialized string
369
 * Returns 1 if successful or -1 on error
370
 */
371
int libewf_serialized_string_read_hexadecimal_data(
372
     libewf_serialized_string_t *serialized_string,
373
     const uint8_t *data,
374
     size_t data_size,
375
     libcerror_error_t **error )
376
0
{
377
0
  static char *function = "libewf_serialized_string_read_hexadecimal_data";
378
0
  size_t data_offset    = 0;
379
0
  int zero_values_only  = 0;
380
381
0
  if( serialized_string == NULL )
382
0
  {
383
0
    libcerror_error_set(
384
0
     error,
385
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
386
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
387
0
     "%s: invalid serialized string.",
388
0
     function );
389
390
0
    return( -1 );
391
0
  }
392
0
  if( serialized_string->data != NULL )
393
0
  {
394
0
    libcerror_error_set(
395
0
     error,
396
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
397
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
398
0
     "%s: invalid serialized string - data value already set.",
399
0
     function );
400
401
0
    return( -1 );
402
0
  }
403
0
  if( data == NULL )
404
0
  {
405
0
    libcerror_error_set(
406
0
     error,
407
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
408
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
409
0
     "%s: invalid data.",
410
0
     function );
411
412
0
    return( -1 );
413
0
  }
414
0
  if( data_size > (size_t) MEMORY_MAXIMUM_ALLOCATION_SIZE )
415
0
  {
416
0
    libcerror_error_set(
417
0
     error,
418
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
419
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
420
0
     "%s: invalid data size value out of bounds.",
421
0
     function );
422
423
0
    return( -1 );
424
0
  }
425
0
  if( ( data_size >= 1 )
426
0
   && ( data[ data_size - 1 ] == 0 ) )
427
0
  {
428
0
    data_size -= 1;
429
0
  }
430
0
  serialized_string->data = (uint8_t *) memory_allocate(
431
0
                                         sizeof( uint8_t ) * ( data_size + 1 ) );
432
433
0
  if( serialized_string->data == NULL )
434
0
  {
435
0
    libcerror_error_set(
436
0
     error,
437
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
438
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
439
0
     "%s: unable to create data.",
440
0
     function );
441
442
0
    goto on_error;
443
0
  }
444
0
  serialized_string->data_size = data_size + 1;
445
446
0
  zero_values_only = 1;
447
448
0
  for( data_offset = 0;
449
0
       data_offset < data_size;
450
0
       data_offset++ )
451
0
  {
452
0
    if( data[ data_offset ] != (uint8_t) '0' )
453
0
    {
454
0
      zero_values_only = 0;
455
0
    }
456
0
    if( ( data[ data_offset ] >= (uint8_t) '0' )
457
0
     && ( data[ data_offset ] <= (uint8_t) '9' ) )
458
0
    {
459
0
      serialized_string->data[ data_offset ] = data[ data_offset ];
460
0
    }
461
0
    else if( ( data[ data_offset ] >= (uint8_t) 'A' )
462
0
          && ( data[ data_offset ] <= (uint8_t) 'F' ) )
463
0
    {
464
0
      serialized_string->data[ data_offset ] = (uint8_t) ( 'a' - 'A' ) + data[ data_offset ];
465
0
    }
466
0
    else if( ( data[ data_offset ] >= (uint8_t) 'a' )
467
0
          && ( data[ data_offset ] <= (uint8_t) 'f' ) )
468
0
    {
469
0
      serialized_string->data[ data_offset ] = data[ data_offset ];
470
0
    }
471
0
    else
472
0
    {
473
0
      libcerror_error_set(
474
0
       error,
475
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
476
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
477
0
       "%s: unsupported character in hexadecimal string.",
478
0
       function );
479
480
0
      goto on_error;
481
0
    }
482
0
  }
483
0
  serialized_string->data[ data_offset ] = 0;
484
485
0
  if( zero_values_only != 0 )
486
0
  {
487
0
    memory_free(
488
0
     serialized_string->data );
489
490
0
    serialized_string->data      = NULL;
491
0
    serialized_string->data_size = 0;
492
0
  }
493
0
  return( 1 );
494
495
0
on_error:
496
0
  if( serialized_string->data != NULL )
497
0
  {
498
0
    memory_free(
499
0
     serialized_string->data );
500
501
0
    serialized_string->data = NULL;
502
0
  }
503
0
  serialized_string->data_size = 0;
504
505
0
  return( -1 );
506
0
}
507
508
/* Retrieves the size of the UTF-8 encoded string
509
 * The returned size includes the end of string character
510
 * Returns 1 if successful, 0 if data is not set or -1 on error
511
 */
512
int libewf_serialized_string_get_utf8_string_size(
513
     libewf_serialized_string_t *serialized_string,
514
     size_t *utf8_string_size,
515
     libcerror_error_t **error )
516
0
{
517
0
  static char *function = "libewf_serialized_string_get_utf8_string_size";
518
519
0
  if( serialized_string == NULL )
520
0
  {
521
0
    libcerror_error_set(
522
0
     error,
523
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
524
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
525
0
     "%s: invalid serialized string.",
526
0
     function );
527
528
0
    return( -1 );
529
0
  }
530
0
  if( utf8_string_size == NULL )
531
0
  {
532
0
    libcerror_error_set(
533
0
     error,
534
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
535
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
536
0
     "%s: invalid UTF-8 string size.",
537
0
     function );
538
539
0
    return( -1 );
540
0
  }
541
0
  if( ( serialized_string->data == NULL )
542
0
   || ( serialized_string->data_size == 0 ) )
543
0
  {
544
0
    *utf8_string_size = 0;
545
546
0
    return( 0 );
547
0
  }
548
0
  *utf8_string_size = serialized_string->data_size;
549
550
0
  return( 1 );
551
0
}
552
553
/* Retrieves the UTF-8 encoded string value
554
 * The size should include the end of string character
555
 * Returns 1 if successful, 0 if data is not set or -1 on error
556
 */
557
int libewf_serialized_string_get_utf8_string(
558
     libewf_serialized_string_t *serialized_string,
559
     uint8_t *utf8_string,
560
     size_t utf8_string_size,
561
     libcerror_error_t **error )
562
0
{
563
0
  static char *function = "libewf_serialized_string_get_utf8_string";
564
565
0
  if( serialized_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 serialized string.",
572
0
     function );
573
574
0
    return( -1 );
575
0
  }
576
0
  if( utf8_string == NULL )
577
0
  {
578
0
    libcerror_error_set(
579
0
     error,
580
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
581
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
582
0
     "%s: invalid UTF-8 string.",
583
0
     function );
584
585
0
    return( -1 );
586
0
  }
587
0
  if( utf8_string_size > (size_t) SSIZE_MAX )
588
0
  {
589
0
    libcerror_error_set(
590
0
     error,
591
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
592
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
593
0
     "%s: invalid UTF-8 string size value exceeds maximum.",
594
0
     function );
595
596
0
    return( -1 );
597
0
  }
598
0
  if( ( serialized_string->data == NULL )
599
0
   || ( serialized_string->data_size == 0 ) )
600
0
  {
601
0
    if( utf8_string_size < 1 )
602
0
    {
603
0
      libcerror_error_set(
604
0
       error,
605
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
606
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
607
0
       "%s: invalid UTF-8 string size value too small.",
608
0
       function );
609
610
0
      return( -1 );
611
0
    }
612
0
    utf8_string[ 0 ] = 0;
613
614
0
    return( 0 );
615
0
  }
616
0
  if( utf8_string_size < serialized_string->data_size )
617
0
  {
618
0
    libcerror_error_set(
619
0
     error,
620
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
621
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
622
0
     "%s: invalid UTF-8 string size value too small.",
623
0
     function );
624
625
0
    return( -1 );
626
0
  }
627
0
  if( memory_copy(
628
0
       utf8_string,
629
0
       serialized_string->data,
630
0
       serialized_string->data_size ) == NULL )
631
0
  {
632
0
    libcerror_error_set(
633
0
     error,
634
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
635
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
636
0
     "%s: unable to set UTF-8 string.",
637
0
     function );
638
639
0
    return( -1 );
640
0
  }
641
0
  return( 1 );
642
0
}
643
644
/* Retrieves the size of the UTF-16 encoded string
645
 * The returned size includes the end of string character
646
 * Returns 1 if successful, 0 if data is not set or -1 on error
647
 */
648
int libewf_serialized_string_get_utf16_string_size(
649
     libewf_serialized_string_t *serialized_string,
650
     size_t *utf16_string_size,
651
     libcerror_error_t **error )
652
0
{
653
0
  static char *function = "libewf_serialized_string_get_utf16_string_size";
654
655
0
  if( serialized_string == NULL )
656
0
  {
657
0
    libcerror_error_set(
658
0
     error,
659
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
660
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
661
0
     "%s: invalid serialized string.",
662
0
     function );
663
664
0
    return( -1 );
665
0
  }
666
0
  if( utf16_string_size == NULL )
667
0
  {
668
0
    libcerror_error_set(
669
0
     error,
670
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
671
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
672
0
     "%s: invalid UTF-16 string size.",
673
0
     function );
674
675
0
    return( -1 );
676
0
  }
677
0
  if( ( serialized_string->data == NULL )
678
0
   || ( serialized_string->data_size == 0 ) )
679
0
  {
680
0
    *utf16_string_size = 0;
681
682
0
    return( 0 );
683
0
  }
684
0
  if( libuna_utf16_string_size_from_utf8(
685
0
       serialized_string->data,
686
0
       serialized_string->data_size,
687
0
       utf16_string_size,
688
0
       error ) != 1 )
689
0
  {
690
0
    libcerror_error_set(
691
0
     error,
692
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
693
0
     LIBCERROR_RUNTIME_ERROR_GET_FAILED,
694
0
     "%s: unable to retrieve UTF-16 string size.",
695
0
     function );
696
697
0
    return( -1 );
698
0
  }
699
0
  return( 1 );
700
0
}
701
702
/* Retrieves the UTF-16 encoded string value
703
 * The size should include the end of string character
704
 * Returns 1 if successful, 0 if data is not set or -1 on error
705
 */
706
int libewf_serialized_string_get_utf16_string(
707
     libewf_serialized_string_t *serialized_string,
708
     uint16_t *utf16_string,
709
     size_t utf16_string_size,
710
     libcerror_error_t **error )
711
0
{
712
0
  static char *function = "libewf_serialized_string_get_utf16_string";
713
714
0
  if( serialized_string == NULL )
715
0
  {
716
0
    libcerror_error_set(
717
0
     error,
718
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
719
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
720
0
     "%s: invalid serialized string.",
721
0
     function );
722
723
0
    return( -1 );
724
0
  }
725
0
  if( utf16_string == NULL )
726
0
  {
727
0
    libcerror_error_set(
728
0
     error,
729
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
730
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
731
0
     "%s: invalid UTF-16 string.",
732
0
     function );
733
734
0
    return( -1 );
735
0
  }
736
0
  if( utf16_string_size > (size_t) SSIZE_MAX )
737
0
  {
738
0
    libcerror_error_set(
739
0
     error,
740
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
741
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
742
0
     "%s: invalid UTF-16 string size value exceeds maximum.",
743
0
     function );
744
745
0
    return( -1 );
746
0
  }
747
0
  if( ( serialized_string->data == NULL )
748
0
   || ( serialized_string->data_size == 0 ) )
749
0
  {
750
0
    if( utf16_string_size < 1 )
751
0
    {
752
0
      libcerror_error_set(
753
0
       error,
754
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
755
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
756
0
       "%s: invalid UTF-16 string size value too small.",
757
0
       function );
758
759
0
      return( -1 );
760
0
    }
761
0
    utf16_string[ 0 ] = 0;
762
763
0
    return( 0 );
764
0
  }
765
0
  if( libuna_utf16_string_copy_from_utf8(
766
0
       utf16_string,
767
0
       utf16_string_size,
768
0
       serialized_string->data,
769
0
       serialized_string->data_size,
770
0
       error ) != 1 )
771
0
  {
772
0
    libcerror_error_set(
773
0
     error,
774
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
775
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
776
0
     "%s: unable to copy string to UTF-16 string.",
777
0
     function );
778
779
0
    return( -1 );
780
0
  }
781
0
  return( 1 );
782
0
}
783
784
/* Compares an UTF-8 string with a serialized string
785
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
786
 */
787
int libewf_serialized_string_compare_with_utf8_string(
788
     libewf_serialized_string_t *serialized_string,
789
     const uint8_t *utf8_string,
790
     size_t utf8_string_length,
791
     libcerror_error_t **error )
792
0
{
793
0
  static char *function = "libewf_serialized_string_compare_with_utf8_string";
794
0
  int result            = 0;
795
796
0
  if( serialized_string == NULL )
797
0
  {
798
0
    libcerror_error_set(
799
0
     error,
800
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
801
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
802
0
     "%s: invalid serialized string.",
803
0
     function );
804
805
0
    return( -1 );
806
0
  }
807
0
  if( ( serialized_string->data == NULL )
808
0
   || ( serialized_string->data_size == 0 ) )
809
0
  {
810
0
    result = LIBUNA_COMPARE_GREATER;
811
0
  }
812
0
  else
813
0
  {
814
0
    result = libuna_utf8_string_compare_with_utf8_stream(
815
0
        utf8_string,
816
0
        utf8_string_length,
817
0
        serialized_string->data,
818
0
        serialized_string->data_size,
819
0
        error );
820
0
  }
821
0
  if( result == -1 )
822
0
  {
823
0
    libcerror_error_set(
824
0
     error,
825
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
826
0
     LIBCERROR_RUNTIME_ERROR_GENERIC,
827
0
     "%s: unable to compare serialize string with UTF-8 string.",
828
0
     function );
829
830
0
    return( -1 );
831
0
  }
832
0
  return( result );
833
0
}
834
835
/* Compares an UTF-16 string with a serialized string
836
 * Returns LIBUNA_COMPARE_LESS, LIBUNA_COMPARE_EQUAL, LIBUNA_COMPARE_GREATER if successful or -1 on error
837
 */
838
int libewf_serialized_string_compare_with_utf16_string(
839
     libewf_serialized_string_t *serialized_string,
840
     const uint16_t *utf16_string,
841
     size_t utf16_string_length,
842
     libcerror_error_t **error )
843
0
{
844
0
  static char *function = "libewf_serialized_string_compare_with_utf16_string";
845
0
  int result            = 0;
846
847
0
  if( serialized_string == NULL )
848
0
  {
849
0
    libcerror_error_set(
850
0
     error,
851
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
852
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
853
0
     "%s: invalid serialized string.",
854
0
     function );
855
856
0
    return( -1 );
857
0
  }
858
0
  if( ( serialized_string->data == NULL )
859
0
   || ( serialized_string->data_size == 0 ) )
860
0
  {
861
0
    result = LIBUNA_COMPARE_GREATER;
862
0
  }
863
0
  else
864
0
  {
865
0
    result = libuna_utf16_string_compare_with_utf8_stream(
866
0
        utf16_string,
867
0
        utf16_string_length,
868
0
        serialized_string->data,
869
0
        serialized_string->data_size,
870
0
        error );
871
0
  }
872
0
  if( result == -1 )
873
0
  {
874
0
    libcerror_error_set(
875
0
     error,
876
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
877
0
     LIBCERROR_RUNTIME_ERROR_GENERIC,
878
0
     "%s: unable to compare serialize string with UTF-16 string.",
879
0
     function );
880
881
0
    return( -1 );
882
0
  }
883
0
  return( result );
884
0
}
885