Skip to content

Commit

Permalink
Move tag-related and spiffy-related code to their own source files
Browse files Browse the repository at this point in the history
qcbor_decode.c was just too big

* Move all tag-related code into qcbor_tag_decode.[ch]

* split out spiffy decode source and DecodeNesting

---------

Co-authored-by: Laurence Lundblade <[email protected]>
  • Loading branch information
laurencelundblade and Laurence Lundblade authored Dec 1, 2024
1 parent c45d98f commit 41e40d5
Show file tree
Hide file tree
Showing 14 changed files with 5,869 additions and 5,706 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ target_sources(qcbor
PRIVATE
src/ieee754.c
src/qcbor_decode.c
src/qcbor_spiffy_decode.c
src/qcbor_tag_decode.c
src/qcbor_number_decode.c
src/qcbor_encode.c
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LIBS=-lm
CFLAGS=$(CMD_LINE) -I inc -I test -Os -fPIC


QCBOR_OBJ=src/UsefulBuf.o src/qcbor_encode.o src/qcbor_decode.o src/qcbor_number_decode.o src/qcbor_tag_decode.o src/ieee754.o src/qcbor_err_to_str.o
QCBOR_OBJ=src/UsefulBuf.o src/qcbor_encode.o src/qcbor_decode.o src/qcbor_spiffy_decode.o src/qcbor_number_decode.o src/qcbor_tag_decode.o src/ieee754.o src/qcbor_err_to_str.o

TEST_OBJ=test/UsefulBuf_Tests.o test/qcbor_encode_tests.o \
test/qcbor_decode_tests.o test/run_tests.o \
Expand Down
6 changes: 6 additions & 0 deletions QCBOR.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
0FA9BEBA216DC7AD00BA646B /* qcbor_encode_tests.c in Sources */ = {isa = PBXBuildFile; fileRef = 0FA9BEB8216DC7AD00BA646B /* qcbor_encode_tests.c */; };
0FA9BEBD216DE31700BA646B /* UsefulBuf_Tests.c in Sources */ = {isa = PBXBuildFile; fileRef = 0FA9BEBC216DE31700BA646B /* UsefulBuf_Tests.c */; };
E7180F232CF1657B00513186 /* qcbor_number_decode.c in Sources */ = {isa = PBXBuildFile; fileRef = E7180F222CF1657A00513186 /* qcbor_number_decode.c */; };
E7180F262CF8F2EB00513186 /* qcbor_spiffy_decode.c in Sources */ = {isa = PBXBuildFile; fileRef = E7180F252CF8F2EB00513186 /* qcbor_spiffy_decode.c */; };
E73B57592161CA690080D658 /* ieee754.c in Sources */ = {isa = PBXBuildFile; fileRef = E73B57582161CA690080D658 /* ieee754.c */; };
E73B575E2161CA7C0080D658 /* float_tests.c in Sources */ = {isa = PBXBuildFile; fileRef = E73B575A2161CA7C0080D658 /* float_tests.c */; };
E73B575F2161CA7C0080D658 /* half_to_double_from_rfc7049.c in Sources */ = {isa = PBXBuildFile; fileRef = E73B575D2161CA7C0080D658 /* half_to_double_from_rfc7049.c */; };
Expand Down Expand Up @@ -161,6 +162,8 @@
E7180F212CF1638F00513186 /* decode_private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = decode_private.h; path = src/decode_private.h; sourceTree = "<group>"; };
E7180F222CF1657A00513186 /* qcbor_number_decode.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = qcbor_number_decode.c; path = src/qcbor_number_decode.c; sourceTree = "<group>"; };
E7180F242CF2390100513186 /* qcbor_number_decode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = qcbor_number_decode.h; path = inc/qcbor/qcbor_number_decode.h; sourceTree = "<group>"; };
E7180F252CF8F2EB00513186 /* qcbor_spiffy_decode.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; name = qcbor_spiffy_decode.c; path = src/qcbor_spiffy_decode.c; sourceTree = "<group>"; };
E7180F272CF917D200513186 /* decode_nesting.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = decode_nesting.h; path = src/decode_nesting.h; sourceTree = "<group>"; };
E73B57572161CA680080D658 /* ieee754.h */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 3; lastKnownFileType = sourcecode.c.h; name = ieee754.h; path = src/ieee754.h; sourceTree = "<group>"; tabWidth = 3; };
E73B57582161CA690080D658 /* ieee754.c */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 3; lastKnownFileType = sourcecode.c.c; name = ieee754.c; path = src/ieee754.c; sourceTree = "<group>"; tabWidth = 3; };
E73B575A2161CA7C0080D658 /* float_tests.c */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 3; lastKnownFileType = sourcecode.c.c; name = float_tests.c; path = test/float_tests.c; sourceTree = "<group>"; tabWidth = 3; };
Expand Down Expand Up @@ -294,8 +297,10 @@
children = (
E776E08D214AE07500E67947 /* UsefulBuf.c */,
E776E08C214AE07400E67947 /* qcbor_encode.c */,
E7180F272CF917D200513186 /* decode_nesting.h */,
E7180F212CF1638F00513186 /* decode_private.h */,
E776E08E214AE07500E67947 /* qcbor_decode.c */,
E7180F252CF8F2EB00513186 /* qcbor_spiffy_decode.c */,
E7180F222CF1657A00513186 /* qcbor_number_decode.c */,
E7CA1F1D2C8C337E0008F454 /* qcbor_tag_decode.c */,
E73B57572161CA680080D658 /* ieee754.h */,
Expand Down Expand Up @@ -564,6 +569,7 @@
E7C960B92800A09E00FB537C /* ub-example.c in Sources */,
E743D0F324AD08020017899F /* example.c in Sources */,
0FA9BEBD216DE31700BA646B /* UsefulBuf_Tests.c in Sources */,
E7180F262CF8F2EB00513186 /* qcbor_spiffy_decode.c in Sources */,
E7180F232CF1657B00513186 /* qcbor_number_decode.c in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,19 @@ maps and arrays without the caller having to do anything. This
includes mixed definite and indefinte maps and arrays. (Some work
remains to support map searching with indefinite length strings.)

## v2

** Tag decoding

** Map Sorting

** Serialization Modes

** Files Rearrangement

** Bignumber ??


## Comparison to TinyCBOR

TinyCBOR is a popular widely used implementation. Like QCBOR,
Expand Down
172 changes: 2 additions & 170 deletions inc/qcbor/qcbor_decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -1214,137 +1214,6 @@ QCBORError
QCBORDecode_EndCheck(QCBORDecodeContext *pCtx);


#ifndef QCBOR_DISABLE_TAGS
/**
* @brief Returns the tag numbers for an item.
*
* @param[in] pCtx The decoder context.
* @param[out] puTagNumber The returned tag number.
*
* In QCBOR v2, all tag numbers on an item MUST be fetched with this
* method. If not, @ref QCBOR_ERR_UNPROCESSED_TAG_NUMBER will
* occur. This is a major change from QCBORv1. The QCBOR v1 behavior
* is too lax for proper CBOR decoding. When a tag number occurs it
* indicates the item is a new data type (except for a few tag numbers
* that are hints). Note also that in RFC 7049, tag numbers were
* incorrectly characterized as optional implying they could be
* ignored.
*
* In typical item decoding, tag numbers are not used, not present and
* not expected. There's no need to call this.
*
* When the protocol being decoded does use a tag number then this
* must be called for the items were the tag numbers occur before the
* items themselves are decoded. Making this call prevents the
* @ref QCBOR_ERR_UNPROCESSED_TAG_NUMBER error, but the caller still has to
* check that the tag number is the right one. Probably the tag number
* will be used to switch the flow of the decoder.
*
* It's possible that an item might use the presence/absence of a tag
* number to switch the flow of decoding. If there's a possibility of
* a tag number then this must be called.
*
* If this is called and there is no tag number, then this will return
* @ref QCBOR_SUCCESS and the tag number returned will be
* @ref CBOR_TAG_INVALID64.
*
* Usually there is only one tag number per item, but CBOR allows
* more. That it allows nesting of tags where the content of one tag
* is another tag. If there are multiple tag numbers, this must be
* called multiple times. This only returns one tag number at a time,
* because tag numbers are typically processed one at a time.
*
* If there is an error decoding the tag or the item it is on, the
* error code will be set and the tag number @ref CBOR_TAG_INVALID64
* will be returned. That is, @ref CBOR_TAG_INVALID64 will be returned if
* there is a decode error or there is no tag number.
*/
void
QCBORDecode_VGetNextTagNumber(QCBORDecodeContext *pCtx, uint64_t *puTagNumber);


/**
* @brief Returns the tag numbers for an item.
*
* @param[in] pCtx The decoder context.
* @param[out] puTagNumber The returned tag number.
*
* @return See error table of decoding errors set by QCBORDecode_VGetNext().
*
* Like QCBORDecode_VGetNextTagNumber(), but returns the
* error rather than set last error.
*/
QCBORError
QCBORDecode_GetNextTagNumber(QCBORDecodeContext *pCtx, uint64_t *puTagNumber);


/**
* @brief Returns the tag numbers for a decoded item.
*
* @param[in] pCtx The decoder context.
* @param[in] pItem The CBOR item to get the tag for.
* @param[in] uIndex The index of the tag to get.
*
* @returns The nth tag number or @ref CBOR_TAG_INVALID64.
*
* Typically, this is only used with @ref QCBOR_DECODE_CONFIG_UNPROCESSED_TAG_NUMBERS.
* Normally, tag numbers are processed QCBORDecode_VGetNextTagNumber() or
* QCBORTagContentCallBack.
*
* When QCBOR decodes an item that is a tag, it will fully decode tags
* it is able to. Tags that it is unable to process are put in a list
* in the QCBORItem.
*
* Tags nest. Here the tag with index 0 is the outermost, the one
* furthest form the data item that is the tag content. This is
* the opposite order of QCBORDecode_GetNthTag(), but more
* useful.
*
* Deep tag nesting is rare so this implementation imposes a limit of
* @ref QCBOR_MAX_TAGS_PER_ITEM on nesting and returns @ref
* QCBOR_ERR_TOO_MANY_TAGS if there are more. This is a limit of this
* implementation, not of CBOR. (To be able to handle deeper nesting,
* the constant can be increased and the library recompiled. It will
* use more memory).
*
* See also @ref Tag-Decoding @ref CBORTags, @ref Tag-Usage and @ref Tags-Overview.
*
* To reduce memory used by a @ref QCBORItem, tag numbers larger than
* @c UINT16_MAX are mapped so the tag numbers in @c uTags should be
* accessed with this function rather than directly.
*
* This returns @ref CBOR_TAG_INVALID64 if any error occurred when
* getting the item. This is also returned if there are no tags on the
* item or no tag at @c uIndex.
*/
uint64_t
QCBORDecode_GetNthTagNumber(const QCBORDecodeContext *pCtx, const QCBORItem *pItem, uint8_t uIndex);


/**
* @brief Returns the tag numbers for last-decoded item.
*
* @param[in] pCtx The decoder context.
* @param[in] uIndex The index of the tag to get.
*
* @returns The nth tag number or @ref CBOR_TAG_INVALID64.
*
* This returns tags of the most recently decoded item. See
* QCBORDecode_GetNthTagNumber(). This is particularly of use for spiffy
* decode functions that don't return a @ref QCBORItem.
*
* This does not work for QCBORDecode_GetNext(),
* QCBORDecode_PeekNext(), QCBORDecode_VPeekNext() or
* QCBORDecode_VGetNextConsume() but these all return a
* @ref QCBORItem, so it is not necessary.
*
* If a decoding error is set, then this returns @ref CBOR_TAG_INVALID64.
*/
uint64_t
QCBORDecode_GetNthTagNumberOfLast(QCBORDecodeContext *pCtx, uint8_t uIndex);

#endif /* ! QCBOR_DISABLE_TAGS */


/**
* @brief Check that a decode completed successfully.
Expand Down Expand Up @@ -1551,12 +1420,12 @@ QCBORDecode_SetError(QCBORDecodeContext *pCtx, QCBORError uError);
* which causes no error to be returned when un processed tag numbers
* are encountered.
*
* Second, it installs all the same tag content handlers that v1 had hardwwired.
* Second, it installs all the same tag content handlers that were hardwired in v1.
* QCBORDecode_InstallTagDecoders(pMe, QCBORDecode_TagDecoderTablev1, NULL);
*
* This is listed as deprecated even though it is new in QCBOR v2
* because it recommended that v1 mode not be used because the tag
* number processing is too loose.
* number processing is too loose. See @ref v2-Tag-Decoding.
*
* This links in a fair bit of object code for all the tag content
* handlers that were always present in v1. To get the v1 behavior
Expand All @@ -1568,43 +1437,6 @@ void
QCBORDecode_CompatibilityV1(QCBORDecodeContext *pCtx);



#ifndef QCBOR_DISABLE_TAGS

/**
* @brief Returns the tag numbers for an item. (deprecated).
*
* @param[in] pCtx The decoder context.
* @param[in] uIndex The index of the tag to get.
*
* This is the same as QCBORDecode_GetNthTagNumber() but the order is
* opposite when there are multiple tags. @c uIndex 0 is the tag
* number closest to the tag content. QCBORDecode_GetNthTagNumber() is
* more useful for checking the next tag number and switching the
* decode flow.
*/
uint64_t
QCBORDecode_GetNthTag(QCBORDecodeContext *pCtx, const QCBORItem *pItem, uint32_t uIndex);


/**
* @brief Returns the tag numbers for last-decoded item (deprecated).
*
* @param[in] pCtx The decoder context.
* @param[in] uIndex The index of the tag to get.
*
* @returns The nth tag number or CBOR_TAG_INVALID64.
*
* This is the same as QCBORDecode_GetNthTagNumberOfLast() but the
* order is opposite when there are multiple tags. @c uIndex 0 is the
* tag number closest to the tag content.
* QCBORDecode_GetNthTagNumber() is more useful for checking
* the next tag number and switching the decode flow.
*/
uint64_t
QCBORDecode_GetNthTagOfLast(const QCBORDecodeContext *pCtx, uint32_t uIndex);

#endif /* ! QCBOR_DISABLE_TAGS */
/* ========================================================================= *
* END OF DEPRECATED FUNCTIONS *
* ========================================================================= */
Expand Down
Loading

0 comments on commit 41e40d5

Please sign in to comment.