blob: 6934a820049a274000c557e9ded1120da75622fc [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);
221 asn_DEF_GeneralizedTime.free_struct(&asn_DEF_GeneralizedTime,
222 gt, 0);
223 return rv;
224 } else {
225 return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags,
226 cb, app_key);
227 }
vlm39ba4c42004-09-22 16:06:28 +0000228}
229
vlm5ea810e2005-07-03 05:32:40 +0000230#endif /* __ASN_INTERNAL_TEST_MODE__ */
231
vlmfa67ddc2004-06-03 03:38:44 +0000232int
vlmef6355b2004-09-29 13:26:15 +0000233GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel,
vlmfa67ddc2004-06-03 03:38:44 +0000234 asn_app_consume_bytes_f *cb, void *app_key) {
vlmda674682004-08-11 09:07:36 +0000235 const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr;
vlmfa67ddc2004-06-03 03:38:44 +0000236
vlmb42843a2004-06-05 08:17:50 +0000237 (void)td; /* Unused argument */
238 (void)ilevel; /* Unused argument */
239
vlmfa67ddc2004-06-03 03:38:44 +0000240 if(st && st->buf) {
241 char buf[32];
242 struct tm tm;
243 int ret;
244
245 errno = EPERM;
vlm81057a82004-08-07 03:52:26 +0000246 if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM)
vlm6678cb12004-09-26 13:10:40 +0000247 return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000248
249 ret = snprintf(buf, sizeof(buf),
vlm5ea810e2005-07-03 05:32:40 +0000250 "%04d-%02d-%02d %02d:%02d:%02d (GMT)",
vlmfa67ddc2004-06-03 03:38:44 +0000251 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
252 tm.tm_hour, tm.tm_min, tm.tm_sec);
vlmb42843a2004-06-05 08:17:50 +0000253 assert(ret > 0 && ret < (int)sizeof(buf));
vlm6678cb12004-09-26 13:10:40 +0000254 return (cb(buf, ret, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000255 } else {
vlm6678cb12004-09-26 13:10:40 +0000256 return (cb("<absent>", 8, app_key) < 0) ? -1 : 0;
vlmfa67ddc2004-06-03 03:38:44 +0000257 }
258}
259
vlmfa67ddc2004-06-03 03:38:44 +0000260time_t
vlm81057a82004-08-07 03:52:26 +0000261asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000262 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
263}
264
265time_t
vlmaa116962005-07-04 12:21:51 +0000266asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) {
267 time_t tloc;
268 int fv, fd = 0;
269
270 if(frac_value)
271 tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt);
272 else
273 return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt);
274 if(fd == 0 || frac_digits <= 0) {
275 *frac_value = 0;
276 } else {
277 while(fd > frac_digits)
278 fv /= 10, fd--;
279 while(fd < frac_digits) {
280 int new_fv = fv * 10;
281 if(new_fv / 10 != fv) {
282 /* Too long precision request */
283 fv = 0;
284 break;
285 }
286 fv = new_fv, fd++;
287 }
288
289 *frac_value = fv;
290 }
291
292 return tloc;
293}
294
295time_t
296asn_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 +0000297 struct tm tm_s;
298 uint8_t *buf;
299 uint8_t *end;
vlm81057a82004-08-07 03:52:26 +0000300 int gmtoff_h = 0;
301 int gmtoff_m = 0;
302 int gmtoff = 0; /* h + m */
vlmfa67ddc2004-06-03 03:38:44 +0000303 int offset_specified = 0;
vlmaa116962005-07-04 12:21:51 +0000304 int fvalue = 0;
305 int fdigits = 0;
vlmfa67ddc2004-06-03 03:38:44 +0000306 time_t tloc;
307
308 if(!st || !st->buf) {
309 errno = EINVAL;
310 return -1;
311 } else {
312 buf = st->buf;
313 end = buf + st->size;
314 }
315
316 if(st->size < 10) {
317 errno = EINVAL;
318 return -1;
319 }
320
321 /*
322 * Decode first 10 bytes: "AAAAMMJJhh"
323 */
324 memset(&tm_s, 0, sizeof(tm_s));
325#undef B2F
326#undef B2T
327#define B2F(var) do { \
328 unsigned ch = *buf; \
vlm6eb79212006-01-10 08:08:14 +0000329 if(ch < 0x30 || ch > 0x39) { \
vlmfa67ddc2004-06-03 03:38:44 +0000330 errno = EINVAL; \
331 return -1; \
332 } else { \
333 var = var * 10 + (ch - 0x30); \
334 buf++; \
335 } \
336 } while(0)
337#define B2T(var) B2F(tm_s.var)
338
339 B2T(tm_year); /* 1: A */
340 B2T(tm_year); /* 2: A */
341 B2T(tm_year); /* 3: A */
342 B2T(tm_year); /* 4: A */
343 B2T(tm_mon); /* 5: M */
344 B2T(tm_mon); /* 6: M */
345 B2T(tm_mday); /* 7: J */
346 B2T(tm_mday); /* 8: J */
347 B2T(tm_hour); /* 9: h */
348 B2T(tm_hour); /* 0: h */
349
350 if(buf == end) goto local_finish;
351
352 /*
353 * Parse [mm[ss[(.|,)ffff]]]
354 * ^^
355 */
356 switch(*buf) {
357 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
358 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
359 tm_s.tm_min = (*buf++) - 0x30;
360 if(buf == end) { errno = EINVAL; return -1; }
361 B2T(tm_min);
362 break;
363 case 0x2B: case 0x2D: /* +, - */
364 goto offset;
365 case 0x5A: /* Z */
366 goto utc_finish;
367 default:
368 errno = EINVAL;
369 return -1;
370 }
371
372 if(buf == end) goto local_finish;
373
374 /*
375 * Parse [mm[ss[(.|,)ffff]]]
376 * ^^
377 */
378 switch(*buf) {
379 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
380 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
381 tm_s.tm_sec = (*buf++) - 0x30;
382 if(buf == end) { errno = EINVAL; return -1; }
383 B2T(tm_sec);
384 break;
385 case 0x2B: case 0x2D: /* +, - */
386 goto offset;
387 case 0x5A: /* Z */
388 goto utc_finish;
389 default:
390 errno = EINVAL;
391 return -1;
392 }
393
394 if(buf == end) goto local_finish;
395
396 /*
397 * Parse [mm[ss[(.|,)ffff]]]
398 * ^ ^
399 */
400 switch(*buf) {
vlm5ea810e2005-07-03 05:32:40 +0000401 case 0x2C: case 0x2E: /* (.|,) */
402 /*
403 * Process fractions of seconds.
404 */
vlmfa67ddc2004-06-03 03:38:44 +0000405 for(buf++; buf < end; buf++) {
vlm5ea810e2005-07-03 05:32:40 +0000406 int v = *buf;
vlmaa116962005-07-04 12:21:51 +0000407 int new_fvalue;
vlm5ea810e2005-07-03 05:32:40 +0000408 switch(v) {
vlmfa67ddc2004-06-03 03:38:44 +0000409 case 0x30: case 0x31: case 0x32: case 0x33: case 0x34:
410 case 0x35: case 0x36: case 0x37: case 0x38: case 0x39:
vlmaa116962005-07-04 12:21:51 +0000411 new_fvalue = fvalue * 10 + (v - 0x30);
412 if(new_fvalue / 10 != fvalue) {
vlm5ea810e2005-07-03 05:32:40 +0000413 /* Not enough precision, ignore */
414 } else {
vlmaa116962005-07-04 12:21:51 +0000415 fvalue = new_fvalue;
416 fdigits++;
vlm5ea810e2005-07-03 05:32:40 +0000417 }
vlmfa67ddc2004-06-03 03:38:44 +0000418 continue;
419 default:
420 break;
421 }
422 break;
423 }
424 }
425
426 if(buf == end) goto local_finish;
427
428 switch(*buf) {
429 case 0x2B: case 0x2D: /* +, - */
430 goto offset;
431 case 0x5A: /* Z */
432 goto utc_finish;
433 default:
434 errno = EINVAL;
435 return -1;
436 }
437
438
439offset:
440
441 if(end - buf < 3) {
442 errno = EINVAL;
443 return -1;
444 }
445 buf++;
vlm81057a82004-08-07 03:52:26 +0000446 B2F(gmtoff_h);
447 B2F(gmtoff_h);
vlmfa67ddc2004-06-03 03:38:44 +0000448 if(buf[-3] == 0x2D) /* Negative */
vlm81057a82004-08-07 03:52:26 +0000449 gmtoff = -1;
vlmfa67ddc2004-06-03 03:38:44 +0000450 else
vlm81057a82004-08-07 03:52:26 +0000451 gmtoff = 1;
vlmfa67ddc2004-06-03 03:38:44 +0000452
453 if((end - buf) == 2) {
vlm81057a82004-08-07 03:52:26 +0000454 B2F(gmtoff_m);
455 B2F(gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000456 } else if(end != buf) {
457 errno = EINVAL;
458 return -1;
459 }
460
vlm81057a82004-08-07 03:52:26 +0000461 gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m);
vlmfa67ddc2004-06-03 03:38:44 +0000462
463 /* Fall through */
464utc_finish:
465
466 offset_specified = 1;
467
468 /* Fall through */
469local_finish:
470
471 /*
472 * Validation.
473 */
474 if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1)
475 || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1)
476 || (tm_s.tm_hour > 23)
477 || (tm_s.tm_sec > 60)
478 ) {
479 errno = EINVAL;
480 return -1;
481 }
482
483 /* Canonicalize */
484 tm_s.tm_mon -= 1; /* 0 - 11 */
485 tm_s.tm_year -= 1900;
486 tm_s.tm_isdst = -1;
487
vlm81057a82004-08-07 03:52:26 +0000488 tm_s.tm_sec -= gmtoff;
489
490 /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/
491
vlmc48f2132004-08-12 03:52:53 +0000492 if(offset_specified) {
vlm81057a82004-08-07 03:52:26 +0000493 tloc = timegm(&tm_s);
vlmc48f2132004-08-12 03:52:53 +0000494 } else {
vlm81057a82004-08-07 03:52:26 +0000495 /*
vlmef6355b2004-09-29 13:26:15 +0000496 * Without an offset (or "Z"),
vlm81057a82004-08-07 03:52:26 +0000497 * we can only guess that it is a local zone.
498 * Interpret it in this fashion.
499 */
500 tloc = mktime(&tm_s);
501 }
vlmfa67ddc2004-06-03 03:38:44 +0000502 if(tloc == -1) {
503 errno = EINVAL;
504 return -1;
505 }
506
vlm81057a82004-08-07 03:52:26 +0000507 if(ret_tm) {
508 if(as_gmt) {
509 if(offset_specified) {
510 *ret_tm = tm_s;
511 } else {
512 if(gmtime_r(&tloc, ret_tm) == 0) {
513 errno = EINVAL;
514 return -1;
515 }
516 }
517 } else {
518 if(localtime_r(&tloc, ret_tm) == 0) {
519 errno = EINVAL;
520 return -1;
521 }
vlmfa67ddc2004-06-03 03:38:44 +0000522 }
523 }
524
vlm5ea810e2005-07-03 05:32:40 +0000525 /* Fractions of seconds */
526 if(frac_value) *frac_value = fvalue;
vlmaa116962005-07-04 12:21:51 +0000527 if(frac_digits) *frac_digits = fdigits;
vlm5ea810e2005-07-03 05:32:40 +0000528
vlmfa67ddc2004-06-03 03:38:44 +0000529 return tloc;
530}
531
vlm81057a82004-08-07 03:52:26 +0000532GeneralizedTime_t *
533asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) {
vlm5ea810e2005-07-03 05:32:40 +0000534 return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt);
535}
536
537GeneralizedTime_t *
vlmaa116962005-07-04 12:21:51 +0000538asn_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 +0000539 struct tm tm_s;
540 long gmtoff;
vlm5ea810e2005-07-03 05:32:40 +0000541 const unsigned int buf_size =
542 4 + 2 + 2 /* yyyymmdd */
543 + 2 + 2 + 2 /* hhmmss */
544 + 1 + 6 /* .ffffff */
545 + 1 + 4 /* +hhmm */
546 + 1 /* '\0' */
547 ;
vlm81057a82004-08-07 03:52:26 +0000548 char *buf;
549 char *p;
550 int size;
551
552 /* Check arguments */
553 if(!tm) {
554 errno = EINVAL;
555 return 0;
556 }
557
558 /* Pre-allocate a buffer of sufficient yet small length */
vlm6678cb12004-09-26 13:10:40 +0000559 buf = (char *)MALLOC(buf_size);
vlm81057a82004-08-07 03:52:26 +0000560 if(!buf) return 0;
561
562 gmtoff = GMTOFF(*tm);
563
564 if(force_gmt && gmtoff) {
565 tm_s = *tm;
566 tm_s.tm_sec -= gmtoff;
567 timegm(&tm_s); /* Fix the time */
vlm81057a82004-08-07 03:52:26 +0000568 tm = &tm_s;
vlm65efbd02004-08-11 07:35:08 +0000569#ifdef HAVE_TM_GMTOFF
570 assert(!GMTOFF(tm_s)); /* Will fix itself */
571#else
572 gmtoff = 0; /* Intervention required */
573#endif
vlm81057a82004-08-07 03:52:26 +0000574 }
575
576 size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d",
577 tm->tm_year + 1900,
578 tm->tm_mon + 1,
579 tm->tm_mday,
580 tm->tm_hour,
581 tm->tm_min,
582 tm->tm_sec
583 );
vlm5ea810e2005-07-03 05:32:40 +0000584 if(size != 14) {
585 /* Could be assert(size == 14); */
586 FREEMEM(buf);
587 errno = EINVAL;
588 return 0;
589 }
vlm81057a82004-08-07 03:52:26 +0000590
591 p = buf + size;
vlm5ea810e2005-07-03 05:32:40 +0000592
593 /*
594 * Deal with fractions.
595 */
vlmaa116962005-07-04 12:21:51 +0000596 if(frac_value > 0 && frac_digits > 0) {
vlm5ea810e2005-07-03 05:32:40 +0000597 char *end = p + 1 + 6; /* '.' + maximum 6 digits */
vlm9d531932005-07-04 01:44:01 +0000598 char *z = p;
vlmaa116962005-07-04 12:21:51 +0000599 long fbase;
vlm9d531932005-07-04 01:44:01 +0000600 *z++ = '.';
vlmaa116962005-07-04 12:21:51 +0000601
602 /* Place bounds on precision */
603 while(frac_digits-- > 6)
604 frac_value /= 10;
605
606 /* emulate fbase = pow(10, frac_digits) */
607 for(fbase = 1; frac_digits--;)
608 fbase *= 10;
609
vlm5ea810e2005-07-03 05:32:40 +0000610 do {
vlmaa116962005-07-04 12:21:51 +0000611 int digit = frac_value / fbase;
vlmbaf858b2005-07-04 02:29:36 +0000612 if(digit > 9) { z = 0; break; }
vlm9d531932005-07-04 01:44:01 +0000613 *z++ = digit + 0x30;
vlmaa116962005-07-04 12:21:51 +0000614 frac_value %= fbase;
615 fbase /= 10;
616 } while(fbase > 0 && frac_value > 0 && z < end);
617 if(z) {
vlmbaf858b2005-07-04 02:29:36 +0000618 for(--z; *z == 0x30; --z); /* Strip zeroes */
619 p = z + (*z != '.');
620 size = p - buf;
621 }
vlm5ea810e2005-07-03 05:32:40 +0000622 }
623
vlm81057a82004-08-07 03:52:26 +0000624 if(force_gmt) {
vlmef6355b2004-09-29 13:26:15 +0000625 *p++ = 0x5a; /* "Z" */
vlm81057a82004-08-07 03:52:26 +0000626 *p++ = 0;
627 size++;
628 } else {
vlm5ea810e2005-07-03 05:32:40 +0000629 int ret;
630 gmtoff %= 86400;
631 ret = snprintf(p, buf_size - size, "%+03ld%02ld",
632 gmtoff / 3600, labs(gmtoff % 3600));
633 if(ret != 5) {
634 FREEMEM(buf);
635 errno = EINVAL;
636 return 0;
637 }
vlm81057a82004-08-07 03:52:26 +0000638 size += ret;
639 }
640
641 if(opt_gt) {
642 if(opt_gt->buf)
643 FREEMEM(opt_gt->buf);
644 } else {
vlm6678cb12004-09-26 13:10:40 +0000645 opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt);
vlm81057a82004-08-07 03:52:26 +0000646 if(!opt_gt) { free(buf); return 0; }
647 }
648
vlm1ff928d2004-08-11 08:10:13 +0000649 opt_gt->buf = (unsigned char *)buf;
vlm81057a82004-08-07 03:52:26 +0000650 opt_gt->size = size;
651
652 return opt_gt;
653}
654
655