xs/tzif.c

filter: Statementstmt 16.2% 39 / 240 Branchbran 3.8% 11 / 288 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 9.4% 50 / 528 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/types.c ↑ Summary xs/proto_native_parse.c »
1 /* --- Time zones from the TZif database, without touching the process TZ ---
2 *
3 * Ported from ClickHouse::Encoder's datetime.c: TZif v1-v3 transitions plus
4 * the POSIX footer rule (including permanent DST). Wall times resolve like
5 * the server: a fold takes the earlier instant, a gap the offset in force
6 * after the transition. Zones load lazily and stay immutable. */
7  
8 typedef struct {
9 int kind, month, week, day, seconds, clock;
10 } ch_tz_rule_t;
11  
12 typedef struct ch_tz_s ch_tz_t;
13 struct ch_tz_s {
14 ch_tz_t *next;
15 char *name;
16 int64_t *transitions;
17 unsigned char *indices;
18 uint32_t count, type_count;
19 int32_t offsets[256];
20 int footer, has_dst, standard, daylight;
21 ch_tz_rule_t start, end;
22 };
23  
24 static const int ch_tz_mdays[] = {31,28,31,30,31,30,31,31,30,31,30,31};
25  
26 0 static int ch_tz_leap(int year) {
27 0 ▶ return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
Branch: 3 branches
BranchTrueFalseTotal
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 0 0 0
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 0 0 0
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); 0 0 0
28 }
29  
30 0 static int64_t ch_tz_year_days(int year) {
31 0 int y = year - 1;
32 0 return 365LL * (year - 1970) + y / 4 - y / 100 + y / 400 - 477;
33 }
34  
35 0 static int ch_tz_epoch_year(int64_t epoch) {
36 0 int year = 1970 + (int)(epoch / 31556952);
37 0 ▶ while (epoch < ch_tz_year_days(year) * 86400) year--;
Branch: 1 branch
BranchTrueFalseTotal
while (epoch < ch_tz_year_days(year) * 86400) year--; 0 0 0
38 0 ▶ while (epoch >= ch_tz_year_days(year + 1) * 86400) year++;
Branch: 1 branch
BranchTrueFalseTotal
while (epoch >= ch_tz_year_days(year + 1) * 86400) year++; 0 0 0
39 0 return year;
40 }
41  
42 0 static int64_t ch_tz_rule_epoch(const ch_tz_rule_t *rule, int year,
43 int standard, int before) {
44 0 int day = rule->day, month;
45 0 int64_t days = ch_tz_year_days(year);
46 0 ▶ if (rule->kind == 'M') {
Branch: 1 branch
BranchTrueFalseTotal
if (rule->kind == 'M') { 0 0 0
47 int weekday, length;
48 0 ▶ for (month = 1; month < rule->month; month++)
Branch: 1 branch
BranchTrueFalseTotal
for (month = 1; month < rule->month; month++) 0 0 0
49 0 ▶ days += ch_tz_mdays[month - 1] + (month == 2 && ch_tz_leap(year));
Branch: 2 branches
BranchTrueFalseTotal
days += ch_tz_mdays[month - 1] + (month == 2 && ch_tz_leap(year)); 0 0 0
days += ch_tz_mdays[month - 1] + (month == 2 && ch_tz_leap(year)); 0 0 0
50 0 weekday = (int)((days + 4) % 7);
51 0 ▶ if (weekday < 0) weekday += 7;
Branch: 1 branch
BranchTrueFalseTotal
if (weekday < 0) weekday += 7; 0 0 0
52 0 day = (rule->day - weekday + 7) % 7 + 7 * (rule->week - 1);
53 0 length = ch_tz_mdays[rule->month - 1]
54 0 ▶ + (rule->month == 2 && ch_tz_leap(year));
Branch: 2 branches
BranchTrueFalseTotal
+ (rule->month == 2 && ch_tz_leap(year)); 0 0 0
+ (rule->month == 2 && ch_tz_leap(year)); 0 0 0
55 0 ▶ if (day >= length) day -= 7;
Branch: 1 branch
BranchTrueFalseTotal
if (day >= length) day -= 7; 0 0 0
56 0 ▶ } else if (rule->kind == 'J') {
Branch: 1 branch
BranchTrueFalseTotal
} else if (rule->kind == 'J') { 0 0 0
57 0 day--;
58 0 ▶ if (ch_tz_leap(year) && rule->day >= 60) day++;
Branch: 2 branches
BranchTrueFalseTotal
if (ch_tz_leap(year) && rule->day >= 60) day++; 0 0 0
if (ch_tz_leap(year) && rule->day >= 60) day++; 0 0 0
59 }
60 0 return (days + day) * 86400 + rule->seconds
61 0 ▶ - (rule->clock == 'u' ? 0 : rule->clock == 's' ? standard : before);
Branch: 2 branches
BranchTrueFalseTotal
- (rule->clock == 'u' ? 0 : rule->clock == 's' ? standard : before); 0 0 0
- (rule->clock == 'u' ? 0 : rule->clock == 's' ? standard : before); 0 0 0
62 }
63  
64 /* UTC offset in seconds at an instant; NULL zone is UTC. */
65 6 static int ch_tz_offset(const ch_tz_t *tz, int64_t epoch) {
66 6 uint32_t lo = 0, hi;
67 6 ▶ if (!tz) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!tz) return 0; 0 6 6
68 6 ▶ if (tz->footer && (!tz->count || epoch > tz->transitions[tz->count - 1])) {
Branch: 3 branches
BranchTrueFalseTotal
if (tz->footer && (!tz->count || epoch > tz->transitions[tz->count - 1])) { 0 6 6
if (tz->footer && (!tz->count || epoch > tz->transitions[tz->count - 1])) { 0 0 0
if (tz->footer && (!tz->count || epoch > tz->transitions[tz->count - 1])) { 0 0 0
69 0 int year, y, offset = tz->standard;
70 0 int64_t latest = INT64_MIN;
71 0 ▶ if (!tz->has_dst) return tz->standard;
Branch: 1 branch
BranchTrueFalseTotal
if (!tz->has_dst) return tz->standard; 0 0 0
72 0 year = ch_tz_epoch_year(epoch);
73 0 ▶ for (y = year - 1; y <= year + 1; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = year - 1; y <= year + 1; y++) { 0 0 0
74 0 int64_t start = ch_tz_rule_epoch(&tz->start, y, tz->standard, tz->standard);
75 0 int64_t end = ch_tz_rule_epoch(&tz->end, y, tz->standard, tz->daylight);
76 /* >= so a year-round DST rule (end(y) == start(y+1)) stays in DST */
77 0 ▶ if (start <= epoch && start >= latest) { latest = start; offset = tz->daylight; }
Branch: 2 branches
BranchTrueFalseTotal
if (start <= epoch && start >= latest) { latest = start; offset = tz->daylight; } 0 0 0
if (start <= epoch && start >= latest) { latest = start; offset = tz->daylight; } 0 0 0
78 0 ▶ if (end <= epoch && end > latest) { latest = end; offset = tz->standard; }
Branch: 2 branches
BranchTrueFalseTotal
if (end <= epoch && end > latest) { latest = end; offset = tz->standard; } 0 0 0
if (end <= epoch && end > latest) { latest = end; offset = tz->standard; } 0 0 0
79 }
80 0 return offset;
81 }
82 6 hi = tz->count;
83 6 ▶ while (lo < hi) {
Branch: 1 branch
BranchTrueFalseTotal
while (lo < hi) { 0 6 6
84 0 uint32_t mid = lo + (hi - lo) / 2;
85 0 ▶ if (tz->transitions[mid] <= epoch) lo = mid + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (tz->transitions[mid] <= epoch) lo = mid + 1; 0 0 0
86 0 else hi = mid;
87 }
88 6 ▶ return tz->offsets[lo ? tz->indices[lo - 1] : 0];
Branch: 1 branch
BranchTrueFalseTotal
return tz->offsets[lo ? tz->indices[lo - 1] : 0]; 0 6 6
89 }
90  
91 /* Instant of a wall time given as seconds-as-if-UTC: the offsets two days
92 * either side are the candidates (wider than any real offset change, such
93 * as Samoa's 24 h jump); a fold takes the earlier instant, a gap the
94 * offset after the transition. */
95 2 static int64_t ch_tz_wall_to_epoch(const ch_tz_t *tz, int64_t wall) {
96 2 int offs[3], i, found = 0;
97 2 int64_t best = 0;
98 2 ▶ if (!tz) return wall;
Branch: 1 branch
BranchTrueFalseTotal
if (!tz) return wall; 0 2 2
99 2 offs[0] = ch_tz_offset(tz, wall - 2 * 86400);
100 2 offs[2] = ch_tz_offset(tz, wall + 2 * 86400);
101 2 ▶ if (offs[0] == offs[2] && ch_tz_offset(tz, wall - offs[0]) == offs[0])
Branch: 2 branches
BranchTrueFalseTotal
if (offs[0] == offs[2] && ch_tz_offset(tz, wall - offs[0]) == offs[0]) 2 0 2
if (offs[0] == offs[2] && ch_tz_offset(tz, wall - offs[0]) == offs[0]) 2 0 2
102 2 return wall - offs[0];
103 /* the offset at the wall time itself covers two changes in the window */
104 0 offs[1] = ch_tz_offset(tz, wall);
105 0 ▶ for (i = 0; i < 3; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 3; i++) { 0 0 0
106 0 int64_t t = wall - offs[i];
107 0 ▶ if (ch_tz_offset(tz, t) == offs[i] && (!found || t < best)) {
Branch: 3 branches
BranchTrueFalseTotal
if (ch_tz_offset(tz, t) == offs[i] && (!found || t < best)) { 0 0 0
if (ch_tz_offset(tz, t) == offs[i] && (!found || t < best)) { 0 0 0
if (ch_tz_offset(tz, t) == offs[i] && (!found || t < best)) { 0 0 0
108 0 best = t;
109 0 found = 1;
110 }
111 }
112 0 ▶ return found ? best : wall - offs[2];
Branch: 1 branch
BranchTrueFalseTotal
return found ? best : wall - offs[2]; 0 0 0
113 }
114  
115 0 static int ch_tz_number(const char **p, int limit) {
116 0 int n = 0;
117 0 ▶ if (**p < '0' || **p > '9') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (**p < '0' || **p > '9') return -1; 0 0 0
if (**p < '0' || **p > '9') return -1; 0 0 0
118 0 ▶ while (**p >= '0' && **p <= '9') {
Branch: 2 branches
BranchTrueFalseTotal
while (**p >= '0' && **p <= '9') { 0 0 0
while (**p >= '0' && **p <= '9') { 0 0 0
119 0 int d = *(*p)++ - '0';
120 0 ▶ if (n > (limit - d) / 10) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n > (limit - d) / 10) return -1; 0 0 0
121 0 n = n * 10 + d;
122 }
123 0 ▶ return n <= limit ? n : -1;
Branch: 1 branch
BranchTrueFalseTotal
return n <= limit ? n : -1; 0 0 0
124 }
125  
126 0 static int ch_tz_abbreviation(const char **p) {
127 0 const char *start = *p;
128 0 ▶ if (**p == '<') {
Branch: 1 branch
BranchTrueFalseTotal
if (**p == '<') { 0 0 0
129 0 (*p)++;
130 0 ▶ while (**p && **p != '>') (*p)++;
Branch: 2 branches
BranchTrueFalseTotal
while (**p && **p != '>') (*p)++; 0 0 0
while (**p && **p != '>') (*p)++; 0 0 0
131 0 ▶ if (**p != '>') return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (**p != '>') return 0; 0 0 0
132 0 (*p)++;
133 0 return *p - start >= 3;
134 }
135 0 ▶ while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++;
Branch: 4 branches
BranchTrueFalseTotal
while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++; 0 0 0
while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++; 0 0 0
while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++; 0 0 0
while ((**p >= 'A' && **p <= 'Z') || (**p >= 'a' && **p <= 'z')) (*p)++; 0 0 0
136 0 return *p - start >= 3;
137 }
138  
139 0 static int ch_tz_seconds(const char **p, int *out) {
140 0 int sign = 1, h, m = 0, s = 0;
141 0 ▶ if (**p == '-' || **p == '+') sign = *(*p)++ == '-' ? -1 : 1;
Branch: 3 branches
BranchTrueFalseTotal
if (**p == '-' || **p == '+') sign = *(*p)++ == '-' ? -1 : 1; 0 0 0
if (**p == '-' || **p == '+') sign = *(*p)++ == '-' ? -1 : 1; 0 0 0
if (**p == '-' || **p == '+') sign = *(*p)++ == '-' ? -1 : 1; 0 0 0
142 0 h = ch_tz_number(p, 167);
143 0 ▶ if (h < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (h < 0) return 0; 0 0 0
144 0 ▶ if (**p == ':') { (*p)++; m = ch_tz_number(p, 59); }
Branch: 1 branch
BranchTrueFalseTotal
if (**p == ':') { (*p)++; m = ch_tz_number(p, 59); } 0 0 0
145 0 ▶ if (**p == ':') { (*p)++; s = ch_tz_number(p, 59); }
Branch: 1 branch
BranchTrueFalseTotal
if (**p == ':') { (*p)++; s = ch_tz_number(p, 59); } 0 0 0
146 0 ▶ if (m < 0 || s < 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (m < 0 || s < 0) return 0; 0 0 0
if (m < 0 || s < 0) return 0; 0 0 0
147 0 *out = sign * (h * 3600 + m * 60 + s);
148 0 return 1;
149 }
150  
151 0 static int ch_tz_parse_rule(const char **p, ch_tz_rule_t *rule) {
152 0 rule->clock = 'w';
153 0 rule->seconds = 7200;
154 0 ▶ if (**p == 'M') {
Branch: 1 branch
BranchTrueFalseTotal
if (**p == 'M') { 0 0 0
155 0 rule->kind = *(*p)++;
156 0 rule->month = ch_tz_number(p, 12);
157 0 ▶ if (rule->month < 1 || *(*p)++ != '.') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (rule->month < 1 || *(*p)++ != '.') return 0; 0 0 0
if (rule->month < 1 || *(*p)++ != '.') return 0; 0 0 0
158 0 rule->week = ch_tz_number(p, 5);
159 0 ▶ if (rule->week < 1 || *(*p)++ != '.') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (rule->week < 1 || *(*p)++ != '.') return 0; 0 0 0
if (rule->week < 1 || *(*p)++ != '.') return 0; 0 0 0
160 0 rule->day = ch_tz_number(p, 6);
161 } else {
162 0 ▶ rule->kind = **p == 'J' ? *(*p)++ : 'n';
Branch: 1 branch
BranchTrueFalseTotal
rule->kind = **p == 'J' ? *(*p)++ : 'n'; 0 0 0
163 0 rule->day = ch_tz_number(p, 365);
164 0 ▶ if (rule->kind == 'J' && rule->day == 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (rule->kind == 'J' && rule->day == 0) return 0; 0 0 0
if (rule->kind == 'J' && rule->day == 0) return 0; 0 0 0
165 }
166 0 ▶ if (rule->day < 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (rule->day < 0) return 0; 0 0 0
167 0 ▶ if (**p == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (**p == '/') { 0 0 0
168 0 (*p)++;
169 0 ▶ if (!ch_tz_seconds(p, &rule->seconds)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ch_tz_seconds(p, &rule->seconds)) return 0; 0 0 0
170 0 ▶ if (**p == 's' || **p == 'w') rule->clock = *(*p)++;
Branch: 2 branches
BranchTrueFalseTotal
if (**p == 's' || **p == 'w') rule->clock = *(*p)++; 0 0 0
if (**p == 's' || **p == 'w') rule->clock = *(*p)++; 0 0 0
171 0 ▶ else if (**p == 'u' || **p == 'g' || **p == 'z') { rule->clock = 'u'; (*p)++; }
Branch: 3 branches
BranchTrueFalseTotal
else if (**p == 'u' || **p == 'g' || **p == 'z') { rule->clock = 'u'; (*p)++; } 0 0 0
else if (**p == 'u' || **p == 'g' || **p == 'z') { rule->clock = 'u'; (*p)++; } 0 0 0
else if (**p == 'u' || **p == 'g' || **p == 'z') { rule->clock = 'u'; (*p)++; } 0 0 0
172 }
173 0 return 1;
174 }
175  
176 0 static int ch_tz_footer(ch_tz_t *tz, const char *p) {
177 int offset;
178 0 ▶ if (!ch_tz_abbreviation(&p) || !ch_tz_seconds(&p, &offset)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!ch_tz_abbreviation(&p) || !ch_tz_seconds(&p, &offset)) return 0; 0 0 0
if (!ch_tz_abbreviation(&p) || !ch_tz_seconds(&p, &offset)) return 0; 0 0 0
179 0 tz->standard = -offset;
180 0 tz->footer = 1;
181 0 ▶ if (!*p) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (!*p) return 1; 0 0 0
182 0 ▶ if (!ch_tz_abbreviation(&p)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ch_tz_abbreviation(&p)) return 0; 0 0 0
183 0 tz->has_dst = 1;
184 0 tz->daylight = tz->standard + 3600;
185 0 ▶ if (*p != ',') {
Branch: 1 branch
BranchTrueFalseTotal
if (*p != ',') { 0 0 0
186 0 ▶ if (!ch_tz_seconds(&p, &offset)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!ch_tz_seconds(&p, &offset)) return 0; 0 0 0
187 0 tz->daylight = -offset;
188 }
189 0 ▶ if (*p++ != ',' || !ch_tz_parse_rule(&p, &tz->start)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*p++ != ',' || !ch_tz_parse_rule(&p, &tz->start)) return 0; 0 0 0
if (*p++ != ',' || !ch_tz_parse_rule(&p, &tz->start)) return 0; 0 0 0
190 0 ▶ if (*p++ != ',' || !ch_tz_parse_rule(&p, &tz->end)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (*p++ != ',' || !ch_tz_parse_rule(&p, &tz->end)) return 0; 0 0 0
if (*p++ != ',' || !ch_tz_parse_rule(&p, &tz->end)) return 0; 0 0 0
191 0 return !*p;
192 }
193  
194 0 static uint32_t ch_tz_u32(const unsigned char *p) {
195 0 return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16
196 0 | (uint32_t)p[2] << 8 | p[3];
197 }
198  
199 0 static uint64_t ch_tz_section_size(const unsigned char *h, int width) {
200 0 return (uint64_t)ch_tz_u32(h + 32) * (width + 1)
201 0 + (uint64_t)ch_tz_u32(h + 36) * 6 + ch_tz_u32(h + 40)
202 0 + (uint64_t)ch_tz_u32(h + 28) * (width + 4)
203 0 + ch_tz_u32(h + 20) + ch_tz_u32(h + 24);
204 }
205  
206 48 static void ch_tz_free(ch_tz_t *tz) {
207 50 ▶ while (tz) {
Branch: 1 branch
BranchTrueFalseTotal
while (tz) { 2 48 50
208 2 ch_tz_t *next = tz->next;
209 2 Safefree(tz->name);
210 2 Safefree(tz->transitions);
211 2 Safefree(tz->indices);
212 2 Safefree(tz);
213 2 tz = next;
214 }
215 48 }
216  
217 0 static ch_tz_t *ch_tz_parse(const unsigned char *h, size_t length) {
218 0 ch_tz_t *tz = NULL;
219 const unsigned char *p;
220 uint64_t size;
221 uint32_t i;
222 0 int width = 4;
223 0 ▶ if (length < 44 || memcmp(h, "TZif", 4)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (length < 44 || memcmp(h, "TZif", 4)) return NULL; 0 0 0
if (length < 44 || memcmp(h, "TZif", 4)) return NULL; 0 0 0
224 0 size = ch_tz_section_size(h, width);
225 0 ▶ if (size > length - 44) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (size > length - 44) return NULL; 0 0 0
226 0 ▶ if (h[4] == '2' || h[4] == '3') {
Branch: 2 branches
BranchTrueFalseTotal
if (h[4] == '2' || h[4] == '3') { 0 0 0
if (h[4] == '2' || h[4] == '3') { 0 0 0
227 0 h += 44 + (size_t)size;
228 0 length -= 44 + (size_t)size;
229 0 ▶ if (length < 44 || memcmp(h, "TZif", 4)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (length < 44 || memcmp(h, "TZif", 4)) return NULL; 0 0 0
if (length < 44 || memcmp(h, "TZif", 4)) return NULL; 0 0 0
230 0 width = 8;
231 0 size = ch_tz_section_size(h, width);
232 0 ▶ if (size > length - 44) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (size > length - 44) return NULL; 0 0 0
233 0 ▶ } else if (h[4] != 0) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (h[4] != 0) { 0 0 0
234 0 return NULL;
235 }
236 0 Newxz(tz, 1, ch_tz_t);
237 0 tz->count = ch_tz_u32(h + 32);
238 0 tz->type_count = ch_tz_u32(h + 36);
239 0 ▶ if (!tz->type_count || tz->type_count > 256) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if (!tz->type_count || tz->type_count > 256) goto invalid; 0 0 0
if (!tz->type_count || tz->type_count > 256) goto invalid; 0 0 0
240 0 p = h + 44;
241 0 ▶ if (tz->count) {
Branch: 1 branch
BranchTrueFalseTotal
if (tz->count) { 0 0 0
242 0 Newx(tz->transitions, tz->count, int64_t);
243 0 Newx(tz->indices, tz->count, unsigned char);
244 }
245 0 ▶ for (i = 0; i < tz->count; i++, p += width) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tz->count; i++, p += width) { 0 0 0
246 0 uint64_t v = ch_tz_u32(p);
247 0 tz->transitions[i] = width == 8 ? (int64_t)((v << 32) | ch_tz_u32(p + 4))
248 0 ▶ : (int64_t)(int32_t)v;
Branch: 1 branch
BranchTrueFalseTotal
: (int64_t)(int32_t)v; 0 0 0
249 0 ▶ if (i && tz->transitions[i] <= tz->transitions[i - 1]) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if (i && tz->transitions[i] <= tz->transitions[i - 1]) goto invalid; 0 0 0
if (i && tz->transitions[i] <= tz->transitions[i - 1]) goto invalid; 0 0 0
250 }
251 0 ▶ for (i = 0; i < tz->count; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tz->count; i++) { 0 0 0
252 0 ▶ if (p[i] >= tz->type_count) goto invalid;
Branch: 1 branch
BranchTrueFalseTotal
if (p[i] >= tz->type_count) goto invalid; 0 0 0
253 0 tz->indices[i] = p[i];
254 }
255 0 p += tz->count;
256 0 ▶ for (i = 0; i < tz->type_count; i++, p += 6) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < tz->type_count; i++, p += 6) { 0 0 0
257 0 tz->offsets[i] = (int32_t)ch_tz_u32(p);
258 0 ▶ if (tz->offsets[i] < -604800 || tz->offsets[i] > 604800) goto invalid;
Branch: 2 branches
BranchTrueFalseTotal
if (tz->offsets[i] < -604800 || tz->offsets[i] > 604800) goto invalid; 0 0 0
if (tz->offsets[i] < -604800 || tz->offsets[i] > 604800) goto invalid; 0 0 0
259 }
260 0 ▶ if (width == 8 && length > 44 + size) {
Branch: 2 branches
BranchTrueFalseTotal
if (width == 8 && length > 44 + size) { 0 0 0
if (width == 8 && length > 44 + size) { 0 0 0
261 0 size_t flen = length - 44 - (size_t)size;
262 0 const char *f = (const char *)h + 44 + (size_t)size;
263 0 ▶ if (flen < 2 || f[0] != '\n' || f[flen - 1] != '\n') goto invalid;
Branch: 3 branches
BranchTrueFalseTotal
if (flen < 2 || f[0] != '\n' || f[flen - 1] != '\n') goto invalid; 0 0 0
if (flen < 2 || f[0] != '\n' || f[flen - 1] != '\n') goto invalid; 0 0 0
if (flen < 2 || f[0] != '\n' || f[flen - 1] != '\n') goto invalid; 0 0 0
264 0 ▶ if (flen > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (flen > 2) { 0 0 0
265 char *text;
266 int ok;
267 0 ▶ if (memchr(f + 1, 0, flen - 2)) goto invalid;
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(f + 1, 0, flen - 2)) goto invalid; 0 0 0
268 0 Newx(text, flen - 1, char);
269 0 Copy(f + 1, text, flen - 2, char);
270 0 text[flen - 2] = '\0';
271 0 ok = ch_tz_footer(tz, text);
272 0 Safefree(text);
273 0 ▶ if (!ok) goto invalid;
Branch: 1 branch
BranchTrueFalseTotal
if (!ok) goto invalid; 0 0 0
274 }
275 }
276 0 return tz;
277 0 invalid:
278 0 ch_tz_free(tz);
279 0 return NULL;
280 }
281  
282 /* The named zone from *zones, loading it on first use. NULL with a
283 * message in err when it is unknown or unreadable. */
284 2 static ch_tz_t *ch_tz_load(ch_tz_t **zones, const char *name,
285 char *err, size_t errsz) {
286 static const char *dirs[] = {
287 "/usr/share/zoneinfo", "/usr/lib/zoneinfo", "/usr/share/lib/zoneinfo"
288 };
289 2 const char *tzdir = getenv("TZDIR");
290 ch_tz_t *tz;
291 2 FILE *fh = NULL;
292 2 unsigned char *data = NULL;
293 2 size_t len = 0, cap = 0;
294 int d;
295 2 ▶ for (tz = *zones; tz; tz = tz->next)
Branch: 1 branch
BranchTrueFalseTotal
for (tz = *zones; tz; tz = tz->next) 0 2 2
296 0 ▶ if (!strcmp(tz->name, name)) return tz;
Branch: 1 branch
BranchTrueFalseTotal
if (!strcmp(tz->name, name)) return tz; 0 0 0
297 2 ▶ if (!strcmp(name, "UTC") || !strcmp(name, "Etc/UTC") || !strcmp(name, "GMT")
Branch: 3 branches
BranchTrueFalseTotal
if (!strcmp(name, "UTC") || !strcmp(name, "Etc/UTC") || !strcmp(name, "GMT") 0 2 2
if (!strcmp(name, "UTC") || !strcmp(name, "Etc/UTC") || !strcmp(name, "GMT") 0 0 0
if (!strcmp(name, "UTC") || !strcmp(name, "Etc/UTC") || !strcmp(name, "GMT") 0 0 0
298 0 ▶ || !strcmp(name, "Etc/GMT") || !strcmp(name, "UCT")
Branch: 2 branches
BranchTrueFalseTotal
|| !strcmp(name, "Etc/GMT") || !strcmp(name, "UCT") 0 0 0
|| !strcmp(name, "Etc/GMT") || !strcmp(name, "UCT") 0 0 0
299 0 ▶ || !strcmp(name, "Universal") || !strcmp(name, "Zulu")) {
Branch: 2 branches
BranchTrueFalseTotal
|| !strcmp(name, "Universal") || !strcmp(name, "Zulu")) { 0 0 0
|| !strcmp(name, "Universal") || !strcmp(name, "Zulu")) { 0 0 0
300 2 Newxz(tz, 1, ch_tz_t);
301 2 goto loaded;
302 }
303 0 ▶ if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\')
Branch: 4 branches
BranchTrueFalseTotal
if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\') 0 0 0
if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\') 0 0 0
if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\') 0 0 0
if (!*name || *name == '/' || strstr(name, "..") || strchr(name, '\\') 0 0 0
304 0 ▶ || strlen(name) > 200) {
Branch: 1 branch
BranchTrueFalseTotal
|| strlen(name) > 200) { 0 0 0
305 0 snprintf(err, errsz, "invalid timezone '%.200s'", name);
306 0 return NULL;
307 }
308 0 ▶ for (d = 0; d < (tzdir ? 1 : 3) && !fh; d++) {
Branch: 3 branches
BranchTrueFalseTotal
for (d = 0; d < (tzdir ? 1 : 3) && !fh; d++) { 0 0 0
for (d = 0; d < (tzdir ? 1 : 3) && !fh; d++) { 0 0 0
for (d = 0; d < (tzdir ? 1 : 3) && !fh; d++) { 0 0 0
309 char path[512];
310 0 ▶ snprintf(path, sizeof(path), "%s/%s", tzdir ? tzdir : dirs[d], name);
Branch: 1 branch
BranchTrueFalseTotal
snprintf(path, sizeof(path), "%s/%s", tzdir ? tzdir : dirs[d], name); 0 0 0
311 0 fh = fopen(path, "rb");
312 }
313 0 ▶ if (!fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (!fh) { 0 0 0
314 0 snprintf(err, errsz, "unknown timezone '%s' or missing timezone "
315 "database", name);
316 0 return NULL;
317 }
318 0 for (;;) {
319 size_t n;
320 0 ▶ if (len == cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (len == cap) { 0 0 0
321 0 ▶ if (cap >= 16777216) break;
Branch: 1 branch
BranchTrueFalseTotal
if (cap >= 16777216) break; 0 0 0
322 0 ▶ cap = cap ? cap * 2 : 8192;
Branch: 1 branch
BranchTrueFalseTotal
cap = cap ? cap * 2 : 8192; 0 0 0
323 0 Renew(data, cap, unsigned char);
324 }
325 0 n = fread(data + len, 1, cap - len, fh);
326 0 ▶ if (n == 0) break;
Branch: 1 branch
BranchTrueFalseTotal
if (n == 0) break; 0 0 0
327 0 len += n;
328 }
329 0 ▶ if (ferror(fh) || len >= 16777216) {
Branch: 2 branches
BranchTrueFalseTotal
if (ferror(fh) || len >= 16777216) { 0 0 0
if (ferror(fh) || len >= 16777216) { 0 0 0
330 0 fclose(fh);
331 0 Safefree(data);
332 0 snprintf(err, errsz, "cannot read timezone '%s'", name);
333 0 return NULL;
334 }
335 0 fclose(fh);
336 0 ▶ tz = data ? ch_tz_parse(data, len) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
tz = data ? ch_tz_parse(data, len) : NULL; 0 0 0
337 0 Safefree(data);
338 0 ▶ if (!tz) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tz) { 0 0 0
339 0 snprintf(err, errsz, "invalid TZif data for timezone '%s'", name);
340 0 return NULL;
341 }
342 0 loaded:
343 {
344 2 size_t n = strlen(name);
345 2 Newx(tz->name, n + 1, char);
346 2 Copy(name, tz->name, n + 1, char);
347 }
348 2 tz->next = *zones;
349 2 *zones = tz;
350 2 return tz;
351 }
« xs/types.c ↑ Summary xs/proto_native_parse.c »