| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| } 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 |
|
| 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 |
|
| *need_more = 0; |
| 111 |
|
| *consumed = 0; |
| 112 |
|
| if (err_reason) *err_reason = NULL; |
| 113 |
|
| |
| 114 |
|
| /* Need at least checksum (16) + header (9) */ |
| 115 |
|
| if (data_len < CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE) { |
| 116 |
|
| *need_more = 1; |
| 117 |
|
| return NULL; |
| 118 |
|
| } |
| 119 |
|
| |
| 120 |
|
| /* Read header fields (after 16-byte checksum) */ |
| 121 |
|
| method = (uint8_t)data[CH_CHECKSUM_SIZE]; |
| 122 |
|
| if (method != CH_LZ4_METHOD) { |
| 123 |
|
| if (err_reason) *err_reason = "unsupported compression method"; |
| 124 |
|
| return NULL; |
| 125 |
|
| } |
| 126 |
|
| memcpy(&compressed_with_header, data + CH_CHECKSUM_SIZE + 1, 4); |
| 127 |
|
| memcpy(&uncompressed_size, data + CH_CHECKSUM_SIZE + 5, 4); |
| 128 |
|
| |
| 129 |
|
| if (uncompressed_size > CH_MAX_DECOMPRESS_SIZE) { |
| 130 |
|
| if (err_reason) *err_reason = "decompressed size exceeds 128 MB limit"; |
| 131 |
|
| return NULL; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
|
| if (compressed_with_header < CH_COMPRESS_HEADER_SIZE) { |
| 135 |
|
| if (err_reason) *err_reason = "compressed_with_header too small"; |
| 136 |
|
| return NULL; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
|
| payload_size = compressed_with_header - CH_COMPRESS_HEADER_SIZE; |
| 140 |
|
| |
| 141 |
|
| /* Need full block */ |
| 142 |
|
| if (data_len < CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + payload_size) { |
| 143 |
|
| *need_more = 1; |
| 144 |
|
| return NULL; |
| 145 |
|
| } |
| 146 |
|
| |
| 147 |
|
| /* Verify checksum */ |
| 148 |
|
| { |
| 149 |
|
| ch_uint128_t expected, actual; |
| 150 |
|
| memcpy(&expected.lo, data, 8); |
| 151 |
|
| memcpy(&expected.hi, data + 8, 8); |
| 152 |
|
| actual = ch_city_hash128(data + CH_CHECKSUM_SIZE, compressed_with_header); |
| 153 |
|
| if (actual.lo != expected.lo || actual.hi != expected.hi) { |
| 154 |
|
| if (err_reason) *err_reason = "CityHash128 checksum mismatch"; |
| 155 |
|
| return NULL; |
| 156 |
|
| } |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
|
| Newx(out, uncompressed_size, char); |
| 160 |
|
| ret = LZ4_decompress_safe(data + CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE, |
| 161 |
|
| out, (int)payload_size, (int)uncompressed_size); |
| 162 |
|
| if (ret < 0 || (uint32_t)ret != uncompressed_size) { |
| 163 |
|
| Safefree(out); |
| 164 |
|
| if (err_reason) *err_reason = "LZ4 decompression failed"; |
| 165 |
|
| return NULL; |
| 166 |
|
| } |
| 167 |
|
| |
| 168 |
|
| *out_len = uncompressed_size; |
| 169 |
|
| *consumed = CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + payload_size; |
| 170 |
|
| 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 |
|
| static char* ch_lz4_compress(const char *data, size_t data_len, size_t *out_len) { |
| 178 |
|
| int max_compressed; |
| 179 |
|
| if (data_len > (size_t)INT_MAX) return NULL; |
| 180 |
|
| 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 |
|
| Newx(out, CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + max_compressed, char); |
| 187 |
|
| |
| 188 |
|
| compressed_size = LZ4_compress_default( |
| 189 |
|
| data, out + CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE, |
| 190 |
|
| (int)data_len, max_compressed); |
| 191 |
|
| |
| 192 |
|
| if (compressed_size <= 0) { |
| 193 |
|
| Safefree(out); |
| 194 |
|
| return NULL; |
| 195 |
|
| } |
| 196 |
|
| |
| 197 |
|
| compressed_with_header = (uint32_t)compressed_size + CH_COMPRESS_HEADER_SIZE; |
| 198 |
|
| |
| 199 |
|
| /* Write header */ |
| 200 |
|
| out[CH_CHECKSUM_SIZE] = (char)CH_LZ4_METHOD; |
| 201 |
|
| memcpy(out + CH_CHECKSUM_SIZE + 1, &compressed_with_header, 4); |
| 202 |
|
| { uint32_t uncomp = (uint32_t)data_len; |
| 203 |
|
| memcpy(out + CH_CHECKSUM_SIZE + 5, &uncomp, 4); |
| 204 |
|
| } |
| 205 |
|
| |
| 206 |
|
| /* Compute checksum over header + compressed data */ |
| 207 |
|
| checksum = ch_city_hash128(out + CH_CHECKSUM_SIZE, compressed_with_header); |
| 208 |
|
| memcpy(out, &checksum.lo, 8); |
| 209 |
|
| memcpy(out + 8, &checksum.hi, 8); |
| 210 |
|
| |
| 211 |
|
| *out_len = CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE + compressed_size; |
| 212 |
|
| 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 |
|
| 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 |
|
| *need_more = 0; |
| 231 |
|
| *err = NULL; |
| 232 |
|
| |
| 233 |
|
| out = ch_lz4_decompress(buf + *pos, len - *pos, out_len, |
| 234 |
|
| &comp_consumed, need_more, err); |
| 235 |
|
| if (!out) return NULL; |
| 236 |
|
| *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 |
|
| while (len - *pos >= CH_CHECKSUM_SIZE + CH_COMPRESS_HEADER_SIZE |
| 243 |
|
| && (uint8_t)buf[*pos + CH_CHECKSUM_SIZE] == CH_LZ4_METHOD) { |
| 244 |
|
| size_t extra_len, extra_consumed; |
| 245 |
|
| int extra_need_more = 0; |
| 246 |
|
| const char *extra_err = NULL; |
| 247 |
|
| char *extra = ch_lz4_decompress(buf + *pos, len - *pos, &extra_len, |
| 248 |
|
| &extra_consumed, |
| 249 |
|
| &extra_need_more, &extra_err); |
| 250 |
|
| if (!extra) { |
| 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 |
|
| break; |
| 255 |
|
| } |
| 256 |
|
| if (extra_len > CH_MAX_DECOMPRESS_SIZE - *out_len) { |
| 257 |
|
| Safefree(extra); |
| 258 |
|
| Safefree(out); |
| 259 |
|
| *err = "LZ4 chain decompressed size exceeds limit"; |
| 260 |
|
| return NULL; |
| 261 |
|
| } |
| 262 |
|
| Renew(out, *out_len + extra_len, char); |
| 263 |
|
| Copy(extra, out + *out_len, extra_len, char); |
| 264 |
|
| *out_len += extra_len; |
| 265 |
|
| *pos += extra_consumed; |
| 266 |
|
| Safefree(extra); |
| 267 |
|
| } |
| 268 |
|
| 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 |
57 |
| static int all_digits(const char *s, size_t n) { |
| 279 |
|
| size_t i; |
| 280 |
207 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
150 |
57 |
207 |
|
| 281 |
150 |
▶ | if (s[i] < '0' || s[i] > '9') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[i] < '0' || s[i] > '9') return 0; |
150 |
0 |
150 |
| if (s[i] < '0' || s[i] > '9') return 0; |
0 |
150 |
150 |
|
| 282 |
57 |
| 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
| Branch | True | False | Total |
| 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
| Branch | True | False | Total |
| 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 |
|
| /* 1 if s starts with a valid "YYYY-MM-DD" calendar date. */ |
| 297 |
18 |
| static int valid_date_prefix(const char *s, size_t len) { |
| 298 |
|
| int year, month, day; |
| 299 |
18 |
▶ | if (len < 10 || s[4] != '-' || s[7] != '-') return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 300 |
18 |
▶ | if (!all_digits(s, 4) || !all_digits(s + 5, 2) || !all_digits(s + 8, 2)) |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 301 |
0 |
| return 0; |
| 302 |
18 |
| year = atoi(s); |
| 303 |
18 |
| month = atoi(s + 5); |
| 304 |
18 |
| day = atoi(s + 8); |
| 305 |
18 |
| return valid_civil_date(year, month, day); |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
|
| /* 1 if the "HH:MM:SS" part at s[11..18] is valid. Caller checked len >= 19. |
| 309 |
|
| * s[10] may be a space, 'T', or anything else — the encoder ignores it. */ |
| 310 |
1 |
| static int valid_time_part(const char *s) { |
| 311 |
|
| int hour, min, sec; |
| 312 |
1 |
▶ | if (s[13] != ':' || s[16] != ':') return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[13] != ':' || s[16] != ':') return 0; |
1 |
0 |
1 |
| if (s[13] != ':' || s[16] != ':') return 0; |
0 |
1 |
1 |
|
| 313 |
1 |
▶ | if (!all_digits(s + 11, 2) || !all_digits(s + 14, 2) || !all_digits(s + 17, 2)) |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 314 |
0 |
| return 0; |
| 315 |
1 |
| hour = atoi(s + 11); |
| 316 |
1 |
| min = atoi(s + 14); |
| 317 |
1 |
| sec = atoi(s + 17); |
| 318 |
1 |
▶ | return hour < 24 && min < 60 && sec < 60; |
Branch: 3 branches
| Branch | True | False | Total |
| return hour < 24 && min < 60 && sec < 60; |
1 |
0 |
1 |
| return hour < 24 && min < 60 && sec < 60; |
1 |
0 |
1 |
| return hour < 24 && min < 60 && sec < 60; |
1 |
0 |
1 |
|
| 319 |
|
| } |
| 320 |
|
| |
| 321 |
|
| /* Seconds-since-midnight for the time part, 0 when absent (len < 19). |
| 322 |
|
| * Caller validated via valid_time_part when present. */ |
| 323 |
2 |
| static int32_t parse_hms(const char *s, size_t len) { |
| 324 |
2 |
| int hour = 0, min = 0, sec = 0; |
| 325 |
2 |
▶ | if (len >= 19) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= 19) { |
1 |
1 |
2 |
|
| 326 |
1 |
| hour = atoi(s + 11); |
| 327 |
1 |
| min = atoi(s + 14); |
| 328 |
1 |
| sec = atoi(s + 17); |
| 329 |
|
| } |
| 330 |
2 |
| return hour * 3600 + min * 60 + sec; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
2 |
| static int32_t date_string_to_days(const char *s, size_t len) { |
| 334 |
|
| int year, month, day; |
| 335 |
2 |
▶ | if (len >= 10 && s[4] == '-' && s[7] == '-') { |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 336 |
2 |
| year = atoi(s); |
| 337 |
2 |
| month = atoi(s + 5); |
| 338 |
2 |
| day = atoi(s + 8); |
| 339 |
|
| /* civil_from_days algorithm (Howard Hinnant) */ |
| 340 |
2 |
▶ | if (month <= 2) { year--; month += 9; } else { month -= 3; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (month <= 2) { year--; month += 9; } else { month -= 3; } |
0 |
2 |
2 |
|
| 341 |
|
| { |
| 342 |
2 |
▶ | int era = (year >= 0 ? year : year - 399) / 400; |
Branch: 1 branch
| Branch | True | False | Total |
| int era = (year >= 0 ? year : year - 399) / 400; |
0 |
2 |
2 |
|
| 343 |
2 |
| unsigned yoe = (unsigned)(year - era * 400); |
| 344 |
2 |
| unsigned doy = (153 * (unsigned)month + 2) / 5 + (unsigned)day - 1; |
| 345 |
2 |
| unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy; |
| 346 |
2 |
| return (int32_t)(era * 146097 + (int)doe - 719468); |
| 347 |
|
| } |
| 348 |
|
| } |
| 349 |
|
| /* fallback: numeric value */ |
| 350 |
0 |
| return (int32_t)strtol(s, NULL, 10); |
| 351 |
|
| } |
| 352 |
|
| |
| 353 |
|
| /* --- TabSeparated parser --- */ |
| 354 |
|
| |
| 355 |
|
| /* Parse TabSeparated body into AV of AV. Handles \N -> undef, backslash escapes. */ |
| 356 |
1 |
| static AV* parse_tab_separated(const char *data, size_t len) { |
| 357 |
1 |
| AV *rows = newAV(); |
| 358 |
1 |
| const char *p = data; |
| 359 |
1 |
| const char *end = data + len; |
| 360 |
|
| const char *line_start; |
| 361 |
|
| AV *row; |
| 362 |
|
| char *buf; |
| 363 |
|
| size_t buf_len; |
| 364 |
|
| |
| 365 |
|
| /* pre-allocate scratch buffer for unescaping */ |
| 366 |
1 |
| Newx(buf, len + 1, char); |
| 367 |
|
| |
| 368 |
3 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
2 |
1 |
3 |
|
| 369 |
|
| /* No trailing-newline special case: after each row p advances |
| 370 |
|
| * past its \n, so a final \n always starts a real (possibly |
| 371 |
|
| * empty) row rather than a spurious blank line. */ |
| 372 |
2 |
| row = newAV(); |
| 373 |
2 |
| line_start = p; |
| 374 |
|
| |
| 375 |
16 |
▶ | while (p <= end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p <= end) { |
16 |
0 |
16 |
|
| 376 |
16 |
▶ | int is_end_of_line = (p == end || *p == '\n'); |
Branch: 2 branches
| Branch | True | False | Total |
| int is_end_of_line = (p == end || *p == '\n'); |
16 |
0 |
16 |
| int is_end_of_line = (p == end || *p == '\n'); |
2 |
14 |
16 |
|
| 377 |
16 |
▶ | int is_tab = (!is_end_of_line && *p == '\t'); |
Branch: 2 branches
| Branch | True | False | Total |
| int is_tab = (!is_end_of_line && *p == '\t'); |
14 |
2 |
16 |
| int is_tab = (!is_end_of_line && *p == '\t'); |
2 |
12 |
14 |
|
| 378 |
|
| |
| 379 |
18 |
▶ | if (is_end_of_line || is_tab) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (is_end_of_line || is_tab) { |
14 |
2 |
16 |
| if (is_end_of_line || is_tab) { |
2 |
12 |
14 |
|
| 380 |
4 |
| const char *field_start = line_start; |
| 381 |
4 |
| size_t field_len = p - field_start; |
| 382 |
|
| |
| 383 |
|
| /* check for \N (NULL) */ |
| 384 |
4 |
▶ | if (field_len == 2 && field_start[0] == '\\' && field_start[1] == 'N') { |
Branch: 3 branches
| Branch | True | False | Total |
| 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 |
|
| 385 |
0 |
| av_push(row, newSV(0)); |
| 386 |
|
| } else { |
| 387 |
|
| /* unescape */ |
| 388 |
4 |
| buf_len = 0; |
| 389 |
4 |
| const char *s = field_start; |
| 390 |
4 |
| const char *s_end = field_start + field_len; |
| 391 |
16 |
▶ | while (s < s_end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (s < s_end) { |
12 |
4 |
16 |
|
| 392 |
12 |
▶ | if (*s == '\\' && s + 1 < s_end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*s == '\\' && s + 1 < s_end) { |
0 |
12 |
12 |
| if (*s == '\\' && s + 1 < s_end) { |
0 |
0 |
0 |
|
| 393 |
0 |
| s++; |
| 394 |
0 |
| switch (*s) { |
| 395 |
0 |
| case 'n': buf[buf_len++] = '\n'; break; |
| 396 |
0 |
| case 't': buf[buf_len++] = '\t'; break; |
| 397 |
0 |
| case '\\': buf[buf_len++] = '\\'; break; |
| 398 |
0 |
| case '\'': buf[buf_len++] = '\''; break; |
| 399 |
0 |
| case '0': buf[buf_len++] = '\0'; break; |
| 400 |
0 |
| case 'a': buf[buf_len++] = '\a'; break; |
| 401 |
0 |
| case 'b': buf[buf_len++] = '\b'; break; |
| 402 |
0 |
| case 'f': buf[buf_len++] = '\f'; break; |
| 403 |
0 |
| case 'r': buf[buf_len++] = '\r'; break; |
| 404 |
0 |
| default: buf[buf_len++] = '\\'; buf[buf_len++] = *s; break; |
| 405 |
|
| } |
| 406 |
0 |
| s++; |
| 407 |
|
| } else { |
| 408 |
12 |
| buf[buf_len++] = *s++; |
| 409 |
|
| } |
| 410 |
|
| } |
| 411 |
4 |
| av_push(row, newSVpvn(buf, buf_len)); |
| 412 |
|
| } |
| 413 |
|
| |
| 414 |
4 |
▶ | if (is_tab) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_tab) { |
2 |
2 |
4 |
|
| 415 |
2 |
| p++; |
| 416 |
2 |
| line_start = p; |
| 417 |
|
| } else { |
| 418 |
2 |
▶ | if (p < end) p++; /* skip \n */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (p < end) p++; /* skip \n */ |
2 |
0 |
2 |
|
| 419 |
2 |
| break; |
| 420 |
|
| } |
| 421 |
|
| } else { |
| 422 |
12 |
| p++; |
| 423 |
|
| } |
| 424 |
|
| } |
| 425 |
2 |
| av_push(rows, newRV_noinc((SV*)row)); |
| 426 |
|
| } |
| 427 |
|
| |
| 428 |
1 |
| Safefree(buf); |
| 429 |
1 |
| return rows; |
| 430 |
|
| } |
| 431 |
|
| |