cancel bstr seems to be working
diff --git a/inc/qcbor/qcbor_common.h b/inc/qcbor/qcbor_common.h
index 7e52a26..8ccc8b4 100644
--- a/inc/qcbor/qcbor_common.h
+++ b/inc/qcbor/qcbor_common.h
@@ -502,6 +502,10 @@
        indefinite length map or array in the input CBOR. */
    QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED = 44,
 
+   /** Trying to cancel a byte string wrapping after items have been
+       added to it. */
+   QCBOR_ERR_CANNOT_CANCEL = 45,
+
    /* This is stored in uint8_t; never add values > 255 */
 } QCBORError;
 
diff --git a/inc/qcbor/qcbor_encode.h b/inc/qcbor/qcbor_encode.h
index 016226b..82ea6c0 100644
--- a/inc/qcbor/qcbor_encode.h
+++ b/inc/qcbor/qcbor_encode.h
@@ -1774,6 +1774,17 @@
 
 
 /**
+ @brief Cancel a wrapping bstr.
+
+ @param[in] pCtx              The encoding context to close of bstr wrapping in.
+
+ This only works if nothing has been added into the wrapped byte string.
+ If something has been added, this sets the TODO: error.
+ **/
+void QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pCtx);
+
+
+/**
  @brief Add some already-encoded CBOR bytes.
 
  @param[in] pCtx     The encoding context to add the already-encode CBOR to.
diff --git a/src/qcbor_encode.c b/src/qcbor_encode.c
index 59c420b..9c286a9 100644
--- a/src/qcbor_encode.c
+++ b/src/qcbor_encode.c
@@ -114,6 +114,19 @@
    return QCBOR_SUCCESS;
 }
 
+inline static uint8_t Nesting_Decrement(QCBORTrackNesting *pNesting)
+{
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
+   if(!pNesting->pCurrentNesting->uCount) {
+      return 99; // TODO: error
+   }
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
+   pNesting->pCurrentNesting->uCount--;
+
+   return QCBOR_SUCCESS;
+}
+
 inline static uint16_t Nesting_GetCount(QCBORTrackNesting *pNesting)
 {
    /* The nesting count recorded is always the actual number of
@@ -886,7 +899,34 @@
 
 
 /*
- * Public functions for closing arrays and maps. See qcbor/qcbor_encode.h
+ * Public function for canceling a bstr wrap. See qcbor/qcbor_encode.h
+ */
+void QCBOREncode_CancelBstrWrap(QCBOREncodeContext *pMe)
+{
+#ifndef QCBOR_DISABLE_ENCODE_USAGE_GUARDS
+   if(pMe->uError == QCBOR_SUCCESS) {
+      if(!Nesting_IsInNest(&(pMe->nesting))) {
+         pMe->uError = QCBOR_ERR_TOO_MANY_CLOSES;
+         return;
+      } else if(Nesting_GetMajorType(&(pMe->nesting)) != CBOR_MAJOR_TYPE_BYTE_STRING) {
+         pMe->uError = QCBOR_ERR_CLOSE_MISMATCH;
+         return;
+      }
+      const size_t uCurrent = UsefulOutBuf_GetEndPosition(&(pMe->OutBuf));
+      if(pMe->nesting.pCurrentNesting->uStart != uCurrent) {
+         pMe->uError = QCBOR_ERR_CANNOT_CANCEL;
+         return;
+      }
+   }
+#endif /* QCBOR_DISABLE_ENCODE_USAGE_GUARDS */
+
+   Nesting_Decrease(&(pMe->nesting));
+   Nesting_Decrement(&(pMe->nesting));
+}
+
+
+/*
+ * Public function for closing arrays and maps. See qcbor/qcbor_encode.h
  */
 void QCBOREncode_CloseMapOrArrayIndefiniteLength(QCBOREncodeContext *me, uint8_t uMajorType)
 {
@@ -911,7 +951,7 @@
 
 
 /*
- * Public functions to finish and get the encoded result. See qcbor/qcbor_encode.h
+ * Public function to finish and get the encoded result. See qcbor/qcbor_encode.h
  */
 QCBORError QCBOREncode_Finish(QCBOREncodeContext *me, UsefulBufC *pEncodedCBOR)
 {
diff --git a/test/qcbor_encode_tests.c b/test/qcbor_encode_tests.c
index 18116cb..99dd7b7 100644
--- a/test/qcbor_encode_tests.c
+++ b/test/qcbor_encode_tests.c
@@ -1745,6 +1745,7 @@
  */
 static const uint8_t spExpectedTypeAndLen[] = {0x81, 0x58, 0x25};
 
+static const uint8_t spExpectedTypeAndLenXXX[] = {0x82, 0x19, 0x01, 0xC3, 0x18, 0x2A};
 /*
  Very basic bstr wrapping test
  */
@@ -1806,6 +1807,43 @@
       return -7;
    }
 
+
+   // Fourth test, cancelling a byte string
+   QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+
+   QCBOREncode_OpenArray(&EC);
+   QCBOREncode_AddUInt64(&EC, 451);
+
+   QCBOREncode_BstrWrap(&EC);
+   QCBOREncode_CancelBstrWrap(&EC);
+
+
+   QCBOREncode_AddUInt64(&EC, 42);
+   QCBOREncode_CloseArray(&EC);
+   if(QCBOREncode_Finish(&EC, &Encoded)) {
+      return -8;
+   }
+   if(CheckResults(Encoded, spExpectedTypeAndLenXXX)) {
+      return -9;
+   }
+
+   // Fifth test, failed cancelling
+   QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(spBigBuf));
+
+   QCBOREncode_OpenArray(&EC);
+   QCBOREncode_AddUInt64(&EC, 451);
+
+   QCBOREncode_BstrWrap(&EC);
+   QCBOREncode_AddUInt64(&EC, 99);
+   QCBOREncode_CancelBstrWrap(&EC);
+
+   QCBOREncode_AddUInt64(&EC, 42);
+   QCBOREncode_CloseArray(&EC);
+   QCBORError uErr = QCBOREncode_Finish(&EC, &Encoded);
+   if(uErr != QCBOR_ERR_CANNOT_CANCEL) {
+      return -10;
+   }
+
    return 0;
 }