Encode a static array. Handles the size calculations and possible packing. */
| 126 | |
| 127 | /* Encode a static array. Handles the size calculations and possible packing. */ |
| 128 | static 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)) |
no test coverage detected