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