| 1 |
/*- |
| 2 |
* Copyright (c) 2003, 2004 Lev Walkin <[email protected]>. All rights reserved. |
| 3 |
* Redistribution and modifications are permitted subject to BSD license. |
| 4 |
*/ |
| 5 |
#define _POSIX_PTHREAD_SEMANTICS /* for Sun */ |
| 6 |
#define _REENTRANT /* for Sun */ |
| 7 |
#include <asn_internal.h> |
| 8 |
#include <GeneralizedTime.h> |
| 9 |
#include <errno.h> |
| 10 |
|
| 11 |
#ifdef __CYGWIN__ |
| 12 |
#include "/usr/include/time.h" |
| 13 |
#else |
| 14 |
#include <time.h> |
| 15 |
#endif /* __CYGWIN__ */ |
| 16 |
|
| 17 |
#if defined(WIN32) |
| 18 |
#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().") |
| 24 |
#pragma message( "PLEASE STOP AND READ!") |
| 25 |
|
| 26 |
static struct tm *localtime_r(const time_t *tloc, struct tm *result) { |
| 27 |
struct tm *tm; |
| 28 |
if((tm = localtime(tloc))) |
| 29 |
return memcpy(result, tm, sizeof(struct tm)); |
| 30 |
return 0; |
| 31 |
} |
| 32 |
|
| 33 |
static struct tm *gmtime_r(const time_t *tloc, struct tm *result) { |
| 34 |
struct tm *tm; |
| 35 |
if((tm = gmtime(tloc))) |
| 36 |
return memcpy(result, tm, sizeof(struct tm)); |
| 37 |
return 0; |
| 38 |
} |
| 39 |
|
| 40 |
#define tzset() _tzset() |
| 41 |
#define putenv(c) _putenv(c) |
| 42 |
#define _EMULATE_TIMEGM |
| 43 |
|
| 44 |
#endif /* WIN32 */ |
| 45 |
|
| 46 |
#if defined(sun) || defined(_sun_) || defined(__solaris__) |
| 47 |
#define _EMULATE_TIMEGM |
| 48 |
#endif |
| 49 |
|
| 50 |
/* |
| 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 |
|
| 70 |
#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 |
|
| 79 |
/* |
| 80 |
* Override our GMTOFF decision for other known platforms. |
| 81 |
*/ |
| 82 |
#ifdef __CYGWIN__ |
| 83 |
#undef GMTOFF |
| 84 |
static 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 |
|
| 101 |
#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 |
#ifdef __MINGW32__ \ |
| 113 |
putenv("TZ= UTC"); \ |
| 114 |
#else \ |
| 115 |
setenv("TZ", "UTC", 1); \ |
| 116 |
#endif \ |
| 117 |
} \ |
| 118 |
tzset(); \ |
| 119 |
} while(0) |
| 120 |
#define ATZOLDTZ do { \ |
| 121 |
if (tzold) { \ |
| 122 |
#ifdef __MINGW32__ \ |
| 123 |
putenv("TZ= UTC"); \ |
| 124 |
#else \ |
| 125 |
setenv("TZ", tzold, 1); \ |
| 126 |
#endif \ |
| 127 |
*tzoldbuf = 0; \ |
| 128 |
if(tzold != tzoldbuf) \ |
| 129 |
FREEMEM(tzold); \ |
| 130 |
} else { \ |
| 131 |
#ifdef __MINGW32__ \ |
| 132 |
putenv("TZ= "); \ |
| 133 |
#else \ |
| 134 |
unsetenv("TZ"); \ |
| 135 |
#endif \ |
| 136 |
} \ |
| 137 |
tzset(); \ |
| 138 |
} while(0); } while(0); |
| 139 |
|
| 140 |
#ifdef _EMULATE_TIMEGM |
| 141 |
static time_t timegm(struct tm *tm) { |
| 142 |
time_t tloc; |
| 143 |
ATZVARS; |
| 144 |
ATZSAVETZ; |
| 145 |
tloc = mktime(tm); |
| 146 |
ATZOLDTZ; |
| 147 |
return tloc; |
| 148 |
} |
| 149 |
#endif /* _EMULATE_TIMEGM */ |
| 150 |
|
| 151 |
|
| 152 |
#ifndef __ASN_INTERNAL_TEST_MODE__ |
| 153 |
|
| 154 |
/* |
| 155 |
* GeneralizedTime basic type description. |
| 156 |
*/ |
| 157 |
static ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = { |
| 158 |
(ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), /* [UNIVERSAL 24] IMPLICIT ...*/ |
| 159 |
(ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), /* [UNIVERSAL 26] IMPLICIT ...*/ |
| 160 |
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) /* ... OCTET STRING */ |
| 161 |
}; |
| 162 |
asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = { |
| 163 |
"GeneralizedTime", |
| 164 |
"GeneralizedTime", |
| 165 |
OCTET_STRING_free, |
| 166 |
GeneralizedTime_print, |
| 167 |
GeneralizedTime_constraint, /* Check validity of time */ |
| 168 |
OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
| 169 |
GeneralizedTime_encode_der, |
| 170 |
OCTET_STRING_decode_xer_utf8, |
| 171 |
GeneralizedTime_encode_xer, |
| 172 |
0, 0, |
| 173 |
0, /* Use generic outmost tag fetcher */ |
| 174 |
asn_DEF_GeneralizedTime_tags, |
| 175 |
sizeof(asn_DEF_GeneralizedTime_tags) |
| 176 |
/ sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2, |
| 177 |
asn_DEF_GeneralizedTime_tags, |
| 178 |
sizeof(asn_DEF_GeneralizedTime_tags) |
| 179 |
/ sizeof(asn_DEF_GeneralizedTime_tags[0]), |
| 180 |
0, /* No PER visible constraints */ |
| 181 |
0, 0, /* No members */ |
| 182 |
0 /* No specifics */ |
| 183 |
}; |
| 184 |
|
| 185 |
#endif /* __ASN_INTERNAL_TEST_MODE__ */ |
| 186 |
|
| 187 |
/* |
| 188 |
* Check that the time looks like the time. |
| 189 |
*/ |
| 190 |
int |
| 191 |
GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
| 192 |
asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
| 193 |
const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
| 194 |
time_t tloc; |
| 195 |
|
| 196 |
errno = EPERM; /* Just an unlikely error code */ |
| 197 |
tloc = asn_GT2time(st, 0, 0); |
| 198 |
if(tloc == -1 && errno != EPERM) { |
| 199 |
_ASN_CTFAIL(app_key, td, |
| 200 |
"%s: Invalid time format: %s (%s:%d)", |
| 201 |
td->name, strerror(errno), __FILE__, __LINE__); |
| 202 |
return -1; |
| 203 |
} |
| 204 |
|
| 205 |
return 0; |
| 206 |
} |
| 207 |
|
| 208 |
asn_enc_rval_t |
| 209 |
GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
| 210 |
int tag_mode, ber_tlv_tag_t tag, |
| 211 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 212 |
GeneralizedTime_t *st = (GeneralizedTime_t *)sptr; |
| 213 |
asn_enc_rval_t erval; |
| 214 |
int fv, fd; /* seconds fraction value and number of digits */ |
| 215 |
struct tm tm; |
| 216 |
time_t tloc; |
| 217 |
|
| 218 |
/* |
| 219 |
* Encode as a canonical DER. |
| 220 |
*/ |
| 221 |
errno = EPERM; |
| 222 |
tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); /* Recognize time */ |
| 223 |
if(tloc == -1 && errno != EPERM) |
| 224 |
/* Failed to recognize time. Fail completely. */ |
| 225 |
_ASN_ENCODE_FAILED; |
| 226 |
|
| 227 |
st = asn_time2GT_frac(0, &tm, fv, fd, 1); /* Save time canonically */ |
| 228 |
if(!st) _ASN_ENCODE_FAILED; /* Memory allocation failure. */ |
| 229 |
|
| 230 |
erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key); |
| 231 |
|
| 232 |
FREEMEM(st->buf); |
| 233 |
FREEMEM(st); |
| 234 |
|
| 235 |
return erval; |
| 236 |
} |
| 237 |
|
| 238 |
#ifndef __ASN_INTERNAL_TEST_MODE__ |
| 239 |
|
| 240 |
asn_enc_rval_t |
| 241 |
GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
| 242 |
int ilevel, enum xer_encoder_flags_e flags, |
| 243 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 244 |
|
| 245 |
if(flags & XER_F_CANONICAL) { |
| 246 |
GeneralizedTime_t *gt; |
| 247 |
asn_enc_rval_t rv; |
| 248 |
int fv, fd; /* fractional parts */ |
| 249 |
struct tm tm; |
| 250 |
|
| 251 |
errno = EPERM; |
| 252 |
if(asn_GT2time_frac((GeneralizedTime_t *)sptr, |
| 253 |
&fv, &fd, &tm, 1) == -1 |
| 254 |
&& errno != EPERM) |
| 255 |
_ASN_ENCODE_FAILED; |
| 256 |
|
| 257 |
gt = asn_time2GT_frac(0, &tm, fv, fd, 1); |
| 258 |
if(!gt) _ASN_ENCODE_FAILED; |
| 259 |
|
| 260 |
rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 261 |
cb, app_key); |
| 262 |
ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt); |
| 263 |
return rv; |
| 264 |
} else { |
| 265 |
return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 266 |
cb, app_key); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
#endif /* __ASN_INTERNAL_TEST_MODE__ */ |
| 271 |
|
| 272 |
int |
| 273 |
GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 274 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 275 |
const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
| 276 |
|
| 277 |
(void)td; /* Unused argument */ |
| 278 |
(void)ilevel; /* Unused argument */ |
| 279 |
|
| 280 |
if(st && st->buf) { |
| 281 |
char buf[32]; |
| 282 |
struct tm tm; |
| 283 |
int ret; |
| 284 |
|
| 285 |
errno = EPERM; |
| 286 |
if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM) |
| 287 |
return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0; |
| 288 |
|
| 289 |
ret = snprintf(buf, sizeof(buf), |
| 290 |
"%04d-%02d-%02d %02d:%02d:%02d (GMT)", |
| 291 |
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 292 |
tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 293 |
assert(ret > 0 && ret < (int)sizeof(buf)); |
| 294 |
return (cb(buf, ret, app_key) < 0) ? -1 : 0; |
| 295 |
} else { |
| 296 |
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
time_t |
| 301 |
asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) { |
| 302 |
return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt); |
| 303 |
} |
| 304 |
|
| 305 |
time_t |
| 306 |
asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) { |
| 307 |
time_t tloc; |
| 308 |
int fv, fd = 0; |
| 309 |
|
| 310 |
if(frac_value) |
| 311 |
tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt); |
| 312 |
else |
| 313 |
return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt); |
| 314 |
if(fd == 0 || frac_digits <= 0) { |
| 315 |
*frac_value = 0; |
| 316 |
} else { |
| 317 |
while(fd > frac_digits) |
| 318 |
fv /= 10, fd--; |
| 319 |
while(fd < frac_digits) { |
| 320 |
int new_fv = fv * 10; |
| 321 |
if(new_fv / 10 != fv) { |
| 322 |
/* Too long precision request */ |
| 323 |
fv = 0; |
| 324 |
break; |
| 325 |
} |
| 326 |
fv = new_fv, fd++; |
| 327 |
} |
| 328 |
|
| 329 |
*frac_value = fv; |
| 330 |
} |
| 331 |
|
| 332 |
return tloc; |
| 333 |
} |
| 334 |
|
| 335 |
time_t |
| 336 |
asn_GT2time_frac(const GeneralizedTime_t *st, int *frac_value, int *frac_digits, struct tm *ret_tm, int as_gmt) { |
| 337 |
struct tm tm_s; |
| 338 |
uint8_t *buf; |
| 339 |
uint8_t *end; |
| 340 |
int gmtoff_h = 0; |
| 341 |
int gmtoff_m = 0; |
| 342 |
int gmtoff = 0; /* h + m */ |
| 343 |
int offset_specified = 0; |
| 344 |
int fvalue = 0; |
| 345 |
int fdigits = 0; |
| 346 |
time_t tloc; |
| 347 |
|
| 348 |
if(!st || !st->buf) { |
| 349 |
errno = EINVAL; |
| 350 |
return -1; |
| 351 |
} else { |
| 352 |
buf = st->buf; |
| 353 |
end = buf + st->size; |
| 354 |
} |
| 355 |
|
| 356 |
if(st->size < 10) { |
| 357 |
errno = EINVAL; |
| 358 |
return -1; |
| 359 |
} |
| 360 |
|
| 361 |
/* |
| 362 |
* Decode first 10 bytes: "AAAAMMJJhh" |
| 363 |
*/ |
| 364 |
memset(&tm_s, 0, sizeof(tm_s)); |
| 365 |
#undef B2F |
| 366 |
#undef B2T |
| 367 |
#define B2F(var) do { \ |
| 368 |
unsigned ch = *buf; \ |
| 369 |
if(ch < 0x30 || ch > 0x39) { \ |
| 370 |
errno = EINVAL; \ |
| 371 |
return -1; \ |
| 372 |
} else { \ |
| 373 |
var = var * 10 + (ch - 0x30); \ |
| 374 |
buf++; \ |
| 375 |
} \ |
| 376 |
} while(0) |
| 377 |
#define B2T(var) B2F(tm_s.var) |
| 378 |
|
| 379 |
B2T(tm_year); /* 1: A */ |
| 380 |
B2T(tm_year); /* 2: A */ |
| 381 |
B2T(tm_year); /* 3: A */ |
| 382 |
B2T(tm_year); /* 4: A */ |
| 383 |
B2T(tm_mon); /* 5: M */ |
| 384 |
B2T(tm_mon); /* 6: M */ |
| 385 |
B2T(tm_mday); /* 7: J */ |
| 386 |
B2T(tm_mday); /* 8: J */ |
| 387 |
B2T(tm_hour); /* 9: h */ |
| 388 |
B2T(tm_hour); /* 0: h */ |
| 389 |
|
| 390 |
if(buf == end) goto local_finish; |
| 391 |
|
| 392 |
/* |
| 393 |
* Parse [mm[ss[(.|,)ffff]]] |
| 394 |
* ^^ |
| 395 |
*/ |
| 396 |
switch(*buf) { |
| 397 |
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 398 |
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 399 |
tm_s.tm_min = (*buf++) - 0x30; |
| 400 |
if(buf == end) { errno = EINVAL; return -1; } |
| 401 |
B2T(tm_min); |
| 402 |
break; |
| 403 |
case 0x2B: case 0x2D: /* +, - */ |
| 404 |
goto offset; |
| 405 |
case 0x5A: /* Z */ |
| 406 |
goto utc_finish; |
| 407 |
default: |
| 408 |
errno = EINVAL; |
| 409 |
return -1; |
| 410 |
} |
| 411 |
|
| 412 |
if(buf == end) goto local_finish; |
| 413 |
|
| 414 |
/* |
| 415 |
* Parse [mm[ss[(.|,)ffff]]] |
| 416 |
* ^^ |
| 417 |
*/ |
| 418 |
switch(*buf) { |
| 419 |
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 420 |
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 421 |
tm_s.tm_sec = (*buf++) - 0x30; |
| 422 |
if(buf == end) { errno = EINVAL; return -1; } |
| 423 |
B2T(tm_sec); |
| 424 |
break; |
| 425 |
case 0x2B: case 0x2D: /* +, - */ |
| 426 |
goto offset; |
| 427 |
case 0x5A: /* Z */ |
| 428 |
goto utc_finish; |
| 429 |
default: |
| 430 |
errno = EINVAL; |
| 431 |
return -1; |
| 432 |
} |
| 433 |
|
| 434 |
if(buf == end) goto local_finish; |
| 435 |
|
| 436 |
/* |
| 437 |
* Parse [mm[ss[(.|,)ffff]]] |
| 438 |
* ^ ^ |
| 439 |
*/ |
| 440 |
switch(*buf) { |
| 441 |
case 0x2C: case 0x2E: /* (.|,) */ |
| 442 |
/* |
| 443 |
* Process fractions of seconds. |
| 444 |
*/ |
| 445 |
for(buf++; buf < end; buf++) { |
| 446 |
int v = *buf; |
| 447 |
int new_fvalue; |
| 448 |
switch(v) { |
| 449 |
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 450 |
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 451 |
new_fvalue = fvalue * 10 + (v - 0x30); |
| 452 |
if(new_fvalue / 10 != fvalue) { |
| 453 |
/* Not enough precision, ignore */ |
| 454 |
} else { |
| 455 |
fvalue = new_fvalue; |
| 456 |
fdigits++; |
| 457 |
} |
| 458 |
continue; |
| 459 |
default: |
| 460 |
break; |
| 461 |
} |
| 462 |
break; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
if(buf == end) goto local_finish; |
| 467 |
|
| 468 |
switch(*buf) { |
| 469 |
case 0x2B: case 0x2D: /* +, - */ |
| 470 |
goto offset; |
| 471 |
case 0x5A: /* Z */ |
| 472 |
goto utc_finish; |
| 473 |
default: |
| 474 |
errno = EINVAL; |
| 475 |
return -1; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
offset: |
| 480 |
|
| 481 |
if(end - buf < 3) { |
| 482 |
errno = EINVAL; |
| 483 |
return -1; |
| 484 |
} |
| 485 |
buf++; |
| 486 |
B2F(gmtoff_h); |
| 487 |
B2F(gmtoff_h); |
| 488 |
if(buf[-3] == 0x2D) /* Negative */ |
| 489 |
gmtoff = -1; |
| 490 |
else |
| 491 |
gmtoff = 1; |
| 492 |
|
| 493 |
if((end - buf) == 2) { |
| 494 |
B2F(gmtoff_m); |
| 495 |
B2F(gmtoff_m); |
| 496 |
} else if(end != buf) { |
| 497 |
errno = EINVAL; |
| 498 |
return -1; |
| 499 |
} |
| 500 |
|
| 501 |
gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m); |
| 502 |
|
| 503 |
/* Fall through */ |
| 504 |
utc_finish: |
| 505 |
|
| 506 |
offset_specified = 1; |
| 507 |
|
| 508 |
/* Fall through */ |
| 509 |
local_finish: |
| 510 |
|
| 511 |
/* |
| 512 |
* Validation. |
| 513 |
*/ |
| 514 |
if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1) |
| 515 |
|| (tm_s.tm_mday > 31 || tm_s.tm_mday < 1) |
| 516 |
|| (tm_s.tm_hour > 23) |
| 517 |
|| (tm_s.tm_sec > 60) |
| 518 |
) { |
| 519 |
errno = EINVAL; |
| 520 |
return -1; |
| 521 |
} |
| 522 |
|
| 523 |
/* Canonicalize */ |
| 524 |
tm_s.tm_mon -= 1; /* 0 - 11 */ |
| 525 |
tm_s.tm_year -= 1900; |
| 526 |
tm_s.tm_isdst = -1; |
| 527 |
|
| 528 |
tm_s.tm_sec -= gmtoff; |
| 529 |
|
| 530 |
/*** AT THIS POINT tm_s is either GMT or local (unknown) ****/ |
| 531 |
|
| 532 |
if(offset_specified) { |
| 533 |
tloc = timegm(&tm_s); |
| 534 |
} else { |
| 535 |
/* |
| 536 |
* Without an offset (or "Z"), |
| 537 |
* we can only guess that it is a local zone. |
| 538 |
* Interpret it in this fashion. |
| 539 |
*/ |
| 540 |
tloc = mktime(&tm_s); |
| 541 |
} |
| 542 |
if(tloc == -1) { |
| 543 |
errno = EINVAL; |
| 544 |
return -1; |
| 545 |
} |
| 546 |
|
| 547 |
if(ret_tm) { |
| 548 |
if(as_gmt) { |
| 549 |
if(offset_specified) { |
| 550 |
*ret_tm = tm_s; |
| 551 |
} else { |
| 552 |
if(gmtime_r(&tloc, ret_tm) == 0) { |
| 553 |
errno = EINVAL; |
| 554 |
return -1; |
| 555 |
} |
| 556 |
} |
| 557 |
} else { |
| 558 |
if(localtime_r(&tloc, ret_tm) == 0) { |
| 559 |
errno = EINVAL; |
| 560 |
return -1; |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
/* Fractions of seconds */ |
| 566 |
if(frac_value) *frac_value = fvalue; |
| 567 |
if(frac_digits) *frac_digits = fdigits; |
| 568 |
|
| 569 |
return tloc; |
| 570 |
} |
| 571 |
|
| 572 |
GeneralizedTime_t * |
| 573 |
asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) { |
| 574 |
return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt); |
| 575 |
} |
| 576 |
|
| 577 |
GeneralizedTime_t * |
| 578 |
asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, int frac_value, int frac_digits, int force_gmt) { |
| 579 |
struct tm tm_s; |
| 580 |
long gmtoff; |
| 581 |
const unsigned int buf_size = |
| 582 |
4 + 2 + 2 /* yyyymmdd */ |
| 583 |
+ 2 + 2 + 2 /* hhmmss */ |
| 584 |
+ 1 + 6 /* .ffffff */ |
| 585 |
+ 1 + 4 /* +hhmm */ |
| 586 |
+ 1 /* '\0' */ |
| 587 |
; |
| 588 |
char *buf; |
| 589 |
char *p; |
| 590 |
int size; |
| 591 |
|
| 592 |
/* Check arguments */ |
| 593 |
if(!tm) { |
| 594 |
errno = EINVAL; |
| 595 |
return 0; |
| 596 |
} |
| 597 |
|
| 598 |
/* Pre-allocate a buffer of sufficient yet small length */ |
| 599 |
buf = (char *)MALLOC(buf_size); |
| 600 |
if(!buf) return 0; |
| 601 |
|
| 602 |
gmtoff = GMTOFF(*tm); |
| 603 |
|
| 604 |
if(force_gmt && gmtoff) { |
| 605 |
tm_s = *tm; |
| 606 |
tm_s.tm_sec -= gmtoff; |
| 607 |
timegm(&tm_s); /* Fix the time */ |
| 608 |
tm = &tm_s; |
| 609 |
#ifdef HAVE_TM_GMTOFF |
| 610 |
assert(!GMTOFF(tm_s)); /* Will fix itself */ |
| 611 |
#else /* !HAVE_TM_GMTOFF */ |
| 612 |
gmtoff = 0; |
| 613 |
#endif |
| 614 |
} |
| 615 |
|
| 616 |
size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d", |
| 617 |
tm->tm_year + 1900, |
| 618 |
tm->tm_mon + 1, |
| 619 |
tm->tm_mday, |
| 620 |
tm->tm_hour, |
| 621 |
tm->tm_min, |
| 622 |
tm->tm_sec |
| 623 |
); |
| 624 |
if(size != 14) { |
| 625 |
/* Could be assert(size == 14); */ |
| 626 |
FREEMEM(buf); |
| 627 |
errno = EINVAL; |
| 628 |
return 0; |
| 629 |
} |
| 630 |
|
| 631 |
p = buf + size; |
| 632 |
|
| 633 |
/* |
| 634 |
* Deal with fractions. |
| 635 |
*/ |
| 636 |
if(frac_value > 0 && frac_digits > 0) { |
| 637 |
char *end = p + 1 + 6; /* '.' + maximum 6 digits */ |
| 638 |
char *z = p; |
| 639 |
long fbase; |
| 640 |
*z++ = '.'; |
| 641 |
|
| 642 |
/* Place bounds on precision */ |
| 643 |
while(frac_digits-- > 6) |
| 644 |
frac_value /= 10; |
| 645 |
|
| 646 |
/* emulate fbase = pow(10, frac_digits) */ |
| 647 |
for(fbase = 1; frac_digits--;) |
| 648 |
fbase *= 10; |
| 649 |
|
| 650 |
do { |
| 651 |
int digit = frac_value / fbase; |
| 652 |
if(digit > 9) { z = 0; break; } |
| 653 |
*z++ = digit + 0x30; |
| 654 |
frac_value %= fbase; |
| 655 |
fbase /= 10; |
| 656 |
} while(fbase > 0 && frac_value > 0 && z < end); |
| 657 |
if(z) { |
| 658 |
for(--z; *z == 0x30; --z); /* Strip zeroes */ |
| 659 |
p = z + (*z != '.'); |
| 660 |
size = p - buf; |
| 661 |
} |
| 662 |
} |
| 663 |
|
| 664 |
if(force_gmt) { |
| 665 |
*p++ = 0x5a; /* "Z" */ |
| 666 |
*p++ = 0; |
| 667 |
size++; |
| 668 |
} else { |
| 669 |
int ret; |
| 670 |
gmtoff %= 86400; |
| 671 |
ret = snprintf(p, buf_size - size, "%+03ld%02ld", |
| 672 |
gmtoff / 3600, labs(gmtoff % 3600)); |
| 673 |
if(ret != 5) { |
| 674 |
FREEMEM(buf); |
| 675 |
errno = EINVAL; |
| 676 |
return 0; |
| 677 |
} |
| 678 |
size += ret; |
| 679 |
} |
| 680 |
|
| 681 |
if(opt_gt) { |
| 682 |
if(opt_gt->buf) |
| 683 |
FREEMEM(opt_gt->buf); |
| 684 |
} else { |
| 685 |
opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt); |
| 686 |
if(!opt_gt) { FREEMEM(buf); return 0; } |
| 687 |
} |
| 688 |
|
| 689 |
opt_gt->buf = (unsigned char *)buf; |
| 690 |
opt_gt->size = size; |
| 691 |
|
| 692 |
return opt_gt; |
| 693 |
} |
| 694 |
|
| 695 |
|