-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathjson2msgpack.cpp
More file actions
420 lines (369 loc) · 15 KB
/
Copy pathjson2msgpack.cpp
File metadata and controls
420 lines (369 loc) · 15 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* The MIT License (MIT)
*
* Copyright (c) 2015-2017 Nicholas Fraser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "common.h"
#include <ctype.h>
#include <errno.h>
using namespace rapidjson;
typedef struct options_t {
const char* command;
const char* out_filename;
const char* in_filename;
bool lax;
bool use_float;
bool base64_prefix;
size_t base64_min_bytes;
} options_t;
static const char* prefix_ext = "ext:";
static const char* prefix_base64 = "base64:";
// libb64 doesn't have the means to return errors on invalid characters, so
// for base64 detection, we manually scan the string for invalid characters.
// For detection we allow CR/LF but we don't allow spaces (otherwise pretty
// much any normal string would be detected as base64.)
static bool is_base64(const char* string, size_t length) {
for (size_t i = 0; i < length; ++i) {
char c = string[i];
if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
|| c == '+' || c == '/' || c == '=' || c == '\r' || c == '\n')) {
return false;
}
}
return true;
}
static char* convert_base64(options_t* options, const char* p, size_t len, size_t* out_bytes) {
size_t bytes = len * 3 / 4 + 4; // TODO check this
char* data = (char*)malloc(bytes);
if (!data) {
fprintf(stderr, "%s: allocation failure\n", options->command);
return NULL;
}
base64_decodestate state;
base64_init_decodestate(&state);
bytes = base64_decode_block(p, len, data, &state);
*out_bytes = bytes;
return data;
}
static bool write_string(options_t* options, mpack_writer_t* writer, const char* string, size_t length, bool allow_detection) {
if (options->base64_prefix) {
// check for base64 prefix
if (length >= strlen(prefix_base64) && memcmp(string, prefix_base64, strlen(prefix_base64)) == 0) {
const char* base64_data = string + strlen(prefix_base64);
size_t base64_len = length - strlen(prefix_base64);
if (!is_base64(base64_data, base64_len)) {
fprintf(stderr, "%s: string prefixed with \"base64:\" contains invalid base64\n", options->command);
return false;
}
// write base64
size_t count;
char* bytes = convert_base64(options, base64_data, base64_len, &count);
if (bytes) {
mpack_write_bin(writer, bytes, count);
free(bytes);
return mpack_writer_error(writer) == mpack_ok;
}
return false;
}
// check for ext prefix
if (length >= strlen(prefix_ext) && memcmp(string, prefix_ext, strlen(prefix_ext)) == 0) {
// parse exttype
const char* exttype_str = string + strlen(prefix_ext);
char* remainder;
errno = 0;
int64_t exttype = strtol(exttype_str, &remainder, 10);
if (errno != 0 || *(remainder++) != ':' || strlen(remainder) < strlen(prefix_base64)
|| memcmp(remainder, prefix_base64, strlen(prefix_base64)) != 0) {
fprintf(stderr, "\"%s\"\n", remainder);
fprintf(stderr, "%s: string prefixed with \"ext:\" contains invalid prefix\n", options->command);
return false;
}
if (exttype < INT8_MIN || exttype > INT8_MAX) {
fprintf(stderr, "%s: string prefixed with \"ext:\" has out-of-bounds ext type: %" PRIi64 "\n", options->command, exttype);
return false;
}
// check base64
const char* base64_data = remainder + strlen(prefix_base64);
size_t base64_len = strlen(base64_data);
if (!is_base64(base64_data, base64_len)) {
fprintf(stderr, "\"%s\"\n", base64_data);
fprintf(stderr, "%s: string prefixed with \"ext:\" contains invalid base64\n", options->command);
return false;
}
// write ext
size_t count;
char* bytes = convert_base64(options, base64_data, base64_len, &count);
if (bytes) {
mpack_write_ext(writer, (int8_t)exttype, bytes, count);
free(bytes);
return mpack_writer_error(writer) == mpack_ok;
}
return false;
}
}
// try to parse as base64
if (allow_detection && options->base64_min_bytes != 0 && length >= options->base64_min_bytes && is_base64(string, length)) {
size_t count;
char* bytes = convert_base64(options, string, length, &count);
if (bytes) {
mpack_write_bin(writer, bytes, count);
free(bytes);
return mpack_writer_error(writer) == mpack_ok;
}
return false;
}
mpack_write_str(writer, string, length);
return mpack_writer_error(writer) == mpack_ok;
}
static bool write_value(options_t* options, Value& value, mpack_writer_t* writer) {
switch (value.GetType()) {
case kNullType: mpack_write_nil(writer); break;
case kTrueType: mpack_write_true(writer); break;
case kFalseType: mpack_write_false(writer); break;
case kNumberType:
if (value.IsDouble()) {
if (options->use_float)
mpack_write_float(writer, (float)value.GetDouble());
else
mpack_write_double(writer, value.GetDouble());
} else if (value.IsUint64()) {
mpack_write_u64(writer, value.GetUint64());
} else {
mpack_write_i64(writer, value.GetInt64());
}
break;
case kStringType:
if (!write_string(options, writer, value.GetString(), value.GetStringLength(), true))
return false;
break;
case kArrayType: {
mpack_start_array(writer, value.Size());
Value::ValueIterator it = value.Begin(), end = value.End();
for (; it != end; ++it) {
if (!write_value(options, *it, writer))
return false;
}
mpack_finish_array(writer);
break;
}
case kObjectType: {
mpack_start_map(writer, value.MemberCount());
Value::MemberIterator it = value.MemberBegin(), end = value.MemberEnd();
for (; it != end; ++it) {
if (!write_string(options, writer, it->name.GetString(), it->name.GetStringLength(), false))
return false;
if (!write_value(options, it->value, writer))
return false;
}
mpack_finish_map(writer);
break;
}
default:
return false;
}
return mpack_writer_error(writer) == mpack_ok;
}
static bool load_file(options_t* options, char** out_data, size_t* out_size) {
FILE* in_file;
if (options->in_filename) {
in_file = fopen(options->in_filename, "rb");
if (in_file == NULL) {
fprintf(stderr, "%s: could not open \"%s\" for reading.\n", options->command, options->in_filename);
return false;
}
} else {
in_file = stdin;
}
size_t capacity = 4096;
size_t size = 0;
char* data = (char*)malloc(capacity);
while (1) {
size_t n = fread(data + size, 1, capacity - size, in_file);
// RapidJSON in-situ requires a null-terminated string, so we need to scan the
// data to make sure it has no null bytes. They are not legal JSON anyway.
for (size_t i = size; i < size + n; ++i) {
if (data[i] == '\0') {
fprintf(stderr, "%s: JSON cannot contain null bytes\n", options->command);
if (in_file != stdin)
fclose(in_file);
free(data);
return false;
}
}
size += n;
if (ferror(in_file)) {
fprintf(stderr, "%s: error reading data\n", options->command);
if (in_file != stdin)
fclose(in_file);
free(data);
return false;
}
// We always need enough space to store the null-terminator
if (size == capacity) {
capacity *= 2;
data = (char*)realloc(data, capacity);
}
if (feof(in_file))
break;
// This shouldn't happen; no bytes should mean error or EOF. We
// check and throw an error anyway to avoid an infinite loop.
if (n == 0) {
fprintf(stderr, "%s: error reading data\n", options->command);
if (in_file != stdin)
fclose(in_file);
free(data);
return false;
}
}
data[size] = '\0';
fclose(in_file);
*out_data = data;
*out_size = size;
return true;
}
static bool convert(options_t* options) {
char* data = NULL;
size_t size = 0;
if (!load_file(options, &data, &size))
return false;
// The data has been null-terminated by load_file()
StringStream stream(data);
mpack_writer_t writer;
if (options->out_filename != NULL)
mpack_writer_init_file(&writer, options->out_filename);
else
mpack_writer_init_stdfile(&writer, stdout, true);
while (stream.Peek() != '\0') {
// skip space characters
while (isspace(stream.Peek()))
stream.Take();
if (stream.Peek() == '\0')
break;
Document document;
if (options->lax)
document.ParseStream<kParseStopWhenDoneFlag | kParseFullPrecisionFlag | kParseCommentsFlag | kParseTrailingCommasFlag>(stream);
else
document.ParseStream<kParseStopWhenDoneFlag | kParseFullPrecisionFlag>(stream);
if (document.HasParseError()) {
fprintf(stderr, "%s: error parsing JSON at offset %i:\n %s\n", options->command,
(int)document.GetErrorOffset(), GetParseError_En(document.GetParseError()));
mpack_writer_destroy(&writer);
free(data);
return false;
}
write_value(options, document, &writer);
}
mpack_error_t error = mpack_writer_destroy(&writer);
if (error != mpack_ok) {
fprintf(stderr, "%s: error writing MessagePack: %s (%i)\n", options->command,
mpack_error_to_string(error), (int)error);
free(data);
return false;
}
free(data);
return true;
}
static void parse_min_bytes(options_t* options) {
const char* arg = optarg;
char* end;
errno = 0;
int64_t value = strtol(arg, &end, 10);
if (errno != 0 || *end != '\0' || value <= 0) {
fprintf(stderr, "%s: -B requires a positive integer, not \"%s\"\n", options->command, arg);
exit(EXIT_FAILURE);
}
if (SIZE_MAX < INT64_MAX && value > (int64_t)SIZE_MAX) {
fprintf(stderr, "%s: -B argument is out of bounds: %" PRIi64 "\n", options->command, value);
exit(EXIT_FAILURE);
}
options->base64_min_bytes = (size_t)value;
}
static void usage(const char* command) {
fprintf(stderr, "Usage: %s [-i <infile>] [-o <outfile>] [-lfb] [-B <min>]\n", command);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -i <infile> Input filename (default stdin)\n");
fprintf(stderr, " -o <outfile> Output filename (default stdout)\n");
fprintf(stderr, " -l Lax mode, allows comments and trailing commas\n");
fprintf(stderr, " -f Write floats instead of doubles\n");
fprintf(stderr, " -b Convert base64 strings with \"base64:\" prefix to bin\n");
fprintf(stderr, " -B <min> Try to convert any base64 string of at least <min> bytes to bin\n");
fprintf(stderr, " -h Print this help\n");
fprintf(stderr, " -v Print version information\n");
}
static void version(const char* command) {
fprintf(stderr, "%s version %s -- %s\n", command, VERSION, "https://github.com/ludocode/msgpack-tools");
fprintf(stderr, "RapidJSON version %s -- %s\n", RAPIDJSON_VERSION_STRING, "http://rapidjson.org/");
fprintf(stderr, "MPack version %s -- %s\n", MPACK_VERSION_STRING, "https://github.com/ludocode/mpack");
fprintf(stderr, "libb64 version %s -- %s\n", LIBB64_VERSION, "http://libb64.sourceforge.net/");
}
int main(int argc, char** argv) {
options_t options;
memset(&options, 0, sizeof(options));
options.command = argv[0];
opterr = 0;
int opt;
while ((opt = getopt(argc, argv, "i:o:lfbB:hv?")) != -1) {
switch (opt) {
case 'i':
options.in_filename = optarg;
break;
case 'o':
options.out_filename = optarg;
break;
case 'l':
options.lax = true;
break;
case 'f':
options.use_float = true;
break;
case 'b':
options.base64_prefix = true;
break;
case 'B':
parse_min_bytes(&options);
break;
case 'h':
usage(options.command);
return EXIT_SUCCESS;
case 'v':
version(options.command);
return EXIT_SUCCESS;
default: /* ? */
if (optopt == 0) {
// we allow both -h and -? as help
usage(options.command);
return EXIT_SUCCESS;
}
if (optopt == 'i' || optopt == 'o' || optopt == 'B')
fprintf(stderr, "%s: -%c requires an argument\n", options.command, optopt);
else
fprintf(stderr, "%s: invalid option -- '%c'\n", options.command, optopt);
usage(options.command);
return EXIT_FAILURE;
}
}
if (optind < argc) {
fprintf(stderr, "%s: not an option -- \"%s\"\n", options.command, argv[optind]);
usage(options.command);
return EXIT_FAILURE;
}
return convert(&options) ? EXIT_SUCCESS : EXIT_FAILURE;
}