MCPcopy Create free account
hub / github.com/Santroller/Santroller / encode_array

Function encode_array

lib/nanopb/pb_encode.c:128–240  ·  view source on GitHub ↗

Encode a static array. Handles the size calculations and possible packing. */

Source from the content-addressed store, hash-verified

126
127/* Encode a static array. Handles the size calculations and possible packing. */
128static bool checkreturn encode_array(pb_ostream_t *stream, pb_field_iter_t *field)
129{
130 pb_size_t i;
131 pb_size_t count;
132#ifndef PB_ENCODE_ARRAYS_UNPACKED
133 size_t size;
134#endif
135
136 count = *(pb_size_t*)field->pSize;
137
138 if (count == 0)
139 return true;
140
141 if (PB_ATYPE(field->type) != PB_ATYPE_POINTER && count > field->array_size)
142 PB_RETURN_ERROR(stream, "array max size exceeded");
143
144#ifndef PB_ENCODE_ARRAYS_UNPACKED
145 /* We always pack arrays if the datatype allows it. */
146 if (PB_LTYPE(field->type) <= PB_LTYPE_LAST_PACKABLE)
147 {
148 if (!pb_encode_tag(stream, PB_WT_STRING, field->tag))
149 return false;
150
151 /* Determine the total size of packed array. */
152 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32)
153 {
154 size = 4 * (size_t)count;
155 }
156 else if (PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
157 {
158 size = 8 * (size_t)count;
159 }
160 else
161 {
162 pb_ostream_t sizestream = PB_OSTREAM_SIZING;
163 void *pData_orig = field->pData;
164 for (i = 0; i < count; i++)
165 {
166 if (!pb_enc_varint(&sizestream, field))
167 PB_RETURN_ERROR(stream, PB_GET_ERROR(&sizestream));
168 field->pData = (char*)field->pData + field->data_size;
169 }
170 field->pData = pData_orig;
171 size = sizestream.bytes_written;
172 }
173
174 if (!pb_encode_varint(stream, (pb_uint64_t)size))
175 return false;
176
177 if (stream->callback == NULL)
178 return pb_write(stream, NULL, size); /* Just sizing.. */
179
180 /* Write the data */
181 for (i = 0; i < count; i++)
182 {
183 if (PB_LTYPE(field->type) == PB_LTYPE_FIXED32 || PB_LTYPE(field->type) == PB_LTYPE_FIXED64)
184 {
185 if (!pb_enc_fixed(stream, field))

Callers 2

pb_encode.cFile · 0.85
encode_fieldFunction · 0.85

Calls 7

pb_encode_tagFunction · 0.85
pb_enc_varintFunction · 0.85
pb_encode_varintFunction · 0.85
pb_writeFunction · 0.85
pb_enc_fixedFunction · 0.85
pb_encode_tag_for_fieldFunction · 0.85
encode_basic_fieldFunction · 0.85

Tested by

no test coverage detected