zongqiang.zhang | 0c6a088 | 2019-08-07 14:48:21 +0800 | [diff] [blame^] | 1 | /* CWPack - cwpack.h */ |
| 2 | /* |
| 3 | The MIT License (MIT) |
| 4 | |
| 5 | Copyright (c) 2017 Claes Wihlborg |
| 6 | |
| 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: |
| 12 | |
| 13 | The above copyright notice and this permission notice shall be included in all copies or |
| 14 | substantial portions of the Software. |
| 15 | |
| 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 | |
| 57 | typedef struct cw_pack_context { |
| 58 | uint8_t* start; |
| 59 | uint8_t* current; |
| 60 | uint8_t* end; |
| 61 | bool be_compatible; |
| 62 | int return_code; |
| 63 | int err_no; /* handlers can save error here */ |
| 64 | pack_overflow_handler handle_pack_overflow; |
| 65 | } cw_pack_context; |
| 66 | |
| 67 | |
| 68 | int cw_pack_context_init (cw_pack_context* pack_context, void* data, unsigned long length, pack_overflow_handler hpo); |
| 69 | void cw_pack_set_compatibility (cw_pack_context* pack_context, bool be_compatible); |
| 70 | |
| 71 | void cw_pack_nil (cw_pack_context* pack_context); |
| 72 | void cw_pack_true (cw_pack_context* pack_context); |
| 73 | void cw_pack_false (cw_pack_context* pack_context); |
| 74 | void cw_pack_boolean (cw_pack_context* pack_context, bool b); |
| 75 | |
| 76 | void cw_pack_signed (cw_pack_context* pack_context, int64_t i); |
| 77 | void cw_pack_unsigned (cw_pack_context* pack_context, uint64_t i); |
| 78 | |
| 79 | void cw_pack_float (cw_pack_context* pack_context, float f); |
| 80 | void cw_pack_double (cw_pack_context* pack_context, double d); |
| 81 | void cw_pack_real (cw_pack_context* pack_context, double d); /* Pack as float if precision isn't destroyed */ |
| 82 | |
| 83 | void cw_pack_array_size (cw_pack_context* pack_context, uint32_t n); |
| 84 | void cw_pack_map_size (cw_pack_context* pack_context, uint32_t n); |
| 85 | void cw_pack_str (cw_pack_context* pack_context, const char* v, uint32_t l); |
| 86 | void cw_pack_bin (cw_pack_context* pack_context, const void* v, uint32_t l); |
| 87 | void cw_pack_ext (cw_pack_context* pack_context, int8_t type, const void* v, uint32_t l); |
| 88 | |
| 89 | void cw_pack_insert (cw_pack_context* pack_context, const void* v, uint32_t l); |
| 90 | |
| 91 | |
| 92 | /***************************** U N P A C K ********************************/ |
| 93 | |
| 94 | |
| 95 | typedef enum |
| 96 | { |
| 97 | CWP_ITEM_MIN_RESERVED_EXT = -128, |
| 98 | CWP_ITEM_MAX_RESERVED_EXT = -1, |
| 99 | CWP_ITEM_MIN_USER_EXT = 0, |
| 100 | CWP_ITEM_MAX_USER_EXT = 127, |
| 101 | CWP_ITEM_NIL = 300, |
| 102 | CWP_ITEM_BOOLEAN = 301, |
| 103 | CWP_ITEM_POSITIVE_INTEGER = 302, |
| 104 | CWP_ITEM_NEGATIVE_INTEGER = 303, |
| 105 | CWP_ITEM_FLOAT = 304, |
| 106 | CWP_ITEM_DOUBLE = 305, |
| 107 | CWP_ITEM_STR = 306, |
| 108 | CWP_ITEM_BIN = 307, |
| 109 | CWP_ITEM_ARRAY = 308, |
| 110 | CWP_ITEM_MAP = 309, |
| 111 | CWP_ITEM_EXT = 310, |
| 112 | CWP_NOT_AN_ITEM = 999, |
| 113 | } cwpack_item_types; |
| 114 | |
| 115 | |
| 116 | typedef struct { |
| 117 | const void* start; |
| 118 | uint32_t length; |
| 119 | } cwpack_blob; |
| 120 | |
| 121 | |
| 122 | typedef struct { |
| 123 | uint32_t size; |
| 124 | } cwpack_container; |
| 125 | |
| 126 | |
| 127 | typedef struct { |
| 128 | cwpack_item_types type; |
| 129 | union |
| 130 | { |
| 131 | bool boolean; |
| 132 | uint64_t u64; |
| 133 | int64_t i64; |
| 134 | float real; |
| 135 | double long_real; |
| 136 | cwpack_container array; |
| 137 | cwpack_container map; |
| 138 | cwpack_blob str; |
| 139 | cwpack_blob bin; |
| 140 | cwpack_blob ext; |
| 141 | } as; |
| 142 | } cwpack_item; |
| 143 | |
| 144 | struct cw_unpack_context; |
| 145 | |
| 146 | typedef int (*unpack_underflow_handler)(struct cw_unpack_context*, unsigned long); |
| 147 | |
| 148 | typedef struct cw_unpack_context { |
| 149 | cwpack_item item; |
| 150 | uint8_t* start; |
| 151 | uint8_t* current; |
| 152 | uint8_t* end; /* logical end of buffer */ |
| 153 | int return_code; |
| 154 | int err_no; /* handlers can save error here */ |
| 155 | unpack_underflow_handler handle_unpack_underflow; |
| 156 | } cw_unpack_context; |
| 157 | |
| 158 | |
| 159 | |
| 160 | int cw_unpack_context_init (cw_unpack_context* unpack_context, void* data, unsigned long length, unpack_underflow_handler huu); |
| 161 | |
| 162 | void cw_unpack_next (cw_unpack_context* unpack_context); |
| 163 | void cw_skip_items (cw_unpack_context* unpack_context, long item_count); |
| 164 | |
| 165 | |
| 166 | #endif /* CWPack_H__ */ |