Skip to content

Commit 67140d8

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 69d79ab commit 67140d8

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
@@ -333,25 +333,21 @@ struct CborParserOperations
333333
* called before \ref read_bytes and \ref transfer_bytes to ensure it is safe
334334
* to read the requested number of bytes from the reader.
335335
*
336-
* \param token An opaque object passed to \ref cbor_parser_init_reader
337-
* that may be used to pass context information between the
338-
* \ref CborParserOperations methods.
336+
* \param value The CBOR value being parsed.
339337
*
340338
* \param len The number of bytes sought.
341339
*
342340
* \retval true \a len bytes may be read from the reader.
343341
* \retval false Insufficient data is available to be read at this time.
344342
*/
345-
bool (*can_read_bytes)(void *token, size_t len);
343+
bool (*can_read_bytes)(const struct CborValue *value, size_t len);
346344

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

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

377371
/**
378372
* Overwrite the user-supplied pointer \a userptr with the address where the
@@ -382,9 +376,7 @@ struct CborParserOperations
382376
* This routine is used for accessing strings embedded in CBOR documents
383377
* (both text and binary strings).
384378
*
385-
* \param token An opaque object passed to \ref cbor_parser_init_reader
386-
* that may be used to pass context information between the
387-
* \ref CborParserOperations methods.
379+
* \param value The CBOR value being parsed.
388380
*
389381
* \param userptr The pointer that will be updated to reference the location
390382
* of the data in the buffer.
@@ -394,7 +386,7 @@ struct CborParserOperations
394386
*
395387
* \param len The number of bytes sought.
396388
*/
397-
CborError (*transfer_string)(void *token, const void **userptr, size_t offset, size_t len);
389+
CborError (*transfer_string)(struct CborValue *value, const void **userptr, size_t offset, size_t len);
398390
};
399391

400392
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)