srl_reader_varint.h

filter: Statementstmt 59.0% 26 / 44 Branchbran 30.7% 16 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 43.7% 42 / 96 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« srl_reader_decompress.h ↑ Summary srl_reader_misc.h »
1 #ifndef SRL_READER_VARINT_H_
2 #define SRL_READER_VARINT_H_
3  
4 #include "srl_inline.h"
5 #include "srl_common.h"
6 #include "srl_reader.h"
7 #include "srl_reader_error.h"
8  
9 SRL_STATIC_INLINE void
10 srl_skip_varint(pTHX_ srl_reader_buffer_t *buf)
11 {
12 U8 max_varint_len = sizeof(UV) == sizeof(U32) ? 5 : 10;
13 while (SRL_RDR_NOT_DONE(buf) && *buf->pos & 0x80) {
14 buf->pos++;
15 if (!max_varint_len--)
16 SRL_RDR_ERROR(buf, "varint too long");
17 }
18  
19 if (expect_false( SRL_RDR_DONE(buf) ))
20 SRL_RDR_ERROR(buf, "end of packet reached before varint parsed");
21  
22 if (expect_false( !max_varint_len ))
23 SRL_RDR_ERROR(buf, "varint too long");
24  
25 /* The final byte of a maximum-length ULEB128 carries only the bits
26 * remaining in UV. Reject values that would overflow when shifted. */
27 if (expect_false( max_varint_len == 1
28 && (sizeof(UV) == sizeof(U32) ? *buf->pos > 0x0F : *buf->pos > 0x01) ))
29 SRL_RDR_ERROR(buf, "varint too big");
30  
31 buf->pos++;
32 }
33  
34 SRL_STATIC_INLINE UV
35 srl_varint_length(pTHX_ UV value)
36 {
37 UV length = 0;
38 while (value >= 0x80) {
39 length++;
40 value >>= 7;
41 }
42  
43 return ++length;
44 }
45  
46 SRL_STATIC_INLINE UV
47 238 srl_read_varint_uv_safe(pTHX_ srl_reader_buffer_t *buf)
48 {
49 238 UV uv= 0;
50 238 unsigned int lshift= 0;
51  
52 245 ▶ while (SRL_RDR_NOT_DONE(buf) && *buf->pos & 0x80) {
Branch: 2 branches
BranchTrueFalseTotal
while (SRL_RDR_NOT_DONE(buf) && *buf->pos & 0x80) { 245 0 245
while (SRL_RDR_NOT_DONE(buf) && *buf->pos & 0x80) { 7 238 245
53 7 uv |= ((UV)(*buf->pos++ & 0x7F) << lshift);
54 7 lshift += 7;
55 7 ▶ if (lshift > (sizeof(UV) * 8))
Branch: 1 branch
BranchTrueFalseTotal
if (lshift > (sizeof(UV) * 8)) 0 7 7
56 0 SRL_RDR_ERROR(buf, "varint too big");
57 }
58  
59 238 ▶ if (expect_true( SRL_RDR_NOT_DONE(buf) )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true( SRL_RDR_NOT_DONE(buf) )) { 238 0 238
60 238 const U8 terminal = *buf->pos;
61 238 ▶ if (expect_false( lshift >= (sizeof(UV) * 8)
Branch: 3 branches
BranchTrueFalseTotal
if (expect_false( lshift >= (sizeof(UV) * 8) 238 0 238
if (expect_false( lshift >= (sizeof(UV) * 8) 0 238 238
if (expect_false( lshift >= (sizeof(UV) * 8) 0 238 238
62 || (UV)terminal > (UV_MAX >> lshift) ))
63 0 SRL_RDR_ERROR(buf, "varint too big");
64 238 uv |= ((UV)terminal << lshift);
65 238 buf->pos++;
66 } else {
67 0 SRL_RDR_ERROR(buf, "end of packet reached before varint parsed");
68 }
69  
70 238 return uv;
71 }
72  
73 #define SET_UV_FROM_VARINT(buf, uv, ptr) STMT_START { \
74 U32 b; \
75 \
76 /* Splitting into 32-bit pieces gives better performance on 32-bit \
77 processors. */ \
78 U32 part0 = 0, part1 = 0, part2 = 0; \
79 do { \
80 \
81 b = *(ptr++); part0 = b ; if (!(b & 0x80)) break; \
82 part0 -= 0x80; \
83 b = *(ptr++); part0 += b << 7; if (!(b & 0x80)) break; \
84 part0 -= 0x80 << 7; \
85 b = *(ptr++); part0 += b << 14; if (!(b & 0x80)) break; \
86 part0 -= 0x80 << 14; \
87 b = *(ptr++); part0 += b << 21; if (!(b & 0x80)) break; \
88 part0 -= 0x80 << 21; \
89 \
90 b = *(ptr++); part1 = b ; if (!(b & 0x80)) break; \
91 part1 -= 0x80; \
92 b = *(ptr++); part1 += b << 7; if (!(b & 0x80)) break; \
93 part1 -= 0x80 << 7; \
94 b = *(ptr++); part1 += b << 14; if (!(b & 0x80)) break; \
95 part1 -= 0x80 << 14; \
96 b = *(ptr++); part1 += b << 21; if (!(b & 0x80)) break; \
97 part1 -= 0x80 << 21; \
98 \
99 b = *(ptr++); part2 = b ; if (!(b & 0x80)) break; \
100 part2 -= 0x80; \
101 b = *(ptr++); part2 += b << 7; \
102 if (!(b & 0x80)) { \
103 if (expect_false( b > 1 )) \
104 SRL_RDR_ERROR(buf, "varint too big"); \
105 break; \
106 } \
107 /* "part2 -= 0x80 << 7" is irrelevant because (0x80 << 7) << 56 is 0. */\
108 \
109 /* We have overrun the maximum size of a varint (10 bytes). The data \
110 must be corrupt. */ \
111 SRL_RDR_ERROR(buf, "varint not terminated in time, corrupt packet"); \
112 \
113 } while (0); \
114 \
115 uv= (((UV)part0) ) | \
116 (((UV)part1) << 28) | \
117 (((UV)part2) << 56); \
118 \
119 } STMT_END
120  
121 SRL_STATIC_INLINE UV
122 srl_read_varint_uv_nocheck(pTHX_ srl_reader_buffer_t *buf)
123 {
124 UV uv= 0;
125 unsigned int lshift= 0;
126  
127 while (*buf->pos & 0x80) {
128 uv |= ((UV)(*buf->pos++ & 0x7F) << lshift);
129 lshift += 7;
130 if (expect_false( lshift > (sizeof(UV) * 8) ))
131 SRL_RDR_ERROR(buf, "varint too big");
132 }
133  
134 {
135 const U8 terminal = *buf->pos;
136 if (expect_false( lshift >= (sizeof(UV) * 8)
137 || (UV)terminal > (UV_MAX >> lshift) ))
138 SRL_RDR_ERROR(buf, "varint too big");
139 uv |= ((UV)terminal << lshift);
140 buf->pos++;
141 }
142 return uv;
143 }
144  
145 SRL_STATIC_INLINE UV
146 srl_read_varint_u32_nocheck(pTHX_ srl_reader_buffer_t *buf)
147 {
148 const U8* ptr = buf->pos;
149 U32 b;
150 U32 part0 = 0;
151  
152 b = *(ptr++); part0 = b ; if (!(b & 0x80)) goto done;
153 part0 -= 0x80;
154 b = *(ptr++); part0 += b << 7; if (!(b & 0x80)) goto done;
155 part0 -= 0x80 << 7;
156 b = *(ptr++); part0 += b << 14; if (!(b & 0x80)) goto done;
157 part0 -= 0x80 << 14;
158 b = *(ptr++); part0 += b << 21; if (!(b & 0x80)) goto done;
159 part0 -= 0x80 << 21;
160 b = *(ptr++); part0 += b << 28; if (b < 16) goto done;
161  
162 SRL_RDR_ERROR(buf, "varint overflows U32, cannot restore packet");
163  
164 done:
165 buf->pos= (U8*)ptr;
166  
167 return part0;
168 }
169  
170 #if UVSIZE > 4
171 SRL_STATIC_INLINE UV
172 16 srl_read_varint_u64_nocheck(pTHX_ srl_reader_buffer_t *buf)
173 {
174 UV uv;
175 16 const U8* ptr = buf->pos;
176  
177 16 ▶ SET_UV_FROM_VARINT(buf, uv, ptr);
Branch: 11 branches
BranchTrueFalseTotal
SET_UV_FROM_VARINT(buf, uv, ptr); 13 3 16
SET_UV_FROM_VARINT(buf, uv, ptr); 3 0 3
SET_UV_FROM_VARINT(buf, uv, ptr); 0 0 0
SET_UV_FROM_VARINT(buf, uv, ptr); 0 0 0
SET_UV_FROM_VARINT(buf, uv, ptr); 0 0 0
SET_UV_FROM_VARINT(buf, uv, ptr); 0 0 0
SET_UV_FROM_VARINT(buf, uv, ptr); 0 0 0
SET_UV_FROM_VARINT(buf, uv, ptr); 0 0 0
SET_UV_FROM_VARINT(buf, uv, ptr); 0 0 0
SET_UV_FROM_VARINT(buf, uv, ptr); 0 0 0
SET_UV_FROM_VARINT(buf, uv, ptr); 0 0 0
178  
179 16 buf->pos= (U8*)ptr;
180  
181 16 return uv;
182 }
183 #endif
184  
185 SRL_STATIC_INLINE UV
186 254 srl_read_varint_uv(pTHX_ srl_reader_buffer_t *buf)
187 {
188 /* We can use the unrolled-loop version of varint decoding
189 * IFF there is certainly enough space in the input buffer
190 * even for the longest possible varint (11 bytes, see
191 * SRL_MAX_VARINT_LENGTH). Otherwise, use the safe byte-by-byte
192 * reader which bounds-checks each byte against buf->end. */
193 254 ▶ if (expect_true( buf->end - buf->pos >= SRL_MAX_VARINT_LENGTH )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_true( buf->end - buf->pos >= SRL_MAX_VARINT_LENGTH )) { 16 238 254
194 #if UVSIZE > 4
195 16 return srl_read_varint_u64_nocheck(aTHX_ buf);
196 #else
197 return srl_read_varint_u32_nocheck(aTHX_ buf);
198 #endif
199 } else {
200 238 return srl_read_varint_uv_safe(aTHX_ buf);
201 }
202 }
203  
204 /* buf->pos must be just past the tag (COPY, REFP, ALIAS, OBJECTV) whose
205 * offset is read, so that the tag is at buf->pos - 1 */
206 SRL_STATIC_INLINE UV
207 0 srl_read_varint_uv_offset(pTHX_ srl_reader_buffer_t *buf, const char * const errstr)
208 {
209 0 const UV tag_ofs= (UV)(buf->pos - 1 - buf->body_pos);
210 0 const UV offset= srl_read_varint_uv(aTHX_ buf);
211 /* V2+ body offsets are one-based: zero points immediately before the
212 * body, into uninitialized header space after decompression. In V1,
213 * zero points at the document's magic, which is not a value either. */
214 0 ▶ if (expect_false(offset == 0))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(offset == 0)) 0 0 0
215 0 SRL_RDR_ERRORf1(buf,
216 "Corrupted packet%s. Offset 0 does not point into the document body",
217 errstr);
218 /* In V1 the offsets of the header bytes are nonzero too; after
219 * decompression the header bytes were never even written. */
220 0 ▶ if (expect_false(offset < (UV)(buf->body_start - buf->body_pos)))
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false(offset < (UV)(buf->body_start - buf->body_pos))) 0 0 0
221 0 SRL_RDR_ERRORf1(buf, "Back-reference offset points before the document body%s", errstr);
222 /* A "copy" reference can only refer to things that precede its tag
223 * in the same body; an offset pointing at the tag itself would make
224 * readers that follow it loop forever. Compare the offset to the tag's
225 * offset instead of adding an uncontrolled UV to body_pos as
226 * that pointer addition can wrap below body_pos for a large offset. */
227 0 ▶ if (expect_false( offset >= tag_ofs )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( offset >= tag_ofs )) { 0 0 0
228 0 SRL_RDR_ERRORf4(buf, "Corrupted packet%s. Offset %"UVuf" points past current position %"UVuf" in packet with length of %"UVuf" bytes long",
229 errstr, offset, (UV)SRL_RDR_POS_OFS(buf), (UV)SRL_RDR_SIZE(buf));
230 }
231 0 return offset;
232 }
233  
234 SRL_STATIC_INLINE UV
235 251 srl_read_varint_uv_length(pTHX_ srl_reader_buffer_t *buf, const char * const errstr)
236 {
237 251 UV len= srl_read_varint_uv(aTHX_ buf);
238 251 ▶ SRL_RDR_ASSERT_SPACE(buf, len, errstr);
Branch: 3 branches
BranchTrueFalseTotal
SRL_RDR_ASSERT_SPACE(buf, len, errstr); 251 0 251
SRL_RDR_ASSERT_SPACE(buf, len, errstr); 0 251 251
SRL_RDR_ASSERT_SPACE(buf, len, errstr); 0 251 251
239 251 return len;
240 }
241  
242 SRL_STATIC_INLINE UV
243 0 srl_read_varint_uv_count(pTHX_ srl_reader_buffer_t *buf, const char * const errstr)
244 {
245 0 UV len= srl_read_varint_uv(aTHX_ buf);
246 0 ▶ if (expect_false( len > I32_MAX )) {
Branch: 1 branch
BranchTrueFalseTotal
if (expect_false( len > I32_MAX )) { 0 0 0
247 0 SRL_RDR_ERRORf3(buf, "Corrupted packet%s. Count %"UVuf" exceeds I32_MAX (%i), which is impossible.",
248 errstr, len, I32_MAX);
249 }
250 0 return len;
251 }
252  
253 #endif
« srl_reader_decompress.h ↑ Summary srl_reader_misc.h »