| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#define _POSIX_PTHREAD_SEMANTICS |
| 6 |
#define _REENTRANT |
| 7 |
#define _BSD_SOURCE |
| 8 |
#include <asn_internal.h> |
| 9 |
#include <GeneralizedTime.h> |
| 10 |
|
| 11 |
#ifdef __CYGWIN__ |
| 12 |
#include "/usr/include/time.h" |
| 13 |
#else |
| 14 |
#include <time.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#include <stdio.h> |
| 18 |
#include <errno.h> |
| 19 |
|
| 20 |
#if defined(_WIN32) |
| 21 |
#pragma message( "PLEASE STOP AND READ!") |
| 22 |
#pragma message( " localtime_r is implemented via localtime(), which may be not thread-safe.") |
| 23 |
#pragma message( " gmtime_r is implemented via gmtime(), which may be not thread-safe.") |
| 24 |
#pragma message( " ") |
| 25 |
#pragma message( " You must fix the code by inserting appropriate locking") |
| 26 |
#pragma message( " if you want to use asn_GT2time() or asn_UT2time().") |
| 27 |
#pragma message( "PLEASE STOP AND READ!") |
| 28 |
|
| 29 |
static struct tm *localtime_r(const time_t *tloc, struct tm *result) { |
| 30 |
struct tm *tm; |
| 31 |
if((tm = localtime(tloc))) |
| 32 |
return memcpy(result, tm, sizeof(struct tm)); |
| 33 |
return 0; |
| 34 |
} |
| 35 |
|
| 36 |
static struct tm *gmtime_r(const time_t *tloc, struct tm *result) { |
| 37 |
struct tm *tm; |
| 38 |
if((tm = gmtime(tloc))) |
| 39 |
return memcpy(result, tm, sizeof(struct tm)); |
| 40 |
return 0; |
| 41 |
} |
| 42 |
|
| 43 |
#define tzset() _tzset() |
| 44 |
#define putenv(c) _putenv(c) |
| 45 |
#define _EMULATE_TIMEGM |
| 46 |
|
| 47 |
#endif |
| 48 |
|
| 49 |
#if defined(sun) || defined(_sun_) || defined(__solaris__) |
| 50 |
#define _EMULATE_TIMEGM |
| 51 |
#endif |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
#if defined(__FreeBSD__) \ |
| 58 |
|| (defined(__GNUC__) && defined(__APPLE_CC__)) \ |
| 59 |
|| (defined __GLIBC__ && __GLIBC__ >= 2) |
| 60 |
#undef HAVE_TM_GMTOFF |
| 61 |
#define HAVE_TM_GMTOFF |
| 62 |
#endif |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
#ifdef HAVE_TM_GMTOFF |
| 68 |
#define GMTOFF(tm) ((tm).tm_gmtoff) |
| 69 |
#else |
| 70 |
#define GMTOFF(tm) (-timezone) |
| 71 |
#endif |
| 72 |
|
| 73 |
#if defined(_WIN32) |
| 74 |
#pragma message( "PLEASE STOP AND READ!") |
| 75 |
#pragma message( " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe.") |
| 76 |
#pragma message( " ") |
| 77 |
#pragma message( " You must fix the code by inserting appropriate locking") |
| 78 |
#pragma message( " if you want to use asn_GT2time() or asn_UT2time().") |
| 79 |
#pragma message( "PLEASE STOP AND READ!") |
| 80 |
#else |
| 81 |
#if (defined(_EMULATE_TIMEGM) || !defined(HAVE_TM_GMTOFF)) |
| 82 |
#warning "PLEASE STOP AND READ!" |
| 83 |
#warning " timegm() is implemented via getenv(\"TZ\")/setenv(\"TZ\"), which may be not thread-safe." |
| 84 |
#warning " " |
| 85 |
#warning " You must fix the code by inserting appropriate locking" |
| 86 |
#warning " if you want to use asn_GT2time() or asn_UT2time()." |
| 87 |
#warning "PLEASE STOP AND READ!" |
| 88 |
#endif |
| 89 |
#endif |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
#ifdef __CYGWIN__ |
| 95 |
#undef GMTOFF |
| 96 |
static long GMTOFF(struct tm a){ |
| 97 |
struct tm *lt; |
| 98 |
time_t local_time, gmt_time; |
| 99 |
long zone; |
| 100 |
|
| 101 |
tzset(); |
| 102 |
gmt_time = time (NULL); |
| 103 |
|
| 104 |
lt = gmtime(&gmt_time); |
| 105 |
|
| 106 |
local_time = mktime(lt); |
| 107 |
return (gmt_time - local_time); |
| 108 |
} |
| 109 |
#define _EMULATE_TIMEGM |
| 110 |
|
| 111 |
#endif |
| 112 |
|
| 113 |
#define ATZVARS do { \ |
| 114 |
char tzoldbuf[64]; \ |
| 115 |
char *tzold |
| 116 |
#define ATZSAVETZ do { \ |
| 117 |
tzold = getenv("TZ"); \ |
| 118 |
if(tzold) { \ |
| 119 |
size_t tzlen = strlen(tzold); \ |
| 120 |
if(tzlen < sizeof(tzoldbuf)) { \ |
| 121 |
tzold = memcpy(tzoldbuf, tzold, tzlen + 1); \ |
| 122 |
} else { \ |
| 123 |
char *dupptr = tzold; \ |
| 124 |
tzold = MALLOC(tzlen + 1); \ |
| 125 |
if(tzold) memcpy(tzold, dupptr, tzlen + 1); \ |
| 126 |
} \ |
| 127 |
setenv("TZ", "UTC", 1); \ |
| 128 |
} \ |
| 129 |
tzset(); \ |
| 130 |
} while(0) |
| 131 |
#define ATZOLDTZ do { \ |
| 132 |
if (tzold) { \ |
| 133 |
setenv("TZ", tzold, 1); \ |
| 134 |
*tzoldbuf = 0; \ |
| 135 |
if(tzold != tzoldbuf) \ |
| 136 |
FREEMEM(tzold); \ |
| 137 |
} else { \ |
| 138 |
unsetenv("TZ"); \ |
| 139 |
} \ |
| 140 |
tzset(); \ |
| 141 |
} while(0); } while(0); |
| 142 |
|
| 143 |
#ifndef HAVE_TIMEGM |
| 144 |
#ifdef _EMULATE_TIMEGM |
| 145 |
static time_t timegm(struct tm *tm) { |
| 146 |
time_t tloc; |
| 147 |
ATZVARS; |
| 148 |
ATZSAVETZ; |
| 149 |
tloc = mktime(tm); |
| 150 |
ATZOLDTZ; |
| 151 |
return tloc; |
| 152 |
} |
| 153 |
#endif |
| 154 |
#endif |
| 155 |
|
| 156 |
|
| 157 |
#ifndef ASN___INTERNAL_TEST_MODE |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static const ber_tlv_tag_t asn_DEF_GeneralizedTime_tags[] = { |
| 163 |
(ASN_TAG_CLASS_UNIVERSAL | (24 << 2)), |
| 164 |
(ASN_TAG_CLASS_UNIVERSAL | (26 << 2)), |
| 165 |
(ASN_TAG_CLASS_UNIVERSAL | (4 << 2)) |
| 166 |
}; |
| 167 |
static asn_per_constraints_t asn_DEF_GeneralizedTime_constraints = { |
| 168 |
{ APC_CONSTRAINED, 7, 7, 0x20, 0x7e }, |
| 169 |
{ APC_SEMI_CONSTRAINED, -1, -1, 0, 0 }, |
| 170 |
0, 0 |
| 171 |
}; |
| 172 |
asn_TYPE_descriptor_t asn_DEF_GeneralizedTime = { |
| 173 |
"GeneralizedTime", |
| 174 |
"GeneralizedTime", |
| 175 |
OCTET_STRING_free, |
| 176 |
GeneralizedTime_print, |
| 177 |
GeneralizedTime_constraint, |
| 178 |
OCTET_STRING_decode_ber, |
| 179 |
GeneralizedTime_encode_der, |
| 180 |
OCTET_STRING_decode_xer_utf8, |
| 181 |
GeneralizedTime_encode_xer, |
| 182 |
OCTET_STRING_decode_uper, |
| 183 |
OCTET_STRING_encode_uper, |
| 184 |
0, |
| 185 |
asn_DEF_GeneralizedTime_tags, |
| 186 |
sizeof(asn_DEF_GeneralizedTime_tags) |
| 187 |
/ sizeof(asn_DEF_GeneralizedTime_tags[0]) - 2, |
| 188 |
asn_DEF_GeneralizedTime_tags, |
| 189 |
sizeof(asn_DEF_GeneralizedTime_tags) |
| 190 |
/ sizeof(asn_DEF_GeneralizedTime_tags[0]), |
| 191 |
&asn_DEF_GeneralizedTime_constraints, |
| 192 |
0, 0, |
| 193 |
0 |
| 194 |
}; |
| 195 |
|
| 196 |
#endif |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
int |
| 202 |
GeneralizedTime_constraint(asn_TYPE_descriptor_t *td, const void *sptr, |
| 203 |
asn_app_constraint_failed_f *ctfailcb, void *app_key) { |
| 204 |
const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
| 205 |
time_t tloc; |
| 206 |
|
| 207 |
errno = EPERM; |
| 208 |
tloc = asn_GT2time(st, 0, 0); |
| 209 |
if(tloc == -1 && errno != EPERM) { |
| 210 |
ASN__CTFAIL(app_key, td, sptr, |
| 211 |
"%s: Invalid time format: %s (%s:%d)", |
| 212 |
td->name, strerror(errno), __FILE__, __LINE__); |
| 213 |
return -1; |
| 214 |
} |
| 215 |
|
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
asn_enc_rval_t |
| 220 |
GeneralizedTime_encode_der(asn_TYPE_descriptor_t *td, void *sptr, |
| 221 |
int tag_mode, ber_tlv_tag_t tag, |
| 222 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 223 |
GeneralizedTime_t *st = (GeneralizedTime_t *)sptr; |
| 224 |
asn_enc_rval_t erval; |
| 225 |
int fv, fd; |
| 226 |
struct tm tm; |
| 227 |
time_t tloc; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
errno = EPERM; |
| 233 |
tloc = asn_GT2time_frac(st, &fv, &fd, &tm, 1); |
| 234 |
if(tloc == -1 && errno != EPERM) |
| 235 |
|
| 236 |
ASN__ENCODE_FAILED; |
| 237 |
|
| 238 |
st = asn_time2GT_frac(0, &tm, fv, fd, 1); |
| 239 |
if(!st) ASN__ENCODE_FAILED; |
| 240 |
|
| 241 |
erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key); |
| 242 |
|
| 243 |
FREEMEM(st->buf); |
| 244 |
FREEMEM(st); |
| 245 |
|
| 246 |
return erval; |
| 247 |
} |
| 248 |
|
| 249 |
#ifndef ASN___INTERNAL_TEST_MODE |
| 250 |
|
| 251 |
asn_enc_rval_t |
| 252 |
GeneralizedTime_encode_xer(asn_TYPE_descriptor_t *td, void *sptr, |
| 253 |
int ilevel, enum xer_encoder_flags_e flags, |
| 254 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 255 |
|
| 256 |
if(flags & XER_F_CANONICAL) { |
| 257 |
GeneralizedTime_t *gt; |
| 258 |
asn_enc_rval_t rv; |
| 259 |
int fv, fd; |
| 260 |
struct tm tm; |
| 261 |
|
| 262 |
errno = EPERM; |
| 263 |
if(asn_GT2time_frac((GeneralizedTime_t *)sptr, |
| 264 |
&fv, &fd, &tm, 1) == -1 |
| 265 |
&& errno != EPERM) |
| 266 |
ASN__ENCODE_FAILED; |
| 267 |
|
| 268 |
gt = asn_time2GT_frac(0, &tm, fv, fd, 1); |
| 269 |
if(!gt) ASN__ENCODE_FAILED; |
| 270 |
|
| 271 |
rv = OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 272 |
cb, app_key); |
| 273 |
ASN_STRUCT_FREE(asn_DEF_GeneralizedTime, gt); |
| 274 |
return rv; |
| 275 |
} else { |
| 276 |
return OCTET_STRING_encode_xer_utf8(td, sptr, ilevel, flags, |
| 277 |
cb, app_key); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
#endif |
| 282 |
|
| 283 |
int |
| 284 |
GeneralizedTime_print(asn_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 285 |
asn_app_consume_bytes_f *cb, void *app_key) { |
| 286 |
const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
| 287 |
|
| 288 |
(void)td; |
| 289 |
(void)ilevel; |
| 290 |
|
| 291 |
if(st && st->buf) { |
| 292 |
char buf[32]; |
| 293 |
struct tm tm; |
| 294 |
int ret; |
| 295 |
|
| 296 |
errno = EPERM; |
| 297 |
if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM) |
| 298 |
return (cb("<bad-value>", 11, app_key) < 0) ? -1 : 0; |
| 299 |
|
| 300 |
ret = snprintf(buf, sizeof(buf), |
| 301 |
"%04d-%02d-%02d %02d:%02d:%02d (GMT)", |
| 302 |
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 303 |
tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 304 |
assert(ret > 0 && ret < (int)sizeof(buf)); |
| 305 |
return (cb(buf, ret, app_key) < 0) ? -1 : 0; |
| 306 |
} else { |
| 307 |
return (cb("<absent>", 8, app_key) < 0) ? -1 : 0; |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
time_t |
| 312 |
asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) { |
| 313 |
return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt); |
| 314 |
} |
| 315 |
|
| 316 |
time_t |
| 317 |
asn_GT2time_prec(const GeneralizedTime_t *st, int *frac_value, int frac_digits, struct tm *ret_tm, int as_gmt) { |
| 318 |
time_t tloc; |
| 319 |
int fv, fd = 0; |
| 320 |
|
| 321 |
if(frac_value) |
| 322 |
tloc = asn_GT2time_frac(st, &fv, &fd, ret_tm, as_gmt); |
| 323 |
else |
| 324 |
return asn_GT2time_frac(st, 0, 0, ret_tm, as_gmt); |
| 325 |
if(fd == 0 || frac_digits <= 0) { |
| 326 |
*frac_value = 0; |
| 327 |
} else { |
| 328 |
while(fd > frac_digits) |
| 329 |
fv /= 10, fd--; |
| 330 |
while(fd < frac_digits) { |
| 331 |
if(fv < INT_MAX / 10) { |
| 332 |
fv *= 10; |
| 333 |
fd++; |
| 334 |
} else { |
| 335 |
|
| 336 |
fv = 0; |
| 337 |
break; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
*frac_value = fv; |
| 342 |
} |
| 343 |
|
| 344 |
return tloc; |
| 345 |
} |
| 346 |
|
| 347 |
time_t |
| 348 |
asn_GT2time_frac(const GeneralizedTime_t *st, int *frac_value, int *frac_digits, struct tm *ret_tm, int as_gmt) { |
| 349 |
struct tm tm_s; |
| 350 |
uint8_t *buf; |
| 351 |
uint8_t *end; |
| 352 |
int gmtoff_h = 0; |
| 353 |
int gmtoff_m = 0; |
| 354 |
int gmtoff = 0; |
| 355 |
int offset_specified = 0; |
| 356 |
int fvalue = 0; |
| 357 |
int fdigits = 0; |
| 358 |
time_t tloc; |
| 359 |
|
| 360 |
if(!st || !st->buf) { |
| 361 |
errno = EINVAL; |
| 362 |
return -1; |
| 363 |
} else { |
| 364 |
buf = st->buf; |
| 365 |
end = buf + st->size; |
| 366 |
} |
| 367 |
|
| 368 |
if(st->size < 10) { |
| 369 |
errno = EINVAL; |
| 370 |
return -1; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
memset(&tm_s, 0, sizeof(tm_s)); |
| 377 |
#undef B2F |
| 378 |
#undef B2T |
| 379 |
#define B2F(var) do { \ |
| 380 |
unsigned ch = *buf; \ |
| 381 |
if(ch < 0x30 || ch > 0x39) { \ |
| 382 |
errno = EINVAL; \ |
| 383 |
return -1; \ |
| 384 |
} else { \ |
| 385 |
var = var * 10 + (ch - 0x30); \ |
| 386 |
buf++; \ |
| 387 |
} \ |
| 388 |
} while(0) |
| 389 |
#define B2T(var) B2F(tm_s.var) |
| 390 |
|
| 391 |
B2T(tm_year); |
| 392 |
B2T(tm_year); |
| 393 |
B2T(tm_year); |
| 394 |
B2T(tm_year); |
| 395 |
B2T(tm_mon); |
| 396 |
B2T(tm_mon); |
| 397 |
B2T(tm_mday); |
| 398 |
B2T(tm_mday); |
| 399 |
B2T(tm_hour); |
| 400 |
B2T(tm_hour); |
| 401 |
|
| 402 |
if(buf == end) goto local_finish; |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
switch(*buf) { |
| 409 |
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 410 |
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 411 |
tm_s.tm_min = (*buf++) - 0x30; |
| 412 |
if(buf == end) { errno = EINVAL; return -1; } |
| 413 |
B2T(tm_min); |
| 414 |
break; |
| 415 |
case 0x2B: case 0x2D: |
| 416 |
goto offset; |
| 417 |
case 0x5A: |
| 418 |
goto utc_finish; |
| 419 |
default: |
| 420 |
errno = EINVAL; |
| 421 |
return -1; |
| 422 |
} |
| 423 |
|
| 424 |
if(buf == end) goto local_finish; |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
switch(*buf) { |
| 431 |
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 432 |
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 433 |
tm_s.tm_sec = (*buf++) - 0x30; |
| 434 |
if(buf == end) { errno = EINVAL; return -1; } |
| 435 |
B2T(tm_sec); |
| 436 |
break; |
| 437 |
case 0x2B: case 0x2D: |
| 438 |
goto offset; |
| 439 |
case 0x5A: |
| 440 |
goto utc_finish; |
| 441 |
default: |
| 442 |
errno = EINVAL; |
| 443 |
return -1; |
| 444 |
} |
| 445 |
|
| 446 |
if(buf == end) goto local_finish; |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
switch(*buf) { |
| 453 |
case 0x2C: case 0x2E: |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
for(buf++; buf < end; buf++) { |
| 458 |
int v = *buf; |
| 459 |
|
| 460 |
switch(v) { |
| 461 |
case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 462 |
case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 463 |
if(fvalue < INT_MAX/10) { |
| 464 |
fvalue = fvalue * 10 + (v - 0x30); |
| 465 |
fdigits++; |
| 466 |
} else { |
| 467 |
|
| 468 |
} |
| 469 |
continue; |
| 470 |
default: |
| 471 |
break; |
| 472 |
} |
| 473 |
break; |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
if(buf == end) goto local_finish; |
| 478 |
|
| 479 |
switch(*buf) { |
| 480 |
case 0x2B: case 0x2D: |
| 481 |
goto offset; |
| 482 |
case 0x5A: |
| 483 |
goto utc_finish; |
| 484 |
default: |
| 485 |
errno = EINVAL; |
| 486 |
return -1; |
| 487 |
} |
| 488 |
|
| 489 |
|
| 490 |
offset: |
| 491 |
|
| 492 |
if(end - buf < 3) { |
| 493 |
errno = EINVAL; |
| 494 |
return -1; |
| 495 |
} |
| 496 |
buf++; |
| 497 |
B2F(gmtoff_h); |
| 498 |
B2F(gmtoff_h); |
| 499 |
if(buf[-3] == 0x2D) |
| 500 |
gmtoff = -1; |
| 501 |
else |
| 502 |
gmtoff = 1; |
| 503 |
|
| 504 |
if((end - buf) == 2) { |
| 505 |
B2F(gmtoff_m); |
| 506 |
B2F(gmtoff_m); |
| 507 |
} else if(end != buf) { |
| 508 |
errno = EINVAL; |
| 509 |
return -1; |
| 510 |
} |
| 511 |
|
| 512 |
gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m); |
| 513 |
|
| 514 |
|
| 515 |
utc_finish: |
| 516 |
|
| 517 |
offset_specified = 1; |
| 518 |
|
| 519 |
|
| 520 |
local_finish: |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1) |
| 526 |
|| (tm_s.tm_mday > 31 || tm_s.tm_mday < 1) |
| 527 |
|| (tm_s.tm_hour > 23) |
| 528 |
|| (tm_s.tm_sec > 60) |
| 529 |
) { |
| 530 |
errno = EINVAL; |
| 531 |
return -1; |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
tm_s.tm_mon -= 1; |
| 536 |
tm_s.tm_year -= 1900; |
| 537 |
tm_s.tm_isdst = -1; |
| 538 |
|
| 539 |
tm_s.tm_sec -= gmtoff; |
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
if(offset_specified) { |
| 544 |
tloc = timegm(&tm_s); |
| 545 |
} else { |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
tloc = mktime(&tm_s); |
| 552 |
} |
| 553 |
if(tloc == -1) { |
| 554 |
errno = EINVAL; |
| 555 |
return -1; |
| 556 |
} |
| 557 |
|
| 558 |
if(ret_tm) { |
| 559 |
if(as_gmt) { |
| 560 |
if(offset_specified) { |
| 561 |
*ret_tm = tm_s; |
| 562 |
} else { |
| 563 |
if(gmtime_r(&tloc, ret_tm) == 0) { |
| 564 |
errno = EINVAL; |
| 565 |
return -1; |
| 566 |
} |
| 567 |
} |
| 568 |
} else { |
| 569 |
if(localtime_r(&tloc, ret_tm) == 0) { |
| 570 |
errno = EINVAL; |
| 571 |
return -1; |
| 572 |
} |
| 573 |
} |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
if(frac_value) *frac_value = fvalue; |
| 578 |
if(frac_digits) *frac_digits = fdigits; |
| 579 |
|
| 580 |
return tloc; |
| 581 |
} |
| 582 |
|
| 583 |
GeneralizedTime_t * |
| 584 |
asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) { |
| 585 |
return asn_time2GT_frac(opt_gt, tm, 0, 0, force_gmt); |
| 586 |
} |
| 587 |
|
| 588 |
GeneralizedTime_t * |
| 589 |
asn_time2GT_frac(GeneralizedTime_t *opt_gt, const struct tm *tm, int frac_value, int frac_digits, int force_gmt) { |
| 590 |
struct tm tm_s; |
| 591 |
long gmtoff; |
| 592 |
const unsigned int buf_size = |
| 593 |
4 + 2 + 2 |
| 594 |
+ 2 + 2 + 2 |
| 595 |
+ 1 + 6 |
| 596 |
+ 1 + 4 |
| 597 |
+ 1 |
| 598 |
; |
| 599 |
char *buf; |
| 600 |
char *p; |
| 601 |
int size; |
| 602 |
|
| 603 |
|
| 604 |
if(!tm) { |
| 605 |
errno = EINVAL; |
| 606 |
return 0; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
buf = (char *)MALLOC(buf_size); |
| 611 |
if(!buf) return 0; |
| 612 |
|
| 613 |
gmtoff = GMTOFF(*tm); |
| 614 |
|
| 615 |
if(force_gmt && gmtoff) { |
| 616 |
tm_s = *tm; |
| 617 |
tm_s.tm_sec -= gmtoff; |
| 618 |
timegm(&tm_s); |
| 619 |
tm = &tm_s; |
| 620 |
#ifdef HAVE_TM_GMTOFF |
| 621 |
assert(!GMTOFF(tm_s)); |
| 622 |
#else |
| 623 |
gmtoff = 0; |
| 624 |
#endif |
| 625 |
} |
| 626 |
|
| 627 |
size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d", |
| 628 |
tm->tm_year + 1900, |
| 629 |
tm->tm_mon + 1, |
| 630 |
tm->tm_mday, |
| 631 |
tm->tm_hour, |
| 632 |
tm->tm_min, |
| 633 |
tm->tm_sec |
| 634 |
); |
| 635 |
if(size != 14) { |
| 636 |
|
| 637 |
FREEMEM(buf); |
| 638 |
errno = EINVAL; |
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
p = buf + size; |
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
if(frac_value > 0 && frac_digits > 0) { |
| 648 |
char *end = p + 1 + 6; |
| 649 |
char *z = p; |
| 650 |
long fbase; |
| 651 |
*z++ = '.'; |
| 652 |
|
| 653 |
|
| 654 |
while(frac_digits-- > 6) |
| 655 |
frac_value /= 10; |
| 656 |
|
| 657 |
|
| 658 |
for(fbase = 1; frac_digits--;) |
| 659 |
fbase *= 10; |
| 660 |
|
| 661 |
do { |
| 662 |
int digit = frac_value / fbase; |
| 663 |
if(digit > 9) { z = 0; break; } |
| 664 |
*z++ = digit + 0x30; |
| 665 |
frac_value %= fbase; |
| 666 |
fbase /= 10; |
| 667 |
} while(fbase > 0 && frac_value > 0 && z < end); |
| 668 |
if(z) { |
| 669 |
for(--z; *z == 0x30; --z); |
| 670 |
p = z + (*z != '.'); |
| 671 |
size = p - buf; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
if(force_gmt) { |
| 676 |
*p++ = 0x5a; |
| 677 |
*p++ = 0; |
| 678 |
size++; |
| 679 |
} else { |
| 680 |
int ret; |
| 681 |
gmtoff %= 86400; |
| 682 |
ret = snprintf(p, buf_size - size, "%+03ld%02ld", |
| 683 |
gmtoff / 3600, labs(gmtoff % 3600) / 60); |
| 684 |
if(ret != 5) { |
| 685 |
FREEMEM(buf); |
| 686 |
errno = EINVAL; |
| 687 |
return 0; |
| 688 |
} |
| 689 |
size += ret; |
| 690 |
} |
| 691 |
|
| 692 |
if(opt_gt) { |
| 693 |
if(opt_gt->buf) |
| 694 |
FREEMEM(opt_gt->buf); |
| 695 |
} else { |
| 696 |
opt_gt = (GeneralizedTime_t *)CALLOC(1, sizeof *opt_gt); |
| 697 |
if(!opt_gt) { FREEMEM(buf); return 0; } |
| 698 |
} |
| 699 |
|
| 700 |
opt_gt->buf = (unsigned char *)buf; |
| 701 |
opt_gt->size = size; |
| 702 |
|
| 703 |
return opt_gt; |
| 704 |
} |
| 705 |
|
| 706 |
|