Coverage Report

Created: 2026-12-01 06:43

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libiec61850/src/mms/iso_mms/asn1c/GeneralizedTime.c
Line
Count
Source
1
/*-
2
 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3
 * Redistribution and modifications are permitted subject to BSD license.
4
 */
5
#define _POSIX_PTHREAD_SEMANTICS  /* for Sun */
6
#define _REENTRANT      /* for Sun */
7
#include <asn_internal.h>
8
#include <GeneralizedTime.h>
9
#include <errno.h>
10
11
#ifdef  __CYGWIN__
12
#include "/usr/include/time.h"
13
#else
14
#include <time.h>
15
#endif  /* __CYGWIN__ */
16
17
#ifdef __IAR_SYSTEMS_ICC__
18
19
static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
20
    struct tm *tm;
21
    if((tm = localtime(tloc)))
22
        return memcpy(result, tm, sizeof(struct tm));
23
    return 0;
24
}
25
26
static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
27
    struct tm *tm;
28
    if((tm = gmtime(tloc)))
29
        return memcpy(result, tm, sizeof(struct tm));
30
    return 0;
31
}
32
#endif /* __IAR_SYSTEMS_ICC__ */
33
34
#if defined(WIN32)
35
36
#ifdef __GNUC__
37
38
static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
39
  struct tm *tm;
40
  if((tm = localtime(tloc)))
41
    return memcpy(result, tm, sizeof(struct tm));
42
  return 0;
43
}
44
45
static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
46
  struct tm *tm;
47
  if((tm = gmtime(tloc)))
48
    return memcpy(result, tm, sizeof(struct tm));
49
  return 0;
50
}
51
52
#else
53
54
int
55
localtime_s(struct tm* _tm, const time_t *time);
56
57
int
58
gmtime_s(struct tm* _tm, const time_t* time);
59
60
61
62
static struct tm*
63
localtime_r(const time_t *tloc, struct tm *result)
64
{
65
66
    if(localtime_s(result, tloc) == 0)
67
        return result;
68
    else
69
        return 0;
70
}
71
72
static struct tm*
73
gmtime_r(const time_t *tloc, struct tm *result)
74
{
75
    if(gmtime_s(result, tloc) == 0)
76
        return result;
77
    else
78
        return 0;
79
}
80
#endif /* __GNUC__ */
81
82
#define tzset() _tzset()
83
#define putenv(c) _putenv(c)
84
#define _EMULATE_TIMEGM
85
86
#endif  /* WIN32 */
87
88
#if defined(sun) || defined(_sun_) || defined(__solaris__)
89
#define _EMULATE_TIMEGM
90
#endif
91
92
/*
93
 * Where to look for offset from GMT, Phase I.
94
 * Several platforms are known.
95
 */
96
#if defined(__FreeBSD__)        \
97
  || (defined(__GNUC__) && defined(__APPLE_CC__)) \
98
  || (defined __GLIBC__ && __GLIBC__ >= 2)
99
#undef  HAVE_TM_GMTOFF
100
#define HAVE_TM_GMTOFF
101
#endif  /* BSDs and newer glibc */
102
103
/*
104
 * Where to look for offset from GMT, Phase II.
105
 */
106
#ifdef  HAVE_TM_GMTOFF
107
#define GMTOFF(tm)  ((tm).tm_gmtoff)
108
#else /* HAVE_TM_GMTOFF */
109
#define GMTOFF(tm)  (-timezone)
110
#endif  /* HAVE_TM_GMTOFF */
111
112
/*
113
 * Override our GMTOFF decision for other known platforms.
114
 */
115
#ifdef __CYGWIN__
116
#undef  GMTOFF
117
static long GMTOFF(struct tm a){
118
  struct tm *lt;
119
  time_t local_time, gmt_time;
120
  long zone;
121
122
  tzset();
123
  gmt_time = time (NULL);
124
125
  lt = gmtime(&gmt_time);
126
127
  local_time = mktime(lt);
128
  return (gmt_time - local_time);
129
}
130
#define _EMULATE_TIMEGM
131
132
#endif  /* __CYGWIN__ */
133
134
#define ATZVARS do {              \
135
  char tzoldbuf[64];            \
136
  char *tzold
137
#define ATZSAVETZ do {              \
138
  tzold = getenv("TZ");           \
139
  if(tzold) {             \
140
    size_t tzlen = strlen(tzold);       \
141
    if(tzlen < sizeof(tzoldbuf))        \
142
      tzold = memcpy(tzoldbuf, tzold, tzlen + 1); \
143
    else              \
144
      tzold = strdup(tzold);  /* Ignore error */  \
145
    setenv("TZ", "UTC", 1);         \
146
  }               \
147
  tzset();              \
148
} while(0)
149
#define ATZOLDTZ do {             \
150
  if (tzold) {              \
151
    setenv("TZ", tzold, 1);         \
152
    *tzoldbuf = 0;            \
153
    if(tzold != tzoldbuf)         \
154
      FREEMEM(tzold);         \
155
  } else {              \
156
    unsetenv("TZ");           \
157
  }               \
158
  tzset();              \
159
} while(0); } while(0);
160
161
#ifdef  _EMULATE_TIMEGM
162
static time_t timegm(struct tm *tm) {
163
  time_t tloc;
164
  tloc = mktime(tm);
165
  return tloc;
166
}
167
#endif  /* _EMULATE_TIMEGM */
168
169
170
#ifndef __ASN_INTERNAL_TEST_MODE__
171
172
/*
173
 * GeneralizedTime basic type description.
174
 */
175
static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
176
  (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)),  /* [UNIVERSAL 24] IMPLICIT ...*/
177
  (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)),  /* [UNIVERSAL 26] IMPLICIT ...*/
178
  (ASN_TAG_CLASS_UNIVERSAL | (4 << 2))    /* ... OCTET STRING */
179
};
180
asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
181
  "GeneralizedTime",
182
  "GeneralizedTime",
183
  OCTET_STRING_free,
184
  GeneralizedTime_print,
185
  GeneralizedTime_constraint, /* Check validity of time */
186
  OCTET_STRING_decode_ber,    /* Implemented in terms of OCTET STRING */
187
  GeneralizedTime_encode_der,
188
  OCTET_STRING_decode_xer_utf8,
189
  GeneralizedTime_encode_xer,
190
  0, 0,
191
  0, /* Use generic outmost tag fetcher */
192
  asn_DEF_GeneralizedTime_tags,
193
  sizeof(asn_DEF_GeneralizedTime_tags)
194
    / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
195
  asn_DEF_GeneralizedTime_tags,
196
  sizeof(asn_DEF_GeneralizedTime_tags)
197
    / sizeof(asn_DEF_GeneralizedTime_tags[0]),
198
  0,  /* No PER visible constraints */
199
  0, 0, /* No members */
200
  0 /* No specifics */
201
};
202
203
#endif  /* __ASN_INTERNAL_TEST_MODE__ */
204
205
/*
206
 * Check that the time looks like the time.
207
 */
208
int
209
GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
210
0
    asn_app_constraint_failed_f *ctfailcb, void *app_key) {
211
0
  const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
212
0
  time_t tloc;
213
214
0
  errno = EPERM;      /* Just an unlikely error code */
215
0
  tloc = asn_GT2time(st, 0, 0);
216
0
  if(tloc == -1 && errno != EPERM) {
217
0
    _ASN_CTFAIL(app_key, td,
218
0
      "%s: Invalid time format: %s (%s:%d)",
219
0
      td->name, strerror(errno), __FILE__, __LINE__);
220
0
    return -1;
221
0
  }
222
223
0
  return 0;
224
0
}
225
226
asn_enc_rval_t
227
GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
228
  int tag_mode, ber_tlv_tag_t tag,
229
0
  asn_app_consume_bytes_f *cb, void *app_key) {
230
0
  GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
231
0
  asn_enc_rval_t erval;
232
0
  int fv, fd; /* seconds fraction value and number of digits */
233
0
  struct tm tm;
234
0
  time_t tloc;
235
236
  /*
237
   * Encode as a canonical DER.
238
   */
239
0
  errno = EPERM;
240
0
  tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1);  /* Recognize time */
241
0
  if(tloc == -1 && errno != EPERM)
242
    /* Failed to recognize time. Fail completely. */
243
0
    _ASN_ENCODE_FAILED;
244
245
0
  if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
246
247
0
  erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
248
249
0
  return erval;
250
0
}
251
252
#ifndef __ASN_INTERNAL_TEST_MODE__
253
254
asn_enc_rval_t
255
GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
256
  int ilevel, enum xer_encoder_flags_e flags,
257
0
    asn_app_consume_bytes_f *cb, void *app_key) {
258
259
0
  if(flags & XER_F_CANONICAL) {
260
0
    GeneralizedTime_t *gt = NULL; /* modified MZ */
261
0
    asn_enc_rval_t rv;
262
0
    int fv, fd;   /* fractional parts */
263
0
    struct tm tm;
264
265
0
    errno = EPERM;
266
0
    if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
267
0
          &fv, &fd, &tm, 1) == -1
268
0
        && errno != EPERM)
269
0
      _ASN_ENCODE_FAILED;
270
271
#if 0
272
    gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
273
#endif
274
0
    if(!gt) _ASN_ENCODE_FAILED;
275
  
276
0
    rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
277
0
      cb, app_key);
278
0
    ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
279
0
    return rv;
280
0
  } else {
281
0
    return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
282
0
      cb, app_key);
283
0
  }
284
0
}
285
286
#endif  /* __ASN_INTERNAL_TEST_MODE__ */
287
288
int
289
GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
290
0
  asn_app_consume_bytes_f *cb, void *app_key) {
291
0
  const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
292
293
0
  (void)td; /* Unused argument */
294
0
  (void)ilevel; /* Unused argument */
295
296
0
  if(st && st->buf) {
297
0
    char buf[32];
298
0
    struct tm tm;
299
0
    int ret;
300
301
0
    errno = EPERM;
302
0
    if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
303
0
      return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
304
305
0
    ret = snprintf(buf, sizeof(buf),
306
0
      "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
307
0
      tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
308
0
      tm.tm_hour, tm.tm_min, tm.tm_sec);
309
0
    assert(ret > 0 && ret < (int)sizeof(buf));
310
0
    return (cb(buf, ret, app_key) < 0) ? -1 : 0;
311
0
  } else {
312
0
    return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
313
0
  }
314
0
}
315
316
time_t
317
0
asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
318
0
  return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
319
0
}
320
321
time_t
322
0
asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
323
0
  time_t tloc;
324
0
  int fv, fd = 0;
325
326
0
  if(frac_value)
327
0
    tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
328
0
  else
329
0
    return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
330
0
  if(fd == 0 || frac_digits <= 0) {
331
0
    *frac_value = 0;
332
0
  } else {
333
0
    while(fd > frac_digits)
334
0
      fv /= 10, fd--;
335
0
    while(fd < frac_digits) {
336
0
      int new_fv = fv * 10;
337
0
      if(new_fv / 10 != fv) {
338
        /* Too long precision request */
339
0
        fv = 0;
340
0
        break;
341
0
      }
342
0
      fv = new_fv, fd++;
343
0
    }
344
345
0
    *frac_value = fv;
346
0
  }
347
348
0
  return tloc;
349
0
}
350
351
time_t
352
0
asn_GT2time_frac(const GeneralizedTime_t *st, int *frac_value, int *frac_digits, struct tm *ret_tm, int as_gmt) {
353
0
  struct tm tm_s;
354
0
  uint8_t *buf;
355
0
  uint8_t *end;
356
0
  int gmtoff_h = 0;
357
0
  int gmtoff_m = 0;
358
0
  int gmtoff = 0; /* h + m */
359
0
  int offset_specified = 0;
360
0
  int fvalue = 0;
361
0
  int fdigits = 0;
362
0
  time_t tloc = 0;
363
364
0
  if(!st || !st->buf) {
365
0
    errno = EINVAL;
366
0
    return -1;
367
0
  } else {
368
0
    buf = st->buf;
369
0
    end = buf + st->size;
370
0
  }
371
372
0
  if(st->size < 10) {
373
0
    errno = EINVAL;
374
0
    return -1;
375
0
  }
376
377
  /*
378
   * Decode first 10 bytes: "AAAAMMJJhh"
379
   */
380
0
  memset(&tm_s, 0, sizeof(tm_s));
381
0
#undef  B2F
382
0
#undef  B2T
383
0
#define B2F(var)  do {         \
384
0
    unsigned ch = *buf;       \
385
0
    if(ch < 0x30 || ch > 0x39) {     \
386
0
      errno = EINVAL;       \
387
0
      return -1;        \
388
0
    } else {         \
389
0
      var = var * 10 + (ch - 0x30);   \
390
0
      buf++;          \
391
0
    }            \
392
0
  } while(0)
393
0
#define B2T(var)  B2F(tm_s.var)
394
395
0
  B2T(tm_year);  /* 1: A */
396
0
  B2T(tm_year); /* 2: A */
397
0
  B2T(tm_year); /* 3: A */
398
0
  B2T(tm_year); /* 4: A */
399
0
  B2T(tm_mon);  /* 5: M */
400
0
  B2T(tm_mon);  /* 6: M */
401
0
  B2T(tm_mday); /* 7: J */
402
0
  B2T(tm_mday); /* 8: J */
403
0
  B2T(tm_hour); /* 9: h */
404
0
  B2T(tm_hour); /* 0: h */
405
406
0
  if(buf == end) goto local_finish;
407
408
  /*
409
   * Parse [mm[ss[(.|,)ffff]]]
410
   *        ^^
411
   */
412
0
  switch(*buf) {
413
0
  case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
414
0
  case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
415
0
    tm_s.tm_min = (*buf++) - 0x30;
416
0
    if(buf == end) { errno = EINVAL; return -1; }
417
0
    B2T(tm_min);
418
0
    break;
419
0
  case 0x2B: case 0x2D:  /* +, - */
420
0
    goto offset;
421
0
  case 0x5A:    /* Z */
422
0
    goto utc_finish;
423
0
  default:
424
0
    errno = EINVAL;
425
0
    return -1;
426
0
  }
427
428
0
  if(buf == end) goto local_finish;
429
430
  /*
431
   * Parse [mm[ss[(.|,)ffff]]]
432
   *           ^^
433
   */
434
0
  switch(*buf) {
435
0
  case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
436
0
  case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
437
0
    tm_s.tm_sec = (*buf++) - 0x30;
438
0
    if(buf == end) { errno = EINVAL; return -1; }
439
0
    B2T(tm_sec);
440
0
    break;
441
0
  case 0x2B: case 0x2D:  /* +, - */
442
0
    goto offset;
443
0
  case 0x5A:    /* Z */
444
0
    goto utc_finish;
445
0
  default:
446
0
    errno = EINVAL;
447
0
    return -1;
448
0
  }
449
450
0
  if(buf == end) goto local_finish;
451
452
  /*
453
   * Parse [mm[ss[(.|,)ffff]]]
454
   *               ^ ^
455
   */
456
0
  switch(*buf) {
457
0
  case 0x2C: case 0x2E: /* (.|,) */
458
    /*
459
     * Process fractions of seconds.
460
     */
461
0
    for(buf++; buf < end; buf++) {
462
0
      int v = *buf;
463
0
      int new_fvalue;
464
0
      switch(v) {
465
0
      case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
466
0
      case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
467
0
        new_fvalue = fvalue * 10 + (v - 0x30);
468
0
        if(new_fvalue / 10 != fvalue) {
469
          /* Not enough precision, ignore */
470
0
        } else {
471
0
          fvalue = new_fvalue;
472
0
          fdigits++;
473
0
        }
474
0
        continue;
475
0
      default:
476
0
        break;
477
0
      }
478
0
      break;
479
0
    }
480
0
  }
481
482
0
  if(buf == end) goto local_finish;
483
484
0
  switch(*buf) {
485
0
  case 0x2B: case 0x2D:  /* +, - */
486
0
    goto offset;
487
0
  case 0x5A:    /* Z */
488
0
    goto utc_finish;
489
0
  default:
490
0
    errno = EINVAL;
491
0
    return -1;
492
0
  }
493
494
495
0
offset:
496
497
0
  if(end - buf < 3) {
498
0
    errno = EINVAL;
499
0
    return -1;
500
0
  }
501
0
  buf++;
502
0
  B2F(gmtoff_h);
503
0
  B2F(gmtoff_h);
504
0
  if(buf[-3] == 0x2D) /* Negative */
505
0
    gmtoff = -1;
506
0
  else
507
0
    gmtoff = 1;
508
509
0
  if((end - buf) == 2) {
510
0
    B2F(gmtoff_m);
511
0
    B2F(gmtoff_m);
512
0
  } else if(end != buf) {
513
0
    errno = EINVAL;
514
0
    return -1;
515
0
  }
516
517
0
  gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
518
519
  /* Fall through */
520
0
utc_finish:
521
522
0
  offset_specified = 1;
523
524
  /* Fall through */
525
0
local_finish:
526
527
  /*
528
   * Validation.
529
   */
530
0
  if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
531
0
  || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
532
0
  || (tm_s.tm_hour > 23)
533
0
  || (tm_s.tm_sec > 60)
534
0
  ) {
535
0
    errno = EINVAL;
536
0
    return -1;
537
0
  }
538
539
  /* Canonicalize */
540
0
  tm_s.tm_mon -= 1; /* 0 - 11 */
541
0
  tm_s.tm_year -= 1900;
542
0
  tm_s.tm_isdst = -1;
543
544
0
  tm_s.tm_sec -= gmtoff;
545
546
  /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
547
548
0
  if(offset_specified) {
549
    /* tloc = timegm(&tm_s); */
550
0
  } else {
551
    /*
552
     * Without an offset (or "Z"),
553
     * we can only guess that it is a local zone.
554
     * Interpret it in this fashion.
555
     */
556
0
    tloc = mktime(&tm_s);
557
0
  }
558
0
  if(tloc == -1) {
559
0
    errno = EINVAL;
560
0
    return -1;
561
0
  }
562
563
0
  if(ret_tm) {
564
0
    if(as_gmt) {
565
0
      if(offset_specified) {
566
0
        *ret_tm = tm_s;
567
0
      } else {
568
0
        if(gmtime_r(&tloc, ret_tm) == 0) {
569
0
          errno = EINVAL;
570
0
          return -1;
571
0
        }
572
0
      }
573
0
    } else {
574
0
      if(localtime_r(&tloc, ret_tm) == 0) {
575
0
        errno = EINVAL;
576
0
        return -1;
577
0
      }
578
0
    }
579
0
  }
580
581
  /* Fractions of seconds */
582
0
  if(frac_value) *frac_value = fvalue;
583
0
  if(frac_digits) *frac_digits = fdigits;
584
585
0
  return tloc;
586
0
}
587
588
#if 0
589
590
GeneralizedTime_t *
591
asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
592
  return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
593
}
594
595
GeneralizedTime_t *
596
asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, int frac_value, int frac_digits, int force_gmt) {
597
  struct tm tm_s;
598
  long gmtoff;
599
  const unsigned int buf_size =
600
    4 + 2 + 2 /* yyyymmdd */
601
    + 2 + 2 + 2 /* hhmmss */
602
    + 1 + 6   /* .ffffff */
603
    + 1 + 4   /* +hhmm */
604
    + 1   /* '\0' */
605
    ;
606
  char *buf;
607
  char *p;
608
  int size;
609
610
  /* Check arguments */
611
  if(!tm) {
612
    errno = EINVAL;
613
    return 0;
614
  }
615
616
  /* Pre-allocate a buffer of sufficient yet small length */
617
  buf = (char *)MALLOC(buf_size);
618
  if(!buf) return 0;
619
620
  gmtoff = GMTOFF(*tm);
621
622
  if(force_gmt && gmtoff) {
623
    tm_s = *tm;
624
    tm_s.tm_sec -= gmtoff;
625
    timegm(&tm_s);  /* Fix the time */
626
    tm = &tm_s;
627
#ifdef  HAVE_TM_GMTOFF
628
    assert(!GMTOFF(tm_s));  /* Will fix itself */
629
#else /* !HAVE_TM_GMTOFF */
630
    gmtoff = 0;
631
#endif
632
  }
633
634
  size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
635
    tm->tm_year + 1900,
636
    tm->tm_mon + 1,
637
    tm->tm_mday,
638
    tm->tm_hour,
639
    tm->tm_min,
640
    tm->tm_sec
641
  );
642
  if(size != 14) {
643
    /* Could be assert(size == 14); */
644
    FREEMEM(buf);
645
    errno = EINVAL;
646
    return 0;
647
  }
648
649
  p = buf + size;
650
651
  /*
652
   * Deal with fractions.
653
   */
654
  if(frac_value > 0 && frac_digits > 0) {
655
    char *end = p + 1 + 6;  /* '.' + maximum 6 digits */
656
    char *z = p;
657
    long fbase;
658
    *z++ = '.';
659
660
    /* Place bounds on precision */
661
    while(frac_digits-- > 6)
662
      frac_value /= 10;
663
664
    /* emulate fbase = pow(10, frac_digits) */
665
    for(fbase = 1; frac_digits--;)
666
      fbase *= 10;
667
668
    do {
669
      int digit = frac_value / fbase;
670
      if(digit > 9) { z = 0; break; }
671
      *z++ = digit + 0x30;
672
      frac_value %= fbase;
673
      fbase /= 10;
674
    } while(fbase > 0 && frac_value > 0 && z < end);
675
    if(z) {
676
      for(--z; *z == 0x30; --z);  /* Strip zeroes */
677
      p = z + (*z != '.');
678
      size = p - buf;
679
    }
680
  }
681
682
  if(force_gmt) {
683
    *p++ = 0x5a;  /* "Z" */
684
    *p++ = 0;
685
    size++;
686
  } else {
687
    int ret;
688
    gmtoff %= 86400;
689
    ret = snprintf(p, buf_size - size, "%+03ld%02ld",
690
      gmtoff / 3600, labs(gmtoff % 3600));
691
    if(ret != 5) {
692
      FREEMEM(buf);
693
      errno = EINVAL;
694
      return 0;
695
    }
696
    size += ret;
697
  }
698
699
  if(opt_gt) {
700
    if(opt_gt->buf)
701
      FREEMEM(opt_gt->buf);
702
  } else {
703
    opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
704
    if(!opt_gt) { FREEMEM(buf); return 0; }
705
  }
706
707
  opt_gt->buf = (unsigned char *)buf;
708
  opt_gt->size = size;
709
710
  return opt_gt;
711
}
712
713
#endif