1 /*
2  * ProFTPD - FTP server daemon
3  * Copyright (c) 2017 The ProFTPD Project team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
18  *
19  * As a special exemption, The ProFTPD Project team and other respective
20  * copyright holders give permission to link this program with OpenSSL, and
21  * distribute the resulting executable, without including the source code for
22  * OpenSSL in the source distribution.
23  */
24 
25 /* JSON API */
26 
27 #ifndef PR_JSON_H
28 #define PR_JSON_H
29 
30 #include "conf.h"
31 
32 typedef struct json_list_st pr_json_array_t;
33 typedef struct json_obj_st pr_json_object_t;
34 
35 /* JSON Types */
36 
37 #define PR_JSON_TYPE_BOOL		1
38 #define PR_JSON_TYPE_NUMBER		2
39 #define PR_JSON_TYPE_NULL		3
40 #define PR_JSON_TYPE_STRING		4
41 #define PR_JSON_TYPE_ARRAY		5
42 #define PR_JSON_TYPE_OBJECT		6
43 
44 /* JSON Objects */
45 
46 pr_json_object_t *pr_json_object_alloc(pool *p);
47 
48 int pr_json_object_free(pr_json_object_t *json);
49 
50 pr_json_object_t *pr_json_object_from_text(pool *p, const char *text);
51 
52 char *pr_json_object_to_text(pool *p, const pr_json_object_t *json,
53   const char *indent);
54 
55 /* Returns the number of members (keys) in the given object. */
56 int pr_json_object_count(const pr_json_object_t *json);
57 
58 /* Removes the object member under this key. */
59 int pr_json_object_remove(pr_json_object_t *json, const char *key);
60 
61 /* Checks where a member under the given key exists.  Returns TRUE, FALSE,
62  * or -1 is there was some other error.
63  */
64 int pr_json_object_exists(const pr_json_object_t *json, const char *key);
65 
66 int pr_json_object_get_bool(pool *p, const pr_json_object_t *json,
67   const char *key, int *val);
68 int pr_json_object_set_bool(pool *p, pr_json_object_t *json, const char *key,
69   int val);
70 
71 int pr_json_object_get_null(pool *p, const pr_json_object_t *json,
72   const char *key);
73 int pr_json_object_set_null(pool *p, pr_json_object_t *json, const char *key);
74 
75 int pr_json_object_get_number(pool *p, const pr_json_object_t *json,
76   const char *key, double *val);
77 int pr_json_object_set_number(pool *p, pr_json_object_t *json, const char *key,
78   double val);
79 
80 int pr_json_object_get_string(pool *p, const pr_json_object_t *json,
81   const char *key, char **val);
82 int pr_json_object_set_string(pool *p, pr_json_object_t *json, const char *key,
83   const char *val);
84 
85 int pr_json_object_get_array(pool *p, const pr_json_object_t *json,
86   const char *key, pr_json_array_t **val);
87 int pr_json_object_set_array(pool *p, pr_json_object_t *json, const char *key,
88   const pr_json_array_t *val);
89 
90 int pr_json_object_get_object(pool *p, const pr_json_object_t *json,
91   const char *key, pr_json_object_t **val);
92 int pr_json_object_set_object(pool *p, pr_json_object_t *json, const char *key,
93   const pr_json_object_t *val);
94 
95 /* JSON Arrays */
96 
97 pr_json_array_t *pr_json_array_alloc(pool *p);
98 
99 int pr_json_array_free(pr_json_array_t *json);
100 
101 pr_json_array_t *pr_json_array_from_text(pool *p, const char *text);
102 
103 char *pr_json_array_to_text(pool *p, const pr_json_array_t *json,
104   const char *indent);
105 
106 /* Returns the number of items in the given array. */
107 int pr_json_array_count(const pr_json_array_t *json);
108 
109 /* Removes the array item under this key. */
110 int pr_json_array_remove(pr_json_array_t *json, unsigned int idx);
111 
112 /* Checks where an item at the given index exists.  Returns TRUE, FALSE,
113  * or -1 is there was some other error.
114  */
115 int pr_json_array_exists(const pr_json_array_t *json, unsigned int idx);
116 
117 int pr_json_array_append_bool(pool *p, pr_json_array_t *json, int val);
118 int pr_json_array_get_bool(pool *p, const pr_json_array_t *json,
119   unsigned int idx, int *val);
120 
121 int pr_json_array_append_null(pool *p, pr_json_array_t *json);
122 int pr_json_array_get_null(pool *p, const pr_json_array_t *json,
123   unsigned int idx);
124 
125 int pr_json_array_append_number(pool *p, pr_json_array_t *json, double val);
126 int pr_json_array_get_number(pool *p, const pr_json_array_t *json,
127   unsigned int idx, double *val);
128 
129 int pr_json_array_append_string(pool *p, pr_json_array_t *json,
130   const char *val);
131 int pr_json_array_get_string(pool *p, const pr_json_array_t *json,
132   unsigned int idx, char **val);
133 
134 int pr_json_array_append_array(pool *p, pr_json_array_t *json,
135   const pr_json_array_t *val);
136 int pr_json_array_get_array(pool *p, const pr_json_array_t *json,
137   unsigned int idx, pr_json_array_t **val);
138 
139 int pr_json_array_append_object(pool *p, pr_json_array_t *json,
140   const pr_json_object_t *val);
141 int pr_json_array_get_object(pool *p, const pr_json_array_t *json,
142   unsigned int idx, pr_json_object_t **val);
143 
144 /* Miscellaneous */
145 
146 /* Validates that the given text is a valid JSON string. */
147 int pr_json_text_validate(pool *p, const char *text);
148 
149 /* Provides textual label of the JSON type. */
150 const char *pr_json_type_name(unsigned int json_type);
151 
152 /* Internal use only. */
153 int init_json(void);
154 int finish_json(void);
155 
156 #endif /* PR_JSON_H */
157