Coverage Report

Created: 2024-02-25 07:20

/src/libmsiecf/libfdatetime/libfdatetime_posix_time.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * POSIX time functions
3
 *
4
 * Copyright (C) 2009-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 <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libfdatetime_definitions.h"
28
#include "libfdatetime_date_time_values.h"
29
#include "libfdatetime_libcerror.h"
30
#include "libfdatetime_posix_time.h"
31
#include "libfdatetime_types.h"
32
33
/* Creates a POSIX time
34
 * Make sure the value posix_time is referencing, is set to NULL
35
 * Returns 1 if successful or -1 on error
36
 */
37
int libfdatetime_posix_time_initialize(
38
     libfdatetime_posix_time_t **posix_time,
39
     libcerror_error_t **error )
40
0
{
41
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
42
0
  static char *function                                   = "libfdatetime_posix_time_initialize";
43
44
0
  if( posix_time == NULL )
45
0
  {
46
0
    libcerror_error_set(
47
0
     error,
48
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
49
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
50
0
     "%s: invalid POSIX time.",
51
0
     function );
52
53
0
    return( -1 );
54
0
  }
55
0
  if( *posix_time != NULL )
56
0
  {
57
0
    libcerror_error_set(
58
0
     error,
59
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
60
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
61
0
     "%s: invalid POSIX time value already set.",
62
0
     function );
63
64
0
    return( -1 );
65
0
  }
66
0
  internal_posix_time = memory_allocate_structure(
67
0
                         libfdatetime_internal_posix_time_t );
68
69
0
  if( internal_posix_time == NULL )
70
0
  {
71
0
    libcerror_error_set(
72
0
     error,
73
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
74
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
75
0
     "%s: unable to create POSIX time.",
76
0
     function );
77
78
0
    goto on_error;
79
0
  }
80
0
  if( memory_set(
81
0
       internal_posix_time,
82
0
       0,
83
0
       sizeof( libfdatetime_internal_posix_time_t ) ) == NULL )
84
0
  {
85
0
    libcerror_error_set(
86
0
     error,
87
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
88
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
89
0
     "%s: unable to clear POSIX time.",
90
0
     function );
91
92
0
    goto on_error;
93
0
  }
94
0
  *posix_time = (libfdatetime_posix_time_t *) internal_posix_time;
95
96
0
  return( 1 );
97
98
0
on_error:
99
0
  if( internal_posix_time != NULL )
100
0
  {
101
0
    memory_free(
102
0
     internal_posix_time );
103
0
  }
104
0
  return( -1 );
105
0
}
106
107
/* Frees a POSIX time
108
 * Returns 1 if successful or -1 on error
109
 */
110
int libfdatetime_posix_time_free(
111
     libfdatetime_posix_time_t **posix_time,
112
     libcerror_error_t **error )
113
0
{
114
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
115
0
  static char *function                                   = "libfdatetime_posix_time_free";
116
117
0
  if( posix_time == NULL )
118
0
  {
119
0
    libcerror_error_set(
120
0
     error,
121
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
122
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
123
0
     "%s: invalid POSIX time.",
124
0
     function );
125
126
0
    return( -1 );
127
0
  }
128
0
  if( *posix_time != NULL )
129
0
  {
130
0
    internal_posix_time = (libfdatetime_internal_posix_time_t *) *posix_time;
131
0
    *posix_time         = NULL;
132
133
0
    memory_free(
134
0
     internal_posix_time );
135
0
  }
136
0
  return( 1 );
137
0
}
138
139
/* Converts a byte stream into a POSIX time
140
 * Returns 1 if successful or -1 on error
141
 */
142
int libfdatetime_posix_time_copy_from_byte_stream(
143
     libfdatetime_posix_time_t *posix_time,
144
     const uint8_t *byte_stream,
145
     size_t byte_stream_size,
146
     int byte_order,
147
     uint8_t value_type,
148
     libcerror_error_t **error )
149
0
{
150
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
151
0
  static char *function                                   = "libfdatetime_posix_time_copy_from_byte_stream";
152
0
  uint64_t value_64bit                                    = 0;
153
0
  uint8_t is_signed                                       = 0;
154
155
0
  if( posix_time == NULL )
156
0
  {
157
0
    libcerror_error_set(
158
0
     error,
159
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
160
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
161
0
     "%s: invalid POSIX time.",
162
0
     function );
163
164
0
    return( -1 );
165
0
  }
166
0
  internal_posix_time = (libfdatetime_internal_posix_time_t *) posix_time;
167
168
0
  if( byte_stream == NULL )
169
0
  {
170
0
    libcerror_error_set(
171
0
     error,
172
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
173
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
174
0
     "%s: invalid byte stream.",
175
0
     function );
176
177
0
    return( -1 );
178
0
  }
179
0
  if( byte_stream_size > (size_t) SSIZE_MAX )
180
0
  {
181
0
    libcerror_error_set(
182
0
     error,
183
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
184
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
185
0
     "%s: byte stream size exceeds maximum.",
186
0
     function );
187
188
0
    return( -1 );
189
0
  }
190
0
  if( ( byte_order != LIBFDATETIME_ENDIAN_BIG )
191
0
   && ( byte_order != LIBFDATETIME_ENDIAN_LITTLE ) )
192
0
  {
193
0
    libcerror_error_set(
194
0
     error,
195
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
196
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
197
0
     "%s: unsupported byte order.",
198
0
     function );
199
200
0
    return( -1 );
201
0
  }
202
0
  if( ( value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
203
0
   || ( value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED ) )
204
0
  {
205
0
    if( byte_stream_size < 4 )
206
0
    {
207
0
      libcerror_error_set(
208
0
       error,
209
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
210
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
211
0
       "%s: byte stream too small.",
212
0
       function );
213
214
0
      return( -1 );
215
0
    }
216
0
    if( byte_order == LIBFDATETIME_ENDIAN_LITTLE )
217
0
    {
218
0
      byte_stream_copy_to_uint32_little_endian(
219
0
       byte_stream,
220
0
       value_64bit );
221
0
    }
222
0
    else if( byte_order == LIBFDATETIME_ENDIAN_BIG )
223
0
    {
224
0
      byte_stream_copy_to_uint32_big_endian(
225
0
       byte_stream,
226
0
       value_64bit );
227
0
    }
228
0
    is_signed = (uint8_t) ( value_64bit >> 31 );
229
230
0
    if( ( is_signed != 0 )
231
0
     && ( ( value_64bit & 0x7fffffffUL ) == 0 ) )
232
0
    {
233
0
      libcerror_error_set(
234
0
       error,
235
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
236
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
237
0
       "%s: unsupported timestamp.",
238
0
       function );
239
240
0
      return( -1 );
241
0
    }
242
0
  }
243
0
  else if( ( value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_SIGNED )
244
0
        || ( value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_UNSIGNED )
245
0
        || ( value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
246
0
        || ( value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
247
0
        || ( value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
248
0
        || ( value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
249
0
  {
250
0
    if( byte_stream_size < 8 )
251
0
    {
252
0
      libcerror_error_set(
253
0
       error,
254
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
255
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
256
0
       "%s: byte stream too small.",
257
0
       function );
258
259
0
      return( -1 );
260
0
    }
261
0
    if( byte_order == LIBFDATETIME_ENDIAN_LITTLE )
262
0
    {
263
0
      byte_stream_copy_to_uint64_little_endian(
264
0
       byte_stream,
265
0
       value_64bit );
266
0
    }
267
0
    else if( byte_order == LIBFDATETIME_ENDIAN_BIG )
268
0
    {
269
0
      byte_stream_copy_to_uint64_big_endian(
270
0
       byte_stream,
271
0
       value_64bit );
272
0
    }
273
0
    is_signed = (uint8_t) ( value_64bit >> 63 );
274
275
#if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
276
    if( ( is_signed != 0 )
277
     && ( ( value_64bit & 0x7fffffffffffffffUL ) == 0 ) )
278
#else
279
0
    if( ( is_signed != 0 )
280
0
     && ( ( value_64bit & 0x7fffffffffffffffULL ) == 0 ) )
281
0
#endif
282
0
    {
283
0
      libcerror_error_set(
284
0
       error,
285
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
286
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
287
0
       "%s: unsupported timestamp.",
288
0
       function );
289
290
0
      return( -1 );
291
0
    }
292
0
  }
293
0
  else
294
0
  {
295
0
    libcerror_error_set(
296
0
     error,
297
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
298
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
299
0
     "%s: unsupported value type.",
300
0
     function );
301
302
0
    return( -1 );
303
0
  }
304
0
  internal_posix_time->timestamp  = value_64bit;
305
0
  internal_posix_time->value_type = value_type;
306
307
0
  return( 1 );
308
0
}
309
310
/* Converts a 32-bit value into a POSIX time
311
 * Returns 1 if successful or -1 on error
312
 */
313
int libfdatetime_posix_time_copy_from_32bit(
314
     libfdatetime_posix_time_t *posix_time,
315
     uint32_t value_32bit,
316
     uint8_t value_type,
317
     libcerror_error_t **error )
318
0
{
319
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
320
0
  static char *function                                   = "libfdatetime_posix_time_copy_from_32bit";
321
0
  uint8_t is_signed                                       = 0;
322
323
0
  if( posix_time == NULL )
324
0
  {
325
0
    libcerror_error_set(
326
0
     error,
327
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
328
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
329
0
     "%s: invalid POSIX time.",
330
0
     function );
331
332
0
    return( -1 );
333
0
  }
334
0
  internal_posix_time = (libfdatetime_internal_posix_time_t *) posix_time;
335
336
0
  if( ( value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
337
0
   && ( value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED ) )
338
0
  {
339
0
    libcerror_error_set(
340
0
     error,
341
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
342
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
343
0
     "%s: unsupported value type.",
344
0
     function );
345
346
0
    return( -1 );
347
0
  }
348
0
  is_signed = (uint8_t) ( value_32bit >> 31 );
349
350
0
  if( ( is_signed != 0 )
351
0
   && ( ( value_32bit & 0x7fffffffUL ) == 0 ) )
352
0
  {
353
0
    libcerror_error_set(
354
0
     error,
355
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
356
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
357
0
     "%s: unsupported timestamp.",
358
0
     function );
359
360
0
    return( -1 );
361
0
  }
362
0
  internal_posix_time->timestamp  = (uint64_t) value_32bit;
363
0
  internal_posix_time->value_type = value_type;
364
365
0
  return( 1 );
366
0
}
367
368
/* Converts a POSIX time into a 32-bit value
369
 * Returns 1 if successful or -1 on error
370
 */
371
int libfdatetime_posix_time_copy_to_32bit(
372
     libfdatetime_posix_time_t *posix_time,
373
     uint32_t *value_32bit,
374
     uint8_t *value_type,
375
     libcerror_error_t **error )
376
0
{
377
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
378
0
  static char *function                                   = "libfdatetime_posix_time_copy_to_32bit";
379
380
0
  if( posix_time == NULL )
381
0
  {
382
0
    libcerror_error_set(
383
0
     error,
384
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
385
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
386
0
     "%s: invalid POSIX time.",
387
0
     function );
388
389
0
    return( -1 );
390
0
  }
391
0
  internal_posix_time = (libfdatetime_internal_posix_time_t *) posix_time;
392
393
0
  if( ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
394
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED ) )
395
0
  {
396
0
    libcerror_error_set(
397
0
     error,
398
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
399
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
400
0
     "%s: invalid POSIX time - unsupported value type.",
401
0
     function );
402
403
0
    return( -1 );
404
0
  }
405
0
  if( value_32bit == NULL )
406
0
  {
407
0
    libcerror_error_set(
408
0
     error,
409
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
410
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
411
0
     "%s: invalid 32-bit value.",
412
0
     function );
413
414
0
    return( -1 );
415
0
  }
416
0
  if( value_type == NULL )
417
0
  {
418
0
    libcerror_error_set(
419
0
     error,
420
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
421
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
422
0
     "%s: invalid value type.",
423
0
     function );
424
425
0
    return( -1 );
426
0
  }
427
0
  *value_32bit = (uint32_t) internal_posix_time->timestamp;
428
0
  *value_type  = internal_posix_time->value_type;
429
430
0
  return( 1 );
431
0
}
432
433
/* Converts a 64-bit value into a POSIX time
434
 * Returns 1 if successful or -1 on error
435
 */
436
int libfdatetime_posix_time_copy_from_64bit(
437
     libfdatetime_posix_time_t *posix_time,
438
     uint64_t value_64bit,
439
     uint8_t value_type,
440
     libcerror_error_t **error )
441
0
{
442
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
443
0
  static char *function                                   = "libfdatetime_posix_time_copy_from_64bit";
444
0
  uint8_t is_signed                                       = 0;
445
446
0
  if( posix_time == NULL )
447
0
  {
448
0
    libcerror_error_set(
449
0
     error,
450
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
451
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
452
0
     "%s: invalid POSIX time.",
453
0
     function );
454
455
0
    return( -1 );
456
0
  }
457
0
  internal_posix_time = (libfdatetime_internal_posix_time_t *) posix_time;
458
459
0
  if( ( value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_SIGNED )
460
0
   && ( value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_UNSIGNED )
461
0
   && ( value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
462
0
   && ( value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
463
0
   && ( value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
464
0
   && ( value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
465
0
  {
466
0
    libcerror_error_set(
467
0
     error,
468
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
469
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
470
0
     "%s: unsupported value type.",
471
0
     function );
472
473
0
    return( -1 );
474
0
  }
475
0
  is_signed = (uint8_t) ( value_64bit >> 63 );
476
477
#if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
478
  if( ( is_signed != 0 )
479
   && ( ( value_64bit & 0x7fffffffffffffffUL ) == 0 ) )
480
#else
481
0
  if( ( is_signed != 0 )
482
0
   && ( ( value_64bit & 0x7fffffffffffffffULL ) == 0 ) )
483
0
#endif
484
0
  {
485
0
    libcerror_error_set(
486
0
     error,
487
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
488
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
489
0
     "%s: unsupported timestamp.",
490
0
     function );
491
492
0
    return( -1 );
493
0
  }
494
0
  internal_posix_time->timestamp  = value_64bit;
495
0
  internal_posix_time->value_type = value_type;
496
497
0
  return( 1 );
498
0
}
499
500
/* Converts a POSIX time into a 64-bit value
501
 * Returns 1 if successful or -1 on error
502
 */
503
int libfdatetime_posix_time_copy_to_64bit(
504
     libfdatetime_posix_time_t *posix_time,
505
     uint64_t *value_64bit,
506
     uint8_t *value_type,
507
     libcerror_error_t **error )
508
0
{
509
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
510
0
  static char *function                                   = "libfdatetime_posix_time_copy_to_64bit";
511
512
0
  if( posix_time == NULL )
513
0
  {
514
0
    libcerror_error_set(
515
0
     error,
516
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
517
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
518
0
     "%s: invalid POSIX time.",
519
0
     function );
520
521
0
    return( -1 );
522
0
  }
523
0
  internal_posix_time = (libfdatetime_internal_posix_time_t *) posix_time;
524
525
0
  if( ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_SIGNED )
526
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_UNSIGNED )
527
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
528
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
529
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
530
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
531
0
  {
532
0
    libcerror_error_set(
533
0
     error,
534
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
535
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
536
0
     "%s: invalid POSIX time - unsupported value type.",
537
0
     function );
538
539
0
    return( -1 );
540
0
  }
541
0
  if( value_64bit == NULL )
542
0
  {
543
0
    libcerror_error_set(
544
0
     error,
545
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
546
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
547
0
     "%s: invalid 64-bit value.",
548
0
     function );
549
550
0
    return( -1 );
551
0
  }
552
0
  if( value_type == NULL )
553
0
  {
554
0
    libcerror_error_set(
555
0
     error,
556
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
557
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
558
0
     "%s: invalid value type.",
559
0
     function );
560
561
0
    return( -1 );
562
0
  }
563
0
  *value_64bit = internal_posix_time->timestamp;
564
0
  *value_type  = internal_posix_time->value_type;
565
566
0
  return( 1 );
567
0
}
568
569
/* Converts a POSIX time into date time values
570
 * Returns 1 if successful or -1 on error
571
 */
572
int libfdatetime_internal_posix_time_copy_to_date_time_values(
573
     libfdatetime_internal_posix_time_t *internal_posix_time,
574
     libfdatetime_date_time_values_t *date_time_values,
575
     libcerror_error_t **error )
576
0
{
577
0
  static char *function    = "libfdatetime_internal_posix_time_copy_to_date_time_values";
578
0
  uint64_t posix_timestamp = 0;
579
0
  uint16_t days_in_year    = 0;
580
0
  uint8_t days_in_month    = 0;
581
0
  uint8_t is_signed        = 0;
582
583
0
  if( internal_posix_time == NULL )
584
0
  {
585
0
    libcerror_error_set(
586
0
     error,
587
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
588
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
589
0
     "%s: invalid POSIX time.",
590
0
     function );
591
592
0
    return( -1 );
593
0
  }
594
0
  if( ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
595
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED )
596
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_SIGNED )
597
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_UNSIGNED )
598
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
599
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
600
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
601
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
602
0
  {
603
0
    libcerror_error_set(
604
0
     error,
605
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
606
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
607
0
     "%s: invalid POSIX time - unsupported value type.",
608
0
     function );
609
610
0
    return( -1 );
611
0
  }
612
0
  if( date_time_values == NULL )
613
0
  {
614
0
    libcerror_error_set(
615
0
     error,
616
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
617
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
618
0
     "%s: invalid date time values.",
619
0
     function );
620
621
0
    return( -1 );
622
0
  }
623
0
  posix_timestamp = internal_posix_time->timestamp;
624
625
0
  if( ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
626
0
   || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED ) )
627
0
  {
628
0
    is_signed = (uint8_t) ( posix_timestamp >> 31 );
629
630
0
    posix_timestamp &= 0x7fffffffUL;
631
0
  }
632
0
  else if( ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_SIGNED )
633
0
        || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_UNSIGNED )
634
0
        || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
635
0
        || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
636
0
        || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
637
0
        || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
638
0
  {
639
0
    is_signed = (uint8_t) ( posix_timestamp >> 63 );
640
641
#if defined( __BORLANDC__ ) && ( __BORLANDC__ < 0x0560 )
642
    posix_timestamp &= 0x7fffffffffffffffUL;
643
#else
644
0
    posix_timestamp &= 0x7fffffffffffffffULL;
645
0
#endif
646
0
  }
647
0
  if( ( is_signed != 0 )
648
0
   && ( posix_timestamp == 0 ) )
649
0
  {
650
0
    libcerror_error_set(
651
0
     error,
652
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
653
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
654
0
     "%s: unsupported timestamp.",
655
0
     function );
656
657
0
    return( -1 );
658
0
  }
659
0
        date_time_values->nano_seconds = 0;
660
661
0
  if( ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
662
0
   || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
663
0
  {
664
    /* The timestamp is in units of nano seconds correct the value to seconds
665
     */
666
0
    if( is_signed == 0 )
667
0
    {
668
0
      date_time_values->nano_seconds = posix_timestamp % 1000;
669
0
    }
670
0
    else
671
0
    {
672
0
      date_time_values->nano_seconds = 1000 - ( posix_timestamp % 1000 );
673
0
    }
674
0
    posix_timestamp /= 1000;
675
0
  }
676
0
  if( ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
677
0
   || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
678
0
   || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
679
0
   || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
680
0
  {
681
    /* The timestamp is in units of micro seconds correct the value to seconds
682
     */
683
0
    if( is_signed == 0 )
684
0
    {
685
0
      date_time_values->micro_seconds = posix_timestamp % 1000;
686
0
    }
687
0
    else
688
0
    {
689
0
      date_time_values->micro_seconds = 1000 - ( posix_timestamp % 1000 );
690
0
    }
691
0
    posix_timestamp /= 1000;
692
693
0
    if( is_signed == 0 )
694
0
    {
695
0
      date_time_values->milli_seconds = posix_timestamp % 1000;
696
0
    }
697
0
    else
698
0
    {
699
0
      date_time_values->milli_seconds = 1000 - ( posix_timestamp % 1000 );
700
0
    }
701
0
    posix_timestamp /= 1000;
702
0
  }
703
0
  else
704
0
  {
705
0
          date_time_values->micro_seconds = 0;
706
0
          date_time_values->milli_seconds = 0;
707
0
  }
708
  /* There are 60 seconds in a minute correct the value to minutes
709
   */
710
0
  date_time_values->seconds = posix_timestamp % 60;
711
0
  posix_timestamp          /= 60;
712
713
0
  if( ( is_signed != 0 )
714
0
   && ( date_time_values->seconds > 0 ) )
715
0
  {
716
0
    date_time_values->seconds = 60 - date_time_values->seconds;
717
0
  }
718
  /* There are 60 minutes in an hour correct the value to hours
719
   */
720
0
  date_time_values->minutes = posix_timestamp % 60;
721
0
  posix_timestamp          /= 60;
722
723
0
  if( ( is_signed != 0 )
724
0
   && ( date_time_values->minutes > 0 ) )
725
0
  {
726
0
    date_time_values->minutes = 60 - date_time_values->minutes;
727
0
  }
728
  /* There are 24 hours in a day correct the value to days
729
   */
730
0
  date_time_values->hours = posix_timestamp % 24;
731
0
  posix_timestamp        /= 24;
732
733
0
  if( ( is_signed != 0 )
734
0
   && ( date_time_values->hours > 0 ) )
735
0
  {
736
0
    date_time_values->hours = 24 - date_time_values->hours;
737
0
  }
738
  /* Determine the number of years starting at 'Jan 1, 1970 00:00:00'
739
   * correct the value to days within the year
740
   */
741
0
  if( is_signed == 0 )
742
0
  {
743
    /* Add 1 day to compensate that Jan 1, 1970 is represented as 0
744
     */
745
0
    posix_timestamp += 1;
746
747
0
    if( posix_timestamp >= 10957 )
748
0
    {
749
0
      date_time_values->year = 2000;
750
751
0
      posix_timestamp -= 10957;
752
0
    }
753
0
    else
754
0
    {
755
0
      date_time_values->year = 1970;
756
0
    }
757
0
  }
758
0
  else
759
0
  {
760
    /* Remove 1 day to compensate that Jan 1, 1970 is represented as 0
761
     */
762
0
    posix_timestamp -= 1;
763
764
0
    date_time_values->year = 1969;
765
0
  }
766
0
  while( posix_timestamp > 0 )
767
0
  {
768
    /* Check for a leap year
769
     * The year is ( ( dividable by 4 ) and ( not dividable by 100 ) ) or ( dividable by 400 )
770
     */
771
0
    if( ( ( ( date_time_values->year % 4 ) == 0 )
772
0
      &&  ( ( date_time_values->year % 100 ) != 0 ) )
773
0
     || ( ( date_time_values->year % 400 ) == 0 ) )
774
0
    {
775
0
      days_in_year = 366;
776
0
    }
777
0
    else
778
0
    {
779
0
      days_in_year = 365;
780
0
    }
781
0
    if( posix_timestamp <= days_in_year )
782
0
    {
783
0
      break;
784
0
    }
785
0
    posix_timestamp -= days_in_year;
786
787
0
    if( is_signed == 0 )
788
0
    {
789
0
      date_time_values->year += 1;
790
0
    }
791
0
    else
792
0
    {
793
0
      date_time_values->year -= 1;
794
0
    }
795
0
  }
796
797
  /* Determine the month correct the value to days within the month
798
   */
799
0
  if( is_signed == 0 )
800
0
  {
801
0
    date_time_values->month = 1;
802
0
  }
803
0
  else
804
0
  {
805
0
    date_time_values->month = 12;
806
0
  }
807
0
  do
808
0
  {
809
    /* February (2)
810
     */
811
0
    if( date_time_values->month == 2 )
812
0
    {
813
0
      if( ( ( ( date_time_values->year % 4 ) == 0 )
814
0
        &&  ( ( date_time_values->year % 100 ) != 0 ) )
815
0
       || ( ( date_time_values->year % 400 ) == 0 ) )
816
0
      {
817
0
        days_in_month = 29;
818
0
      }
819
0
      else
820
0
      {
821
0
        days_in_month = 28;
822
0
      }
823
0
    }
824
    /* April (4), June (6), September (9), November (11)
825
     */
826
0
    else if( ( date_time_values->month == 4 )
827
0
          || ( date_time_values->month == 6 )
828
0
          || ( date_time_values->month == 9 )
829
0
          || ( date_time_values->month == 11 ) )
830
0
    {
831
0
      days_in_month = 30;
832
0
    }
833
    /* January (1), March (3), May (5), July (7), August (8), October (10), December (12)
834
     */
835
0
    else if( ( date_time_values->month == 1 )
836
0
          || ( date_time_values->month == 3 )
837
0
          || ( date_time_values->month == 5 )
838
0
          || ( date_time_values->month == 7 )
839
0
          || ( date_time_values->month == 8 )
840
0
          || ( date_time_values->month == 10 )
841
0
          || ( date_time_values->month == 12 ) )
842
0
    {
843
0
      days_in_month = 31;
844
0
    }
845
    /* This should never happen, but just in case
846
     */
847
0
    else
848
0
    {
849
0
      libcerror_error_set(
850
0
       error,
851
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
852
0
       LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
853
0
       "%s: unsupported month: %d.",
854
0
       function,
855
0
       date_time_values->month );
856
857
0
      return( -1 );
858
0
    }
859
0
    if( posix_timestamp <= days_in_month )
860
0
    {
861
0
      break;
862
0
    }
863
0
    posix_timestamp -= days_in_month;
864
865
0
    if( is_signed == 0 )
866
0
    {
867
0
      date_time_values->month += 1;
868
0
    }
869
0
    else
870
0
    {
871
0
      date_time_values->month -= 1;
872
0
    }
873
0
  }
874
0
  while( posix_timestamp > 0 );
875
876
  /* Determine the day
877
   */
878
0
  date_time_values->day = (uint8_t) posix_timestamp;
879
880
0
  if( is_signed != 0 )
881
0
  {
882
0
    date_time_values->day = days_in_month - date_time_values->day;
883
0
  }
884
0
  return( 1 );
885
0
}
886
887
/* Deterimes the size of the string for the POSIX time
888
 * The string size includes the end of string character
889
 * Returns 1 if successful or -1 on error
890
 */
891
int libfdatetime_posix_time_get_string_size(
892
     libfdatetime_posix_time_t *posix_time,
893
     size_t *string_size,
894
     uint32_t string_format_flags,
895
     libcerror_error_t **error )
896
0
{
897
0
  libfdatetime_date_time_values_t date_time_values;
898
899
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
900
0
  static char *function                                   = "libfdatetime_posix_time_get_string_size";
901
0
  int result                                              = 0;
902
903
0
  if( posix_time == NULL )
904
0
  {
905
0
    libcerror_error_set(
906
0
     error,
907
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
908
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
909
0
     "%s: invalid POSIX time.",
910
0
     function );
911
912
0
    return( -1 );
913
0
  }
914
0
  internal_posix_time = (libfdatetime_internal_posix_time_t *) posix_time;
915
916
0
  if( ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
917
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED )
918
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_SIGNED )
919
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_UNSIGNED )
920
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
921
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
922
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
923
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
924
0
  {
925
0
    libcerror_error_set(
926
0
     error,
927
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
928
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
929
0
     "%s: invalid POSIX time - unsupported value type.",
930
0
     function );
931
932
0
    return( -1 );
933
0
  }
934
0
  if( string_size == 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 string size.",
941
0
     function );
942
943
0
    return( -1 );
944
0
  }
945
0
  result = libfdatetime_internal_posix_time_copy_to_date_time_values(
946
0
            (libfdatetime_internal_posix_time_t *) posix_time,
947
0
            &date_time_values,
948
0
            error );
949
950
0
  if( result != 1 )
951
0
  {
952
#if defined( HAVE_DEBUG_OUTPUT )
953
    libcerror_error_set(
954
     error,
955
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
956
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
957
     "%s: unable to set date time values.",
958
     function );
959
960
/* TODO debug print error */
961
962
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
963
964
0
    if( ( error != NULL )
965
0
     && ( *error != NULL ) )
966
0
    {
967
0
      libcerror_error_free(
968
0
       error );
969
0
    }
970
0
  }
971
0
  else
972
0
  {
973
0
    result = libfdatetime_date_time_values_get_string_size(
974
0
              &date_time_values,
975
0
              string_size,
976
0
              string_format_flags,
977
0
              error );
978
979
0
    if( result == -1 )
980
0
    {
981
0
      libcerror_error_set(
982
0
       error,
983
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
984
0
       LIBCERROR_RUNTIME_ERROR_GET_FAILED,
985
0
       "%s: unable to get string size.",
986
0
       function );
987
988
0
      return( -1 );
989
0
    }
990
0
  }
991
0
  if( result != 1 )
992
0
  {
993
    /* Make sure the string can hold the hexadecimal representation of the POSIX time
994
     */
995
0
    if( ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
996
0
     || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED ) )
997
0
    {
998
0
      *string_size = 13;
999
0
    }
1000
0
    else
1001
0
    {
1002
0
      *string_size = 21;
1003
0
    }
1004
0
  }
1005
0
  return( 1 );
1006
0
}
1007
1008
/* Converts the POSIX time into an UTF-8 string in hexadecimal representation
1009
 * The string size should include the end of string character
1010
 * Returns 1 if successful or -1 on error
1011
 */
1012
int libfdatetime_internal_posix_time_copy_to_utf8_string_in_hexadecimal(
1013
     libfdatetime_internal_posix_time_t *internal_posix_time,
1014
     uint8_t *utf8_string,
1015
     size_t utf8_string_size,
1016
     size_t *utf8_string_index,
1017
     libcerror_error_t **error )
1018
0
{
1019
0
  static char *function = "libfdatetime_internal_posix_time_copy_to_utf8_string_in_hexadecimal";
1020
0
  size_t string_index   = 0;
1021
0
  size_t string_size    = 0;
1022
0
  uint8_t byte_value    = 0;
1023
0
  int8_t byte_shift     = 0;
1024
1025
0
  if( internal_posix_time == NULL )
1026
0
  {
1027
0
    libcerror_error_set(
1028
0
     error,
1029
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1030
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1031
0
     "%s: invalid POSIX time.",
1032
0
     function );
1033
1034
0
    return( -1 );
1035
0
  }
1036
0
  if( ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
1037
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED )
1038
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_SIGNED )
1039
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_UNSIGNED )
1040
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
1041
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
1042
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
1043
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
1044
0
  {
1045
0
    libcerror_error_set(
1046
0
     error,
1047
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1048
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1049
0
     "%s: invalid POSIX time - unsupported value type.",
1050
0
     function );
1051
1052
0
    return( -1 );
1053
0
  }
1054
0
  if( utf8_string == NULL )
1055
0
  {
1056
0
    libcerror_error_set(
1057
0
     error,
1058
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1059
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1060
0
     "%s: invalid UTF-8 string.",
1061
0
     function );
1062
1063
0
    return( -1 );
1064
0
  }
1065
0
  if( utf8_string_size > (size_t) SSIZE_MAX )
1066
0
  {
1067
0
    libcerror_error_set(
1068
0
     error,
1069
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1070
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1071
0
     "%s: invalid UTF-8 string size value exceeds maximum.",
1072
0
     function );
1073
1074
0
    return( -1 );
1075
0
  }
1076
0
  if( utf8_string_index == NULL )
1077
0
  {
1078
0
    libcerror_error_set(
1079
0
     error,
1080
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1081
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1082
0
     "%s: invalid UTF-8 string index.",
1083
0
     function );
1084
1085
0
    return( -1 );
1086
0
  }
1087
0
  if( ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
1088
0
   || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED ) )
1089
0
  {
1090
0
    string_size = 13;
1091
0
    byte_shift  = 28;
1092
0
  }
1093
0
  else
1094
0
  {
1095
0
    string_size = 21;
1096
0
    byte_shift  = 60;
1097
0
  }
1098
0
  if( ( utf8_string_size < string_size )
1099
0
   || ( *utf8_string_index > ( utf8_string_size - string_size ) ) )
1100
0
  {
1101
0
    libcerror_error_set(
1102
0
     error,
1103
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1104
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1105
0
     "%s: UTF-8 string is too small.",
1106
0
     function );
1107
1108
0
    return( -1 );
1109
0
  }
1110
0
  string_index = *utf8_string_index;
1111
1112
0
  utf8_string[ string_index++ ] = (uint8_t) '(';
1113
0
  utf8_string[ string_index++ ] = (uint8_t) '0';
1114
0
  utf8_string[ string_index++ ] = (uint8_t) 'x';
1115
1116
0
  do
1117
0
  {
1118
0
    byte_value = ( internal_posix_time->timestamp >> byte_shift ) & 0x0f;
1119
1120
0
    if( byte_value <= 9 )
1121
0
    {
1122
0
      utf8_string[ string_index++ ] = (uint8_t) '0' + byte_value;
1123
0
    }
1124
0
    else
1125
0
    {
1126
0
      utf8_string[ string_index++ ] = (uint8_t) 'a' + byte_value - 10;
1127
0
    }
1128
0
    byte_shift -= 4;
1129
0
  }
1130
0
  while( byte_shift >= 0 );
1131
1132
0
  utf8_string[ string_index++ ] = (uint8_t) ')';
1133
1134
0
  utf8_string[ string_index++ ] = 0;
1135
1136
0
  *utf8_string_index = string_index;
1137
1138
0
  return( 1 );
1139
0
}
1140
1141
/* Converts the POSIX time into an UTF-8 string
1142
 * The string size should include the end of string character
1143
 * Returns 1 if successful or -1 on error
1144
 */
1145
int libfdatetime_posix_time_copy_to_utf8_string(
1146
     libfdatetime_posix_time_t *posix_time,
1147
     uint8_t *utf8_string,
1148
     size_t utf8_string_size,
1149
     uint32_t string_format_flags,
1150
     libcerror_error_t **error )
1151
0
{
1152
0
  static char *function    = "libfdatetime_posix_time_copy_to_utf8_string";
1153
0
  size_t utf8_string_index = 0;
1154
1155
0
  if( libfdatetime_posix_time_copy_to_utf8_string_with_index(
1156
0
       posix_time,
1157
0
       utf8_string,
1158
0
       utf8_string_size,
1159
0
       &utf8_string_index,
1160
0
       string_format_flags,
1161
0
       error ) != 1 )
1162
0
  {
1163
0
    libcerror_error_set(
1164
0
     error,
1165
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1166
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
1167
0
     "%s: unable to copy POSIX time to UTF-8 string.",
1168
0
     function );
1169
1170
0
    return( -1 );
1171
0
  }
1172
0
  return( 1 );
1173
0
}
1174
1175
/* Converts the POSIX time into an UTF-8 string
1176
 * The string size should include the end of string character
1177
 * Returns 1 if successful or -1 on error
1178
 */
1179
int libfdatetime_posix_time_copy_to_utf8_string_with_index(
1180
     libfdatetime_posix_time_t *posix_time,
1181
     uint8_t *utf8_string,
1182
     size_t utf8_string_size,
1183
     size_t *utf8_string_index,
1184
     uint32_t string_format_flags,
1185
     libcerror_error_t **error )
1186
0
{
1187
0
  libfdatetime_date_time_values_t date_time_values;
1188
1189
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
1190
0
  static char *function                                   = "libfdatetime_posix_time_copy_to_utf8_string_with_index";
1191
0
  int result                                              = 0;
1192
1193
0
  if( posix_time == NULL )
1194
0
  {
1195
0
    libcerror_error_set(
1196
0
     error,
1197
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1198
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1199
0
     "%s: invalid POSIX time.",
1200
0
     function );
1201
1202
0
    return( -1 );
1203
0
  }
1204
0
  internal_posix_time = (libfdatetime_internal_posix_time_t *) posix_time;
1205
1206
0
  result = libfdatetime_internal_posix_time_copy_to_date_time_values(
1207
0
            internal_posix_time,
1208
0
            &date_time_values,
1209
0
            error );
1210
1211
0
  if( result != 1 )
1212
0
  {
1213
#if defined( HAVE_DEBUG_OUTPUT )
1214
    libcerror_error_set(
1215
     error,
1216
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1217
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1218
     "%s: unable to set date time values.",
1219
     function );
1220
1221
/* TODO debug print error */
1222
1223
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1224
1225
0
    if( ( error != NULL )
1226
0
     && ( *error != NULL ) )
1227
0
    {
1228
0
      libcerror_error_free(
1229
0
       error );
1230
0
    }
1231
0
  }
1232
0
  else
1233
0
  {
1234
0
    result = libfdatetime_date_time_values_copy_to_utf8_string_with_index(
1235
0
              &date_time_values,
1236
0
              utf8_string,
1237
0
              utf8_string_size,
1238
0
              utf8_string_index,
1239
0
              string_format_flags,
1240
0
              error );
1241
1242
0
    if( result == -1 )
1243
0
    {
1244
0
      libcerror_error_set(
1245
0
       error,
1246
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1247
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1248
0
       "%s: unable to copy date time values to UTF-8 string.",
1249
0
       function );
1250
1251
0
      return( -1 );
1252
0
    }
1253
0
  }
1254
0
  if( result != 1 )
1255
0
  {
1256
0
    result = libfdatetime_internal_posix_time_copy_to_utf8_string_in_hexadecimal(
1257
0
              internal_posix_time,
1258
0
              utf8_string,
1259
0
              utf8_string_size,
1260
0
              utf8_string_index,
1261
0
              error );
1262
1263
0
    if( result == -1 )
1264
0
    {
1265
0
      libcerror_error_set(
1266
0
       error,
1267
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1268
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1269
0
       "%s: unable to POSIX time to hexadecimal UTF-8 string.",
1270
0
       function );
1271
1272
0
      return( -1 );
1273
0
    }
1274
0
  }
1275
0
  return( 1 );
1276
0
}
1277
1278
/* Converts the POSIX time into an UTF-16 string in hexadecimal representation
1279
 * The string size should include the end of string character
1280
 * Returns 1 if successful or -1 on error
1281
 */
1282
int libfdatetime_internal_posix_time_copy_to_utf16_string_in_hexadecimal(
1283
     libfdatetime_internal_posix_time_t *internal_posix_time,
1284
     uint16_t *utf16_string,
1285
     size_t utf16_string_size,
1286
     size_t *utf16_string_index,
1287
     libcerror_error_t **error )
1288
0
{
1289
0
  static char *function = "libfdatetime_internal_posix_time_copy_to_utf16_string_in_hexadecimal";
1290
0
  size_t string_index   = 0;
1291
0
  size_t string_size    = 0;
1292
0
  uint8_t byte_value    = 0;
1293
0
  int8_t byte_shift     = 0;
1294
1295
0
  if( internal_posix_time == NULL )
1296
0
  {
1297
0
    libcerror_error_set(
1298
0
     error,
1299
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1300
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1301
0
     "%s: invalid POSIX time.",
1302
0
     function );
1303
1304
0
    return( -1 );
1305
0
  }
1306
0
  if( ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
1307
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED )
1308
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_SIGNED )
1309
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_UNSIGNED )
1310
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
1311
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
1312
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
1313
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
1314
0
  {
1315
0
    libcerror_error_set(
1316
0
     error,
1317
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1318
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1319
0
     "%s: invalid POSIX time - unsupported value type.",
1320
0
     function );
1321
1322
0
    return( -1 );
1323
0
  }
1324
0
  if( utf16_string == NULL )
1325
0
  {
1326
0
    libcerror_error_set(
1327
0
     error,
1328
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1329
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1330
0
     "%s: invalid UTF-16 string.",
1331
0
     function );
1332
1333
0
    return( -1 );
1334
0
  }
1335
0
  if( utf16_string_size > (size_t) SSIZE_MAX )
1336
0
  {
1337
0
    libcerror_error_set(
1338
0
     error,
1339
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1340
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1341
0
     "%s: invalid UTF-16 string size value exceeds maximum.",
1342
0
     function );
1343
1344
0
    return( -1 );
1345
0
  }
1346
0
  if( utf16_string_index == NULL )
1347
0
  {
1348
0
    libcerror_error_set(
1349
0
     error,
1350
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1351
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1352
0
     "%s: invalid UTF-16 string index.",
1353
0
     function );
1354
1355
0
    return( -1 );
1356
0
  }
1357
0
  if( ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
1358
0
   || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED ) )
1359
0
  {
1360
0
    string_size = 13;
1361
0
    byte_shift  = 28;
1362
0
  }
1363
0
  else
1364
0
  {
1365
0
    string_size = 21;
1366
0
    byte_shift  = 60;
1367
0
  }
1368
0
  if( ( utf16_string_size < string_size )
1369
0
   || ( *utf16_string_index > ( utf16_string_size - string_size ) ) )
1370
0
  {
1371
0
    libcerror_error_set(
1372
0
     error,
1373
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1374
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1375
0
     "%s: UTF-16 string is too small.",
1376
0
     function );
1377
1378
0
    return( -1 );
1379
0
  }
1380
0
  string_index = *utf16_string_index;
1381
1382
0
  utf16_string[ string_index++ ] = (uint16_t) '(';
1383
0
  utf16_string[ string_index++ ] = (uint16_t) '0';
1384
0
  utf16_string[ string_index++ ] = (uint16_t) 'x';
1385
1386
0
  do
1387
0
  {
1388
0
    byte_value = ( internal_posix_time->timestamp >> byte_shift ) & 0x0f;
1389
1390
0
    if( byte_value <= 9 )
1391
0
    {
1392
0
      utf16_string[ string_index++ ] = (uint16_t) '0' + byte_value;
1393
0
    }
1394
0
    else
1395
0
    {
1396
0
      utf16_string[ string_index++ ] = (uint16_t) 'a' + byte_value - 10;
1397
0
    }
1398
0
    byte_shift -= 4;
1399
0
  }
1400
0
  while( byte_shift >= 0 );
1401
1402
0
  utf16_string[ string_index++ ] = (uint16_t) ')';
1403
1404
0
  utf16_string[ string_index++ ] = 0;
1405
1406
0
  *utf16_string_index = string_index;
1407
1408
0
  return( 1 );
1409
0
}
1410
1411
/* Converts the POSIX time into an UTF-16 string
1412
 * The string size should include the end of string character
1413
 * Returns 1 if successful or -1 on error
1414
 */
1415
int libfdatetime_posix_time_copy_to_utf16_string(
1416
     libfdatetime_posix_time_t *posix_time,
1417
     uint16_t *utf16_string,
1418
     size_t utf16_string_size,
1419
     uint32_t string_format_flags,
1420
     libcerror_error_t **error )
1421
0
{
1422
0
  static char *function     = "libfdatetime_posix_time_copy_to_utf16_string";
1423
0
  size_t utf16_string_index = 0;
1424
1425
0
  if( libfdatetime_posix_time_copy_to_utf16_string_with_index(
1426
0
       posix_time,
1427
0
       utf16_string,
1428
0
       utf16_string_size,
1429
0
       &utf16_string_index,
1430
0
       string_format_flags,
1431
0
       error ) != 1 )
1432
0
  {
1433
0
    libcerror_error_set(
1434
0
     error,
1435
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1436
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
1437
0
     "%s: unable to copy POSIX time to UTF-16 string.",
1438
0
     function );
1439
1440
0
    return( -1 );
1441
0
  }
1442
0
  return( 1 );
1443
0
}
1444
1445
/* Converts the POSIX time into an UTF-16 string
1446
 * The string size should include the end of string character
1447
 * Returns 1 if successful or -1 on error
1448
 */
1449
int libfdatetime_posix_time_copy_to_utf16_string_with_index(
1450
     libfdatetime_posix_time_t *posix_time,
1451
     uint16_t *utf16_string,
1452
     size_t utf16_string_size,
1453
     size_t *utf16_string_index,
1454
     uint32_t string_format_flags,
1455
     libcerror_error_t **error )
1456
0
{
1457
0
  libfdatetime_date_time_values_t date_time_values;
1458
1459
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
1460
0
  static char *function                                   = "libfdatetime_posix_time_copy_to_utf16_string_with_index";
1461
0
  int result                                              = 0;
1462
1463
0
  if( posix_time == NULL )
1464
0
  {
1465
0
    libcerror_error_set(
1466
0
     error,
1467
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1468
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1469
0
     "%s: invalid POSIX time.",
1470
0
     function );
1471
1472
0
    return( -1 );
1473
0
  }
1474
0
  internal_posix_time = (libfdatetime_internal_posix_time_t *) posix_time;
1475
1476
0
  result = libfdatetime_internal_posix_time_copy_to_date_time_values(
1477
0
            internal_posix_time,
1478
0
            &date_time_values,
1479
0
            error );
1480
1481
0
  if( result != 1 )
1482
0
  {
1483
#if defined( HAVE_DEBUG_OUTPUT )
1484
    libcerror_error_set(
1485
     error,
1486
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1487
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1488
     "%s: unable to set date time values.",
1489
     function );
1490
1491
/* TODO debug print error */
1492
1493
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1494
1495
0
    if( ( error != NULL )
1496
0
     && ( *error != NULL ) )
1497
0
    {
1498
0
      libcerror_error_free(
1499
0
       error );
1500
0
    }
1501
0
  }
1502
0
  else
1503
0
  {
1504
0
    result = libfdatetime_date_time_values_copy_to_utf16_string_with_index(
1505
0
              &date_time_values,
1506
0
              utf16_string,
1507
0
              utf16_string_size,
1508
0
              utf16_string_index,
1509
0
              string_format_flags,
1510
0
              error );
1511
1512
0
    if( result == -1 )
1513
0
    {
1514
0
      libcerror_error_set(
1515
0
       error,
1516
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1517
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1518
0
       "%s: unable to copy date time values to UTF-16 string.",
1519
0
       function );
1520
1521
0
      return( -1 );
1522
0
    }
1523
0
  }
1524
0
  if( result != 1 )
1525
0
  {
1526
0
    result = libfdatetime_internal_posix_time_copy_to_utf16_string_in_hexadecimal(
1527
0
              internal_posix_time,
1528
0
              utf16_string,
1529
0
              utf16_string_size,
1530
0
              utf16_string_index,
1531
0
              error );
1532
1533
0
    if( result == -1 )
1534
0
    {
1535
0
      libcerror_error_set(
1536
0
       error,
1537
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1538
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1539
0
       "%s: unable to POSIX time to hexadecimal UTF-16 string.",
1540
0
       function );
1541
1542
0
      return( -1 );
1543
0
    }
1544
0
  }
1545
0
  return( 1 );
1546
0
}
1547
1548
/* Converts the POSIX time into an UTF-32 string in hexadecimal representation
1549
 * The string size should include the end of string character
1550
 * Returns 1 if successful or -1 on error
1551
 */
1552
int libfdatetime_internal_posix_time_copy_to_utf32_string_in_hexadecimal(
1553
     libfdatetime_internal_posix_time_t *internal_posix_time,
1554
     uint32_t *utf32_string,
1555
     size_t utf32_string_size,
1556
     size_t *utf32_string_index,
1557
     libcerror_error_t **error )
1558
0
{
1559
0
  static char *function = "libfdatetime_internal_posix_time_copy_to_utf32_string_in_hexadecimal";
1560
0
  size_t string_index   = 0;
1561
0
  size_t string_size    = 0;
1562
0
  uint8_t byte_value    = 0;
1563
0
  int8_t byte_shift     = 0;
1564
1565
0
  if( internal_posix_time == NULL )
1566
0
  {
1567
0
    libcerror_error_set(
1568
0
     error,
1569
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1570
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1571
0
     "%s: invalid POSIX time.",
1572
0
     function );
1573
1574
0
    return( -1 );
1575
0
  }
1576
0
  if( ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
1577
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED )
1578
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_SIGNED )
1579
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_64BIT_UNSIGNED )
1580
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_SIGNED )
1581
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_MICRO_SECONDS_64BIT_UNSIGNED )
1582
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_SIGNED )
1583
0
   && ( internal_posix_time->value_type != LIBFDATETIME_POSIX_TIME_VALUE_TYPE_NANO_SECONDS_64BIT_UNSIGNED ) )
1584
0
  {
1585
0
    libcerror_error_set(
1586
0
     error,
1587
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1588
0
     LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
1589
0
     "%s: invalid POSIX time - unsupported value type.",
1590
0
     function );
1591
1592
0
    return( -1 );
1593
0
  }
1594
0
  if( utf32_string == NULL )
1595
0
  {
1596
0
    libcerror_error_set(
1597
0
     error,
1598
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1599
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1600
0
     "%s: invalid UTF-32 string.",
1601
0
     function );
1602
1603
0
    return( -1 );
1604
0
  }
1605
0
  if( utf32_string_size > (size_t) SSIZE_MAX )
1606
0
  {
1607
0
    libcerror_error_set(
1608
0
     error,
1609
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1610
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1611
0
     "%s: invalid UTF-32 string size value exceeds maximum.",
1612
0
     function );
1613
1614
0
    return( -1 );
1615
0
  }
1616
0
  if( utf32_string_index == NULL )
1617
0
  {
1618
0
    libcerror_error_set(
1619
0
     error,
1620
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1621
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1622
0
     "%s: invalid UTF-32 string index.",
1623
0
     function );
1624
1625
0
    return( -1 );
1626
0
  }
1627
0
  if( ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_SIGNED )
1628
0
   || ( internal_posix_time->value_type == LIBFDATETIME_POSIX_TIME_VALUE_TYPE_SECONDS_32BIT_UNSIGNED ) )
1629
0
  {
1630
0
    string_size = 13;
1631
0
    byte_shift  = 28;
1632
0
  }
1633
0
  else
1634
0
  {
1635
0
    string_size = 21;
1636
0
    byte_shift  = 60;
1637
0
  }
1638
0
  if( ( utf32_string_size < string_size )
1639
0
   || ( *utf32_string_index > ( utf32_string_size - string_size ) ) )
1640
0
  {
1641
0
    libcerror_error_set(
1642
0
     error,
1643
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1644
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_TOO_SMALL,
1645
0
     "%s: UTF-32 string is too small.",
1646
0
     function );
1647
1648
0
    return( -1 );
1649
0
  }
1650
0
  string_index = *utf32_string_index;
1651
1652
0
  utf32_string[ string_index++ ] = (uint32_t) '(';
1653
0
  utf32_string[ string_index++ ] = (uint32_t) '0';
1654
0
  utf32_string[ string_index++ ] = (uint32_t) 'x';
1655
1656
0
  do
1657
0
  {
1658
0
    byte_value = ( internal_posix_time->timestamp >> byte_shift ) & 0x0f;
1659
1660
0
    if( byte_value <= 9 )
1661
0
    {
1662
0
      utf32_string[ string_index++ ] = (uint32_t) '0' + byte_value;
1663
0
    }
1664
0
    else
1665
0
    {
1666
0
      utf32_string[ string_index++ ] = (uint32_t) 'a' + byte_value - 10;
1667
0
    }
1668
0
    byte_shift -= 4;
1669
0
  }
1670
0
  while( byte_shift >= 0 );
1671
1672
0
  utf32_string[ string_index++ ] = (uint32_t) ')';
1673
1674
0
  utf32_string[ string_index++ ] = 0;
1675
1676
0
  *utf32_string_index = string_index;
1677
1678
0
  return( 1 );
1679
0
}
1680
1681
/* Converts the POSIX time into an UTF-32 string
1682
 * The string size should include the end of string character
1683
 * Returns 1 if successful or -1 on error
1684
 */
1685
int libfdatetime_posix_time_copy_to_utf32_string(
1686
     libfdatetime_posix_time_t *posix_time,
1687
     uint32_t *utf32_string,
1688
     size_t utf32_string_size,
1689
     uint32_t string_format_flags,
1690
     libcerror_error_t **error )
1691
0
{
1692
0
  static char *function     = "libfdatetime_posix_time_copy_to_utf32_string";
1693
0
  size_t utf32_string_index = 0;
1694
1695
0
  if( libfdatetime_posix_time_copy_to_utf32_string_with_index(
1696
0
       posix_time,
1697
0
       utf32_string,
1698
0
       utf32_string_size,
1699
0
       &utf32_string_index,
1700
0
       string_format_flags,
1701
0
       error ) != 1 )
1702
0
  {
1703
0
    libcerror_error_set(
1704
0
     error,
1705
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1706
0
     LIBCERROR_RUNTIME_ERROR_COPY_FAILED,
1707
0
     "%s: unable to copy POSIX time to UTF-32 string.",
1708
0
     function );
1709
1710
0
    return( -1 );
1711
0
  }
1712
0
  return( 1 );
1713
0
}
1714
1715
/* Converts the POSIX time into an UTF-32 string
1716
 * The string size should include the end of string character
1717
 * Returns 1 if successful or -1 on error
1718
 */
1719
int libfdatetime_posix_time_copy_to_utf32_string_with_index(
1720
     libfdatetime_posix_time_t *posix_time,
1721
     uint32_t *utf32_string,
1722
     size_t utf32_string_size,
1723
     size_t *utf32_string_index,
1724
     uint32_t string_format_flags,
1725
     libcerror_error_t **error )
1726
0
{
1727
0
  libfdatetime_date_time_values_t date_time_values;
1728
1729
0
  libfdatetime_internal_posix_time_t *internal_posix_time = NULL;
1730
0
  static char *function                                   = "libfdatetime_posix_time_copy_to_utf32_string_with_index";
1731
0
  int result                                              = 0;
1732
1733
0
  if( posix_time == NULL )
1734
0
  {
1735
0
    libcerror_error_set(
1736
0
     error,
1737
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1738
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1739
0
     "%s: invalid POSIX time.",
1740
0
     function );
1741
1742
0
    return( -1 );
1743
0
  }
1744
0
  internal_posix_time = (libfdatetime_internal_posix_time_t *) posix_time;
1745
1746
0
  result = libfdatetime_internal_posix_time_copy_to_date_time_values(
1747
0
            internal_posix_time,
1748
0
            &date_time_values,
1749
0
            error );
1750
1751
0
  if( result != 1 )
1752
0
  {
1753
#if defined( HAVE_DEBUG_OUTPUT )
1754
    libcerror_error_set(
1755
     error,
1756
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
1757
     LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1758
     "%s: unable to set date time values.",
1759
     function );
1760
1761
/* TODO debug print error */
1762
1763
#endif /* defined( HAVE_DEBUG_OUTPUT ) */
1764
1765
0
    if( ( error != NULL )
1766
0
     && ( *error != NULL ) )
1767
0
    {
1768
0
      libcerror_error_free(
1769
0
       error );
1770
0
    }
1771
0
  }
1772
0
  else
1773
0
  {
1774
0
    result = libfdatetime_date_time_values_copy_to_utf32_string_with_index(
1775
0
              &date_time_values,
1776
0
              utf32_string,
1777
0
              utf32_string_size,
1778
0
              utf32_string_index,
1779
0
              string_format_flags,
1780
0
              error );
1781
1782
0
    if( result == -1 )
1783
0
    {
1784
0
      libcerror_error_set(
1785
0
       error,
1786
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1787
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1788
0
       "%s: unable to set UTF-32 string.",
1789
0
       function );
1790
1791
0
      return( -1 );
1792
0
    }
1793
0
  }
1794
0
  if( result != 1 )
1795
0
  {
1796
0
    if( utf32_string == NULL )
1797
0
    {
1798
0
      libcerror_error_set(
1799
0
       error,
1800
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1801
0
       LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1802
0
       "%s: invalid UTF-32 string.",
1803
0
       function );
1804
1805
0
      return( -1 );
1806
0
    }
1807
0
    if( utf32_string_size > (size_t) SSIZE_MAX )
1808
0
    {
1809
0
      libcerror_error_set(
1810
0
       error,
1811
0
       LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1812
0
       LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
1813
0
       "%s: unable to copy date time values to UTF-32 string.",
1814
0
       function );
1815
1816
0
      return( -1 );
1817
0
    }
1818
0
  }
1819
0
  if( result != 1 )
1820
0
  {
1821
0
    result = libfdatetime_internal_posix_time_copy_to_utf32_string_in_hexadecimal(
1822
0
              internal_posix_time,
1823
0
              utf32_string,
1824
0
              utf32_string_size,
1825
0
              utf32_string_index,
1826
0
              error );
1827
1828
0
    if( result == -1 )
1829
0
    {
1830
0
      libcerror_error_set(
1831
0
       error,
1832
0
       LIBCERROR_ERROR_DOMAIN_RUNTIME,
1833
0
       LIBCERROR_RUNTIME_ERROR_SET_FAILED,
1834
0
       "%s: unable to POSIX time to hexadecimal UTF-32 string.",
1835
0
       function );
1836
1837
0
      return( -1 );
1838
0
    }
1839
0
  }
1840
0
  return( 1 );
1841
0
}
1842