1 /* pb_decode.c -- decode a protobuf using minimal resources
3 * 2011 Petteri Aimonen <jpa@kapsi.fi>
10 const pb_decoder_t PB_DECODERS[16] = {
28 ////////////////////////
30 ////////////////////////
32 static bool buf_read(pb_istream_t *stream, uint8_t *buf, size_t count)
34 uint8_t *source = (uint8_t*)stream->state;
37 memcpy(buf, source, count);
39 stream->state = source + count;
43 pb_istream_t pb_istream_from_buffer(uint8_t *buf, size_t bufsize)
45 pb_istream_t stream = {&buf_read, buf, bufsize};
49 bool pb_decode_varint32(pb_istream_t *stream, uint32_t *dest)
52 if (!pb_decode_varint64(stream, &temp))
58 bool pb_decode_varint64(pb_istream_t *stream, uint64_t *dest)
64 while (bitpos < 64 && pb_read(stream, &byte, 1))
66 *dest |= (byte & 0x7F) << bitpos;
76 bool pb_skip_varint(pb_istream_t *stream)
81 if (!pb_read(stream, &byte, 1))
83 } while (byte & 0x80);
87 bool pb_skip_string(pb_istream_t *stream)
90 if (!pb_decode_varint32(stream, &length))
93 return pb_read(stream, NULL, length);
96 /* Currently all wire type related stuff is kept hidden from
97 * callbacks. They shouldn't need it. It's better for performance
98 * to just assume the correct type and fail safely on corrupt message.
108 static bool skip(pb_istream_t *stream, int wire_type)
112 case WT_VARINT: return pb_skip_varint(stream);
113 case WT_64BIT: return pb_read(stream, NULL, 8);
114 case WT_STRING: return pb_skip_string(stream);
115 case WT_32BIT: return pb_read(stream, NULL, 4);
116 default: return false;
120 // Read a raw value to buffer, for the purpose of passing it to callback.
121 // Size is maximum size on call, and actual size on return.
122 static bool read_raw_value(pb_istream_t *stream, int wire_type, uint8_t *buf, size_t *size)
124 size_t max_size = *size;
132 if (*size > max_size) return false;
133 if (!pb_read(stream, buf++, 1)) return false;
134 } while (*buf & 0x80);
139 return pb_read(stream, buf, 8);
143 return pb_read(stream, buf, 4);
145 default: return false;
149 // Decode string length from stream and return a substream with limited length
150 static bool make_string_substream(pb_istream_t *stream, pb_istream_t *substream)
153 if (!pb_decode_varint32(stream, &size))
156 *substream = *stream;
157 if (substream->bytes_left < size)
160 substream->bytes_left = size;
161 stream->bytes_left -= size;
165 bool decode_field(pb_istream_t *stream, int wire_type, const pb_field_t *field, void *dest_struct)
167 pb_decoder_t func = PB_DECODERS[PB_LTYPE(field->type)];
168 void *pData = (char*)dest_struct + field->data_offset;
169 void *pSize = (char*)dest_struct + field->size_offset;
171 switch (PB_HTYPE(field->type))
173 case PB_HTYPE_REQUIRED:
174 return func(stream, field, pData);
176 case PB_HTYPE_OPTIONAL:
177 *(bool*)pSize = true;
178 return func(stream, field, pData);
181 if (wire_type == WT_STRING
182 && PB_LTYPE(field->type) != PB_LTYPE_BYTES
183 && PB_LTYPE(field->type) != PB_LTYPE_STRING
184 && PB_LTYPE(field->type) != PB_LTYPE_SUBMESSAGE)
187 size_t *size = (size_t*)pSize;
188 pb_istream_t substream;
189 if (!make_string_substream(stream, &substream))
192 while (substream.bytes_left && *size < field->array_size)
194 void *pItem = pData + field->data_size * (*size);
195 if (!func(stream, field, pItem))
199 return (substream.bytes_left == 0);
204 size_t *size = (size_t*)pSize;
205 if (*size >= field->array_size)
208 void *pItem = pData + field->data_size * (*size);
210 return func(stream, field, pItem);
213 case PB_HTYPE_CALLBACK:
214 if (wire_type == WT_STRING)
216 pb_callback_t *pCallback = (pb_callback_t*)pData;
217 pb_istream_t substream;
219 if (!make_string_substream(stream, &substream))
222 while (substream.bytes_left)
224 if (!pCallback->funcs.decode(&substream, field, pCallback->arg))
230 // Copy the single scalar value to stack.
231 // This is required so that we can limit the stream length,
232 // which in turn allows to use same callback for packed and
233 // not-packed fields.
235 size_t size = sizeof(buffer);
236 if (!read_raw_value(stream, wire_type, buffer, &size))
238 pb_istream_t substream = pb_istream_from_buffer(buffer, size);
240 pb_callback_t *pCallback = (pb_callback_t*)pData;
241 return pCallback->funcs.decode(&substream, field, pCallback->arg);
249 bool pb_decode(pb_istream_t *stream, const pb_field_t fields[], void *dest_struct)
251 // Used to check for required fields
252 uint32_t fields_seen = 0;
255 // Initialize size/has fields and apply default values
256 for (i = 0; fields[i].tag != 0; i++)
258 void *pData = (char*)dest_struct + fields[i].data_offset;
259 void *pSize = (char*)dest_struct + fields[i].size_offset;
260 if (PB_HTYPE(fields[i].type) == PB_HTYPE_OPTIONAL)
262 *(bool*)pSize = false;
264 else if (PB_HTYPE(fields[i].type) == PB_HTYPE_ARRAY)
269 if (PB_HTYPE(fields[i].type) != PB_HTYPE_ARRAY &&
270 PB_HTYPE(fields[i].type) != PB_HTYPE_CALLBACK)
272 if (fields[i].ptr != NULL)
274 memcpy(pData, fields[i].ptr, fields[i].data_size);
278 memset(pData, 0, fields[i].data_size);
283 while (stream->bytes_left)
286 if (!pb_decode_varint32(stream, &temp))
290 int wire_type = temp & 7;
293 while (fields[i].tag != 0 && fields[i].tag != tag)
298 if (fields[i].tag == 0) // No match found, skip data
300 skip(stream, wire_type);
304 fields_seen |= 1 << (i & 31);
306 if (!decode_field(stream, wire_type, &fields[i], dest_struct))
310 // Check that all required fields (mod 31) were present.
311 for (i = 0; fields[i].tag != 0; i++)
313 if (PB_HTYPE(fields[i].type) == PB_HTYPE_REQUIRED &&
314 !(fields_seen & (1 << (i & 31))))
325 bool pb_dec_uint32(pb_istream_t *stream, const pb_field_t *field, void *dest)
327 return pb_decode_varint32(stream, (uint32_t*)dest);
330 bool pb_dec_sint32(pb_istream_t *stream, const pb_field_t *field, void *dest)
332 uint32_t *x = (uint32_t*)dest;
333 bool status = pb_decode_varint32(stream, x);
334 *x = (*x >> 1) ^ -(int32_t)(*x & 1);
338 bool pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, void *dest)
340 uint8_t bytes[4] = {0};
341 bool status = pb_read(stream, bytes, 4);
343 #ifdef __BIG_ENDIAN__
344 uint8_t lebytes[4] = {bytes[3], bytes[2], bytes[1], bytes[0]};
345 memcpy(dest, lebytes, 4);
347 memcpy(dest, bytes, 4);
352 bool pb_dec_uint64(pb_istream_t *stream, const pb_field_t *field, void *dest)
354 return pb_decode_varint64(stream, (uint64_t*)dest);
357 bool pb_dec_sint64(pb_istream_t *stream, const pb_field_t *field, void *dest)
359 uint64_t *x = (uint64_t*)dest;
360 bool status = pb_decode_varint64(stream, x);
361 *x = (*x >> 1) ^ -(int64_t)(*x & 1);
365 bool pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, void *dest)
367 uint8_t bytes[8] = {0};
368 bool status = pb_read(stream, bytes, 8);
370 #ifdef __BIG_ENDIAN__
371 uint8_t lebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4],
372 bytes[3], bytes[2], bytes[1], bytes[0]};
373 memcpy(dest, lebytes, 4);
375 memcpy(dest, bytes, 4);
380 bool pb_dec_bool(pb_istream_t *stream, const pb_field_t *field, void *dest)
383 bool status = pb_decode_varint32(stream, &temp);
384 *(bool*)dest = !!temp;
388 bool pb_dec_enum(pb_istream_t *stream, const pb_field_t *field, void *dest)
390 // Enum sizes can vary, copy only data_size amount of bytes.
392 bool status = pb_decode_varint32(stream, &temp);
393 memcpy(dest, &temp, field->data_size);
397 bool pb_dec_float(pb_istream_t *stream, const pb_field_t *field, void *dest)
399 return pb_read(stream, (uint8_t*)dest, sizeof(float));
402 bool pb_dec_double(pb_istream_t *stream, const pb_field_t *field, void *dest)
404 return pb_read(stream, (uint8_t*)dest, sizeof(double));
407 bool pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, void *dest)
409 pb_bytes_array_t *x = (pb_bytes_array_t*)dest;
412 if (!pb_decode_varint32(stream, &temp))
416 // Note: data_size includes the size of the x.size field, too.
417 // Calculate actual size starting from offset.
418 if (x->size > field->data_size - offsetof(pb_bytes_array_t, bytes))
421 return pb_read(stream, x->bytes, x->size);
424 bool pb_dec_string(pb_istream_t *stream, const pb_field_t *field, void *dest)
427 if (!pb_decode_varint32(stream, &size))
430 if (size > field->data_size - 1)
433 bool status = pb_read(stream, (uint8_t*)dest, size);
434 *((uint8_t*)dest + size) = 0;
438 bool pb_dec_submessage(pb_istream_t *stream, const pb_field_t *field, void *dest)
440 pb_istream_t substream;
442 if (!make_string_substream(stream, &substream))
445 if (field->ptr == NULL)
448 return pb_decode(&substream, (pb_field_t*)field->ptr, dest);