All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
as_msgpack.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2023 Aerospike, Inc.
3  *
4  * Portions may be licensed to Aerospike, Inc. under one or more contributor
5  * license agreements.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
8  * use this file except in compliance with the License. You may obtain a copy of
9  * the License at http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations under
15  * the License.
16  */
17 #pragma once
18 
20 #include <aerospike/as_std.h>
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #define AS_PACKER_BUFFER_SIZE 8192
27 
28 #define AS_PACKED_MAP_FLAG_NONE 0x00
29 #define AS_PACKED_MAP_FLAG_K_ORDERED 0x01
30 #define AS_PACKED_MAP_FLAG_V_ORDERED 0x02 // not allowed on its own
31 #define AS_PACKED_MAP_FLAG_PRESERVE_ORDER 0x08
32 #define AS_PACKED_MAP_FLAG_KV_ORDERED (AS_PACKED_MAP_FLAG_K_ORDERED | AS_PACKED_MAP_FLAG_V_ORDERED)
33 
34 #define AS_PACKED_LIST_FLAG_NONE 0x00
35 #define AS_PACKED_LIST_FLAG_ORDERED 0x01
36 
37 #define AS_PACKED_PERSIST_INDEX 0x10
38 
39 typedef struct as_packer_buffer {
41  unsigned char *buffer;
42  uint32_t length;
44 
45 typedef struct as_packer {
48  unsigned char *buffer;
49  uint32_t offset;
50  uint32_t capacity;
51 } as_packer;
52 
53 typedef struct as_unpacker {
54  const unsigned char *buffer;
55  uint32_t offset;
56  uint32_t length;
57 } as_unpacker;
58 
59 typedef struct as_msgpack_ext_s {
60  const uint8_t *data; // pointer to ext contents
61  uint32_t size; // size of ext contents
62  uint32_t type_offset; // offset where the type field is located
63  uint8_t type; // type of ext contents
65 
66 typedef enum msgpack_compare_e {
73 
74 /******************************************************************************
75  * FUNCTIONS
76  ******************************************************************************/
77 
80 
81 /**
82  * @return 0 on success
83  */
84 int as_pack_val(as_packer *pk, const as_val *val);
85 /**
86  * @return 0 on success
87  */
88 int as_unpack_val(as_unpacker *pk, as_val **val);
89 
90 AS_EXTERN msgpack_compare_t as_val_cmp(const as_val* v1, const as_val* v2);
91 
92 /******************************************************************************
93  * Pack direct functions
94  ******************************************************************************/
95 
96 static inline uint32_t as_pack_nil_size()
97 {
98  return 1;
99 }
100 static inline uint32_t as_pack_bool_size()
101 {
102  return 1;
103 }
104 int as_pack_nil(as_packer *pk);
105 int as_pack_bool(as_packer *pk, bool val);
106 
107 static inline uint32_t as_pack_cmp_inf_size()
108 {
109  return 3;
110 }
111 static inline uint32_t as_pack_cmp_wildcard_size()
112 {
113  return 3;
114 }
115 int as_pack_cmp_inf(as_packer *pk);
117 
118 uint32_t as_pack_uint64_size(uint64_t val);
119 uint32_t as_pack_int64_size(int64_t val);
120 int as_pack_uint64(as_packer *pk, uint64_t val);
121 int as_pack_int64(as_packer *pk, int64_t val);
122 
123 static inline uint32_t as_pack_float_size()
124 {
125  return 1 + 4;
126 }
127 static inline uint32_t as_pack_double_size()
128 {
129  return 1 + 8;
130 }
131 /**
132  * Pack a float.
133  * @return 0 on success
134  */
135 int as_pack_float(as_packer *pk, float val);
136 int as_pack_double(as_packer *pk, double val);
137 
138 int as_pack_bytes(as_packer *pk, const uint8_t *buf, uint32_t sz);
139 
140 uint32_t as_pack_str_size(uint32_t str_sz);
141 uint32_t as_pack_bin_size(uint32_t buf_sz);
142 /**
143  * Pack a str.
144  * @return 0 on success
145  */
146 int as_pack_str(as_packer *pk, const uint8_t *buf, uint32_t sz);
147 int as_pack_bin(as_packer *pk, const uint8_t *buf, uint32_t sz);
148 /**
149  * Pack a list header with ele_count.
150  * @return 0 on success
151  */
152 int as_pack_list_header(as_packer *pk, uint32_t ele_count);
153 /**
154  * Get packed header size for list with ele_count.
155  * @return header size in bytes
156  */
157 uint32_t as_pack_list_header_get_size(uint32_t ele_count);
158 /**
159  * Pack a map header with ele_count.
160  * @return 0 on success
161  */
162 int as_pack_map_header(as_packer *pk, uint32_t ele_count);
163 /**
164  * Get packed header size for map with ele_count.
165  * @return header size in bytes
166  */
167 static inline uint32_t as_pack_map_header_get_size(uint32_t ele_count)
168 {
169  return as_pack_list_header_get_size(ele_count);
170 }
171 /**
172  * Get size of an ext header.
173  * @param content_size size in bytes of ext contents
174  * @return size of header in bytes
175  */
176 uint32_t as_pack_ext_header_get_size(uint32_t content_size);
177 /**
178  * Pack an ext type.
179  * @return 0 on success
180  */
181 int as_pack_ext_header(as_packer *pk, uint32_t content_size, uint8_t type);
182 int as_pack_buf_ext_header(uint8_t *buf, uint32_t size, uint32_t content_size, uint8_t type);
183 
184 int as_pack_append(as_packer *pk, const unsigned char *buf, uint32_t sz);
185 
186 /******************************************************************************
187  * Unpack direct functions
188  ******************************************************************************/
189 
190 /**
191  * Check next element without consuming any bytes.
192  * @return type of next element
193  */
195 as_val_t as_unpack_buf_peek_type(const uint8_t *buf, uint32_t size);
196 /**
197  * Check next element without consuming any bytes.
198  * @return true if ext type
199  */
200 bool as_unpack_peek_is_ext(const as_unpacker *pk);
201 /**
202  * Unpack boolean.
203  * @return 0 on success
204  */
205 int as_unpack_boolean(as_unpacker *pk, bool *value);
206 /**
207  * Unpack nil.
208  * @return 0 on success
209  */
210 int as_unpack_nil(as_unpacker *pk);
211 /**
212  * Get size of packed value.
213  * @return negative int on error, size on success
214  */
215 int64_t as_unpack_size(as_unpacker *pk);
216 /**
217  * Get size of packed blob.
218  * @return negative int on error, size on success
219  */
220 int64_t as_unpack_blob_size(as_unpacker *pk);
221 /**
222  * Unpack integer.
223  * @return 0 on success
224  */
225 int as_unpack_int64(as_unpacker *pk, int64_t *i);
226 int as_unpack_uint64(as_unpacker *pk, uint64_t *i);
227 /**
228  * Unpack double.
229  * @return 0 on success
230  */
231 int as_unpack_double(as_unpacker *pk, double *x);
232 /**
233  * Unpack str (or bin).
234  * @return NULL on failure
235  */
236 const uint8_t *as_unpack_str(as_unpacker *pk, uint32_t *sz_r);
237 /**
238  * Unpack bin (or str).
239  * @return NULL on failure
240  */
241 const uint8_t *as_unpack_bin(as_unpacker *pk, uint32_t *sz_r);
242 /**
243  * Unpack extension type.
244  * @return true on success
245  */
247 /**
248  * Unpack list element count from buffer.
249  */
250 int64_t as_unpack_buf_list_element_count(const uint8_t *buf, uint32_t size);
251 /**
252  * Get element count of packed list.
253  * @return negative int on failure, element count on success
254  */
256 /**
257  * Unpack map element count from buffer.
258  */
259 int64_t as_unpack_buf_map_element_count(const uint8_t *buf, uint32_t size);
260 /**
261  * Get element count of packed map.
262  * @return negative int on failure, element count on success
263  */
265 
266 /**
267  * Compare two msgpack buffers.
268  */
269 msgpack_compare_t as_unpack_buf_compare(const uint8_t *buf1, uint32_t size1, const uint8_t *buf2, uint32_t size2);
271 /**
272  * Compare two msgpack buffers.
273  * @return true if buf1 < buf2
274  */
275 static inline bool as_unpack_buf_is_less(const uint8_t *buf1, uint32_t size1, const uint8_t *buf2, uint32_t size2)
276 {
277  return as_unpack_buf_compare(buf1, size1, buf2, size2) == MSGPACK_COMPARE_LESS;
278 }
279 
280 #ifdef __cplusplus
281 } // end extern "C"
282 #endif
int as_pack_append(as_packer *pk, const unsigned char *buf, uint32_t sz)
struct as_packer_buffer * tail
Definition: as_msgpack.h:47
as_val_t as_unpack_buf_peek_type(const uint8_t *buf, uint32_t size)
uint32_t length
Definition: as_msgpack.h:42
int64_t as_unpack_buf_list_element_count(const uint8_t *buf, uint32_t size)
uint8_t type
Definition: as_proto.h:36
int as_pack_bin(as_packer *pk, const uint8_t *buf, uint32_t sz)
int as_pack_double(as_packer *pk, double val)
uint32_t length
Definition: as_msgpack.h:56
int as_pack_map_header(as_packer *pk, uint32_t ele_count)
int as_pack_nil(as_packer *pk)
int as_unpack_int64(as_unpacker *pk, int64_t *i)
int as_unpack_uint64(as_unpacker *pk, uint64_t *i)
uint32_t as_pack_bin_size(uint32_t buf_sz)
uint32_t offset
Definition: as_msgpack.h:49
int64_t as_unpack_buf_map_element_count(const uint8_t *buf, uint32_t size)
static uint32_t as_pack_map_header_get_size(uint32_t ele_count)
Definition: as_msgpack.h:167
Definition: as_val.h:61
int as_pack_uint64(as_packer *pk, uint64_t val)
int as_pack_float(as_packer *pk, float val)
static uint32_t as_pack_nil_size()
Definition: as_msgpack.h:96
int as_pack_val(as_packer *pk, const as_val *val)
uint32_t capacity
Definition: as_msgpack.h:50
uint32_t as_pack_list_header_get_size(uint32_t ele_count)
int as_pack_list_header(as_packer *pk, uint32_t ele_count)
int as_unpack_boolean(as_unpacker *pk, bool *value)
AS_EXTERN as_serializer * as_msgpack_init(as_serializer *)
#define AS_EXTERN
Definition: as_std.h:25
int as_pack_ext_header(as_packer *pk, uint32_t content_size, uint8_t type)
unsigned char * buffer
Definition: as_msgpack.h:48
int as_unpack_nil(as_unpacker *pk)
int as_pack_bytes(as_packer *pk, const uint8_t *buf, uint32_t sz)
uint8_t as_val_t
Definition: as_val.h:32
uint32_t as_pack_int64_size(int64_t val)
msgpack_compare_t as_unpack_buf_compare(const uint8_t *buf1, uint32_t size1, const uint8_t *buf2, uint32_t size2)
uint32_t as_pack_ext_header_get_size(uint32_t content_size)
static uint32_t as_pack_bool_size()
Definition: as_msgpack.h:100
static bool as_unpack_buf_is_less(const uint8_t *buf1, uint32_t size1, const uint8_t *buf2, uint32_t size2)
Definition: as_msgpack.h:275
int as_pack_buf_ext_header(uint8_t *buf, uint32_t size, uint32_t content_size, uint8_t type)
int64_t as_unpack_map_header_element_count(as_unpacker *pk)
int as_pack_str(as_packer *pk, const uint8_t *buf, uint32_t sz)
uint32_t as_pack_str_size(uint32_t str_sz)
static uint32_t as_pack_float_size()
Definition: as_msgpack.h:123
as_val_t as_unpack_peek_type(const as_unpacker *pk)
uint64_t sz
Definition: as_proto.h:37
uint32_t type_offset
Definition: as_msgpack.h:62
uint32_t as_pack_uint64_size(uint64_t val)
struct as_packer_buffer * head
Definition: as_msgpack.h:46
struct as_packer_buffer * next
Definition: as_msgpack.h:40
int64_t as_unpack_blob_size(as_unpacker *pk)
uint32_t offset
Definition: as_msgpack.h:55
unsigned char * buffer
Definition: as_msgpack.h:41
static uint32_t as_pack_double_size()
Definition: as_msgpack.h:127
msgpack_compare_t
Definition: as_msgpack.h:66
uint32_t size
Definition: as_msgpack.h:61
static uint32_t as_pack_cmp_wildcard_size()
Definition: as_msgpack.h:111
AS_EXTERN as_serializer * as_msgpack_new(void)
int as_unpack_val(as_unpacker *pk, as_val **val)
int as_pack_cmp_inf(as_packer *pk)
const uint8_t * as_unpack_bin(as_unpacker *pk, uint32_t *sz_r)
AS_EXTERN msgpack_compare_t as_val_cmp(const as_val *v1, const as_val *v2)
const uint8_t * data
Definition: as_msgpack.h:60
int as_pack_bool(as_packer *pk, bool val)
const unsigned char * buffer
Definition: as_msgpack.h:54
int as_pack_int64(as_packer *pk, int64_t val)
int64_t as_unpack_size(as_unpacker *pk)
int as_pack_cmp_wildcard(as_packer *pk)
msgpack_compare_t as_unpack_compare(as_unpacker *pk1, as_unpacker *pk2)
int64_t as_unpack_list_header_element_count(as_unpacker *pk)
bool as_unpack_peek_is_ext(const as_unpacker *pk)
const uint8_t * as_unpack_str(as_unpacker *pk, uint32_t *sz_r)
int as_unpack_double(as_unpacker *pk, double *x)
uint8_t type
Definition: as_msgpack.h:63
static uint32_t as_pack_cmp_inf_size()
Definition: as_msgpack.h:107
int as_unpack_ext(as_unpacker *pk, as_msgpack_ext *ext)