zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 1 | /* CWPack - cwpack.h */ |
| 2 | /* |
| 3 | The MIT License (MIT) |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 4 | |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 5 | Copyright (c) 2017 Claes Wihlborg |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 6 | |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 8 | software and associated documentation files (the "Software"), to deal in the Software |
| 9 | without restriction, including without limitation the rights to use, copy, modify, |
| 10 | merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 11 | persons to whom the Software is furnished to do so, subject to the following conditions: |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 12 | |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 13 | The above copyright notice and this permission notice shall be included in all copies or |
| 14 | substantial portions of the Software. |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 15 | |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
| 17 | BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 19 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #ifndef CWPack_H__ |
| 24 | #define CWPack_H__ |
| 25 | |
| 26 | |
| 27 | //#include <stdint.h> |
| 28 | #include <stdbool.h> |
| 29 | |
| 30 | //zzq add |
| 31 | #include "stdint.h" |
| 32 | |
| 33 | |
| 34 | |
| 35 | /******************************* Return Codes *****************************/ |
| 36 | |
| 37 | #define CWP_RC_OK 0 |
| 38 | #define CWP_RC_END_OF_INPUT -1 |
| 39 | #define CWP_RC_BUFFER_OVERFLOW -2 |
| 40 | #define CWP_RC_BUFFER_UNDERFLOW -3 |
| 41 | #define CWP_RC_MALFORMED_INPUT -4 |
| 42 | #define CWP_RC_WRONG_BYTE_ORDER -5 |
| 43 | #define CWP_RC_ERROR_IN_HANDLER -6 |
| 44 | #define CWP_RC_ILLEGAL_CALL -7 |
| 45 | #define CWP_RC_MALLOC_ERROR -8 |
| 46 | #define CWP_RC_STOPPED -9 |
| 47 | |
| 48 | |
| 49 | |
| 50 | /******************************* P A C K **********************************/ |
| 51 | |
| 52 | |
| 53 | struct cw_pack_context; |
| 54 | |
| 55 | typedef int (*pack_overflow_handler)(struct cw_pack_context*, unsigned long); |
| 56 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 57 | typedef struct cw_pack_context |
| 58 | { |
| 59 | uint8_t* start; |
| 60 | uint8_t* current; |
| 61 | uint8_t* end; |
| 62 | bool be_compatible; |
| 63 | int return_code; |
| 64 | int err_no; /* handlers can save error here */ |
| 65 | pack_overflow_handler handle_pack_overflow; |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 66 | } cw_pack_context; |
| 67 | |
| 68 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 69 | int cw_pack_context_init(cw_pack_context* pack_context, void* data, unsigned long length, |
| 70 | pack_overflow_handler hpo); |
| 71 | void cw_pack_set_compatibility(cw_pack_context* pack_context, bool be_compatible); |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 72 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 73 | void cw_pack_nil(cw_pack_context* pack_context); |
| 74 | void cw_pack_true(cw_pack_context* pack_context); |
| 75 | void cw_pack_false(cw_pack_context* pack_context); |
| 76 | void cw_pack_boolean(cw_pack_context* pack_context, bool b); |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 77 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 78 | void cw_pack_signed(cw_pack_context* pack_context, int64_t i); |
| 79 | void cw_pack_unsigned(cw_pack_context* pack_context, uint64_t i); |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 80 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 81 | void cw_pack_float(cw_pack_context* pack_context, float f); |
| 82 | void cw_pack_double(cw_pack_context* pack_context, double d); |
| 83 | void cw_pack_real(cw_pack_context* pack_context, |
| 84 | double d); /* Pack as float if precision isn't destroyed */ |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 85 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 86 | void cw_pack_array_size(cw_pack_context* pack_context, uint32_t n); |
| 87 | void cw_pack_map_size(cw_pack_context* pack_context, uint32_t n); |
| 88 | void cw_pack_str(cw_pack_context* pack_context, const char* v, uint32_t l); |
| 89 | void cw_pack_bin(cw_pack_context* pack_context, const void* v, uint32_t l); |
| 90 | void cw_pack_ext(cw_pack_context* pack_context, int8_t type, const void* v, uint32_t l); |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 91 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 92 | void cw_pack_insert(cw_pack_context* pack_context, const void* v, uint32_t l); |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 93 | |
| 94 | |
| 95 | /***************************** U N P A C K ********************************/ |
| 96 | |
| 97 | |
| 98 | typedef enum |
| 99 | { |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 100 | CWP_ITEM_MIN_RESERVED_EXT = -128, |
| 101 | CWP_ITEM_MAX_RESERVED_EXT = -1, |
| 102 | CWP_ITEM_MIN_USER_EXT = 0, |
| 103 | CWP_ITEM_MAX_USER_EXT = 127, |
| 104 | CWP_ITEM_NIL = 300, |
| 105 | CWP_ITEM_BOOLEAN = 301, |
| 106 | CWP_ITEM_POSITIVE_INTEGER = 302, |
| 107 | CWP_ITEM_NEGATIVE_INTEGER = 303, |
| 108 | CWP_ITEM_FLOAT = 304, |
| 109 | CWP_ITEM_DOUBLE = 305, |
| 110 | CWP_ITEM_STR = 306, |
| 111 | CWP_ITEM_BIN = 307, |
| 112 | CWP_ITEM_ARRAY = 308, |
| 113 | CWP_ITEM_MAP = 309, |
| 114 | CWP_ITEM_EXT = 310, |
| 115 | CWP_NOT_AN_ITEM = 999, |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 116 | } cwpack_item_types; |
| 117 | |
| 118 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 119 | typedef struct |
| 120 | { |
| 121 | const void* start; |
| 122 | uint32_t length; |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 123 | } cwpack_blob; |
| 124 | |
| 125 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 126 | typedef struct |
| 127 | { |
| 128 | uint32_t size; |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 129 | } cwpack_container; |
| 130 | |
| 131 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 132 | typedef struct |
| 133 | { |
| 134 | cwpack_item_types type; |
| 135 | union |
| 136 | { |
| 137 | bool boolean; |
| 138 | uint64_t u64; |
| 139 | int64_t i64; |
| 140 | float real; |
| 141 | double long_real; |
| 142 | cwpack_container array; |
| 143 | cwpack_container map; |
| 144 | cwpack_blob str; |
| 145 | cwpack_blob bin; |
| 146 | cwpack_blob ext; |
| 147 | } as; |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 148 | } cwpack_item; |
| 149 | |
| 150 | struct cw_unpack_context; |
| 151 | |
| 152 | typedef int (*unpack_underflow_handler)(struct cw_unpack_context*, unsigned long); |
| 153 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 154 | typedef struct cw_unpack_context |
| 155 | { |
| 156 | cwpack_item item; |
| 157 | uint8_t* start; |
| 158 | uint8_t* current; |
| 159 | uint8_t* end; /* logical end of buffer */ |
| 160 | int return_code; |
| 161 | int err_no; /* handlers can save error here */ |
| 162 | unpack_underflow_handler handle_unpack_underflow; |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 163 | } cw_unpack_context; |
| 164 | |
| 165 | |
| 166 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 167 | int cw_unpack_context_init(cw_unpack_context* unpack_context, void* data, |
| 168 | unsigned long length, unpack_underflow_handler huu); |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 169 | |
guangcheng.qin | f87ed97 | 2019-08-30 10:50:01 +0800 | [diff] [blame^] | 170 | void cw_unpack_next(cw_unpack_context* unpack_context); |
| 171 | void cw_skip_items(cw_unpack_context* unpack_context, long item_count); |
zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame] | 172 | |
| 173 | |
| 174 | #endif /* CWPack_H__ */ |