Skip to content

Commit 3394f51

Browse files
committed
cborparser: Pass CborValue to operation routines.
The `token` parameter is not sufficient since it is effectively shared by all `CborValue` instances. Since `tinycbor` often uses a temporary `CborValue` context to perform some operation, we need to store our context inside that `CborValue` so that we don't pollute the global state of the reader.
1 parent 286d132 commit 3394f51

File tree

3 files changed

+24
-32
lines changed

3 files changed

+24
-32
lines changed

src/cbor.h

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -334,25 +334,21 @@ struct CborParserOperations
334334
* called before \ref read_bytes and \ref transfer_bytes to ensure it is safe
335335
* to read the requested number of bytes from the reader.
336336
*
337-
* \param token An opaque object passed to \ref cbor_parser_init_reader
338-
* that may be used to pass context information between the
339-
* \ref CborParserOperations methods.
337+
* \param value The CBOR value being parsed.
340338
*
341339
* \param len The number of bytes sought.
342340
*
343341
* \retval true \a len bytes may be read from the reader.
344342
* \retval false Insufficient data is available to be read at this time.
345343
*/
346-
bool (*can_read_bytes)(void *token, size_t len);
344+
bool (*can_read_bytes)(const struct CborValue *value, size_t len);
347345

348346
/**
349347
* Reads \a len bytes from the reader starting at \a offset bytes from
350348
* the current read position and copies them to \a dst. The read pointer
351349
* is *NOT* modified by this operation.
352350
*
353-
* \param token An opaque object passed to \ref cbor_parser_init_reader
354-
* that may be used to pass context information between the
355-
* \ref CborParserOperations methods.
351+
* \param value The CBOR value being parsed.
356352
*
357353
* \param dst The buffer the read bytes will be copied to.
358354
*
@@ -361,19 +357,17 @@ struct CborParserOperations
361357
*
362358
* \param len The number of bytes sought.
363359
*/
364-
void *(*read_bytes)(void *token, void *dst, size_t offset, size_t len);
360+
void *(*read_bytes)(const struct CborValue *value, void *dst, size_t offset, size_t len);
365361

366362
/**
367363
* Skips past \a len bytes from the reader without reading them. The read
368364
* pointer is advanced in the process.
369365
*
370-
* \param token An opaque object passed to \ref cbor_parser_init_reader
371-
* that may be used to pass context information between the
372-
* \ref CborParserOperations methods.
366+
* \param value The CBOR value being parsed.
373367
*
374368
* \param len The number of bytes skipped.
375369
*/
376-
void (*advance_bytes)(void *token, size_t len);
370+
void (*advance_bytes)(struct CborValue *value, size_t len);
377371

378372
/**
379373
* Overwrite the user-supplied pointer \a userptr with the address where the
@@ -383,9 +377,7 @@ struct CborParserOperations
383377
* This routine is used for accessing strings embedded in CBOR documents
384378
* (both text and binary strings).
385379
*
386-
* \param token An opaque object passed to \ref cbor_parser_init_reader
387-
* that may be used to pass context information between the
388-
* \ref CborParserOperations methods.
380+
* \param value The CBOR value being parsed.
389381
*
390382
* \param userptr The pointer that will be updated to reference the location
391383
* of the data in the buffer.
@@ -395,7 +387,7 @@ struct CborParserOperations
395387
*
396388
* \param len The number of bytes sought.
397389
*/
398-
CborError (*transfer_string)(void *token, const void **userptr, size_t offset, size_t len);
390+
CborError (*transfer_string)(struct CborValue *value, const void **userptr, size_t offset, size_t len);
399391
};
400392

401393
struct CborParser

src/cborinternal_p.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,9 +203,9 @@ static inline bool can_read_bytes(const CborValue *it, size_t n)
203203
if (CBOR_PARSER_READER_CONTROL >= 0) {
204204
if (it->parser->flags & CborParserFlag_ExternalSource || CBOR_PARSER_READER_CONTROL != 0) {
205205
#ifdef CBOR_PARSER_CAN_READ_BYTES_FUNCTION
206-
return CBOR_PARSER_CAN_READ_BYTES_FUNCTION(it->source.token, n);
206+
return CBOR_PARSER_CAN_READ_BYTES_FUNCTION(it, n);
207207
#else
208-
return it->parser->source.ops->can_read_bytes(it->source.token, n);
208+
return it->parser->source.ops->can_read_bytes(it, n);
209209
#endif
210210
}
211211
}
@@ -221,9 +221,9 @@ static inline void advance_bytes(CborValue *it, size_t n)
221221
if (CBOR_PARSER_READER_CONTROL >= 0) {
222222
if (it->parser->flags & CborParserFlag_ExternalSource || CBOR_PARSER_READER_CONTROL != 0) {
223223
#ifdef CBOR_PARSER_ADVANCE_BYTES_FUNCTION
224-
CBOR_PARSER_ADVANCE_BYTES_FUNCTION(it->source.token, n);
224+
CBOR_PARSER_ADVANCE_BYTES_FUNCTION(it, n);
225225
#else
226-
it->parser->source.ops->advance_bytes(it->source.token, n);
226+
it->parser->source.ops->advance_bytes(it, n);
227227
#endif
228228
return;
229229
}
@@ -237,9 +237,9 @@ static inline CborError transfer_string(CborValue *it, const void **ptr, size_t
237237
if (CBOR_PARSER_READER_CONTROL >= 0) {
238238
if (it->parser->flags & CborParserFlag_ExternalSource || CBOR_PARSER_READER_CONTROL != 0) {
239239
#ifdef CBOR_PARSER_TRANSFER_STRING_FUNCTION
240-
return CBOR_PARSER_TRANSFER_STRING_FUNCTION(it->source.token, ptr, offset, len);
240+
return CBOR_PARSER_TRANSFER_STRING_FUNCTION(it, ptr, offset, len);
241241
#else
242-
return it->parser->source.ops->transfer_string(it->source.token, ptr, offset, len);
242+
return it->parser->source.ops->transfer_string(it, ptr, offset, len);
243243
#endif
244244
}
245245
}
@@ -258,9 +258,9 @@ static inline void *read_bytes_unchecked(const CborValue *it, void *dst, size_t
258258
if (CBOR_PARSER_READER_CONTROL >= 0) {
259259
if (it->parser->flags & CborParserFlag_ExternalSource || CBOR_PARSER_READER_CONTROL != 0) {
260260
#ifdef CBOR_PARSER_READ_BYTES_FUNCTION
261-
return CBOR_PARSER_READ_BYTES_FUNCTION(it->source.token, dst, offset, n);
261+
return CBOR_PARSER_READ_BYTES_FUNCTION(it, dst, offset, n);
262262
#else
263-
return it->parser->source.ops->read_bytes(it->source.token, dst, offset, n);
263+
return it->parser->source.ops->read_bytes(it, dst, offset, n);
264264
#endif
265265
}
266266
}

tests/parser/tst_parser.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -765,21 +765,21 @@ struct Input {
765765
};
766766

767767
static const CborParserOperations byteArrayOps = {
768-
/* can_read_bytes = */ [](void *token, size_t len) {
769-
auto input = static_cast<Input *>(token);
768+
/* can_read_bytes = */ [](const CborValue *value, size_t len) {
769+
auto input = static_cast<Input *>(value->source.token);
770770
return input->data.size() - input->consumed >= int(len);
771771
},
772-
/* read_bytes = */ [](void *token, void *dst, size_t offset, size_t len) {
773-
auto input = static_cast<Input *>(token);
772+
/* read_bytes = */ [](const CborValue *value, void *dst, size_t offset, size_t len) {
773+
auto input = static_cast<Input *>(value->source.token);
774774
return memcpy(dst, input->data.constData() + input->consumed + offset, len);
775775
},
776-
/* advance_bytes = */ [](void *token, size_t len) {
777-
auto input = static_cast<Input *>(token);
776+
/* advance_bytes = */ [](CborValue *value, size_t len) {
777+
auto input = static_cast<Input *>(value->source.token);
778778
input->consumed += int(len);
779779
},
780-
/* transfer_string = */ [](void *token, const void **userptr, size_t offset, size_t len) {
780+
/* transfer_string = */ [](CborValue *value, const void **userptr, size_t offset, size_t len) {
781781
// ###
782-
auto input = static_cast<Input *>(token);
782+
auto input = static_cast<Input *>(value->source.token);
783783
if (input->data.size() - input->consumed < int(len + offset))
784784
return CborErrorUnexpectedEOF;
785785
input->consumed += int(offset);

0 commit comments

Comments
 (0)