vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 1 | /*- |
| 2 | * Copyright (c) 2003, 2004 Lev Walkin <vlm@lionet.info>. All rights reserved. |
| 3 | * Redistribution and modifications are permitted subject to BSD license. |
| 4 | */ |
| 5 | #include <GeneralizedTime.h> |
| 6 | #include <time.h> |
| 7 | #include <errno.h> |
| 8 | #ifndef __NO_ASSERT_H__ |
| 9 | #include <assert.h> |
| 10 | #endif /* __NO_ASSERT_H__ */ |
| 11 | |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 12 | #ifdef WIN32 |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 13 | #warning PLEASE STOP AND READ! |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 14 | #warning localtime_r is implemented via localtime(), which is not thread-safe. |
| 15 | #warning gmtime_r is implemented via gmtime(), which is not thread-safe. |
| 16 | #warning |
| 17 | #warning You must fix the code by inserting appropriate locking |
| 18 | #warning if you want to use asn_GT2time() or asn_UT2time(). |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 19 | #warning PLEASE STOP AND READ! |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 20 | |
| 21 | static struct tm *localtime_r(time_t *tloc, struct tm *result) { |
| 22 | struct tm *tm; |
| 23 | if((tm = localtime(tloc))) |
| 24 | return memcpy(result, tm, sizeof(struct tm)); |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | static struct tm *gmtime_r(time_t *tloc, struct tm *result) { |
| 29 | struct tm *tm; |
| 30 | if((tm = gmtime(tloc))) |
| 31 | return memcpy(result, tm, sizeof(struct tm)); |
| 32 | return 0; |
| 33 | } |
| 34 | |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame^] | 35 | #define tzset() _tzset() |
| 36 | static time_t timegm(struct tm *tm) { |
| 37 | time_t tloc; |
| 38 | char *tz; |
| 39 | |
| 40 | tz = getenv("TZ"); |
| 41 | setenv("TZ", "", 1); |
| 42 | tzset(); |
| 43 | tloc = mktime(tm); |
| 44 | if (tz) |
| 45 | setenv("TZ", tz, 1); |
| 46 | else |
| 47 | unsetenv("TZ"); |
| 48 | tzset(); |
| 49 | return tloc; |
| 50 | } |
| 51 | |
| 52 | #if 0 /* Alternate version */ |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 53 | /* vlm: I am not sure about validity of this algorithm. */ |
| 54 | static time_t timegm(struct tm *tm) { |
| 55 | struct tm tmp; |
| 56 | time_t tloc = mktime(tm); |
| 57 | localtime_r(&tloc, &tmp); /* Figure out our GMT offset */ |
| 58 | tloc += tmp.tm_gmtoff; |
| 59 | tm->tm_zone = "GMT"; |
| 60 | tm->tm_gmtoff = 0; /* Simulate GMT */ |
| 61 | return tloc; |
| 62 | } |
vlm | 348a37a | 2004-08-12 03:58:25 +0000 | [diff] [blame^] | 63 | #endif |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 64 | #endif /* WIN32 */ |
vlm | 6e73a04 | 2004-08-11 07:17:22 +0000 | [diff] [blame] | 65 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 66 | #ifndef __NO_ASN_TABLE__ |
| 67 | |
| 68 | /* |
| 69 | * GeneralizedTime basic type description. |
| 70 | */ |
| 71 | static ber_tlv_tag_t asn1_DEF_GeneralizedTime_tags[] = { |
| 72 | (ASN_TAG_CLASS_UNIVERSAL | (24 << 2)) |
| 73 | }; |
| 74 | asn1_TYPE_descriptor_t asn1_DEF_GeneralizedTime = { |
| 75 | "GeneralizedTime", |
| 76 | GeneralizedTime_constraint, /* Check validity of time */ |
| 77 | OCTET_STRING_decode_ber, /* Implemented in terms of OCTET STRING */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 78 | GeneralizedTime_encode_der, /* Implemented in terms of OCTET STRING */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 79 | GeneralizedTime_print, |
| 80 | OCTET_STRING_free, |
| 81 | 0, /* Use generic outmost tag fetcher */ |
| 82 | asn1_DEF_GeneralizedTime_tags, |
| 83 | sizeof(asn1_DEF_GeneralizedTime_tags) |
| 84 | / sizeof(asn1_DEF_GeneralizedTime_tags[0]), |
| 85 | 1, /* Single UNIVERSAL tag may be implicitly overriden */ |
| 86 | -1, /* Both ways are fine */ |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 87 | 0 /* No specifics */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | #endif /* __NO_ASN_TABLE__ */ |
| 91 | |
| 92 | /* |
| 93 | * Check that the time looks like the time. |
| 94 | */ |
| 95 | int |
| 96 | GeneralizedTime_constraint(asn1_TYPE_descriptor_t *td, const void *sptr, |
| 97 | asn_app_consume_bytes_f *app_errlog, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 98 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 99 | time_t tloc; |
| 100 | |
| 101 | errno = EPERM; /* Just an unlikely error code */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 102 | tloc = asn_GT2time(st, 0, 0); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 103 | if(tloc == -1 && errno != EPERM) { |
vlm | e3f0f28 | 2004-08-11 09:44:13 +0000 | [diff] [blame] | 104 | _ASN_ERRLOG(app_errlog, app_key, |
| 105 | "%s: Invalid time format: %s", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 106 | td->name, strerror(errno)); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 113 | der_enc_rval_t |
| 114 | GeneralizedTime_encode_der(asn1_TYPE_descriptor_t *td, void *ptr, |
| 115 | int tag_mode, ber_tlv_tag_t tag, |
| 116 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 117 | GeneralizedTime_t *st = (GeneralizedTime_t *)ptr; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 118 | der_enc_rval_t erval; |
| 119 | |
| 120 | /* If not canonical DER, re-encode into canonical DER. */ |
| 121 | if(st->size && st->buf[st->size-1] != 'Z') { |
| 122 | struct tm tm; |
| 123 | time_t tloc; |
| 124 | |
| 125 | errno = EPERM; |
| 126 | tloc = asn_GT2time(st, &tm, 1); /* Recognize time */ |
| 127 | if(tloc == -1 && errno != EPERM) { |
| 128 | /* Failed to recognize time. Fail completely. */ |
| 129 | erval.encoded = -1; |
| 130 | erval.failed_type = td; |
| 131 | erval.structure_ptr = ptr; |
| 132 | return erval; |
| 133 | } |
| 134 | st = asn_time2GT(0, &tm, 1); /* Save time canonically */ |
| 135 | if(!st) { |
| 136 | /* Memory allocation failure. */ |
| 137 | erval.encoded = -1; |
| 138 | erval.failed_type = td; |
| 139 | erval.structure_ptr = ptr; |
| 140 | return erval; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | erval = OCTET_STRING_encode_der(td, st, tag_mode, tag, cb, app_key); |
| 145 | |
| 146 | if(st != ptr) { |
| 147 | FREEMEM(st->buf); |
| 148 | FREEMEM(st); |
| 149 | } |
| 150 | |
| 151 | return erval; |
| 152 | } |
| 153 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 154 | int |
| 155 | GeneralizedTime_print(asn1_TYPE_descriptor_t *td, const void *sptr, int ilevel, |
| 156 | asn_app_consume_bytes_f *cb, void *app_key) { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 157 | const GeneralizedTime_t *st = (const GeneralizedTime_t *)sptr; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 158 | |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 159 | (void)td; /* Unused argument */ |
| 160 | (void)ilevel; /* Unused argument */ |
| 161 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 162 | if(st && st->buf) { |
| 163 | char buf[32]; |
| 164 | struct tm tm; |
| 165 | int ret; |
| 166 | |
| 167 | errno = EPERM; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 168 | if(asn_GT2time(st, &tm, 1) == -1 && errno != EPERM) |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 169 | return cb("<bad-value>", 11, app_key); |
| 170 | |
| 171 | ret = snprintf(buf, sizeof(buf), |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 172 | "%04d-%02d-%02d %02d:%02d%02d (GMT)", |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 173 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, |
| 174 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
vlm | b42843a | 2004-06-05 08:17:50 +0000 | [diff] [blame] | 175 | assert(ret > 0 && ret < (int)sizeof(buf)); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 176 | return cb(buf, ret, app_key); |
| 177 | } else { |
| 178 | return cb("<absent>", 8, app_key); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | * Where to look for offset from GMT, Phase I. |
| 184 | * Several platforms are known. |
| 185 | */ |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 186 | #if defined(__FreeBSD__) \ |
| 187 | || (defined(__GNUC__) && defined(__APPLE_CC__)) \ |
| 188 | || (defined __GLIBC__ && __GLIBC__ >= 2) |
| 189 | #undef HAVE_TM_GMTOFF |
| 190 | #define HAVE_TM_GMTOFF |
| 191 | #endif /* BSDs and newer glibc */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 192 | |
| 193 | /* |
| 194 | * Where to look for offset from GMT, Phase II. |
| 195 | */ |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 196 | #ifdef HAVE_TM_GMTOFF |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 197 | #define GMTOFF(tm) ((tm).tm_gmtoff) |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 198 | #else /* HAVE_TM_GMTOFF */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 199 | #define GMTOFF(tm) (-timezone) |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 200 | #endif /* HAVE_TM_GMTOFF */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 201 | |
| 202 | time_t |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 203 | asn_GT2time(const GeneralizedTime_t *st, struct tm *ret_tm, int as_gmt) { |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 204 | struct tm tm_s; |
| 205 | uint8_t *buf; |
| 206 | uint8_t *end; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 207 | int gmtoff_h = 0; |
| 208 | int gmtoff_m = 0; |
| 209 | int gmtoff = 0; /* h + m */ |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 210 | int offset_specified = 0; |
| 211 | time_t tloc; |
| 212 | |
| 213 | if(!st || !st->buf) { |
| 214 | errno = EINVAL; |
| 215 | return -1; |
| 216 | } else { |
| 217 | buf = st->buf; |
| 218 | end = buf + st->size; |
| 219 | } |
| 220 | |
| 221 | if(st->size < 10) { |
| 222 | errno = EINVAL; |
| 223 | return -1; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Decode first 10 bytes: "AAAAMMJJhh" |
| 228 | */ |
| 229 | memset(&tm_s, 0, sizeof(tm_s)); |
| 230 | #undef B2F |
| 231 | #undef B2T |
| 232 | #define B2F(var) do { \ |
| 233 | unsigned ch = *buf; \ |
| 234 | if(ch < 0x30 && ch > 0x39) { \ |
| 235 | errno = EINVAL; \ |
| 236 | return -1; \ |
| 237 | } else { \ |
| 238 | var = var * 10 + (ch - 0x30); \ |
| 239 | buf++; \ |
| 240 | } \ |
| 241 | } while(0) |
| 242 | #define B2T(var) B2F(tm_s.var) |
| 243 | |
| 244 | B2T(tm_year); /* 1: A */ |
| 245 | B2T(tm_year); /* 2: A */ |
| 246 | B2T(tm_year); /* 3: A */ |
| 247 | B2T(tm_year); /* 4: A */ |
| 248 | B2T(tm_mon); /* 5: M */ |
| 249 | B2T(tm_mon); /* 6: M */ |
| 250 | B2T(tm_mday); /* 7: J */ |
| 251 | B2T(tm_mday); /* 8: J */ |
| 252 | B2T(tm_hour); /* 9: h */ |
| 253 | B2T(tm_hour); /* 0: h */ |
| 254 | |
| 255 | if(buf == end) goto local_finish; |
| 256 | |
| 257 | /* |
| 258 | * Parse [mm[ss[(.|,)ffff]]] |
| 259 | * ^^ |
| 260 | */ |
| 261 | switch(*buf) { |
| 262 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 263 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 264 | tm_s.tm_min = (*buf++) - 0x30; |
| 265 | if(buf == end) { errno = EINVAL; return -1; } |
| 266 | B2T(tm_min); |
| 267 | break; |
| 268 | case 0x2B: case 0x2D: /* +, - */ |
| 269 | goto offset; |
| 270 | case 0x5A: /* Z */ |
| 271 | goto utc_finish; |
| 272 | default: |
| 273 | errno = EINVAL; |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | if(buf == end) goto local_finish; |
| 278 | |
| 279 | /* |
| 280 | * Parse [mm[ss[(.|,)ffff]]] |
| 281 | * ^^ |
| 282 | */ |
| 283 | switch(*buf) { |
| 284 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 285 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 286 | tm_s.tm_sec = (*buf++) - 0x30; |
| 287 | if(buf == end) { errno = EINVAL; return -1; } |
| 288 | B2T(tm_sec); |
| 289 | break; |
| 290 | case 0x2B: case 0x2D: /* +, - */ |
| 291 | goto offset; |
| 292 | case 0x5A: /* Z */ |
| 293 | goto utc_finish; |
| 294 | default: |
| 295 | errno = EINVAL; |
| 296 | return -1; |
| 297 | } |
| 298 | |
| 299 | if(buf == end) goto local_finish; |
| 300 | |
| 301 | /* |
| 302 | * Parse [mm[ss[(.|,)ffff]]] |
| 303 | * ^ ^ |
| 304 | */ |
| 305 | switch(*buf) { |
| 306 | case 0x2C: case 0x2E: /* (.|,) */ |
| 307 | /* Fractions of seconds are not supported |
| 308 | * by time_t or struct tm. Skip them */ |
| 309 | for(buf++; buf < end; buf++) { |
| 310 | switch(*buf) { |
| 311 | case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: |
| 312 | case 0x35: case 0x36: case 0x37: case 0x38: case 0x39: |
| 313 | continue; |
| 314 | default: |
| 315 | break; |
| 316 | } |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if(buf == end) goto local_finish; |
| 322 | |
| 323 | switch(*buf) { |
| 324 | case 0x2B: case 0x2D: /* +, - */ |
| 325 | goto offset; |
| 326 | case 0x5A: /* Z */ |
| 327 | goto utc_finish; |
| 328 | default: |
| 329 | errno = EINVAL; |
| 330 | return -1; |
| 331 | } |
| 332 | |
| 333 | |
| 334 | offset: |
| 335 | |
| 336 | if(end - buf < 3) { |
| 337 | errno = EINVAL; |
| 338 | return -1; |
| 339 | } |
| 340 | buf++; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 341 | B2F(gmtoff_h); |
| 342 | B2F(gmtoff_h); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 343 | if(buf[-3] == 0x2D) /* Negative */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 344 | gmtoff = -1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 345 | else |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 346 | gmtoff = 1; |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 347 | |
| 348 | if((end - buf) == 2) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 349 | B2F(gmtoff_m); |
| 350 | B2F(gmtoff_m); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 351 | } else if(end != buf) { |
| 352 | errno = EINVAL; |
| 353 | return -1; |
| 354 | } |
| 355 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 356 | gmtoff = gmtoff * (3600 * gmtoff_h + 60 * gmtoff_m); |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 357 | |
| 358 | /* Fall through */ |
| 359 | utc_finish: |
| 360 | |
| 361 | offset_specified = 1; |
| 362 | |
| 363 | /* Fall through */ |
| 364 | local_finish: |
| 365 | |
| 366 | /* |
| 367 | * Validation. |
| 368 | */ |
| 369 | if((tm_s.tm_mon > 12 || tm_s.tm_mon < 1) |
| 370 | || (tm_s.tm_mday > 31 || tm_s.tm_mday < 1) |
| 371 | || (tm_s.tm_hour > 23) |
| 372 | || (tm_s.tm_sec > 60) |
| 373 | ) { |
| 374 | errno = EINVAL; |
| 375 | return -1; |
| 376 | } |
| 377 | |
| 378 | /* Canonicalize */ |
| 379 | tm_s.tm_mon -= 1; /* 0 - 11 */ |
| 380 | tm_s.tm_year -= 1900; |
| 381 | tm_s.tm_isdst = -1; |
| 382 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 383 | tm_s.tm_sec -= gmtoff; |
| 384 | |
| 385 | /*** AT THIS POINT tm_s is either GMT or local (unknown) ****/ |
| 386 | |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 387 | if(offset_specified) { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 388 | tloc = timegm(&tm_s); |
vlm | c48f213 | 2004-08-12 03:52:53 +0000 | [diff] [blame] | 389 | } else { |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 390 | /* |
| 391 | * Without an offset (or 'Z'), |
| 392 | * we can only guess that it is a local zone. |
| 393 | * Interpret it in this fashion. |
| 394 | */ |
| 395 | tloc = mktime(&tm_s); |
| 396 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 397 | if(tloc == -1) { |
| 398 | errno = EINVAL; |
| 399 | return -1; |
| 400 | } |
| 401 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 402 | if(ret_tm) { |
| 403 | if(as_gmt) { |
| 404 | if(offset_specified) { |
| 405 | *ret_tm = tm_s; |
| 406 | } else { |
| 407 | if(gmtime_r(&tloc, ret_tm) == 0) { |
| 408 | errno = EINVAL; |
| 409 | return -1; |
| 410 | } |
| 411 | } |
| 412 | } else { |
| 413 | if(localtime_r(&tloc, ret_tm) == 0) { |
| 414 | errno = EINVAL; |
| 415 | return -1; |
| 416 | } |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
vlm | fa67ddc | 2004-06-03 03:38:44 +0000 | [diff] [blame] | 420 | return tloc; |
| 421 | } |
| 422 | |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 423 | |
| 424 | GeneralizedTime_t * |
| 425 | asn_time2GT(GeneralizedTime_t *opt_gt, const struct tm *tm, int force_gmt) { |
| 426 | struct tm tm_s; |
| 427 | long gmtoff; |
| 428 | const unsigned int buf_size = 24; /* 4+2+2 +2+2+2 +4 + cushion */ |
| 429 | char *buf; |
| 430 | char *p; |
| 431 | int size; |
| 432 | |
| 433 | /* Check arguments */ |
| 434 | if(!tm) { |
| 435 | errno = EINVAL; |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | /* Pre-allocate a buffer of sufficient yet small length */ |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 440 | (void *)buf = MALLOC(buf_size); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 441 | if(!buf) return 0; |
| 442 | |
| 443 | gmtoff = GMTOFF(*tm); |
| 444 | |
| 445 | if(force_gmt && gmtoff) { |
| 446 | tm_s = *tm; |
| 447 | tm_s.tm_sec -= gmtoff; |
| 448 | timegm(&tm_s); /* Fix the time */ |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 449 | tm = &tm_s; |
vlm | 65efbd0 | 2004-08-11 07:35:08 +0000 | [diff] [blame] | 450 | #ifdef HAVE_TM_GMTOFF |
| 451 | assert(!GMTOFF(tm_s)); /* Will fix itself */ |
| 452 | #else |
| 453 | gmtoff = 0; /* Intervention required */ |
| 454 | #endif |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 455 | } |
| 456 | |
| 457 | size = snprintf(buf, buf_size, "%04d%02d%02d%02d%02d%02d", |
| 458 | tm->tm_year + 1900, |
| 459 | tm->tm_mon + 1, |
| 460 | tm->tm_mday, |
| 461 | tm->tm_hour, |
| 462 | tm->tm_min, |
| 463 | tm->tm_sec |
| 464 | ); |
| 465 | assert(size == 14); |
| 466 | |
| 467 | p = buf + size; |
| 468 | if(force_gmt) { |
| 469 | *p++ = 0x5a; /* 'Z' */ |
| 470 | *p++ = 0; |
| 471 | size++; |
| 472 | } else { |
| 473 | int ret = snprintf(p, buf_size - size, "%+03ld%02ld", |
| 474 | gmtoff / 3600, gmtoff % 3600); |
| 475 | assert(ret >= 5 && ret <= 7); |
| 476 | size += ret; |
| 477 | } |
| 478 | |
| 479 | if(opt_gt) { |
| 480 | if(opt_gt->buf) |
| 481 | FREEMEM(opt_gt->buf); |
| 482 | } else { |
vlm | da67468 | 2004-08-11 09:07:36 +0000 | [diff] [blame] | 483 | (void *)opt_gt = CALLOC(1, sizeof *opt_gt); |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 484 | if(!opt_gt) { free(buf); return 0; } |
| 485 | } |
| 486 | |
vlm | 1ff928d | 2004-08-11 08:10:13 +0000 | [diff] [blame] | 487 | opt_gt->buf = (unsigned char *)buf; |
vlm | 81057a8 | 2004-08-07 03:52:26 +0000 | [diff] [blame] | 488 | opt_gt->size = size; |
| 489 | |
| 490 | return opt_gt; |
| 491 | } |
| 492 | |
| 493 | |