Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 1 | /*============================================================================== |
| 2 | |
| 3 | Copyright (c) 2018, Laurence Lundblade. |
| 4 | All rights reserved. |
| 5 | |
| 6 | Redistribution and use in source and binary forms, with or without |
| 7 | modification, are permitted provided that the following conditions are |
| 8 | met: |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 9 | * 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. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 18 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 19 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 20 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 22 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 23 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 25 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 26 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 27 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
Laurence Lundblade | 0dbc917 | 2018-11-01 14:17:21 +0700 | [diff] [blame] | 28 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 29 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | ==============================================================================*/ |
| 31 | // Created by Laurence Lundblade on 10/26/18. |
| 32 | |
| 33 | #include <stdio.h> |
Michael Eckel | 7e8effa | 2020-03-09 18:19:17 +0100 | [diff] [blame] | 34 | #include "qcbor/qcbor.h" |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 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. |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 43 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 44 | */ |
| 45 | |
| 46 | int main(int argc, const char * argv[]) |
| 47 | { |
| 48 | (void)argc; // Suppress unused warning |
| 49 | (void)argv; // Suppress unused warning |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 50 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 51 | uint8_t pBuf[300]; |
| 52 | // Very simple CBOR, a map with one boolean that is true in it |
| 53 | QCBOREncodeContext EC; |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 54 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 55 | QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf)); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 56 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 57 | QCBOREncode_OpenMap(&EC); |
| 58 | QCBOREncode_AddBoolToMapN(&EC, 66, true); |
| 59 | QCBOREncode_CloseMap(&EC); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 60 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 61 | UsefulBufC Encoded; |
Laurence Lundblade | 07e9ae4 | 2018-11-02 22:28:53 +0700 | [diff] [blame] | 62 | if(QCBOREncode_Finish(&EC, &Encoded)) { |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 63 | return -1; |
| 64 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 65 | |
| 66 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 67 | // Decode it and see that is right |
| 68 | QCBORDecodeContext DC; |
| 69 | QCBORItem Item; |
| 70 | QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 71 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 72 | QCBORDecode_GetNext(&DC, &Item); |
| 73 | if(Item.uDataType != QCBOR_TYPE_MAP) { |
| 74 | return -2; |
| 75 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 76 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 77 | QCBORDecode_GetNext(&DC, &Item); |
| 78 | if(Item.uDataType != QCBOR_TYPE_TRUE) { |
| 79 | return -3; |
| 80 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 81 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 82 | if(QCBORDecode_Finish(&DC)) { |
| 83 | return -4; |
| 84 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 85 | |
| 86 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 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); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 97 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 98 | UsefulBufC Encoded2; |
Laurence Lundblade | 07e9ae4 | 2018-11-02 22:28:53 +0700 | [diff] [blame] | 99 | if(QCBOREncode_Finish(&EC, &Encoded2)) { |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 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 | ] |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 114 | |
| 115 | |
| 116 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 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 | */ |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 128 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 129 | // Decode it and see if it is OK |
| 130 | QCBORDecode_Init(&DC, Encoded2, QCBOR_DECODE_MODE_NORMAL); |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 131 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 132 | // 0 1:3 |
| 133 | QCBORDecode_GetNext(&DC, &Item); |
| 134 | if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 3) { |
| 135 | return -6; |
| 136 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 137 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 138 | // 1 1:2 |
| 139 | QCBORDecode_GetNext(&DC, &Item); |
| 140 | if(Item.uDataType != QCBOR_TYPE_INT64 || Item.val.uint64 != 451) { |
| 141 | return -7; |
| 142 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 143 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 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 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 149 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 150 | // 2 1:1 |
| 151 | QCBORDecode_GetNext(&DC, &Item); |
| 152 | if(Item.uDataType != QCBOR_TYPE_TRUE) { |
| 153 | return -9; |
| 154 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 155 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 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 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 161 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 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 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 167 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 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 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 173 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 174 | if(QCBORDecode_Finish(&DC)) { |
| 175 | return -13; |
| 176 | } |
Laurence Lundblade | 3aee3a3 | 2018-12-17 16:17:45 -0800 | [diff] [blame] | 177 | |
Laurence Lundblade | c2b1457 | 2018-11-01 13:07:49 +0700 | [diff] [blame] | 178 | return 0; |
| 179 | } |