blob: 7a89f6f3d3b9317d947e5c9161b0d2f1584bb8e6 [file] [log] [blame]
Jim Schaad49301d72016-01-03 21:56:21 -08001#include <stdlib.h>
Jim Schaad3683e6a2016-02-01 12:39:29 -08002#ifdef _MSC_VER
3#endif
Jim Schaad49301d72016-01-03 21:56:21 -08004#include <stdio.h>
5#include <memory.h>
6#include <assert.h>
7
Jim Schaad6f269f12016-01-03 22:01:27 -08008#include <cn-cbor/cn-cbor.h>
Jim Schaad49301d72016-01-03 21:56:21 -08009
Jim Schaadfecd78d2016-01-03 22:07:22 -080010#ifdef USE_CBOR_CONTEXT
Jim Schaad49301d72016-01-03 21:56:21 -080011#include "context.h"
12
13typedef unsigned char byte;
14
15typedef struct {
16 cn_cbor_context context;
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020017 byte *pFirst;
Jim Schaad0dcce252016-01-15 12:00:18 -080018 unsigned int iFailLeft;
Jim Schaad49301d72016-01-03 21:56:21 -080019} MyContext;
20
21typedef struct _MyItem {
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020022 struct _MyItem *pNext;
23 size_t size;
24 byte pad[4];
25 byte data[4];
Jim Schaad49301d72016-01-03 21:56:21 -080026} MyItem;
27
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020028bool CheckMemory(MyContext *pContext)
Jim Schaad49301d72016-01-03 21:56:21 -080029{
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020030 MyItem *p = NULL;
Jim Schaad49301d72016-01-03 21:56:21 -080031 // Walk memory and check every block
32
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020033 for (p = (MyItem *)pContext->pFirst; p != NULL; p = p->pNext) {
34 if (p->pad[0] == (byte)0xab) {
Jim Schaad49301d72016-01-03 21:56:21 -080035 // Block has been freed
Carlos Gomes Martinho2a145192020-04-22 16:25:59 +020036 for (unsigned i = 0; i < p->size + 8; i++) {
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020037 if (p->pad[i] != (byte)0xab) {
Jim Schaad49301d72016-01-03 21:56:21 -080038 fprintf(stderr, "Freed block is modified");
39 assert(false);
40 }
41 }
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020042 } else if (p->pad[0] == (byte)0xef) {
Carlos Gomes Martinho2a145192020-04-22 16:25:59 +020043 for (unsigned i = 0; i < 4; i++) {
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020044 if ((p->pad[i] != (byte)0xef) ||
45 (p->pad[i + 4 + p->size] != (byte)0xef)) {
Jim Schaad49301d72016-01-03 21:56:21 -080046 fprintf(stderr, "Curent block was overrun");
47 assert(false);
48 }
49 }
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020050 } else {
Jim Schaad49301d72016-01-03 21:56:21 -080051 fprintf(stderr, "Incorrect pad value");
52 assert(false);
53 }
54 }
55
56 return true;
57}
58
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020059void *MyCalloc(size_t count, size_t size, void *context)
Jim Schaad49301d72016-01-03 21:56:21 -080060{
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020061 MyItem *pb = NULL;
62 MyContext *myContext = (MyContext *)context;
Jim Schaad49301d72016-01-03 21:56:21 -080063
64 CheckMemory(myContext);
65
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020066 if (myContext->iFailLeft == 0)
67 return NULL;
Jim Schaade565d0b2016-01-20 17:26:23 -080068 myContext->iFailLeft--;
69
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020070 pb = (MyItem *)malloc(sizeof(MyItem) + count * size);
Jim Schaad0dcce252016-01-15 12:00:18 -080071
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020072 memset(pb, 0xef, sizeof(MyItem) + count * size);
73 memset(&pb->data, 0, count * size);
Jim Schaad49301d72016-01-03 21:56:21 -080074
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020075 pb->pNext = (struct _MyItem *)myContext->pFirst;
Jim Schaade565d0b2016-01-20 17:26:23 -080076 myContext->pFirst = (byte *)pb;
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020077 pb->size = count * size;
Jim Schaad49301d72016-01-03 21:56:21 -080078
Jim Schaade565d0b2016-01-20 17:26:23 -080079 return &pb->data;
Jim Schaad49301d72016-01-03 21:56:21 -080080}
81
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020082void MyFree(void *ptr, void *context)
Jim Schaad49301d72016-01-03 21:56:21 -080083{
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020084 MyItem *pb = (MyItem *)((byte *)ptr - sizeof(MyItem) + 4);
85 MyContext *myContext = (MyContext *)context;
Jim Schaad49301d72016-01-03 21:56:21 -080086
87 CheckMemory(myContext);
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020088 if (ptr == NULL)
89 return;
Jim Schaad49301d72016-01-03 21:56:21 -080090
91 memset(&pb->pad, 0xab, pb->size + 8);
92}
93
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020094cn_cbor_context *CreateContext(unsigned int iFailPoint)
Jim Schaad49301d72016-01-03 21:56:21 -080095{
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +020096 MyContext *p = malloc(sizeof(MyContext));
Jim Schaad49301d72016-01-03 21:56:21 -080097
98 p->context.calloc_func = MyCalloc;
99 p->context.free_func = MyFree;
100 p->context.context = p;
101 p->pFirst = NULL;
Jim Schaad0dcce252016-01-15 12:00:18 -0800102 p->iFailLeft = iFailPoint;
Jim Schaad49301d72016-01-03 21:56:21 -0800103
104 return &p->context;
105}
106
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +0200107void FreeContext(cn_cbor_context *pContext)
Jim Schaad49301d72016-01-03 21:56:21 -0800108{
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +0200109 MyContext *myContext = (MyContext *)pContext;
110 MyItem *pItem;
111 MyItem *pItem2;
Jim Schaad49301d72016-01-03 21:56:21 -0800112
113 CheckMemory(myContext);
114
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +0200115 for (pItem = (MyItem *)myContext->pFirst; pItem != NULL; pItem = pItem2) {
Jim Schaad49301d72016-01-03 21:56:21 -0800116 pItem2 = pItem->pNext;
117 free(pItem);
118 }
119
120 free(myContext);
121
122 return;
123}
Jim Schaadbff945a2016-01-03 22:04:53 -0800124
Carlos Gomes Martinho9223f012020-04-24 09:42:16 +0200125#endif // USE_CBOR_CONTEXT