blob: 0c304935515e18745fd38f2d79b7932bdafc69d8 [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>
vlmfa67ddc2004-06-03 03:38:44 +00009#include <errno.h>
vlmfa67ddc2004-06-03 03:38:44 +000010
vlma411eee2006-09-13 00:21:58 +000011#ifdef __CYGWIN__
12#include "/usr/include/time.h"
13#else
14#include <time.h>
15#endif /* __CYGWIN__ */
16
vlmc12c2102004-09-04 04:43:28 +000017#if defined(WIN32)
vlm73a781a2005-06-15 19:01:45 +000018#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().")
vlmd117a852006-07-13 13:20:19 +000024#pragma message( "PLEASE STOP AND READ!")
vlmc48f2132004-08-12 03:52:53 +000025
vlm17909e92004-09-02 05:20:39 +000026static struct tm *localtime_r(const time_t *tloc, struct tm *result) {
vlmc48f2132004-08-12 03:52:53 +000027 struct tm *tm;
28 if((tm = localtime(tloc)))
29 return memcpy(result, tm, sizeof(struct tm));
30 return 0;
31}
32
vlm17909e92004-09-02 05:20:39 +000033static struct tm *gmtime_r(const time_t *tloc, struct tm *result) {
vlmc48f2132004-08-12 03:52:53 +000034 struct tm *tm;
35 if((tm = gmtime(tloc)))
36 return memcpy(result, tm, sizeof(struct tm));
37 return 0;
38}
39
vlmbed6f812004-09-17 06:46:10 +000040#define tzset() _tzset()
vlm73a781a2005-06-15 19:01:45 +000041#define putenv(c) _putenv(c)
vlmbed6f812004-09-17 06:46:10 +000042#define _EMULATE_TIMEGM
43
44#endif /* WIN32 */
45
vlmd117a852006-07-13 13:20:19 +000046#if defined(sun) || defined(_sun_) || defined(__solaris__)
vlm6534a8d2004-10-20 15:40:04 +000047#define _EMULATE_TIMEGM
48#endif
49
vlmbed6f812004-09-17 06:46:10 +000050/*
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
vlmd117a852006-07-13 13:20:19 +000070#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
vlmbed6f812004-09-17 06:46:10 +000079/*
80 * Override our GMTOFF decision for other known platforms.
81 */
82#ifdef __CYGWIN__
83#undef GMTOFF
84static 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#define _EMULATE_TIMEGM
98
99#endif /* __CYGWIN__ */
100
vlmd117a852006-07-13 13:20:19 +0000101#define ATZVARS do { \
102 char tzoldbuf[64]; \
103 char *tzold
104#define ATZSAVETZ do { \
105 tzold = getenv("TZ"); \
106 if(tzold) { \
107 size_t tzlen = strlen(tzold); \
108 if(tzlen < sizeof(tzoldbuf)) \
109 tzold = memcpy(tzoldbuf, tzold, tzlen + 1); \
110 else \
111 tzold = strdup(tzold); /* Ignore error */ \
112 setenv("TZ", "UTC", 1); \
113 } \
114 tzset(); \
115} while(0)
116#define ATZOLDTZ do { \
117 if (tzold) { \
118 setenv("TZ", tzold, 1); \
119 *tzoldbuf = 0; \
120 if(tzold != tzoldbuf) \
121 free(tzold); \
122 } else { \
123 unsetenv("TZ"); \
124 } \
125 tzset(); \
126} while(0); } while(0);
127
vlmbed6f812004-09-17 06:46:10 +0000128#ifdef _EMULATE_TIMEGM
vlm348a37a2004-08-12 03:58:25 +0000129static time_t timegm(struct tm *tm) {
130 time_t tloc;
vlmd117a852006-07-13 13:20:19 +0000131 ATZVARS;
132 ATZSAVETZ;
vlm348a37a2004-08-12 03:58:25 +0000133 tloc = mktime(tm);
vlmd117a852006-07-13 13:20:19 +0000134 ATZOLDTZ;
vlm348a37a2004-08-12 03:58:25 +0000135 return tloc;
136}
vlmbed6f812004-09-17 06:46:10 +0000137#endif /* _EMULATE_TIMEGM */
vlm348a37a2004-08-12 03:58:25 +0000138
vlm6e73a042004-08-11 07:17:22 +0000139
vlm5ea810e2005-07-03 05:32:40 +0000140#ifndef __ASN_INTERNAL_TEST_MODE__
vlmfa67ddc2004-06-03 03:38:44 +0000141
142/*
143 * GeneralizedTime basic type description.
144 */
vlmef6355b2004-09-29 13:26:15 +0000145static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = {
vlm6678cb12004-09-26 13:10:40 +0000146 (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/
147 (ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/
148 (ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */
vlmfa67ddc2004-06-03 03:38:44 +0000149};
vlmef6355b2004-09-29 13:26:15 +0000150asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
vlmfa67ddc2004-06-03 03:38:44 +0000151 "GeneralizedTime",
vlm9de248e2004-10-20 15:50:55 +0000152 "GeneralizedTime",
vlm39ba4c42004-09-22 16:06:28 +0000153 OCTET_STRING_free,
154 GeneralizedTime_print,
vlmfa67ddc2004-06-03 03:38:44 +0000155 GeneralizedTime_constraint, /* Check validity of time */
156 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
vlm9de248e2004-10-20 15:50:55 +0000157 GeneralizedTime_encode_der,
158 OCTET_STRING_decode_xer_utf8,
vlm39ba4c42004-09-22 16:06:28 +0000159 GeneralizedTime_encode_xer,
vlm18dd82c2006-08-18 01:34:18 +0000160 0, 0,
vlmfa67ddc2004-06-03 03:38:44 +0000161 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +0000162 asn_DEF_GeneralizedTime_tags,
163 sizeof(asn_DEF_GeneralizedTime_tags)
164 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
165 asn_DEF_GeneralizedTime_tags,
166 sizeof(asn_DEF_GeneralizedTime_tags)
167 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
vlm337167e2005-11-26 11:25:14 +0000168 0, /* No PER visible constraints */
vlme413c122004-08-20 13:23:42 +0000169 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +0000170 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +0000171};
172
vlm5ea810e2005-07-03 05:32:40 +0000173#endif /* __ASN_INTERNAL_TEST_MODE__ */
vlmfa67ddc2004-06-03 03:38:44 +0000174
175/*
176 * Check that the time looks like the time.
177 */
178int
vlmef6355b2004-09-29 13:26:15 +0000179GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmaf68ef52006-07-13 11:19:01 +0000180 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000181 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000182 time_t tloc;
183
184 errno = EPERM; /* Just an unlikely error code */
vlm81057a82004-08-07 03:52:26 +0000185 tloc = asn_GT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000186 if(tloc == -1 && errno != EPERM) {
vlmaf68ef52006-07-13 11:19:01 +0000187 _ASN_CTFAIL(app_key, td,
vlm758530a2004-08-22 13:47:59 +0000188 "%s: Invalid time format: %s (%s:%d)",
189 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000190 return -1;
191 }
192
193 return 0;
194}
195
vlm39ba4c42004-09-22 16:06:28 +0000196asn_enc_rval_t
vlm5ea810e2005-07-03 05:32:40 +0000197GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
vlm81057a82004-08-07 03:52:26 +0000198 int tag_mode, ber_tlv_tag_t tag,
199 asn_app_consume_bytes_f *cb, void *app_key) {
vlm5ea810e2005-07-03 05:32:40 +0000200 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
vlm39ba4c42004-09-22 16:06:28 +0000201 asn_enc_rval_t erval;
vlmaa116962005-07-04 12:21:51 +0000202 int fv, fd; /* seconds fraction value and number of digits */
vlm5ea810e2005-07-03 05:32:40 +0000203 struct tm tm;
204 time_t tloc;
vlm81057a82004-08-07 03:52:26 +0000205
vlm5ea810e2005-07-03 05:32:40 +0000206 /*
207 * Encode as a canonical DER.
208 */
209 errno = EPERM;
vlmaa116962005-07-04 12:21:51 +0000210 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
vlm5ea810e2005-07-03 05:32:40 +0000211 if(tloc == -1 && errno != EPERM)
212 /* Failed to recognize time. Fail completely. */
213 _ASN_ENCODE_FAILED;
vlm81057a82004-08-07 03:52:26 +0000214
vlmaa116962005-07-04 12:21:51 +0000215 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
vlm5ea810e2005-07-03 05:32:40 +0000216 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
vlm81057a82004-08-07 03:52:26 +0000217
218 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
219
vlm5ea810e2005-07-03 05:32:40 +0000220 FREEMEM(st->buf);
221 FREEMEM(st);
vlm81057a82004-08-07 03:52:26 +0000222
223 return erval;
224}
225
vlm5ea810e2005-07-03 05:32:40 +0000226#ifndef __ASN_INTERNAL_TEST_MODE__
227
vlm39ba4c42004-09-22 16:06:28 +0000228asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000229GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000230 int ilevel, enum xer_encoder_flags_e flags,
231 asn_app_consume_bytes_f *cb, void *app_key) {
vlm39ba4c42004-09-22 16:06:28 +0000232
233 if(flags & XER_F_CANONICAL) {
vlm5ea810e2005-07-03 05:32:40 +0000234 GeneralizedTime_t *gt;
235 asn_enc_rval_t rv;
vlmaa116962005-07-04 12:21:51 +0000236 int fv, fd; /* fractional parts */
vlm39ba4c42004-09-22 16:06:28 +0000237 struct tm tm;
vlm39ba4c42004-09-22 16:06:28 +0000238
239 errno = EPERM;
vlm5ea810e2005-07-03 05:32:40 +0000240 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
vlmaa116962005-07-04 12:21:51 +0000241 &fv, &fd, &tm, 1) == -1
vlm39ba4c42004-09-22 16:06:28 +0000242 && errno != EPERM)
243 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000244
vlmaa116962005-07-04 12:21:51 +0000245 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
vlm5ea810e2005-07-03 05:32:40 +0000246 if(!gt) _ASN_ENCODE_FAILED;
247
248 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
249 cb, app_key);
vlmbe78c802006-03-17 02:11:12 +0000250 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
vlm5ea810e2005-07-03 05:32:40 +0000251 return rv;
252 } else {
253 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
254 cb, app_key);
255 }
vlm39ba4c42004-09-22 16:06:28 +0000256}
257
vlm5ea810e2005-07-03 05:32:40 +0000258#endif /* __ASN_INTERNAL_TEST_MODE__ */
259
vlmfa67ddc2004-06-03 03:38:44 +0000260int
vlmef6355b2004-09-29 13:26:15 +0000261GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000262 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000263 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000264
vlmb42843a2004-06-05 08:17:50 +0000265 (void)td; /* Unused argument */
266 (void)ilevel; /* Unused argument */
267
vlmfa67ddc2004-06-03 03:38:44 +0000268 if(st && st->buf) {
269 char buf[32];
270 struct tm tm;
271 int ret;
272
273 errno = EPERM;
vlm81057a82004-08-07 03:52:26 +0000274 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
vlm6678cb12004-09-26 13:10:40 +0000275 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000276
277 ret = snprintf(buf, sizeof(buf),
vlm5ea810e2005-07-03 05:32:40 +0000278 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000279 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
280 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000281 assert(ret > 0 && ret < (int)sizeof(buf));
vlm6678cb12004-09-26 13:10:40 +0000282 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000283 } else {
vlm6678cb12004-09-26 13:10:40 +0000284 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000285 }
286}
287
vlmfa67ddc2004-06-03 03:38:44 +0000288time_t
vlm81057a82004-08-07 03:52:26 +0000289asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000290 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
291}
292
293time_t
vlmaa116962005-07-04 12:21:51 +0000294asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
295 time_t tloc;
296 int fv, fd = 0;
297
298 if(frac_value)
299 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
300 else
301 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
302 if(fd == 0 || frac_digits <= 0) {
303 *frac_value = 0;
304 } else {
305 while(fd > frac_digits)
306 fv /= 10, fd--;
307 while(fd < frac_digits) {
308 int new_fv = fv * 10;
309 if(new_fv / 10 != fv) {
310 /* Too long precision request */
311 fv = 0;
312 break;
313 }
314 fv = new_fv, fd++;
315 }
316
317 *frac_value = fv;
318 }
319
320 return tloc;
321}
322
323time_t
324asn_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 +0000325 struct tm tm_s;
326 uint8_t *buf;
327 uint8_t *end;
vlm81057a82004-08-07 03:52:26 +0000328 int gmtoff_h = 0;
329 int gmtoff_m = 0;
330 int gmtoff = 0; /* h + m */
vlmfa67ddc2004-06-03 03:38:44 +0000331 int offset_specified = 0;
vlmaa116962005-07-04 12:21:51 +0000332 int fvalue = 0;
333 int fdigits = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000334 time_t tloc;
335
336 if(!st || !st->buf) {
337 errno = EINVAL;
338 return -1;
339 } else {
340 buf = st->buf;
341 end = buf + st->size;
342 }
343
344 if(st->size < 10) {
345 errno = EINVAL;
346 return -1;
347 }
348
349 /*
350 * Decode first 10 bytes: "AAAAMMJJhh"
351 */
352 memset(&tm_s, 0, sizeof(tm_s));
353#undef B2F
354#undef B2T
355#define B2F(var) do { \
356 unsigned ch = *buf; \
vlm6eb79212006-01-10 08:08:14 +0000357 if(ch < 0x30 || ch > 0x39) { \
vlmfa67ddc2004-06-03 03:38:44 +0000358 errno = EINVAL; \
359 return -1; \
360 } else { \
361 var = var * 10 + (ch - 0x30); \
362 buf++; \
363 } \
364 } while(0)
365#define B2T(var) B2F(tm_s.var)
366
367 B2T(tm_year); /* 1: A */
368 B2T(tm_year); /* 2: A */
369 B2T(tm_year); /* 3: A */
370 B2T(tm_year); /* 4: A */
371 B2T(tm_mon); /* 5: M */
372 B2T(tm_mon); /* 6: M */
373 B2T(tm_mday); /* 7: J */
374 B2T(tm_mday); /* 8: J */
375 B2T(tm_hour); /* 9: h */
376 B2T(tm_hour); /* 0: h */
377
378 if(buf == end) goto local_finish;
379
380 /*
381 * Parse [mm[ss[(.|,)ffff]]]
382 * ^^
383 */
384 switch(*buf) {
385 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
386 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
387 tm_s.tm_min = (*buf++) - 0x30;
388 if(buf == end) { errno = EINVAL; return -1; }
389 B2T(tm_min);
390 break;
391 case 0x2B: case 0x2D: /* +, - */
392 goto offset;
393 case 0x5A: /* Z */
394 goto utc_finish;
395 default:
396 errno = EINVAL;
397 return -1;
398 }
399
400 if(buf == end) goto local_finish;
401
402 /*
403 * Parse [mm[ss[(.|,)ffff]]]
404 * ^^
405 */
406 switch(*buf) {
407 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
408 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
409 tm_s.tm_sec = (*buf++) - 0x30;
410 if(buf == end) { errno = EINVAL; return -1; }
411 B2T(tm_sec);
412 break;
413 case 0x2B: case 0x2D: /* +, - */
414 goto offset;
415 case 0x5A: /* Z */
416 goto utc_finish;
417 default:
418 errno = EINVAL;
419 return -1;
420 }
421
422 if(buf == end) goto local_finish;
423
424 /*
425 * Parse [mm[ss[(.|,)ffff]]]
426 * ^ ^
427 */
428 switch(*buf) {
vlm5ea810e2005-07-03 05:32:40 +0000429 case 0x2C: case 0x2E: /* (.|,) */
430 /*
431 * Process fractions of seconds.
432 */
vlmfa67ddc2004-06-03 03:38:44 +0000433 for(buf++; buf < end; buf++) {
vlm5ea810e2005-07-03 05:32:40 +0000434 int v = *buf;
vlmaa116962005-07-04 12:21:51 +0000435 int new_fvalue;
vlm5ea810e2005-07-03 05:32:40 +0000436 switch(v) {
vlmfa67ddc2004-06-03 03:38:44 +0000437 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
438 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
vlmaa116962005-07-04 12:21:51 +0000439 new_fvalue = fvalue * 10 + (v - 0x30);
440 if(new_fvalue / 10 != fvalue) {
vlm5ea810e2005-07-03 05:32:40 +0000441 /* Not enough precision, ignore */
442 } else {
vlmaa116962005-07-04 12:21:51 +0000443 fvalue = new_fvalue;
444 fdigits++;
vlm5ea810e2005-07-03 05:32:40 +0000445 }
vlmfa67ddc2004-06-03 03:38:44 +0000446 continue;
447 default:
448 break;
449 }
450 break;
451 }
452 }
453
454 if(buf == end) goto local_finish;
455
456 switch(*buf) {
457 case 0x2B: case 0x2D: /* +, - */
458 goto offset;
459 case 0x5A: /* Z */
460 goto utc_finish;
461 default:
462 errno = EINVAL;
463 return -1;
464 }
465
466
467offset:
468
469 if(end - buf < 3) {
470 errno = EINVAL;
471 return -1;
472 }
473 buf++;
vlm81057a82004-08-07 03:52:26 +0000474 B2F(gmtoff_h);
475 B2F(gmtoff_h);
vlmfa67ddc2004-06-03 03:38:44 +0000476 if(buf[-3] == 0x2D) /* Negative */
vlm81057a82004-08-07 03:52:26 +0000477 gmtoff = -1;
vlmfa67ddc2004-06-03 03:38:44 +0000478 else
vlm81057a82004-08-07 03:52:26 +0000479 gmtoff = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000480
481 if((end - buf) == 2) {
vlm81057a82004-08-07 03:52:26 +0000482 B2F(gmtoff_m);
483 B2F(gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000484 } else if(end != buf) {
485 errno = EINVAL;
486 return -1;
487 }
488
vlm81057a82004-08-07 03:52:26 +0000489 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000490
491 /* Fall through */
492utc_finish:
493
494 offset_specified = 1;
495
496 /* Fall through */
497local_finish:
498
499 /*
500 * Validation.
501 */
502 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
503 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
504 || (tm_s.tm_hour > 23)
505 || (tm_s.tm_sec > 60)
506 ) {
507 errno = EINVAL;
508 return -1;
509 }
510
511 /* Canonicalize */
512 tm_s.tm_mon -= 1; /* 0 - 11 */
513 tm_s.tm_year -= 1900;
514 tm_s.tm_isdst = -1;
515
vlm81057a82004-08-07 03:52:26 +0000516 tm_s.tm_sec -= gmtoff;
517
518 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
519
vlmc48f2132004-08-12 03:52:53 +0000520 if(offset_specified) {
vlm81057a82004-08-07 03:52:26 +0000521 tloc = timegm(&tm_s);
vlmc48f2132004-08-12 03:52:53 +0000522 } else {
vlm81057a82004-08-07 03:52:26 +0000523 /*
vlmef6355b2004-09-29 13:26:15 +0000524 * Without an offset (or "Z"),
vlm81057a82004-08-07 03:52:26 +0000525 * we can only guess that it is a local zone.
526 * Interpret it in this fashion.
527 */
528 tloc = mktime(&tm_s);
529 }
vlmfa67ddc2004-06-03 03:38:44 +0000530 if(tloc == -1) {
531 errno = EINVAL;
532 return -1;
533 }
534
vlm81057a82004-08-07 03:52:26 +0000535 if(ret_tm) {
536 if(as_gmt) {
537 if(offset_specified) {
538 *ret_tm = tm_s;
539 } else {
540 if(gmtime_r(&tloc, ret_tm) == 0) {
541 errno = EINVAL;
542 return -1;
543 }
544 }
545 } else {
546 if(localtime_r(&tloc, ret_tm) == 0) {
547 errno = EINVAL;
548 return -1;
549 }
vlmfa67ddc2004-06-03 03:38:44 +0000550 }
551 }
552
vlm5ea810e2005-07-03 05:32:40 +0000553 /* Fractions of seconds */
554 if(frac_value) *frac_value = fvalue;
vlmaa116962005-07-04 12:21:51 +0000555 if(frac_digits) *frac_digits = fdigits;
vlm5ea810e2005-07-03 05:32:40 +0000556
vlmfa67ddc2004-06-03 03:38:44 +0000557 return tloc;
558}
559
vlm81057a82004-08-07 03:52:26 +0000560GeneralizedTime_t *
561asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000562 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
563}
564
565GeneralizedTime_t *
vlmaa116962005-07-04 12:21:51 +0000566asn_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 +0000567 struct tm tm_s;
568 long gmtoff;
vlm5ea810e2005-07-03 05:32:40 +0000569 const unsigned int buf_size =
570 4 + 2 + 2 /* yyyymmdd */
571 + 2 + 2 + 2 /* hhmmss */
572 + 1 + 6 /* .ffffff */
573 + 1 + 4 /* +hhmm */
574 + 1 /* '\0' */
575 ;
vlm81057a82004-08-07 03:52:26 +0000576 char *buf;
577 char *p;
578 int size;
579
580 /* Check arguments */
581 if(!tm) {
582 errno = EINVAL;
583 return 0;
584 }
585
586 /* Pre-allocate a buffer of sufficient yet small length */
vlm6678cb12004-09-26 13:10:40 +0000587 buf = (char *)MALLOC(buf_size);
vlm81057a82004-08-07 03:52:26 +0000588 if(!buf) return 0;
589
590 gmtoff = GMTOFF(*tm);
591
592 if(force_gmt && gmtoff) {
593 tm_s = *tm;
594 tm_s.tm_sec -= gmtoff;
595 timegm(&tm_s); /* Fix the time */
vlm81057a82004-08-07 03:52:26 +0000596 tm = &tm_s;
vlm65efbd02004-08-11 07:35:08 +0000597#ifdef HAVE_TM_GMTOFF
598 assert(!GMTOFF(tm_s)); /* Will fix itself */
vlmd117a852006-07-13 13:20:19 +0000599#else /* !HAVE_TM_GMTOFF */
600 gmtoff = 0;
vlm65efbd02004-08-11 07:35:08 +0000601#endif
vlm81057a82004-08-07 03:52:26 +0000602 }
603
604 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
605 tm->tm_year + 1900,
606 tm->tm_mon + 1,
607 tm->tm_mday,
608 tm->tm_hour,
609 tm->tm_min,
610 tm->tm_sec
611 );
vlm5ea810e2005-07-03 05:32:40 +0000612 if(size != 14) {
613 /* Could be assert(size == 14); */
614 FREEMEM(buf);
615 errno = EINVAL;
616 return 0;
617 }
vlm81057a82004-08-07 03:52:26 +0000618
619 p = buf + size;
vlm5ea810e2005-07-03 05:32:40 +0000620
621 /*
622 * Deal with fractions.
623 */
vlmaa116962005-07-04 12:21:51 +0000624 if(frac_value > 0 && frac_digits > 0) {
vlm5ea810e2005-07-03 05:32:40 +0000625 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
vlm9d531932005-07-04 01:44:01 +0000626 char *z = p;
vlmaa116962005-07-04 12:21:51 +0000627 long fbase;
vlm9d531932005-07-04 01:44:01 +0000628 *z++ = '.';
vlmaa116962005-07-04 12:21:51 +0000629
630 /* Place bounds on precision */
631 while(frac_digits-- > 6)
632 frac_value /= 10;
633
634 /* emulate fbase = pow(10, frac_digits) */
635 for(fbase = 1; frac_digits--;)
636 fbase *= 10;
637
vlm5ea810e2005-07-03 05:32:40 +0000638 do {
vlmaa116962005-07-04 12:21:51 +0000639 int digit = frac_value / fbase;
vlmbaf858b2005-07-04 02:29:36 +0000640 if(digit > 9) { z = 0; break; }
vlm9d531932005-07-04 01:44:01 +0000641 *z++ = digit + 0x30;
vlmaa116962005-07-04 12:21:51 +0000642 frac_value %= fbase;
643 fbase /= 10;
644 } while(fbase > 0 && frac_value > 0 && z < end);
645 if(z) {
vlmbaf858b2005-07-04 02:29:36 +0000646 for(--z; *z == 0x30; --z); /* Strip zeroes */
647 p = z + (*z != '.');
648 size = p - buf;
649 }
vlm5ea810e2005-07-03 05:32:40 +0000650 }
651
vlm81057a82004-08-07 03:52:26 +0000652 if(force_gmt) {
vlmef6355b2004-09-29 13:26:15 +0000653 *p++ = 0x5a; /* "Z" */
vlm81057a82004-08-07 03:52:26 +0000654 *p++ = 0;
655 size++;
656 } else {
vlm5ea810e2005-07-03 05:32:40 +0000657 int ret;
658 gmtoff %= 86400;
659 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
660 gmtoff / 3600, labs(gmtoff % 3600));
661 if(ret != 5) {
662 FREEMEM(buf);
663 errno = EINVAL;
664 return 0;
665 }
vlm81057a82004-08-07 03:52:26 +0000666 size += ret;
667 }
668
669 if(opt_gt) {
670 if(opt_gt->buf)
671 FREEMEM(opt_gt->buf);
672 } else {
vlm6678cb12004-09-26 13:10:40 +0000673 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
vlm81057a82004-08-07 03:52:26 +0000674 if(!opt_gt) { free(buf); return 0; }
675 }
676
vlm1ff928d2004-08-11 08:10:13 +0000677 opt_gt->buf = (unsigned char *)buf;
vlm81057a82004-08-07 03:52:26 +0000678 opt_gt->size = size;
679
680 return opt_gt;
681}
682
683