1 /* pb_decode.c -- decode a protobuf using minimal resources
3 * 2011 Petteri Aimonen <jpa@kapsi.fi>
10 const pb_decoder_t PB_DECODERS[PB_LTYPES_COUNT] = {
11 (pb_decoder_t)&pb_dec_varint,
12 (pb_decoder_t)&pb_dec_svarint,
13 (pb_decoder_t)&pb_dec_fixed,
15 (pb_decoder_t)&pb_dec_bytes,
16 (pb_decoder_t)&pb_dec_string,
17 (pb_decoder_t)&pb_dec_submessage
24 bool pb_read(pb_istream_t *stream, uint8_t *buf, size_t count)
27 if (stream->bytes_left < count)
30 status = stream->callback(stream, buf, count);
31 stream->bytes_left -= count;
35 static bool buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
37 uint8_t *source = (uint8_t*)stream->state;
40 memcpy(buf, source, count);
42 stream->state = source + count;
46 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
49 stream.callback = &buf_read;
51 stream.bytes_left = bufsize;
59 bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
62 bool status = pb_decode_varint64(stream, &temp);
67 bool pb_decode_varint64(pb_istream_t *stream, uint64_t *dest)
73 while (bitpos < 64 && pb_read(stream, &byte, 1))
75 *dest |= (byte & 0x7F) << bitpos;
85 bool pb_skip_varint(pb_istream_t *stream)
90 if (!pb_read(stream, &byte, 1))
92 } while (byte & 0x80);
96 bool pb_skip_string(pb_istream_t *stream)
99 if (!pb_decode_varint32(stream, &length))
102 return pb_read(stream, NULL, length);
105 /* Currently all wire type related stuff is kept hidden from
106 * callbacks. They shouldn't need it. It's better for performance
107 * to just assume the correct type and fail safely on corrupt message.
117 static bool skip(pb_istream_t *stream, int wire_type)
121 case WT_VARINT: return pb_skip_varint(stream);
122 case WT_64BIT: return pb_read(stream, NULL, 8);
123 case WT_STRING: return pb_skip_string(stream);
124 case WT_32BIT: return pb_read(stream, NULL, 4);
125 default: return false;
129 /* Read a raw value to buffer, for the purpose of passing it to callback.
130 * Size is maximum size on call, and actual size on return. */
131 static bool read_raw_value(pb_istream_t *stream, int wire_type, uint8_t *buf, size_t *size)
133 size_t max_size = *size;
141 if (*size > max_size) return false;
142 if (!pb_read(stream, buf, 1)) return false;
143 } while (*buf++ & 0x80);
148 return pb_read(stream, buf, 8);
152 return pb_read(stream, buf, 4);
154 default: return false;
158 /* Decode string length from stream and return a substream with limited length */
159 static bool make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
162 if (!pb_decode_varint32(stream, &size))
165 *substream = *stream;
166 if (substream->bytes_left < size)
169 substream->bytes_left = size;
170 stream->bytes_left -= size;
174 bool decode_field(pb_istream_t *stream, int wire_type, const pb_field_t *field, void *dest_struct)
176 pb_decoder_t func = PB_DECODERS[PB_LTYPE(field->type)];
177 void *pData = (char*)dest_struct + field->data_offset;
178 void *pSize = (char*)dest_struct + field->size_offset;
180 switch (PB_HTYPE(field->type))
182 case PB_HTYPE_REQUIRED:
183 return func(stream, field, pData);
185 case PB_HTYPE_OPTIONAL:
186 *(bool*)pSize = true;
187 return func(stream, field, pData);
190 if (wire_type == WT_STRING
191 && PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
194 size_t *size = (size_t*)pSize;
195 pb_istream_t substream;
196 if (!make_string_substream(stream, &substream))
199 while (substream.bytes_left && *size < field->array_size)
201 void *pItem = (uint8_t*)pData + field->data_size * (*size);
202 if (!func(stream, field, pItem))
206 return (substream.bytes_left == 0);
211 size_t *size = (size_t*)pSize;
212 void *pItem = (uint8_t*)pData + field->data_size * (*size);
213 if (*size >= field->array_size)
217 return func(stream, field, pItem);
220 case PB_HTYPE_CALLBACK:
221 if (wire_type == WT_STRING)
223 pb_callback_t *pCallback = (pb_callback_t*)pData;
224 pb_istream_t substream;
226 if (!make_string_substream(stream, &substream))
229 while (substream.bytes_left)
231 if (!pCallback->funcs.decode(&substream, field, pCallback->arg))
238 /* Copy the single scalar value to stack.
239 * This is required so that we can limit the stream length,
240 * which in turn allows to use same callback for packed and
241 * not-packed fields. */
242 pb_istream_t substream;
243 pb_callback_t *pCallback = (pb_callback_t*)pData;
245 size_t size = sizeof(buffer);
247 if (!read_raw_value(stream, wire_type, buffer, &size))
249 substream = pb_istream_from_buffer(buffer, size);
251 return pCallback->funcs.decode(&substream, field, pCallback->arg);
259 bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
261 /* Used to check for required fields */
262 uint32_t fields_seen = 0;
265 /* Initialize size/has fields and apply default values */
266 for (i = 0; fields[i].tag != 0; i++)
268 void *pData = (char*)dest_struct + fields[i].data_offset;
269 void *pSize = (char*)dest_struct + fields[i].size_offset;
270 if (PB_HTYPE(fields[i].type) == PB_HTYPE_OPTIONAL)
272 *(bool*)pSize = false;
274 /* Initialize to default value */
275 if (fields[i].ptr != NULL)
276 memcpy(pData, fields[i].ptr, fields[i].data_size);
278 memset(pData, 0, fields[i].data_size);
280 else if (PB_HTYPE(fields[i].type) == PB_HTYPE_ARRAY)
286 while (stream->bytes_left)
290 if (!pb_decode_varint32(stream, &temp))
294 wire_type = temp & 7;
297 while (fields[i].tag != 0 && fields[i].tag != tag)
302 if (fields[i].tag == 0) /* No match found, skip data */
304 skip(stream, wire_type);
308 fields_seen |= 1 << (i & 31);
310 if (!decode_field(stream, wire_type, &fields[i], dest_struct))
314 /* Check that all required fields (mod 31) were present. */
315 for (i = 0; fields[i].tag != 0; i++)
317 if (PB_HTYPE(fields[i].type) == PB_HTYPE_REQUIRED &&
318 !(fields_seen & (1 << (i & 31))))
329 /* Copy destsize bytes from src so that values are casted properly.
330 * On little endian machine, copy first n bytes of src
331 * On big endian machine, copy last n bytes of src
332 * srcsize must always be larger than destsize
334 static void endian_copy(void *dest, void *src, size_t destsize, size_t srcsize)
336 #ifdef __BIG_ENDIAN__
337 memcpy(dest, (char*)src + (srcsize - destsize), destsize);
339 memcpy(dest, src, destsize);
343 bool pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
346 bool status = pb_decode_varint64(stream, &temp);
347 endian_copy(dest, &temp, field->data_size, sizeof(temp));
351 bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
354 bool status = pb_decode_varint64(stream, &temp);
355 temp = (temp >> 1) ^ -(int64_t)(temp & 1);
356 endian_copy(dest, &temp, field->data_size, sizeof(temp));
360 bool pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest)
362 uint8_t bytes[8] = {0};
363 bool status = pb_read(stream, bytes, field->data_size);
365 #ifdef __BIG_ENDIAN__
366 uint8_t lebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
367 bytes[3], bytes[2], bytes[1], bytes[0]};
368 endian_copy(dest, lebytes, field->data_size, 8);
370 endian_copy(dest, bytes, field->data_size, 8);
375 bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest)
377 pb_bytes_array_t *x = (pb_bytes_array_t*)dest;
380 if (!pb_decode_varint32(stream, &temp))
384 /* Note: data_size includes the size of the x.size field, too.
385 * Calculate actual size starting from offset. */
386 if (x->size > field->data_size - offsetof(pb_bytes_array_t, bytes))
389 return pb_read(stream, x->bytes, x->size);
392 bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest)
396 if (!pb_decode_varint32(stream, &size))
399 if (size > field->data_size - 1)
402 status = pb_read(stream, (uint8_t*)dest, size);
403 *((uint8_t*)dest + size) = 0;
407 bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
409 pb_istream_t substream;
411 if (!make_string_substream(stream, &substream))
414 if (field->ptr == NULL)
417 return pb_decode(&substream, (pb_field_t*)field->ptr, dest);