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 |= (uint64_t)(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 /* Iterator for pb_field_t list */
176 const pb_field_t *start;
177 const pb_field_t *current;
182 } pb_field_iterator_t;
184 static void pb_field_init(pb_field_iterator_t *iter, const pb_field_t *fields, void *dest_struct)
186 iter->start = iter->current = fields;
187 iter->field_index = 0;
188 iter->pData = dest_struct;
189 iter->dest_struct = dest_struct;
192 static bool pb_field_next(pb_field_iterator_t *iter)
194 bool notwrapped = true;
197 if (iter->current->tag == 0)
199 iter->current = iter->start;
200 iter->field_index = 0;
201 iter->pData = iter->dest_struct;
205 iter->pData = (char*)iter->pData + iter->current->data_offset;
206 iter->pSize = (char*)iter->pData + iter->current->size_offset;
210 static bool pb_field_find(pb_field_iterator_t *iter, int tag)
212 int start = iter->field_index;
215 if (iter->current->tag == tag)
218 } while (iter->field_index != start);
223 /*************************
224 * Decode a single field *
225 *************************/
227 bool decode_field(pb_istream_t *stream, int wire_type, pb_field_iterator_t *iter)
229 pb_decoder_t func = PB_DECODERS[PB_LTYPE(iter->current->type)];
231 switch (PB_HTYPE(iter->current->type))
233 case PB_HTYPE_REQUIRED:
234 return func(stream, iter->current, iter->pData);
236 case PB_HTYPE_OPTIONAL:
237 *(bool*)iter->pSize = true;
238 return func(stream, iter->current, iter->pData);
241 if (wire_type == WT_STRING
242 && PB_LTYPE(iter->current->type) <= PB_LTYPE_LAST_PACKABLE)
245 size_t *size = (size_t*)iter->pSize;
246 pb_istream_t substream;
247 if (!make_string_substream(stream, &substream))
250 while (substream.bytes_left && *size < iter->current->array_size)
252 void *pItem = (uint8_t*)iter->pData + iter->current->data_size * (*size);
253 if (!func(stream, iter->current, pItem))
257 return (substream.bytes_left == 0);
262 size_t *size = (size_t*)iter->pSize;
263 void *pItem = (uint8_t*)iter->pData + iter->current->data_size * (*size);
264 if (*size >= iter->current->array_size)
268 return func(stream, iter->current, pItem);
271 case PB_HTYPE_CALLBACK:
272 if (wire_type == WT_STRING)
274 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
275 pb_istream_t substream;
277 if (!make_string_substream(stream, &substream))
280 while (substream.bytes_left)
282 if (!pCallback->funcs.decode(&substream, iter->current, pCallback->arg))
289 /* Copy the single scalar value to stack.
290 * This is required so that we can limit the stream length,
291 * which in turn allows to use same callback for packed and
292 * not-packed fields. */
293 pb_istream_t substream;
294 pb_callback_t *pCallback = (pb_callback_t*)iter->pData;
296 size_t size = sizeof(buffer);
298 if (!read_raw_value(stream, wire_type, buffer, &size))
300 substream = pb_istream_from_buffer(buffer, size);
302 return pCallback->funcs.decode(&substream, iter->current, pCallback->arg);
310 /*********************
311 * Decode all fields *
312 *********************/
314 bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
316 uint32_t fields_seen = 0; /* Used to check for required fields */
317 pb_field_iterator_t iter;
320 if (fields[0].tag == 0)
322 /* No fields -> nothing to do */
323 return pb_read(stream, NULL, stream->bytes_left);
326 pb_field_init(&iter, fields, dest_struct);
328 /* Initialize size/has fields and apply default values */
331 if (PB_HTYPE(iter.current->type) == PB_HTYPE_OPTIONAL)
333 *(bool*)iter.pSize = false;
335 /* Initialize to default value */
336 if (iter.current->ptr != NULL)
337 memcpy(iter.pData, iter.current->ptr, iter.current->data_size);
339 memset(iter.pData, 0, iter.current->data_size);
341 else if (PB_HTYPE(iter.current->type) == PB_HTYPE_ARRAY)
343 *(size_t*)iter.pSize = 0;
345 } while (pb_field_next(&iter));
347 while (stream->bytes_left)
351 if (!pb_decode_varint32(stream, &temp))
355 wire_type = temp & 7;
357 if (!pb_field_find(&iter, tag))
359 /* No match found, skip data */
360 skip(stream, wire_type);
364 fields_seen |= 1 << (iter.field_index & 31);
366 if (!decode_field(stream, wire_type, &iter))
370 /* Check that all required fields (mod 31) were present. */
371 for (i = 0; fields[i].tag != 0; i++)
373 if (PB_HTYPE(fields[i].type) == PB_HTYPE_REQUIRED &&
374 !(fields_seen & (1 << (i & 31))))
385 /* Copy destsize bytes from src so that values are casted properly.
386 * On little endian machine, copy first n bytes of src
387 * On big endian machine, copy last n bytes of src
388 * srcsize must always be larger than destsize
390 static void endian_copy(void *dest, void *src, size_t destsize, size_t srcsize)
392 #ifdef __BIG_ENDIAN__
393 memcpy(dest, (char*)src + (srcsize - destsize), destsize);
395 memcpy(dest, src, destsize);
399 bool pb_dec_varint(pb_istream_t *stream, const pb_field_t *field, void *dest)
402 bool status = pb_decode_varint64(stream, &temp);
403 endian_copy(dest, &temp, field->data_size, sizeof(temp));
407 bool pb_dec_svarint(pb_istream_t *stream, const pb_field_t *field, void *dest)
410 bool status = pb_decode_varint64(stream, &temp);
411 temp = (temp >> 1) ^ -(int64_t)(temp & 1);
412 endian_copy(dest, &temp, field->data_size, sizeof(temp));
416 bool pb_dec_fixed(pb_istream_t *stream, const pb_field_t *field, void *dest)
418 #ifdef __BIG_ENDIAN__
419 uint8_t bytes[8] = {0};
420 bool status = pb_read(stream, bytes, field->data_size);
421 uint8_t lebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
422 bytes[3], bytes[2], bytes[1], bytes[0]};
423 endian_copy(dest, lebytes, field->data_size, 8);
426 return pb_read(stream, (uint8_t*)dest, field->data_size);
430 bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest)
432 pb_bytes_array_t *x = (pb_bytes_array_t*)dest;
435 if (!pb_decode_varint32(stream, &temp))
439 /* Note: data_size includes the size of the x.size field, too.
440 * Calculate actual size starting from offset. */
441 if (x->size > field->data_size - offsetof(pb_bytes_array_t, bytes))
444 return pb_read(stream, x->bytes, x->size);
447 bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, uint8_t *dest)
451 if (!pb_decode_varint32(stream, &size))
454 if (size > field->data_size - 1)
457 status = pb_read(stream, (uint8_t*)dest, size);
458 *((uint8_t*)dest + size) = 0;
462 bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
464 pb_istream_t substream;
466 if (!make_string_substream(stream, &substream))
469 if (field->ptr == NULL)
472 return pb_decode(&substream, (pb_field_t*)field->ptr, dest);