]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/jansson/jansson.h
start fixing hf 15: (#684)
[proxmark3-svn] / client / jansson / jansson.h
CommitLineData
556826b5
OM
1/*
2 * Copyright (c) 2009-2016 Petri Lehtinen <petri@digip.org>
3 *
4 * Jansson is free software; you can redistribute it and/or modify
5 * it under the terms of the MIT license. See LICENSE for details.
6 */
7
8#ifndef JANSSON_H
9#define JANSSON_H
10
11#include <stdio.h>
12#include <stdint.h>
13#include <stdlib.h> /* for size_t */
14#include <stdarg.h>
15
16#include "jansson_config.h"
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22/* version */
23
24#define JANSSON_MAJOR_VERSION 2
25#define JANSSON_MINOR_VERSION 11
26#define JANSSON_MICRO_VERSION 0
27
28/* Micro version is omitted if it's 0 */
29#define JANSSON_VERSION "2.11"
30
31/* Version as a 3-byte hex number, e.g. 0x010201 == 1.2.1. Use this
32 for numeric comparisons, e.g. #if JANSSON_VERSION_HEX >= ... */
33#define JANSSON_VERSION_HEX ((JANSSON_MAJOR_VERSION << 16) | \
34 (JANSSON_MINOR_VERSION << 8) | \
35 (JANSSON_MICRO_VERSION << 0))
36
37/* If __atomic or __sync builtins are available the library is thread
38 * safe for all read-only functions plus reference counting. */
39#if JSON_HAVE_ATOMIC_BUILTINS || JSON_HAVE_SYNC_BUILTINS
40#define JANSSON_THREAD_SAFE_REFCOUNT 1
41#endif
42
43/* types */
44
45typedef enum {
46 JSON_OBJECT,
47 JSON_ARRAY,
48 JSON_STRING,
49 JSON_INTEGER,
50 JSON_REAL,
51 JSON_TRUE,
52 JSON_FALSE,
53 JSON_NULL
54} json_type;
55
56typedef struct json_t {
57 json_type type;
58 volatile size_t refcount;
59} json_t;
60
61#ifndef JANSSON_USING_CMAKE /* disabled if using cmake */
62#if JSON_INTEGER_IS_LONG_LONG
63#ifdef _WIN32
64#define JSON_INTEGER_FORMAT "I64d"
65#else
66#define JSON_INTEGER_FORMAT "lld"
67#endif
68typedef long long json_int_t;
69#else
70#define JSON_INTEGER_FORMAT "ld"
71typedef long json_int_t;
72#endif /* JSON_INTEGER_IS_LONG_LONG */
73#endif
74
75#define json_typeof(json) ((json)->type)
76#define json_is_object(json) ((json) && json_typeof(json) == JSON_OBJECT)
77#define json_is_array(json) ((json) && json_typeof(json) == JSON_ARRAY)
78#define json_is_string(json) ((json) && json_typeof(json) == JSON_STRING)
79#define json_is_integer(json) ((json) && json_typeof(json) == JSON_INTEGER)
80#define json_is_real(json) ((json) && json_typeof(json) == JSON_REAL)
81#define json_is_number(json) (json_is_integer(json) || json_is_real(json))
82#define json_is_true(json) ((json) && json_typeof(json) == JSON_TRUE)
83#define json_is_false(json) ((json) && json_typeof(json) == JSON_FALSE)
84#define json_boolean_value json_is_true
85#define json_is_boolean(json) (json_is_true(json) || json_is_false(json))
86#define json_is_null(json) ((json) && json_typeof(json) == JSON_NULL)
87
88/* construction, destruction, reference counting */
89
90json_t *json_object(void);
91json_t *json_array(void);
92json_t *json_string(const char *value);
93json_t *json_stringn(const char *value, size_t len);
94json_t *json_string_nocheck(const char *value);
95json_t *json_stringn_nocheck(const char *value, size_t len);
96json_t *json_integer(json_int_t value);
97json_t *json_real(double value);
98json_t *json_true(void);
99json_t *json_false(void);
100#define json_boolean(val) ((val) ? json_true() : json_false())
101json_t *json_null(void);
102
103/* do not call JSON_INTERNAL_INCREF or JSON_INTERNAL_DECREF directly */
104#if JSON_HAVE_ATOMIC_BUILTINS
105#define JSON_INTERNAL_INCREF(json) __atomic_add_fetch(&json->refcount, 1, __ATOMIC_ACQUIRE)
106#define JSON_INTERNAL_DECREF(json) __atomic_sub_fetch(&json->refcount, 1, __ATOMIC_RELEASE)
107#elif JSON_HAVE_SYNC_BUILTINS
108#define JSON_INTERNAL_INCREF(json) __sync_add_and_fetch(&json->refcount, 1)
109#define JSON_INTERNAL_DECREF(json) __sync_sub_and_fetch(&json->refcount, 1)
110#else
111#define JSON_INTERNAL_INCREF(json) (++json->refcount)
112#define JSON_INTERNAL_DECREF(json) (--json->refcount)
113#endif
114
115static JSON_INLINE
116json_t *json_incref(json_t *json)
117{
118 if(json && json->refcount != (size_t)-1)
119 JSON_INTERNAL_INCREF(json);
120 return json;
121}
122
123/* do not call json_delete directly */
124void json_delete(json_t *json);
125
126static JSON_INLINE
127void json_decref(json_t *json)
128{
129 if(json && json->refcount != (size_t)-1 && JSON_INTERNAL_DECREF(json) == 0)
130 json_delete(json);
131}
132
133#if defined(__GNUC__) || defined(__clang__)
134static JSON_INLINE
135void json_decrefp(json_t **json)
136{
137 if(json) {
138 json_decref(*json);
139 *json = NULL;
140 }
141}
142
143#define json_auto_t json_t __attribute__((cleanup(json_decrefp)))
144#endif
145
146
147/* error reporting */
148
149#define JSON_ERROR_TEXT_LENGTH 160
150#define JSON_ERROR_SOURCE_LENGTH 80
151
152typedef struct json_error_t {
153 int line;
154 int column;
155 int position;
156 char source[JSON_ERROR_SOURCE_LENGTH];
157 char text[JSON_ERROR_TEXT_LENGTH];
158} json_error_t;
159
160enum json_error_code {
161 json_error_unknown,
162 json_error_out_of_memory,
163 json_error_stack_overflow,
164 json_error_cannot_open_file,
165 json_error_invalid_argument,
166 json_error_invalid_utf8,
167 json_error_premature_end_of_input,
168 json_error_end_of_input_expected,
169 json_error_invalid_syntax,
170 json_error_invalid_format,
171 json_error_wrong_type,
172 json_error_null_character,
173 json_error_null_value,
174 json_error_null_byte_in_key,
175 json_error_duplicate_key,
176 json_error_numeric_overflow,
177 json_error_item_not_found,
178 json_error_index_out_of_range
179};
180
181static JSON_INLINE enum json_error_code json_error_code(const json_error_t *e) {
182 return (enum json_error_code)e->text[JSON_ERROR_TEXT_LENGTH - 1];
183}
184
185/* getters, setters, manipulation */
186
187void json_object_seed(size_t seed);
188size_t json_object_size(const json_t *object);
189json_t *json_object_get(const json_t *object, const char *key);
190int json_object_set_new(json_t *object, const char *key, json_t *value);
191int json_object_set_new_nocheck(json_t *object, const char *key, json_t *value);
192int json_object_del(json_t *object, const char *key);
193int json_object_clear(json_t *object);
194int json_object_update(json_t *object, json_t *other);
195int json_object_update_existing(json_t *object, json_t *other);
196int json_object_update_missing(json_t *object, json_t *other);
197void *json_object_iter(json_t *object);
198void *json_object_iter_at(json_t *object, const char *key);
199void *json_object_key_to_iter(const char *key);
200void *json_object_iter_next(json_t *object, void *iter);
201const char *json_object_iter_key(void *iter);
202json_t *json_object_iter_value(void *iter);
203int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
204
205#define json_object_foreach(object, key, value) \
206 for(key = json_object_iter_key(json_object_iter(object)); \
207 key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
208 key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key))))
209
210#define json_object_foreach_safe(object, n, key, value) \
211 for(key = json_object_iter_key(json_object_iter(object)), \
212 n = json_object_iter_next(object, json_object_key_to_iter(key)); \
213 key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
214 key = json_object_iter_key(n), \
215 n = json_object_iter_next(object, json_object_key_to_iter(key)))
216
217#define json_array_foreach(array, index, value) \
218 for(index = 0; \
219 index < json_array_size(array) && (value = json_array_get(array, index)); \
220 index++)
221
222static JSON_INLINE
223int json_object_set(json_t *object, const char *key, json_t *value)
224{
225 return json_object_set_new(object, key, json_incref(value));
226}
227
228static JSON_INLINE
229int json_object_set_nocheck(json_t *object, const char *key, json_t *value)
230{
231 return json_object_set_new_nocheck(object, key, json_incref(value));
232}
233
234static JSON_INLINE
235int json_object_iter_set(json_t *object, void *iter, json_t *value)
236{
237 return json_object_iter_set_new(object, iter, json_incref(value));
238}
239
240size_t json_array_size(const json_t *array);
241json_t *json_array_get(const json_t *array, size_t index);
242int json_array_set_new(json_t *array, size_t index, json_t *value);
243int json_array_append_new(json_t *array, json_t *value);
244int json_array_insert_new(json_t *array, size_t index, json_t *value);
245int json_array_remove(json_t *array, size_t index);
246int json_array_clear(json_t *array);
247int json_array_extend(json_t *array, json_t *other);
248
249static JSON_INLINE
250int json_array_set(json_t *array, size_t ind, json_t *value)
251{
252 return json_array_set_new(array, ind, json_incref(value));
253}
254
255static JSON_INLINE
256int json_array_append(json_t *array, json_t *value)
257{
258 return json_array_append_new(array, json_incref(value));
259}
260
261static JSON_INLINE
262int json_array_insert(json_t *array, size_t ind, json_t *value)
263{
264 return json_array_insert_new(array, ind, json_incref(value));
265}
266
267const char *json_string_value(const json_t *string);
268size_t json_string_length(const json_t *string);
269json_int_t json_integer_value(const json_t *integer);
270double json_real_value(const json_t *real);
271double json_number_value(const json_t *json);
272
273int json_string_set(json_t *string, const char *value);
274int json_string_setn(json_t *string, const char *value, size_t len);
275int json_string_set_nocheck(json_t *string, const char *value);
276int json_string_setn_nocheck(json_t *string, const char *value, size_t len);
277int json_integer_set(json_t *integer, json_int_t value);
278int json_real_set(json_t *real, double value);
279
280/* pack, unpack */
281
282json_t *json_pack(const char *fmt, ...);
283json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...);
284json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap);
285
286#define JSON_VALIDATE_ONLY 0x1
287#define JSON_STRICT 0x2
288
289int json_unpack(json_t *root, const char *fmt, ...);
290int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...);
291int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap);
292
293/* sprintf */
294
295json_t *json_sprintf(const char *fmt, ...);
296json_t *json_vsprintf(const char *fmt, va_list ap);
297
298
299/* equality */
300
301int json_equal(const json_t *value1, const json_t *value2);
302
303
304/* copying */
305
306json_t *json_copy(json_t *value);
307json_t *json_deep_copy(const json_t *value);
308
309
310/* decoding */
311
312#define JSON_REJECT_DUPLICATES 0x1
313#define JSON_DISABLE_EOF_CHECK 0x2
314#define JSON_DECODE_ANY 0x4
315#define JSON_DECODE_INT_AS_REAL 0x8
316#define JSON_ALLOW_NUL 0x10
317
318typedef size_t (*json_load_callback_t)(void *buffer, size_t buflen, void *data);
319
320json_t *json_loads(const char *input, size_t flags, json_error_t *error);
321json_t *json_loadb(const char *buffer, size_t buflen, size_t flags, json_error_t *error);
322json_t *json_loadf(FILE *input, size_t flags, json_error_t *error);
323json_t *json_loadfd(int input, size_t flags, json_error_t *error);
324json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
325json_t *json_load_callback(json_load_callback_t callback, void *data, size_t flags, json_error_t *error);
326
327
328/* encoding */
329
330#define JSON_MAX_INDENT 0x1F
331#define JSON_INDENT(n) ((n) & JSON_MAX_INDENT)
332#define JSON_COMPACT 0x20
333#define JSON_ENSURE_ASCII 0x40
334#define JSON_SORT_KEYS 0x80
335#define JSON_PRESERVE_ORDER 0x100
336#define JSON_ENCODE_ANY 0x200
337#define JSON_ESCAPE_SLASH 0x400
338#define JSON_REAL_PRECISION(n) (((n) & 0x1F) << 11)
339#define JSON_EMBED 0x10000
340
341typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
342
343char *json_dumps(const json_t *json, size_t flags);
344size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags);
345int json_dumpf(const json_t *json, FILE *output, size_t flags);
346int json_dumpfd(const json_t *json, int output, size_t flags);
347int json_dump_file(const json_t *json, const char *path, size_t flags);
348int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags);
349
350/* custom memory allocation */
351
352typedef void *(*json_malloc_t)(size_t);
353typedef void (*json_free_t)(void *);
354
355void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn);
356void json_get_alloc_funcs(json_malloc_t *malloc_fn, json_free_t *free_fn);
357
358#ifdef __cplusplus
359}
360#endif
361
362#endif
Impressum, Datenschutz