xs/codecs.c

filter: Statementstmt 40.6% 93 / 229 Branchbran 36.6% 66 / 180 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 38.8% 159 / 409 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 /*
94 * Decompress a ClickHouse LZ4 compressed block.
95 * Input: compressed block starting at checksum (16 + 9 + payload bytes).
96 * Returns malloc'd buffer with decompressed data, sets *out_len.
97 * Returns NULL on error or if need more data (sets *need_more=1).
98 */
99 0 static char* ch_lz4_decompress(const char *data, size_t data_len,
100 size_t *out_len, size_t *consumed,
101 int *need_more, const char **err_reason) {
102 uint32_t compressed_with_header, uncompressed_size;
103 uint32_t payload_size;
104 uint8_t method;
105 char *out;
106 int ret;
107  
108 0 *need_more = 0;
109 0 *consumed = 0;
110 0 ▶ if (err_reason) *err_reason = NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = NULL; 0 0 0
111  
112 /* Need at least checksum (16) + header (9) */
113 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
114 0 *need_more = 1;
115 0 return NULL;
116 }
117  
118 /* Read header fields (after 16-byte checksum) */
119 0 method = (uint8_t)data[CH_CHECKSUM_SIZE];
120 0 ▶ if (method != CH_LZ4_METHOD) {
Branch: 1 branch
BranchTrueFalseTotal
if (method != CH_LZ4_METHOD) { 0 0 0
121 0 ▶ if (err_reason) *err_reason = "unsupported compression method";
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = "unsupported compression method"; 0 0 0
122 0 return NULL;
123 }
124 0 memcpy(&compressed_with_header, data + CH_CHECKSUM_SIZE + 1, 4);
125 0 memcpy(&uncompressed_size, data + CH_CHECKSUM_SIZE + 5, 4);
126  
127 0 ▶ if (uncompressed_size > CH_MAX_DECOMPRESS_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (uncompressed_size > CH_MAX_DECOMPRESS_SIZE) { 0 0 0
128 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
129 0 return NULL;
130 }
131  
132 0 ▶ if (compressed_with_header < CH_COMPRESS_HEADER_SIZE) {
Branch: 1 branch
BranchTrueFalseTotal
if (compressed_with_header < CH_COMPRESS_HEADER_SIZE) { 0 0 0
133 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
134 0 return NULL;
135 }
136  
137 0 payload_size = compressed_with_header - CH_COMPRESS_HEADER_SIZE;
138  
139 /* Need full block */
140 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
141 0 *need_more = 1;
142 0 return NULL;
143 }
144  
145 /* Verify checksum */
146 {
147 ch_uint128_t expected, actual;
148 0 memcpy(&expected.lo, data, 8);
149 0 memcpy(&expected.hi, data + 8, 8);
150 0 actual = ch_city_hash128(data + CH_CHECKSUM_SIZE, compressed_with_header);
151 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
152 0 ▶ if (err_reason) *err_reason = "CityHash128 checksum mismatch";
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = "CityHash128 checksum mismatch"; 0 0 0
153 0 return NULL;
154 }
155 }
156  
157 0 Newx(out, uncompressed_size, char);
158 0 ret = LZ4_decompress_safe(data + CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE,
159 out, (int)payload_size, (int)uncompressed_size);
160 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
161 0 Safefree(out);
162 0 ▶ if (err_reason) *err_reason = "LZ4 decompression failed";
Branch: 1 branch
BranchTrueFalseTotal
if (err_reason) *err_reason = "LZ4 decompression failed"; 0 0 0
163 0 return NULL;
164 }
165  
166 0 *out_len = uncompressed_size;
167 0 *consumed = CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + payload_size;
168 0 return out;
169 }
170  
171 /* Input per frame: the server refuses a compressed frame over 1 GiB, and
172 * incompressible input grows by up to 1/255 */
173 #define CH_MAX_FRAME 0x3F800000
174  
175 /* One ClickHouse compressed frame (checksum, method, sizes, payload) of
176 * data[0..len), len <= CH_MAX_FRAME, appended to out. LZ4 into its bound
177 * cannot fail. */
178 0 static void ch_compress_frame(native_buf_t *out, const char *data, size_t len,
179 int zstd, int level) {
180 0 size_t bound, comp = 0, start = out->len;
181 0 uint32_t cwh, ulen = (uint32_t)len;
182 ch_uint128_t checksum;
183 char *hdr;
184  
185 #ifdef HAVE_ZSTD
186 if (zstd) {
187 bound = ZSTD_compressBound(len);
188 nbuf_grow(out, CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + bound);
189 hdr = out->data + start + CH_CHECKSUM_SIZE;
190 comp = ZSTD_compress(hdr + CH_COMPRESS_HEADER_SIZE, bound, data, len, level);
191 /* only an allocation fails: send the frame in LZ4, which needs none */
192 if (ZSTD_isError(comp)) zstd = 0;
193 }
194 #else
195 0 zstd = 0;
196 (void)level;
197 #endif
198 0 ▶ if (!zstd) {
Branch: 1 branch
BranchTrueFalseTotal
if (!zstd) { 0 0 0
199 0 bound = (size_t)LZ4_compressBound((int)len);
200 0 nbuf_grow(out, CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + bound);
201 0 hdr = out->data + start + CH_CHECKSUM_SIZE;
202 0 comp = (size_t)LZ4_compress_default(data, hdr + CH_COMPRESS_HEADER_SIZE,
203 (int)len, (int)bound);
204 }
205 0 cwh = (uint32_t)(comp + CH_COMPRESS_HEADER_SIZE);
206 0 ▶ hdr[0] = (char)(zstd ? CH_ZSTD_METHOD : CH_LZ4_METHOD);
Branch: 1 branch
BranchTrueFalseTotal
hdr[0] = (char)(zstd ? CH_ZSTD_METHOD : CH_LZ4_METHOD); 0 0 0
207 0 memcpy(hdr + 1, &cwh, 4);
208 0 memcpy(hdr + 5, &ulen, 4);
209 0 checksum = ch_city_hash128(hdr, cwh);
210 0 memcpy(out->data + start, &checksum.lo, 8);
211 0 memcpy(out->data + start + 8, &checksum.hi, 8);
212 0 out->len = start + CH_CHECKSUM_SIZE + cwh;
213 0 }
214  
215 /* A Data block's body appended to out, compressed as self compresses: in
216 * frames of compress_block_size (0: the largest the server takes), so the
217 * server decompresses a frame at a time. Frees body. */
218 31 static void nbuf_put_block(native_buf_t *out, native_buf_t *body,
219 const ev_clickhouse_t *self) {
220 31 size_t step = self->compress_block_size, pos = 0;
221 31 ▶ if (!step || step > CH_MAX_FRAME) step = CH_MAX_FRAME;
Branch: 2 branches
BranchTrueFalseTotal
if (!step || step > CH_MAX_FRAME) step = CH_MAX_FRAME; 31 0 31
if (!step || step > CH_MAX_FRAME) step = CH_MAX_FRAME; 0 31 31
222 31 ▶ if (!self->compress)
Branch: 1 branch
BranchTrueFalseTotal
if (!self->compress) 31 0 31
223 31 nbuf_append(out, body->data, body->len);
224 else do {
225 0 size_t n = body->len - pos < step ? body->len - pos : step;
226 0 ch_compress_frame(out, body->data + pos, n, self->compress_zstd,
227 0 self->compress_level);
228 0 pos += n;
229 0 ▶ } while (pos < body->len);
Branch: 1 branch
BranchTrueFalseTotal
} while (pos < body->len); 0 0 0
230 31 Safefree(body->data);
231 31 body->data = NULL;
232 31 }
233  
234 /* Decompress one or more consecutive LZ4 sub-blocks starting at buf[*pos].
235 * Advances *pos past every consumed sub-block; *out_len is total decompressed
236 * size. Returns malloc'd buffer (caller Safefrees) on success, NULL on
237 * not-enough-data (sets *need_more=1) or on hard error (sets *err) — both for
238 * the first frame, the only one known to belong to this packet.
239 * A short decode over the returned bytes always means need-more-data: the
240 * region isn't length-delimited, so a missing continuation frame is
241 * indistinguishable from corruption, and every consumed frame passed its
242 * CityHash check. */
243 0 static char* ch_lz4_decompress_chain(const char *buf, size_t len, size_t *pos,
244 size_t *out_len, int *need_more,
245 const char **err) {
246 size_t comp_consumed;
247 char *out;
248  
249 0 *need_more = 0;
250 0 *err = NULL;
251  
252 0 out = ch_lz4_decompress(buf + *pos, len - *pos, out_len,
253 &comp_consumed, need_more, err);
254 0 ▶ if (!out) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!out) return NULL; 0 0 0
255 0 *pos += comp_consumed;
256  
257 /* A packet's compressed region is not length-delimited, so past the block's
258 * last frame *pos may sit on the next coalesced packet, whose bytes are
259 * arbitrary. A speculative frame must therefore never stall or fail the
260 * chain — it just ends it, consuming nothing. */
261 0 while (len - *pos >= CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE
262 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
263 size_t extra_len, extra_consumed;
264 0 int extra_need_more = 0;
265 0 const char *extra_err = NULL;
266 0 char *extra = ch_lz4_decompress(buf + *pos, len - *pos, &extra_len,
267 &extra_consumed,
268 &extra_need_more, &extra_err);
269 0 ▶ if (!extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (!extra) { 0 0 0
270 /* Not a validated continuation frame (truncated, or the checksum
271 * says these bytes are not ours). Keep what we have; a short
272 * decode over it reads as need-more-data upstream. */
273 0 break;
274 }
275 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
276 0 Safefree(extra);
277 0 Safefree(out);
278 0 *err = "LZ4 chain decompressed size exceeds limit";
279 0 return NULL;
280 }
281 0 Renew(out, *out_len + extra_len, char);
282 0 Copy(extra, out + *out_len, extra_len, char);
283 0 *out_len += extra_len;
284 0 *pos += extra_consumed;
285 0 Safefree(extra);
286 }
287 0 return out;
288 }
289  
290 /* --- Days-since-epoch calculation for Date encoding --- */
291  
292 /* Calendar validation for Date/DateTime string encoding. Encode callers
293 * must reject invalid input via the encode-failure channel — silently
294 * wrapping "2024-13-45" into some nearby date corrupts the table. */
295 63 static int all_digits(const char *s, size_t n) {
296 size_t i;
297 229 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 166 63 229
298 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
299 63 return 1;
300 }
301  
302 18 static int valid_civil_date(int year, int month, int day) {
303 static const uint8_t dim[12] =
304 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
305 int max_day;
306 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
307 18 max_day = dim[month - 1];
308 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
309 0 max_day = 29;
310 18 return day <= max_day;
311 }
312  
313 #define DIG2(p) (((p)[0] - '0') * 10 + ((p)[1] - '0'))
314 #define DIG4(p) (DIG2(p) * 100 + DIG2((p) + 2))
315  
316 /* 1 if s starts with a valid "YYYY-MM-DD" calendar date. */
317 18 static int valid_date_prefix(const char *s, size_t len) {
318 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
319 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
320 0 return 0;
321 18 return valid_civil_date(DIG4(s), DIG2(s + 5), DIG2(s + 8));
322 }
323  
324 /* 1 if the "HH:MM:SS" part at s[11..18] is valid. Caller checked len >= 19.
325 * s[10] may be a space, 'T', or anything else — the encoder ignores it. */
326 1 static int valid_time_part(const char *s) {
327 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
328 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
329 0 return 0;
330 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
331 }
332  
333 /* Seconds-since-midnight for the time part, 0 when absent (len < 19).
334 * Caller validated via valid_time_part when present. */
335 2 static int32_t parse_hms(const char *s, size_t len) {
336 2 ▶ if (len < 19) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (len < 19) return 0; 1 1 2
337 1 return DIG2(s + 11) * 3600 + DIG2(s + 14) * 60 + DIG2(s + 17);
338 }
339  
340 2 static int32_t date_string_to_days(const char *s, size_t len) {
341 int year, month, day;
342 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
343 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
344 2 year = DIG4(s);
345 2 month = DIG2(s + 5);
346 2 day = DIG2(s + 8);
347 } else {
348 0 year = atoi(s);
349 0 month = atoi(s + 5);
350 0 day = atoi(s + 8);
351 }
352 /* civil_from_days algorithm (Howard Hinnant) */
353 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
354 {
355 2 ▶ int era = (year >= 0 ? year : year - 399) / 400;
Branch: 1 branch
BranchTrueFalseTotal
int era = (year >= 0 ? year : year - 399) / 400; 0 2 2
356 2 unsigned yoe = (unsigned)(year - era * 400);
357 2 unsigned doy = (153 * (unsigned)month + 2) / 5 + (unsigned)day - 1;
358 2 unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy;
359 2 return (int32_t)(era * 146097 + (int)doe - 719468);
360 }
361 }
362 /* fallback: numeric value */
363 0 return (int32_t)strtol(s, NULL, 10);
364 }
365  
366 /* --- TabSeparated parser --- */
367  
368 /* Parse TabSeparated body into AV of AV. Handles \N -> undef, backslash escapes. */
369 1 static AV* parse_tab_separated(const char *data, size_t len) {
370 1 AV *rows = newAV();
371 1 const char *p = data;
372 1 const char *end = data + len;
373 const char *line_start;
374 AV *row;
375 char *buf;
376 size_t buf_len;
377  
378 /* pre-allocate scratch buffer for unescaping */
379 1 Newx(buf, len + 1, char);
380  
381 3 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 2 1 3
382 /* No trailing-newline special case: after each row p advances
383 * past its \n, so a final \n always starts a real (possibly
384 * empty) row rather than a spurious blank line. */
385 2 row = newAV();
386 2 line_start = p;
387  
388 16 ▶ while (p <= end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p <= end) { 16 0 16
389 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
390 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
391  
392 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
393 4 const char *field_start = line_start;
394 4 size_t field_len = p - field_start;
395  
396 /* check for \N (NULL) */
397 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
398 0 av_push(row, newSV(0));
399 } else {
400 /* unescape */
401 4 buf_len = 0;
402 4 const char *s = field_start;
403 4 const char *s_end = field_start + field_len;
404 16 ▶ while (s < s_end) {
Branch: 1 branch
BranchTrueFalseTotal
while (s < s_end) { 12 4 16
405 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
406 0 s++;
407 0 switch (*s) {
408 0 case 'n': buf[buf_len++] = '\n'; break;
409 0 case 't': buf[buf_len++] = '\t'; break;
410 0 case '\\': buf[buf_len++] = '\\'; break;
411 0 case '\'': buf[buf_len++] = '\''; break;
412 0 case '0': buf[buf_len++] = '\0'; break;
413 0 case 'a': buf[buf_len++] = '\a'; break;
414 0 case 'b': buf[buf_len++] = '\b'; break;
415 0 case 'f': buf[buf_len++] = '\f'; break;
416 0 case 'r': buf[buf_len++] = '\r'; break;
417 0 default: buf[buf_len++] = '\\'; buf[buf_len++] = *s; break;
418 }
419 0 s++;
420 } else {
421 12 buf[buf_len++] = *s++;
422 }
423 }
424 4 av_push(row, newSVpvn(buf, buf_len));
425 }
426  
427 4 ▶ if (is_tab) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_tab) { 2 2 4
428 2 p++;
429 2 line_start = p;
430 } else {
431 2 ▶ if (p < end) p++; /* skip \n */
Branch: 1 branch
BranchTrueFalseTotal
if (p < end) p++; /* skip \n */ 2 0 2
432 2 break;
433 }
434 } else {
435 12 p++;
436 }
437 }
438 2 av_push(rows, newRV_noinc((SV*)row));
439 }
440  
441 1 Safefree(buf);
442 1 return rows;
443 }
444  
« xs/proto_native_parse.c ↑ Summary ClickHouse.xs »