forked from derickr/timelib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_zoneinfo.c
348 lines (287 loc) · 8.47 KB
/
parse_zoneinfo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* The MIT License (MIT)
*
* Copyright (c) 2017-2018 MongoDB, Inc.
* Copyright (c) 2015 Red Hat, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "timelib.h"
#include "timelib_private.h"
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#ifdef _WIN32
# include "win_dirent.h"
#endif
#ifndef MAXPATHLEN
# ifdef _WIN32
# define MAXPATHLEN 2048
# elif PATH_MAX
# define MAXPATHLEN PATH_MAX
# elif defined(MAX_PATH)
# define MAXPATHLEN MAX_PATH
# else
# define MAXPATHLEN 256
# endif
#endif
#if _MSC_VER
# define TIMELIB_DIR_SEPARATOR "\\"
#else
# define TIMELIB_DIR_SEPARATOR "/"
#endif
/* Filter out some non-tzdata files and the posix/right databases, if
* present. */
static int index_filter(const struct dirent *ent)
{
return strcmp(ent->d_name, ".") != 0
&& strcmp(ent->d_name, "..") != 0
&& strcmp(ent->d_name, "posix") != 0
&& strcmp(ent->d_name, "posixrules") != 0
&& strcmp(ent->d_name, "right") != 0
&& strcmp(ent->d_name, "Etc") != 0
&& strcmp(ent->d_name, "localtime") != 0
&& strstr(ent->d_name, ".list") == NULL
&& strstr(ent->d_name, ".tab") == NULL;
}
static int timelib_alphasort(const struct dirent **a, const struct dirent **b)
{
return strcmp((*a)->d_name, (*b)->d_name);
}
static int sysdbcmp(const void *first, const void *second)
{
const timelib_tzdb_index_entry *alpha = first, *beta = second;
return timelib_strcasecmp(alpha->id, beta->id);
}
/* Returns true if the passed-in stat structure describes a
* probably-valid timezone file. */
static int is_valid_tzfile(const struct stat *st, int fd)
{
if (fd) {
char buf[20];
if (read(fd, buf, 20) != 20) {
return 0;
}
lseek(fd, SEEK_SET, 0);
if (memcmp(buf, "TZif", 4)) {
return 0;
}
}
return S_ISREG(st->st_mode) && st->st_size > 20;
}
/* Return the contents of the tzfile if found, else NULL. On success, the
* length of the mapped data is placed in *length. */
static char *read_tzfile(const char *directory, const char *timezone, size_t *length)
{
char *fname;
size_t fname_len;
char *buffer;
struct stat st;
int fd;
#ifdef _WIN32
int read_bytes;
#else
ssize_t read_bytes;
#endif
if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
return NULL;
}
fname_len = strlen(directory) + strlen(TIMELIB_DIR_SEPARATOR) + strlen(timezone) + 1;
fname = malloc(fname_len);
if (snprintf(fname, fname_len, "%s%s%s", directory, TIMELIB_DIR_SEPARATOR, timezone) < 0) {
free(fname);
return NULL;
}
fd = open(fname, O_RDONLY);
free(fname);
if (fd == -1) {
return NULL;
} else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
close(fd);
return NULL;
}
*length = st.st_size;
buffer = calloc(1, *length + 1);
read_bytes = read(fd, buffer, *length);
close(fd);
if (read_bytes == -1 || read_bytes != st.st_size) {
return NULL;
}
return buffer;
}
static int timelib_scandir(const char *directory_name, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **))
{
DIR *dir;
struct dirent **entries = NULL;
int entries_size = 0;
int entries_count = 0;
char entry_container[sizeof(struct dirent) + MAXPATHLEN];
struct dirent *entry = (struct dirent *)&entry_container;
dir = opendir(directory_name);
if (!dir) {
return -1;
}
while ((entry = readdir(dir)) != NULL) {
int new_entry_size = 0;
struct dirent *new_entry = NULL;
/* Skip if the filter matches */
if (filter && (*filter)(entry) == 0) {
continue;
}
/* Make sure our entries array is large enough */
if (entries_count == entries_size) {
struct dirent **new_entries;
if (entries_size == 0) {
entries_size = 16;
} else {
entries_size *= 2;
}
new_entries = (struct dirent **) realloc(entries, entries_size * sizeof(struct dirent *));
if (!new_entries) {
return -1;
}
entries = new_entries;
}
/* Add our new entry to the list */
new_entry_size = sizeof(struct dirent) + (strlen(entry->d_name) + 1);
new_entry = (struct dirent *) malloc(new_entry_size);
if (new_entry == NULL) {
goto cleanup;
}
entries[entries_count++] = (struct dirent *) memcpy(new_entry, entry, new_entry_size);
}
closedir(dir);
*namelist = entries;
if (compar) {
qsort(*namelist, entries_count, sizeof(struct dirent *), (int (*) (const void *, const void *)) compar);
}
return entries_count;
cleanup:
while (entries_count-- > 0) {
free(entries[entries_count]);
}
free(entries);
return -1;
}
/* Create the zone identifier index by trawling the filesystem. */
static int create_zone_index(const char *directory, timelib_tzdb *db)
{
size_t dirstack_size, dirstack_top;
size_t index_size, index_next;
timelib_tzdb_index_entry *db_index;
char **dirstack;
size_t data_size = 0;
unsigned char *tmp_data = NULL;
/* LIFO stack to hold directory entries to scan; each slot is a
* directory name relative to the zoneinfo prefix. */
dirstack_size = 32;
dirstack = malloc(dirstack_size * sizeof(*dirstack));
dirstack_top = 1;
dirstack[0] = strdup("");
/* Index array. */
index_size = 64;
db_index = malloc(index_size * sizeof(timelib_tzdb_index_entry));
index_next = 0;
do {
struct dirent **ents;
char name[PATH_MAX], *top;
int count;
/* Pop the top stack entry, and iterate through its contents. */
top = dirstack[--dirstack_top];
snprintf(name, sizeof(name), "%s/%s", directory, top);
count = timelib_scandir(name, &ents, index_filter, timelib_alphasort);
if (count == -1) {
free(dirstack);
free(db_index);
return -errno;
}
while (count > 0) {
struct stat st;
const char *leaf = ents[count - 1]->d_name;
snprintf(name, sizeof(name), "%s%s%s%s%s", directory, TIMELIB_DIR_SEPARATOR, top, TIMELIB_DIR_SEPARATOR, leaf);
if (strlen(name) && stat(name, &st) == 0) {
/* Name, relative to the zoneinfo prefix. */
const char *root = top;
if (root[0] == '/') {
root++;
}
snprintf(name, sizeof(name), "%s%s%s", root, *root ? TIMELIB_DIR_SEPARATOR : "", leaf);
if (S_ISDIR(st.st_mode)) {
if (dirstack_top == dirstack_size) {
dirstack_size *= 2;
dirstack = realloc(dirstack, dirstack_size * sizeof(*dirstack));
}
dirstack[dirstack_top++] = strdup(name);
} else {
if (index_next == index_size) {
index_size *= 2;
db_index = realloc(db_index, index_size * sizeof(timelib_tzdb_index_entry));
}
db_index[index_next].id = strdup(name);
{
size_t length;
char *tzfile_data = read_tzfile(directory, name, &length);
if (tzfile_data) {
tmp_data = realloc(tmp_data, data_size + length);
memcpy(tmp_data + data_size, tzfile_data, length);
db_index[index_next].pos = data_size;
data_size += length;
free(tzfile_data);
index_next++;
} else {
free(db_index[index_next].id);
}
}
}
}
free(ents[--count]);
}
if (count != -1) {
free(ents);
}
free(top);
} while (dirstack_top);
qsort(db_index, index_next, sizeof(*db_index), sysdbcmp);
db->index = db_index;
db->index_size = index_next;
db->data = tmp_data;
free(dirstack);
return 0;
}
timelib_tzdb *timelib_zoneinfo(char *directory)
{
timelib_tzdb *tmp = malloc(sizeof(timelib_tzdb));
tmp->version = "0.system";
tmp->data = NULL;
if (create_zone_index(directory, tmp) < 0) {
free(tmp);
return NULL;
}
return tmp;
}
void timelib_zoneinfo_dtor(timelib_tzdb *tzdb)
{
int i;
for (i = 0; i < tzdb->index_size; i++) {
free(tzdb->index[i].id);
}
free((timelib_tzdb_index_entry*) tzdb->index);
free((char*) tzdb->data);
free(tzdb);
}