kerberos: remember account details from dissect_krb5_PAC_UPN_DNS_INFO on EncTicketPar...
[wireshark-sm.git] / wiretap / json.c
blob47fd028c763d2c1171bef01ed574488584be1115
1 /* json.c
3 * Copyright 2015, Dario Lombardo <lomato@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
8 #include "config.h"
10 #include <string.h>
12 #include "wtap-int.h"
13 #include "file_wrappers.h"
15 #include "json.h"
16 #include <wsutil/wsjson.h>
18 /* Maximum size of json file. */
19 #define MAX_FILE_SIZE (50*1024*1024)
21 static int json_file_type_subtype = -1;
23 void register_json(void);
25 wtap_open_return_val json_open(wtap *wth, int *err, char **err_info)
27 uint8_t* filebuf;
28 int bytes_read;
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 filebuf = (uint8_t*)g_malloc0(MAX_FILE_SIZE);
33 if (!filebuf)
34 return WTAP_OPEN_ERROR;
36 bytes_read = file_read(filebuf, MAX_FILE_SIZE, wth->fh);
37 if (bytes_read < 0) {
38 /* Read error. */
39 *err = file_error(wth->fh, err_info);
40 g_free(filebuf);
41 return WTAP_OPEN_ERROR;
43 if (bytes_read == 0) {
44 /* empty file, not *anybody's* */
45 g_free(filebuf);
46 return WTAP_OPEN_NOT_MINE;
49 if (json_validate(filebuf, bytes_read) == false) {
50 g_free(filebuf);
51 return WTAP_OPEN_NOT_MINE;
54 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
55 g_free(filebuf);
56 return WTAP_OPEN_ERROR;
59 wth->file_type_subtype = json_file_type_subtype;
60 wth->file_encap = WTAP_ENCAP_JSON;
61 wth->file_tsprec = WTAP_TSPREC_SEC;
62 wth->subtype_read = wtap_full_file_read;
63 wth->subtype_seek_read = wtap_full_file_seek_read;
64 wth->snapshot_length = 0;
66 g_free(filebuf);
67 return WTAP_OPEN_MINE;
70 static const struct supported_block_type json_blocks_supported[] = {
72 * This is a file format that we dissect, so we provide only one
73 * "packet" with the file's contents, and don't support any
74 * options.
76 { WTAP_BLOCK_PACKET, ONE_BLOCK_SUPPORTED, NO_OPTIONS_SUPPORTED }
79 static const struct file_type_subtype_info json_info = {
80 "JavaScript Object Notation", "json", "json", NULL,
81 false, BLOCKS_SUPPORTED(json_blocks_supported),
82 NULL, NULL, NULL
85 void register_json(void)
87 json_file_type_subtype = wtap_register_file_type_subtype(&json_info);
90 * Register name for backwards compatibility with the
91 * wtap_filetypes table in Lua.
93 wtap_register_backwards_compatibility_lua_name("JSON",
94 json_file_type_subtype);
98 * Editor modelines - https://www.wireshark.org/tools/modelines.html
100 * Local variables:
101 * c-basic-offset: 4
102 * tab-width: 8
103 * indent-tabs-mode: nil
104 * End:
106 * vi: set shiftwidth=4 tabstop=8 expandtab:
107 * :indentSize=4:tabSize=8:noTabs=true: