Debian Codegraph @

biosig4c++/t240/GeneralizedTime.c

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