blob: 51070406600dda31cc99796eca6fbb120c62d8e8 [file] [log] [blame]
vlmfa67ddc2004-06-03 03:38:44 +00001/*-
2 * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved.
3 * Redistribution and modifications are permitted subject to BSD license.
4 */
vlm6534a8d2004-10-20 15:40:04 +00005#define _POSIX_PTHREAD_SEMANTICS /* for Sun */
6#define _REENTRANT /* for Sun */
vlm39ba4c42004-09-22 16:06:28 +00007#include <asn_internal.h>
vlmfa67ddc2004-06-03 03:38:44 +00008#include <GeneralizedTime.h>
9#include <time.h>
10#include <errno.h>
vlmfa67ddc2004-06-03 03:38:44 +000011
vlmc12c2102004-09-04 04:43:28 +000012#if defined(WIN32)
vlm73a781a2005-06-15 19:01:45 +000013#pragma message( "PLEASE STOP AND READ!")
14#pragma message( " localtime_r is implemented via localtime(), which may be not thread-safe.")
15#pragma message( " gmtime_r is implemented via gmtime(), which may be not thread-safe.")
16#pragma message( " ")
17#pragma message( " You must fix the code by inserting appropriate locking")
18#pragma message( " if you want to use asn_GT2time() or asn_UT2time().")
19#pragma message( "PLEASE STOP AND READ! ")
vlmc48f2132004-08-12 03:52:53 +000020
vlm17909e92004-09-02 05:20:39 +000021static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
vlmc48f2132004-08-12 03:52:53 +000022 struct tm *tm;
23 if((tm = localtime(tloc)))
24 return memcpy(result, tm, sizeof(struct tm));
25 return 0;
26}
27
vlm17909e92004-09-02 05:20:39 +000028static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
vlmc48f2132004-08-12 03:52:53 +000029 struct tm *tm;
30 if((tm = gmtime(tloc)))
31 return memcpy(result, tm, sizeof(struct tm));
32 return 0;
33}
34
vlmbed6f812004-09-17 06:46:10 +000035#define tzset() _tzset()
vlm73a781a2005-06-15 19:01:45 +000036#define putenv(c) _putenv(c)
vlmbed6f812004-09-17 06:46:10 +000037#define _EMULATE_TIMEGM
38
39#endif /* WIN32 */
40
vlm6534a8d2004-10-20 15:40:04 +000041#if defined(sun)
42#define _EMULATE_TIMEGM
43#endif
44
vlmbed6f812004-09-17 06:46:10 +000045/*
46 * Where to look for offset from GMT, Phase I.
47 * Several platforms are known.
48 */
49#if defined(__FreeBSD__) \
50 || (defined(__GNUC__) && defined(__APPLE_CC__)) \
51 || (defined __GLIBC__ && __GLIBC__ >= 2)
52#undef HAVE_TM_GMTOFF
53#define HAVE_TM_GMTOFF
54#endif /* BSDs and newer glibc */
55
56/*
57 * Where to look for offset from GMT, Phase II.
58 */
59#ifdef HAVE_TM_GMTOFF
60#define GMTOFF(tm) ((tm).tm_gmtoff)
61#else /* HAVE_TM_GMTOFF */
62#define GMTOFF(tm) (-timezone)
63#endif /* HAVE_TM_GMTOFF */
64
65/*
66 * Override our GMTOFF decision for other known platforms.
67 */
68#ifdef __CYGWIN__
69#undef GMTOFF
70static long GMTOFF(struct tm a){
71 struct tm *lt;
72 time_t local_time, gmt_time;
73 long zone;
74
75 tzset();
76 gmt_time = time (NULL);
77
78 lt = gmtime(&gmt_time);
79
80 local_time = mktime(lt);
81 return (gmt_time - local_time);
82}
83#define _EMULATE_TIMEGM
84
85#endif /* __CYGWIN__ */
86
87#ifdef _EMULATE_TIMEGM
vlm348a37a2004-08-12 03:58:25 +000088static time_t timegm(struct tm *tm) {
89 time_t tloc;
90 char *tz;
vlmd3692282004-08-12 07:47:03 +000091 char *buf;
vlm348a37a2004-08-12 03:58:25 +000092
93 tz = getenv("TZ");
vlm6534a8d2004-10-20 15:40:04 +000094 putenv("TZ=UTC");
vlmbed6f812004-09-17 06:46:10 +000095 tzset();
vlm348a37a2004-08-12 03:58:25 +000096 tloc = mktime(tm);
vlmd3692282004-08-12 07:47:03 +000097 if (tz) {
vlmbed6f812004-09-17 06:46:10 +000098 int bufsize = strlen(tz) + 4;
99 buf = alloca(bufsize);
100 snprintf(buf, bufsize, "TZ=%s", tz);
vlmd3692282004-08-12 07:47:03 +0000101 } else {
102 buf = "TZ=";
103 }
vlm6534a8d2004-10-20 15:40:04 +0000104 putenv(buf);
vlmbed6f812004-09-17 06:46:10 +0000105 tzset();
vlm348a37a2004-08-12 03:58:25 +0000106 return tloc;
107}
vlmbed6f812004-09-17 06:46:10 +0000108#endif /* _EMULATE_TIMEGM */
vlm348a37a2004-08-12 03:58:25 +0000109
vlm6e73a042004-08-11 07:17:22 +0000110
vlm5ea810e2005-07-03 05:32:40 +0000111#ifndef __ASN_INTERNAL_TEST_MODE__
vlmfa67ddc2004-06-03 03:38:44 +0000112
113/*
114 * GeneralizedTime basic type description.
115 */
vlmef6355b2004-09-29 13:26:15 +0000116static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
vlm6678cb12004-09-26 13:10:40 +0000117 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
118 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
119 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
vlmfa67ddc2004-06-03 03:38:44 +0000120};
vlmef6355b2004-09-29 13:26:15 +0000121asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
vlmfa67ddc2004-06-03 03:38:44 +0000122 "GeneralizedTime",
vlm9de248e2004-10-20 15:50:55 +0000123 "GeneralizedTime",
vlm39ba4c42004-09-22 16:06:28 +0000124 OCTET_STRING_free,
125 GeneralizedTime_print,
vlmfa67ddc2004-06-03 03:38:44 +0000126 GeneralizedTime_constraint, /* Check validity of time */
127 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
vlm9de248e2004-10-20 15:50:55 +0000128 GeneralizedTime_encode_der,
129 OCTET_STRING_decode_xer_utf8,
vlm39ba4c42004-09-22 16:06:28 +0000130 GeneralizedTime_encode_xer,
vlmfa67ddc2004-06-03 03:38:44 +0000131 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +0000132 asn_DEF_GeneralizedTime_tags,
133 sizeof(asn_DEF_GeneralizedTime_tags)
134 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
135 asn_DEF_GeneralizedTime_tags,
136 sizeof(asn_DEF_GeneralizedTime_tags)
137 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
vlme413c122004-08-20 13:23:42 +0000138 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +0000139 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +0000140};
141
vlm5ea810e2005-07-03 05:32:40 +0000142#endif /* __ASN_INTERNAL_TEST_MODE__ */
vlmfa67ddc2004-06-03 03:38:44 +0000143
144/*
145 * Check that the time looks like the time.
146 */
147int
vlmef6355b2004-09-29 13:26:15 +0000148GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +0000149 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000150 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000151 time_t tloc;
152
153 errno = EPERM; /* Just an unlikely error code */
vlm81057a82004-08-07 03:52:26 +0000154 tloc = asn_GT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000155 if(tloc == -1 && errno != EPERM) {
vlme3f0f282004-08-11 09:44:13 +0000156 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000157 "%s: Invalid time format: %s (%s:%d)",
158 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000159 return -1;
160 }
161
162 return 0;
163}
164
vlm39ba4c42004-09-22 16:06:28 +0000165asn_enc_rval_t
vlm5ea810e2005-07-03 05:32:40 +0000166GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
vlm81057a82004-08-07 03:52:26 +0000167 int tag_mode, ber_tlv_tag_t tag,
168 asn_app_consume_bytes_f *cb, void *app_key) {
vlm5ea810e2005-07-03 05:32:40 +0000169 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
vlm39ba4c42004-09-22 16:06:28 +0000170 asn_enc_rval_t erval;
vlm5ea810e2005-07-03 05:32:40 +0000171 long fv, fb; /* seconds fraction value and base */
172 struct tm tm;
173 time_t tloc;
vlm81057a82004-08-07 03:52:26 +0000174
vlm5ea810e2005-07-03 05:32:40 +0000175 /*
176 * Encode as a canonical DER.
177 */
178 errno = EPERM;
179 tloc = asn_GT2time_frac(st, &fv, &fb, &tm, 1); /* Recognize time */
180 if(tloc == -1 && errno != EPERM)
181 /* Failed to recognize time. Fail completely. */
182 _ASN_ENCODE_FAILED;
vlm81057a82004-08-07 03:52:26 +0000183
vlm5ea810e2005-07-03 05:32:40 +0000184 st = asn_time2GT_frac(0, &tm, fv, fb, 1); /* Save time canonically */
185 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
vlm81057a82004-08-07 03:52:26 +0000186
187 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
188
vlm5ea810e2005-07-03 05:32:40 +0000189 FREEMEM(st->buf);
190 FREEMEM(st);
vlm81057a82004-08-07 03:52:26 +0000191
192 return erval;
193}
194
vlm5ea810e2005-07-03 05:32:40 +0000195#ifndef __ASN_INTERNAL_TEST_MODE__
196
vlm39ba4c42004-09-22 16:06:28 +0000197asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000198GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000199 int ilevel, enum xer_encoder_flags_e flags,
200 asn_app_consume_bytes_f *cb, void *app_key) {
vlm39ba4c42004-09-22 16:06:28 +0000201
202 if(flags & XER_F_CANONICAL) {
vlm5ea810e2005-07-03 05:32:40 +0000203 GeneralizedTime_t *gt;
204 asn_enc_rval_t rv;
205 long fv, fb; /* fractional parts */
vlm39ba4c42004-09-22 16:06:28 +0000206 struct tm tm;
vlm39ba4c42004-09-22 16:06:28 +0000207
208 errno = EPERM;
vlm5ea810e2005-07-03 05:32:40 +0000209 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
210 &fv, &fb, &tm, 1) == -1
vlm39ba4c42004-09-22 16:06:28 +0000211 && errno != EPERM)
212 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000213
vlm5ea810e2005-07-03 05:32:40 +0000214 gt = asn_time2GT_frac(0, &tm, fv, fb, 1);
215 if(!gt) _ASN_ENCODE_FAILED;
216
217 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
218 cb, app_key);
219 asn_DEF_GeneralizedTime.free_struct(&asn_DEF_GeneralizedTime,
220 gt, 0);
221 return rv;
222 } else {
223 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
224 cb, app_key);
225 }
vlm39ba4c42004-09-22 16:06:28 +0000226}
227
vlm5ea810e2005-07-03 05:32:40 +0000228#endif /* __ASN_INTERNAL_TEST_MODE__ */
229
vlmfa67ddc2004-06-03 03:38:44 +0000230int
vlmef6355b2004-09-29 13:26:15 +0000231GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000232 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000233 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000234
vlmb42843a2004-06-05 08:17:50 +0000235 (void)td; /* Unused argument */
236 (void)ilevel; /* Unused argument */
237
vlmfa67ddc2004-06-03 03:38:44 +0000238 if(st && st->buf) {
239 char buf[32];
240 struct tm tm;
241 int ret;
242
243 errno = EPERM;
vlm81057a82004-08-07 03:52:26 +0000244 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
vlm6678cb12004-09-26 13:10:40 +0000245 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000246
247 ret = snprintf(buf, sizeof(buf),
vlm5ea810e2005-07-03 05:32:40 +0000248 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000249 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
250 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000251 assert(ret > 0 && ret < (int)sizeof(buf));
vlm6678cb12004-09-26 13:10:40 +0000252 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000253 } else {
vlm6678cb12004-09-26 13:10:40 +0000254 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000255 }
256}
257
vlmfa67ddc2004-06-03 03:38:44 +0000258time_t
vlm81057a82004-08-07 03:52:26 +0000259asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000260 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
261}
262
263time_t
264asn_GT2time_frac(const GeneralizedTime_t *st, long *frac_value, long *frac_base, struct tm *ret_tm, int as_gmt) {
vlmfa67ddc2004-06-03 03:38:44 +0000265 struct tm tm_s;
266 uint8_t *buf;
267 uint8_t *end;
vlm81057a82004-08-07 03:52:26 +0000268 int gmtoff_h = 0;
269 int gmtoff_m = 0;
270 int gmtoff = 0; /* h + m */
vlmfa67ddc2004-06-03 03:38:44 +0000271 int offset_specified = 0;
vlm5ea810e2005-07-03 05:32:40 +0000272 long fvalue = 0;
273 long fbase = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000274 time_t tloc;
275
276 if(!st || !st->buf) {
277 errno = EINVAL;
278 return -1;
279 } else {
280 buf = st->buf;
281 end = buf + st->size;
282 }
283
284 if(st->size < 10) {
285 errno = EINVAL;
286 return -1;
287 }
288
289 /*
290 * Decode first 10 bytes: "AAAAMMJJhh"
291 */
292 memset(&tm_s, 0, sizeof(tm_s));
293#undef B2F
294#undef B2T
295#define B2F(var) do { \
296 unsigned ch = *buf; \
297 if(ch < 0x30 && ch > 0x39) { \
298 errno = EINVAL; \
299 return -1; \
300 } else { \
301 var = var * 10 + (ch - 0x30); \
302 buf++; \
303 } \
304 } while(0)
305#define B2T(var) B2F(tm_s.var)
306
307 B2T(tm_year); /* 1: A */
308 B2T(tm_year); /* 2: A */
309 B2T(tm_year); /* 3: A */
310 B2T(tm_year); /* 4: A */
311 B2T(tm_mon); /* 5: M */
312 B2T(tm_mon); /* 6: M */
313 B2T(tm_mday); /* 7: J */
314 B2T(tm_mday); /* 8: J */
315 B2T(tm_hour); /* 9: h */
316 B2T(tm_hour); /* 0: h */
317
318 if(buf == end) goto local_finish;
319
320 /*
321 * Parse [mm[ss[(.|,)ffff]]]
322 * ^^
323 */
324 switch(*buf) {
325 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
326 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
327 tm_s.tm_min = (*buf++) - 0x30;
328 if(buf == end) { errno = EINVAL; return -1; }
329 B2T(tm_min);
330 break;
331 case 0x2B: case 0x2D: /* +, - */
332 goto offset;
333 case 0x5A: /* Z */
334 goto utc_finish;
335 default:
336 errno = EINVAL;
337 return -1;
338 }
339
340 if(buf == end) goto local_finish;
341
342 /*
343 * Parse [mm[ss[(.|,)ffff]]]
344 * ^^
345 */
346 switch(*buf) {
347 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
348 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
349 tm_s.tm_sec = (*buf++) - 0x30;
350 if(buf == end) { errno = EINVAL; return -1; }
351 B2T(tm_sec);
352 break;
353 case 0x2B: case 0x2D: /* +, - */
354 goto offset;
355 case 0x5A: /* Z */
356 goto utc_finish;
357 default:
358 errno = EINVAL;
359 return -1;
360 }
361
362 if(buf == end) goto local_finish;
363
364 /*
365 * Parse [mm[ss[(.|,)ffff]]]
366 * ^ ^
367 */
368 switch(*buf) {
vlm5ea810e2005-07-03 05:32:40 +0000369 case 0x2C: case 0x2E: /* (.|,) */
370 /*
371 * Process fractions of seconds.
372 */
vlmfa67ddc2004-06-03 03:38:44 +0000373 for(buf++; buf < end; buf++) {
vlm5ea810e2005-07-03 05:32:40 +0000374 int v = *buf;
375 switch(v) {
vlmfa67ddc2004-06-03 03:38:44 +0000376 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
377 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
vlm5ea810e2005-07-03 05:32:40 +0000378 if((fbase * 10 / fbase) != 10) {
379 /* Not enough precision, ignore */
380 } else {
381 fbase *= 10;
382 fvalue = fvalue * 10 + (v - 0x30);
383 }
vlmfa67ddc2004-06-03 03:38:44 +0000384 continue;
385 default:
386 break;
387 }
388 break;
389 }
390 }
391
392 if(buf == end) goto local_finish;
393
394 switch(*buf) {
395 case 0x2B: case 0x2D: /* +, - */
396 goto offset;
397 case 0x5A: /* Z */
398 goto utc_finish;
399 default:
400 errno = EINVAL;
401 return -1;
402 }
403
404
405offset:
406
407 if(end - buf < 3) {
408 errno = EINVAL;
409 return -1;
410 }
411 buf++;
vlm81057a82004-08-07 03:52:26 +0000412 B2F(gmtoff_h);
413 B2F(gmtoff_h);
vlmfa67ddc2004-06-03 03:38:44 +0000414 if(buf[-3] == 0x2D) /* Negative */
vlm81057a82004-08-07 03:52:26 +0000415 gmtoff = -1;
vlmfa67ddc2004-06-03 03:38:44 +0000416 else
vlm81057a82004-08-07 03:52:26 +0000417 gmtoff = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000418
419 if((end - buf) == 2) {
vlm81057a82004-08-07 03:52:26 +0000420 B2F(gmtoff_m);
421 B2F(gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000422 } else if(end != buf) {
423 errno = EINVAL;
424 return -1;
425 }
426
vlm81057a82004-08-07 03:52:26 +0000427 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000428
429 /* Fall through */
430utc_finish:
431
432 offset_specified = 1;
433
434 /* Fall through */
435local_finish:
436
437 /*
438 * Validation.
439 */
440 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
441 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
442 || (tm_s.tm_hour > 23)
443 || (tm_s.tm_sec > 60)
444 ) {
445 errno = EINVAL;
446 return -1;
447 }
448
449 /* Canonicalize */
450 tm_s.tm_mon -= 1; /* 0 - 11 */
451 tm_s.tm_year -= 1900;
452 tm_s.tm_isdst = -1;
453
vlm81057a82004-08-07 03:52:26 +0000454 tm_s.tm_sec -= gmtoff;
455
456 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
457
vlmc48f2132004-08-12 03:52:53 +0000458 if(offset_specified) {
vlm81057a82004-08-07 03:52:26 +0000459 tloc = timegm(&tm_s);
vlmc48f2132004-08-12 03:52:53 +0000460 } else {
vlm81057a82004-08-07 03:52:26 +0000461 /*
vlmef6355b2004-09-29 13:26:15 +0000462 * Without an offset (or "Z"),
vlm81057a82004-08-07 03:52:26 +0000463 * we can only guess that it is a local zone.
464 * Interpret it in this fashion.
465 */
466 tloc = mktime(&tm_s);
467 }
vlmfa67ddc2004-06-03 03:38:44 +0000468 if(tloc == -1) {
469 errno = EINVAL;
470 return -1;
471 }
472
vlm81057a82004-08-07 03:52:26 +0000473 if(ret_tm) {
474 if(as_gmt) {
475 if(offset_specified) {
476 *ret_tm = tm_s;
477 } else {
478 if(gmtime_r(&tloc, ret_tm) == 0) {
479 errno = EINVAL;
480 return -1;
481 }
482 }
483 } else {
484 if(localtime_r(&tloc, ret_tm) == 0) {
485 errno = EINVAL;
486 return -1;
487 }
vlmfa67ddc2004-06-03 03:38:44 +0000488 }
489 }
490
vlm5ea810e2005-07-03 05:32:40 +0000491 /* Fractions of seconds */
492 if(frac_value) *frac_value = fvalue;
493 if(frac_base) *frac_base = fbase;
494
vlmfa67ddc2004-06-03 03:38:44 +0000495 return tloc;
496}
497
vlm81057a82004-08-07 03:52:26 +0000498GeneralizedTime_t *
499asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000500 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
501}
502
503GeneralizedTime_t *
504asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, long frac_value, long frac_base, int force_gmt) {
vlm81057a82004-08-07 03:52:26 +0000505 struct tm tm_s;
506 long gmtoff;
vlm5ea810e2005-07-03 05:32:40 +0000507 const unsigned int buf_size =
508 4 + 2 + 2 /* yyyymmdd */
509 + 2 + 2 + 2 /* hhmmss */
510 + 1 + 6 /* .ffffff */
511 + 1 + 4 /* +hhmm */
512 + 1 /* '\0' */
513 ;
vlm81057a82004-08-07 03:52:26 +0000514 char *buf;
515 char *p;
516 int size;
517
518 /* Check arguments */
519 if(!tm) {
520 errno = EINVAL;
521 return 0;
522 }
523
524 /* Pre-allocate a buffer of sufficient yet small length */
vlm6678cb12004-09-26 13:10:40 +0000525 buf = (char *)MALLOC(buf_size);
vlm81057a82004-08-07 03:52:26 +0000526 if(!buf) return 0;
527
528 gmtoff = GMTOFF(*tm);
529
530 if(force_gmt && gmtoff) {
531 tm_s = *tm;
532 tm_s.tm_sec -= gmtoff;
533 timegm(&tm_s); /* Fix the time */
vlm81057a82004-08-07 03:52:26 +0000534 tm = &tm_s;
vlm65efbd02004-08-11 07:35:08 +0000535#ifdef HAVE_TM_GMTOFF
536 assert(!GMTOFF(tm_s)); /* Will fix itself */
537#else
538 gmtoff = 0; /* Intervention required */
539#endif
vlm81057a82004-08-07 03:52:26 +0000540 }
541
542 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
543 tm->tm_year + 1900,
544 tm->tm_mon + 1,
545 tm->tm_mday,
546 tm->tm_hour,
547 tm->tm_min,
548 tm->tm_sec
549 );
vlm5ea810e2005-07-03 05:32:40 +0000550 if(size != 14) {
551 /* Could be assert(size == 14); */
552 FREEMEM(buf);
553 errno = EINVAL;
554 return 0;
555 }
vlm81057a82004-08-07 03:52:26 +0000556
557 p = buf + size;
vlm5ea810e2005-07-03 05:32:40 +0000558
559 /*
560 * Deal with fractions.
561 */
vlm9d531932005-07-04 01:44:01 +0000562 if(frac_base >= 10
563 && frac_value > 0
vlmbaf858b2005-07-04 02:29:36 +0000564 && (frac_value/frac_base) == 0
vlm9d531932005-07-04 01:44:01 +0000565 ) {
vlm5ea810e2005-07-03 05:32:40 +0000566 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
vlm9d531932005-07-04 01:44:01 +0000567 char *z = p;
568 *z++ = '.';
569 frac_value %= frac_base;
vlm5ea810e2005-07-03 05:32:40 +0000570 do {
571 int digit;
572 frac_base /= 10;
573 digit = frac_value / frac_base;
vlmbaf858b2005-07-04 02:29:36 +0000574 if(digit > 9) { z = 0; break; }
vlm5ea810e2005-07-03 05:32:40 +0000575 frac_value %= frac_base;
vlm9d531932005-07-04 01:44:01 +0000576 *z++ = digit + 0x30;
577 } while(frac_base >= 10 && frac_value > 0 && z < end);
vlmbaf858b2005-07-04 02:29:36 +0000578 if(z && (frac_base == 1 || frac_base >= 10)) {
579 for(--z; *z == 0x30; --z); /* Strip zeroes */
580 p = z + (*z != '.');
581 size = p - buf;
582 }
vlm5ea810e2005-07-03 05:32:40 +0000583 }
584
vlm81057a82004-08-07 03:52:26 +0000585 if(force_gmt) {
vlmef6355b2004-09-29 13:26:15 +0000586 *p++ = 0x5a; /* "Z" */
vlm81057a82004-08-07 03:52:26 +0000587 *p++ = 0;
588 size++;
589 } else {
vlm5ea810e2005-07-03 05:32:40 +0000590 int ret;
591 gmtoff %= 86400;
592 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
593 gmtoff / 3600, labs(gmtoff % 3600));
594 if(ret != 5) {
595 FREEMEM(buf);
596 errno = EINVAL;
597 return 0;
598 }
vlm81057a82004-08-07 03:52:26 +0000599 size += ret;
600 }
601
602 if(opt_gt) {
603 if(opt_gt->buf)
604 FREEMEM(opt_gt->buf);
605 } else {
vlm6678cb12004-09-26 13:10:40 +0000606 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
vlm81057a82004-08-07 03:52:26 +0000607 if(!opt_gt) { free(buf); return 0; }
608 }
609
vlm1ff928d2004-08-11 08:10:13 +0000610 opt_gt->buf = (unsigned char *)buf;
vlm81057a82004-08-07 03:52:26 +0000611 opt_gt->size = size;
612
613 return opt_gt;
614}
615
616