3 * Copyright 2015, Dario Lombardo <lomato@gmail.com>
5 * SPDX-License-Identifier: GPL-2.0-or-later
13 #include "file_wrappers.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
)
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
);
34 return WTAP_OPEN_ERROR
;
36 bytes_read
= file_read(filebuf
, MAX_FILE_SIZE
, wth
->fh
);
39 *err
= file_error(wth
->fh
, err_info
);
41 return WTAP_OPEN_ERROR
;
43 if (bytes_read
== 0) {
44 /* empty file, not *anybody's* */
46 return WTAP_OPEN_NOT_MINE
;
49 if (json_validate(filebuf
, bytes_read
) == false) {
51 return WTAP_OPEN_NOT_MINE
;
54 if (file_seek(wth
->fh
, 0, SEEK_SET
, err
) == -1) {
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;
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
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
),
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
103 * indent-tabs-mode: nil
106 * vi: set shiftwidth=4 tabstop=8 expandtab:
107 * :indentSize=4:tabSize=8:noTabs=true: