Add QCBOREncode_Tell and QCBOREncode_SubString (#251)
* QCBOREncode_Tell and QCBOREncode_SubString
* Test and documentation improvements
* test, doc, back out error checks that didn't work
* more tests; tidiness
* Small documentation improvement
* Add RetreiveUndecodedInput
* Proper factoring for UsefulOutBuf Storage
* nits
---------
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 2a99110..218e12c 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -166,7 +166,7 @@
{
return pNesting->pCurrentNesting == &pNesting->pArrays[0] ? false : true;
}
-#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+#endif /* ! QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
@@ -1085,3 +1085,28 @@
return nReturn;
}
+
+
+/*
+ * Public function to get substring of encoded-so-far. See qcbor/qcbor_encode.h
+ */
+UsefulBufC
+QCBOREncode_SubString(QCBOREncodeContext *pMe, const size_t uStart)
+{
+ if(pMe->uError) {
+ return NULLUsefulBufC;
+ }
+
+ /* An attempt was made to detect usage errors by comparing uStart
+ * to offsets of open arrays and maps in pMe->nesting, but it is
+ * not possible because there's not enough information in just
+ * the offset. It's not possible to known if Tell() was called before
+ * or after an Open(). To detect this error, the nesting level
+ * would also need to be known. This is not frequently used, so
+ * it is not worth adding this complexity.
+ */
+
+ const size_t uEnd = QCBOREncode_Tell(pMe);
+
+ return UsefulOutBuf_SubString(&(pMe->OutBuf), uStart, uEnd - uStart);
+}