1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
/* json-util.c - Helper funtions for the JSON based interface to gpgme
* Copyright (C) 2018, 2025 g10 Code GmbH
*
* This file is part of GPGME.
*
* GPGME is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* GPGME is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, see <https://gnu.org/licenses/>.
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#include "json-common.h"
void
xoutofcore (const char *type)
{
gpg_error_t err = gpg_error_from_syserror ();
log_error ("%s failed: %s\n", type, gpg_strerror (err));
exit (2);
}
/* Free a NULL terminated array */
void
xfree_array (char **array)
{
if (array)
{
int idx;
for (idx = 0; array[idx]; idx++)
xfree (array[idx]);
xfree (array);
}
}
const char *
data_type_to_string (gpgme_data_type_t dt)
{
const char *s = "[?]";
switch (dt)
{
case GPGME_DATA_TYPE_INVALID : s = "invalid"; break;
case GPGME_DATA_TYPE_UNKNOWN : s = "unknown"; break;
case GPGME_DATA_TYPE_PGP_SIGNED : s = "PGP-signed"; break;
case GPGME_DATA_TYPE_PGP_SIGNATURE: s = "PGP-signature"; break;
case GPGME_DATA_TYPE_PGP_ENCRYPTED: s = "PGP-encrypted"; break;
case GPGME_DATA_TYPE_PGP_OTHER : s = "PGP"; break;
case GPGME_DATA_TYPE_PGP_KEY : s = "PGP-key"; break;
case GPGME_DATA_TYPE_CMS_SIGNED : s = "CMS-signed"; break;
case GPGME_DATA_TYPE_CMS_ENCRYPTED: s = "CMS-encrypted"; break;
case GPGME_DATA_TYPE_CMS_OTHER : s = "CMS"; break;
case GPGME_DATA_TYPE_X509_CERT : s = "X.509"; break;
case GPGME_DATA_TYPE_PKCS12 : s = "PKCS12"; break;
}
return s;
}
/* Create a JSON error object. If JSON is not NULL the error message
* is appended to that object. An existing "type" item will be replaced. */
static cjson_t
error_object_v (cjson_t json, const char *message, va_list arg_ptr,
gpg_error_t err)
{
cjson_t response, j_tmp;
char *msg;
msg = gpgrt_vbsprintf (message, arg_ptr);
if (!msg)
xoutofcore ("error_object");
response = json? json : xjson_CreateObject ();
if (!(j_tmp = cJSON_GetObjectItem (response, "type")))
xjson_AddStringToObject (response, "type", "error");
else /* Replace existing "type". */
{
j_tmp = cJSON_CreateString ("error");
if (!j_tmp)
xoutofcore ("cJSON_CreateString");
cJSON_ReplaceItemInObject (response, "type", j_tmp);
}
xjson_AddStringToObject (response, "msg", msg);
xfree (msg);
xjson_AddNumberToObject (response, "code", err);
return response;
}
/* Call cJSON_Print but terminate in case of an error. */
char *
xjson_Print (cjson_t object)
{
char *buf;
buf = cJSON_Print (object);
if (!buf)
xoutofcore ("cJSON_Print");
return buf;
}
/* Call cJSON_CreateObject but terminate in case of an error. */
cjson_t
xjson_CreateObject (void)
{
cjson_t json = cJSON_CreateObject ();
if (!json)
xoutofcore ("cJSON_CreateObject");
return json;
}
/* Call cJSON_CreateArray but terminate in case of an error. */
cjson_t
xjson_CreateArray (void)
{
cjson_t json = cJSON_CreateArray ();
if (!json)
xoutofcore ("cJSON_CreateArray");
return json;
}
/* Wrapper around cJSON_AddStringToObject which returns an gpg-error
* code instead of the NULL or the new object. */
gpg_error_t
cjson_AddStringToObject (cjson_t object, const char *name, const char *string)
{
if (!cJSON_AddStringToObject (object, name, string))
return gpg_error_from_syserror ();
return 0;
}
/* Same as cjson_AddStringToObject but prints an error message and
* terminates the process. */
void
xjson_AddStringToObject (cjson_t object, const char *name, const char *string)
{
if (!cJSON_AddStringToObject (object, name, string))
xoutofcore ("cJSON_AddStringToObject");
}
/* Same as xjson_AddStringToObject but ignores NULL strings */
void
xjson_AddStringToObject0 (cjson_t object, const char *name, const char *string)
{
if (!string)
return;
xjson_AddStringToObject (object, name, string);
}
/* Wrapper around cJSON_AddBoolToObject which terminates the process
* in case of an error. */
void
xjson_AddBoolToObject (cjson_t object, const char *name, int abool)
{
if (!cJSON_AddBoolToObject (object, name, abool))
xoutofcore ("cJSON_AddStringToObject");
return ;
}
/* Wrapper around cJSON_AddNumberToObject which terminates the process
* in case of an error. */
void
xjson_AddNumberToObject (cjson_t object, const char *name, double dbl)
{
if (!cJSON_AddNumberToObject (object, name, dbl))
xoutofcore ("cJSON_AddNumberToObject");
return ;
}
/* Wrapper around cJSON_AddItemToObject which terminates the process
* in case of an error. */
void
xjson_AddItemToObject (cjson_t object, const char *name, cjson_t item)
{
if (!cJSON_AddItemToObject (object, name, item))
xoutofcore ("cJSON_AddItemToObject");
return ;
}
cjson_t
error_object (cjson_t json, const char *message, ...)
{
cjson_t response;
va_list arg_ptr;
va_start (arg_ptr, message);
response = error_object_v (json, message, arg_ptr, 0);
va_end (arg_ptr);
return response;
}
cjson_t
gpg_error_object (cjson_t json, gpg_error_t err, const char *message, ...)
{
cjson_t response;
va_list arg_ptr;
va_start (arg_ptr, message);
response = error_object_v (json, message, arg_ptr, err);
va_end (arg_ptr);
return response;
}
char *
error_object_string (const char *message, ...)
{
cjson_t response;
va_list arg_ptr;
char *msg;
va_start (arg_ptr, message);
response = error_object_v (NULL, message, arg_ptr, 0);
va_end (arg_ptr);
msg = xjson_Print (response);
cJSON_Delete (response);
return msg;
}
|