blob: 33975b5723981fc6c39d9dd44c7a2f02126cb447 [file] [log] [blame]
Laurence Lundbladec2b14572018-11-01 13:07:49 +07001/*==============================================================================
2
3Copyright (c) 2018, Laurence Lundblade.
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07009 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * The name "Laurence Lundblade" may not be used to
16 endorse or promote products derived from this software without
17 specific prior written permission.
18
Laurence Lundbladec2b14572018-11-01 13:07:49 +070019THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
20WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
22ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
23BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070025SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Laurence Lundbladec2b14572018-11-01 13:07:49 +070027WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070028OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
Laurence Lundbladec2b14572018-11-01 13:07:49 +070029IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30==============================================================================*/
31// Created by Laurence Lundblade on 10/26/18.
32
33#include <stdio.h>
34#include "qcbor.h"
35
36
37
38/*
39 A small user of CBOR encoding and decoding
40 that is good as an example and for
41 checking code size with all the
42 inlining and dead stripping on.
43
44 */
45
46int main(int argc, const char * argv[])
47{
48 (void)argc; // Suppress unused warning
49 (void)argv; // Suppress unused warning
50
51 uint8_t pBuf[300];
52 // Very simple CBOR, a map with one boolean that is true in it
53 QCBOREncodeContext EC;
54
55 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
56
57 QCBOREncode_OpenMap(&EC);
58 QCBOREncode_AddBoolToMapN(&EC, 66, true);
59 QCBOREncode_CloseMap(&EC);
60
61 UsefulBufC Encoded;
62 if(QCBOREncode_Finish2(&EC, &Encoded)) {
63 return -1;
64 }
65
66
67 // Decode it and see that is right
68 QCBORDecodeContext DC;
69 QCBORItem Item;
70 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
71
72 QCBORDecode_GetNext(&DC, &Item);
73 if(Item.uDataType != QCBOR_TYPE_MAP) {
74 return -2;
75 }
76
77 QCBORDecode_GetNext(&DC, &Item);
78 if(Item.uDataType != QCBOR_TYPE_TRUE) {
79 return -3;
80 }
81
82 if(QCBORDecode_Finish(&DC)) {
83 return -4;
84 }
85
86
87 // Make another encoded message with the CBOR from the previous put into this one
88 UsefulBuf_MAKE_STACK_UB(MemoryForEncoded2, 20);
89 QCBOREncode_Init(&EC, MemoryForEncoded2);
90 QCBOREncode_OpenArray(&EC);
91 QCBOREncode_AddUInt64(&EC, 451);
92 QCBOREncode_AddEncoded(&EC, Encoded);
93 QCBOREncode_OpenMap(&EC);
94 QCBOREncode_AddEncodedToMapN(&EC, -70000, Encoded);
95 QCBOREncode_CloseMap(&EC);
96 QCBOREncode_CloseArray(&EC);
97
98 UsefulBufC Encoded2;
99 if(QCBOREncode_Finish2(&EC, &Encoded2)) {
100 return -5;
101 }
102 /*
103 [ // 0 1:3
104 451, // 1 1:2
105 { // 1 1:2 2:1
106 66: true // 2 1:1
107 },
108 { // 1 1:1 2:1
109 -70000: { // 2 1:1 2:1 3:1
110 66: true // 3 XXXXXX
111 }
112 }
113 ]
114
115
116
117 83 # array(3)
118 19 01C3 # unsigned(451)
119 A1 # map(1)
120 18 42 # unsigned(66)
121 F5 # primitive(21)
122 A1 # map(1)
123 3A 0001116F # negative(69999)
124 A1 # map(1)
125 18 42 # unsigned(66)
126 F5 # primitive(21)
127 */
128
129 // Decode it and see if it is OK
130 QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL);
131
132 // 0 1:3
133 QCBORDecode_GetNext(&DC, &Item);
134 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) {
135 return -6;
136 }
137
138 // 1 1:2
139 QCBORDecode_GetNext(&DC, &Item);
140 if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) {
141 return -7;
142 }
143
144 // 1 1:2 2:1
145 QCBORDecode_GetNext(&DC, &Item);
146 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
147 return -8;
148 }
149
150 // 2 1:1
151 QCBORDecode_GetNext(&DC, &Item);
152 if(Item.uDataType != QCBOR_TYPE_TRUE) {
153 return -9;
154 }
155
156 // 1 1:1 2:1
157 QCBORDecode_GetNext(&DC, &Item);
158 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1) {
159 return -10;
160 }
161
162 // 2 1:1 2:1 3:1
163 QCBORDecode_GetNext(&DC, &Item);
164 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 1 || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != -70000) {
165 return -11;
166 }
167
168 // 3 XXXXXX
169 QCBORDecode_GetNext(&DC, &Item);
170 if(Item.uDataType != QCBOR_TYPE_TRUE || Item.uLabelType != QCBOR_TYPE_INT64 || Item.label.int64 != 66) {
171 return -12;
172 }
173
174 if(QCBORDecode_Finish(&DC)) {
175 return -13;
176 }
177
178 return 0;
179}