blob: 6e90deac7b356a7c6a31786fed362cc6d98e0a39 [file] [log] [blame]
zongqiang.zhang0c6a0882019-08-07 14:48:21 +08001/* CWPack - cwpack.h */
2/*
3 The MIT License (MIT)
guangcheng.qinf87ed972019-08-30 10:50:01 +08004
zongqiang.zhang0c6a0882019-08-07 14:48:21 +08005 Copyright (c) 2017 Claes Wihlborg
guangcheng.qinf87ed972019-08-30 10:50:01 +08006
zongqiang.zhang0c6a0882019-08-07 14:48:21 +08007 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.qinf87ed972019-08-30 10:50:01 +080012
zongqiang.zhang0c6a0882019-08-07 14:48:21 +080013 The above copyright notice and this permission notice shall be included in all copies or
14 substantial portions of the Software.
guangcheng.qinf87ed972019-08-30 10:50:01 +080015
zongqiang.zhang0c6a0882019-08-07 14:48:21 +080016 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
53struct cw_pack_context;
54
55typedef int (*pack_overflow_handler)(struct cw_pack_context*, unsigned long);
56
guangcheng.qinf87ed972019-08-30 10:50:01 +080057typedef 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.zhang0c6a0882019-08-07 14:48:21 +080066} cw_pack_context;
67
68
guangcheng.qinf87ed972019-08-30 10:50:01 +080069int cw_pack_context_init(cw_pack_context* pack_context, void* data, unsigned long length,
70 pack_overflow_handler hpo);
71void cw_pack_set_compatibility(cw_pack_context* pack_context, bool be_compatible);
zongqiang.zhang0c6a0882019-08-07 14:48:21 +080072
guangcheng.qinf87ed972019-08-30 10:50:01 +080073void cw_pack_nil(cw_pack_context* pack_context);
74void cw_pack_true(cw_pack_context* pack_context);
75void cw_pack_false(cw_pack_context* pack_context);
76void cw_pack_boolean(cw_pack_context* pack_context, bool b);
zongqiang.zhang0c6a0882019-08-07 14:48:21 +080077
guangcheng.qinf87ed972019-08-30 10:50:01 +080078void cw_pack_signed(cw_pack_context* pack_context, int64_t i);
79void cw_pack_unsigned(cw_pack_context* pack_context, uint64_t i);
zongqiang.zhang0c6a0882019-08-07 14:48:21 +080080
guangcheng.qinf87ed972019-08-30 10:50:01 +080081void cw_pack_float(cw_pack_context* pack_context, float f);
82void cw_pack_double(cw_pack_context* pack_context, double d);
83void cw_pack_real(cw_pack_context* pack_context,
84 double d); /* Pack as float if precision isn't destroyed */
zongqiang.zhang0c6a0882019-08-07 14:48:21 +080085
guangcheng.qinf87ed972019-08-30 10:50:01 +080086void cw_pack_array_size(cw_pack_context* pack_context, uint32_t n);
87void cw_pack_map_size(cw_pack_context* pack_context, uint32_t n);
88void cw_pack_str(cw_pack_context* pack_context, const char* v, uint32_t l);
89void cw_pack_bin(cw_pack_context* pack_context, const void* v, uint32_t l);
90void cw_pack_ext(cw_pack_context* pack_context, int8_t type, const void* v, uint32_t l);
zongqiang.zhang0c6a0882019-08-07 14:48:21 +080091
guangcheng.qinf87ed972019-08-30 10:50:01 +080092void cw_pack_insert(cw_pack_context* pack_context, const void* v, uint32_t l);
zongqiang.zhang0c6a0882019-08-07 14:48:21 +080093
94
95/***************************** U N P A C K ********************************/
96
97
98typedef enum
99{
guangcheng.qinf87ed972019-08-30 10:50:01 +0800100 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.zhang0c6a0882019-08-07 14:48:21 +0800116} cwpack_item_types;
117
118
guangcheng.qinf87ed972019-08-30 10:50:01 +0800119typedef struct
120{
121 const void* start;
122 uint32_t length;
zongqiang.zhang0c6a0882019-08-07 14:48:21 +0800123} cwpack_blob;
124
125
guangcheng.qinf87ed972019-08-30 10:50:01 +0800126typedef struct
127{
128 uint32_t size;
zongqiang.zhang0c6a0882019-08-07 14:48:21 +0800129} cwpack_container;
130
131
guangcheng.qinf87ed972019-08-30 10:50:01 +0800132typedef 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.zhang0c6a0882019-08-07 14:48:21 +0800148} cwpack_item;
149
150struct cw_unpack_context;
151
152typedef int (*unpack_underflow_handler)(struct cw_unpack_context*, unsigned long);
153
guangcheng.qinf87ed972019-08-30 10:50:01 +0800154typedef 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.zhang0c6a0882019-08-07 14:48:21 +0800163} cw_unpack_context;
164
165
166
guangcheng.qinf87ed972019-08-30 10:50:01 +0800167int cw_unpack_context_init(cw_unpack_context* unpack_context, void* data,
168 unsigned long length, unpack_underflow_handler huu);
zongqiang.zhang0c6a0882019-08-07 14:48:21 +0800169
guangcheng.qinf87ed972019-08-30 10:50:01 +0800170void cw_unpack_next(cw_unpack_context* unpack_context);
171void cw_skip_items(cw_unpack_context* unpack_context, long item_count);
zongqiang.zhang0c6a0882019-08-07 14:48:21 +0800172
173
174#endif /* CWPack_H__ */