1
1
/****************************************************************************
2
2
**
3
- ** Copyright (C) 2016 Intel Corporation
3
+ ** Copyright (C) 2021 Intel Corporation
4
4
**
5
5
** Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
** of this software and associated documentation files (the "Software"), to deal
@@ -208,6 +208,18 @@ void cbor_encoder_init(CborEncoder *encoder, uint8_t *buffer, size_t size, int f
208
208
encoder -> flags = flags ;
209
209
}
210
210
211
+ void cbor_encoder_init_writer (CborEncoder * encoder , CborEncoderWriteFunction writer , void * token )
212
+ {
213
+ #ifdef CBOR_ENCODER_WRITE_FUNCTION
214
+ (void ) writer ;
215
+ #else
216
+ encoder -> data .writer = writer ;
217
+ #endif
218
+ encoder -> end = (uint8_t * )token ;
219
+ encoder -> remaining = 2 ;
220
+ encoder -> flags = CborIteratorFlag_WriterFunction ;
221
+ }
222
+
211
223
static inline void put16 (void * where , uint16_t v )
212
224
{
213
225
uint16_t v_be = cbor_htons (v );
@@ -221,8 +233,12 @@ static inline void put16(void *where, uint16_t v)
221
233
* being created in the tinycbor output */
222
234
static inline bool isOomError (CborError err )
223
235
{
224
- (void ) err ;
225
- return true;
236
+ if (CBOR_ENCODER_WRITER_CONTROL < 0 )
237
+ return true;
238
+
239
+ /* CborErrorOutOfMemory is the only negative error code, intentionally
240
+ * so we can write the test like this */
241
+ return (int )err < 0 ;
226
242
}
227
243
228
244
static inline void put32 (void * where , uint32_t v )
@@ -253,8 +269,20 @@ static inline void advance_ptr(CborEncoder *encoder, size_t n)
253
269
encoder -> data .bytes_needed += n ;
254
270
}
255
271
256
- static inline CborError append_to_buffer (CborEncoder * encoder , const void * data , size_t len )
272
+ static inline CborError append_to_buffer (CborEncoder * encoder , const void * data , size_t len ,
273
+ CborEncoderAppendType appendType )
257
274
{
275
+ if (CBOR_ENCODER_WRITER_CONTROL >= 0 ) {
276
+ if (encoder -> flags & CborIteratorFlag_WriterFunction || CBOR_ENCODER_WRITER_CONTROL != 0 ) {
277
+ # ifdef CBOR_ENCODER_WRITE_FUNCTION
278
+ return CBOR_ENCODER_WRITE_FUNCTION (encoder -> end , data , len , appendType );
279
+ # else
280
+ return encoder -> data .writer (encoder -> end , data , len , appendType );
281
+ # endif
282
+ }
283
+ }
284
+
285
+ #if CBOR_ENCODER_WRITER_CONTROL <= 0
258
286
if (would_overflow (encoder , len )) {
259
287
if (encoder -> end != NULL ) {
260
288
len -= encoder -> end - encoder -> data .ptr ;
@@ -268,12 +296,13 @@ static inline CborError append_to_buffer(CborEncoder *encoder, const void *data,
268
296
269
297
memcpy (encoder -> data .ptr , data , len );
270
298
encoder -> data .ptr += len ;
299
+ #endif
271
300
return CborNoError ;
272
301
}
273
302
274
303
static inline CborError append_byte_to_buffer (CborEncoder * encoder , uint8_t byte )
275
304
{
276
- return append_to_buffer (encoder , & byte , 1 );
305
+ return append_to_buffer (encoder , & byte , 1 , CborEncoderAppendCborData );
277
306
}
278
307
279
308
static inline CborError encode_number_no_update (CborEncoder * encoder , uint64_t ui , uint8_t shiftedMajorType )
@@ -302,7 +331,7 @@ static inline CborError encode_number_no_update(CborEncoder *encoder, uint64_t u
302
331
* bufstart = shiftedMajorType + Value8Bit + more ;
303
332
}
304
333
305
- return append_to_buffer (encoder , bufstart , bufend - bufstart );
334
+ return append_to_buffer (encoder , bufstart , bufend - bufstart , CborEncoderAppendCborData );
306
335
}
307
336
308
337
static inline void saturated_decrement (CborEncoder * encoder )
@@ -399,7 +428,7 @@ CborError cbor_encode_floating_point(CborEncoder *encoder, CborType fpType, cons
399
428
else
400
429
put16 (buf + 1 , * (const uint16_t * )value );
401
430
saturated_decrement (encoder );
402
- return append_to_buffer (encoder , buf , size + 1 );
431
+ return append_to_buffer (encoder , buf , size + 1 , CborEncoderAppendCborData );
403
432
}
404
433
405
434
/**
@@ -418,7 +447,7 @@ static CborError encode_string(CborEncoder *encoder, size_t length, uint8_t shif
418
447
CborError err = encode_number (encoder , length , shiftedMajorType );
419
448
if (err && !isOomError (err ))
420
449
return err ;
421
- return append_to_buffer (encoder , string , length );
450
+ return append_to_buffer (encoder , string , length , CborEncoderAppendStringData );
422
451
}
423
452
424
453
/**
@@ -466,9 +495,12 @@ static CborError create_container(CborEncoder *encoder, CborEncoder *container,
466
495
saturated_decrement (encoder );
467
496
container -> remaining = length + 1 ; /* overflow ok on CborIndefiniteLength */
468
497
498
+ cbor_static_assert ((int )CborIteratorFlag_ContainerIsMap_ == (int )CborIteratorFlag_ContainerIsMap );
469
499
cbor_static_assert (((MapType << MajorTypeShift ) & CborIteratorFlag_ContainerIsMap ) == CborIteratorFlag_ContainerIsMap );
470
500
cbor_static_assert (((ArrayType << MajorTypeShift ) & CborIteratorFlag_ContainerIsMap ) == 0 );
471
501
container -> flags = shiftedMajorType & CborIteratorFlag_ContainerIsMap ;
502
+ if (CBOR_ENCODER_WRITER_CONTROL == 0 )
503
+ container -> flags |= encoder -> flags & CborIteratorFlag_WriterFunction ;
472
504
473
505
if (length == CborIndefiniteLength ) {
474
506
container -> flags |= CborIteratorFlag_UnknownLength ;
0 commit comments