Option to disable decoding of tags not commonly used to reduce object code (#74)
Disables UUID, Regex, MIME, B64, B64URL. They can still be decoded if they are untagged strings.
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/README.md b/README.md
index 80abd96..1f689c8 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# QCBOR
-QCBOR is a near-complete implementation of
+QCBOR is an implementation of nearly everything in
[RFC8949](https://tools.ietf.org/html/rfc8949). This RFC defines the
Concise Binary Object Representation (CBOR). Since RFC 8949 is fully
compatible with RFC 7049, this is also a near-complete implementation
@@ -235,8 +235,8 @@
| | smallest | largest |
|---------------|----------|---------|
| encode only | 850 | 2100 |
- | decode only | 2050 | 13400 |
- | combined | 2900 | 15500 |
+ | decode only | 2000 | 13400 |
+ | combined | 2850 | 15500 |
From the table above, one can see that the amount of code pulled in
from the QCBOR library varies a lot, ranging from 1KB to 15KB. The
@@ -274,12 +274,13 @@
code because Usefulbuf provides similar defenses and this code was
carefully written to be defensive.
- Disable features with defines like
- QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA (saves about 400 bytes)
- QCBOR_DISABLE_ENCODE_USAGE_GUARDS (saves about 150), and
- QCBOR_DISABLE_PREFERRED_FLOAT (saves about 900 bytes), and
- QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS (saves about 400 bytes). More
- of these defines are planned than are currently implemented.
+ Disable features with defines like:
+ QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA (saves about 400 bytes)
+ QCBOR_DISABLE_ENCODE_USAGE_GUARDS (saves about 150), and
+ QCBOR_DISABLE_PREFERRED_FLOAT (saves about 900 bytes), and
+ QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS (saves about 400 bytes).
+ QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS (saves about 200 bytes).
+ QCBOR_DISABLE_UNCOMMON_TAGS (saves about 100 bytes).
If QCBOR is installed as a shared library, then of course only one
copy of the code is in memory no matter how many applications use it.
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index fc9cf2b..ebcbc97 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -2099,6 +2099,7 @@
#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
+#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
/**
* @brief Decode the MIME type tag
*
@@ -2120,6 +2121,7 @@
return QCBOR_SUCCESS;
}
+#endif /* QCBOR_DISABLE_UNCOMMON_TAGS */
/**
@@ -2142,10 +2144,12 @@
{CBOR_TAG_NEG_BIGNUM, QCBOR_TYPE_NEGBIGNUM | IS_BYTE_STRING_BIT},
{CBOR_TAG_CBOR, QBCOR_TYPE_WRAPPED_CBOR | IS_BYTE_STRING_BIT},
{CBOR_TAG_URI, QCBOR_TYPE_URI},
+#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
{CBOR_TAG_B64URL, QCBOR_TYPE_BASE64URL},
{CBOR_TAG_B64, QCBOR_TYPE_BASE64},
{CBOR_TAG_REGEX, QCBOR_TYPE_REGEX},
{CBOR_TAG_BIN_UUID, QCBOR_TYPE_UUID | IS_BYTE_STRING_BIT},
+#endif /* QCBOR_DISABLE_UNCOMMON_TAGS */
{CBOR_TAG_CBOR_SEQUENCE, QBCOR_TYPE_WRAPPED_CBOR_SEQUENCE | IS_BYTE_STRING_BIT},
{CBOR_TAG_INVALID16, QCBOR_TYPE_NONE}
};
@@ -2254,10 +2258,11 @@
uTagToProcess == CBOR_TAG_BIGFLOAT) {
uReturn = QCBORDecode_MantissaAndExponent(pMe, pDecodedItem);
#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
-
+#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
} else if(uTagToProcess == CBOR_TAG_MIME ||
uTagToProcess == CBOR_TAG_BINARY_MIME) {
uReturn = DecodeMIME(pDecodedItem);
+#endif /* QCBOR_DISABLE_UNCOMMON_TAGS */
} else {
/* See if it is a pass-through byte/text string tag; process if so */
diff --git a/test/qcbor_decode_tests.c b/test/qcbor_decode_tests.c
index fbc292f..acde3ca 100644
--- a/test/qcbor_decode_tests.c
+++ b/test/qcbor_decode_tests.c
@@ -6685,10 +6685,12 @@
return 23;
}
+#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
return 30;
}
+#endif
QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
return 31;
@@ -6702,10 +6704,12 @@
return 33;
}
+#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
return 40;
}
+#endif
QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
return 41;
@@ -6719,10 +6723,12 @@
return 43;
}
+#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
return 50;
}
+#endif
QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
return 51;
@@ -6736,6 +6742,7 @@
return 53;
}
+#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
// MIME
bool bIsNot7Bit;
QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
@@ -6767,10 +6774,13 @@
return 65;
}
+
QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
return 70;
}
+#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
+
QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
return 71;