/src/wireshark/wiretap/json.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* json.c |
2 | | * |
3 | | * Copyright 2015, Dario Lombardo <lomato@gmail.com> |
4 | | * |
5 | | * SPDX-License-Identifier: GPL-2.0-or-later |
6 | | */ |
7 | | |
8 | | #include "config.h" |
9 | | #include "json.h" |
10 | | |
11 | | #include <string.h> |
12 | | |
13 | | #include "wtap-int.h" |
14 | | #include "file_wrappers.h" |
15 | | |
16 | | #include <wsutil/wsjson.h> |
17 | | |
18 | | /* Maximum size of json file. */ |
19 | 0 | #define MAX_FILE_SIZE (50*1024*1024) |
20 | | |
21 | | static int json_file_type_subtype = -1; |
22 | | |
23 | | void register_json(void); |
24 | | |
25 | | wtap_open_return_val json_open(wtap *wth, int *err, char **err_info) |
26 | 0 | { |
27 | 0 | uint8_t* filebuf; |
28 | 0 | int bytes_read; |
29 | | |
30 | | /* XXX checking the full file contents might be a bit expensive, maybe |
31 | | * resort to simpler heuristics like '{' or '[' (with some other chars)? */ |
32 | 0 | filebuf = (uint8_t*)g_malloc0(MAX_FILE_SIZE); |
33 | 0 | if (!filebuf) |
34 | 0 | return WTAP_OPEN_ERROR; |
35 | | |
36 | 0 | bytes_read = file_read(filebuf, MAX_FILE_SIZE, wth->fh); |
37 | 0 | if (bytes_read < 0) { |
38 | | /* Read error. */ |
39 | 0 | *err = file_error(wth->fh, err_info); |
40 | 0 | g_free(filebuf); |
41 | 0 | return WTAP_OPEN_ERROR; |
42 | 0 | } |
43 | 0 | if (bytes_read == 0) { |
44 | | /* empty file, not *anybody's* */ |
45 | 0 | g_free(filebuf); |
46 | 0 | return WTAP_OPEN_NOT_MINE; |
47 | 0 | } |
48 | | |
49 | | /* We could reduce the maximum size to read and accept if the parser |
50 | | * returns JSMN_ERROR_PART (i.e., only fail on JSMN_ERROR_INVAL as we |
51 | | * shouldn't get JSMN_ERROR_NOMEM if tokens is NULL.) That way we |
52 | | * could handle bigger files without testing the entire file. |
53 | | * packet-json shows excess unparsed data at the end with the |
54 | | * data-text-lines dissector. |
55 | | */ |
56 | 0 | if (json_parse_len(filebuf, bytes_read, NULL, 0) < 0) { |
57 | 0 | g_free(filebuf); |
58 | 0 | return WTAP_OPEN_NOT_MINE; |
59 | 0 | } |
60 | | |
61 | 0 | if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) { |
62 | 0 | g_free(filebuf); |
63 | 0 | return WTAP_OPEN_ERROR; |
64 | 0 | } |
65 | | |
66 | 0 | wth->file_type_subtype = json_file_type_subtype; |
67 | 0 | wth->file_encap = WTAP_ENCAP_JSON; |
68 | 0 | wth->file_tsprec = WTAP_TSPREC_SEC; |
69 | 0 | wth->subtype_read = wtap_full_file_read; |
70 | 0 | wth->subtype_seek_read = wtap_full_file_seek_read; |
71 | 0 | wth->snapshot_length = 0; |
72 | |
|
73 | 0 | g_free(filebuf); |
74 | 0 | return WTAP_OPEN_MINE; |
75 | 0 | } |
76 | | |
77 | | static const struct supported_block_type json_blocks_supported[] = { |
78 | | /* |
79 | | * This is a file format that we dissect, so we provide only one |
80 | | * "packet" with the file's contents, and don't support any |
81 | | * options. |
82 | | */ |
83 | | { WTAP_BLOCK_PACKET, ONE_BLOCK_SUPPORTED, NO_OPTIONS_SUPPORTED } |
84 | | }; |
85 | | |
86 | | static const struct file_type_subtype_info json_info = { |
87 | | "JavaScript Object Notation", "json", "json", NULL, |
88 | | false, BLOCKS_SUPPORTED(json_blocks_supported), |
89 | | NULL, NULL, NULL |
90 | | }; |
91 | | |
92 | | void register_json(void) |
93 | 8 | { |
94 | 8 | json_file_type_subtype = wtap_register_file_type_subtype(&json_info); |
95 | | |
96 | | /* |
97 | | * Register name for backwards compatibility with the |
98 | | * wtap_filetypes table in Lua. |
99 | | */ |
100 | 8 | wtap_register_backwards_compatibility_lua_name("JSON", |
101 | 8 | json_file_type_subtype); |
102 | 8 | } |
103 | | |
104 | | /* |
105 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
106 | | * |
107 | | * Local variables: |
108 | | * c-basic-offset: 4 |
109 | | * tab-width: 8 |
110 | | * indent-tabs-mode: nil |
111 | | * End: |
112 | | * |
113 | | * vi: set shiftwidth=4 tabstop=8 expandtab: |
114 | | * :indentSize=4:tabSize=8:noTabs=true: |
115 | | */ |