partial indefinite lenght stuff
diff --git a/inc/qcbor.h b/inc/qcbor.h
index 48275af..0aff218 100644
--- a/inc/qcbor.h
+++ b/inc/qcbor.h
@@ -693,7 +693,7 @@
*/
typedef struct {
void *pAllocaterContext;
- void * (*AllocatorFunction)(void *pMem, size_t uNewSize);
+ void * (*AllocatorFunction)(void *pMem, void *pOld, size_t uNewSize);
bool bAlwaysAlloc;
} QCBORStringAllocator;
@@ -1460,6 +1460,8 @@
*/
void QCBOR_Decode_SetUpAllocator(QCBORDecodeContext *pCtx, const QCBORStringAllocator *pAllocator);
+const QCBORStringAllocator *QCBORDecode_GetAllocator(QCBORDecodeContext *pCtx);
+
/**
diff --git a/qcbor_decode_malloc.c b/qcbor_decode_malloc.c
new file mode 100644
index 0000000..4332254
--- /dev/null
+++ b/qcbor_decode_malloc.c
@@ -0,0 +1,85 @@
+//
+// qcbor_decode_malloc.c
+// QCBOR
+//
+// Created by Laurence Lundblade on 10/1/18.
+// Copyright © 2018 Laurence Lundblade. All rights reserved.
+//
+
+#include "qcbor.h"
+#include <stdlib.h>
+
+
+static void *MemMalloc(void *ctx, void *old, size_t y)
+{
+ return realloc(old, y);
+}
+
+void QCBORDecodeMalloc_Init(QCBORDecodeContext *me, UsefulBufC EncodedCBOR, int8_t nDecodeMode)
+{
+ QCBORDecode_Init(me, EncodedCBOR, nDecodeMode);
+
+ QCBORStringAllocator *pAllocator = malloc(sizeof(QCBORStringAllocator));
+
+ pAllocator->AllocatorFunction = MemMalloc;
+
+ QCBOR_Decode_SetUpAllocator(me, pAllocator);
+}
+
+
+int QCBORDecodeMalloc_Finish(QCBORDecodeContext *me)
+{
+ QCBORStringAllocator *pAllocator = QCBORDecode_GetAllocator(me);
+
+ free(pAllocator);
+ return QCBORDecode_Finish(me);
+}
+
+
+
+
+struct MemPoolType {
+ size_t uSize;
+ size_t uPos;
+};
+
+static void *MemPoolAlloc(void *ctx, void *old, size_t y)
+{
+ struct MemPoolType *p = (struct MemPoolType *)ctx;
+ if(old) {
+ // trying a realloc
+ return NULL;
+ } else {
+ // New chunk
+ if(p->uPos + y > p->uSize) {
+ return NULL; // won't fit
+ }
+ void *pReturn = ctx + sizeof( struct MemPoolType ) + p->uPos;
+ p->uPos += y;
+ return pReturn;
+ }
+
+}
+
+
+int QCBORDecodeMemPool_Init(QCBORDecodeContext *me, UsefulBufC EncodedCBOR, int8_t nDecodeMode, UsefulBuf MemPool)
+{
+ QCBORDecode_Init(me, EncodedCBOR, nDecodeMode);
+
+ if(MemPool.len < 20) {
+ return -1;
+ }
+
+ QCBORStringAllocator *pAllocator = MemPool.ptr;
+ pAllocator->AllocatorFunction = MemPoolAlloc;
+
+ struct MemPoolType *p = MemPool.ptr + sizeof(QCBORStringAllocator);
+ p->uSize = MemPool.len - sizeof(QCBORStringAllocator);
+ p->uPos = 0;
+
+ pAllocator->pAllocaterContext = p;\
+
+ QCBOR_Decode_SetUpAllocator(me, pAllocator);
+
+ return 0;
+}
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index 7544e84..a02a06e 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -191,6 +191,12 @@
pCtx->pStringAllocator = (void *)pAllocator;
}
+const QCBORStringAllocator *QCBORDecode_GetAllocator(QCBORDecodeContext *pCtx)
+{
+ return pCtx->pStringAllocator;
+}
+
+
/*
This decodes the fundamental part of a CBOR data item, the type and number
@@ -668,9 +674,8 @@
UsefulBuf XX(QCBORStringAllocator *pAlloc, UsefulBufC yy, size_t add)
{
- // TODO: what about allocator context?
// TODO: pointer arithmatic
- uint8_t *x = (*pAlloc->AllocatorFunction) (yy.ptr, yy.len + add );
+ uint8_t *x = (*pAlloc->AllocatorFunction) (pAlloc->pAllocaterContext, yy.ptr, yy.len + add );
return (UsefulBuf) {x, yy.len + add};
}
@@ -800,6 +805,21 @@
+/*
+
+ Use the 64-bit map. 48 8-bit tags built in, 1 16 bit tag, 15 64-bit tags can be assigned as of interest
+
+ There is a tag map.
+
+ TODO: how does tinyCBOR do it?
+
+
+
+
+
+ */
+
+
/*
Decoder errors handled in this file