Skip to content

Commit 9829c2a

Browse files
committed
WSHUB-458: 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 6679e85 commit 9829c2a

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
@@ -330,25 +330,21 @@ struct CborParserOperations
330330
* called before \ref read_bytes and \ref transfer_bytes to ensure it is safe
331331
* to read the requested number of bytes from the reader.
332332
*
333-
* \param token An opaque object passed to \ref cbor_parser_init_reader
334-
* that may be used to pass context information between the
335-
* \ref CborParserOperations methods.
333+
* \param value The CBOR value being parsed.
336334
*
337335
* \param len The number of bytes sought.
338336
*
339337
* \retval true \a len bytes may be read from the reader.
340338
* \retval false Insufficient data is available to be read at this time.
341339
*/
342-
bool (*can_read_bytes)(void *token, size_t len);
340+
bool (*can_read_bytes)(const struct CborValue *value, size_t len);
343341

344342
/**
345343
* Reads \a len bytes from the reader starting at \a offset bytes from
346344
* the current read position and copies them to \a dst. The read pointer
347345
* is *NOT* modified by this operation.
348346
*
349-
* \param token An opaque object passed to \ref cbor_parser_init_reader
350-
* that may be used to pass context information between the
351-
* \ref CborParserOperations methods.
347+
* \param value The CBOR value being parsed.
352348
*
353349
* \param dst The buffer the read bytes will be copied to.
354350
*
@@ -357,19 +353,17 @@ struct CborParserOperations
357353
*
358354
* \param len The number of bytes sought.
359355
*/
360-
void *(*read_bytes)(void *token, void *dst, size_t offset, size_t len);
356+
void *(*read_bytes)(const struct CborValue *value, void *dst, size_t offset, size_t len);
361357

362358
/**
363359
* Skips past \a len bytes from the reader without reading them. The read
364360
* pointer is advanced in the process.
365361
*
366-
* \param token An opaque object passed to \ref cbor_parser_init_reader
367-
* that may be used to pass context information between the
368-
* \ref CborParserOperations methods.
362+
* \param value The CBOR value being parsed.
369363
*
370364
* \param len The number of bytes skipped.
371365
*/
372-
void (*advance_bytes)(void *token, size_t len);
366+
void (*advance_bytes)(struct CborValue *value, size_t len);
373367

374368
/**
375369
* Overwrite the user-supplied pointer \a userptr with the address where the
@@ -379,9 +373,7 @@ struct CborParserOperations
379373
* This routine is used for accessing strings embedded in CBOR documents
380374
* (both text and binary strings).
381375
*
382-
* \param token An opaque object passed to \ref cbor_parser_init_reader
383-
* that may be used to pass context information between the
384-
* \ref CborParserOperations methods.
376+
* \param value The CBOR value being parsed.
385377
*
386378
* \param userptr The pointer that will be updated to reference the location
387379
* of the data in the buffer.
@@ -391,7 +383,7 @@ struct CborParserOperations
391383
*
392384
* \param len The number of bytes sought.
393385
*/
394-
CborError (*transfer_string)(void *token, const void **userptr, size_t offset, size_t len);
386+
CborError (*transfer_string)(struct CborValue *value, const void **userptr, size_t offset, size_t len);
395387
};
396388

397389
struct CborParser

src/cborinternal_p.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ static inline bool can_read_bytes(const CborValue *it, size_t n)
176176
if (CBOR_PARSER_READER_CONTROL >= 0) {
177177
if (it->parser->flags & CborParserFlag_ExternalSource || CBOR_PARSER_READER_CONTROL != 0) {
178178
#ifdef CBOR_PARSER_CAN_READ_BYTES_FUNCTION
179-
return CBOR_PARSER_CAN_READ_BYTES_FUNCTION(it->source.token, n);
179+
return CBOR_PARSER_CAN_READ_BYTES_FUNCTION(it, n);
180180
#else
181-
return it->parser->source.ops->can_read_bytes(it->source.token, n);
181+
return it->parser->source.ops->can_read_bytes(it, n);
182182
#endif
183183
}
184184
}
@@ -194,9 +194,9 @@ static inline void advance_bytes(CborValue *it, size_t n)
194194
if (CBOR_PARSER_READER_CONTROL >= 0) {
195195
if (it->parser->flags & CborParserFlag_ExternalSource || CBOR_PARSER_READER_CONTROL != 0) {
196196
#ifdef CBOR_PARSER_ADVANCE_BYTES_FUNCTION
197-
CBOR_PARSER_ADVANCE_BYTES_FUNCTION(it->source.token, n);
197+
CBOR_PARSER_ADVANCE_BYTES_FUNCTION(it, n);
198198
#else
199-
it->parser->source.ops->advance_bytes(it->source.token, n);
199+
it->parser->source.ops->advance_bytes(it, n);
200200
#endif
201201
return;
202202
}
@@ -210,9 +210,9 @@ static inline CborError transfer_string(CborValue *it, const void **ptr, size_t
210210
if (CBOR_PARSER_READER_CONTROL >= 0) {
211211
if (it->parser->flags & CborParserFlag_ExternalSource || CBOR_PARSER_READER_CONTROL != 0) {
212212
#ifdef CBOR_PARSER_TRANSFER_STRING_FUNCTION
213-
return CBOR_PARSER_TRANSFER_STRING_FUNCTION(it->source.token, ptr, offset, len);
213+
return CBOR_PARSER_TRANSFER_STRING_FUNCTION(it, ptr, offset, len);
214214
#else
215-
return it->parser->source.ops->transfer_string(it->source.token, ptr, offset, len);
215+
return it->parser->source.ops->transfer_string(it, ptr, offset, len);
216216
#endif
217217
}
218218
}
@@ -231,9 +231,9 @@ static inline void *read_bytes_unchecked(const CborValue *it, void *dst, size_t
231231
if (CBOR_PARSER_READER_CONTROL >= 0) {
232232
if (it->parser->flags & CborParserFlag_ExternalSource || CBOR_PARSER_READER_CONTROL != 0) {
233233
#ifdef CBOR_PARSER_READ_BYTES_FUNCTION
234-
return CBOR_PARSER_READ_BYTES_FUNCTION(it->source.token, dst, offset, n);
234+
return CBOR_PARSER_READ_BYTES_FUNCTION(it, dst, offset, n);
235235
#else
236-
return it->parser->source.ops->read_bytes(it->source.token, dst, offset, n);
236+
return it->parser->source.ops->read_bytes(it, dst, offset, n);
237237
#endif
238238
}
239239
}

tests/parser/tst_parser.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -763,21 +763,21 @@ struct Input {
763763
};
764764

765765
static const CborParserOperations byteArrayOps = {
766-
/* can_read_bytes = */ [](void *token, size_t len) {
767-
auto input = static_cast<Input *>(token);
766+
/* can_read_bytes = */ [](const CborValue *value, size_t len) {
767+
auto input = static_cast<Input *>(value->source.token);
768768
return input->data.size() - input->consumed >= int(len);
769769
},
770-
/* read_bytes = */ [](void *token, void *dst, size_t offset, size_t len) {
771-
auto input = static_cast<Input *>(token);
770+
/* read_bytes = */ [](const CborValue *value, void *dst, size_t offset, size_t len) {
771+
auto input = static_cast<Input *>(value->source.token);
772772
return memcpy(dst, input->data.constData() + input->consumed + offset, len);
773773
},
774-
/* advance_bytes = */ [](void *token, size_t len) {
775-
auto input = static_cast<Input *>(token);
774+
/* advance_bytes = */ [](CborValue *value, size_t len) {
775+
auto input = static_cast<Input *>(value->source.token);
776776
input->consumed += int(len);
777777
},
778-
/* transfer_string = */ [](void *token, const void **userptr, size_t offset, size_t len) {
778+
/* transfer_string = */ [](CborValue *value, const void **userptr, size_t offset, size_t len) {
779779
// ###
780-
auto input = static_cast<Input *>(token);
780+
auto input = static_cast<Input *>(value->source.token);
781781
if (input->data.size() - input->consumed < int(len + offset))
782782
return CborErrorUnexpectedEOF;
783783
input->consumed += int(offset);

0 commit comments

Comments
 (0)