srl_reader_skip.h

filter: Statementstmt 11.1% 3 / 27 Branchbran 3.5% 1 / 28 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 7.2% 4 / 55 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srl_reader_checksum.h ↑ Summary srl_checksum_policy.h »
1 #ifndef SRL_READER_SKIP_H_
2 #define SRL_READER_SKIP_H_
3  
4 /* Stepping over self-contained values: tags that carry their whole
5 * payload inline, with no nested values and no back-reference. Used by
6 * the tools that walk a Sereal body without decoding it (Sereal::Merger,
7 * Sereal::Splitter, Sereal::Path::Iterator), so that each of them agrees
8 * with the decoder on payload sizes and on which protocol version
9 * introduced a tag. */
10  
11 #include "srl_inline.h"
12 #include "srl_common.h"
13 #include "srl_reader.h"
14 #include "srl_reader_error.h"
15 #include "srl_reader_varint.h"
16 #include "srl_protocol.h"
17 #include "srl_float.h"
18  
19 /* Payload sizes, as the decoder reads them. LONG_DOUBLE is
20 * platform-dependent, like the decoder's sizeof(long double) read. */
21 #define SRL_FLOAT_SIZE 4
22 #define SRL_DOUBLE_SIZE 8
23 #define SRL_LONG_DOUBLE_SIZE sizeof(long double)
24 #define SRL_FLOAT_128_SIZE 16
25  
26 #define SRL_RDR_ASSERT_PROTOCOL(buf, protocol_version, min_version, tag_name) STMT_START { \
27 if (expect_false((protocol_version) < (min_version))) \
28 SRL_RDR_ERRORf1((buf), tag_name " tag requires protocol version %u or later", \
29 (unsigned int)(min_version)); \
30 } STMT_END
31  
32 /* Declarations establish the interpretation of the document before any
33 * value is read. Processing one contiguous prefix gives the rest of the
34 * decoder a single, immutable interpretation of native LONG_DOUBLE bytes.
35 * The bounded copy of the reader prevents a malformed descriptor from
36 * consuming bytes belonging to the next declaration or the root value.
37 *
38 * An unknown declaration cannot be skipped safely even though its length
39 * is known: it may change the meaning of subsequent values. A known native
40 * layout is validated here before any object bytes are copied. Unidentified
41 * representations instead require explicit caller trust in ABI compatibility;
42 * their storage size is still checked against the object the reader copies. */
43 SRL_STATIC_INLINE void
44 243 srl_read_declarations(pTHX_ srl_reader_buffer_t *buf, U8 version, int allow_unknown_long_double)
45 {
46 243 buf->long_double_size = 0;
47 243 ▶ if (version < 7) return;
Branch: 1 branch
BranchTrueFalseTotal
if (version < 7) return; 243 0 243
48 0 while (SRL_RDR_SPACE_LEFT(buf) >= 2
49 0 ▶ && buf->pos[0] == SRL_HDR_EXTEND && buf->pos[1] == SRL_EXT_DECLARE) {
Branch: 3 branches
BranchTrueFalseTotal
&& buf->pos[0] == SRL_HDR_EXTEND && buf->pos[1] == SRL_EXT_DECLARE) { 0 0 0
&& buf->pos[0] == SRL_HDR_EXTEND && buf->pos[1] == SRL_EXT_DECLARE) { 0 0 0
&& buf->pos[0] == SRL_HDR_EXTEND && buf->pos[1] == SRL_EXT_DECLARE) { 0 0 0
50 UV kind, len, native_size;
51 U8 format, reader_format;
52 int unknown_layout;
53 srl_reader_buffer_t declaration;
54 0 buf->pos += 2;
55 0 len = srl_read_varint_uv_length(aTHX_ buf, " while reading declaration");
56 0 declaration = *buf;
57 0 declaration.end = buf->pos + len;
58 0 kind = srl_read_varint_uv(aTHX_ &declaration);
59 0 ▶ if (kind != SRL_DECL_LONG_DOUBLE)
Branch: 1 branch
BranchTrueFalseTotal
if (kind != SRL_DECL_LONG_DOUBLE) 0 0 0
60 0 SRL_RDR_ERRORf1(&declaration, "Unknown declaration kind %"UVuf, kind);
61 0 ▶ if (buf->long_double_size)
Branch: 1 branch
BranchTrueFalseTotal
if (buf->long_double_size) 0 0 0
62 0 SRL_RDR_ERRORf1(&declaration, "Duplicate %s declaration", SRL_DECL_NAME(kind));
63 0 ▶ if (SRL_RDR_SPACE_LEFT(&declaration) != 2)
Branch: 1 branch
BranchTrueFalseTotal
if (SRL_RDR_SPACE_LEFT(&declaration) != 2) 0 0 0
64 0 SRL_RDR_ERRORf1(&declaration, "Invalid %s declaration length", SRL_DECL_NAME(kind));
65 0 format = *declaration.pos++;
66 0 native_size = *declaration.pos++;
67 0 reader_format = srl_native_long_double_format();
68 0 ▶ unknown_layout = !reader_format || !srl_long_double_format_is_known(format);
Branch: 2 branches
BranchTrueFalseTotal
unknown_layout = !reader_format || !srl_long_double_format_is_known(format); 0 0 0
unknown_layout = !reader_format || !srl_long_double_format_is_known(format); 0 0 0
69 /* Size is always enforced because the value reader copies one local
70 * C long-double object. Caller trust can bridge an unidentified ABI,
71 * but cannot override a known representation/byte-order mismatch. */
72 0 ▶ if (native_size != sizeof(long double)
Branch: 1 branch
BranchTrueFalseTotal
if (native_size != sizeof(long double) 0 0 0
73 0 ▶ || ((format != reader_format || !reader_format)
Branch: 2 branches
BranchTrueFalseTotal
|| ((format != reader_format || !reader_format) 0 0 0
|| ((format != reader_format || !reader_format) 0 0 0
74 0 ▶ && !(allow_unknown_long_double && unknown_layout)))
Branch: 2 branches
BranchTrueFalseTotal
&& !(allow_unknown_long_double && unknown_layout))) 0 0 0
&& !(allow_unknown_long_double && unknown_layout))) 0 0 0
75 0 SRL_RDR_ERRORf4(&declaration,
76 "LONG_DOUBLE format mismatch (packet format %u, size %"UVuf"; reader format %u, size %"UVuf")",
77 (unsigned int)format, native_size,
78 (unsigned int)reader_format, (UV)sizeof(long double));
79 0 buf->long_double_size = native_size;
80 0 buf->pos = declaration.end;
81 }
82 0 buf->body_start = buf->pos;
83 }
84  
85 /* Read the magnitude of a NEG_VARINT (buf->pos just past the tag) and
86 * validate it like the decoder does. */
87 SRL_STATIC_INLINE UV
88 srl_read_neg_varint_magnitude(pTHX_ srl_reader_buffer_t *buf)
89 {
90 UV magnitude = srl_read_varint_uv(aTHX_ buf);
91 if (expect_false(magnitude == 0 || magnitude > (UV)IV_MAX + 1))
92 SRL_RDR_ERRORf1(buf, "Invalid NEG_VARINT magnitude %"UVuf, magnitude);
93 return magnitude;
94 }
95  
96 /* Read an EXTEND tag's extended-tag ID and the length of its payload
97 * (buf->pos just past the EXTEND tag), leaving buf->pos at the payload.
98 * UNDEF_DEADLY has no payload and is an ordinary undef when reading.
99 * Only v-strings have a payload; unknown IDs are rejected. */
100 SRL_STATIC_INLINE UV
101 srl_read_extend_header(pTHX_ srl_reader_buffer_t *buf, U8 protocol_version, U8 *ext_tag_out)
102 {
103 U8 ext_tag;
104 SRL_RDR_ASSERT_PROTOCOL(buf, protocol_version, 6, "EXTEND");
105 SRL_RDR_ASSERT_SPACE(buf, 1, " while reading extended tag ID");
106 ext_tag = *buf->pos++;
107 if (ext_tag == SRL_EXT_DECLARE) {
108 SRL_RDR_ASSERT_PROTOCOL(buf, protocol_version, 7, "DECLARE");
109 SRL_RDR_ERROR(buf, "Declaration outside document prefix");
110 }
111 if (ext_tag == SRL_EXT_UNDEF_DEADLY) {
112 if (ext_tag_out)
113 *ext_tag_out = ext_tag;
114 return 0;
115 }
116 if (ext_tag != SRL_EXT_VSTRING_BINARY && ext_tag != SRL_EXT_VSTRING_UTF8)
117 SRL_RDR_ERRORf1(buf, "Unknown extended tag ID %u", (unsigned int)ext_tag);
118 if (ext_tag_out)
119 *ext_tag_out = ext_tag;
120 return srl_read_varint_uv_length(aTHX_ buf, " while reading extended VSTRING");
121 }
122  
123 /* buf->pos points just past tag, which has its track flag removed. If
124 * tag is a self-contained value, step over its payload and return 1;
125 * otherwise return 0 and leave buf untouched. Croaks on truncated
126 * payloads and on tags that protocol_version does not allow. Rewriters set
127 * forbid_undef_deadly because compliant encoders must not emit that tag. */
128 SRL_STATIC_INLINE int
129 srl_reader_skip_scalar(pTHX_ srl_reader_buffer_t *buf, U8 tag, U8 protocol_version, int forbid_undef_deadly)
130 {
131 UV len;
132  
133 if (tag <= SRL_HDR_NEG_HIGH)
134 return 1;
135  
136 if (tag >= SRL_HDR_SHORT_BINARY_LOW) {
137 len = tag & SRL_MASK_SHORT_BINARY_LEN;
138 SRL_RDR_ASSERT_SPACE(buf, len, " while reading SHORT_BINARY");
139 buf->pos += len;
140 return 1;
141 }
142  
143 switch (tag) {
144 case SRL_HDR_VARINT:
145 case SRL_HDR_ZIGZAG:
146 srl_skip_varint(aTHX_ buf);
147 return 1;
148  
149 case SRL_HDR_NEG_VARINT:
150 SRL_RDR_ASSERT_PROTOCOL(buf, protocol_version, 6, "NEG_VARINT");
151 (void)srl_read_neg_varint_magnitude(aTHX_ buf);
152 return 1;
153  
154 case SRL_HDR_FLOAT: len = SRL_FLOAT_SIZE; break;
155 case SRL_HDR_DOUBLE: len = SRL_DOUBLE_SIZE; break;
156 case SRL_HDR_LONG_DOUBLE:
157 if (protocol_version >= 7 && !buf->long_double_size)
158 SRL_RDR_ERROR(buf, "LONG_DOUBLE requires a declaration");
159 len = protocol_version >= 7 ? buf->long_double_size : SRL_LONG_DOUBLE_SIZE;
160 break;
161 case SRL_HDR_FLOAT_128:
162 SRL_RDR_ASSERT_PROTOCOL(buf, protocol_version, 5, "FLOAT_128");
163 len = SRL_FLOAT_128_SIZE;
164 break;
165  
166 case SRL_HDR_NO:
167 case SRL_HDR_YES:
168 SRL_RDR_ASSERT_PROTOCOL(buf, protocol_version, 5, "NO/YES");
169 return 1;
170  
171 case SRL_HDR_CANONICAL_UNDEF:
172 SRL_RDR_ASSERT_PROTOCOL(buf, protocol_version, 3, "CANONICAL_UNDEF");
173 return 1;
174  
175 case SRL_HDR_TRUE:
176 case SRL_HDR_FALSE:
177 case SRL_HDR_UNDEF:
178 return 1;
179  
180 case SRL_HDR_BINARY:
181 case SRL_HDR_STR_UTF8:
182 len = srl_read_varint_uv_length(aTHX_ buf, " while reading BINARY or STR_UTF8");
183 buf->pos += len;
184 return 1;
185  
186 case SRL_HDR_EXTEND: {
187 U8 ext_tag;
188 len = srl_read_extend_header(aTHX_ buf, protocol_version, &ext_tag);
189 if (forbid_undef_deadly && ext_tag == SRL_EXT_UNDEF_DEADLY)
190 SRL_RDR_ERROR(buf, "Cannot emit UNDEF_DEADLY extended tag");
191 buf->pos += len;
192 return 1;
193 }
194  
195 default:
196 return 0;
197 }
198  
199 SRL_RDR_ASSERT_SPACE(buf, len, " while reading a floating point value");
200 buf->pos += len;
201 return 1;
202 }
203  
204 #endif
« srl_reader_checksum.h ↑ Summary srl_checksum_policy.h »