blob: 982cca56ce55a09337b9fd581a4cc65aeb84c232 [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) \
vlmf5aff922006-09-13 04:02:00 +0000121 FREEMEM(tzold); \
vlmd117a852006-07-13 13:20:19 +0000122 } 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};
vlm86380d32006-10-09 12:07:58 +0000150static asn_per_constraints_t asn_DEF_GeneralizedTime_constraints = {
151 { APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, /* Value */
152 { APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, /* Size */
153 0, 0
154};
vlmef6355b2004-09-29 13:26:15 +0000155asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = {
vlmfa67ddc2004-06-03 03:38:44 +0000156 "GeneralizedTime",
vlm9de248e2004-10-20 15:50:55 +0000157 "GeneralizedTime",
vlm39ba4c42004-09-22 16:06:28 +0000158 OCTET_STRING_free,
159 GeneralizedTime_print,
vlmfa67ddc2004-06-03 03:38:44 +0000160 GeneralizedTime_constraint, /* Check validity of time */
161 OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */
vlm9de248e2004-10-20 15:50:55 +0000162 GeneralizedTime_encode_der,
163 OCTET_STRING_decode_xer_utf8,
vlm39ba4c42004-09-22 16:06:28 +0000164 GeneralizedTime_encode_xer,
vlm86380d32006-10-09 12:07:58 +0000165 OCTET_STRING_decode_uper,
166 OCTET_STRING_encode_uper,
vlmfa67ddc2004-06-03 03:38:44 +0000167 0, /* Use generic outmost tag fetcher */
vlmef6355b2004-09-29 13:26:15 +0000168 asn_DEF_GeneralizedTime_tags,
169 sizeof(asn_DEF_GeneralizedTime_tags)
170 / sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2,
171 asn_DEF_GeneralizedTime_tags,
172 sizeof(asn_DEF_GeneralizedTime_tags)
173 / sizeof(asn_DEF_GeneralizedTime_tags[0]),
vlm86380d32006-10-09 12:07:58 +0000174 &asn_DEF_GeneralizedTime_constraints,
vlme413c122004-08-20 13:23:42 +0000175 0, 0, /* No members */
vlmb42843a2004-06-05 08:17:50 +0000176 0 /* No specifics */
vlmfa67ddc2004-06-03 03:38:44 +0000177};
178
vlm5ea810e2005-07-03 05:32:40 +0000179#endif /* __ASN_INTERNAL_TEST_MODE__ */
vlmfa67ddc2004-06-03 03:38:44 +0000180
181/*
182 * Check that the time looks like the time.
183 */
184int
vlmef6355b2004-09-29 13:26:15 +0000185GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr,
vlmaf68ef52006-07-13 11:19:01 +0000186 asn_app_constraint_failed_f *ctfailcb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000187 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000188 time_t tloc;
189
190 errno = EPERM; /* Just an unlikely error code */
vlm81057a82004-08-07 03:52:26 +0000191 tloc = asn_GT2time(st, 0, 0);
vlmfa67ddc2004-06-03 03:38:44 +0000192 if(tloc == -1 && errno != EPERM) {
vlmaf68ef52006-07-13 11:19:01 +0000193 _ASN_CTFAIL(app_key, td,
vlm758530a2004-08-22 13:47:59 +0000194 "%s: Invalid time format: %s (%s:%d)",
195 td->name, strerror(errno), __FILE__, __LINE__);
vlmfa67ddc2004-06-03 03:38:44 +0000196 return -1;
197 }
198
199 return 0;
200}
201
vlm39ba4c42004-09-22 16:06:28 +0000202asn_enc_rval_t
vlm5ea810e2005-07-03 05:32:40 +0000203GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr,
vlm81057a82004-08-07 03:52:26 +0000204 int tag_mode, ber_tlv_tag_t tag,
205 asn_app_consume_bytes_f *cb, void *app_key) {
vlm5ea810e2005-07-03 05:32:40 +0000206 GeneralizedTime_t *st = (GeneralizedTime_t *)sptr;
vlm39ba4c42004-09-22 16:06:28 +0000207 asn_enc_rval_t erval;
vlmaa116962005-07-04 12:21:51 +0000208 int fv, fd; /* seconds fraction value and number of digits */
vlm5ea810e2005-07-03 05:32:40 +0000209 struct tm tm;
210 time_t tloc;
vlm81057a82004-08-07 03:52:26 +0000211
vlm5ea810e2005-07-03 05:32:40 +0000212 /*
213 * Encode as a canonical DER.
214 */
215 errno = EPERM;
vlmaa116962005-07-04 12:21:51 +0000216 tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */
vlm5ea810e2005-07-03 05:32:40 +0000217 if(tloc == -1 && errno != EPERM)
218 /* Failed to recognize time. Fail completely. */
219 _ASN_ENCODE_FAILED;
vlm81057a82004-08-07 03:52:26 +0000220
vlmaa116962005-07-04 12:21:51 +0000221 st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */
vlm5ea810e2005-07-03 05:32:40 +0000222 if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */
vlm81057a82004-08-07 03:52:26 +0000223
224 erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key);
225
vlm5ea810e2005-07-03 05:32:40 +0000226 FREEMEM(st->buf);
227 FREEMEM(st);
vlm81057a82004-08-07 03:52:26 +0000228
229 return erval;
230}
231
vlm5ea810e2005-07-03 05:32:40 +0000232#ifndef __ASN_INTERNAL_TEST_MODE__
233
vlm39ba4c42004-09-22 16:06:28 +0000234asn_enc_rval_t
vlmef6355b2004-09-29 13:26:15 +0000235GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr,
vlm39ba4c42004-09-22 16:06:28 +0000236 int ilevel, enum xer_encoder_flags_e flags,
237 asn_app_consume_bytes_f *cb, void *app_key) {
vlm39ba4c42004-09-22 16:06:28 +0000238
239 if(flags & XER_F_CANONICAL) {
vlm5ea810e2005-07-03 05:32:40 +0000240 GeneralizedTime_t *gt;
241 asn_enc_rval_t rv;
vlmaa116962005-07-04 12:21:51 +0000242 int fv, fd; /* fractional parts */
vlm39ba4c42004-09-22 16:06:28 +0000243 struct tm tm;
vlm39ba4c42004-09-22 16:06:28 +0000244
245 errno = EPERM;
vlm5ea810e2005-07-03 05:32:40 +0000246 if(asn_GT2time_frac((GeneralizedTime_t *)sptr,
vlmaa116962005-07-04 12:21:51 +0000247 &fv, &fd, &tm, 1) == -1
vlm39ba4c42004-09-22 16:06:28 +0000248 && errno != EPERM)
249 _ASN_ENCODE_FAILED;
vlm39ba4c42004-09-22 16:06:28 +0000250
vlmaa116962005-07-04 12:21:51 +0000251 gt = asn_time2GT_frac(0, &tm, fv, fd, 1);
vlm5ea810e2005-07-03 05:32:40 +0000252 if(!gt) _ASN_ENCODE_FAILED;
253
254 rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
255 cb, app_key);
vlmbe78c802006-03-17 02:11:12 +0000256 ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt);
vlm5ea810e2005-07-03 05:32:40 +0000257 return rv;
258 } else {
259 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
260 cb, app_key);
261 }
vlm39ba4c42004-09-22 16:06:28 +0000262}
263
vlm5ea810e2005-07-03 05:32:40 +0000264#endif /* __ASN_INTERNAL_TEST_MODE__ */
265
vlmfa67ddc2004-06-03 03:38:44 +0000266int
vlmef6355b2004-09-29 13:26:15 +0000267GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000268 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000269 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000270
vlmb42843a2004-06-05 08:17:50 +0000271 (void)td; /* Unused argument */
272 (void)ilevel; /* Unused argument */
273
vlmfa67ddc2004-06-03 03:38:44 +0000274 if(st && st->buf) {
275 char buf[32];
276 struct tm tm;
277 int ret;
278
279 errno = EPERM;
vlm81057a82004-08-07 03:52:26 +0000280 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
vlm6678cb12004-09-26 13:10:40 +0000281 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000282
283 ret = snprintf(buf, sizeof(buf),
vlm5ea810e2005-07-03 05:32:40 +0000284 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000285 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
286 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000287 assert(ret > 0 && ret < (int)sizeof(buf));
vlm6678cb12004-09-26 13:10:40 +0000288 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000289 } else {
vlm6678cb12004-09-26 13:10:40 +0000290 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000291 }
292}
293
vlmfa67ddc2004-06-03 03:38:44 +0000294time_t
vlm81057a82004-08-07 03:52:26 +0000295asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000296 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
297}
298
299time_t
vlmaa116962005-07-04 12:21:51 +0000300asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
301 time_t tloc;
302 int fv, fd = 0;
303
304 if(frac_value)
305 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
306 else
307 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
308 if(fd == 0 || frac_digits <= 0) {
309 *frac_value = 0;
310 } else {
311 while(fd > frac_digits)
312 fv /= 10, fd--;
313 while(fd < frac_digits) {
314 int new_fv = fv * 10;
315 if(new_fv / 10 != fv) {
316 /* Too long precision request */
317 fv = 0;
318 break;
319 }
320 fv = new_fv, fd++;
321 }
322
323 *frac_value = fv;
324 }
325
326 return tloc;
327}
328
329time_t
330asn_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 +0000331 struct tm tm_s;
332 uint8_t *buf;
333 uint8_t *end;
vlm81057a82004-08-07 03:52:26 +0000334 int gmtoff_h = 0;
335 int gmtoff_m = 0;
336 int gmtoff = 0; /* h + m */
vlmfa67ddc2004-06-03 03:38:44 +0000337 int offset_specified = 0;
vlmaa116962005-07-04 12:21:51 +0000338 int fvalue = 0;
339 int fdigits = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000340 time_t tloc;
341
342 if(!st || !st->buf) {
343 errno = EINVAL;
344 return -1;
345 } else {
346 buf = st->buf;
347 end = buf + st->size;
348 }
349
350 if(st->size < 10) {
351 errno = EINVAL;
352 return -1;
353 }
354
355 /*
356 * Decode first 10 bytes: "AAAAMMJJhh"
357 */
358 memset(&tm_s, 0, sizeof(tm_s));
359#undef B2F
360#undef B2T
361#define B2F(var) do { \
362 unsigned ch = *buf; \
vlm6eb79212006-01-10 08:08:14 +0000363 if(ch < 0x30 || ch > 0x39) { \
vlmfa67ddc2004-06-03 03:38:44 +0000364 errno = EINVAL; \
365 return -1; \
366 } else { \
367 var = var * 10 + (ch - 0x30); \
368 buf++; \
369 } \
370 } while(0)
371#define B2T(var) B2F(tm_s.var)
372
373 B2T(tm_year); /* 1: A */
374 B2T(tm_year); /* 2: A */
375 B2T(tm_year); /* 3: A */
376 B2T(tm_year); /* 4: A */
377 B2T(tm_mon); /* 5: M */
378 B2T(tm_mon); /* 6: M */
379 B2T(tm_mday); /* 7: J */
380 B2T(tm_mday); /* 8: J */
381 B2T(tm_hour); /* 9: h */
382 B2T(tm_hour); /* 0: h */
383
384 if(buf == end) goto local_finish;
385
386 /*
387 * Parse [mm[ss[(.|,)ffff]]]
388 * ^^
389 */
390 switch(*buf) {
391 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
392 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
393 tm_s.tm_min = (*buf++) - 0x30;
394 if(buf == end) { errno = EINVAL; return -1; }
395 B2T(tm_min);
396 break;
397 case 0x2B: case 0x2D: /* +, - */
398 goto offset;
399 case 0x5A: /* Z */
400 goto utc_finish;
401 default:
402 errno = EINVAL;
403 return -1;
404 }
405
406 if(buf == end) goto local_finish;
407
408 /*
409 * Parse [mm[ss[(.|,)ffff]]]
410 * ^^
411 */
412 switch(*buf) {
413 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
414 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
415 tm_s.tm_sec = (*buf++) - 0x30;
416 if(buf == end) { errno = EINVAL; return -1; }
417 B2T(tm_sec);
418 break;
419 case 0x2B: case 0x2D: /* +, - */
420 goto offset;
421 case 0x5A: /* Z */
422 goto utc_finish;
423 default:
424 errno = EINVAL;
425 return -1;
426 }
427
428 if(buf == end) goto local_finish;
429
430 /*
431 * Parse [mm[ss[(.|,)ffff]]]
432 * ^ ^
433 */
434 switch(*buf) {
vlm5ea810e2005-07-03 05:32:40 +0000435 case 0x2C: case 0x2E: /* (.|,) */
436 /*
437 * Process fractions of seconds.
438 */
vlmfa67ddc2004-06-03 03:38:44 +0000439 for(buf++; buf < end; buf++) {
vlm5ea810e2005-07-03 05:32:40 +0000440 int v = *buf;
vlmaa116962005-07-04 12:21:51 +0000441 int new_fvalue;
vlm5ea810e2005-07-03 05:32:40 +0000442 switch(v) {
vlmfa67ddc2004-06-03 03:38:44 +0000443 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
444 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
vlmaa116962005-07-04 12:21:51 +0000445 new_fvalue = fvalue * 10 + (v - 0x30);
446 if(new_fvalue / 10 != fvalue) {
vlm5ea810e2005-07-03 05:32:40 +0000447 /* Not enough precision, ignore */
448 } else {
vlmaa116962005-07-04 12:21:51 +0000449 fvalue = new_fvalue;
450 fdigits++;
vlm5ea810e2005-07-03 05:32:40 +0000451 }
vlmfa67ddc2004-06-03 03:38:44 +0000452 continue;
453 default:
454 break;
455 }
456 break;
457 }
458 }
459
460 if(buf == end) goto local_finish;
461
462 switch(*buf) {
463 case 0x2B: case 0x2D: /* +, - */
464 goto offset;
465 case 0x5A: /* Z */
466 goto utc_finish;
467 default:
468 errno = EINVAL;
469 return -1;
470 }
471
472
473offset:
474
475 if(end - buf < 3) {
476 errno = EINVAL;
477 return -1;
478 }
479 buf++;
vlm81057a82004-08-07 03:52:26 +0000480 B2F(gmtoff_h);
481 B2F(gmtoff_h);
vlmfa67ddc2004-06-03 03:38:44 +0000482 if(buf[-3] == 0x2D) /* Negative */
vlm81057a82004-08-07 03:52:26 +0000483 gmtoff = -1;
vlmfa67ddc2004-06-03 03:38:44 +0000484 else
vlm81057a82004-08-07 03:52:26 +0000485 gmtoff = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000486
487 if((end - buf) == 2) {
vlm81057a82004-08-07 03:52:26 +0000488 B2F(gmtoff_m);
489 B2F(gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000490 } else if(end != buf) {
491 errno = EINVAL;
492 return -1;
493 }
494
vlm81057a82004-08-07 03:52:26 +0000495 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000496
497 /* Fall through */
498utc_finish:
499
500 offset_specified = 1;
501
502 /* Fall through */
503local_finish:
504
505 /*
506 * Validation.
507 */
508 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
509 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
510 || (tm_s.tm_hour > 23)
511 || (tm_s.tm_sec > 60)
512 ) {
513 errno = EINVAL;
514 return -1;
515 }
516
517 /* Canonicalize */
518 tm_s.tm_mon -= 1; /* 0 - 11 */
519 tm_s.tm_year -= 1900;
520 tm_s.tm_isdst = -1;
521
vlm81057a82004-08-07 03:52:26 +0000522 tm_s.tm_sec -= gmtoff;
523
524 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
525
vlmc48f2132004-08-12 03:52:53 +0000526 if(offset_specified) {
vlm81057a82004-08-07 03:52:26 +0000527 tloc = timegm(&tm_s);
vlmc48f2132004-08-12 03:52:53 +0000528 } else {
vlm81057a82004-08-07 03:52:26 +0000529 /*
vlmef6355b2004-09-29 13:26:15 +0000530 * Without an offset (or "Z"),
vlm81057a82004-08-07 03:52:26 +0000531 * we can only guess that it is a local zone.
532 * Interpret it in this fashion.
533 */
534 tloc = mktime(&tm_s);
535 }
vlmfa67ddc2004-06-03 03:38:44 +0000536 if(tloc == -1) {
537 errno = EINVAL;
538 return -1;
539 }
540
vlm81057a82004-08-07 03:52:26 +0000541 if(ret_tm) {
542 if(as_gmt) {
543 if(offset_specified) {
544 *ret_tm = tm_s;
545 } else {
546 if(gmtime_r(&tloc, ret_tm) == 0) {
547 errno = EINVAL;
548 return -1;
549 }
550 }
551 } else {
552 if(localtime_r(&tloc, ret_tm) == 0) {
553 errno = EINVAL;
554 return -1;
555 }
vlmfa67ddc2004-06-03 03:38:44 +0000556 }
557 }
558
vlm5ea810e2005-07-03 05:32:40 +0000559 /* Fractions of seconds */
560 if(frac_value) *frac_value = fvalue;
vlmaa116962005-07-04 12:21:51 +0000561 if(frac_digits) *frac_digits = fdigits;
vlm5ea810e2005-07-03 05:32:40 +0000562
vlmfa67ddc2004-06-03 03:38:44 +0000563 return tloc;
564}
565
vlm81057a82004-08-07 03:52:26 +0000566GeneralizedTime_t *
567asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000568 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
569}
570
571GeneralizedTime_t *
vlmaa116962005-07-04 12:21:51 +0000572asn_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 +0000573 struct tm tm_s;
574 long gmtoff;
vlm5ea810e2005-07-03 05:32:40 +0000575 const unsigned int buf_size =
576 4 + 2 + 2 /* yyyymmdd */
577 + 2 + 2 + 2 /* hhmmss */
578 + 1 + 6 /* .ffffff */
579 + 1 + 4 /* +hhmm */
580 + 1 /* '\0' */
581 ;
vlm81057a82004-08-07 03:52:26 +0000582 char *buf;
583 char *p;
584 int size;
585
586 /* Check arguments */
587 if(!tm) {
588 errno = EINVAL;
589 return 0;
590 }
591
592 /* Pre-allocate a buffer of sufficient yet small length */
vlm6678cb12004-09-26 13:10:40 +0000593 buf = (char *)MALLOC(buf_size);
vlm81057a82004-08-07 03:52:26 +0000594 if(!buf) return 0;
595
596 gmtoff = GMTOFF(*tm);
597
598 if(force_gmt && gmtoff) {
599 tm_s = *tm;
600 tm_s.tm_sec -= gmtoff;
601 timegm(&tm_s); /* Fix the time */
vlm81057a82004-08-07 03:52:26 +0000602 tm = &tm_s;
vlm65efbd02004-08-11 07:35:08 +0000603#ifdef HAVE_TM_GMTOFF
604 assert(!GMTOFF(tm_s)); /* Will fix itself */
vlmd117a852006-07-13 13:20:19 +0000605#else /* !HAVE_TM_GMTOFF */
606 gmtoff = 0;
vlm65efbd02004-08-11 07:35:08 +0000607#endif
vlm81057a82004-08-07 03:52:26 +0000608 }
609
610 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
611 tm->tm_year + 1900,
612 tm->tm_mon + 1,
613 tm->tm_mday,
614 tm->tm_hour,
615 tm->tm_min,
616 tm->tm_sec
617 );
vlm5ea810e2005-07-03 05:32:40 +0000618 if(size != 14) {
619 /* Could be assert(size == 14); */
620 FREEMEM(buf);
621 errno = EINVAL;
622 return 0;
623 }
vlm81057a82004-08-07 03:52:26 +0000624
625 p = buf + size;
vlm5ea810e2005-07-03 05:32:40 +0000626
627 /*
628 * Deal with fractions.
629 */
vlmaa116962005-07-04 12:21:51 +0000630 if(frac_value > 0 && frac_digits > 0) {
vlm5ea810e2005-07-03 05:32:40 +0000631 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
vlm9d531932005-07-04 01:44:01 +0000632 char *z = p;
vlmaa116962005-07-04 12:21:51 +0000633 long fbase;
vlm9d531932005-07-04 01:44:01 +0000634 *z++ = '.';
vlmaa116962005-07-04 12:21:51 +0000635
636 /* Place bounds on precision */
637 while(frac_digits-- > 6)
638 frac_value /= 10;
639
640 /* emulate fbase = pow(10, frac_digits) */
641 for(fbase = 1; frac_digits--;)
642 fbase *= 10;
643
vlm5ea810e2005-07-03 05:32:40 +0000644 do {
vlmaa116962005-07-04 12:21:51 +0000645 int digit = frac_value / fbase;
vlmbaf858b2005-07-04 02:29:36 +0000646 if(digit > 9) { z = 0; break; }
vlm9d531932005-07-04 01:44:01 +0000647 *z++ = digit + 0x30;
vlmaa116962005-07-04 12:21:51 +0000648 frac_value %= fbase;
649 fbase /= 10;
650 } while(fbase > 0 && frac_value > 0 && z < end);
651 if(z) {
vlmbaf858b2005-07-04 02:29:36 +0000652 for(--z; *z == 0x30; --z); /* Strip zeroes */
653 p = z + (*z != '.');
654 size = p - buf;
655 }
vlm5ea810e2005-07-03 05:32:40 +0000656 }
657
vlm81057a82004-08-07 03:52:26 +0000658 if(force_gmt) {
vlmef6355b2004-09-29 13:26:15 +0000659 *p++ = 0x5a; /* "Z" */
vlm81057a82004-08-07 03:52:26 +0000660 *p++ = 0;
661 size++;
662 } else {
vlm5ea810e2005-07-03 05:32:40 +0000663 int ret;
664 gmtoff %= 86400;
665 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
666 gmtoff / 3600, labs(gmtoff % 3600));
667 if(ret != 5) {
668 FREEMEM(buf);
669 errno = EINVAL;
670 return 0;
671 }
vlm81057a82004-08-07 03:52:26 +0000672 size += ret;
673 }
674
675 if(opt_gt) {
676 if(opt_gt->buf)
677 FREEMEM(opt_gt->buf);
678 } else {
vlm6678cb12004-09-26 13:10:40 +0000679 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
vlmf5aff922006-09-13 04:02:00 +0000680 if(!opt_gt) { FREEMEM(buf); return 0; }
vlm81057a82004-08-07 03:52:26 +0000681 }
682
vlm1ff928d2004-08-11 08:10:13 +0000683 opt_gt->buf = (unsigned char *)buf;
vlm81057a82004-08-07 03:52:26 +0000684 opt_gt->size = size;
685
686 return opt_gt;
687}
688
689