xs/codecs.c

filter: Statementstmt 39.3% 85 / 216 Branchbran 36.6% 63 / 172 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 38.1% 148 / 388 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/proto_native_parse.c ↑ Summary ClickHouse.xs »
1 /* --- Gzip compression/decompression --- */
2  
3 /* Compress data with gzip. Returns malloc'd buffer, sets *out_len. NULL on error. */
4 0 static char* gzip_compress(const char *data, size_t data_len, size_t *out_len) {
5 z_stream strm;
6 char *out;
7 size_t out_cap;
8 int ret;
9  
10 0 ▶ if (data_len > (size_t)UINT_MAX) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (data_len > (size_t)UINT_MAX) return NULL; 0 0 0
11  
12 0 Zero(&strm, 1, z_stream);
13 0 ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, 15 + 16, 8, Z_DEFAULT_STRATEGY);
14 0 ▶ if (ret != Z_OK) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ret != Z_OK) return NULL; 0 0 0
15  
16 0 out_cap = deflateBound(&strm, (uLong)data_len);
17 0 ▶ if (out_cap > (size_t)UINT_MAX) { deflateEnd(&strm); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (out_cap > (size_t)UINT_MAX) { deflateEnd(&strm); return NULL; } 0 0 0
18 0 Newx(out, out_cap, char);
19  
20 0 strm.next_in = (Bytef *)data;
21 0 strm.avail_in = (uInt)data_len;
22 0 strm.next_out = (Bytef *)out;
23 0 strm.avail_out = (uInt)out_cap;
24  
25 0 ret = deflate(&strm, Z_FINISH);
26 0 ▶ if (ret != Z_STREAM_END) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != Z_STREAM_END) { 0 0 0
27 0 Safefree(out);
28 0 deflateEnd(&strm);
29 0 return NULL;
30 }
31  
32 0 *out_len = strm.total_out;
33 0 deflateEnd(&strm);
34 0 return out;
35 }
36  
37 /* Decompress gzip data. Returns malloc'd buffer, sets *out_len. NULL on error. */
38 1 static char* gzip_decompress(const char *data, size_t data_len, size_t *out_len) {
39 z_stream strm;
40 char *out;
41 size_t out_cap;
42 int ret;
43  
44 1 ▶ if (data_len > (size_t)UINT_MAX) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (data_len > (size_t)UINT_MAX) return NULL; 0 1 1
45  
46 1 Zero(&strm, 1, z_stream);
47 1 ret = inflateInit2(&strm, 15 + 16); /* auto-detect gzip */
48 1 ▶ if (ret != Z_OK) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (ret != Z_OK) return NULL; 0 1 1
49  
50 /* Estimate 4x expansion, but clamp to CH_MAX_DECOMPRESS_SIZE so the
51 * initial allocation can never exceed the cap on its own. Comparing
52 * data_len against cap/4 (instead of multiplying first) also avoids a
53 * size_t overflow of data_len*4 on 32-bit. If the real output is
54 * larger than the cap, the doubling branch below trips the limit. */
55 1 ▶ if (data_len > CH_MAX_DECOMPRESS_SIZE / 4)
Branch: 1 branch
BranchTrueFalseTotal
if (data_len > CH_MAX_DECOMPRESS_SIZE / 4) 0 1 1
56 0 out_cap = CH_MAX_DECOMPRESS_SIZE;
57 else
58 1 out_cap = data_len * 4;
59 1 ▶ if (out_cap < 4096) out_cap = 4096;
Branch: 1 branch
BranchTrueFalseTotal
if (out_cap < 4096) out_cap = 4096; 1 0 1
60 1 Newx(out, out_cap, char);
61  
62 1 strm.next_in = (Bytef *)data;
63 1 strm.avail_in = (uInt)data_len;
64  
65 1 *out_len = 0;
66 do {
67 1 ▶ if (*out_len + 4096 > out_cap) {
Branch: 1 branch
BranchTrueFalseTotal
if (*out_len + 4096 > out_cap) { 0 1 1
68 0 out_cap *= 2;
69 0 ▶ if (out_cap > CH_MAX_DECOMPRESS_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (out_cap > CH_MAX_DECOMPRESS_SIZE) { 0 0 0
70 0 Safefree(out);
71 0 inflateEnd(&strm);
72 0 return NULL;
73 }
74 0 Renew(out, out_cap, char);
75 }
76 1 strm.next_out = (Bytef *)(out + *out_len);
77 1 strm.avail_out = (uInt)(out_cap - *out_len);
78  
79 1 ret = inflate(&strm, Z_NO_FLUSH);
80 1 ▶ if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR ||
Branch: 3 branches
BranchTrueFalseTotal
if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR || 1 0 1
if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR || 1 0 1
if (ret == Z_STREAM_ERROR || ret == Z_DATA_ERROR || 1 0 1
81 1 ▶ ret == Z_MEM_ERROR || ret == Z_BUF_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
ret == Z_MEM_ERROR || ret == Z_BUF_ERROR) { 0 1 1
82 0 Safefree(out);
83 0 inflateEnd(&strm);
84 0 return NULL;
85 }
86 1 *out_len = strm.total_out;
87 1 ▶ } while (ret != Z_STREAM_END);
Branch: 1 branch
BranchTrueFalseTotal
} while (ret != Z_STREAM_END); 0 1 1
88  
89 1 inflateEnd(&strm);
90 1 return out;
91 }
92  
93 #ifdef HAVE_LZ4
94  
95 /*
96 * Decompress a ClickHouse LZ4 compressed block.
97 * Input: compressed block starting at checksum (16 + 9 + payload bytes).
98 * Returns malloc'd buffer with decompressed data, sets *out_len.
99 * Returns NULL on error or if need more data (sets *need_more=1).
100 */
101 0 static char* ch_lz4_decompress(const char *data, size_t data_len,
102 size_t *out_len, size_t *consumed,
103 int *need_more, const char **err_reason) {
104 uint32_t compressed_with_header, uncompressed_size;
105 uint32_t payload_size;
106 uint8_t method;
107 char *out;
108 int ret;
109  
110 0 *need_more = 0;
111 0 *consumed = 0;
112 0 ▶ if (err_reason) *err_reason = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = NULL; 0 0 0
113  
114 /* Need at least checksum (16) + header (9) */
115 0 ▶ if (data_len < CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_len < CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE) { 0 0 0
116 0 *need_more = 1;
117 0 return NULL;
118 }
119  
120 /* Read header fields (after 16-byte checksum) */
121 0 method = (uint8_t)data[CH_CHECKSUM_SIZE];
122 0 ▶ if (method != CH_LZ4_METHOD) {
Branch: 1 branch
BranchTrueFalseTotal
if (method != CH_LZ4_METHOD) { 0 0 0
123 0 ▶ if (err_reason) *err_reason = "unsupported compression method";
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = "unsupported compression method"; 0 0 0
124 0 return NULL;
125 }
126 0 memcpy(&compressed_with_header, data + CH_CHECKSUM_SIZE + 1, 4);
127 0 memcpy(&uncompressed_size, data + CH_CHECKSUM_SIZE + 5, 4);
128  
129 0 ▶ if (uncompressed_size > CH_MAX_DECOMPRESS_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (uncompressed_size > CH_MAX_DECOMPRESS_SIZE) { 0 0 0
130 0 ▶ if (err_reason) *err_reason = "decompressed size exceeds 128 MB limit";
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = "decompressed size exceeds 128 MB limit"; 0 0 0
131 0 return NULL;
132 }
133  
134 0 ▶ if (compressed_with_header < CH_COMPRESS_HEADER_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (compressed_with_header < CH_COMPRESS_HEADER_SIZE) { 0 0 0
135 0 ▶ if (err_reason) *err_reason = "compressed_with_header too small";
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = "compressed_with_header too small"; 0 0 0
136 0 return NULL;
137 }
138  
139 0 payload_size = compressed_with_header - CH_COMPRESS_HEADER_SIZE;
140  
141 /* Need full block */
142 0 ▶ if (data_len < CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + payload_size) {
Branch: 1 branch
BranchTrueFalseTotal
if (data_len < CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + payload_size) { 0 0 0
143 0 *need_more = 1;
144 0 return NULL;
145 }
146  
147 /* Verify checksum */
148 {
149 ch_uint128_t expected, actual;
150 0 memcpy(&expected.lo, data, 8);
151 0 memcpy(&expected.hi, data + 8, 8);
152 0 actual = ch_city_hash128(data + CH_CHECKSUM_SIZE, compressed_with_header);
153 0 ▶ if (actual.lo != expected.lo || actual.hi != expected.hi) {
Branch: 2 branches
BranchTrueFalseTotal
if (actual.lo != expected.lo || actual.hi != expected.hi) { 0 0 0
if (actual.lo != expected.lo || actual.hi != expected.hi) { 0 0 0
154 0 ▶ if (err_reason) *err_reason = "CityHash128 checksum mismatch";
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = "CityHash128 checksum mismatch"; 0 0 0
155 0 return NULL;
156 }
157 }
158  
159 0 Newx(out, uncompressed_size, char);
160 0 ret = LZ4_decompress_safe(data + CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE,
161 out, (int)payload_size, (int)uncompressed_size);
162 0 ▶ if (ret < 0 || (uint32_t)ret != uncompressed_size) {
Branch: 2 branches
BranchTrueFalseTotal
if (ret < 0 || (uint32_t)ret != uncompressed_size) { 0 0 0
if (ret < 0 || (uint32_t)ret != uncompressed_size) { 0 0 0
163 0 Safefree(out);
164 0 ▶ if (err_reason) *err_reason = "LZ4 decompression failed";
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = "LZ4 decompression failed"; 0 0 0
165 0 return NULL;
166 }
167  
168 0 *out_len = uncompressed_size;
169 0 *consumed = CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + payload_size;
170 0 return out;
171 }
172  
173 /*
174 * Compress data into a ClickHouse LZ4 compressed block.
175 * Returns malloc'd buffer (checksum + header + LZ4 payload), sets *out_len.
176 */
177 0 static char* ch_lz4_compress(const char *data, size_t data_len, size_t *out_len) {
178 int max_compressed;
179 0 ▶ if (data_len > (size_t)INT_MAX) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (data_len > (size_t)INT_MAX) return NULL; 0 0 0
180 0 max_compressed = LZ4_compressBound((int)data_len);
181 char *out;
182 int compressed_size;
183 uint32_t compressed_with_header;
184 ch_uint128_t checksum;
185  
186 0 Newx(out, CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + max_compressed, char);
187  
188 0 compressed_size = LZ4_compress_default(
189 data, out + CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE,
190 (int)data_len, max_compressed);
191  
192 0 ▶ if (compressed_size <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (compressed_size <= 0) { 0 0 0
193 0 Safefree(out);
194 0 return NULL;
195 }
196  
197 0 compressed_with_header = (uint32_t)compressed_size + CH_COMPRESS_HEADER_SIZE;
198  
199 /* Write header */
200 0 out[CH_CHECKSUM_SIZE] = (char)CH_LZ4_METHOD;
201 0 memcpy(out + CH_CHECKSUM_SIZE + 1, &compressed_with_header, 4);
202 0 { uint32_t uncomp = (uint32_t)data_len;
203 0 memcpy(out + CH_CHECKSUM_SIZE + 5, &uncomp, 4);
204 }
205  
206 /* Compute checksum over header + compressed data */
207 0 checksum = ch_city_hash128(out + CH_CHECKSUM_SIZE, compressed_with_header);
208 0 memcpy(out, &checksum.lo, 8);
209 0 memcpy(out + 8, &checksum.hi, 8);
210  
211 0 *out_len = CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + compressed_size;
212 0 return out;
213 }
214  
215 /* Decompress one or more consecutive LZ4 sub-blocks starting at buf[*pos].
216 * Advances *pos past every consumed sub-block; *out_len is total decompressed
217 * size. Returns malloc'd buffer (caller Safefrees) on success, NULL on
218 * not-enough-data (sets *need_more=1) or on hard error (sets *err) — both for
219 * the first frame, the only one known to belong to this packet.
220 * A short decode over the returned bytes always means need-more-data: the
221 * region isn't length-delimited, so a missing continuation frame is
222 * indistinguishable from corruption, and every consumed frame passed its
223 * CityHash check. */
224 0 static char* ch_lz4_decompress_chain(const char *buf, size_t len, size_t *pos,
225 size_t *out_len, int *need_more,
226 const char **err) {
227 size_t comp_consumed;
228 char *out;
229  
230 0 *need_more = 0;
231 0 *err = NULL;
232  
233 0 out = ch_lz4_decompress(buf + *pos, len - *pos, out_len,
234 &comp_consumed, need_more, err);
235 0 ▶ if (!out) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return NULL; 0 0 0
236 0 *pos += comp_consumed;
237  
238 /* A packet's compressed region is not length-delimited, so past the block's
239 * last frame *pos may sit on the next coalesced packet, whose bytes are
240 * arbitrary. A speculative frame must therefore never stall or fail the
241 * chain — it just ends it, consuming nothing. */
242 0 while (len - *pos >= CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE
243 0 ▶ && (uint8_t)buf[*pos + CH_CHECKSUM_SIZE] == CH_LZ4_METHOD) {
Branch: 2 branches
BranchTrueFalseTotal
&& (uint8_t)buf[*pos + CH_CHECKSUM_SIZE] == CH_LZ4_METHOD) { 0 0 0
&& (uint8_t)buf[*pos + CH_CHECKSUM_SIZE] == CH_LZ4_METHOD) { 0 0 0
244 size_t extra_len, extra_consumed;
245 0 int extra_need_more = 0;
246 0 const char *extra_err = NULL;
247 0 char *extra = ch_lz4_decompress(buf + *pos, len - *pos, &extra_len,
248 &extra_consumed,
249 &extra_need_more, &extra_err);
250 0 ▶ if (!extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (!extra) { 0 0 0
251 /* Not a validated continuation frame (truncated, or the checksum
252 * says these bytes are not ours). Keep what we have; a short
253 * decode over it reads as need-more-data upstream. */
254 0 break;
255 }
256 0 ▶ if (extra_len > CH_MAX_DECOMPRESS_SIZE - *out_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (extra_len > CH_MAX_DECOMPRESS_SIZE - *out_len) { 0 0 0
257 0 Safefree(extra);
258 0 Safefree(out);
259 0 *err = "LZ4 chain decompressed size exceeds limit";
260 0 return NULL;
261 }
262 0 Renew(out, *out_len + extra_len, char);
263 0 Copy(extra, out + *out_len, extra_len, char);
264 0 *out_len += extra_len;
265 0 *pos += extra_consumed;
266 0 Safefree(extra);
267 }
268 0 return out;
269 }
270  
271 #endif /* HAVE_LZ4 */
272  
273 /* --- Days-since-epoch calculation for Date encoding --- */
274  
275 /* Calendar validation for Date/DateTime string encoding. Encode callers
276 * must reject invalid input via the encode-failure channel — silently
277 * wrapping "2024-13-45" into some nearby date corrupts the table. */
278 63 static int all_digits(const char *s, size_t n) {
279 size_t i;
280 229 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 166 63 229
281 166 ▶ if (s[i] < '0' || s[i] > '9') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (s[i] < '0' || s[i] > '9') return 0; 166 0 166
if (s[i] < '0' || s[i] > '9') return 0; 0 166 166
282 63 return 1;
283 }
284  
285 18 static int valid_civil_date(int year, int month, int day) {
286 static const uint8_t dim[12] =
287 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
288 int max_day;
289 18 ▶ if (month < 1 || month > 12 || day < 1) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (month < 1 || month > 12 || day < 1) return 0; 18 0 18
if (month < 1 || month > 12 || day < 1) return 0; 18 0 18
if (month < 1 || month > 12 || day < 1) return 0; 0 18 18
290 18 max_day = dim[month - 1];
291 18 ▶ if (month == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
Branch: 4 branches
BranchTrueFalseTotal
if (month == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) 0 18 18
if (month == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) 0 0 0
if (month == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) 0 0 0
if (month == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) 0 0 0
292 0 max_day = 29;
293 18 return day <= max_day;
294 }
295  
296 #define DIG2(p) (((p)[0] - '0') * 10 + ((p)[1] - '0'))
297 #define DIG4(p) (DIG2(p) * 100 + DIG2((p) + 2))
298  
299 /* 1 if s starts with a valid "YYYY-MM-DD" calendar date. */
300 18 static int valid_date_prefix(const char *s, size_t len) {
301 18 ▶ if (len < 10 || s[4] != '-' || s[7] != '-') return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (len < 10 || s[4] != '-' || s[7] != '-') return 0; 18 0 18
if (len < 10 || s[4] != '-' || s[7] != '-') return 0; 18 0 18
if (len < 10 || s[4] != '-' || s[7] != '-') return 0; 0 18 18
302 18 ▶ if (!all_digits(s, 4) || !all_digits(s + 5, 2) || !all_digits(s + 8, 2))
Branch: 3 branches
BranchTrueFalseTotal
if (!all_digits(s, 4) || !all_digits(s + 5, 2) || !all_digits(s + 8, 2)) 18 0 18
if (!all_digits(s, 4) || !all_digits(s + 5, 2) || !all_digits(s + 8, 2)) 18 0 18
if (!all_digits(s, 4) || !all_digits(s + 5, 2) || !all_digits(s + 8, 2)) 0 18 18
303 0 return 0;
304 18 return valid_civil_date(DIG4(s), DIG2(s + 5), DIG2(s + 8));
305 }
306  
307 /* 1 if the "HH:MM:SS" part at s[11..18] is valid. Caller checked len >= 19.
308 * s[10] may be a space, 'T', or anything else — the encoder ignores it. */
309 1 static int valid_time_part(const char *s) {
310 1 ▶ if (s[13] != ':' || s[16] != ':') return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (s[13] != ':' || s[16] != ':') return 0; 1 0 1
if (s[13] != ':' || s[16] != ':') return 0; 0 1 1
311 1 ▶ if (!all_digits(s + 11, 2) || !all_digits(s + 14, 2) || !all_digits(s + 17, 2))
Branch: 3 branches
BranchTrueFalseTotal
if (!all_digits(s + 11, 2) || !all_digits(s + 14, 2) || !all_digits(s + 17, 2)) 1 0 1
if (!all_digits(s + 11, 2) || !all_digits(s + 14, 2) || !all_digits(s + 17, 2)) 1 0 1
if (!all_digits(s + 11, 2) || !all_digits(s + 14, 2) || !all_digits(s + 17, 2)) 0 1 1
312 0 return 0;
313 1 ▶ return DIG2(s + 11) < 24 && DIG2(s + 14) < 60 && DIG2(s + 17) < 60;
Branch: 3 branches
BranchTrueFalseTotal
return DIG2(s + 11) < 24 && DIG2(s + 14) < 60 && DIG2(s + 17) < 60; 1 0 1
return DIG2(s + 11) < 24 && DIG2(s + 14) < 60 && DIG2(s + 17) < 60; 1 0 1
return DIG2(s + 11) < 24 && DIG2(s + 14) < 60 && DIG2(s + 17) < 60; 1 0 1
314 }
315  
316 /* Seconds-since-midnight for the time part, 0 when absent (len < 19).
317 * Caller validated via valid_time_part when present. */
318 2 static int32_t parse_hms(const char *s, size_t len) {
319 2 ▶ if (len < 19) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 19) return 0; 1 1 2
320 1 return DIG2(s + 11) * 3600 + DIG2(s + 14) * 60 + DIG2(s + 17);
321 }
322  
323 2 static int32_t date_string_to_days(const char *s, size_t len) {
324 int year, month, day;
325 2 ▶ if (len >= 10 && s[4] == '-' && s[7] == '-') {
Branch: 3 branches
BranchTrueFalseTotal
if (len >= 10 && s[4] == '-' && s[7] == '-') { 2 0 2
if (len >= 10 && s[4] == '-' && s[7] == '-') { 2 0 2
if (len >= 10 && s[4] == '-' && s[7] == '-') { 2 0 2
326 2 ▶ if (all_digits(s, 4) && all_digits(s + 5, 2) && all_digits(s + 8, 2)) {
Branch: 3 branches
BranchTrueFalseTotal
if (all_digits(s, 4) && all_digits(s + 5, 2) && all_digits(s + 8, 2)) { 2 0 2
if (all_digits(s, 4) && all_digits(s + 5, 2) && all_digits(s + 8, 2)) { 2 0 2
if (all_digits(s, 4) && all_digits(s + 5, 2) && all_digits(s + 8, 2)) { 2 0 2
327 2 year = DIG4(s);
328 2 month = DIG2(s + 5);
329 2 day = DIG2(s + 8);
330 } else {
331 0 year = atoi(s);
332 0 month = atoi(s + 5);
333 0 day = atoi(s + 8);
334 }
335 /* civil_from_days algorithm (Howard Hinnant) */
336 2 ▶ if (month <= 2) { year--; month += 9; } else { month -= 3; }
Branch: 1 branch
BranchTrueFalseTotal
if (month <= 2) { year--; month += 9; } else { month -= 3; } 0 2 2
337 {
338 2 ▶ int era = (year >= 0 ? year : year - 399) / 400;
Branch: 1 branch
BranchTrueFalseTotal
int era = (year >= 0 ? year : year - 399) / 400; 0 2 2
339 2 unsigned yoe = (unsigned)(year - era * 400);
340 2 unsigned doy = (153 * (unsigned)month + 2) / 5 + (unsigned)day - 1;
341 2 unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy;
342 2 return (int32_t)(era * 146097 + (int)doe - 719468);
343 }
344 }
345 /* fallback: numeric value */
346 0 return (int32_t)strtol(s, NULL, 10);
347 }
348  
349 /* --- TabSeparated parser --- */
350  
351 /* Parse TabSeparated body into AV of AV. Handles \N -> undef, backslash escapes. */
352 1 static AV* parse_tab_separated(const char *data, size_t len) {
353 1 AV *rows = newAV();
354 1 const char *p = data;
355 1 const char *end = data + len;
356 const char *line_start;
357 AV *row;
358 char *buf;
359 size_t buf_len;
360  
361 /* pre-allocate scratch buffer for unescaping */
362 1 Newx(buf, len + 1, char);
363  
364 3 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 2 1 3
365 /* No trailing-newline special case: after each row p advances
366 * past its \n, so a final \n always starts a real (possibly
367 * empty) row rather than a spurious blank line. */
368 2 row = newAV();
369 2 line_start = p;
370  
371 16 ▶ while (p <= end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p <= end) { 16 0 16
372 16 ▶ int is_end_of_line = (p == end || *p == '\n');
Branch: 2 branches
BranchTrueFalseTotal
int is_end_of_line = (p == end || *p == '\n'); 16 0 16
int is_end_of_line = (p == end || *p == '\n'); 2 14 16
373 16 ▶ int is_tab = (!is_end_of_line && *p == '\t');
Branch: 2 branches
BranchTrueFalseTotal
int is_tab = (!is_end_of_line && *p == '\t'); 14 2 16
int is_tab = (!is_end_of_line && *p == '\t'); 2 12 14
374  
375 18 ▶ if (is_end_of_line || is_tab) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_end_of_line || is_tab) { 14 2 16
if (is_end_of_line || is_tab) { 2 12 14
376 4 const char *field_start = line_start;
377 4 size_t field_len = p - field_start;
378  
379 /* check for \N (NULL) */
380 4 ▶ if (field_len == 2 && field_start[0] == '\\' && field_start[1] == 'N') {
Branch: 3 branches
BranchTrueFalseTotal
if (field_len == 2 && field_start[0] == '\\' && field_start[1] == 'N') { 0 4 4
if (field_len == 2 && field_start[0] == '\\' && field_start[1] == 'N') { 0 0 0
if (field_len == 2 && field_start[0] == '\\' && field_start[1] == 'N') { 0 0 0
381 0 av_push(row, newSV(0));
382 } else {
383 /* unescape */
384 4 buf_len = 0;
385 4 const char *s = field_start;
386 4 const char *s_end = field_start + field_len;
387 16 ▶ while (s < s_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < s_end) { 12 4 16
388 12 ▶ if (*s == '\\' && s + 1 < s_end) {
Branch: 2 branches
BranchTrueFalseTotal
if (*s == '\\' && s + 1 < s_end) { 0 12 12
if (*s == '\\' && s + 1 < s_end) { 0 0 0
389 0 s++;
390 0 switch (*s) {
391 0 case 'n': buf[buf_len++] = '\n'; break;
392 0 case 't': buf[buf_len++] = '\t'; break;
393 0 case '\\': buf[buf_len++] = '\\'; break;
394 0 case '\'': buf[buf_len++] = '\''; break;
395 0 case '0': buf[buf_len++] = '\0'; break;
396 0 case 'a': buf[buf_len++] = '\a'; break;
397 0 case 'b': buf[buf_len++] = '\b'; break;
398 0 case 'f': buf[buf_len++] = '\f'; break;
399 0 case 'r': buf[buf_len++] = '\r'; break;
400 0 default: buf[buf_len++] = '\\'; buf[buf_len++] = *s; break;
401 }
402 0 s++;
403 } else {
404 12 buf[buf_len++] = *s++;
405 }
406 }
407 4 av_push(row, newSVpvn(buf, buf_len));
408 }
409  
410 4 ▶ if (is_tab) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_tab) { 2 2 4
411 2 p++;
412 2 line_start = p;
413 } else {
414 2 ▶ if (p < end) p++; /* skip \n */
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; /* skip \n */ 2 0 2
415 2 break;
416 }
417 } else {
418 12 p++;
419 }
420 }
421 2 av_push(rows, newRV_noinc((SV*)row));
422 }
423  
424 1 Safefree(buf);
425 1 return rows;
426 }
427  
« xs/proto_native_parse.c ↑ Summary ClickHouse.xs »