parse_tz.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * The MIT License (MIT)
  3. *
  4. * Copyright (c) 2015-2019 Derick Rethans
  5. * Copyright (c) 2018 MongoDB, Inc.
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. #include "timelib.h"
  26. #include "timelib_private.h"
  27. #define TIMELIB_SUPPORTS_V2DATA
  28. #define TIMELIB_SUPPORT_SLIM_FILE
  29. #include "timezonedb.h"
  30. #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
  31. # if defined(__LITTLE_ENDIAN__)
  32. # undef WORDS_BIGENDIAN
  33. # else
  34. # if defined(__BIG_ENDIAN__)
  35. # define WORDS_BIGENDIAN
  36. # endif
  37. # endif
  38. #endif
  39. #if (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN))
  40. # if __BYTE_ORDER == __BIG_ENDIAN
  41. # define WORDS_BIGENDIAN
  42. # endif
  43. #endif
  44. #if defined(__s390__)
  45. # if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  46. # define WORDS_BIGENDIAN
  47. # else
  48. # undef WORDS_BIGENDIAN
  49. # endif
  50. #endif
  51. #ifdef WORDS_BIGENDIAN
  52. static inline uint32_t timelib_conv_int_unsigned(uint32_t value)
  53. {
  54. return value;
  55. }
  56. static inline uint64_t timelib_conv_int64_unsigned(uint64_t value)
  57. {
  58. return value;
  59. }
  60. #else
  61. static inline uint32_t timelib_conv_int_unsigned(uint32_t value)
  62. {
  63. return
  64. ((value & 0x000000ff) << 24) +
  65. ((value & 0x0000ff00) << 8) +
  66. ((value & 0x00ff0000) >> 8) +
  67. ((value & 0xff000000) >> 24);
  68. }
  69. static inline uint64_t timelib_conv_int64_unsigned(uint64_t value)
  70. {
  71. return
  72. ((value & 0x00000000000000ff) << 56) +
  73. ((value & 0x000000000000ff00) << 40) +
  74. ((value & 0x0000000000ff0000) << 24) +
  75. ((value & 0x00000000ff000000) << 8) +
  76. ((value & 0x000000ff00000000) >> 8) +
  77. ((value & 0x0000ff0000000000) >> 24) +
  78. ((value & 0x00ff000000000000) >> 40) +
  79. ((value & 0xff00000000000000) >> 56);
  80. }
  81. #endif
  82. #define timelib_conv_int_signed(value) ((int32_t) timelib_conv_int_unsigned((int32_t) value))
  83. #define timelib_conv_int64_signed(value) ((int64_t) timelib_conv_int64_unsigned((int64_t) value))
  84. static int read_php_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
  85. {
  86. uint32_t version;
  87. /* read ID */
  88. version = (*tzf)[3] - '0';
  89. *tzf += 4;
  90. /* read BC flag */
  91. tz->bc = (**tzf == '\1');
  92. *tzf += 1;
  93. /* read country code */
  94. memcpy(tz->location.country_code, *tzf, 2);
  95. tz->location.country_code[2] = '\0';
  96. *tzf += 2;
  97. /* skip rest of preamble */
  98. *tzf += 13;
  99. return version;
  100. }
  101. static int read_tzif_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
  102. {
  103. uint32_t version;
  104. /* read ID */
  105. switch ((*tzf)[4]) {
  106. case '\0':
  107. version = 0;
  108. break;
  109. case '2':
  110. version = 2;
  111. break;
  112. case '3':
  113. version = 3;
  114. break;
  115. case '4':
  116. version = 4;
  117. break;
  118. default:
  119. return -1;
  120. }
  121. *tzf += 5;
  122. /* set BC flag and country code to default */
  123. tz->bc = 0;
  124. tz->location.country_code[0] = '?';
  125. tz->location.country_code[1] = '?';
  126. tz->location.country_code[2] = '\0';
  127. /* skip rest of preamble */
  128. *tzf += 15;
  129. return version;
  130. }
  131. static int read_preamble(const unsigned char **tzf, timelib_tzinfo *tz, unsigned int *type)
  132. {
  133. /* read marker (TZif) or (PHP) */
  134. if (memcmp(*tzf, "PHP", 3) == 0) {
  135. *type = TIMELIB_TZINFO_PHP;
  136. return read_php_preamble(tzf, tz);
  137. } else if (memcmp(*tzf, "TZif", 4) == 0) {
  138. *type = TIMELIB_TZINFO_ZONEINFO;
  139. return read_tzif_preamble(tzf, tz);
  140. } else {
  141. return -1;
  142. }
  143. }
  144. static void read_32bit_header(const unsigned char **tzf, timelib_tzinfo *tz)
  145. {
  146. uint32_t buffer[6];
  147. memcpy(&buffer, *tzf, sizeof(buffer));
  148. tz->_bit32.ttisgmtcnt = timelib_conv_int_unsigned(buffer[0]);
  149. tz->_bit32.ttisstdcnt = timelib_conv_int_unsigned(buffer[1]);
  150. tz->_bit32.leapcnt = timelib_conv_int_unsigned(buffer[2]);
  151. tz->_bit32.timecnt = timelib_conv_int_unsigned(buffer[3]);
  152. tz->_bit32.typecnt = timelib_conv_int_unsigned(buffer[4]);
  153. tz->_bit32.charcnt = timelib_conv_int_unsigned(buffer[5]);
  154. *tzf += sizeof(buffer);
  155. }
  156. static int detect_slim_file(timelib_tzinfo *tz)
  157. {
  158. if (
  159. (tz->_bit32.ttisgmtcnt == 0) &&
  160. (tz->_bit32.ttisstdcnt == 0) &&
  161. (tz->_bit32.leapcnt == 0) &&
  162. (tz->_bit32.timecnt == 0) &&
  163. (tz->_bit32.typecnt == 1) &&
  164. (tz->_bit32.charcnt == 1)
  165. ) {
  166. return 1;
  167. }
  168. return 0;
  169. }
  170. static int read_64bit_transitions(const unsigned char **tzf, timelib_tzinfo *tz)
  171. {
  172. int64_t *buffer = NULL;
  173. uint32_t i;
  174. unsigned char *cbuffer = NULL;
  175. if (tz->bit64.timecnt) {
  176. buffer = (int64_t*) timelib_malloc(tz->bit64.timecnt * sizeof(int64_t));
  177. if (!buffer) {
  178. return TIMELIB_ERROR_CANNOT_ALLOCATE;
  179. }
  180. memcpy(buffer, *tzf, sizeof(int64_t) * tz->bit64.timecnt);
  181. *tzf += (sizeof(int64_t) * tz->bit64.timecnt);
  182. for (i = 0; i < tz->bit64.timecnt; i++) {
  183. buffer[i] = timelib_conv_int64_signed(buffer[i]);
  184. /* Sanity check to see whether TS is just increasing */
  185. if (i > 0 && !(buffer[i] > buffer[i - 1])) {
  186. return TIMELIB_ERROR_CORRUPT_TRANSITIONS_DONT_INCREASE;
  187. }
  188. }
  189. cbuffer = (unsigned char*) timelib_malloc(tz->bit64.timecnt * sizeof(unsigned char));
  190. if (!cbuffer) {
  191. timelib_free(buffer);
  192. return TIMELIB_ERROR_CANNOT_ALLOCATE;
  193. }
  194. memcpy(cbuffer, *tzf, sizeof(unsigned char) * tz->bit64.timecnt);
  195. *tzf += sizeof(unsigned char) * tz->bit64.timecnt;
  196. }
  197. tz->trans = buffer;
  198. tz->trans_idx = cbuffer;
  199. return 0;
  200. }
  201. static void skip_32bit_transitions(const unsigned char **tzf, timelib_tzinfo *tz)
  202. {
  203. if (tz->_bit32.timecnt) {
  204. *tzf += (sizeof(int32_t) * tz->_bit32.timecnt);
  205. *tzf += sizeof(unsigned char) * tz->_bit32.timecnt;
  206. }
  207. }
  208. static int read_64bit_types(const unsigned char **tzf, timelib_tzinfo *tz)
  209. {
  210. unsigned char *buffer;
  211. int32_t *leap_buffer;
  212. unsigned int i, j;
  213. /* Offset Types */
  214. buffer = (unsigned char*) timelib_malloc(tz->bit64.typecnt * sizeof(unsigned char) * 6);
  215. if (!buffer) {
  216. return TIMELIB_ERROR_CANNOT_ALLOCATE;
  217. }
  218. memcpy(buffer, *tzf, sizeof(unsigned char) * 6 * tz->bit64.typecnt);
  219. *tzf += sizeof(unsigned char) * 6 * tz->bit64.typecnt;
  220. // We add two extra to have space for potential new ttinfo entries due to new types defined in the
  221. // POSIX string
  222. tz->type = (ttinfo*) timelib_calloc(1, (tz->bit64.typecnt + 2) * sizeof(ttinfo));
  223. if (!tz->type) {
  224. timelib_free(buffer);
  225. return TIMELIB_ERROR_CANNOT_ALLOCATE;
  226. }
  227. for (i = 0; i < tz->bit64.typecnt; i++) {
  228. j = i * 6;
  229. tz->type[i].offset = 0;
  230. tz->type[i].offset += (int32_t) (((uint32_t) buffer[j]) << 24) + (buffer[j + 1] << 16) + (buffer[j + 2] << 8) + tz->type[i].offset + buffer[j + 3];
  231. tz->type[i].isdst = buffer[j + 4];
  232. tz->type[i].abbr_idx = buffer[j + 5];
  233. }
  234. timelib_free(buffer);
  235. /* Abbreviations */
  236. tz->timezone_abbr = (char*) timelib_malloc(tz->bit64.charcnt);
  237. if (!tz->timezone_abbr) {
  238. return TIMELIB_ERROR_CORRUPT_NO_ABBREVIATION;
  239. }
  240. memcpy(tz->timezone_abbr, *tzf, sizeof(char) * tz->bit64.charcnt);
  241. *tzf += sizeof(char) * tz->bit64.charcnt;
  242. /* Leap seconds (only use in 'right/') format */
  243. if (tz->bit64.leapcnt) {
  244. leap_buffer = (int32_t *) timelib_malloc(tz->bit64.leapcnt * (sizeof(int64_t) + sizeof(int32_t)));
  245. if (!leap_buffer) {
  246. return TIMELIB_ERROR_CANNOT_ALLOCATE;
  247. }
  248. memcpy(leap_buffer, *tzf, tz->bit64.leapcnt * (sizeof(int64_t) + sizeof(int32_t)));
  249. *tzf += tz->bit64.leapcnt * (sizeof(int64_t) + sizeof(int32_t));
  250. tz->leap_times = (tlinfo*) timelib_malloc(tz->bit64.leapcnt * sizeof(tlinfo));
  251. if (!tz->leap_times) {
  252. timelib_free(leap_buffer);
  253. return TIMELIB_ERROR_CANNOT_ALLOCATE;
  254. }
  255. for (i = 0; i < tz->bit64.leapcnt; i++) {
  256. tz->leap_times[i].trans = timelib_conv_int64_signed(leap_buffer[i * 3 + 1] * 4294967296 + leap_buffer[i * 3]);
  257. tz->leap_times[i].offset = timelib_conv_int_signed(leap_buffer[i * 3 + 2]);
  258. }
  259. timelib_free(leap_buffer);
  260. }
  261. /* Standard/Wall Indicators (unused) */
  262. if (tz->bit64.ttisstdcnt) {
  263. buffer = (unsigned char*) timelib_malloc(tz->bit64.ttisstdcnt * sizeof(unsigned char));
  264. if (!buffer) {
  265. return TIMELIB_ERROR_CANNOT_ALLOCATE;
  266. }
  267. memcpy(buffer, *tzf, sizeof(unsigned char) * tz->bit64.ttisstdcnt);
  268. *tzf += sizeof(unsigned char) * tz->bit64.ttisstdcnt;
  269. for (i = 0; i < tz->bit64.ttisstdcnt; i++) {
  270. tz->type[i].isstdcnt = buffer[i];
  271. }
  272. timelib_free(buffer);
  273. }
  274. /* UT/Local Time Indicators (unused) */
  275. if (tz->bit64.ttisgmtcnt) {
  276. buffer = (unsigned char*) timelib_malloc(tz->bit64.ttisgmtcnt * sizeof(unsigned char));
  277. if (!buffer) {
  278. return TIMELIB_ERROR_CANNOT_ALLOCATE;
  279. }
  280. memcpy(buffer, *tzf, sizeof(unsigned char) * tz->bit64.ttisgmtcnt);
  281. *tzf += sizeof(unsigned char) * tz->bit64.ttisgmtcnt;
  282. for (i = 0; i < tz->bit64.ttisgmtcnt; i++) {
  283. tz->type[i].isgmtcnt = buffer[i];
  284. }
  285. timelib_free(buffer);
  286. }
  287. return 0;
  288. }
  289. static void skip_32bit_types(const unsigned char **tzf, timelib_tzinfo *tz)
  290. {
  291. /* Offset Types */
  292. *tzf += sizeof(unsigned char) * 6 * tz->_bit32.typecnt;
  293. /* Abbreviations */
  294. *tzf += sizeof(char) * tz->_bit32.charcnt;
  295. /* Leap seconds (only use in 'right/') format */
  296. if (tz->_bit32.leapcnt) {
  297. *tzf += sizeof(int32_t) * tz->_bit32.leapcnt * 2;
  298. }
  299. /* Standard/Wall Indicators (unused) */
  300. if (tz->_bit32.ttisstdcnt) {
  301. *tzf += sizeof(unsigned char) * tz->_bit32.ttisstdcnt;
  302. }
  303. /* UT/Local Time Indicators (unused) */
  304. if (tz->_bit32.ttisgmtcnt) {
  305. *tzf += sizeof(unsigned char) * tz->_bit32.ttisgmtcnt;
  306. }
  307. }
  308. static void read_posix_string(const unsigned char **tzf, timelib_tzinfo *tz)
  309. {
  310. const unsigned char *begin;
  311. // POSIX string is delimited by \n
  312. (*tzf)++;
  313. begin = *tzf;
  314. while (*tzf[0] != '\n') {
  315. (*tzf)++;
  316. }
  317. tz->posix_string = timelib_calloc(1, *tzf - begin + 1);
  318. memcpy(tz->posix_string, begin, *tzf - begin);
  319. // skip over closing \n
  320. (*tzf)++;
  321. }
  322. static signed int find_ttinfo_index(timelib_tzinfo *tz, int32_t offset, int isdst, char *abbr)
  323. {
  324. uint64_t i;
  325. for (i = 0; i < tz->bit64.typecnt; i++) {
  326. if (
  327. (offset == tz->type[i].offset) &&
  328. (isdst == tz->type[i].isdst) &&
  329. (strcmp(abbr, &tz->timezone_abbr[tz->type[i].abbr_idx]) == 0)
  330. ) {
  331. return i;
  332. }
  333. }
  334. return TIMELIB_UNSET;
  335. }
  336. static unsigned int add_abbr(timelib_tzinfo *tz, char *abbr)
  337. {
  338. size_t old_length = tz->bit64.charcnt;
  339. size_t new_length = old_length + strlen(abbr) + 1;
  340. tz->timezone_abbr = (char*) timelib_realloc(tz->timezone_abbr, new_length);
  341. memcpy(tz->timezone_abbr + old_length, abbr, strlen(abbr));
  342. tz->bit64.charcnt = new_length;
  343. tz->timezone_abbr[new_length - 1] = '\0';
  344. return old_length;
  345. }
  346. static signed int add_new_ttinfo_index(timelib_tzinfo *tz, int32_t offset, int isdst, char *abbr)
  347. {
  348. tz->type[tz->bit64.typecnt].offset = offset;
  349. tz->type[tz->bit64.typecnt].isdst = isdst;
  350. tz->type[tz->bit64.typecnt].abbr_idx = add_abbr(tz, abbr);
  351. tz->type[tz->bit64.typecnt].isstdcnt = 0;
  352. tz->type[tz->bit64.typecnt].isgmtcnt = 0;
  353. ++tz->bit64.typecnt;
  354. return tz->bit64.typecnt - 1;
  355. }
  356. static int integrate_posix_string(timelib_tzinfo *tz)
  357. {
  358. tz->posix_info = timelib_parse_posix_str(tz->posix_string);
  359. if (!tz->posix_info) {
  360. return 0;
  361. }
  362. tz->posix_info->type_index_std_type = find_ttinfo_index(tz, tz->posix_info->std_offset, 0, tz->posix_info->std);
  363. if (tz->posix_info->type_index_std_type == TIMELIB_UNSET) {
  364. tz->posix_info->type_index_std_type = add_new_ttinfo_index(tz, tz->posix_info->std_offset, 0, tz->posix_info->std);
  365. return 1;
  366. }
  367. /* If there is no DST set for this zone, return */
  368. if (!tz->posix_info->dst) {
  369. return 1;
  370. }
  371. tz->posix_info->type_index_dst_type = find_ttinfo_index(tz, tz->posix_info->dst_offset, 1, tz->posix_info->dst);
  372. if (tz->posix_info->type_index_dst_type == TIMELIB_UNSET) {
  373. tz->posix_info->type_index_dst_type = add_new_ttinfo_index(tz, tz->posix_info->dst_offset, 1, tz->posix_info->dst);
  374. return 1;
  375. }
  376. return 1;
  377. }
  378. static void read_location(const unsigned char **tzf, timelib_tzinfo *tz)
  379. {
  380. uint32_t buffer[3];
  381. uint32_t comments_len;
  382. memcpy(&buffer, *tzf, sizeof(buffer));
  383. tz->location.latitude = timelib_conv_int_unsigned(buffer[0]);
  384. tz->location.latitude = (tz->location.latitude / 100000) - 90;
  385. tz->location.longitude = timelib_conv_int_unsigned(buffer[1]);
  386. tz->location.longitude = (tz->location.longitude / 100000) - 180;
  387. comments_len = timelib_conv_int_unsigned(buffer[2]);
  388. *tzf += sizeof(buffer);
  389. tz->location.comments = timelib_malloc(comments_len + 1);
  390. memcpy(tz->location.comments, *tzf, comments_len);
  391. tz->location.comments[comments_len] = '\0';
  392. *tzf += comments_len;
  393. }
  394. static void set_default_location_and_comments(const unsigned char **tzf, timelib_tzinfo *tz)
  395. {
  396. tz->location.latitude = 0;
  397. tz->location.longitude = 0;
  398. tz->location.comments = timelib_malloc(2);
  399. tz->location.comments[0] = '?';
  400. tz->location.comments[1] = '\0';
  401. }
  402. static char *format_ut_time(timelib_sll ts, timelib_tzinfo *tz)
  403. {
  404. char *tmp = timelib_calloc(1, 64);
  405. timelib_time *t = timelib_time_ctor();
  406. timelib_unixtime2gmt(t, ts);
  407. snprintf(
  408. tmp, 64,
  409. "%04lld-%02lld-%02lld %02lld:%02lld:%02lld UT",
  410. t->y, t->m, t->d,
  411. t->h, t->i, t->s
  412. );
  413. timelib_time_dtor(t);
  414. return tmp;
  415. }
  416. static char *format_offset_type(timelib_tzinfo *tz, int i)
  417. {
  418. char *tmp = timelib_calloc(1, 64);
  419. snprintf(
  420. tmp, 64,
  421. "%3d [%6ld %1d %3d '%s' (%d,%d)]",
  422. i,
  423. (long int) tz->type[i].offset,
  424. tz->type[i].isdst,
  425. tz->type[i].abbr_idx,
  426. &tz->timezone_abbr[tz->type[i].abbr_idx],
  427. tz->type[i].isstdcnt,
  428. tz->type[i].isgmtcnt
  429. );
  430. return tmp;
  431. }
  432. void timelib_dump_tzinfo(timelib_tzinfo *tz)
  433. {
  434. uint32_t i;
  435. char *date_str, *trans_str;
  436. printf("Country Code: %s\n", tz->location.country_code);
  437. printf("Geo Location: %f,%f\n", tz->location.latitude, tz->location.longitude);
  438. printf("Comments:\n%s\n", tz->location.comments);
  439. printf("BC: %s\n", tz->bc ? "no" : "yes");
  440. printf("Slim File: %s\n", detect_slim_file(tz) ? "yes" : "no");
  441. printf("\n64-bit:\n");
  442. printf("UTC/Local count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.ttisgmtcnt);
  443. printf("Std/Wall count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.ttisstdcnt);
  444. printf("Leap.sec. count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.leapcnt);
  445. printf("Trans. count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.timecnt);
  446. printf("Local types count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.typecnt);
  447. printf("Zone Abbr. count: " TIMELIB_ULONG_FMT "\n", (timelib_ulong) tz->bit64.charcnt);
  448. trans_str = format_offset_type(tz, 0);
  449. printf("%22s (%20s) = %s\n", "", "", trans_str);
  450. timelib_free(trans_str);
  451. for (i = 0; i < tz->bit64.timecnt; i++) {
  452. date_str = format_ut_time(tz->trans[i], tz);
  453. trans_str = format_offset_type(tz, tz->trans_idx[i]);
  454. printf(
  455. "%s (%20" PRId64 ") = %s\n",
  456. date_str,
  457. tz->trans[i],
  458. trans_str
  459. );
  460. timelib_free(date_str);
  461. timelib_free(trans_str);
  462. }
  463. for (i = 0; i < tz->bit64.leapcnt; i++) {
  464. date_str = format_ut_time(tz->trans[i], tz);
  465. printf (
  466. "%s (%20ld) = %d\n",
  467. date_str,
  468. (long) tz->leap_times[i].trans,
  469. tz->leap_times[i].offset
  470. );
  471. timelib_free(date_str);
  472. }
  473. if (!tz->posix_string) {
  474. printf("\n%43sNo POSIX string\n", "");
  475. return;
  476. }
  477. if (strcmp("", tz->posix_string) == 0) {
  478. printf("\n%43sEmpty POSIX string\n", "");
  479. return;
  480. }
  481. printf("\n%43sPOSIX string: %s\n", "", tz->posix_string);
  482. if (tz->posix_info && tz->posix_info->std) {
  483. trans_str = format_offset_type(tz, tz->posix_info->type_index_std_type);
  484. printf("%43sstd: %s\n", "", trans_str);
  485. timelib_free(trans_str);
  486. if (tz->posix_info->dst) {
  487. trans_str = format_offset_type(tz, tz->posix_info->type_index_dst_type);
  488. printf("%43sdst: %s\n", "", trans_str);
  489. timelib_free(trans_str);
  490. }
  491. }
  492. }
  493. static int seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
  494. {
  495. int left = 0, right = tzdb->index_size - 1;
  496. if (tzdb->index_size == 0) {
  497. return 0;
  498. }
  499. do {
  500. int mid = ((unsigned)left + right) >> 1;
  501. int cmp = timelib_strcasecmp(timezone, tzdb->index[mid].id);
  502. if (cmp < 0) {
  503. right = mid - 1;
  504. } else if (cmp > 0) {
  505. left = mid + 1;
  506. } else { /* (cmp == 0) */
  507. (*tzf) = &(tzdb->data[tzdb->index[mid].pos]);
  508. return 1;
  509. }
  510. } while (left <= right);
  511. return 0;
  512. }
  513. const timelib_tzdb *timelib_builtin_db(void)
  514. {
  515. return &timezonedb_builtin;
  516. }
  517. const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
  518. {
  519. *count = tzdb->index_size;
  520. return tzdb->index;
  521. }
  522. int timelib_timezone_id_is_valid(const char *timezone, const timelib_tzdb *tzdb)
  523. {
  524. const unsigned char *tzf;
  525. return (seek_to_tz_position(&tzf, timezone, tzdb));
  526. }
  527. static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
  528. {
  529. if (memcmp(*tzf, "TZif2", 5) == 0) {
  530. *tzf += 20;
  531. return 1;
  532. } else if (memcmp(*tzf, "TZif3", 5) == 0) {
  533. *tzf += 20;
  534. return 1;
  535. } else if (memcmp(*tzf, "TZif4", 5) == 0) {
  536. *tzf += 20;
  537. return 1;
  538. } else {
  539. return 0;
  540. }
  541. }
  542. static void read_64bit_header(const unsigned char **tzf, timelib_tzinfo *tz)
  543. {
  544. uint32_t buffer[6];
  545. memcpy(&buffer, *tzf, sizeof(buffer));
  546. tz->bit64.ttisgmtcnt = timelib_conv_int_unsigned(buffer[0]);
  547. tz->bit64.ttisstdcnt = timelib_conv_int_unsigned(buffer[1]);
  548. tz->bit64.leapcnt = timelib_conv_int_unsigned(buffer[2]);
  549. tz->bit64.timecnt = timelib_conv_int_unsigned(buffer[3]);
  550. tz->bit64.typecnt = timelib_conv_int_unsigned(buffer[4]);
  551. tz->bit64.charcnt = timelib_conv_int_unsigned(buffer[5]);
  552. *tzf += sizeof(buffer);
  553. }
  554. static timelib_tzinfo* timelib_tzinfo_ctor(const char *name)
  555. {
  556. timelib_tzinfo *t;
  557. t = timelib_calloc(1, sizeof(timelib_tzinfo));
  558. t->name = timelib_strdup(name);
  559. return t;
  560. }
  561. timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *tzdb, int *error_code)
  562. {
  563. const unsigned char *tzf;
  564. timelib_tzinfo *tmp;
  565. int version;
  566. int transitions_result, types_result;
  567. unsigned int type = TIMELIB_TZINFO_ZONEINFO; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */
  568. *error_code = TIMELIB_ERROR_NO_ERROR;
  569. if (seek_to_tz_position(&tzf, timezone, tzdb)) {
  570. tmp = timelib_tzinfo_ctor(timezone);
  571. version = read_preamble(&tzf, tmp, &type);
  572. if (version < 2 || version > 4) {
  573. *error_code = TIMELIB_ERROR_UNSUPPORTED_VERSION;
  574. timelib_tzinfo_dtor(tmp);
  575. return NULL;
  576. }
  577. //printf("- timezone: %s, version: %0d\n", timezone, version);
  578. read_32bit_header(&tzf, tmp);
  579. skip_32bit_transitions(&tzf, tmp);
  580. skip_32bit_types(&tzf, tmp);
  581. if (!skip_64bit_preamble(&tzf, tmp)) {
  582. /* 64 bit preamble is not in place */
  583. *error_code = TIMELIB_ERROR_CORRUPT_NO_64BIT_PREAMBLE;
  584. timelib_tzinfo_dtor(tmp);
  585. return NULL;
  586. }
  587. read_64bit_header(&tzf, tmp);
  588. if ((transitions_result = read_64bit_transitions(&tzf, tmp)) != 0) {
  589. /* Corrupt file as transitions do not increase */
  590. *error_code = transitions_result;
  591. timelib_tzinfo_dtor(tmp);
  592. return NULL;
  593. }
  594. if ((types_result = read_64bit_types(&tzf, tmp)) != 0) {
  595. *error_code = types_result;
  596. timelib_tzinfo_dtor(tmp);
  597. return NULL;
  598. }
  599. read_posix_string(&tzf, tmp);
  600. if (strcmp("", tmp->posix_string) == 0) {
  601. *error_code = TIMELIB_ERROR_EMPTY_POSIX_STRING;
  602. } else if (!integrate_posix_string(tmp)) {
  603. *error_code = TIMELIB_ERROR_CORRUPT_POSIX_STRING;
  604. timelib_tzinfo_dtor(tmp);
  605. return NULL;
  606. }
  607. if (type == TIMELIB_TZINFO_PHP) {
  608. read_location(&tzf, tmp);
  609. } else {
  610. set_default_location_and_comments(&tzf, tmp);
  611. }
  612. } else {
  613. *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;
  614. tmp = NULL;
  615. }
  616. return tmp;
  617. }
  618. void timelib_tzinfo_dtor(timelib_tzinfo *tz)
  619. {
  620. TIMELIB_TIME_FREE(tz->name);
  621. TIMELIB_TIME_FREE(tz->trans);
  622. TIMELIB_TIME_FREE(tz->trans_idx);
  623. TIMELIB_TIME_FREE(tz->type);
  624. TIMELIB_TIME_FREE(tz->timezone_abbr);
  625. TIMELIB_TIME_FREE(tz->leap_times);
  626. TIMELIB_TIME_FREE(tz->location.comments);
  627. TIMELIB_TIME_FREE(tz->posix_string);
  628. if (tz->posix_info) {
  629. timelib_posix_str_dtor(tz->posix_info);
  630. }
  631. TIMELIB_TIME_FREE(tz);
  632. tz = NULL;
  633. }
  634. timelib_tzinfo *timelib_tzinfo_clone(timelib_tzinfo *tz)
  635. {
  636. timelib_tzinfo *tmp = timelib_tzinfo_ctor(tz->name);
  637. tmp->_bit32.ttisgmtcnt = tz->_bit32.ttisgmtcnt;
  638. tmp->_bit32.ttisstdcnt = tz->_bit32.ttisstdcnt;
  639. tmp->_bit32.leapcnt = tz->_bit32.leapcnt;
  640. tmp->_bit32.timecnt = tz->_bit32.timecnt;
  641. tmp->_bit32.typecnt = tz->_bit32.typecnt;
  642. tmp->_bit32.charcnt = tz->_bit32.charcnt;
  643. tmp->bit64.ttisgmtcnt = tz->bit64.ttisgmtcnt;
  644. tmp->bit64.ttisstdcnt = tz->bit64.ttisstdcnt;
  645. tmp->bit64.leapcnt = tz->bit64.leapcnt;
  646. tmp->bit64.timecnt = tz->bit64.timecnt;
  647. tmp->bit64.typecnt = tz->bit64.typecnt;
  648. tmp->bit64.charcnt = tz->bit64.charcnt;
  649. if (tz->bit64.timecnt) {
  650. tmp->trans = (int64_t *) timelib_malloc(tz->bit64.timecnt * sizeof(int64_t));
  651. tmp->trans_idx = (unsigned char*) timelib_malloc(tz->bit64.timecnt * sizeof(unsigned char));
  652. memcpy(tmp->trans, tz->trans, tz->bit64.timecnt * sizeof(int64_t));
  653. memcpy(tmp->trans_idx, tz->trans_idx, tz->bit64.timecnt * sizeof(unsigned char));
  654. }
  655. tmp->type = (ttinfo*) timelib_malloc(tz->bit64.typecnt * sizeof(ttinfo));
  656. memcpy(tmp->type, tz->type, tz->bit64.typecnt * sizeof(ttinfo));
  657. tmp->timezone_abbr = (char*) timelib_malloc(tz->bit64.charcnt);
  658. memcpy(tmp->timezone_abbr, tz->timezone_abbr, tz->bit64.charcnt);
  659. if (tz->bit64.leapcnt) {
  660. tmp->leap_times = (tlinfo*) timelib_malloc(tz->bit64.leapcnt * sizeof(tlinfo));
  661. memcpy(tmp->leap_times, tz->leap_times, tz->bit64.leapcnt * sizeof(tlinfo));
  662. }
  663. if (tz->posix_string) {
  664. tmp->posix_string = timelib_strdup(tz->posix_string);
  665. }
  666. return tmp;
  667. }
  668. /**
  669. * Algorithm From RFC 8536, Section 3.2
  670. * https://tools.ietf.org/html/rfc8536#section-3.2
  671. */
  672. ttinfo* timelib_fetch_timezone_offset(timelib_tzinfo *tz, timelib_sll ts, timelib_sll *transition_time)
  673. {
  674. uint32_t left, right;
  675. /* RFC 8536: If there are no transitions, local time for all timestamps is specified
  676. * by the TZ string in the footer if present and nonempty; otherwise, it is specified
  677. * by time type 0.
  678. *
  679. * timelib: If there is also no time type 0, return NULL.
  680. */
  681. if (!tz->bit64.timecnt || !tz->trans) {
  682. if (tz->posix_info) {
  683. *transition_time = INT64_MIN;
  684. return timelib_fetch_posix_timezone_offset(tz, ts, NULL);
  685. }
  686. if (tz->bit64.typecnt == 1) {
  687. *transition_time = INT64_MIN;
  688. return &(tz->type[0]);
  689. }
  690. return NULL;
  691. }
  692. /* RFC 8536: Local time for timestamps before the first transition is specified by
  693. * the first time type (time type 0). */
  694. if (ts < tz->trans[0]) {
  695. *transition_time = INT64_MIN;
  696. return &(tz->type[0]);
  697. }
  698. /* RFC 8536: Local time for timestamps on or after the last transition is specified
  699. * by the TZ string in the footer (Section 3.3) if present and nonempty; otherwise,
  700. * it is unspecified.
  701. *
  702. * timelib: For 'unspecified', timelib assumes the last transition
  703. */
  704. if (ts >= tz->trans[tz->bit64.timecnt - 1]) {
  705. if (tz->posix_info) {
  706. return timelib_fetch_posix_timezone_offset(tz, ts, transition_time);
  707. }
  708. *transition_time = tz->trans[tz->bit64.timecnt - 1];
  709. return &(tz->type[tz->trans_idx[tz->bit64.timecnt - 1]]);
  710. }
  711. /* RFC 8536: The type corresponding to a transition time specifies local time for
  712. * timestamps starting at the given transition time and continuing up to, but not
  713. * including, the next transition time. */
  714. left = 0;
  715. right = tz->bit64.timecnt - 1;
  716. while (right - left > 1) {
  717. uint32_t mid = (left + right) >> 1;
  718. if (ts < tz->trans[mid]) {
  719. right = mid;
  720. } else {
  721. left = mid;
  722. }
  723. }
  724. *transition_time = tz->trans[left];
  725. return &(tz->type[tz->trans_idx[left]]);
  726. }
  727. static tlinfo* fetch_leaptime_offset(timelib_tzinfo *tz, timelib_sll ts)
  728. {
  729. int i;
  730. if (!tz->bit64.leapcnt || !tz->leap_times) {
  731. return NULL;
  732. }
  733. for (i = tz->bit64.leapcnt - 1; i > 0; i--) {
  734. if (ts > tz->leap_times[i].trans) {
  735. return &(tz->leap_times[i]);
  736. }
  737. }
  738. return NULL;
  739. }
  740. int timelib_timestamp_is_in_dst(timelib_sll ts, timelib_tzinfo *tz)
  741. {
  742. ttinfo *to;
  743. timelib_sll dummy;
  744. if ((to = timelib_fetch_timezone_offset(tz, ts, &dummy))) {
  745. return to->isdst;
  746. }
  747. return -1;
  748. }
  749. timelib_time_offset *timelib_get_time_zone_info(timelib_sll ts, timelib_tzinfo *tz)
  750. {
  751. ttinfo *to;
  752. tlinfo *tl;
  753. int32_t offset = 0, leap_secs = 0;
  754. char *abbr;
  755. timelib_time_offset *tmp = timelib_time_offset_ctor();
  756. timelib_sll transition_time;
  757. if ((to = timelib_fetch_timezone_offset(tz, ts, &transition_time))) {
  758. offset = to->offset;
  759. abbr = &(tz->timezone_abbr[to->abbr_idx]);
  760. tmp->is_dst = to->isdst;
  761. tmp->transition_time = transition_time;
  762. } else {
  763. offset = 0;
  764. abbr = tz->timezone_abbr;
  765. tmp->is_dst = 0;
  766. tmp->transition_time = 0;
  767. }
  768. if ((tl = fetch_leaptime_offset(tz, ts))) {
  769. leap_secs = -tl->offset;
  770. }
  771. tmp->offset = offset;
  772. tmp->leap_secs = leap_secs;
  773. tmp->abbr = abbr ? timelib_strdup(abbr) : timelib_strdup("GMT");
  774. return tmp;
  775. }
  776. timelib_sll timelib_get_current_offset(timelib_time *t)
  777. {
  778. timelib_time_offset *gmt_offset;
  779. timelib_sll retval;
  780. switch (t->zone_type) {
  781. case TIMELIB_ZONETYPE_ABBR:
  782. case TIMELIB_ZONETYPE_OFFSET:
  783. return t->z + (t->dst * 3600);
  784. case TIMELIB_ZONETYPE_ID:
  785. gmt_offset = timelib_get_time_zone_info(t->sse, t->tz_info);
  786. retval = gmt_offset->offset;
  787. timelib_time_offset_dtor(gmt_offset);
  788. return retval;
  789. default:
  790. return 0;
  791. }
  792. }
  793. int timelib_same_timezone(timelib_time *one, timelib_time *two)
  794. {
  795. if (one->zone_type != two->zone_type) {
  796. return 0;
  797. }
  798. if (one->zone_type == TIMELIB_ZONETYPE_ABBR || one->zone_type == TIMELIB_ZONETYPE_OFFSET) {
  799. if ((one->z + (one->dst * 3600)) == (two->z + (two->dst * 3600))) {
  800. return 1;
  801. }
  802. return 0;
  803. }
  804. if (one->zone_type == TIMELIB_ZONETYPE_ID && strcmp(one->tz_info->name, two->tz_info->name) == 0) {
  805. return 1;
  806. }
  807. return 0;
  808. }