]> gitweb.ps.run Git - matrix_esp_thesis/blob - ext/mjson/src/mjson.h
remove newline
[matrix_esp_thesis] / ext / mjson / src / mjson.h
1 // Copyright (c) 2018-2020 Cesanta Software Limited
2 // All rights reserved
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 // SOFTWARE.
21
22 #ifndef MJSON_H
23 #define MJSON_H
24
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #ifndef MJSON_ENABLE_PRINT
30 #define MJSON_ENABLE_PRINT 1
31 #endif
32
33 #ifndef MJSON_ENABLE_RPC
34 #define MJSON_ENABLE_RPC 1
35 #endif
36
37 #ifndef MJSON_ENABLE_BASE64
38 #define MJSON_ENABLE_BASE64 1
39 #endif
40
41 #ifndef MJSON_ENABLE_MERGE
42 #define MJSON_ENABLE_MERGE 1
43 #endif
44
45 #ifndef MJSON_ENABLE_PRETTY
46 #define MJSON_ENABLE_PRETTY 1
47 #endif
48
49 #ifndef MJSON_ENABLE_NEXT
50 #define MJSON_ENABLE_NEXT 1
51 #endif
52
53 #ifndef MJSON_RPC_LIST_NAME
54 #define MJSON_RPC_LIST_NAME "rpc.list"
55 #endif
56
57 #ifndef MJSON_DYNBUF_CHUNK
58 #define MJSON_DYNBUF_CHUNK 256  // Allocation granularity for print_dynamic_buf
59 #endif
60
61 #ifndef MJSON_REALLOC
62 #define MJSON_REALLOC realloc
63 #endif
64
65 #ifdef __cplusplus
66 extern "C" {
67 #endif
68
69 #define MJSON_ERROR_INVALID_INPUT (-1)
70 #define MJSON_ERROR_TOO_DEEP (-2)
71 #define MJSON_TOK_INVALID 0
72 #define MJSON_TOK_KEY 1
73 #define MJSON_TOK_STRING 11
74 #define MJSON_TOK_NUMBER 12
75 #define MJSON_TOK_TRUE 13
76 #define MJSON_TOK_FALSE 14
77 #define MJSON_TOK_NULL 15
78 #define MJSON_TOK_ARRAY 91
79 #define MJSON_TOK_OBJECT 123
80 #define MJSON_TOK_IS_VALUE(t) ((t) > 10 && (t) < 20)
81
82 typedef int (*mjson_cb_t)(int event, const char *buf, int offset, int len,
83                           void *fn_data);
84
85 #ifndef MJSON_MAX_DEPTH
86 #define MJSON_MAX_DEPTH 20
87 #endif
88
89 int mjson(const char *buf, int len, mjson_cb_t cb, void *ud);
90 int mjson_find(const char *buf, int len, const char *jp, const char **tp,
91                int *tl);
92 int mjson_get_number(const char *buf, int len, const char *path, double *v);
93 int mjson_get_bool(const char *buf, int len, const char *path, int *v);
94 int mjson_get_string(const char *buf, int len, const char *path, char *to,
95                      int n);
96 int mjson_get_hex(const char *buf, int len, const char *path, char *to, int n);
97
98 #if MJSON_ENABLE_NEXT
99 int mjson_next(const char *buf, int len, int offset, int *key_offset,
100                int *key_len, int *val_offset, int *val_len, int *vale_type);
101 #endif
102
103 #if MJSON_ENABLE_BASE64
104 int mjson_get_base64(const char *buf, int len, const char *path, char *dst,
105                      int dst_len);
106 int mjson_base64_dec(const char *src, int src_len, char *dst, int dst_len);
107 #endif
108
109 #if MJSON_ENABLE_PRINT
110 typedef int (*mjson_print_fn_t)(const char *buf, int len, void *fn_data);
111 typedef int (*mjson_vprint_fn_t)(mjson_print_fn_t fn, void *, va_list *);
112
113 struct mjson_fixedbuf {
114   char *ptr;
115   int size, len;
116 };
117
118 int mjson_printf(mjson_print_fn_t fn, void *fn_data, const char *fmt, ...);
119 int mjson_vprintf(mjson_print_fn_t fn, void *fn_data, const char *fmt,
120                   va_list *ap);
121 int mjson_print_str(mjson_print_fn_t fn, void *fn_data, const char *buf,
122                     int len);
123 int mjson_print_int(mjson_print_fn_t fn, void *fn_data, int value,
124                     int is_signed);
125 int mjson_print_long(mjson_print_fn_t fn, void *fn_data, long value,
126                      int is_signed);
127 int mjson_print_buf(mjson_print_fn_t fn, void *fn_data, const char *buf,
128                     int len);
129 int mjson_print_dbl(mjson_print_fn_t fn, void *fn_data, double d, int width);
130
131 int mjson_print_null(const char *ptr, int len, void *fn_data);
132 int mjson_print_fixed_buf(const char *ptr, int len, void *fn_data);
133 int mjson_print_dynamic_buf(const char *ptr, int len, void *fn_data);
134
135 int mjson_snprintf(char *buf, size_t len, const char *fmt, ...);
136 char *mjson_aprintf(const char *fmt, ...);
137
138 #if MJSON_ENABLE_PRETTY
139 int mjson_pretty(const char *s, int n, const char *pad, mjson_print_fn_t fn,
140                  void *fn_data);
141 #endif
142
143 #if MJSON_ENABLE_MERGE
144 int mjson_merge(const char *s, int n, const char *s2, int n2,
145                 mjson_print_fn_t fn, void *fn_data);
146 #endif
147
148 #endif  // MJSON_ENABLE_PRINT
149
150 #if MJSON_ENABLE_RPC
151
152 void jsonrpc_init(mjson_print_fn_t response_cb, void *fn_data);
153 int mjson_globmatch(const char *s1, int n1, const char *s2, int n2);
154
155 struct jsonrpc_request {
156   struct jsonrpc_ctx *ctx;
157   const char *frame;    // Points to the whole frame
158   int frame_len;        // Frame length
159   const char *params;   // Points to the "params" in the request frame
160   int params_len;       // Length of the "params"
161   const char *id;       // Points to the "id" in the request frame
162   int id_len;           // Length of the "id"
163   const char *method;   // Points to the "method" in the request frame
164   int method_len;       // Length of the "method"
165   mjson_print_fn_t fn;  // Printer function
166   void *fn_data;        // Printer function data
167   void *userdata;       // Callback's user data as specified at export time
168 };
169
170 struct jsonrpc_method {
171   const char *method;
172   int method_sz;
173   void (*cb)(struct jsonrpc_request *);
174   struct jsonrpc_method *next;
175 };
176
177 // Main RPC context, stores current request information and a list of
178 // exported RPC methods.
179 struct jsonrpc_ctx {
180   struct jsonrpc_method *methods;
181   mjson_print_fn_t response_cb;
182   void *response_cb_data;
183 };
184
185 // Registers function fn under the given name within the given RPC context
186 #define jsonrpc_ctx_export(ctx, name, fn)                                 \
187   do {                                                                    \
188     static struct jsonrpc_method m = {(name), sizeof(name) - 1, (fn), 0}; \
189     m.next = (ctx)->methods;                                              \
190     (ctx)->methods = &m;                                                  \
191   } while (0)
192
193 void jsonrpc_ctx_init(struct jsonrpc_ctx *ctx, mjson_print_fn_t response_cb,
194                       void *response_cb_data);
195 void jsonrpc_return_error(struct jsonrpc_request *r, int code,
196                           const char *message, const char *data_fmt, ...);
197 void jsonrpc_return_success(struct jsonrpc_request *r, const char *result_fmt,
198                             ...);
199 void jsonrpc_ctx_process(struct jsonrpc_ctx *ctx, const char *req, int req_sz,
200                          mjson_print_fn_t fn, void *fn_data, void *userdata);
201
202 extern struct jsonrpc_ctx jsonrpc_default_context;
203 extern void jsonrpc_list(struct jsonrpc_request *r);
204
205 #define jsonrpc_export(name, fn) \
206   jsonrpc_ctx_export(&jsonrpc_default_context, (name), (fn))
207
208 #define jsonrpc_process(buf, len, fn, fnd, ud) \
209   jsonrpc_ctx_process(&jsonrpc_default_context, (buf), (len), (fn), (fnd), (ud))
210
211 #define JSONRPC_ERROR_INVALID -32700    /* Invalid JSON was received */
212 #define JSONRPC_ERROR_NOT_FOUND -32601  /* The method does not exist */
213 #define JSONRPC_ERROR_BAD_PARAMS -32602 /* Invalid params passed */
214 #define JSONRPC_ERROR_INTERNAL -32603   /* Internal JSON-RPC error */
215
216 #endif  // MJSON_ENABLE_RPC
217 #ifdef __cplusplus
218 }
219 #endif
220 #endif  // MJSON_H