Loading...
Searching...
No Matches
as_msgpack.h
Go to the documentation of this file.
1/*
2 * Copyright 2008-2024 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
23extern "C" {
24#endif
25
26//---------------------------------
27// Macros
28//---------------------------------
29
30#define AS_PACKER_BUFFER_SIZE 8192
31
32#define AS_PACKED_MAP_FLAG_NONE 0x00
33#define AS_PACKED_MAP_FLAG_K_ORDERED 0x01
34#define AS_PACKED_MAP_FLAG_V_ORDERED 0x02 // not allowed on its own
35#define AS_PACKED_MAP_FLAG_PRESERVE_ORDER 0x08
36#define AS_PACKED_MAP_FLAG_KV_ORDERED (AS_PACKED_MAP_FLAG_K_ORDERED | AS_PACKED_MAP_FLAG_V_ORDERED)
37
38#define AS_PACKED_LIST_FLAG_NONE 0x00
39#define AS_PACKED_LIST_FLAG_ORDERED 0x01
40
41#define AS_PACKED_PERSIST_INDEX 0x10
42
43//---------------------------------
44// Types
45//---------------------------------
46
47typedef struct as_packer_buffer {
49 unsigned char *buffer;
50 uint32_t length;
52
53typedef struct as_packer {
56 unsigned char *buffer;
57 uint32_t offset;
58 uint32_t capacity;
59} as_packer;
60
61typedef struct as_unpacker {
62 const unsigned char *buffer;
63 uint32_t offset;
64 uint32_t length;
66
67typedef struct as_msgpack_ext_s {
68 const uint8_t *data; // pointer to ext contents
69 uint32_t size; // size of ext contents
70 uint32_t type_offset; // offset where the type field is located
71 uint8_t type; // type of ext contents
73
81
82//---------------------------------
83// Functions
84//---------------------------------
85
88
89/**
90 * @return 0 on success
91 */
92int as_pack_val(as_packer *pk, const as_val *val);
93/**
94 * @return 0 on success
95 */
97
99
100//---------------------------------
101// Pack direct functions
102//---------------------------------
103
104static inline uint32_t as_pack_nil_size(void)
105{
106 return 1;
107}
108static inline uint32_t as_pack_bool_size(void)
109{
110 return 1;
111}
113int as_pack_bool(as_packer *pk, bool val);
114
115static inline uint32_t as_pack_cmp_inf_size(void)
116{
117 return 3;
118}
119static inline uint32_t as_pack_cmp_wildcard_size(void)
120{
121 return 3;
122}
125
126uint32_t as_pack_uint64_size(uint64_t val);
127uint32_t as_pack_int64_size(int64_t val);
128AS_EXTERN int as_pack_uint64(as_packer *pk, uint64_t val);
129AS_EXTERN int as_pack_int64(as_packer *pk, int64_t val);
130
131static inline uint32_t as_pack_float_size(void)
132{
133 return 1 + 4;
134}
135static inline uint32_t as_pack_double_size(void)
136{
137 return 1 + 8;
138}
139/**
140 * Pack a float.
141 * @return 0 on success
142 */
143int as_pack_float(as_packer *pk, float val);
144int as_pack_double(as_packer *pk, double val);
145
146int as_pack_bytes(as_packer *pk, const uint8_t *buf, uint32_t sz);
147
148uint32_t as_pack_str_size(uint32_t str_sz);
149uint32_t as_pack_bin_size(uint32_t buf_sz);
150/**
151 * Pack a str.
152 * @return 0 on success
153 */
154int as_pack_str(as_packer *pk, const uint8_t *buf, uint32_t sz);
155int as_pack_bin(as_packer *pk, const uint8_t *buf, uint32_t sz);
156/**
157 * Pack a list header with ele_count.
158 * @return 0 on success
159 */
160AS_EXTERN int as_pack_list_header(as_packer *pk, uint32_t ele_count);
161/**
162 * Get packed header size for list with ele_count.
163 * @return header size in bytes
164 */
165uint32_t as_pack_list_header_get_size(uint32_t ele_count);
166/**
167 * Pack a map header with ele_count.
168 * @return 0 on success
169 */
170AS_EXTERN int as_pack_map_header(as_packer *pk, uint32_t ele_count);
171/**
172 * Get packed header size for map with ele_count.
173 * @return header size in bytes
174 */
175static inline uint32_t as_pack_map_header_get_size(uint32_t ele_count)
176{
177 return as_pack_list_header_get_size(ele_count);
178}
179/**
180 * Get size of an ext header.
181 * @param content_size size in bytes of ext contents
182 * @return size of header in bytes
183 */
184uint32_t as_pack_ext_header_get_size(uint32_t content_size);
185/**
186 * Pack an ext type.
187 * @return 0 on success
188 */
189AS_EXTERN int as_pack_ext_header(as_packer *pk, uint32_t content_size, uint8_t type);
190int as_pack_buf_ext_header(uint8_t *buf, uint32_t size, uint32_t content_size, uint8_t type);
191
192AS_EXTERN int as_pack_append(as_packer *pk, const unsigned char *buf, uint32_t sz);
193
194//---------------------------------
195// Unpack direct functions
196//---------------------------------
197
198/**
199 * Check next element without consuming any bytes.
200 * @return type of next element
201 */
203as_val_t as_unpack_buf_peek_type(const uint8_t *buf, uint32_t size);
204/**
205 * Check next element without consuming any bytes.
206 * @return true if ext type
207 */
209/**
210 * Unpack boolean.
211 * @return 0 on success
212 */
213int as_unpack_boolean(as_unpacker *pk, bool *value);
214/**
215 * Unpack nil.
216 * @return 0 on success
217 */
219/**
220 * Get size of packed value.
221 * @return negative int on error, size on success
222 */
224/**
225 * Get size of packed blob.
226 * @return negative int on error, size on success
227 */
229/**
230 * Unpack integer.
231 * @return 0 on success
232 */
233int as_unpack_int64(as_unpacker *pk, int64_t *i);
234int as_unpack_uint64(as_unpacker *pk, uint64_t *i);
235/**
236 * Unpack double.
237 * @return 0 on success
238 */
239int as_unpack_double(as_unpacker *pk, double *x);
240/**
241 * Unpack str (or bin).
242 * @return NULL on failure
243 */
244const uint8_t *as_unpack_str(as_unpacker *pk, uint32_t *sz_r);
245/**
246 * Unpack bin (or str).
247 * @return NULL on failure
248 */
249const uint8_t *as_unpack_bin(as_unpacker *pk, uint32_t *sz_r);
250/**
251 * Unpack extension type.
252 * @return true on success
253 */
255/**
256 * Unpack list element count from buffer.
257 */
258int64_t as_unpack_buf_list_element_count(const uint8_t *buf, uint32_t size);
259/**
260 * Get element count of packed list.
261 * @return negative int on failure, element count on success
262 */
264/**
265 * Unpack map element count from buffer.
266 */
267int64_t as_unpack_buf_map_element_count(const uint8_t *buf, uint32_t size);
268/**
269 * Get element count of packed map.
270 * @return negative int on failure, element count on success
271 */
273
274/**
275 * Compare two msgpack buffers.
276 */
277msgpack_compare_t as_unpack_buf_compare(const uint8_t *buf1, uint32_t size1, const uint8_t *buf2, uint32_t size2);
279/**
280 * Compare two msgpack buffers.
281 * @return true if buf1 < buf2
282 */
283static inline bool as_unpack_buf_is_less(const uint8_t *buf1, uint32_t size1, const uint8_t *buf2, uint32_t size2)
284{
285 return as_unpack_buf_compare(buf1, size1, buf2, size2) == MSGPACK_COMPARE_LESS;
286}
287
288#ifdef __cplusplus
289} // end extern "C"
290#endif
int as_pack_str(as_packer *pk, const uint8_t *buf, uint32_t sz)
uint32_t as_pack_uint64_size(uint64_t val)
int as_unpack_nil(as_unpacker *pk)
static uint32_t as_pack_cmp_wildcard_size(void)
Definition as_msgpack.h:119
int as_unpack_double(as_unpacker *pk, double *x)
int64_t as_unpack_buf_list_element_count(const uint8_t *buf, uint32_t size)
int as_pack_bool(as_packer *pk, bool val)
AS_EXTERN int as_pack_nil(as_packer *pk)
int as_unpack_ext(as_unpacker *pk, as_msgpack_ext *ext)
uint32_t as_pack_ext_header_get_size(uint32_t content_size)
as_val_t as_unpack_buf_peek_type(const uint8_t *buf, uint32_t size)
const uint8_t * as_unpack_str(as_unpacker *pk, uint32_t *sz_r)
int as_unpack_val(as_unpacker *pk, as_val **val)
AS_EXTERN int as_pack_uint64(as_packer *pk, uint64_t val)
msgpack_compare_t
Definition as_msgpack.h:74
@ MSGPACK_COMPARE_LESS
Definition as_msgpack.h:77
@ MSGPACK_COMPARE_END
Definition as_msgpack.h:76
@ MSGPACK_COMPARE_GREATER
Definition as_msgpack.h:79
@ MSGPACK_COMPARE_EQUAL
Definition as_msgpack.h:78
@ MSGPACK_COMPARE_ERROR
Definition as_msgpack.h:75
int as_pack_bytes(as_packer *pk, const uint8_t *buf, uint32_t sz)
static uint32_t as_pack_float_size(void)
Definition as_msgpack.h:131
AS_EXTERN int as_pack_list_header(as_packer *pk, uint32_t ele_count)
uint32_t as_pack_int64_size(int64_t val)
msgpack_compare_t as_unpack_compare(as_unpacker *pk1, as_unpacker *pk2)
int64_t as_unpack_blob_size(as_unpacker *pk)
int64_t as_unpack_size(as_unpacker *pk)
uint32_t as_pack_str_size(uint32_t str_sz)
static uint32_t as_pack_map_header_get_size(uint32_t ele_count)
Definition as_msgpack.h:175
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:283
AS_EXTERN int as_pack_ext_header(as_packer *pk, uint32_t content_size, uint8_t type)
static uint32_t as_pack_bool_size(void)
Definition as_msgpack.h:108
int64_t as_unpack_buf_map_element_count(const uint8_t *buf, uint32_t size)
int as_pack_float(as_packer *pk, float val)
uint32_t as_pack_bin_size(uint32_t buf_sz)
static uint32_t as_pack_cmp_inf_size(void)
Definition as_msgpack.h:115
int as_pack_double(as_packer *pk, double val)
msgpack_compare_t as_unpack_buf_compare(const uint8_t *buf1, uint32_t size1, const uint8_t *buf2, uint32_t size2)
bool as_unpack_peek_is_ext(const as_unpacker *pk)
int as_pack_bin(as_packer *pk, const uint8_t *buf, uint32_t sz)
int as_pack_cmp_wildcard(as_packer *pk)
int as_unpack_boolean(as_unpacker *pk, bool *value)
as_val_t as_unpack_peek_type(const as_unpacker *pk)
AS_EXTERN as_serializer * as_msgpack_init(as_serializer *)
int64_t as_unpack_map_header_element_count(as_unpacker *pk)
AS_EXTERN as_serializer * as_msgpack_new(void)
static uint32_t as_pack_double_size(void)
Definition as_msgpack.h:135
AS_EXTERN int as_pack_map_header(as_packer *pk, uint32_t ele_count)
int as_pack_val(as_packer *pk, const as_val *val)
uint32_t as_pack_list_header_get_size(uint32_t ele_count)
AS_EXTERN msgpack_compare_t as_val_cmp(const as_val *v1, const as_val *v2)
AS_EXTERN int as_pack_int64(as_packer *pk, int64_t val)
int as_pack_cmp_inf(as_packer *pk)
AS_EXTERN int as_pack_append(as_packer *pk, const unsigned char *buf, uint32_t sz)
const uint8_t * as_unpack_bin(as_unpacker *pk, uint32_t *sz_r)
int as_unpack_int64(as_unpacker *pk, int64_t *i)
int64_t as_unpack_list_header_element_count(as_unpacker *pk)
int as_pack_buf_ext_header(uint8_t *buf, uint32_t size, uint32_t content_size, uint8_t type)
static uint32_t as_pack_nil_size(void)
Definition as_msgpack.h:104
int as_unpack_uint64(as_unpacker *pk, uint64_t *i)
uint8_t type
Definition as_proto.h:1
uint64_t sz
Definition as_proto.h:2
#define AS_EXTERN
Definition as_std.h:25
uint8_t as_val_t
Definition as_val.h:32
uint32_t size
Definition as_msgpack.h:69
const uint8_t * data
Definition as_msgpack.h:68
uint32_t type_offset
Definition as_msgpack.h:70
uint32_t length
Definition as_msgpack.h:50
struct as_packer_buffer * next
Definition as_msgpack.h:48
unsigned char * buffer
Definition as_msgpack.h:49
struct as_packer_buffer * head
Definition as_msgpack.h:54
unsigned char * buffer
Definition as_msgpack.h:56
uint32_t offset
Definition as_msgpack.h:57
uint32_t capacity
Definition as_msgpack.h:58
struct as_packer_buffer * tail
Definition as_msgpack.h:55
const unsigned char * buffer
Definition as_msgpack.h:62
uint32_t offset
Definition as_msgpack.h:63
uint32_t length
Definition as_msgpack.h:64