Code Review
/
apps
/
agl-service-can-low-level.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
review
|
tree
raw
|
inline
| side by side
Check for supported GCC CCFLAGS when building tests.
[apps/agl-service-can-low-level.git]
/
pb_encode.c
diff --git
a/pb_encode.c
b/pb_encode.c
index
9c3ed2d
..
c2d0e2c
100644
(file)
--- a/
pb_encode.c
+++ b/
pb_encode.c
@@
-173,11
+173,14
@@
static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
if (!pb_encode_tag_for_field(stream, field))
return false;
if (!pb_encode_tag_for_field(stream, field))
return false;
- /* Special case for strings */
+ /* Normally the data is stored directly in the array entries, but
+ * for pointer-type string fields, the array entries are actually
+ * string pointers. So we have to dereference once more to get to
+ * the character data. */
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
PB_LTYPE(field->type) == PB_LTYPE_STRING)
{
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER &&
PB_LTYPE(field->type) == PB_LTYPE_STRING)
{
- if (!func(stream, field, *(const void**)p))
+ if (!func(stream, field, *(const void*
const
*)p))
return false;
}
else
return false;
}
else
@@
-192,32
+195,38
@@
static bool checkreturn encode_array(pb_ostream_t *stream, const pb_field_t *fie
return true;
}
return true;
}
-/* Encode a field with static
allocation, i.e. one whose data is stored
- * i
n the structure itself
. */
-static bool checkreturn encode_
stat
ic_field(pb_ostream_t *stream,
+/* Encode a field with static
or pointer allocation, i.e. one whose data
+ * i
s available to the encoder directly
. */
+static bool checkreturn encode_
bas
ic_field(pb_ostream_t *stream,
const pb_field_t *field, const void *pData)
{
pb_encoder_t func;
const void *pSize;
const pb_field_t *field, const void *pData)
{
pb_encoder_t func;
const void *pSize;
- bool
dummy
= true;
+ bool
implicit_has
= true;
func = PB_ENCODERS[PB_LTYPE(field->type)];
if (field->size_offset)
pSize = (const char*)pData + field->size_offset;
func = PB_ENCODERS[PB_LTYPE(field->type)];
if (field->size_offset)
pSize = (const char*)pData + field->size_offset;
- else if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
- pSize = *(const void**)pData ? &dummy : pData;
else
else
- pSize = &
dummy
;
+ pSize = &
implicit_has
;
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
- pData = *(const void**)pData;
+ {
+ /* pData is a pointer to the field, which contains pointer to
+ * the data. If the 2nd pointer is NULL, it is interpreted as if
+ * the has_field was false.
+ */
+
+ pData = *(const void* const*)pData;
+ implicit_has = (pData != NULL);
+ }
switch (PB_HTYPE(field->type))
{
case PB_HTYPE_REQUIRED:
if (!pData)
switch (PB_HTYPE(field->type))
{
case PB_HTYPE_REQUIRED:
if (!pData)
-
return false
;
+
PB_RETURN_ERROR(stream, "missing required field")
;
if (!pb_encode_tag_for_field(stream, field))
return false;
if (!func(stream, field, pData))
if (!pb_encode_tag_for_field(stream, field))
return false;
if (!func(stream, field, pData))
@@
-276,7
+285,7
@@
static bool checkreturn encode_field(pb_ostream_t *stream,
{
case PB_ATYPE_STATIC:
case PB_ATYPE_POINTER:
{
case PB_ATYPE_STATIC:
case PB_ATYPE_POINTER:
- return encode_
stat
ic_field(stream, field, pData);
+ return encode_
bas
ic_field(stream, field, pData);
case PB_ATYPE_CALLBACK:
return encode_callback_field(stream, field, pData);
case PB_ATYPE_CALLBACK:
return encode_callback_field(stream, field, pData);
@@
-490,7
+499,12
@@
bool checkreturn pb_encode_submessage(pb_ostream_t *stream, const pb_field_t fie
bool status;
if (!pb_encode(&substream, fields, src_struct))
bool status;
if (!pb_encode(&substream, fields, src_struct))
+ {
+#ifndef PB_NO_ERRMSG
+ stream->errmsg = substream.errmsg;
+#endif
return false;
return false;
+ }
size = substream.bytes_written;
size = substream.bytes_written;
@@
-589,14
+603,14
@@
bool checkreturn pb_enc_fixed32(pb_ostream_t *stream, const pb_field_t *field, c
bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
- const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
-
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
{
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
{
- return pb_encode_string(stream, *(const uint8_t**)bytes->bytes, bytes->size);
+ const pb_bytes_ptr_t *bytes = (const pb_bytes_ptr_t*)src;
+ return pb_encode_string(stream, bytes->bytes, bytes->size);
}
else
{
}
else
{
+ const pb_bytes_array_t *bytes = (const pb_bytes_array_t*)src;
if (bytes->size + offsetof(pb_bytes_array_t, bytes) > field->data_size)
PB_RETURN_ERROR(stream, "bytes size exceeded");
if (bytes->size + offsetof(pb_bytes_array_t, bytes) > field->data_size)
PB_RETURN_ERROR(stream, "bytes size exceeded");
@@
-606,25
+620,20
@@
bool checkreturn pb_enc_bytes(pb_ostream_t *stream, const pb_field_t *field, con
bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
bool checkreturn pb_enc_string(pb_ostream_t *stream, const pb_field_t *field, const void *src)
{
- /* strnlen() is not always available, so just use a
for-
loop */
+ /* strnlen() is not always available, so just use a loop */
size_t size = 0;
size_t size = 0;
+ size_t max_size = field->data_size;
const char *p = (const char*)src;
const char *p = (const char*)src;
+
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
if (PB_ATYPE(field->type) == PB_ATYPE_POINTER)
+ max_size = (size_t)-1;
+
+ while (size < max_size && *p != '\0')
{
{
- while (*p != '\0')
- {
- size++;
- p++;
- }
- }
- else
- {
- while (size < field->data_size && *p != '\0')
- {
- size++;
- p++;
- }
+ size++;
+ p++;
}
}
+
return pb_encode_string(stream, (const uint8_t*)src, size);
}
return pb_encode_string(stream, (const uint8_t*)src, size);
}