20
18
* For now, potentially smaller code wins. */
22
20
/* buffer operations */
23
#define BUF_POS_OFS(buf) (((buf).pos) - ((buf).start))
24
#define BUF_SPACE(buf) (((buf).end) - ((buf).pos))
25
#define BUF_SIZE(buf) (((buf).end) - ((buf).start))
21
#define BUF_POS_OFS(buf) (((buf)->pos) - ((buf)->start))
22
#define BUF_SPACE(buf) (((buf)->end) - ((buf)->pos))
23
#define BUF_SIZE(buf) (((buf)->end) - ((buf)->start))
26
24
#define BUF_NEED_GROW(buf, minlen) ((size_t)BUF_SPACE(buf) <= minlen)
27
25
#define BUF_NEED_GROW_TOTAL(buf, minlen) ((size_t)BUF_SIZE(buf) <= minlen)
26
#define BUF_NOT_DONE(buf) ((buf)->pos < (buf)->end)
27
#define BUF_DONE(buf) ((buf)->pos >= (buf)->end)
30
29
/* body-position/size related operations */
31
#define BODY_POS_OFS(buf) (((buf).pos) - ((buf).body_pos))
30
#define BODY_POS_OFS(buf) (((buf)->pos) - ((buf)->body_pos))
33
32
/* these are mostly for right between (de)serializing the header and the body */
34
#define SRL_SET_BODY_POS(enc, pos_ptr) ((enc)->buf.body_pos = pos_ptr)
35
#define SRL_UPDATE_BODY_POS(enc) \
33
#define SRL_SET_BODY_POS(buf, pos_ptr) ((buf)->body_pos = pos_ptr)
34
#define SRL_UPDATE_BODY_POS(buf, protocol_version) \
37
if (expect_false((enc)->protocol_version == 1)) { \
38
SRL_SET_BODY_POS(enc, (enc)->buf.start); \
36
if (expect_false((protocol_version) == 1)) { \
37
SRL_SET_BODY_POS((buf), (buf)->start); \
40
SRL_SET_BODY_POS(enc, (enc)->buf.pos-1); \
39
SRL_SET_BODY_POS((buf), (buf)->pos-1); \
45
43
/* Internal debugging macros, used only in DEBUG mode */
47
#define DEBUG_ASSERT_BUF_SPACE(enc, len) STMT_START { \
48
if((BUF_SPACE(enc->buf) < (ptrdiff_t)(len))) { \
45
#define DEBUG_ASSERT_BUF_SPACE(buf, len) STMT_START { \
46
if((BUF_SPACE(buf) < (ptrdiff_t)(len))) { \
49
47
warn("failed assertion check - pos: %ld [%p %p %p] %ld < %ld", \
50
(long)BUF_POS_OFS(enc->buf), (enc)->buf.start, \
51
(enc)->buf.pos, (enc)->buf.end, \
52
(long)BUF_SPACE(enc->buf),(long)(len)); \
48
(long)BUF_POS_OFS(buf), (buf)->start, \
49
(buf)->pos, (buf)->end, \
50
(long)BUF_SPACE(buf),(long)(len)); \
54
assert(BUF_SPACE(enc->buf) >= (ptrdiff_t)(len)); \
52
assert(BUF_SPACE(buf) >= (ptrdiff_t)(len)); \
57
#define DEBUG_ASSERT_BUF_SPACE(enc, len) ((void)0)
55
#define DEBUG_ASSERT_BUF_SPACE(buf, len) ((void)0)
61
#define DEBUG_ASSERT_BUF_SANE(enc) STMT_START { \
62
if(!(((enc)->buf.start <= (enc)->buf.pos) && ((enc)->buf.pos <= (enc)->buf.end))){ \
63
warn("failed sanity assertion check - pos: %ld [%p %p %p] %ld", \
64
(long)BUF_POS_OFS(enc->buf), (enc)->buf.start, \
65
(enc)->buf.pos, (enc)->buf.end, (long)BUF_SPACE(enc->buf)); \
67
assert(((enc)->buf.start <= (enc)->buf.pos) && ((enc)->buf.pos <= (enc)->buf.end)); \
59
#define DEBUG_ASSERT_BUF_SANE(buf) STMT_START { \
60
if(!(((buf)->start <= (buf)->pos) && ((buf)->pos <= (buf)->end))){ \
61
warn("failed sanity assertion check - pos: %ld [%p %p %p] %ld", \
62
(long)BUF_POS_OFS(buf), (buf)->start, \
63
(buf)->pos, (buf)->end, (long)BUF_SPACE(buf)); \
65
assert(((buf)->start <= (buf)->pos) && ((buf)->pos <= (buf)->end)); \
70
#define DEBUG_ASSERT_BUF_SANE(enc) \
71
assert(((enc)->buf.start <= (enc)->buf.pos) && ((enc)->buf.pos <= (enc)->buf.end))
68
#define DEBUG_ASSERT_BUF_SANE(buf) \
69
assert(((buf)->start <= (buf)->pos) && ((buf)->pos <= (buf)->end))
74
72
/* Allocate a virgin buffer (but not the buffer struct) */
75
73
SRL_STATIC_INLINE int
76
74
srl_buf_init_buffer(pTHX_ srl_buffer_t *buf, const STRLEN init_size)
78
Newx(buf->start, init_size, char);
76
Newx(buf->start, init_size, srl_buffer_char);
79
77
if (expect_false( buf->start == NULL ))
81
79
buf->end = buf->start + init_size - 1;
108
106
Copy(&tmp, buf2, 1, srl_buffer_t);
112
109
SRL_STATIC_INLINE void
113
srl_buf_grow_nocheck(pTHX_ srl_encoder_t *enc, size_t minlen)
110
srl_buf_grow_nocheck(pTHX_ srl_buffer_t *buf, size_t minlen)
115
const size_t pos_ofs= BUF_POS_OFS(enc->buf); /* have to store the offset of pos */
116
const size_t body_ofs= enc->buf.body_pos - enc->buf.start; /* have to store the offset of the body */
112
const size_t pos_ofs= BUF_POS_OFS(buf); /* have to store the offset of pos */
113
const size_t body_ofs= buf->body_pos - buf->start; /* have to store the offset of the body */
118
115
const size_t new_size = minlen;
120
const size_t cur_size = BUF_SIZE(enc->buf);
117
const size_t cur_size = BUF_SIZE(buf);
121
118
const size_t grown_len = (size_t)(cur_size * BUFFER_GROWTH_FACTOR);
122
119
const size_t new_size = 100 + (minlen > grown_len ? minlen : grown_len);
125
DEBUG_ASSERT_BUF_SANE(enc);
122
DEBUG_ASSERT_BUF_SANE(buf);
126
123
/* assert that Renew means GROWING the buffer */
127
assert(enc->buf.start + new_size > enc->buf.end);
124
assert(buf->start + new_size > buf->end);
129
Renew(enc->buf.start, new_size, char);
130
if (enc->buf.start == NULL)
126
Renew(buf->start, new_size, srl_buffer_char);
127
if (buf->start == NULL)
131
128
croak("Out of memory!");
132
enc->buf.end = (char *)(enc->buf.start + new_size);
133
enc->buf.pos= enc->buf.start + pos_ofs;
134
SRL_SET_BODY_POS(enc, enc->buf.start + body_ofs);
136
DEBUG_ASSERT_BUF_SANE(enc);
137
assert(enc->buf.end - enc->buf.start > (ptrdiff_t)0);
138
assert(enc->buf.pos - enc->buf.start >= (ptrdiff_t)0);
130
buf->end = (srl_buffer_char*) (buf->start + new_size);
131
buf->pos = buf->start + pos_ofs;
132
SRL_SET_BODY_POS(buf, buf->start + body_ofs);
134
DEBUG_ASSERT_BUF_SANE(buf);
135
assert(buf->end - buf->start > (ptrdiff_t)0);
136
assert(buf->pos - buf->start >= (ptrdiff_t)0);
139
137
/* The following is checking against -1 because SRL_UPDATE_BODY_POS
140
138
* will actually set the body_pos to pos-1, where pos can be 0.
141
139
* This works out fine in the end, but is admittedly a bit shady.
143
assert(enc->buf.body_pos - enc->buf.start >= (ptrdiff_t)-1);
146
#define BUF_SIZE_ASSERT(enc, minlen) \
148
DEBUG_ASSERT_BUF_SANE(enc); \
149
if (BUF_NEED_GROW(enc->buf, minlen)) \
150
srl_buf_grow_nocheck(aTHX_ (enc), (BUF_SIZE(enc->buf) + minlen)); \
151
DEBUG_ASSERT_BUF_SANE(enc); \
154
#define BUF_SIZE_ASSERT_TOTAL(enc, minlen) \
156
DEBUG_ASSERT_BUF_SANE(enc); \
157
if (BUF_NEED_GROW_TOTAL(enc->buf, minlen)) \
158
srl_buf_grow_nocheck(aTHX_ (enc), (minlen)); \
159
DEBUG_ASSERT_BUF_SANE(enc); \
162
SRL_STATIC_INLINE void
163
srl_buf_cat_str_int(pTHX_ srl_encoder_t *enc, const char *str, size_t len)
165
BUF_SIZE_ASSERT(enc, len);
166
Copy(str, enc->buf.pos, len, char);
168
DEBUG_ASSERT_BUF_SANE(enc);
170
#define srl_buf_cat_str(enc, str, len) srl_buf_cat_str_int(aTHX_ enc, str, len)
171
/* see perl.git:handy.h STR_WITH_LEN macro for explanation of the below code */
172
#define srl_buf_cat_str_s(enc, str) srl_buf_cat_str(enc, ("" str ""), sizeof(str)-1)
174
SRL_STATIC_INLINE void
175
srl_buf_cat_str_nocheck_int(pTHX_ srl_encoder_t *enc, const char *str, size_t len)
177
DEBUG_ASSERT_BUF_SANE(enc);
178
DEBUG_ASSERT_BUF_SPACE(enc, len);
179
Copy(str, enc->buf.pos, len, char);
181
DEBUG_ASSERT_BUF_SANE(enc);
183
#define srl_buf_cat_str_nocheck(enc, str, len) srl_buf_cat_str_nocheck_int(aTHX_ enc, str, len)
184
/* see perl.git:handy.h STR_WITH_LEN macro for explanation of the below code */
185
#define srl_buf_cat_str_s_nocheck(enc, str) srl_buf_cat_str_nocheck(enc, ("" str ""), sizeof(str)-1)
187
SRL_STATIC_INLINE void
188
srl_buf_cat_char_int(pTHX_ srl_encoder_t *enc, const char c)
190
DEBUG_ASSERT_BUF_SANE(enc);
191
BUF_SIZE_ASSERT(enc, 1);
192
DEBUG_ASSERT_BUF_SPACE(enc, 1);
194
DEBUG_ASSERT_BUF_SANE(enc);
196
#define srl_buf_cat_char(enc, c) srl_buf_cat_char_int(aTHX_ enc, c)
198
SRL_STATIC_INLINE void
199
srl_buf_cat_char_nocheck_int(pTHX_ srl_encoder_t *enc, const char c)
201
DEBUG_ASSERT_BUF_SANE(enc);
202
DEBUG_ASSERT_BUF_SPACE(enc, 1);
204
DEBUG_ASSERT_BUF_SANE(enc);
206
#define srl_buf_cat_char_nocheck(enc, c) srl_buf_cat_char_nocheck_int(aTHX_ enc, c)
141
assert(buf->body_pos - buf->start >= (ptrdiff_t)-1);
144
#define BUF_SIZE_ASSERT(buf, minlen) \
146
DEBUG_ASSERT_BUF_SANE(buf); \
147
if (BUF_NEED_GROW(buf, minlen)) \
148
srl_buf_grow_nocheck(aTHX_ (buf), (BUF_SIZE(buf) + minlen)); \
149
DEBUG_ASSERT_BUF_SANE(buf); \
152
#define BUF_SIZE_ASSERT_TOTAL(buf, minlen) \
154
DEBUG_ASSERT_BUF_SANE(buf); \
155
if (BUF_NEED_GROW_TOTAL(buf, minlen)) \
156
srl_buf_grow_nocheck(aTHX_ (buf), (minlen)); \
157
DEBUG_ASSERT_BUF_SANE(buf); \
160
SRL_STATIC_INLINE void
161
srl_buf_cat_str_int(pTHX_ srl_buffer_t *buf, const char *str, size_t len)
163
BUF_SIZE_ASSERT(buf, len);
164
Copy(str, buf->pos, len, char);
166
DEBUG_ASSERT_BUF_SANE(buf);
168
#define srl_buf_cat_str(buf, str, len) srl_buf_cat_str_int(aTHX_ buf, str, len)
169
/* see perl.git:handy.h STR_WITH_LEN macro for explanation of the below code */
170
#define srl_buf_cat_str_s(buf, str) srl_buf_cat_str(buf, ("" str ""), sizeof(str)-1)
172
SRL_STATIC_INLINE void
173
srl_buf_cat_str_nocheck_int(pTHX_ srl_buffer_t *buf, const char *str, size_t len)
175
DEBUG_ASSERT_BUF_SANE(buf);
176
DEBUG_ASSERT_BUF_SPACE(buf, len);
177
Copy(str, buf->pos, len, char);
179
DEBUG_ASSERT_BUF_SANE(buf);
181
#define srl_buf_cat_str_nocheck(buf, str, len) srl_buf_cat_str_nocheck_int(aTHX_ buf, str, len)
182
/* see perl.git:handy.h STR_WITH_LEN macro for explanation of the below code */
183
#define srl_buf_cat_str_s_nocheck(buf, str) srl_buf_cat_str_nocheck(buf, ("" str ""), sizeof(str)-1)
185
SRL_STATIC_INLINE void
186
srl_buf_cat_char_int(pTHX_ srl_buffer_t *buf, const char c)
188
DEBUG_ASSERT_BUF_SANE(buf);
189
BUF_SIZE_ASSERT(buf, 1);
190
DEBUG_ASSERT_BUF_SPACE(buf, 1);
192
DEBUG_ASSERT_BUF_SANE(buf);
194
#define srl_buf_cat_char(buf, c) srl_buf_cat_char_int(aTHX_ buf, c)
196
SRL_STATIC_INLINE void
197
srl_buf_cat_char_nocheck_int(pTHX_ srl_buffer_t *buf, const char c)
199
DEBUG_ASSERT_BUF_SANE(buf);
200
DEBUG_ASSERT_BUF_SPACE(buf, 1);
202
DEBUG_ASSERT_BUF_SANE(buf);
204
#define srl_buf_cat_char_nocheck(buf, c) srl_buf_cat_char_nocheck_int(aTHX_ buf, c)
208
206
/* define constant for other code to use in preallocations */
209
207
#define SRL_MAX_VARINT_LENGTH 11
211
SRL_STATIC_INLINE void
212
srl_buf_cat_varint_nocheck(pTHX_ srl_encoder_t *enc, const char tag, UV n) {
213
DEBUG_ASSERT_BUF_SANE(enc);
214
DEBUG_ASSERT_BUF_SPACE(enc, (tag==0 ? 0 : 1) + SRL_MAX_VARINT_LENGTH);
209
* This implements "varint" and "zigzag varint" types as used in protobufs, etc.
211
* varint is a variable length encoding of unsigned integers, where the low
212
* 7 bits of the input value are encoded into each byte of output, with the high bit
213
* used as a flag to indicate there is another byte worth of bits to be read.
215
* zigzag is a way of encoding signed integers as an unsigned integer in such a way
216
* that positive and negative numbers are interleaved, so that z0=0, z1=-1, z2=1,
217
* z3=-2, z4=2, etc. When the zigzag form is represented as a varint, the result is
218
* that both negative and positive number take space proportional to their distance
221
* see: https://developers.google.com/protocol-buffers/docs/encoding#types
224
#define srl_varint_size(x) ( \
225
z <= (1UL << 7) ? 1 : \
226
z <= (1UL << 14) ? 2 : \
227
z <= (1UL << 21) ? 3 : \
228
z <= (1UL << 28) ? 4 : \
229
z <= (1UL << 35) ? 5 : \
230
z <= (1UL << 42) ? 6 : \
231
z <= (1UL << 49) ? 7 : \
232
z <= (1UL << 56) ? 8 : \
233
z <= (1UL << 63) ? 9 : \
237
SRL_STATIC_INLINE void
238
srl_buf_cat_varint_raw_nocheck(pTHX_ srl_buffer_t *buf, UV value) {
239
DEBUG_ASSERT_BUF_SANE(buf);
240
DEBUG_ASSERT_BUF_SPACE(buf, SRL_MAX_VARINT_LENGTH);
241
while (value >= 0x80) { /* while we are larger than 7 bits long */
242
*buf->pos++ = (value & 0x7f) | 0x80; /* write out the least significant 7 bits, set the high bit */
243
value >>= 7; /* shift off the 7 least significant bits */
245
*buf->pos++ = (U8)value; /* encode the last 7 bits without the high bit being set */
246
DEBUG_ASSERT_BUF_SANE(buf);
250
srl_zigzag_iv(IV value) {
251
return (UV)((value << 1) ^ (value >> (sizeof(IV) * 8 - 1)));
254
SRL_STATIC_INLINE void
255
srl_buf_cat_zigzag_raw_nocheck(pTHX_ srl_buffer_t *buf, const IV value) {
256
srl_buf_cat_varint_raw_nocheck(aTHX_ buf, srl_zigzag_iv(value));
259
SRL_STATIC_INLINE void
260
srl_buf_cat_varint_nocheck(pTHX_ srl_buffer_t *buf, const char tag, UV value) {
261
DEBUG_ASSERT_BUF_SPACE(buf, 1);
215
262
if (expect_true( tag ))
216
*enc->buf.pos++ = tag;
217
while (n >= 0x80) { /* while we are larger than 7 bits long */
218
*enc->buf.pos++ = (n & 0x7f) | 0x80; /* write out the least significant 7 bits, set the high bit */
219
n = n >> 7; /* shift off the 7 least significant bits */
221
*enc->buf.pos++ = n; /* encode the last 7 bits without the high bit being set */
222
DEBUG_ASSERT_BUF_SANE(enc);
225
SRL_STATIC_INLINE void
226
srl_buf_cat_varint(pTHX_ srl_encoder_t *enc, const char tag, const UV n) {
264
srl_buf_cat_varint_raw_nocheck(aTHX_ buf, value);
267
SRL_STATIC_INLINE void
268
srl_buf_cat_zigzag_nocheck(pTHX_ srl_buffer_t *buf, const char tag, const IV value) {
269
srl_buf_cat_varint_nocheck(aTHX_ buf, tag, srl_zigzag_iv(value));
272
SRL_STATIC_INLINE void
273
srl_buf_cat_varint(pTHX_ srl_buffer_t *buf, const char tag, const UV value) {
227
274
/* this implements "varint" from google protocol buffers */
228
DEBUG_ASSERT_BUF_SANE(enc);
229
BUF_SIZE_ASSERT(enc, SRL_MAX_VARINT_LENGTH + 1); /* always allocate space for the tag, overalloc is harmless */
230
srl_buf_cat_varint_nocheck(aTHX_ enc, tag, n);
233
SRL_STATIC_INLINE void
234
srl_buf_cat_zigzag_nocheck(pTHX_ srl_encoder_t *enc, const char tag, const IV n) {
235
const UV z= (n << 1) ^ (n >> (sizeof(IV) * 8 - 1));
236
srl_buf_cat_varint_nocheck(aTHX_ enc, tag, z);
239
SRL_STATIC_INLINE void
240
srl_buf_cat_zigzag(pTHX_ srl_encoder_t *enc, const char tag, const IV n) {
242
* This implements googles "zigzag varints" which effectively interleave negative
243
* and positive numbers.
245
* see: https://developers.google.com/protocol-buffers/docs/encoding#types
247
* Note: maybe for negative numbers we should just invert and then treat as a positive?
250
const UV z= (n << 1) ^ (n >> (sizeof(IV) * 8 - 1));
251
srl_buf_cat_varint(aTHX_ enc, tag, z);
275
BUF_SIZE_ASSERT(buf, SRL_MAX_VARINT_LENGTH + 1); /* always allocate space for the tag, overalloc is harmless */
276
srl_buf_cat_varint_nocheck(aTHX_ buf, tag, value);
279
SRL_STATIC_INLINE void
280
srl_buf_cat_zigzag(pTHX_ srl_buffer_t *buf, const char tag, const IV value) {
281
srl_buf_cat_varint(aTHX_ buf, tag, srl_zigzag_iv(value));