Skip to content

Commit ede7f14

Browse files
Revert "Revert "Give a name to unnamed union in CborEncoder""
This reverts commit 4628a11 and thus brings 0e1440a back. Signed-off-by: Thiago Macieira <[email protected]>
1 parent 31c7f81 commit ede7f14

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

src/cbor.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ struct CborEncoder
162162
union {
163163
uint8_t *ptr;
164164
ptrdiff_t bytes_needed;
165-
};
165+
} data;
166166
const uint8_t *end;
167167
size_t added;
168168
int flags;
@@ -204,12 +204,12 @@ CBOR_API CborError cbor_encoder_close_container_checked(CborEncoder *encoder, co
204204

205205
CBOR_INLINE_API size_t cbor_encoder_get_buffer_size(const CborEncoder *encoder, const uint8_t *buffer)
206206
{
207-
return (size_t)(encoder->ptr - buffer);
207+
return (size_t)(encoder->data.ptr - buffer);
208208
}
209209

210210
CBOR_INLINE_API size_t cbor_encoder_get_extra_bytes_needed(const CborEncoder *encoder)
211211
{
212-
return encoder->end ? 0 : (size_t)encoder->bytes_needed;
212+
return encoder->end ? 0 : (size_t)encoder->data.bytes_needed;
213213
}
214214

215215
/* Parser API */

src/cborencoder.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@
197197
*/
198198
void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int flags)
199199
{
200-
encoder->ptr = buffer;
200+
encoder->data.ptr = buffer;
201201
encoder->end = buffer + size;
202202
encoder->added = 0;
203203
encoder->flags = flags;
@@ -235,34 +235,34 @@ static inline void put64(void *where, uint64_t v)
235235
static inline bool would_overflow(CborEncoder *encoder, size_t len)
236236
{
237237
ptrdiff_t remaining = (ptrdiff_t)encoder->end;
238-
remaining -= remaining ? (ptrdiff_t)encoder->ptr : encoder->bytes_needed;
238+
remaining -= remaining ? (ptrdiff_t)encoder->data.ptr : encoder->data.bytes_needed;
239239
remaining -= (ptrdiff_t)len;
240240
return unlikely(remaining < 0);
241241
}
242242

243243
static inline void advance_ptr(CborEncoder *encoder, size_t n)
244244
{
245245
if (encoder->end)
246-
encoder->ptr += n;
246+
encoder->data.ptr += n;
247247
else
248-
encoder->bytes_needed += n;
248+
encoder->data.bytes_needed += n;
249249
}
250250

251251
static inline CborError append_to_buffer(CborEncoder *encoder, const void *data, size_t len)
252252
{
253253
if (would_overflow(encoder, len)) {
254254
if (encoder->end != NULL) {
255-
len -= encoder->end - encoder->ptr;
255+
len -= encoder->end - encoder->data.ptr;
256256
encoder->end = NULL;
257-
encoder->bytes_needed = 0;
257+
encoder->data.bytes_needed = 0;
258258
}
259259

260260
advance_ptr(encoder, len);
261261
return CborErrorOutOfMemory;
262262
}
263263

264-
memcpy(encoder->ptr, data, len);
265-
encoder->ptr += len;
264+
memcpy(encoder->data.ptr, data, len);
265+
encoder->data.ptr += len;
266266
return CborNoError;
267267
}
268268

@@ -447,7 +447,7 @@ __attribute__((noinline))
447447
static CborError create_container(CborEncoder *encoder, CborEncoder *container, size_t length, uint8_t shiftedMajorType)
448448
{
449449
CborError err;
450-
container->ptr = encoder->ptr;
450+
container->data.ptr = encoder->data.ptr;
451451
container->end = encoder->end;
452452
++encoder->added;
453453
container->added = 0;
@@ -527,9 +527,9 @@ CborError cbor_encoder_create_map(CborEncoder *encoder, CborEncoder *mapEncoder,
527527
CborError cbor_encoder_close_container(CborEncoder *encoder, const CborEncoder *containerEncoder)
528528
{
529529
if (encoder->end)
530-
encoder->ptr = containerEncoder->ptr;
530+
encoder->data.ptr = containerEncoder->data.ptr;
531531
else
532-
encoder->bytes_needed = containerEncoder->bytes_needed;
532+
encoder->data.bytes_needed = containerEncoder->data.bytes_needed;
533533
encoder->end = containerEncoder->end;
534534
if (containerEncoder->flags & CborIteratorFlag_UnknownLength)
535535
return append_byte_to_buffer(encoder, BreakByte);

src/cborencoder_close_container_checked.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
*/
5656
CborError cbor_encoder_close_container_checked(CborEncoder *encoder, const CborEncoder *containerEncoder)
5757
{
58-
const uint8_t *ptr = encoder->ptr;
58+
const uint8_t *ptr = encoder->data.ptr;
5959
CborError err = cbor_encoder_close_container(encoder, containerEncoder);
6060
if (containerEncoder->flags & CborIteratorFlag_UnknownLength || encoder->end == NULL)
6161
return err;
6262

6363
/* check what the original length was */
6464
uint64_t actually_added;
65-
err = extract_number(&ptr, encoder->ptr, &actually_added);
65+
err = extract_number(&ptr, encoder->data.ptr, &actually_added);
6666
if (err)
6767
return err;
6868

tools/json2cbor/json2cbor.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ CborError decode_json(cJSON *json, CborEncoder *encoder)
332332
return err;
333333

334334
*encoder = container; // restore state
335-
encoder->ptr = newbuffer + (container.ptr - buffer);
335+
encoder->data.ptr = newbuffer + (container.data.ptr - buffer);
336336
encoder->end = newbuffer + buffersize;
337337
buffer = newbuffer;
338338
goto encode_double;
@@ -483,7 +483,7 @@ int main(int argc, char **argv)
483483
return EXIT_FAILURE;
484484
}
485485

486-
fwrite(buffer, 1, encoder.ptr - buffer, stdout);
486+
fwrite(buffer, 1, encoder.data.ptr - buffer, stdout);
487487
free(buffer);
488488
return EXIT_SUCCESS;
489489
}

0 commit comments

Comments
 (0)