1 module deimos.cbor.data; 2 3 /* 4 * Copyright (c) 2014-2019 Pavel Kalvoda <me@pavelkalvoda.com> 5 * 6 * libcbor is free software; you can redistribute it and/or modify 7 * it under the terms of the MIT license. See LICENSE for details. 8 */ 9 10 alias cbor_data = const ubyte*; 11 alias cbor_mutable_data = ubyte*; 12 13 /** Specifies the Major type of ::cbor_item_t */ 14 alias cbor_type = int; 15 enum : cbor_type 16 { 17 CBOR_TYPE_UINT = 0, /** 0 - positive integers */ 18 19 CBOR_TYPE_NEGINT = 1, /** 1 - negative integers*/ 20 21 CBOR_TYPE_BYTESTRING = 2, /** 2 - byte strings */ 22 23 CBOR_TYPE_STRING = 3, /** 3 - strings */ 24 25 CBOR_TYPE_ARRAY = 4, /** 4 - arrays */ 26 27 CBOR_TYPE_MAP = 5, /** 5 - maps */ 28 29 CBOR_TYPE_TAG = 6, /** 6 - tags */ 30 31 CBOR_TYPE_FLOAT_CTRL = 7 /** 7 - decimals and special values (true, false, nil, 32 ...) */ 33 } 34 35 /** Possible decoding errors */ 36 alias cbor_error_code = int; 37 enum : cbor_error_code 38 { 39 CBOR_ERR_NONE = 0, 40 CBOR_ERR_NOTENOUGHDATA = 1, 41 CBOR_ERR_NODATA = 2, 42 CBOR_ERR_MALFORMATED = 3, 43 CBOR_ERR_MEMERROR = 4, /** Memory error - item allocation failed. Is it too big for 44 your allocator? */ 45 46 CBOR_ERR_SYNTAXERROR = 5 /** Stack parsing algorithm failed */ 47 } 48 49 /** Possible widths of #CBOR_TYPE_UINT items */ 50 alias cbor_int_width = int; 51 enum : cbor_int_width 52 { 53 CBOR_INT_8 = 0, 54 CBOR_INT_16 = 1, 55 CBOR_INT_32 = 2, 56 CBOR_INT_64 = 3 57 } 58 59 /** Possible widths of #CBOR_TYPE_FLOAT_CTRL items */ 60 alias cbor_float_width = int; 61 enum : cbor_float_width 62 { 63 CBOR_FLOAT_0 = 0, /** Internal use - ctrl and special values */ 64 65 CBOR_FLOAT_16 = 1, /** Half float */ 66 67 CBOR_FLOAT_32 = 2, /** Single float */ 68 69 CBOR_FLOAT_64 = 3 /** Double */ 70 } 71 72 /** Metadata for dynamically sized types */ 73 alias _cbor_dst_metadata = int; 74 enum : _cbor_dst_metadata 75 { 76 _CBOR_METADATA_DEFINITE = 0, 77 _CBOR_METADATA_INDEFINITE = 1 78 } 79 80 /** Semantic mapping for CTRL simple values */ 81 alias _cbor_ctrl = int; 82 enum : _cbor_ctrl 83 { 84 CBOR_CTRL_NONE = 0, 85 CBOR_CTRL_FALSE = 20, 86 CBOR_CTRL_TRUE = 21, 87 CBOR_CTRL_NULL = 22, 88 CBOR_CTRL_UNDEF = 23 89 } 90 91 /** Integers specific metadata */ 92 struct _cbor_int_metadata 93 { 94 cbor_int_width width; 95 } 96 97 /** Bytestrings specific metadata */ 98 struct _cbor_bytestring_metadata 99 { 100 size_t length; 101 _cbor_dst_metadata type; 102 } 103 104 /** Strings specific metadata */ 105 struct _cbor_string_metadata 106 { 107 size_t length; 108 size_t codepoint_count; /* Sum of chunks' codepoint_counts for indefinite 109 strings */ 110 _cbor_dst_metadata type; 111 } 112 113 /** Arrays specific metadata */ 114 struct _cbor_array_metadata 115 { 116 size_t allocated; 117 size_t end_ptr; 118 _cbor_dst_metadata type; 119 } 120 121 /** Maps specific metadata */ 122 struct _cbor_map_metadata 123 { 124 size_t allocated; 125 size_t end_ptr; 126 _cbor_dst_metadata type; 127 } 128 129 /** Arrays specific metadata 130 * 131 * The pointer is included - cbor_item_metadata is 132 * 2 * sizeof(size_t) + sizeof(_cbor_string_type_metadata), 133 * lets use the space 134 */ 135 struct _cbor_tag_metadata 136 { 137 cbor_item_t* tagged_item; 138 ulong value; 139 } 140 141 /** Floats specific metadata - includes CTRL values */ 142 struct _cbor_float_ctrl_metadata 143 { 144 cbor_float_width width; 145 ubyte ctrl; 146 } 147 148 /** Raw memory casts helper */ 149 union _cbor_float_helper 150 { 151 float as_float; 152 uint as_uint; 153 } 154 155 /** Raw memory casts helper */ 156 union _cbor_double_helper 157 { 158 double as_double; 159 ulong as_uint; 160 } 161 162 /** Union of metadata across all possible types - discriminated in #cbor_item_t 163 */ 164 union cbor_item_metadata 165 { 166 _cbor_int_metadata int_metadata; 167 _cbor_bytestring_metadata bytestring_metadata; 168 _cbor_string_metadata string_metadata; 169 _cbor_array_metadata array_metadata; 170 _cbor_map_metadata map_metadata; 171 _cbor_tag_metadata tag_metadata; 172 _cbor_float_ctrl_metadata float_ctrl_metadata; 173 } 174 175 /** The item handle */ 176 struct cbor_item_t 177 { 178 /** Discriminated by type */ 179 cbor_item_metadata metadata; 180 /** Reference count - initialize to 0 */ 181 size_t refcount; 182 /** Major type discriminator */ 183 cbor_type type; 184 /** Raw data block - interpretation depends on metadata */ 185 ubyte* data; 186 } 187 188 /** Defines cbor_item_t#data structure for indefinite strings and bytestrings 189 * 190 * Used to cast the raw representation for a sane manipulation 191 */ 192 struct cbor_indefinite_string_data 193 { 194 size_t chunk_count; 195 size_t chunk_capacity; 196 cbor_item_t** chunks; 197 } 198 199 /** High-level decoding error */ 200 struct cbor_error 201 { 202 /** Aproximate position */ 203 size_t position; 204 /** Description */ 205 cbor_error_code code; 206 } 207 208 /** Simple pair of items for use in maps */ 209 struct cbor_pair 210 { 211 cbor_item_t* key; 212 cbor_item_t* value; 213 } 214 215 /** High-level decoding result */ 216 struct cbor_load_result 217 { 218 /** Error indicator */ 219 cbor_error error; 220 /** Number of bytes read*/ 221 size_t read; 222 } 223 224 /** Streaming decoder result - status */ 225 alias cbor_decoder_status = int; 226 enum : cbor_decoder_status 227 { 228 CBOR_DECODER_FINISHED = 0, /** OK, finished */ 229 230 CBOR_DECODER_NEDATA = 1, /** Not enough data - mismatch with MTB */ 231 232 CBOR_DECODER_EBUFFER = 2, /** Buffer manipulation problem */ 233 234 CBOR_DECODER_ERROR = 3 /** Malformed or reserved MTB/value */ 235 } 236 237 /** Streaming decoder result */ 238 struct cbor_decoder_result 239 { 240 /** Bytes read */ 241 size_t read; 242 /** The result */ 243 cbor_decoder_status status; 244 /** When status == CBOR_DECODER_NEDATA, 245 * the minimum number of bytes required to continue parsing */ 246 size_t required; 247 }