| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| #include <string.h> |
| 7 |
|
| #include <stdint.h> |
| 8 |
|
| |
| 9 |
|
| #include "types.h" |
| 10 |
|
| #include "decimal.h" |
| 11 |
|
| #include "datetime.h" |
| 12 |
|
| #include "json_kind.h" |
| 13 |
|
| #include "decode.h" |
| 14 |
|
| |
| 15 |
|
| /* Not defined by every perl we claim to build against. */ |
| 16 |
|
| #ifndef SSize_t_MAX |
| 17 |
|
| # define SSize_t_MAX ((SSize_t)(~(Size_t)0 >> 1)) |
| 18 |
|
| #endif |
| 19 |
|
| |
| 20 |
|
| |
| 21 |
|
| /* ===== DECODER ============================================================ |
| 22 |
|
| * Symmetric counterpart to encode_column. Reads raw Native bytes through |
| 23 |
|
| * a (cursor, end) pair, recursively building SVs. parse_type() returns |
| 24 |
|
| * the same TypeInfo* used on the encode side, so the type tree is shared. */ |
| 25 |
|
| |
| 26 |
|
| /* Subtraction form: the more obvious `(*p) + (needed) > end` form |
| 27 |
|
| * overflows the pointer when `needed` is attacker-controlled via a |
| 28 |
|
| * crafted varint (CH varints can encode up to ~2^63). All call sites |
| 29 |
|
| * maintain the invariant `*p <= end`, so `end - *p` is a safe pointer |
| 30 |
|
| * difference yielding a non-negative `ptrdiff_t` we can compare against |
| 31 |
|
| * `needed` as a UV. */ |
| 32 |
|
| #define DEC_NEED(needed) \ |
| 33 |
|
| do { \ |
| 34 |
|
| if ((UV)(needed) > (UV)(end - (*p))) \ |
| 35 |
|
| croak("decode: buffer truncated (need %lu more bytes)", \ |
| 36 |
|
| (unsigned long)((UV)(needed) - (UV)(end - (*p)))); \ |
| 37 |
|
| } while (0) |
| 38 |
|
| |
| 39 |
|
| /* Read a little-endian multi-byte unsigned integer from a byte buffer. |
| 40 |
|
| * Endianness-portable replacement for `memcpy(&v, ptr, N)`, which would |
| 41 |
|
| * read big-endian values on a BE host and silently misdecode the wire |
| 42 |
|
| * (CH Native is LE everywhere). For signed and floating-point reads, |
| 43 |
|
| * the caller bit-casts via memcpy from the unsigned result. */ |
| 44 |
41 |
| static inline uint16_t dec_le16(const unsigned char *b) { |
| 45 |
41 |
| return (uint16_t)b[0] | ((uint16_t)b[1] << 8); |
| 46 |
|
| } |
| 47 |
41054 |
| static inline uint32_t dec_le32(const unsigned char *b) { |
| 48 |
41054 |
| return (uint32_t)b[0] |
| 49 |
41054 |
| | ((uint32_t)b[1] << 8) |
| 50 |
41054 |
| | ((uint32_t)b[2] << 16) |
| 51 |
41054 |
| | ((uint32_t)b[3] << 24); |
| 52 |
|
| } |
| 53 |
33874 |
| static inline uint64_t dec_le64(const unsigned char *b) { |
| 54 |
33874 |
| return (uint64_t)b[0] |
| 55 |
33874 |
| | ((uint64_t)b[1] << 8) |
| 56 |
33874 |
| | ((uint64_t)b[2] << 16) |
| 57 |
33874 |
| | ((uint64_t)b[3] << 24) |
| 58 |
33874 |
| | ((uint64_t)b[4] << 32) |
| 59 |
33874 |
| | ((uint64_t)b[5] << 40) |
| 60 |
33874 |
| | ((uint64_t)b[6] << 48) |
| 61 |
33874 |
| | ((uint64_t)b[7] << 56); |
| 62 |
|
| } |
| 63 |
|
| |
| 64 |
5164530 |
| UV dec_varint(pTHX_ const unsigned char **p, const unsigned char *end) { |
| 65 |
5164530 |
▶ | DEC_NEED(1); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(1); |
25014 |
5139516 |
5164530 |
|
| 66 |
5139516 |
| unsigned char b = *(*p)++; |
| 67 |
5139516 |
| UV v = (UV)(b & 0x7f); |
| 68 |
5139516 |
▶ | if (!(b & 0x80)) return v; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(b & 0x80)) return v; |
5114179 |
25337 |
5139516 |
|
| 69 |
25337 |
| int shift = 7; |
| 70 |
|
| while (1) { |
| 71 |
25340 |
▶ | DEC_NEED(1); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(1); |
2 |
25338 |
25340 |
|
| 72 |
25338 |
| b = *(*p)++; |
| 73 |
25338 |
▶ | if (shift == 63 && (b & 0x7e)) croak("decode: varint exceeds 64 bits"); |
Branch: 2 branches
| Branch | True | False | Total |
| if (shift == 63 && (b & 0x7e)) croak("decode: varint exceeds 64 bits"); |
0 |
25338 |
25338 |
| if (shift == 63 && (b & 0x7e)) croak("decode: varint exceeds 64 bits"); |
0 |
0 |
0 |
|
| 74 |
25338 |
| v |= ((UV)(b & 0x7f)) << shift; |
| 75 |
25338 |
▶ | if (!(b & 0x80)) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(b & 0x80)) break; |
25335 |
3 |
25338 |
|
| 76 |
3 |
| shift += 7; |
| 77 |
3 |
▶ | if (shift >= 64) croak("decode: varint exceeds 64 bits"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (shift >= 64) croak("decode: varint exceeds 64 bits"); |
0 |
3 |
3 |
|
| 78 |
|
| } |
| 79 |
25335 |
| return v; |
| 80 |
|
| } |
| 81 |
|
| |
| 82 |
5111548 |
| void dec_lenpfx_string(pTHX_ const unsigned char **p, |
| 83 |
|
| const unsigned char *end, |
| 84 |
|
| const char **out_s, STRLEN *out_len) { |
| 85 |
5111548 |
| UV len = dec_varint(aTHX_ p, end); |
| 86 |
5086535 |
▶ | DEC_NEED(len); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(len); |
93 |
5086442 |
5086535 |
|
| 87 |
5086442 |
| *out_s = (const char *)(*p); |
| 88 |
5086442 |
| *out_len = (STRLEN)len; |
| 89 |
5086442 |
| *p += len; |
| 90 |
5086442 |
| } |
| 91 |
|
| |
| 92 |
|
| /* Shared prologue for decode_block / decode_block_rows: validate the |
| 93 |
|
| * input SV, position the cursor at the requested offset, read the |
| 94 |
|
| * block header (ncols + nrows), and run bounds checks. `fname` is |
| 95 |
|
| * embedded in croak messages so each XSUB reports its own name. |
| 96 |
|
| * Returns ncols/nrows by out-param; the cursor pair (p, end) is set |
| 97 |
|
| * up so the caller can resume column-by-column decoding. */ |
| 98 |
25704 |
| void decode_block_prologue(pTHX_ SV *bytes, UV start_offset, |
| 99 |
|
| const char *fname, |
| 100 |
|
| const unsigned char **out_start, |
| 101 |
|
| const unsigned char **out_p, |
| 102 |
|
| const unsigned char **out_end, |
| 103 |
|
| UV *out_ncols, UV *out_nrows) { |
| 104 |
|
| /* Materialize lvalue / magical SVs (e.g. the PVLV returned by 2-arg |
| 105 |
|
| * substr) before inspecting. SvOK on a fresh substr-LV returns |
| 106 |
|
| * false until SvGETMAGIC has run; SvPVbyte itself triggers the |
| 107 |
|
| * magic, so we just go straight to it and let buf_len = 0 cover |
| 108 |
|
| * the genuine empty-bytes case. Reject only true undef. */ |
| 109 |
25704 |
| SvGETMAGIC(bytes); |
| 110 |
25704 |
▶ | if (!SvOK(bytes)) croak("%s: bytes argument is undef", fname); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(bytes)) croak("%s: bytes argument is undef", fname); |
0 |
25704 |
25704 |
|
| 111 |
|
| STRLEN buf_len; |
| 112 |
25704 |
| const char *buf = SvPVbyte(bytes, buf_len); |
| 113 |
25704 |
▶ | if (start_offset > buf_len) |
Branch: 1 branch
| Branch | True | False | Total |
| if (start_offset > buf_len) |
1 |
25703 |
25704 |
|
| 114 |
1 |
| croak("%s: offset %lu past end of buffer (%lu bytes)", |
| 115 |
|
| fname, (unsigned long)start_offset, (unsigned long)buf_len); |
| 116 |
25703 |
| const unsigned char *p = (const unsigned char *)buf + start_offset; |
| 117 |
25703 |
| const unsigned char *start = p; |
| 118 |
25703 |
| const unsigned char *end = (const unsigned char *)buf + buf_len; |
| 119 |
|
| |
| 120 |
25703 |
| UV ncols = dec_varint(aTHX_ &p, end); |
| 121 |
25701 |
| UV nrows = dec_varint(aTHX_ &p, end); |
| 122 |
|
| |
| 123 |
|
| /* Defensive bounds: ncols and nrows from the wire could be |
| 124 |
|
| * arbitrarily large in a malicious or corrupted block. Each column |
| 125 |
|
| * needs at least 2 bytes of name+type header; each row of the |
| 126 |
|
| * smallest type takes at least 1 byte. Reject obviously-impossible |
| 127 |
|
| * counts up front so we never allocate gigabytes for fuzz input. */ |
| 128 |
25700 |
▶ | if (ncols > (UV)(end - p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ncols > (UV)(end - p)) |
1 |
25699 |
25700 |
|
| 129 |
1 |
| croak("%s: ncols=%lu exceeds remaining buffer (%lu bytes)", |
| 130 |
|
| fname, (unsigned long)ncols, (unsigned long)(end - p)); |
| 131 |
25699 |
| UV remaining_after_headers = (UV)(end - p); |
| 132 |
|
| /* No ncols==0 check: this encoder's own zero-column output is a bare |
| 133 |
|
| * "ncols=0 nrows=N" header and must keep decoding. The amplification |
| 134 |
|
| * that would justify one is handled in decode_block_rows instead. */ |
| 135 |
25699 |
▶ | if (nrows > remaining_after_headers && nrows > 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (nrows > remaining_after_headers && nrows > 0) |
3 |
25696 |
25699 |
| if (nrows > remaining_after_headers && nrows > 0) |
3 |
0 |
3 |
|
| 136 |
3 |
| croak("%s: nrows=%lu exceeds remaining buffer (%lu bytes)", |
| 137 |
|
| fname, (unsigned long)nrows, |
| 138 |
|
| (unsigned long)remaining_after_headers); |
| 139 |
|
| |
| 140 |
25696 |
| *out_start = start; |
| 141 |
25696 |
| *out_p = p; |
| 142 |
25696 |
| *out_end = end; |
| 143 |
25696 |
| *out_ncols = ncols; |
| 144 |
25696 |
| *out_nrows = nrows; |
| 145 |
25696 |
| } |
| 146 |
|
| |
| 147 |
|
| /* Build a JSON-style boolean SV: bless(\(b ? 1 : 0), 'JSON::PP::Boolean'). |
| 148 |
|
| * Used when decoding JSON/Dynamic Bool variant slots so that re-encoding |
| 149 |
|
| * the round-tripped value picks the same Bool wire variant instead of |
| 150 |
|
| * widening it to Int64 (which a naked newSViv(0|1) would trigger). The |
| 151 |
|
| * blessed package matches what json_pkg_is_bool() recognizes on the |
| 152 |
|
| * encode side. */ |
| 153 |
245 |
| static SV *make_json_bool_sv(pTHX_ int b) { |
| 154 |
245 |
| SV *inner = newSViv(b ? 1 : 0); |
| 155 |
245 |
| SV *rv = newRV_noinc(inner); |
| 156 |
245 |
| sv_bless(rv, gv_stashpv("JSON::PP::Boolean", GV_ADD)); |
| 157 |
245 |
| return rv; |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
|
| /* Decode one Dynamic/JSON variant wire slot into `sub`. `kind` is a |
| 161 |
|
| * JsonValueKind (or -1 for SharedVariant, which we read as String). |
| 162 |
|
| * Shared by both T_JSON and T_DYNAMIC decode paths; `ctx` is "JSON" or |
| 163 |
|
| * "Dynamic" for diagnostic context. The caller has already extended |
| 164 |
|
| * `sub` and mortalized it. */ |
| 165 |
1408 |
| static void decode_dynamic_variant_slot(pTHX_ const unsigned char **p, |
| 166 |
|
| const unsigned char *end, |
| 167 |
|
| AV *sub, int kind, SSize_t nv_rows, |
| 168 |
|
| const char *ctx) { |
| 169 |
|
| SSize_t k; |
| 170 |
1408 |
▶ | if (nv_rows && (kind == JV_BOOL || kind == JV_ARRAY_BOOL) |
Branch: 3 branches
| Branch | True | False | Total |
| if (nv_rows && (kind == JV_BOOL || kind == JV_ARRAY_BOOL) |
714 |
694 |
1408 |
| if (nv_rows && (kind == JV_BOOL || kind == JV_ARRAY_BOOL) |
625 |
89 |
714 |
| if (nv_rows && (kind == JV_BOOL || kind == JV_ARRAY_BOOL) |
58 |
567 |
625 |
|
| 171 |
147 |
▶ | && !get_cv("JSON::PP::false", 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| && !get_cv("JSON::PP::false", 0)) |
0 |
147 |
147 |
|
| 172 |
0 |
| load_module(PERL_LOADMOD_NOIMPORT, newSVpvs("JSON::PP"), NULL); |
| 173 |
1408 |
▶ | if (kind < 0 || kind == JV_STRING) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (kind < 0 || kind == JV_STRING) { |
714 |
694 |
1408 |
| if (kind < 0 || kind == JV_STRING) { |
90 |
624 |
714 |
|
| 174 |
|
| /* SharedVariant (-1) is a String column on the wire; same path. */ |
| 175 |
878 |
▶ | for (k = 0; k < nv_rows; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nv_rows; k++) { |
94 |
784 |
878 |
|
| 176 |
|
| const char *vs; STRLEN vl; |
| 177 |
94 |
| dec_lenpfx_string(aTHX_ p, end, &vs, &vl); |
| 178 |
94 |
| av_store(sub, k, newSVpvn(vs, vl)); |
| 179 |
|
| } |
| 180 |
784 |
| return; |
| 181 |
|
| } |
| 182 |
624 |
▶ | if (kind == JV_INT64) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind == JV_INT64) { |
174 |
450 |
624 |
|
| 183 |
|
| /* Division-form bounds check avoids overflow when nv_rows is |
| 184 |
|
| * attacker-controlled (variant disc bytes determine it). */ |
| 185 |
174 |
▶ | if ((UV)nv_rows > (UV)(end - *p) / 8) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)nv_rows > (UV)(end - *p) / 8) |
0 |
174 |
174 |
|
| 186 |
0 |
| croak("decode: buffer truncated (need %lu more bytes)", |
| 187 |
|
| (unsigned long)((UV)nv_rows * 8 - (UV)(end - *p))); |
| 188 |
352 |
▶ | for (k = 0; k < nv_rows; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nv_rows; k++) { |
178 |
174 |
352 |
|
| 189 |
178 |
| uint64_t u = dec_le64(*p); *p += 8; |
| 190 |
178 |
| int64_t v; memcpy(&v, &u, 8); |
| 191 |
178 |
| av_store(sub, k, newSViv((IV)v)); |
| 192 |
|
| } |
| 193 |
174 |
| return; |
| 194 |
|
| } |
| 195 |
450 |
▶ | if (kind == JV_FLOAT64) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind == JV_FLOAT64) { |
70 |
380 |
450 |
|
| 196 |
70 |
▶ | if ((UV)nv_rows > (UV)(end - *p) / 8) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)nv_rows > (UV)(end - *p) / 8) |
0 |
70 |
70 |
|
| 197 |
0 |
| croak("decode: buffer truncated (need %lu more bytes)", |
| 198 |
|
| (unsigned long)((UV)nv_rows * 8 - (UV)(end - *p))); |
| 199 |
140 |
▶ | for (k = 0; k < nv_rows; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nv_rows; k++) { |
70 |
70 |
140 |
|
| 200 |
70 |
| uint64_t u = dec_le64(*p); *p += 8; |
| 201 |
70 |
| double v; memcpy(&v, &u, 8); |
| 202 |
70 |
| av_store(sub, k, newSVnv(v)); |
| 203 |
|
| } |
| 204 |
70 |
| return; |
| 205 |
|
| } |
| 206 |
380 |
▶ | if (kind == JV_BOOL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind == JV_BOOL) { |
89 |
291 |
380 |
|
| 207 |
89 |
▶ | if ((UV)nv_rows > (UV)(end - *p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)nv_rows > (UV)(end - *p)) |
0 |
89 |
89 |
|
| 208 |
0 |
| croak("decode: buffer truncated (need %lu more bytes)", |
| 209 |
|
| (unsigned long)((UV)nv_rows - (UV)(end - *p))); |
| 210 |
181 |
▶ | for (k = 0; k < nv_rows; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nv_rows; k++) { |
92 |
89 |
181 |
|
| 211 |
92 |
| unsigned char b = *(*p)++; |
| 212 |
92 |
| av_store(sub, k, make_json_bool_sv(aTHX_ b)); |
| 213 |
|
| } |
| 214 |
89 |
| return; |
| 215 |
|
| } |
| 216 |
291 |
▶ | if (kind == JV_ARRAY_BOOL || kind == JV_ARRAY_FLOAT64 |
Branch: 2 branches
| Branch | True | False | Total |
| if (kind == JV_ARRAY_BOOL || kind == JV_ARRAY_FLOAT64 |
233 |
58 |
291 |
| if (kind == JV_ARRAY_BOOL || kind == JV_ARRAY_FLOAT64 |
172 |
61 |
233 |
|
| 217 |
172 |
▶ | || kind == JV_ARRAY_INT64 || kind == JV_ARRAY_STRING) { |
Branch: 2 branches
| Branch | True | False | Total |
| || kind == JV_ARRAY_INT64 || kind == JV_ARRAY_STRING) { |
53 |
119 |
172 |
| || kind == JV_ARRAY_INT64 || kind == JV_ARRAY_STRING) { |
53 |
0 |
53 |
|
| 218 |
|
| /* Array variant column: N UInt64 offsets, then offsets[N-1] |
| 219 |
|
| * inner-type elements concatenated. */ |
| 220 |
291 |
▶ | if (nv_rows == 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (nv_rows == 0) return; |
0 |
291 |
291 |
|
| 221 |
291 |
▶ | if ((UV)nv_rows > (UV)(end - *p) / 8) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)nv_rows > (UV)(end - *p) / 8) |
0 |
291 |
291 |
|
| 222 |
0 |
| croak("decode: buffer truncated (need %lu more bytes)", |
| 223 |
|
| (unsigned long)((UV)nv_rows * 8 - (UV)(end - *p))); |
| 224 |
|
| uint64_t *offs; |
| 225 |
291 |
▶ | Newx(offs, nv_rows, uint64_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(offs, nv_rows, uint64_t); |
0 |
291 |
291 |
|
| 226 |
291 |
| SAVEFREEPV(offs); |
| 227 |
291 |
| uint64_t prev_o = 0; |
| 228 |
583 |
▶ | for (k = 0; k < nv_rows; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nv_rows; k++) { |
292 |
291 |
583 |
|
| 229 |
292 |
| offs[k] = dec_le64(*p); *p += 8; |
| 230 |
|
| /* Per-offset overflow + monotonicity; protects later casts to |
| 231 |
|
| * SSize_t (e.g. av_extend) from negative-wrap. */ |
| 232 |
292 |
▶ | if (offs[k] > (uint64_t)SSize_t_MAX || offs[k] < prev_o) |
Branch: 2 branches
| Branch | True | False | Total |
| if (offs[k] > (uint64_t)SSize_t_MAX || offs[k] < prev_o) |
292 |
0 |
292 |
| if (offs[k] > (uint64_t)SSize_t_MAX || offs[k] < prev_o) |
0 |
292 |
292 |
|
| 233 |
0 |
| croak("decode JSON: Array variant offset[%ld]=%lu invalid " |
| 234 |
|
| "(prev=%lu)", |
| 235 |
|
| (long)k, (unsigned long)offs[k], (unsigned long)prev_o); |
| 236 |
292 |
| prev_o = offs[k]; |
| 237 |
|
| } |
| 238 |
291 |
| uint64_t total = offs[nv_rows - 1]; |
| 239 |
|
| /* Defensive: total elements must fit into the remaining buffer |
| 240 |
|
| * (1+ bytes per element minimum). Catches corrupted offset |
| 241 |
|
| * lists before they trigger huge AV allocations. */ |
| 242 |
291 |
▶ | if (total > (uint64_t)(end - *p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (total > (uint64_t)(end - *p)) |
0 |
291 |
291 |
|
| 243 |
0 |
| croak("decode JSON: Array variant total=%lu exceeds remaining " |
| 244 |
|
| "buffer (%lu bytes)", |
| 245 |
|
| (unsigned long)total, (unsigned long)(end - *p)); |
| 246 |
583 |
▶ | for (k = 0; k < nv_rows; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < nv_rows; k++) |
292 |
291 |
583 |
|
| 247 |
292 |
| av_store(sub, k, newRV_noinc((SV*)newAV())); |
| 248 |
|
| |
| 249 |
|
| /* Inner cursor walks through elements while row_idx advances |
| 250 |
|
| * each time we hit the cumulative offset boundary. */ |
| 251 |
291 |
| uint64_t prev = 0; |
| 252 |
291 |
| SSize_t row_idx = 0; |
| 253 |
291 |
| AV *inner = (AV*)SvRV(*av_fetch(sub, 0, 0)); |
| 254 |
291 |
▶ | if (offs[0] > 0) av_extend(inner, (SSize_t)offs[0] - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (offs[0] > 0) av_extend(inner, (SSize_t)offs[0] - 1); |
235 |
56 |
291 |
|
| 255 |
291 |
| SSize_t inner_cursor = 0; |
| 256 |
|
| |
| 257 |
291 |
▶ | STRLEN per_elem = (kind == JV_ARRAY_BOOL) ? 1 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN per_elem = (kind == JV_ARRAY_BOOL) ? 1 : 8; |
58 |
233 |
291 |
|
| 258 |
|
| /* `total` is attacker-controlled (sum of wire offsets); use the |
| 259 |
|
| * division-form check to avoid the multiplication overflowing. */ |
| 260 |
291 |
▶ | if (kind != JV_ARRAY_STRING |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind != JV_ARRAY_STRING |
238 |
53 |
291 |
|
| 261 |
238 |
▶ | && total > (uint64_t)(end - *p) / per_elem) |
Branch: 1 branch
| Branch | True | False | Total |
| && total > (uint64_t)(end - *p) / per_elem) |
0 |
238 |
238 |
|
| 262 |
0 |
| croak("decode: buffer truncated (need %lu more bytes)", |
| 263 |
|
| (unsigned long)(per_elem * total - (uint64_t)(end - *p))); |
| 264 |
|
| |
| 265 |
|
| uint64_t i; |
| 266 |
897 |
▶ | for (i = 0; i < total; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < total; i++) { |
606 |
291 |
897 |
|
| 267 |
607 |
▶ | while (inner_cursor >= (SSize_t)(offs[row_idx] - prev)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (inner_cursor >= (SSize_t)(offs[row_idx] - prev)) { |
1 |
606 |
607 |
|
| 268 |
1 |
| prev = offs[row_idx]; |
| 269 |
1 |
| row_idx++; |
| 270 |
|
| /* If a corrupted offset list has trailing zero-length |
| 271 |
|
| * rows that the outer total didn't cover, row_idx |
| 272 |
|
| * could walk past the populated entries. Bail before |
| 273 |
|
| * av_fetch returns NULL and we deref it. */ |
| 274 |
1 |
▶ | if (row_idx >= nv_rows) |
Branch: 1 branch
| Branch | True | False | Total |
| if (row_idx >= nv_rows) |
0 |
1 |
1 |
|
| 275 |
0 |
| croak("decode: array variant offsets advanced past " |
| 276 |
|
| "nv_rows=%ld (corrupted block)", (long)nv_rows); |
| 277 |
1 |
| inner_cursor = 0; |
| 278 |
1 |
| inner = (AV*)SvRV(*av_fetch(sub, row_idx, 0)); |
| 279 |
1 |
| uint64_t n2 = offs[row_idx] - prev; |
| 280 |
1 |
▶ | if (n2 > 0) av_extend(inner, (SSize_t)n2 - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n2 > 0) av_extend(inner, (SSize_t)n2 - 1); |
1 |
0 |
1 |
|
| 281 |
|
| } |
| 282 |
|
| SV *ev; |
| 283 |
606 |
| switch (kind) { |
| 284 |
153 |
| case JV_ARRAY_BOOL: { |
| 285 |
153 |
| unsigned char b = *(*p)++; |
| 286 |
153 |
| ev = make_json_bool_sv(aTHX_ b); |
| 287 |
153 |
| break; |
| 288 |
|
| } |
| 289 |
161 |
| case JV_ARRAY_INT64: { |
| 290 |
161 |
| uint64_t u = dec_le64(*p); *p += 8; |
| 291 |
161 |
| int64_t v; memcpy(&v, &u, 8); |
| 292 |
161 |
| ev = newSViv((IV)v); |
| 293 |
161 |
| break; |
| 294 |
|
| } |
| 295 |
156 |
| case JV_ARRAY_FLOAT64: { |
| 296 |
156 |
| uint64_t u = dec_le64(*p); *p += 8; |
| 297 |
156 |
| double v; memcpy(&v, &u, 8); |
| 298 |
156 |
| ev = newSVnv(v); |
| 299 |
156 |
| break; |
| 300 |
|
| } |
| 301 |
136 |
| case JV_ARRAY_STRING: { |
| 302 |
|
| const char *vs; STRLEN vl; |
| 303 |
136 |
| dec_lenpfx_string(aTHX_ p, end, &vs, &vl); |
| 304 |
136 |
| ev = newSVpvn(vs, vl); |
| 305 |
136 |
| break; |
| 306 |
|
| } |
| 307 |
0 |
| default: ev = newSV(0); /* unreachable */ |
| 308 |
|
| } |
| 309 |
606 |
| av_store(inner, inner_cursor++, ev); |
| 310 |
|
| } |
| 311 |
291 |
| return; |
| 312 |
|
| } |
| 313 |
0 |
| croak("decode %s: internal: unknown kind %d", ctx, kind); |
| 314 |
|
| } |
| 315 |
|
| |
| 316 |
|
| /* Helpers that bulk-read same-size scalars into the array, since the per- |
| 317 |
|
| * row dispatch overhead of unpack-style XS loops dwarfs the data read. */ |
| 318 |
|
| #define DEC_SCALAR_LOOP(av, nrows, sv_expr) do { \ |
| 319 |
|
| SSize_t r; \ |
| 320 |
|
| for (r = 0; r < (nrows); r++) av_store(av, r, (sv_expr)); \ |
| 321 |
|
| } while (0) |
| 322 |
|
| |
| 323 |
|
| /* What decode_column owns while a column is under construction, freed by |
| 324 |
|
| * SAVEDESTRUCTOR_X if a nested read croaks. Callers drive those croaks as |
| 325 |
|
| * ordinary control flow while awaiting more bytes, so the error path is a |
| 326 |
|
| * hot one. Fields are cleared as ownership moves on, leaving the |
| 327 |
|
| * destructor a no-op on success. |
| 328 |
|
| * |
| 329 |
|
| * `vec` is owned outright rather than SAVEFREEPV'd: the savestack unwinds |
| 330 |
|
| * LIFO, so a later SAVEFREEPV would free the array before this runs. */ |
| 331 |
|
| typedef struct { |
| 332 |
|
| AV *av; /* the column AV being built */ |
| 333 |
|
| SV *inner; /* one nested column RV (Array / Nullable / LC / JSON) */ |
| 334 |
|
| SV **vec; /* nested column RVs (Tuple / Variant), owned */ |
| 335 |
|
| int vec_n; /* how many vec[] slots are populated */ |
| 336 |
|
| } DecGuard; |
| 337 |
|
| |
| 338 |
26954 |
| static void cleanup_dec_guard(pTHX_ void *pv) { |
| 339 |
26954 |
| DecGuard *g = (DecGuard *)pv; |
| 340 |
26954 |
▶ | if (g->vec) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (g->vec) { |
4 |
26950 |
26954 |
|
| 341 |
|
| int i; |
| 342 |
6 |
▶ | for (i = 0; i < g->vec_n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < g->vec_n; i++) |
2 |
4 |
6 |
|
| 343 |
2 |
▶ | if (g->vec[i]) SvREFCNT_dec(g->vec[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (g->vec[i]) SvREFCNT_dec(g->vec[i]); |
2 |
0 |
2 |
|
| 344 |
4 |
| Safefree(g->vec); |
| 345 |
4 |
| g->vec = NULL; |
| 346 |
4 |
| g->vec_n = 0; |
| 347 |
|
| } |
| 348 |
26954 |
▶ | if (g->inner) { SvREFCNT_dec(g->inner); g->inner = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (g->inner) { SvREFCNT_dec(g->inner); g->inner = NULL; } |
0 |
26954 |
26954 |
|
| 349 |
26954 |
▶ | if (g->av) { SvREFCNT_dec((SV *)g->av); g->av = NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (g->av) { SvREFCNT_dec((SV *)g->av); g->av = NULL; } |
25310 |
1644 |
26954 |
|
| 350 |
26954 |
| } |
| 351 |
|
| |
| 352 |
|
| typedef struct { |
| 353 |
|
| UV num_paths; |
| 354 |
|
| char **paths; |
| 355 |
|
| STRLEN *path_lens; |
| 356 |
|
| int **path_kind_list; |
| 357 |
|
| int *path_kind_count; |
| 358 |
|
| UV ntypes; |
| 359 |
|
| int *kinds_in_order; |
| 360 |
|
| } DecodePrefix; |
| 361 |
|
| |
| 362 |
|
| static SV *decode_column_data(pTHX_ const unsigned char **p, |
| 363 |
|
| const unsigned char *end, |
| 364 |
|
| TypeInfo *t, SSize_t nrows); |
| 365 |
|
| |
| 366 |
162 |
| static void decode_prefix(pTHX_ const unsigned char **p, |
| 367 |
|
| const unsigned char *end, TypeInfo *t) { |
| 368 |
|
| int i; |
| 369 |
162 |
▶ | if (!t->has_prefix) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t->has_prefix) return; |
19 |
143 |
162 |
|
| 370 |
143 |
| switch (t->code) { |
| 371 |
17 |
| case T_LOWCARDINALITY: |
| 372 |
17 |
▶ | DEC_NEED(8); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(8); |
0 |
17 |
17 |
|
| 373 |
17 |
▶ | if (dec_le64(*p) != 1) croak("decode: LowCardinality version != 1"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dec_le64(*p) != 1) croak("decode: LowCardinality version != 1"); |
0 |
17 |
17 |
|
| 374 |
17 |
| *p += 8; |
| 375 |
17 |
| return; |
| 376 |
8 |
| case T_VARIANT: |
| 377 |
8 |
▶ | DEC_NEED(8); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(8); |
0 |
8 |
8 |
|
| 378 |
8 |
▶ | if (dec_le64(*p) != 0) croak("decode: Variant mode != 0"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (dec_le64(*p) != 0) croak("decode: Variant mode != 0"); |
0 |
8 |
8 |
|
| 379 |
8 |
| *p += 8; |
| 380 |
24 |
▶ | for (i = 0; i < t->tuple_len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->tuple_len; i++) |
16 |
8 |
24 |
|
| 381 |
16 |
| decode_prefix(aTHX_ p, end, t->tuple[t->variant_wire_to_decl[i]]); |
| 382 |
8 |
| return; |
| 383 |
91 |
| case T_JSON: { |
| 384 |
|
| DecodePrefix *state; |
| 385 |
91 |
| Newxz(state, 1, DecodePrefix); |
| 386 |
91 |
| SAVEFREEPV(state); |
| 387 |
91 |
| t->decode_state = state; |
| 388 |
|
| |
| 389 |
|
| /* Object structure prefix. Versions: V1=0, V2=2, V3=4. */ |
| 390 |
91 |
▶ | DEC_NEED(8); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(8); |
0 |
91 |
91 |
|
| 391 |
91 |
| uint64_t obj_ver = dec_le64(*p); *p += 8; |
| 392 |
91 |
▶ | if (obj_ver != 0 && obj_ver != 2 && obj_ver != 4) |
Branch: 3 branches
| Branch | True | False | Total |
| if (obj_ver != 0 && obj_ver != 2 && obj_ver != 4) |
0 |
91 |
91 |
| if (obj_ver != 0 && obj_ver != 2 && obj_ver != 4) |
0 |
0 |
0 |
| if (obj_ver != 0 && obj_ver != 2 && obj_ver != 4) |
0 |
0 |
0 |
|
| 393 |
0 |
| croak("decode JSON: unsupported Object version %lu " |
| 394 |
|
| "(known: 0, 2, 4); upgrade ClickHouse::Encoder", |
| 395 |
|
| (unsigned long)obj_ver); |
| 396 |
91 |
▶ | if (obj_ver == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (obj_ver == 0) { |
91 |
0 |
91 |
|
| 397 |
|
| /* V1: extra max_dynamic_paths varint before count. */ |
| 398 |
91 |
| (void)dec_varint(aTHX_ p, end); |
| 399 |
|
| } |
| 400 |
91 |
| UV num_paths = dec_varint(aTHX_ p, end); |
| 401 |
|
| /* Defensive: each path takes at least 2 bytes (1-byte varint |
| 402 |
|
| * length prefix + 1-byte name). Reject obviously-impossible |
| 403 |
|
| * counts before allocating arrays sized by num_paths. */ |
| 404 |
91 |
▶ | if (num_paths > (UV)(end - *p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_paths > (UV)(end - *p)) |
0 |
91 |
91 |
|
| 405 |
0 |
| croak("decode JSON: num_paths=%lu exceeds remaining buffer " |
| 406 |
|
| "(%lu bytes)", |
| 407 |
|
| (unsigned long)num_paths, (unsigned long)(end - *p)); |
| 408 |
|
| |
| 409 |
91 |
| char **paths = NULL; |
| 410 |
91 |
| STRLEN *path_lens = NULL; |
| 411 |
91 |
▶ | if (num_paths > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_paths > 0) { |
77 |
14 |
91 |
|
| 412 |
77 |
▶ | Newx(paths, num_paths, char*); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(paths, num_paths, char*); |
0 |
77 |
77 |
|
| 413 |
77 |
| SAVEFREEPV(paths); |
| 414 |
77 |
▶ | Newx(path_lens, num_paths, STRLEN); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(path_lens, num_paths, STRLEN); |
0 |
77 |
77 |
|
| 415 |
77 |
| SAVEFREEPV(path_lens); |
| 416 |
|
| } |
| 417 |
|
| UV pi; |
| 418 |
777 |
▶ | for (pi = 0; pi < num_paths; pi++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (pi = 0; pi < num_paths; pi++) { |
687 |
90 |
777 |
|
| 419 |
|
| const char *ps; |
| 420 |
|
| STRLEN pl; |
| 421 |
687 |
| dec_lenpfx_string(aTHX_ p, end, &ps, &pl); |
| 422 |
686 |
| paths[pi] = (char*)ps; /* aliases input buffer */ |
| 423 |
686 |
| path_lens[pi] = pl; |
| 424 |
|
| } |
| 425 |
90 |
▶ | if (obj_ver == 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (obj_ver == 4) { |
0 |
90 |
90 |
|
| 426 |
|
| /* V3 adds shared_data_serialization_version, and a |
| 427 |
|
| * buckets count when that version is MAP_WITH_BUCKETS |
| 428 |
|
| * (=1) or ADVANCED (=2). Native format with statistics |
| 429 |
|
| * disabled (the default) skips the stats afterwards. */ |
| 430 |
0 |
| UV shared_ver = dec_varint(aTHX_ p, end); |
| 431 |
0 |
▶ | if (shared_ver == 1 || shared_ver == 2) |
Branch: 2 branches
| Branch | True | False | Total |
| if (shared_ver == 1 || shared_ver == 2) |
0 |
0 |
0 |
| if (shared_ver == 1 || shared_ver == 2) |
0 |
0 |
0 |
|
| 432 |
0 |
| (void)dec_varint(aTHX_ p, end); |
| 433 |
|
| } |
| 434 |
|
| |
| 435 |
|
| /* Per-path Dynamic prefix: collect type-name lists. */ |
| 436 |
90 |
| int **path_kind_list = NULL; /* path_kind_list[p][i] = JsonValueKind */ |
| 437 |
90 |
| int *path_kind_count = NULL; |
| 438 |
90 |
▶ | if (num_paths > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_paths > 0) { |
76 |
14 |
90 |
|
| 439 |
76 |
▶ | Newxz(path_kind_list, num_paths, int*); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(path_kind_list, num_paths, int*); |
0 |
76 |
76 |
|
| 440 |
76 |
| SAVEFREEPV(path_kind_list); |
| 441 |
76 |
▶ | Newxz(path_kind_count, num_paths, int); |
Branch: 1 branch
| Branch | True | False | Total |
| Newxz(path_kind_count, num_paths, int); |
0 |
76 |
76 |
|
| 442 |
76 |
| SAVEFREEPV(path_kind_count); |
| 443 |
|
| } |
| 444 |
776 |
▶ | for (pi = 0; pi < num_paths; pi++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (pi = 0; pi < num_paths; pi++) { |
686 |
90 |
776 |
|
| 445 |
686 |
▶ | DEC_NEED(8); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(8); |
0 |
686 |
686 |
|
| 446 |
686 |
| uint64_t dyn_ver = dec_le64(*p); *p += 8; |
| 447 |
686 |
▶ | if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) |
Branch: 3 branches
| Branch | True | False | Total |
| if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) |
0 |
686 |
686 |
| if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) |
0 |
0 |
0 |
| if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) |
0 |
0 |
0 |
|
| 448 |
0 |
| croak("decode JSON: unsupported Dynamic version %lu " |
| 449 |
|
| "(known: 1, 2, 4); upgrade ClickHouse::Encoder", |
| 450 |
|
| (unsigned long)dyn_ver); |
| 451 |
686 |
▶ | if (dyn_ver == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dyn_ver == 1) |
686 |
0 |
686 |
|
| 452 |
686 |
| (void)dec_varint(aTHX_ p, end); |
| 453 |
686 |
| UV ntypes = dec_varint(aTHX_ p, end); |
| 454 |
|
| /* Each type name needs at least 2 bytes (length varint |
| 455 |
|
| * + name byte). Reject implausibly large counts. */ |
| 456 |
686 |
▶ | if (ntypes > (UV)(end - *p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ntypes > (UV)(end - *p)) |
0 |
686 |
686 |
|
| 457 |
0 |
| croak("decode JSON: ntypes=%lu exceeds remaining " |
| 458 |
|
| "buffer (%lu bytes)", |
| 459 |
|
| (unsigned long)ntypes, (unsigned long)(end - *p)); |
| 460 |
|
| |
| 461 |
686 |
| int *kinds_in_order = NULL; |
| 462 |
686 |
▶ | if (ntypes > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ntypes > 0) { |
686 |
0 |
686 |
|
| 463 |
686 |
▶ | Newx(kinds_in_order, ntypes, int); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(kinds_in_order, ntypes, int); |
0 |
686 |
686 |
|
| 464 |
686 |
| SAVEFREEPV(kinds_in_order); |
| 465 |
|
| } |
| 466 |
|
| UV ti; |
| 467 |
1439 |
▶ | for (ti = 0; ti < ntypes; ti++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ti = 0; ti < ntypes; ti++) { |
753 |
686 |
1439 |
|
| 468 |
|
| const char *ts; |
| 469 |
|
| STRLEN tl; |
| 470 |
753 |
| dec_lenpfx_string(aTHX_ p, end, &ts, &tl); |
| 471 |
753 |
| int k = json_kind_from_type_name(ts, tl); |
| 472 |
753 |
▶ | if (k < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (k < 0) |
0 |
753 |
753 |
|
| 473 |
0 |
| croak("decode JSON: unsupported variant type '%.*s' " |
| 474 |
|
| "for path '%.*s' (supported: Bool, Float64, " |
| 475 |
|
| "Int64, String, Array(...) of those)", |
| 476 |
|
| (int)tl, ts, (int)path_lens[pi], paths[pi]); |
| 477 |
753 |
| kinds_in_order[ti] = k; |
| 478 |
|
| } |
| 479 |
686 |
| path_kind_list[pi] = kinds_in_order; |
| 480 |
686 |
| path_kind_count[pi] = (int)ntypes; |
| 481 |
|
| |
| 482 |
|
| /* Variant prefix: 8-byte mode. */ |
| 483 |
686 |
▶ | DEC_NEED(8); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(8); |
0 |
686 |
686 |
|
| 484 |
686 |
| uint64_t var_mode = dec_le64(*p); *p += 8; |
| 485 |
686 |
▶ | if (var_mode != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (var_mode != 0) |
0 |
686 |
686 |
|
| 486 |
0 |
| croak("decode JSON: only BASIC variant mode supported " |
| 487 |
|
| "(got %lu)", (unsigned long)var_mode); |
| 488 |
|
| } |
| 489 |
|
| |
| 490 |
90 |
| state->num_paths = num_paths; |
| 491 |
90 |
| state->paths = paths; |
| 492 |
90 |
| state->path_lens = path_lens; |
| 493 |
90 |
| state->path_kind_list = path_kind_list; |
| 494 |
90 |
| state->path_kind_count = path_kind_count; |
| 495 |
90 |
| return; |
| 496 |
|
| } |
| 497 |
12 |
| case T_DYNAMIC: { |
| 498 |
|
| DecodePrefix *state; |
| 499 |
12 |
| Newxz(state, 1, DecodePrefix); |
| 500 |
12 |
| SAVEFREEPV(state); |
| 501 |
12 |
| t->decode_state = state; |
| 502 |
|
| |
| 503 |
|
| /* Dynamic V1/V2/V3 prefix + Variant data, no Object wrapper. */ |
| 504 |
12 |
▶ | DEC_NEED(8); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(8); |
0 |
12 |
12 |
|
| 505 |
12 |
| uint64_t dyn_ver = dec_le64(*p); *p += 8; |
| 506 |
12 |
▶ | if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) |
Branch: 3 branches
| Branch | True | False | Total |
| if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) |
0 |
12 |
12 |
| if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) |
0 |
0 |
0 |
| if (dyn_ver != 1 && dyn_ver != 2 && dyn_ver != 4) |
0 |
0 |
0 |
|
| 507 |
0 |
| croak("decode Dynamic: unsupported version %lu " |
| 508 |
|
| "(known: 1, 2, 4); upgrade ClickHouse::Encoder", |
| 509 |
|
| (unsigned long)dyn_ver); |
| 510 |
12 |
▶ | if (dyn_ver == 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dyn_ver == 1) |
12 |
0 |
12 |
|
| 511 |
12 |
| (void)dec_varint(aTHX_ p, end); |
| 512 |
12 |
| UV ntypes = dec_varint(aTHX_ p, end); |
| 513 |
|
| /* Same bound as the JSON path's per-path Dynamic prefix: |
| 514 |
|
| * each type name takes at least 2 bytes on the wire. */ |
| 515 |
12 |
▶ | if (ntypes > (UV)(end - *p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ntypes > (UV)(end - *p)) |
0 |
12 |
12 |
|
| 516 |
0 |
| croak("decode Dynamic: ntypes=%lu exceeds remaining " |
| 517 |
|
| "buffer (%lu bytes)", |
| 518 |
|
| (unsigned long)ntypes, (unsigned long)(end - *p)); |
| 519 |
|
| |
| 520 |
12 |
| int *kinds_in_order = NULL; |
| 521 |
12 |
▶ | if (ntypes > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ntypes > 0) { |
11 |
1 |
12 |
|
| 522 |
11 |
▶ | Newx(kinds_in_order, ntypes, int); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(kinds_in_order, ntypes, int); |
0 |
11 |
11 |
|
| 523 |
11 |
| SAVEFREEPV(kinds_in_order); |
| 524 |
|
| } |
| 525 |
|
| UV ti; |
| 526 |
303 |
▶ | for (ti = 0; ti < ntypes; ti++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ti = 0; ti < ntypes; ti++) { |
291 |
12 |
303 |
|
| 527 |
|
| const char *ts; STRLEN tl; |
| 528 |
291 |
| dec_lenpfx_string(aTHX_ p, end, &ts, &tl); |
| 529 |
291 |
| int k = json_kind_from_type_name(ts, tl); |
| 530 |
291 |
▶ | if (k < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (k < 0) |
0 |
291 |
291 |
|
| 531 |
0 |
| croak("decode Dynamic: unsupported variant type '%.*s' " |
| 532 |
|
| "(supported: Bool, Float64, Int64, String, " |
| 533 |
|
| "Array(...) of those)", |
| 534 |
|
| (int)tl, ts); |
| 535 |
291 |
| kinds_in_order[ti] = k; |
| 536 |
|
| } |
| 537 |
|
| |
| 538 |
12 |
▶ | DEC_NEED(8); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(8); |
0 |
12 |
12 |
|
| 539 |
12 |
| uint64_t var_mode = dec_le64(*p); *p += 8; |
| 540 |
12 |
▶ | if (var_mode != 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (var_mode != 0) |
0 |
12 |
12 |
|
| 541 |
0 |
| croak("decode Dynamic: only BASIC variant mode supported " |
| 542 |
|
| "(got %lu)", (unsigned long)var_mode); |
| 543 |
|
| |
| 544 |
12 |
| state->ntypes = ntypes; |
| 545 |
12 |
| state->kinds_in_order = kinds_in_order; |
| 546 |
12 |
| return; |
| 547 |
|
| } |
| 548 |
15 |
| default: |
| 549 |
15 |
▶ | if (t->inner) decode_prefix(aTHX_ p, end, t->inner); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->inner) decode_prefix(aTHX_ p, end, t->inner); |
11 |
4 |
15 |
|
| 550 |
23 |
▶ | for (i = 0; i < t->tuple_len; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->tuple_len; i++) |
8 |
15 |
23 |
|
| 551 |
8 |
| decode_prefix(aTHX_ p, end, t->tuple[i]); |
| 552 |
|
| } |
| 553 |
|
| } |
| 554 |
|
| |
| 555 |
26808 |
| SV *decode_column(pTHX_ const unsigned char **p, const unsigned char *end, |
| 556 |
|
| TypeInfo *t, SSize_t nrows) { |
| 557 |
26808 |
▶ | if (!nrows) return newRV_noinc((SV *)newAV()); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nrows) return newRV_noinc((SV *)newAV()); |
22 |
26786 |
26808 |
|
| 558 |
26786 |
▶ | if (!t->has_prefix) return decode_column_data(aTHX_ p, end, t, nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t->has_prefix) return decode_column_data(aTHX_ p, end, t, nrows); |
26659 |
127 |
26786 |
|
| 559 |
127 |
▶ | if (!t->split_prefix && (t->code == T_LOWCARDINALITY || t->code == T_VARIANT)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (!t->split_prefix && (t->code == T_LOWCARDINALITY || t->code == T_VARIANT)) { |
113 |
14 |
127 |
| if (!t->split_prefix && (t->code == T_LOWCARDINALITY || t->code == T_VARIANT)) { |
104 |
9 |
113 |
| if (!t->split_prefix && (t->code == T_LOWCARDINALITY || t->code == T_VARIANT)) { |
5 |
99 |
104 |
|
| 560 |
14 |
| decode_prefix(aTHX_ p, end, t); |
| 561 |
14 |
| return decode_column_data(aTHX_ p, end, t, nrows); |
| 562 |
|
| } |
| 563 |
113 |
| ENTER; |
| 564 |
113 |
| decode_prefix(aTHX_ p, end, t); |
| 565 |
112 |
| SV *result = decode_column_data(aTHX_ p, end, t, nrows); |
| 566 |
108 |
| LEAVE; |
| 567 |
108 |
| return result; |
| 568 |
|
| } |
| 569 |
|
| |
| 570 |
26961 |
| static SV *decode_column_data(pTHX_ const unsigned char **p, |
| 571 |
|
| const unsigned char *end, |
| 572 |
|
| TypeInfo *t, SSize_t nrows) { |
| 573 |
26961 |
▶ | if (!nrows) return newRV_noinc((SV *)newAV()); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!nrows) return newRV_noinc((SV *)newAV()); |
7 |
26954 |
26961 |
|
| 574 |
26954 |
| SV *result = NULL; |
| 575 |
|
| DecGuard g; |
| 576 |
|
| AV *av; |
| 577 |
|
| SSize_t r; |
| 578 |
|
| |
| 579 |
|
| /* Own scope: XSUBs get no implicit one, so the guard needs it. */ |
| 580 |
26954 |
| ENTER; |
| 581 |
26954 |
| Zero(&g, 1, DecGuard); |
| 582 |
26954 |
| SAVEDESTRUCTOR_X(cleanup_dec_guard, &g); |
| 583 |
|
| |
| 584 |
26954 |
| av = newAV(); |
| 585 |
26954 |
| g.av = av; |
| 586 |
26954 |
▶ | if (nrows > 0) av_extend(av, nrows - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nrows > 0) av_extend(av, nrows - 1); |
26954 |
0 |
26954 |
|
| 587 |
|
| |
| 588 |
26954 |
| switch (t->code) { |
| 589 |
4 |
| case T_INT8: { |
| 590 |
4 |
▶ | DEC_NEED((STRLEN)nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)nrows); |
0 |
4 |
4 |
|
| 591 |
10 |
▶ | DEC_SCALAR_LOOP(av, nrows, newSViv((IV)(int8_t)*(*p)++)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_SCALAR_LOOP(av, nrows, newSViv((IV)(int8_t)*(*p)++)); |
6 |
4 |
10 |
|
| 592 |
4 |
| break; |
| 593 |
|
| } |
| 594 |
17 |
| case T_ENUM8: { |
| 595 |
17 |
▶ | DEC_NEED((STRLEN)nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)nrows); |
0 |
17 |
17 |
|
| 596 |
41 |
▶ | DEC_SCALAR_LOOP(av, nrows, newSViv((IV)(int8_t)*(*p)++)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_SCALAR_LOOP(av, nrows, newSViv((IV)(int8_t)*(*p)++)); |
24 |
17 |
41 |
|
| 597 |
17 |
| break; |
| 598 |
|
| } |
| 599 |
228 |
| case T_UINT8: case T_BOOL: { |
| 600 |
228 |
▶ | DEC_NEED((STRLEN)nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)nrows); |
1 |
227 |
228 |
|
| 601 |
23861 |
▶ | DEC_SCALAR_LOOP(av, nrows, newSVuv((UV)*(*p)++)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_SCALAR_LOOP(av, nrows, newSVuv((UV)*(*p)++)); |
23634 |
227 |
23861 |
|
| 602 |
227 |
| break; |
| 603 |
|
| } |
| 604 |
3 |
| case T_INT16: { |
| 605 |
3 |
▶ | DEC_NEED((STRLEN)(2 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(2 * nrows)); |
0 |
3 |
3 |
|
| 606 |
6 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
3 |
3 |
6 |
|
| 607 |
|
| /* Use dec_le16 + memcpy bit-cast (same pattern as INT32 |
| 608 |
|
| * and INT64) to keep the signed conversion well-defined |
| 609 |
|
| * across compilers; the inline (int16_t)(...) cast on |
| 610 |
|
| * a promoted-int high-bit value is implementation- |
| 611 |
|
| * defined in C99/C11. */ |
| 612 |
3 |
| uint16_t u = dec_le16(*p); |
| 613 |
3 |
| int16_t v; memcpy(&v, &u, 2); |
| 614 |
3 |
| av_store(av, r, newSViv((IV)v)); |
| 615 |
3 |
| *p += 2; |
| 616 |
|
| } |
| 617 |
3 |
| break; |
| 618 |
|
| } |
| 619 |
18 |
| case T_ENUM16: { |
| 620 |
18 |
▶ | DEC_NEED((STRLEN)(2 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(2 * nrows)); |
0 |
18 |
18 |
|
| 621 |
44 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
26 |
18 |
44 |
|
| 622 |
26 |
| uint16_t u = dec_le16(*p); |
| 623 |
26 |
| int16_t v; memcpy(&v, &u, 2); |
| 624 |
26 |
| av_store(av, r, newSViv((IV)v)); |
| 625 |
26 |
| *p += 2; |
| 626 |
|
| } |
| 627 |
18 |
| break; |
| 628 |
|
| } |
| 629 |
7 |
| case T_UINT16: case T_DATE: { |
| 630 |
|
| |
| 631 |
7 |
▶ | DEC_NEED((STRLEN)(2 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(2 * nrows)); |
0 |
7 |
7 |
|
| 632 |
19 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
12 |
7 |
19 |
|
| 633 |
12 |
| uint16_t v = dec_le16(*p); |
| 634 |
12 |
| av_store(av, r, newSVuv((UV)v)); |
| 635 |
12 |
| *p += 2; |
| 636 |
|
| } |
| 637 |
7 |
| break; |
| 638 |
|
| } |
| 639 |
285 |
| case T_INT32: case T_DATE32: case T_DECIMAL32: { |
| 640 |
285 |
▶ | DEC_NEED((STRLEN)(4 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(4 * nrows)); |
2 |
283 |
285 |
|
| 641 |
20438 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
20155 |
283 |
20438 |
|
| 642 |
20155 |
| uint32_t u = dec_le32(*p); |
| 643 |
20155 |
| int32_t v; memcpy(&v, &u, 4); |
| 644 |
20155 |
| av_store(av, r, newSViv((IV)v)); |
| 645 |
20155 |
| *p += 4; |
| 646 |
|
| } |
| 647 |
283 |
| break; |
| 648 |
|
| } |
| 649 |
271 |
| case T_UINT32: case T_DATETIME: { |
| 650 |
271 |
▶ | DEC_NEED((STRLEN)(4 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(4 * nrows)); |
2 |
269 |
271 |
|
| 651 |
21166 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
20897 |
269 |
21166 |
|
| 652 |
20897 |
| uint32_t v = dec_le32(*p); |
| 653 |
20897 |
| av_store(av, r, newSVuv((UV)v)); |
| 654 |
20897 |
| *p += 4; |
| 655 |
|
| } |
| 656 |
269 |
| break; |
| 657 |
|
| } |
| 658 |
28 |
| case T_INT64: case T_DATETIME64: case T_DECIMAL64: { |
| 659 |
28 |
▶ | DEC_NEED((STRLEN)(8 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(8 * nrows)); |
0 |
28 |
28 |
|
| 660 |
66 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
38 |
28 |
66 |
|
| 661 |
38 |
| uint64_t u = dec_le64(*p); |
| 662 |
38 |
| int64_t v; memcpy(&v, &u, 8); |
| 663 |
38 |
| av_store(av, r, newSViv((IV)v)); |
| 664 |
38 |
| *p += 8; |
| 665 |
|
| } |
| 666 |
28 |
| break; |
| 667 |
|
| } |
| 668 |
11 |
| case T_UINT64: { |
| 669 |
11 |
▶ | DEC_NEED((STRLEN)(8 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(8 * nrows)); |
0 |
11 |
11 |
|
| 670 |
34 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
23 |
11 |
34 |
|
| 671 |
23 |
| uint64_t v = dec_le64(*p); |
| 672 |
23 |
| av_store(av, r, newSVuv((UV)v)); |
| 673 |
23 |
| *p += 8; |
| 674 |
|
| } |
| 675 |
11 |
| break; |
| 676 |
|
| } |
| 677 |
1 |
| case T_FLOAT32: { |
| 678 |
1 |
▶ | DEC_NEED((STRLEN)(4 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(4 * nrows)); |
0 |
1 |
1 |
|
| 679 |
3 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
2 |
1 |
3 |
|
| 680 |
2 |
| uint32_t u = dec_le32(*p); |
| 681 |
2 |
| float v; memcpy(&v, &u, 4); |
| 682 |
2 |
| av_store(av, r, newSVnv((NV)v)); |
| 683 |
2 |
| *p += 4; |
| 684 |
|
| } |
| 685 |
1 |
| break; |
| 686 |
|
| } |
| 687 |
205 |
| case T_FLOAT64: { |
| 688 |
205 |
▶ | DEC_NEED((STRLEN)(8 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(8 * nrows)); |
0 |
205 |
205 |
|
| 689 |
20212 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
20007 |
205 |
20212 |
|
| 690 |
20007 |
| uint64_t u = dec_le64(*p); |
| 691 |
20007 |
| double v; memcpy(&v, &u, 8); |
| 692 |
20007 |
| av_store(av, r, newSVnv((NV)v)); |
| 693 |
20007 |
| *p += 8; |
| 694 |
|
| } |
| 695 |
205 |
| break; |
| 696 |
|
| } |
| 697 |
1 |
| case T_BFLOAT16: { |
| 698 |
|
| /* Reconstruct a Float32 by shifting 16 wire bits into the high half. */ |
| 699 |
1 |
▶ | DEC_NEED((STRLEN)(2 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(2 * nrows)); |
0 |
1 |
1 |
|
| 700 |
3 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
2 |
1 |
3 |
|
| 701 |
2 |
| uint32_t bits = (uint32_t)((*p)[0] | ((*p)[1] << 8)) << 16; |
| 702 |
|
| float fv; |
| 703 |
2 |
| memcpy(&fv, &bits, 4); |
| 704 |
2 |
| av_store(av, r, newSVnv((NV)fv)); |
| 705 |
2 |
| *p += 2; |
| 706 |
|
| } |
| 707 |
1 |
| break; |
| 708 |
|
| } |
| 709 |
25425 |
| case T_STRING: { |
| 710 |
5056280 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
5055951 |
329 |
5056280 |
|
| 711 |
|
| const char *s; STRLEN l; |
| 712 |
5055951 |
| dec_lenpfx_string(aTHX_ p, end, &s, &l); |
| 713 |
5030855 |
| av_store(av, r, newSVpvn(s, l)); |
| 714 |
|
| } |
| 715 |
329 |
| break; |
| 716 |
|
| } |
| 717 |
6 |
| case T_FIXEDSTRING: { |
| 718 |
6 |
| STRLEN n = (STRLEN)t->param; |
| 719 |
|
| /* `n * nrows` is the only multiplicative bound here whose |
| 720 |
|
| * multiplier is fully user-controlled (FixedString(N) for |
| 721 |
|
| * any N up to ~2^31). Defend against overflow explicitly |
| 722 |
|
| * rather than relying on prologue's per-row bound. */ |
| 723 |
6 |
▶ | if (n > 0 && (UV)nrows > ((UV)(end - *p)) / n) |
Branch: 2 branches
| Branch | True | False | Total |
| if (n > 0 && (UV)nrows > ((UV)(end - *p)) / n) |
6 |
0 |
6 |
| if (n > 0 && (UV)nrows > ((UV)(end - *p)) / n) |
0 |
6 |
6 |
|
| 724 |
0 |
| croak("decode: FixedString(%lu) x %ld rows exceeds " |
| 725 |
|
| "remaining buffer (%lu bytes)", |
| 726 |
|
| (unsigned long)n, (long)nrows, |
| 727 |
|
| (unsigned long)(end - *p)); |
| 728 |
6 |
▶ | DEC_NEED(n * (STRLEN)nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(n * (STRLEN)nrows); |
0 |
6 |
6 |
|
| 729 |
17 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
11 |
6 |
17 |
|
| 730 |
11 |
| av_store(av, r, newSVpvn((const char *)*p, n)); |
| 731 |
11 |
| *p += n; |
| 732 |
|
| } |
| 733 |
6 |
| break; |
| 734 |
|
| } |
| 735 |
29 |
| case T_DECIMAL128: { |
| 736 |
|
| /* Division-form guard against `16 * nrows` overflow when |
| 737 |
|
| * nrows is attacker-controlled. */ |
| 738 |
29 |
▶ | if ((UV)nrows > (UV)(end - *p) / 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)nrows > (UV)(end - *p) / 16) |
24 |
5 |
29 |
|
| 739 |
24 |
| croak("decode: Decimal128 x %ld rows exceeds remaining buffer", |
| 740 |
|
| (long)nrows); |
| 741 |
5 |
▶ | DEC_NEED((STRLEN)(16 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(16 * nrows)); |
0 |
5 |
5 |
|
| 742 |
813 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
808 |
5 |
813 |
|
| 743 |
808 |
| uint64_t lo = dec_le64(*p); |
| 744 |
808 |
| uint64_t hu = dec_le64(*p + 8); |
| 745 |
808 |
| int64_t hi; memcpy(&hi, &hu, 8); |
| 746 |
808 |
| AV *pair = newAV(); |
| 747 |
808 |
| av_extend(pair, 1); |
| 748 |
808 |
| av_store(pair, 0, newSVuv((UV)lo)); |
| 749 |
808 |
| av_store(pair, 1, newSViv((IV)hi)); |
| 750 |
808 |
| av_store(av, r, newRV_noinc((SV*)pair)); |
| 751 |
808 |
| *p += 16; |
| 752 |
|
| } |
| 753 |
5 |
| break; |
| 754 |
|
| } |
| 755 |
54 |
| case T_DECIMAL256: { |
| 756 |
54 |
▶ | if ((UV)nrows > (UV)(end - *p) / 32) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)nrows > (UV)(end - *p) / 32) |
50 |
4 |
54 |
|
| 757 |
50 |
| croak("decode: Decimal256 x %ld rows exceeds remaining buffer", |
| 758 |
|
| (long)nrows); |
| 759 |
4 |
▶ | DEC_NEED((STRLEN)(32 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(32 * nrows)); |
0 |
4 |
4 |
|
| 760 |
809 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
805 |
4 |
809 |
|
| 761 |
805 |
| AV *limbs = newAV(); |
| 762 |
805 |
| av_extend(limbs, 3); |
| 763 |
|
| int i; |
| 764 |
4025 |
▶ | for (i = 0; i < 4; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 4; i++) { |
3220 |
805 |
4025 |
|
| 765 |
3220 |
| uint64_t l = dec_le64(*p + 8 * i); |
| 766 |
3220 |
| av_store(limbs, i, newSVuv((UV)l)); |
| 767 |
|
| } |
| 768 |
805 |
| av_store(av, r, newRV_noinc((SV*)limbs)); |
| 769 |
805 |
| *p += 32; |
| 770 |
|
| } |
| 771 |
4 |
| break; |
| 772 |
|
| } |
| 773 |
55 |
| case T_UUID: { |
| 774 |
|
| /* Wire: two LE UInt64 halves with bytes reversed within each |
| 775 |
|
| * half. Reassemble to standard 8-4-4-4-12 hex form. */ |
| 776 |
55 |
▶ | if ((UV)nrows > (UV)(end - *p) / 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)nrows > (UV)(end - *p) / 16) |
44 |
11 |
55 |
|
| 777 |
44 |
| croak("decode: UUID x %ld rows exceeds remaining buffer", |
| 778 |
|
| (long)nrows); |
| 779 |
11 |
▶ | DEC_NEED((STRLEN)(16 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(16 * nrows)); |
0 |
11 |
11 |
|
| 780 |
2817 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
2806 |
11 |
2817 |
|
| 781 |
|
| unsigned char b[16]; |
| 782 |
|
| int i; |
| 783 |
25254 |
▶ | for (i = 0; i < 8; i++) b[i] = (*p)[7 - i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) b[i] = (*p)[7 - i]; |
22448 |
2806 |
25254 |
|
| 784 |
25254 |
▶ | for (i = 0; i < 8; i++) b[8+i] = (*p)[15 - i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) b[8+i] = (*p)[15 - i]; |
22448 |
2806 |
25254 |
|
| 785 |
2806 |
| *p += 16; |
| 786 |
|
| char hex[37]; |
| 787 |
|
| static const char H[] = "0123456789abcdef"; |
| 788 |
2806 |
| int j = 0, k; |
| 789 |
47702 |
▶ | for (k = 0; k < 16; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = 0; k < 16; k++) { |
44896 |
2806 |
47702 |
|
| 790 |
44896 |
| hex[j++] = H[(b[k] >> 4) & 0xf]; |
| 791 |
44896 |
| hex[j++] = H[b[k] & 0xf]; |
| 792 |
44896 |
▶ | if (k == 3 || k == 5 || k == 7 || k == 9) hex[j++] = '-'; |
Branch: 4 branches
| Branch | True | False | Total |
| if (k == 3 || k == 5 || k == 7 || k == 9) hex[j++] = '-'; |
42090 |
2806 |
44896 |
| if (k == 3 || k == 5 || k == 7 || k == 9) hex[j++] = '-'; |
39284 |
2806 |
42090 |
| if (k == 3 || k == 5 || k == 7 || k == 9) hex[j++] = '-'; |
36478 |
2806 |
39284 |
| if (k == 3 || k == 5 || k == 7 || k == 9) hex[j++] = '-'; |
2806 |
33672 |
36478 |
|
| 793 |
|
| } |
| 794 |
2806 |
| hex[36] = '\0'; |
| 795 |
2806 |
| av_store(av, r, newSVpvn(hex, 36)); |
| 796 |
|
| } |
| 797 |
11 |
| break; |
| 798 |
|
| } |
| 799 |
7 |
| case T_IPV4: { |
| 800 |
7 |
▶ | DEC_NEED((STRLEN)(4 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(4 * nrows)); |
0 |
7 |
7 |
|
| 801 |
16 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
9 |
7 |
16 |
|
| 802 |
|
| /* Wire is LE uint32 = [oct4][oct3][oct2][oct1]. Read the |
| 803 |
|
| * bytes directly so the output order is endianness- |
| 804 |
|
| * independent. */ |
| 805 |
9 |
| const unsigned char *b = *p; |
| 806 |
|
| char buf[16]; |
| 807 |
9 |
| int n = my_snprintf(buf, sizeof buf, "%u.%u.%u.%u", |
| 808 |
9 |
| b[3], b[2], b[1], b[0]); |
| 809 |
9 |
| av_store(av, r, newSVpvn(buf, n)); |
| 810 |
9 |
| *p += 4; |
| 811 |
|
| } |
| 812 |
7 |
| break; |
| 813 |
|
| } |
| 814 |
28 |
| case T_IPV6: { |
| 815 |
28 |
▶ | if ((UV)nrows > (UV)(end - *p) / 16) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)nrows > (UV)(end - *p) / 16) |
24 |
4 |
28 |
|
| 816 |
24 |
| croak("decode: IPv6 x %ld rows exceeds remaining buffer", |
| 817 |
|
| (long)nrows); |
| 818 |
4 |
▶ | DEC_NEED((STRLEN)(16 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(16 * nrows)); |
0 |
4 |
4 |
|
| 819 |
806 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
802 |
4 |
806 |
|
| 820 |
802 |
| av_store(av, r, newSVpvn((const char *)*p, 16)); |
| 821 |
802 |
| *p += 16; |
| 822 |
|
| } |
| 823 |
4 |
| break; |
| 824 |
|
| } |
| 825 |
75 |
| case T_ARRAY: { |
| 826 |
|
| /* Read nrows UInt64 offsets, then decode flat inner array |
| 827 |
|
| * of total = offsets[nrows-1] elements, then slice. */ |
| 828 |
75 |
▶ | if ((UV)nrows > (UV)(end - *p) / 8) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)nrows > (UV)(end - *p) / 8) |
30 |
45 |
75 |
|
| 829 |
30 |
| croak("decode: Array offsets x %ld rows exceeds remaining buffer", |
| 830 |
|
| (long)nrows); |
| 831 |
45 |
▶ | DEC_NEED((STRLEN)(8 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(8 * nrows)); |
0 |
45 |
45 |
|
| 832 |
|
| SSize_t *offsets; |
| 833 |
45 |
▶ | Newx(offsets, nrows + 1, SSize_t); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(offsets, nrows + 1, SSize_t); |
0 |
45 |
45 |
|
| 834 |
45 |
| SAVEFREEPV(offsets); |
| 835 |
45 |
| offsets[0] = 0; |
| 836 |
6510 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
6465 |
45 |
6510 |
|
| 837 |
6465 |
| uint64_t o = dec_le64(*p); |
| 838 |
|
| /* Per-offset overflow + monotonicity check; the final-only |
| 839 |
|
| * check below is insufficient because an intermediate |
| 840 |
|
| * offset with bit 63 set narrows to a negative SSize_t and |
| 841 |
|
| * later produces an enormous stop-start span in av_extend. */ |
| 842 |
6465 |
▶ | if (o > (uint64_t)SSize_t_MAX || (SSize_t)o < offsets[r]) |
Branch: 2 branches
| Branch | True | False | Total |
| if (o > (uint64_t)SSize_t_MAX || (SSize_t)o < offsets[r]) |
6465 |
0 |
6465 |
| if (o > (uint64_t)SSize_t_MAX || (SSize_t)o < offsets[r]) |
0 |
6465 |
6465 |
|
| 843 |
0 |
| croak("decode: Array offset[%ld]=%lu invalid (prev=%ld)", |
| 844 |
|
| (long)r, (unsigned long)o, (long)offsets[r]); |
| 845 |
6465 |
| offsets[r + 1] = (SSize_t)o; |
| 846 |
6465 |
| *p += 8; |
| 847 |
|
| } |
| 848 |
45 |
| SSize_t total = offsets[nrows]; |
| 849 |
|
| /* Defensive: a corrupted offset must not allocate gigabytes |
| 850 |
|
| * for the inner array. */ |
| 851 |
45 |
▶ | if ((UV)total > (UV)(end - *p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((UV)total > (UV)(end - *p)) |
7 |
38 |
45 |
|
| 852 |
7 |
| croak("decode: Array total=%ld exceeds remaining buffer " |
| 853 |
|
| "(%lu bytes)", |
| 854 |
|
| (long)total, (unsigned long)(end - *p)); |
| 855 |
38 |
| SV *flat_rv = decode_column_data(aTHX_ p, end, t->inner, total); |
| 856 |
34 |
| g.inner = flat_rv; |
| 857 |
34 |
| AV *flat = (AV *)SvRV(flat_rv); |
| 858 |
2099 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
2065 |
34 |
2099 |
|
| 859 |
2065 |
| AV *slice = newAV(); |
| 860 |
2065 |
| SSize_t start = offsets[r], stop = offsets[r + 1]; |
| 861 |
2065 |
▶ | if (stop > start) av_extend(slice, stop - start - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (stop > start) av_extend(slice, stop - start - 1); |
2038 |
27 |
2065 |
|
| 862 |
|
| SSize_t i; |
| 863 |
6529 |
▶ | for (i = start; i < stop; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < stop; i++) { |
4464 |
2065 |
6529 |
|
| 864 |
4464 |
| SV **elem = av_fetch(flat, i, 0); |
| 865 |
4464 |
▶ | av_store(slice, i - start, |
Branch: 1 branch
| Branch | True | False | Total |
| av_store(slice, i - start, |
4464 |
0 |
4464 |
|
| 866 |
|
| elem ? SvREFCNT_inc(*elem) : newSV(0)); |
| 867 |
|
| } |
| 868 |
2065 |
| av_store(av, r, newRV_noinc((SV*)slice)); |
| 869 |
|
| } |
| 870 |
34 |
| g.inner = NULL; |
| 871 |
34 |
| SvREFCNT_dec(flat_rv); |
| 872 |
34 |
| break; |
| 873 |
|
| } |
| 874 |
21 |
| case T_TUPLE: { |
| 875 |
21 |
▶ | if (!t->tuple_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t->tuple_len) { |
1 |
20 |
21 |
|
| 876 |
1 |
▶ | DEC_NEED((STRLEN)nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)nrows); |
0 |
1 |
1 |
|
| 877 |
1 |
| *p += nrows; |
| 878 |
3 |
▶ | for (r = 0; r < nrows; r++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) |
2 |
1 |
3 |
|
| 879 |
2 |
| av_store(av, r, newRV_noinc((SV *)newAV())); |
| 880 |
1 |
| break; |
| 881 |
|
| } |
| 882 |
|
| /* Decode each element type as a column of `nrows`, then |
| 883 |
|
| * transpose into per-row tuples. */ |
| 884 |
|
| int i; |
| 885 |
|
| SV **cols; |
| 886 |
|
| /* Guard-owned; grown one slot at a time so a croak frees only |
| 887 |
|
| * what was decoded. +1 covers the degenerate Tuple() case. */ |
| 888 |
20 |
| Newxz(cols, t->tuple_len + 1, SV*); |
| 889 |
20 |
| g.vec = cols; |
| 890 |
20 |
| g.vec_n = 0; |
| 891 |
54 |
▶ | for (i = 0; i < t->tuple_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->tuple_len; i++) { |
38 |
16 |
54 |
|
| 892 |
38 |
| cols[i] = decode_column_data(aTHX_ p, end, t->tuple[i], nrows); |
| 893 |
34 |
| g.vec_n = i + 1; |
| 894 |
|
| } |
| 895 |
1239 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
1223 |
16 |
1239 |
|
| 896 |
1223 |
| AV *row = newAV(); |
| 897 |
1223 |
▶ | if (t->tuple_len > 0) av_extend(row, t->tuple_len - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->tuple_len > 0) av_extend(row, t->tuple_len - 1); |
1223 |
0 |
1223 |
|
| 898 |
3669 |
▶ | for (i = 0; i < t->tuple_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->tuple_len; i++) { |
2446 |
1223 |
3669 |
|
| 899 |
2446 |
| SV **elem = av_fetch((AV *)SvRV(cols[i]), r, 0); |
| 900 |
2446 |
▶ | av_store(row, i, elem ? SvREFCNT_inc(*elem) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_store(row, i, elem ? SvREFCNT_inc(*elem) : newSV(0)); |
2446 |
0 |
2446 |
|
| 901 |
|
| } |
| 902 |
1223 |
| av_store(av, r, newRV_noinc((SV*)row)); |
| 903 |
|
| } |
| 904 |
16 |
| g.vec = NULL; |
| 905 |
16 |
| g.vec_n = 0; |
| 906 |
48 |
▶ | for (i = 0; i < t->tuple_len; i++) SvREFCNT_dec(cols[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->tuple_len; i++) SvREFCNT_dec(cols[i]); |
32 |
16 |
48 |
|
| 907 |
16 |
| Safefree(cols); |
| 908 |
16 |
| break; |
| 909 |
|
| } |
| 910 |
24 |
| case T_NULLABLE: { |
| 911 |
24 |
▶ | DEC_NEED((STRLEN)nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)nrows); |
0 |
24 |
24 |
|
| 912 |
|
| unsigned char *nulls; |
| 913 |
24 |
| Newx(nulls, nrows, unsigned char); |
| 914 |
24 |
| SAVEFREEPV(nulls); |
| 915 |
74 |
▶ | for (r = 0; r < nrows; r++) nulls[r] = *(*p)++; |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) nulls[r] = *(*p)++; |
50 |
24 |
74 |
|
| 916 |
24 |
| SV *inner_rv = decode_column_data(aTHX_ p, end, t->inner, nrows); |
| 917 |
24 |
| g.inner = inner_rv; |
| 918 |
24 |
| AV *inner = (AV *)SvRV(inner_rv); |
| 919 |
74 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
50 |
24 |
74 |
|
| 920 |
50 |
▶ | if (nulls[r]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nulls[r]) { |
31 |
19 |
50 |
|
| 921 |
31 |
| av_store(av, r, newSV(0)); |
| 922 |
|
| } else { |
| 923 |
19 |
| SV **elem = av_fetch(inner, r, 0); |
| 924 |
19 |
▶ | av_store(av, r, elem ? SvREFCNT_inc(*elem) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_store(av, r, elem ? SvREFCNT_inc(*elem) : newSV(0)); |
19 |
0 |
19 |
|
| 925 |
|
| } |
| 926 |
|
| } |
| 927 |
24 |
| g.inner = NULL; |
| 928 |
24 |
| SvREFCNT_dec(inner_rv); |
| 929 |
24 |
| break; |
| 930 |
|
| } |
| 931 |
27 |
| case T_MAP: { |
| 932 |
|
| /* Map(K, V) on the wire is Array(Tuple(K, V)). Re-dispatch |
| 933 |
|
| * through a synthetic Array(Tuple) type. */ |
| 934 |
|
| TypeInfo array_t, tuple_t; |
| 935 |
27 |
| memset(&array_t, 0, sizeof array_t); |
| 936 |
27 |
| memset(&tuple_t, 0, sizeof tuple_t); |
| 937 |
27 |
| tuple_t.code = T_TUPLE; |
| 938 |
27 |
| tuple_t.tuple = t->tuple; |
| 939 |
27 |
| tuple_t.tuple_len = t->tuple_len; |
| 940 |
27 |
| array_t.code = T_ARRAY; |
| 941 |
27 |
| array_t.inner = &tuple_t; |
| 942 |
27 |
| SV *rv = decode_column_data(aTHX_ p, end, &array_t, nrows); |
| 943 |
9 |
| g.av = NULL; |
| 944 |
9 |
| SvREFCNT_dec((SV *)av); |
| 945 |
9 |
| result = rv; |
| 946 |
9 |
| break; |
| 947 |
|
| } |
| 948 |
16 |
| case T_LOWCARDINALITY: { |
| 949 |
16 |
▶ | DEC_NEED(16); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(16); |
0 |
16 |
16 |
|
| 950 |
16 |
| uint64_t flags = dec_le64(*p); |
| 951 |
16 |
| uint64_t dict_n = dec_le64(*p + 8); |
| 952 |
16 |
| *p += 16; |
| 953 |
|
| /* Defensive: a corrupted dict_n must not allocate gigabytes. */ |
| 954 |
16 |
▶ | if (dict_n > (uint64_t)(end - *p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dict_n > (uint64_t)(end - *p)) |
0 |
16 |
16 |
|
| 955 |
0 |
| croak("decode: LowCardinality dict_n=%lu exceeds remaining " |
| 956 |
|
| "buffer (%lu bytes)", |
| 957 |
|
| (unsigned long)dict_n, (unsigned long)(end - *p)); |
| 958 |
16 |
| int idx_type = (int)(flags & 0xff); |
| 959 |
|
| /* Only TUInt8=0..TUInt64=3 are defined; reject the rest |
| 960 |
|
| * loudly instead of silently aliasing to UInt64. */ |
| 961 |
16 |
▶ | if (idx_type > 3) |
Branch: 1 branch
| Branch | True | False | Total |
| if (idx_type > 3) |
0 |
16 |
16 |
|
| 962 |
0 |
| croak("decode: LowCardinality: unknown index type %d in flags", |
| 963 |
|
| idx_type); |
| 964 |
16 |
| TypeInfo *inner = t->inner; |
| 965 |
16 |
| int is_null = (inner->code == T_NULLABLE); |
| 966 |
16 |
▶ | TypeInfo *leaf = is_null ? inner->inner : inner; |
Branch: 1 branch
| Branch | True | False | Total |
| TypeInfo *leaf = is_null ? inner->inner : inner; |
6 |
10 |
16 |
|
| 967 |
16 |
| SV *dict_rv = decode_column_data(aTHX_ p, end, leaf, (SSize_t)dict_n); |
| 968 |
16 |
| g.inner = dict_rv; |
| 969 |
16 |
| AV *dict = (AV *)SvRV(dict_rv); |
| 970 |
16 |
| SV **dict_values = AvARRAY(dict); |
| 971 |
16 |
▶ | DEC_NEED(8); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED(8); |
0 |
16 |
16 |
|
| 972 |
16 |
| uint64_t idx_n = dec_le64(*p); |
| 973 |
16 |
| *p += 8; |
| 974 |
|
| /* Surface the meaningful error before DEC_NEED would croak on |
| 975 |
|
| * a truncated buffer for an absurdly large idx_n. */ |
| 976 |
16 |
▶ | if (idx_n > (uint64_t)SSize_t_MAX || (SSize_t)idx_n != nrows) |
Branch: 2 branches
| Branch | True | False | Total |
| if (idx_n > (uint64_t)SSize_t_MAX || (SSize_t)idx_n != nrows) |
16 |
0 |
16 |
| if (idx_n > (uint64_t)SSize_t_MAX || (SSize_t)idx_n != nrows) |
0 |
16 |
16 |
|
| 977 |
0 |
| croak("decode: LowCardinality index count (%lu) != nrows (%ld)", |
| 978 |
|
| (unsigned long)idx_n, (long)nrows); |
| 979 |
16 |
▶ | size_t idx_bytes = (idx_type == 0) ? 1 : |
Branch: 3 branches
| Branch | True | False | Total |
| size_t idx_bytes = (idx_type == 0) ? 1 : |
0 |
16 |
16 |
| size_t idx_bytes = (idx_type == 0) ? 1 : |
0 |
0 |
0 |
| size_t idx_bytes = (idx_type == 0) ? 1 : |
0 |
0 |
0 |
|
| 980 |
|
| (idx_type == 1) ? 2 : |
| 981 |
|
| (idx_type == 2) ? 4 : 8; |
| 982 |
16 |
▶ | DEC_NEED((STRLEN)(idx_bytes * idx_n)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(idx_bytes * idx_n)); |
0 |
16 |
16 |
|
| 983 |
56 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
40 |
16 |
56 |
|
| 984 |
40 |
| uint64_t i = 0; |
| 985 |
40 |
| switch (idx_bytes) { |
| 986 |
40 |
| case 1: i = (uint64_t)(*p)[0]; break; |
| 987 |
0 |
| case 2: i = (uint64_t)dec_le16(*p); break; |
| 988 |
0 |
| case 4: i = (uint64_t)dec_le32(*p); break; |
| 989 |
0 |
| case 8: i = dec_le64(*p); break; |
| 990 |
|
| } |
| 991 |
40 |
| *p += idx_bytes; |
| 992 |
40 |
▶ | if (is_null && i == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (is_null && i == 0) { |
17 |
23 |
40 |
| if (is_null && i == 0) { |
7 |
10 |
17 |
|
| 993 |
7 |
| av_store(av, r, newSV(0)); |
| 994 |
|
| } else { |
| 995 |
33 |
▶ | if (i >= dict_n) |
Branch: 1 branch
| Branch | True | False | Total |
| if (i >= dict_n) |
0 |
33 |
33 |
|
| 996 |
0 |
| croak("decode: LowCardinality index %lu out of range " |
| 997 |
|
| "(dict_n=%lu) at row %ld", |
| 998 |
|
| (unsigned long)i, (unsigned long)dict_n, (long)r); |
| 999 |
33 |
| SV *elem = dict_values[i]; |
| 1000 |
33 |
▶ | av_store(av, r, elem ? SvREFCNT_inc(elem) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_store(av, r, elem ? SvREFCNT_inc(elem) : newSV(0)); |
33 |
0 |
33 |
|
| 1001 |
|
| } |
| 1002 |
|
| } |
| 1003 |
16 |
| g.inner = NULL; |
| 1004 |
16 |
| SvREFCNT_dec(dict_rv); |
| 1005 |
16 |
| break; |
| 1006 |
|
| } |
| 1007 |
7 |
| case T_VARIANT: { |
| 1008 |
7 |
▶ | DEC_NEED((STRLEN)nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)nrows); |
0 |
7 |
7 |
|
| 1009 |
|
| unsigned char *wire_disc; |
| 1010 |
7 |
| Newx(wire_disc, nrows, unsigned char); |
| 1011 |
7 |
| SAVEFREEPV(wire_disc); |
| 1012 |
25 |
▶ | for (r = 0; r < nrows; r++) wire_disc[r] = *(*p)++; |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) wire_disc[r] = *(*p)++; |
18 |
7 |
25 |
|
| 1013 |
7 |
| int nvar = t->tuple_len; |
| 1014 |
|
| SSize_t *counts; |
| 1015 |
7 |
| Newxz(counts, nvar, SSize_t); |
| 1016 |
7 |
| SAVEFREEPV(counts); |
| 1017 |
25 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
18 |
7 |
25 |
|
| 1018 |
18 |
| unsigned char w = wire_disc[r]; |
| 1019 |
18 |
▶ | if (w != 255) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w != 255) { |
10 |
8 |
18 |
|
| 1020 |
10 |
▶ | if (w >= nvar) croak("decode: Variant wire idx %u out of range", w); |
Branch: 1 branch
| Branch | True | False | Total |
| if (w >= nvar) croak("decode: Variant wire idx %u out of range", w); |
0 |
10 |
10 |
|
| 1021 |
10 |
| counts[w]++; |
| 1022 |
|
| } |
| 1023 |
|
| } |
| 1024 |
|
| /* Decode each sub-column in wire (alphabetical) order; the |
| 1025 |
|
| * decl index of wire position w is t->variant_wire_to_decl[w]. */ |
| 1026 |
|
| SV **subcols; /* guard-owned, as in T_TUPLE */ |
| 1027 |
7 |
| Newxz(subcols, nvar + 1, SV*); |
| 1028 |
7 |
| g.vec = subcols; |
| 1029 |
7 |
| g.vec_n = 0; |
| 1030 |
|
| int w; |
| 1031 |
21 |
▶ | for (w = 0; w < nvar; w++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (w = 0; w < nvar; w++) { |
14 |
7 |
21 |
|
| 1032 |
14 |
| int decl = t->variant_wire_to_decl[w]; |
| 1033 |
14 |
| subcols[w] = decode_column_data(aTHX_ p, end, t->tuple[decl], counts[w]); |
| 1034 |
14 |
| g.vec_n = w + 1; |
| 1035 |
|
| } |
| 1036 |
|
| SSize_t *cursors; |
| 1037 |
7 |
| Newxz(cursors, nvar, SSize_t); |
| 1038 |
7 |
| SAVEFREEPV(cursors); |
| 1039 |
25 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
18 |
7 |
25 |
|
| 1040 |
18 |
| unsigned char wd = wire_disc[r]; |
| 1041 |
18 |
▶ | if (wd == 255) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wd == 255) { |
8 |
10 |
18 |
|
| 1042 |
8 |
| av_store(av, r, newSV(0)); |
| 1043 |
|
| } else { |
| 1044 |
10 |
| int decl = t->variant_wire_to_decl[wd]; |
| 1045 |
10 |
| SV **elem = av_fetch((AV *)SvRV(subcols[wd]), cursors[wd]++, 0); |
| 1046 |
10 |
| AV *pair = newAV(); |
| 1047 |
10 |
| av_extend(pair, 1); |
| 1048 |
10 |
| av_store(pair, 0, newSViv(decl)); |
| 1049 |
10 |
▶ | av_store(pair, 1, elem ? SvREFCNT_inc(*elem) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_store(pair, 1, elem ? SvREFCNT_inc(*elem) : newSV(0)); |
10 |
0 |
10 |
|
| 1050 |
10 |
| av_store(av, r, newRV_noinc((SV*)pair)); |
| 1051 |
|
| } |
| 1052 |
|
| } |
| 1053 |
7 |
| g.vec = NULL; |
| 1054 |
7 |
| g.vec_n = 0; |
| 1055 |
21 |
▶ | for (w = 0; w < nvar; w++) SvREFCNT_dec(subcols[w]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (w = 0; w < nvar; w++) SvREFCNT_dec(subcols[w]); |
14 |
7 |
21 |
|
| 1056 |
7 |
| Safefree(subcols); |
| 1057 |
7 |
| break; |
| 1058 |
|
| } |
| 1059 |
|
| |
| 1060 |
89 |
| case T_JSON: { |
| 1061 |
89 |
| DecodePrefix *state = (DecodePrefix *)t->decode_state; |
| 1062 |
89 |
| UV num_paths = state->num_paths, pi; |
| 1063 |
89 |
| char **paths = state->paths; |
| 1064 |
89 |
| STRLEN *path_lens = state->path_lens; |
| 1065 |
89 |
| int **path_kind_list = state->path_kind_list; |
| 1066 |
89 |
| int *path_kind_count = state->path_kind_count; |
| 1067 |
|
| |
| 1068 |
|
| /* Build result: AV of HV refs, one per row. */ |
| 1069 |
419 |
▶ | for (r = 0; r < nrows; r++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) |
330 |
89 |
419 |
|
| 1070 |
330 |
| av_store(av, r, newRV_noinc((SV*)newHV())); |
| 1071 |
|
| |
| 1072 |
|
| /* Typed path data (when t was declared as JSON(name Type, ...)): |
| 1073 |
|
| * the inner column data comes on the wire right after all |
| 1074 |
|
| * Dynamic prefixes and before any dynamic Variant data, in |
| 1075 |
|
| * name-sorted order. Decode each typed path's column, |
| 1076 |
|
| * distribute into per-row hashes by path name. */ |
| 1077 |
|
| { |
| 1078 |
|
| int tp; |
| 1079 |
108 |
▶ | for (tp = 0; tp < t->tuple_len; tp++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (tp = 0; tp < t->tuple_len; tp++) { |
19 |
89 |
108 |
|
| 1080 |
19 |
| SV *col_rv = decode_column_data(aTHX_ p, end, t->tuple[tp], |
| 1081 |
|
| nrows); |
| 1082 |
19 |
| g.inner = col_rv; |
| 1083 |
19 |
| AV *col_av = (AV*)SvRV(col_rv); |
| 1084 |
19 |
| STRLEN nlen = strlen(t->tuple_names[tp]); |
| 1085 |
56 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
37 |
19 |
56 |
|
| 1086 |
37 |
| SV **e = av_fetch(col_av, r, 0); |
| 1087 |
37 |
▶ | if (!e) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) continue; |
0 |
37 |
37 |
|
| 1088 |
37 |
| SV *row_rv = *av_fetch(av, r, 0); |
| 1089 |
37 |
| HV *row_hv = (HV*)SvRV(row_rv); |
| 1090 |
37 |
| hv_store(row_hv, t->tuple_names[tp], (I32)nlen, |
| 1091 |
|
| SvREFCNT_inc(*e), 0); |
| 1092 |
|
| } |
| 1093 |
19 |
| g.inner = NULL; |
| 1094 |
19 |
| SvREFCNT_dec(col_rv); |
| 1095 |
|
| } |
| 1096 |
|
| } |
| 1097 |
|
| |
| 1098 |
|
| /* Per-path Variant data: discs + per-variant values. */ |
| 1099 |
774 |
▶ | for (pi = 0; pi < num_paths; pi++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (pi = 0; pi < num_paths; pi++) { |
686 |
88 |
774 |
|
| 1100 |
|
| /* Read N disc bytes. */ |
| 1101 |
686 |
▶ | DEC_NEED((STRLEN)nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)nrows); |
0 |
686 |
686 |
|
| 1102 |
|
| unsigned char *discs; |
| 1103 |
686 |
| Newx(discs, nrows, unsigned char); |
| 1104 |
686 |
| SAVEFREEPV(discs); |
| 1105 |
8271 |
▶ | for (r = 0; r < nrows; r++) discs[r] = *(*p)++; |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) discs[r] = *(*p)++; |
7585 |
686 |
8271 |
|
| 1106 |
|
| |
| 1107 |
|
| /* Compute per-variant row counts and lex-position table. |
| 1108 |
|
| * The wire variant list has (kind_count + 1) entries (the |
| 1109 |
|
| * +1 is SharedVariant inserted at its lex position 7). |
| 1110 |
|
| * Rebuild the kind mask from the type-name list we just |
| 1111 |
|
| * parsed and reuse the same lex-table helper as encode. |
| 1112 |
|
| * |
| 1113 |
|
| * wire_slots must come from json_build_lex_table, not the |
| 1114 |
|
| * wire's count: the table holds at most JSON_LEX_SLOTS, so |
| 1115 |
|
| * a larger count indexes off the end. A mismatch means the |
| 1116 |
|
| * prefix listed a type twice. */ |
| 1117 |
686 |
| int nv = path_kind_count[pi]; |
| 1118 |
|
| int slot_to_kind_or_shared[JSON_LEX_SLOTS]; |
| 1119 |
|
| int wire_slots; |
| 1120 |
|
| { |
| 1121 |
686 |
| unsigned mask = 0; |
| 1122 |
|
| int i; |
| 1123 |
1439 |
▶ | for (i = 0; i < nv; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nv; i++) |
753 |
686 |
1439 |
|
| 1124 |
753 |
| mask |= 1u << path_kind_list[pi][i]; |
| 1125 |
686 |
| wire_slots = json_build_lex_table(mask, |
| 1126 |
|
| slot_to_kind_or_shared); |
| 1127 |
|
| } |
| 1128 |
686 |
▶ | if (wire_slots != nv + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (wire_slots != nv + 1) |
1 |
685 |
686 |
|
| 1129 |
1 |
| croak("decode JSON: path '%.*s' declares %d variant " |
| 1130 |
|
| "types but only %d are distinct (duplicate type " |
| 1131 |
|
| "names in Dynamic prefix)", |
| 1132 |
|
| (int)path_lens[pi], paths[pi], nv, wire_slots - 1); |
| 1133 |
|
| |
| 1134 |
|
| SSize_t *var_counts; |
| 1135 |
685 |
| Newxz(var_counts, wire_slots, SSize_t); |
| 1136 |
685 |
| SAVEFREEPV(var_counts); |
| 1137 |
|
| |
| 1138 |
8269 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
7584 |
685 |
8269 |
|
| 1139 |
7584 |
| unsigned char d = discs[r]; |
| 1140 |
7584 |
▶ | if (d == 0xff) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d == 0xff) continue; |
6880 |
704 |
7584 |
|
| 1141 |
704 |
▶ | if (d >= wire_slots) |
Branch: 1 branch
| Branch | True | False | Total |
| if (d >= wire_slots) |
0 |
704 |
704 |
|
| 1142 |
0 |
| croak("decode JSON: path '%.*s' disc %u out of range " |
| 1143 |
|
| "(wire_slots=%d)", |
| 1144 |
|
| (int)path_lens[pi], paths[pi], d, wire_slots); |
| 1145 |
704 |
| var_counts[d]++; |
| 1146 |
|
| } |
| 1147 |
|
| |
| 1148 |
|
| /* Decode each wire-slot's column data. SharedVariant |
| 1149 |
|
| * (kind=-1) is a String column on the wire, which our |
| 1150 |
|
| * encoder never populates (0 rows here in practice). */ |
| 1151 |
|
| AV **var_avs; |
| 1152 |
685 |
| Newxz(var_avs, wire_slots, AV*); |
| 1153 |
685 |
| SAVEFREEPV(var_avs); |
| 1154 |
|
| int slot; |
| 1155 |
2063 |
▶ | for (slot = 0; slot < wire_slots; slot++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (slot = 0; slot < wire_slots; slot++) { |
1378 |
685 |
2063 |
|
| 1156 |
1378 |
| SSize_t nv_rows = var_counts[slot]; |
| 1157 |
1378 |
| AV *sub = newAV(); |
| 1158 |
1378 |
| var_avs[slot] = sub; |
| 1159 |
1378 |
| sv_2mortal((SV*)sub); |
| 1160 |
1378 |
▶ | if (nv_rows > 0) av_extend(sub, nv_rows - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nv_rows > 0) av_extend(sub, nv_rows - 1); |
693 |
685 |
1378 |
|
| 1161 |
1378 |
| decode_dynamic_variant_slot(aTHX_ p, end, sub, |
| 1162 |
|
| slot_to_kind_or_shared[slot], nv_rows, "JSON"); |
| 1163 |
|
| } |
| 1164 |
|
| |
| 1165 |
|
| /* Distribute values into per-row hashes. */ |
| 1166 |
|
| SSize_t *cursors; |
| 1167 |
685 |
| Newxz(cursors, wire_slots, SSize_t); |
| 1168 |
685 |
| SAVEFREEPV(cursors); |
| 1169 |
8269 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
7584 |
685 |
8269 |
|
| 1170 |
7584 |
| unsigned char d = discs[r]; |
| 1171 |
7584 |
▶ | if (d == 0xff) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (d == 0xff) continue; |
6880 |
704 |
7584 |
|
| 1172 |
704 |
| SV **e = av_fetch(var_avs[d], cursors[d]++, 0); |
| 1173 |
704 |
▶ | if (!e) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e) continue; |
0 |
704 |
704 |
|
| 1174 |
704 |
| SV *row_rv = *av_fetch(av, r, 0); |
| 1175 |
704 |
| HV *row_hv = (HV*)SvRV(row_rv); |
| 1176 |
704 |
| SV *val = SvREFCNT_inc(*e); |
| 1177 |
704 |
| hv_store(row_hv, paths[pi], (I32)path_lens[pi], val, 0); |
| 1178 |
|
| } |
| 1179 |
|
| } |
| 1180 |
|
| |
| 1181 |
|
| /* Trailing shared data: N UInt64 offsets, then if final |
| 1182 |
|
| * offset > 0, offsets[N-1] key strings + value strings. |
| 1183 |
|
| * Only the last offset determines downstream parsing; skip |
| 1184 |
|
| * the rest with a single pointer bump. */ |
| 1185 |
88 |
| uint64_t last_offset = 0; |
| 1186 |
88 |
▶ | if (nrows > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nrows > 0) { |
88 |
0 |
88 |
|
| 1187 |
88 |
▶ | DEC_NEED((STRLEN)(8 * nrows)); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)(8 * nrows)); |
0 |
88 |
88 |
|
| 1188 |
88 |
| last_offset = dec_le64(*p + 8 * (nrows - 1)); |
| 1189 |
88 |
| *p += 8 * nrows; |
| 1190 |
|
| } |
| 1191 |
88 |
▶ | if (last_offset > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_offset > 0) { |
0 |
88 |
88 |
|
| 1192 |
|
| /* Each string is a length varint + bytes (>= 1 byte |
| 1193 |
|
| * total). A corrupted last_offset must not let us spin |
| 1194 |
|
| * calling dec_lenpfx_string 2^32 times before each one |
| 1195 |
|
| * croaks on the truncated buffer. */ |
| 1196 |
0 |
▶ | if (last_offset > (uint64_t)(end - *p)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (last_offset > (uint64_t)(end - *p)) |
0 |
0 |
0 |
|
| 1197 |
0 |
| croak("decode JSON: shared-data last_offset=%lu " |
| 1198 |
|
| "exceeds remaining buffer (%lu bytes)", |
| 1199 |
|
| (unsigned long)last_offset, |
| 1200 |
|
| (unsigned long)(end - *p)); |
| 1201 |
|
| uint64_t i; |
| 1202 |
0 |
▶ | for (i = 0; i < last_offset; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < last_offset; i++) { |
0 |
0 |
0 |
|
| 1203 |
|
| const char *s; STRLEN l; |
| 1204 |
0 |
| dec_lenpfx_string(aTHX_ p, end, &s, &l); |
| 1205 |
|
| } |
| 1206 |
0 |
▶ | for (i = 0; i < last_offset; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < last_offset; i++) { |
0 |
0 |
0 |
|
| 1207 |
|
| const char *s; STRLEN l; |
| 1208 |
0 |
| dec_lenpfx_string(aTHX_ p, end, &s, &l); |
| 1209 |
|
| } |
| 1210 |
|
| } |
| 1211 |
|
| |
| 1212 |
|
| /* Unflatten dotted-path keys into nested hashes. Symmetric to the |
| 1213 |
|
| * encoder which flattens nested hashrefs into dotted paths on the |
| 1214 |
|
| * wire. Collision-safe: if an intermediate hop is already a |
| 1215 |
|
| * non-HV (some path emitted a leaf at "a" while another emitted |
| 1216 |
|
| * "a.b"), the dotted form is left intact at the top level. */ |
| 1217 |
417 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
329 |
88 |
417 |
|
| 1218 |
329 |
| SV *row_rv = *av_fetch(av, r, 0); |
| 1219 |
329 |
| HV *row_hv = (HV*)SvRV(row_rv); |
| 1220 |
|
| /* Snapshot keys: we may mutate row_hv during iteration. */ |
| 1221 |
329 |
| AV *keys = (AV*)sv_2mortal((SV*)newAV()); |
| 1222 |
329 |
| hv_iterinit(row_hv); |
| 1223 |
|
| HE *he; |
| 1224 |
1070 |
▶ | while ((he = hv_iternext(row_hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(row_hv))) { |
741 |
329 |
1070 |
|
| 1225 |
|
| I32 klen; |
| 1226 |
741 |
| char *kstr = hv_iterkey(he, &klen); |
| 1227 |
741 |
▶ | if (memchr(kstr, '.', klen)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memchr(kstr, '.', klen)) |
284 |
457 |
741 |
|
| 1228 |
284 |
| av_push(keys, newSVpvn(kstr, klen)); |
| 1229 |
|
| } |
| 1230 |
329 |
| SSize_t nk = av_len(keys) + 1; |
| 1231 |
|
| SSize_t ki; |
| 1232 |
613 |
▶ | for (ki = 0; ki < nk; ki++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (ki = 0; ki < nk; ki++) { |
284 |
329 |
613 |
|
| 1233 |
284 |
| SV *ksv = *av_fetch(keys, ki, 0); |
| 1234 |
|
| STRLEN klen; |
| 1235 |
284 |
| const char *kstr = SvPV(ksv, klen); |
| 1236 |
|
| /* Walk dotted segments. */ |
| 1237 |
284 |
| HV *cur = row_hv; |
| 1238 |
284 |
| STRLEN seg_start = 0, off; |
| 1239 |
284 |
| int conflict = 0; |
| 1240 |
203062 |
▶ | for (off = 0; off <= klen; off++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (off = 0; off <= klen; off++) { |
202798 |
264 |
203062 |
|
| 1241 |
202798 |
▶ | if (off == klen || kstr[off] == '.') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (off == klen || kstr[off] == '.') { |
202526 |
272 |
202798 |
| if (off == klen || kstr[off] == '.') { |
100381 |
102145 |
202526 |
|
| 1242 |
100653 |
| const char *seg = kstr + seg_start; |
| 1243 |
100653 |
| STRLEN slen = off - seg_start; |
| 1244 |
100653 |
▶ | if (off == klen) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (off == klen) { |
272 |
100381 |
100653 |
|
| 1245 |
|
| /* Final segment: move value here. */ |
| 1246 |
272 |
| SV **leaf = hv_fetch(row_hv, kstr, |
| 1247 |
|
| (I32)klen, 0); |
| 1248 |
272 |
▶ | if (!leaf) { conflict = 1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!leaf) { conflict = 1; break; } |
0 |
272 |
272 |
|
| 1249 |
272 |
▶ | if (hv_exists(cur, seg, (I32)slen)) { conflict = 1; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(cur, seg, (I32)slen)) { conflict = 1; break; } |
8 |
264 |
272 |
|
| 1250 |
264 |
| SV *val = SvREFCNT_inc(*leaf); |
| 1251 |
264 |
▶ | if (!hv_store(cur, seg, (I32)slen, |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_store(cur, seg, (I32)slen, |
0 |
264 |
264 |
|
| 1252 |
|
| val, 0)) { |
| 1253 |
0 |
| SvREFCNT_dec(val); |
| 1254 |
0 |
| conflict = 1; |
| 1255 |
|
| } |
| 1256 |
|
| } else { |
| 1257 |
100381 |
| SV **next = hv_fetch(cur, seg, (I32)slen, |
| 1258 |
|
| 0); |
| 1259 |
|
| HV *next_hv; |
| 1260 |
100381 |
▶ | if (next && SvROK(*next) |
Branch: 2 branches
| Branch | True | False | Total |
| if (next && SvROK(*next) |
208 |
100173 |
100381 |
| if (next && SvROK(*next) |
196 |
12 |
208 |
|
| 1261 |
196 |
▶ | && SvTYPE(SvRV(*next)) == SVt_PVHV) { |
Branch: 1 branch
| Branch | True | False | Total |
| && SvTYPE(SvRV(*next)) == SVt_PVHV) { |
196 |
0 |
196 |
|
| 1262 |
196 |
| next_hv = (HV*)SvRV(*next); |
| 1263 |
100185 |
▶ | } else if (next) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (next) { |
12 |
100173 |
100185 |
|
| 1264 |
12 |
| conflict = 1; |
| 1265 |
12 |
| break; |
| 1266 |
|
| } else { |
| 1267 |
100173 |
| next_hv = newHV(); |
| 1268 |
100173 |
| hv_store(cur, seg, (I32)slen, |
| 1269 |
|
| newRV_noinc((SV*)next_hv), 0); |
| 1270 |
|
| } |
| 1271 |
100369 |
| cur = next_hv; |
| 1272 |
|
| } |
| 1273 |
100633 |
| seg_start = off + 1; |
| 1274 |
|
| } |
| 1275 |
|
| } |
| 1276 |
284 |
▶ | if (!conflict) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!conflict) |
264 |
20 |
284 |
|
| 1277 |
264 |
| hv_delete(row_hv, kstr, (I32)klen, G_DISCARD); |
| 1278 |
|
| } |
| 1279 |
|
| } |
| 1280 |
88 |
| break; |
| 1281 |
|
| } |
| 1282 |
|
| |
| 1283 |
12 |
| case T_DYNAMIC: { |
| 1284 |
12 |
| DecodePrefix *state = (DecodePrefix *)t->decode_state; |
| 1285 |
12 |
| UV ntypes = state->ntypes; |
| 1286 |
12 |
| int *kinds_in_order = state->kinds_in_order; |
| 1287 |
12 |
| int nv = (int)ntypes; |
| 1288 |
12 |
| unsigned mask = 0; |
| 1289 |
|
| int i; |
| 1290 |
303 |
▶ | for (i = 0; i < nv; i++) mask |= 1u << kinds_in_order[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < nv; i++) mask |= 1u << kinds_in_order[i]; |
291 |
12 |
303 |
|
| 1291 |
|
| |
| 1292 |
|
| /* From the lex table, not the wire's ntypes - see T_JSON. */ |
| 1293 |
|
| int slot_to_kind[JSON_LEX_SLOTS]; |
| 1294 |
12 |
| int wire_slots = json_build_lex_table(mask, slot_to_kind); |
| 1295 |
12 |
▶ | if (wire_slots != nv + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (wire_slots != nv + 1) |
3 |
9 |
12 |
|
| 1296 |
3 |
| croak("decode Dynamic: %d variant types declared but only " |
| 1297 |
|
| "%d are distinct (duplicate type names in prefix)", |
| 1298 |
|
| nv, wire_slots - 1); |
| 1299 |
|
| |
| 1300 |
9 |
▶ | DEC_NEED((STRLEN)nrows); |
Branch: 1 branch
| Branch | True | False | Total |
| DEC_NEED((STRLEN)nrows); |
0 |
9 |
9 |
|
| 1301 |
|
| unsigned char *discs; |
| 1302 |
9 |
| Newx(discs, nrows, unsigned char); |
| 1303 |
9 |
| SAVEFREEPV(discs); |
| 1304 |
37 |
▶ | for (r = 0; r < nrows; r++) discs[r] = *(*p)++; |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) discs[r] = *(*p)++; |
28 |
9 |
37 |
|
| 1305 |
|
| |
| 1306 |
|
| SSize_t *var_counts; |
| 1307 |
9 |
| Newxz(var_counts, wire_slots, SSize_t); |
| 1308 |
9 |
| SAVEFREEPV(var_counts); |
| 1309 |
37 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
28 |
9 |
37 |
|
| 1310 |
28 |
▶ | if (discs[r] == 0xff) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (discs[r] == 0xff) continue; |
6 |
22 |
28 |
|
| 1311 |
22 |
▶ | if (discs[r] >= wire_slots) |
Branch: 1 branch
| Branch | True | False | Total |
| if (discs[r] >= wire_slots) |
0 |
22 |
22 |
|
| 1312 |
0 |
| croak("decode Dynamic: disc %u out of range " |
| 1313 |
|
| "(wire_slots=%d)", discs[r], wire_slots); |
| 1314 |
22 |
| var_counts[discs[r]]++; |
| 1315 |
|
| } |
| 1316 |
|
| |
| 1317 |
|
| AV **var_avs; |
| 1318 |
9 |
| Newxz(var_avs, wire_slots, AV*); |
| 1319 |
9 |
| SAVEFREEPV(var_avs); |
| 1320 |
|
| int slot; |
| 1321 |
39 |
▶ | for (slot = 0; slot < wire_slots; slot++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (slot = 0; slot < wire_slots; slot++) { |
30 |
9 |
39 |
|
| 1322 |
30 |
| SSize_t nv_rows = var_counts[slot]; |
| 1323 |
30 |
| AV *sub = newAV(); |
| 1324 |
30 |
| var_avs[slot] = sub; |
| 1325 |
30 |
| sv_2mortal((SV*)sub); |
| 1326 |
30 |
▶ | if (nv_rows > 0) av_extend(sub, nv_rows - 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (nv_rows > 0) av_extend(sub, nv_rows - 1); |
21 |
9 |
30 |
|
| 1327 |
30 |
| decode_dynamic_variant_slot(aTHX_ p, end, sub, |
| 1328 |
|
| slot_to_kind[slot], nv_rows, "Dynamic"); |
| 1329 |
|
| } |
| 1330 |
|
| |
| 1331 |
|
| SSize_t *cursors; |
| 1332 |
9 |
| Newxz(cursors, wire_slots, SSize_t); |
| 1333 |
9 |
| SAVEFREEPV(cursors); |
| 1334 |
37 |
▶ | for (r = 0; r < nrows; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < nrows; r++) { |
28 |
9 |
37 |
|
| 1335 |
28 |
| unsigned char d = discs[r]; |
| 1336 |
28 |
▶ | if (d == 0xff) { av_store(av, r, newSV(0)); continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (d == 0xff) { av_store(av, r, newSV(0)); continue; } |
6 |
22 |
28 |
|
| 1337 |
22 |
| SV **e = av_fetch(var_avs[d], cursors[d]++, 0); |
| 1338 |
22 |
▶ | av_store(av, r, e ? SvREFCNT_inc(*e) : newSV(0)); |
Branch: 1 branch
| Branch | True | False | Total |
| av_store(av, r, e ? SvREFCNT_inc(*e) : newSV(0)); |
22 |
0 |
22 |
|
| 1339 |
|
| } |
| 1340 |
9 |
| break; |
| 1341 |
|
| } |
| 1342 |
|
| |
| 1343 |
0 |
| default: |
| 1344 |
0 |
| croak("decode: unhandled type code %d", t->code); |
| 1345 |
|
| } |
| 1346 |
|
| |
| 1347 |
|
| /* T_MAP already produced `result`; disarm so the guard is a no-op. */ |
| 1348 |
1644 |
▶ | if (!result) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!result) { |
1635 |
9 |
1644 |
|
| 1349 |
1635 |
| g.av = NULL; |
| 1350 |
1635 |
| result = newRV_noinc((SV *)av); |
| 1351 |
|
| } |
| 1352 |
1644 |
| LEAVE; |
| 1353 |
1644 |
| return result; |
| 1354 |
|
| } |
| 1355 |
|
| |
| 1356 |
|
| #undef DEC_NEED |
| 1357 |
|
| #undef DEC_SCALAR_LOOP |