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