X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=pb_decode.c;h=50a11c40c3f43c38fcc86487d27046ede6894b1e;hb=e18352d50678005c9dbb3ac76913555f5317c81c;hp=3992ab86f8c02b8a46c83fcf8bef0dce5f6053a5;hpb=ad7a0e2111aaf599466153097e4c8eebf476244d;p=apps%2Fagl-service-can-low-level.git diff --git a/pb_decode.c b/pb_decode.c index 3992ab86..50a11c40 100644 --- a/pb_decode.c +++ b/pb_decode.c @@ -75,7 +75,7 @@ static bool checkreturn pb_decode_varint32(pb_istream_t *stream, uint32_t *dest) { uint64_t temp; bool status = pb_decode_varint(stream, &temp); - *dest = temp; + *dest = (uint32_t)temp; return status; } @@ -117,12 +117,33 @@ bool checkreturn pb_skip_string(pb_istream_t *stream) return pb_read(stream, NULL, length); } -/* Currently the wire type related stuff is kept hidden from - * callbacks. They shouldn't need it. It's better for performance - * to just assume the correct type and fail safely on corrupt message. - */ +bool checkreturn pb_decode_tag(pb_istream_t *stream, pb_wire_type_t *wire_type, int *tag, bool *eof) +{ + uint32_t temp; + *eof = false; + *wire_type = 0; + *tag = 0; + + if (!pb_decode_varint32(stream, &temp)) + { + if (stream->bytes_left == 0) + *eof = true; + + return false; + } + + if (temp == 0) + { + *eof = true; /* Special feature: allow 0-terminated messages. */ + return false; + } + + *tag = temp >> 3; + *wire_type = (pb_wire_type_t)(temp & 7); + return true; +} -static bool checkreturn skip(pb_istream_t *stream, pb_wire_type_t wire_type) +bool checkreturn pb_skip_field(pb_istream_t *stream, pb_wire_type_t wire_type) { switch (wire_type) { @@ -292,7 +313,7 @@ static bool checkreturn decode_field(pb_istream_t *stream, pb_wire_type_t wire_t pb_callback_t *pCallback = (pb_callback_t*)iter->pData; if (pCallback->funcs.decode == NULL) - return skip(stream, wire_type); + return pb_skip_field(stream, wire_type); if (wire_type == PB_WT_STRING) { @@ -392,27 +413,22 @@ bool checkreturn pb_decode(pb_istream_t *stream, const pb_field_t fields[], void while (stream->bytes_left) { - uint32_t temp; int tag; pb_wire_type_t wire_type; - if (!pb_decode_varint32(stream, &temp)) + bool eof; + + if (!pb_decode_tag(stream, &wire_type, &tag, &eof)) { - if (stream->bytes_left == 0) - break; /* It was EOF */ + if (eof) + break; else - return false; /* It was error */ + return false; } - if (temp == 0) - break; /* Special feature: allow 0-terminated messages. */ - - tag = temp >> 3; - wire_type = (pb_wire_type_t)(temp & 7); - if (!pb_field_find(&iter, tag)) { /* No match found, skip data */ - if (!skip(stream, wire_type)) + if (!pb_skip_field(stream, wire_type)) return false; continue; } @@ -448,6 +464,7 @@ static void endian_copy(void *dest, void *src, size_t destsize, size_t srcsize) #ifdef __BIG_ENDIAN__ memcpy(dest, (char*)src + (srcsize - destsize), destsize); #else + UNUSED(srcsize); memcpy(dest, src, destsize); #endif } @@ -475,11 +492,15 @@ bool checkreturn pb_dec_fixed32(pb_istream_t *stream, const pb_field_t *field, v uint8_t bytes[4] = {0}; bool status = pb_read(stream, bytes, 4); if (status) { - uint8_t bebytes[4] = {bytes[3], bytes[2], bytes[1], bytes[0]}; - memcpy(dest, bebytes, 4); + uint8_t *d = (uint8_t*)dest; + d[0] = bytes[3]; + d[1] = bytes[2]; + d[2] = bytes[1]; + d[3] = bytes[0]; } return status; #else + UNUSED(field); return pb_read(stream, (uint8_t*)dest, 4); #endif } @@ -490,12 +511,19 @@ bool checkreturn pb_dec_fixed64(pb_istream_t *stream, const pb_field_t *field, v uint8_t bytes[8] = {0}; bool status = pb_read(stream, bytes, 8); if (status) { - uint8_t bebytes[8] = {bytes[7], bytes[6], bytes[5], bytes[4], - bytes[3], bytes[2], bytes[1], bytes[0]}; - memcpy(dest, bebytes, 8); + uint8_t *d = (uint8_t*)dest; + d[0] = bytes[7]; + d[1] = bytes[6]; + d[2] = bytes[5]; + d[3] = bytes[4]; + d[4] = bytes[3]; + d[5] = bytes[2]; + d[6] = bytes[1]; + d[7] = bytes[0]; } return status; #else + UNUSED(field); return pb_read(stream, (uint8_t*)dest, 8); #endif } @@ -509,7 +537,8 @@ bool checkreturn pb_dec_bytes(pb_istream_t *stream, const pb_field_t *field, voi return false; x->size = temp; - if (x->size > field->data_size) + /* Check length, noting the space taken by the size_t header. */ + if (x->size > field->data_size - offsetof(pb_bytes_array_t, bytes)) return false; return pb_read(stream, x->bytes, x->size); @@ -522,7 +551,8 @@ bool checkreturn pb_dec_string(pb_istream_t *stream, const pb_field_t *field, vo if (!pb_decode_varint32(stream, &size)) return false; - if (size > field->data_size - 1) + /* Check length, noting the null terminator */ + if (size + 1 > field->data_size) return false; status = pb_read(stream, (uint8_t*)dest, size);