blob: 79f62e82b79d3815fbff47cd2e37182477a08519 [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,
vlm337167e2005-11-26 11:25:14 +0000131 0,
vlmfa67ddc2004-06-03 03:38:44 +0000132 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +0000133 asn_DEF_GeneralizedTime_tags,
134 sizeof(asn_DEF_GeneralizedTime_tags)
135 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
136 asn_DEF_GeneralizedTime_tags,
137 sizeof(asn_DEF_GeneralizedTime_tags)
138 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
vlm337167e2005-11-26 11:25:14 +0000139 0, /* No PER visible constraints */
vlme413c122004-08-20 13:23:42 +0000140 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +0000141 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +0000142};
143
vlm5ea810e2005-07-03 05:32:40 +0000144#endif /* __ASN_INTERNAL_TEST_MODE__ */
vlmfa67ddc2004-06-03 03:38:44 +0000145
146/*
147 * Check that the time looks like the time.
148 */
149int
vlmef6355b2004-09-29 13:26:15 +0000150GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmfa67ddc2004-06-03 03:38:44 +0000151 asn_app_consume_bytes_f *app_errlog, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000152 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000153 time_t tloc;
154
155 errno = EPERM; /* Just an unlikely error code */
vlm81057a82004-08-07 03:52:26 +0000156 tloc = asn_GT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000157 if(tloc == -1 && errno != EPERM) {
vlme3f0f282004-08-11 09:44:13 +0000158 _ASN_ERRLOG(app_errlog, app_key,
vlm758530a2004-08-22 13:47:59 +0000159 "%s: Invalid time format: %s (%s:%d)",
160 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000161 return -1;
162 }
163
164 return 0;
165}
166
vlm39ba4c42004-09-22 16:06:28 +0000167asn_enc_rval_t
vlm5ea810e2005-07-03 05:32:40 +0000168GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
vlm81057a82004-08-07 03:52:26 +0000169 int tag_mode, ber_tlv_tag_t tag,
170 asn_app_consume_bytes_f *cb, void *app_key) {
vlm5ea810e2005-07-03 05:32:40 +0000171 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
vlm39ba4c42004-09-22 16:06:28 +0000172 asn_enc_rval_t erval;
vlmaa116962005-07-04 12:21:51 +0000173 int fv, fd; /* seconds fraction value and number of digits */
vlm5ea810e2005-07-03 05:32:40 +0000174 struct tm tm;
175 time_t tloc;
vlm81057a82004-08-07 03:52:26 +0000176
vlm5ea810e2005-07-03 05:32:40 +0000177 /*
178 * Encode as a canonical DER.
179 */
180 errno = EPERM;
vlmaa116962005-07-04 12:21:51 +0000181 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
vlm5ea810e2005-07-03 05:32:40 +0000182 if(tloc == -1 && errno != EPERM)
183 /* Failed to recognize time. Fail completely. */
184 _ASN_ENCODE_FAILED;
vlm81057a82004-08-07 03:52:26 +0000185
vlmaa116962005-07-04 12:21:51 +0000186 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
vlm5ea810e2005-07-03 05:32:40 +0000187 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
vlm81057a82004-08-07 03:52:26 +0000188
189 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
190
vlm5ea810e2005-07-03 05:32:40 +0000191 FREEMEM(st->buf);
192 FREEMEM(st);
vlm81057a82004-08-07 03:52:26 +0000193
194 return erval;
195}
196
vlm5ea810e2005-07-03 05:32:40 +0000197#ifndef __ASN_INTERNAL_TEST_MODE__
198
vlm39ba4c42004-09-22 16:06:28 +0000199asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000200GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000201 int ilevel, enum xer_encoder_flags_e flags,
202 asn_app_consume_bytes_f *cb, void *app_key) {
vlm39ba4c42004-09-22 16:06:28 +0000203
204 if(flags & XER_F_CANONICAL) {
vlm5ea810e2005-07-03 05:32:40 +0000205 GeneralizedTime_t *gt;
206 asn_enc_rval_t rv;
vlmaa116962005-07-04 12:21:51 +0000207 int fv, fd; /* fractional parts */
vlm39ba4c42004-09-22 16:06:28 +0000208 struct tm tm;
vlm39ba4c42004-09-22 16:06:28 +0000209
210 errno = EPERM;
vlm5ea810e2005-07-03 05:32:40 +0000211 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
vlmaa116962005-07-04 12:21:51 +0000212 &fv, &fd, &tm, 1) == -1
vlm39ba4c42004-09-22 16:06:28 +0000213 && errno != EPERM)
214 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000215
vlmaa116962005-07-04 12:21:51 +0000216 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
vlm5ea810e2005-07-03 05:32:40 +0000217 if(!gt) _ASN_ENCODE_FAILED;
218
219 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
220 cb, app_key);
vlmbe78c802006-03-17 02:11:12 +0000221 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
vlm5ea810e2005-07-03 05:32:40 +0000222 return rv;
223 } else {
224 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
225 cb, app_key);
226 }
vlm39ba4c42004-09-22 16:06:28 +0000227}
228
vlm5ea810e2005-07-03 05:32:40 +0000229#endif /* __ASN_INTERNAL_TEST_MODE__ */
230
vlmfa67ddc2004-06-03 03:38:44 +0000231int
vlmef6355b2004-09-29 13:26:15 +0000232GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000233 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000234 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000235
vlmb42843a2004-06-05 08:17:50 +0000236 (void)td; /* Unused argument */
237 (void)ilevel; /* Unused argument */
238
vlmfa67ddc2004-06-03 03:38:44 +0000239 if(st && st->buf) {
240 char buf[32];
241 struct tm tm;
242 int ret;
243
244 errno = EPERM;
vlm81057a82004-08-07 03:52:26 +0000245 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
vlm6678cb12004-09-26 13:10:40 +0000246 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000247
248 ret = snprintf(buf, sizeof(buf),
vlm5ea810e2005-07-03 05:32:40 +0000249 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000250 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
251 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000252 assert(ret > 0 && ret < (int)sizeof(buf));
vlm6678cb12004-09-26 13:10:40 +0000253 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000254 } else {
vlm6678cb12004-09-26 13:10:40 +0000255 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000256 }
257}
258
vlmfa67ddc2004-06-03 03:38:44 +0000259time_t
vlm81057a82004-08-07 03:52:26 +0000260asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000261 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
262}
263
264time_t
vlmaa116962005-07-04 12:21:51 +0000265asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
266 time_t tloc;
267 int fv, fd = 0;
268
269 if(frac_value)
270 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
271 else
272 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
273 if(fd == 0 || frac_digits <= 0) {
274 *frac_value = 0;
275 } else {
276 while(fd > frac_digits)
277 fv /= 10, fd--;
278 while(fd < frac_digits) {
279 int new_fv = fv * 10;
280 if(new_fv / 10 != fv) {
281 /* Too long precision request */
282 fv = 0;
283 break;
284 }
285 fv = new_fv, fd++;
286 }
287
288 *frac_value = fv;
289 }
290
291 return tloc;
292}
293
294time_t
295asn_GT2time_frac(const GeneralizedTime_t *st, int *frac_value, int *frac_digits, struct tm *ret_tm, int as_gmt) {
vlmfa67ddc2004-06-03 03:38:44 +0000296 struct tm tm_s;
297 uint8_t *buf;
298 uint8_t *end;
vlm81057a82004-08-07 03:52:26 +0000299 int gmtoff_h = 0;
300 int gmtoff_m = 0;
301 int gmtoff = 0; /* h + m */
vlmfa67ddc2004-06-03 03:38:44 +0000302 int offset_specified = 0;
vlmaa116962005-07-04 12:21:51 +0000303 int fvalue = 0;
304 int fdigits = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000305 time_t tloc;
306
307 if(!st || !st->buf) {
308 errno = EINVAL;
309 return -1;
310 } else {
311 buf = st->buf;
312 end = buf + st->size;
313 }
314
315 if(st->size < 10) {
316 errno = EINVAL;
317 return -1;
318 }
319
320 /*
321 * Decode first 10 bytes: "AAAAMMJJhh"
322 */
323 memset(&tm_s, 0, sizeof(tm_s));
324#undef B2F
325#undef B2T
326#define B2F(var) do { \
327 unsigned ch = *buf; \
vlm6eb79212006-01-10 08:08:14 +0000328 if(ch < 0x30 || ch > 0x39) { \
vlmfa67ddc2004-06-03 03:38:44 +0000329 errno = EINVAL; \
330 return -1; \
331 } else { \
332 var = var * 10 + (ch - 0x30); \
333 buf++; \
334 } \
335 } while(0)
336#define B2T(var) B2F(tm_s.var)
337
338 B2T(tm_year); /* 1: A */
339 B2T(tm_year); /* 2: A */
340 B2T(tm_year); /* 3: A */
341 B2T(tm_year); /* 4: A */
342 B2T(tm_mon); /* 5: M */
343 B2T(tm_mon); /* 6: M */
344 B2T(tm_mday); /* 7: J */
345 B2T(tm_mday); /* 8: J */
346 B2T(tm_hour); /* 9: h */
347 B2T(tm_hour); /* 0: h */
348
349 if(buf == end) goto local_finish;
350
351 /*
352 * Parse [mm[ss[(.|,)ffff]]]
353 * ^^
354 */
355 switch(*buf) {
356 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
357 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
358 tm_s.tm_min = (*buf++) - 0x30;
359 if(buf == end) { errno = EINVAL; return -1; }
360 B2T(tm_min);
361 break;
362 case 0x2B: case 0x2D: /* +, - */
363 goto offset;
364 case 0x5A: /* Z */
365 goto utc_finish;
366 default:
367 errno = EINVAL;
368 return -1;
369 }
370
371 if(buf == end) goto local_finish;
372
373 /*
374 * Parse [mm[ss[(.|,)ffff]]]
375 * ^^
376 */
377 switch(*buf) {
378 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
379 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
380 tm_s.tm_sec = (*buf++) - 0x30;
381 if(buf == end) { errno = EINVAL; return -1; }
382 B2T(tm_sec);
383 break;
384 case 0x2B: case 0x2D: /* +, - */
385 goto offset;
386 case 0x5A: /* Z */
387 goto utc_finish;
388 default:
389 errno = EINVAL;
390 return -1;
391 }
392
393 if(buf == end) goto local_finish;
394
395 /*
396 * Parse [mm[ss[(.|,)ffff]]]
397 * ^ ^
398 */
399 switch(*buf) {
vlm5ea810e2005-07-03 05:32:40 +0000400 case 0x2C: case 0x2E: /* (.|,) */
401 /*
402 * Process fractions of seconds.
403 */
vlmfa67ddc2004-06-03 03:38:44 +0000404 for(buf++; buf < end; buf++) {
vlm5ea810e2005-07-03 05:32:40 +0000405 int v = *buf;
vlmaa116962005-07-04 12:21:51 +0000406 int new_fvalue;
vlm5ea810e2005-07-03 05:32:40 +0000407 switch(v) {
vlmfa67ddc2004-06-03 03:38:44 +0000408 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
409 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
vlmaa116962005-07-04 12:21:51 +0000410 new_fvalue = fvalue * 10 + (v - 0x30);
411 if(new_fvalue / 10 != fvalue) {
vlm5ea810e2005-07-03 05:32:40 +0000412 /* Not enough precision, ignore */
413 } else {
vlmaa116962005-07-04 12:21:51 +0000414 fvalue = new_fvalue;
415 fdigits++;
vlm5ea810e2005-07-03 05:32:40 +0000416 }
vlmfa67ddc2004-06-03 03:38:44 +0000417 continue;
418 default:
419 break;
420 }
421 break;
422 }
423 }
424
425 if(buf == end) goto local_finish;
426
427 switch(*buf) {
428 case 0x2B: case 0x2D: /* +, - */
429 goto offset;
430 case 0x5A: /* Z */
431 goto utc_finish;
432 default:
433 errno = EINVAL;
434 return -1;
435 }
436
437
438offset:
439
440 if(end - buf < 3) {
441 errno = EINVAL;
442 return -1;
443 }
444 buf++;
vlm81057a82004-08-07 03:52:26 +0000445 B2F(gmtoff_h);
446 B2F(gmtoff_h);
vlmfa67ddc2004-06-03 03:38:44 +0000447 if(buf[-3] == 0x2D) /* Negative */
vlm81057a82004-08-07 03:52:26 +0000448 gmtoff = -1;
vlmfa67ddc2004-06-03 03:38:44 +0000449 else
vlm81057a82004-08-07 03:52:26 +0000450 gmtoff = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000451
452 if((end - buf) == 2) {
vlm81057a82004-08-07 03:52:26 +0000453 B2F(gmtoff_m);
454 B2F(gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000455 } else if(end != buf) {
456 errno = EINVAL;
457 return -1;
458 }
459
vlm81057a82004-08-07 03:52:26 +0000460 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000461
462 /* Fall through */
463utc_finish:
464
465 offset_specified = 1;
466
467 /* Fall through */
468local_finish:
469
470 /*
471 * Validation.
472 */
473 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
474 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
475 || (tm_s.tm_hour > 23)
476 || (tm_s.tm_sec > 60)
477 ) {
478 errno = EINVAL;
479 return -1;
480 }
481
482 /* Canonicalize */
483 tm_s.tm_mon -= 1; /* 0 - 11 */
484 tm_s.tm_year -= 1900;
485 tm_s.tm_isdst = -1;
486
vlm81057a82004-08-07 03:52:26 +0000487 tm_s.tm_sec -= gmtoff;
488
489 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
490
vlmc48f2132004-08-12 03:52:53 +0000491 if(offset_specified) {
vlm81057a82004-08-07 03:52:26 +0000492 tloc = timegm(&tm_s);
vlmc48f2132004-08-12 03:52:53 +0000493 } else {
vlm81057a82004-08-07 03:52:26 +0000494 /*
vlmef6355b2004-09-29 13:26:15 +0000495 * Without an offset (or "Z"),
vlm81057a82004-08-07 03:52:26 +0000496 * we can only guess that it is a local zone.
497 * Interpret it in this fashion.
498 */
499 tloc = mktime(&tm_s);
500 }
vlmfa67ddc2004-06-03 03:38:44 +0000501 if(tloc == -1) {
502 errno = EINVAL;
503 return -1;
504 }
505
vlm81057a82004-08-07 03:52:26 +0000506 if(ret_tm) {
507 if(as_gmt) {
508 if(offset_specified) {
509 *ret_tm = tm_s;
510 } else {
511 if(gmtime_r(&tloc, ret_tm) == 0) {
512 errno = EINVAL;
513 return -1;
514 }
515 }
516 } else {
517 if(localtime_r(&tloc, ret_tm) == 0) {
518 errno = EINVAL;
519 return -1;
520 }
vlmfa67ddc2004-06-03 03:38:44 +0000521 }
522 }
523
vlm5ea810e2005-07-03 05:32:40 +0000524 /* Fractions of seconds */
525 if(frac_value) *frac_value = fvalue;
vlmaa116962005-07-04 12:21:51 +0000526 if(frac_digits) *frac_digits = fdigits;
vlm5ea810e2005-07-03 05:32:40 +0000527
vlmfa67ddc2004-06-03 03:38:44 +0000528 return tloc;
529}
530
vlm81057a82004-08-07 03:52:26 +0000531GeneralizedTime_t *
532asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000533 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
534}
535
536GeneralizedTime_t *
vlmaa116962005-07-04 12:21:51 +0000537asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, int frac_value, int frac_digits, int force_gmt) {
vlm81057a82004-08-07 03:52:26 +0000538 struct tm tm_s;
539 long gmtoff;
vlm5ea810e2005-07-03 05:32:40 +0000540 const unsigned int buf_size =
541 4 + 2 + 2 /* yyyymmdd */
542 + 2 + 2 + 2 /* hhmmss */
543 + 1 + 6 /* .ffffff */
544 + 1 + 4 /* +hhmm */
545 + 1 /* '\0' */
546 ;
vlm81057a82004-08-07 03:52:26 +0000547 char *buf;
548 char *p;
549 int size;
550
551 /* Check arguments */
552 if(!tm) {
553 errno = EINVAL;
554 return 0;
555 }
556
557 /* Pre-allocate a buffer of sufficient yet small length */
vlm6678cb12004-09-26 13:10:40 +0000558 buf = (char *)MALLOC(buf_size);
vlm81057a82004-08-07 03:52:26 +0000559 if(!buf) return 0;
560
561 gmtoff = GMTOFF(*tm);
562
563 if(force_gmt && gmtoff) {
564 tm_s = *tm;
565 tm_s.tm_sec -= gmtoff;
566 timegm(&tm_s); /* Fix the time */
vlm81057a82004-08-07 03:52:26 +0000567 tm = &tm_s;
vlm65efbd02004-08-11 07:35:08 +0000568#ifdef HAVE_TM_GMTOFF
569 assert(!GMTOFF(tm_s)); /* Will fix itself */
570#else
571 gmtoff = 0; /* Intervention required */
572#endif
vlm81057a82004-08-07 03:52:26 +0000573 }
574
575 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
576 tm->tm_year + 1900,
577 tm->tm_mon + 1,
578 tm->tm_mday,
579 tm->tm_hour,
580 tm->tm_min,
581 tm->tm_sec
582 );
vlm5ea810e2005-07-03 05:32:40 +0000583 if(size != 14) {
584 /* Could be assert(size == 14); */
585 FREEMEM(buf);
586 errno = EINVAL;
587 return 0;
588 }
vlm81057a82004-08-07 03:52:26 +0000589
590 p = buf + size;
vlm5ea810e2005-07-03 05:32:40 +0000591
592 /*
593 * Deal with fractions.
594 */
vlmaa116962005-07-04 12:21:51 +0000595 if(frac_value > 0 && frac_digits > 0) {
vlm5ea810e2005-07-03 05:32:40 +0000596 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
vlm9d531932005-07-04 01:44:01 +0000597 char *z = p;
vlmaa116962005-07-04 12:21:51 +0000598 long fbase;
vlm9d531932005-07-04 01:44:01 +0000599 *z++ = '.';
vlmaa116962005-07-04 12:21:51 +0000600
601 /* Place bounds on precision */
602 while(frac_digits-- > 6)
603 frac_value /= 10;
604
605 /* emulate fbase = pow(10, frac_digits) */
606 for(fbase = 1; frac_digits--;)
607 fbase *= 10;
608
vlm5ea810e2005-07-03 05:32:40 +0000609 do {
vlmaa116962005-07-04 12:21:51 +0000610 int digit = frac_value / fbase;
vlmbaf858b2005-07-04 02:29:36 +0000611 if(digit > 9) { z = 0; break; }
vlm9d531932005-07-04 01:44:01 +0000612 *z++ = digit + 0x30;
vlmaa116962005-07-04 12:21:51 +0000613 frac_value %= fbase;
614 fbase /= 10;
615 } while(fbase > 0 && frac_value > 0 && z < end);
616 if(z) {
vlmbaf858b2005-07-04 02:29:36 +0000617 for(--z; *z == 0x30; --z); /* Strip zeroes */
618 p = z + (*z != '.');
619 size = p - buf;
620 }
vlm5ea810e2005-07-03 05:32:40 +0000621 }
622
vlm81057a82004-08-07 03:52:26 +0000623 if(force_gmt) {
vlmef6355b2004-09-29 13:26:15 +0000624 *p++ = 0x5a; /* "Z" */
vlm81057a82004-08-07 03:52:26 +0000625 *p++ = 0;
626 size++;
627 } else {
vlm5ea810e2005-07-03 05:32:40 +0000628 int ret;
629 gmtoff %= 86400;
630 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
631 gmtoff / 3600, labs(gmtoff % 3600));
632 if(ret != 5) {
633 FREEMEM(buf);
634 errno = EINVAL;
635 return 0;
636 }
vlm81057a82004-08-07 03:52:26 +0000637 size += ret;
638 }
639
640 if(opt_gt) {
641 if(opt_gt->buf)
642 FREEMEM(opt_gt->buf);
643 } else {
vlm6678cb12004-09-26 13:10:40 +0000644 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
vlm81057a82004-08-07 03:52:26 +0000645 if(!opt_gt) { free(buf); return 0; }
646 }
647
vlm1ff928d2004-08-11 08:10:13 +0000648 opt_gt->buf = (unsigned char *)buf;
vlm81057a82004-08-07 03:52:26 +0000649 opt_gt->size = size;
650
651 return opt_gt;
652}
653
654