2015-01-19 18:54:41 +00:00
|
|
|
/* json.c
|
|
|
|
*
|
|
|
|
* Copyright 2015, Dario Lombardo <lomato@gmail.com>
|
|
|
|
*
|
2018-02-07 11:26:45 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2015-01-19 18:54:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "wtap-int.h"
|
|
|
|
#include "file_wrappers.h"
|
|
|
|
|
2016-04-03 17:29:26 +00:00
|
|
|
#include "json.h"
|
2018-04-20 14:55:00 +00:00
|
|
|
#include <wsutil/wsjson.h>
|
2015-01-19 18:54:41 +00:00
|
|
|
|
2018-11-10 22:03:16 +00:00
|
|
|
/* Maximum size of json file. */
|
|
|
|
#define MAX_FILE_SIZE (50*1024*1024)
|
2015-01-19 18:54:41 +00:00
|
|
|
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
static int json_file_type_subtype = -1;
|
|
|
|
|
|
|
|
void register_json(void);
|
|
|
|
|
2015-11-07 22:46:18 +00:00
|
|
|
wtap_open_return_val json_open(wtap *wth, int *err, gchar **err_info)
|
2015-01-19 18:54:41 +00:00
|
|
|
{
|
|
|
|
guint8* filebuf;
|
2015-11-07 22:46:18 +00:00
|
|
|
int bytes_read;
|
2015-01-19 18:54:41 +00:00
|
|
|
|
2018-11-10 22:03:16 +00:00
|
|
|
/* XXX checking the full file contents might be a bit expensive, maybe
|
|
|
|
* resort to simpler heuristics like '{' or '[' (with some other chars)? */
|
2015-01-19 18:54:41 +00:00
|
|
|
filebuf = (guint8*)g_malloc0(MAX_FILE_SIZE);
|
|
|
|
if (!filebuf)
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
|
2015-11-07 22:46:18 +00:00
|
|
|
bytes_read = file_read(filebuf, MAX_FILE_SIZE, wth->fh);
|
|
|
|
if (bytes_read < 0) {
|
|
|
|
/* Read error. */
|
|
|
|
*err = file_error(wth->fh, err_info);
|
|
|
|
g_free(filebuf);
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
if (bytes_read == 0) {
|
|
|
|
/* empty file, not *anybody's* */
|
|
|
|
g_free(filebuf);
|
|
|
|
return WTAP_OPEN_NOT_MINE;
|
|
|
|
}
|
2015-01-19 18:54:41 +00:00
|
|
|
|
2018-11-19 14:52:30 +00:00
|
|
|
if (json_validate(filebuf, bytes_read) == FALSE) {
|
2015-01-19 18:54:41 +00:00
|
|
|
g_free(filebuf);
|
|
|
|
return WTAP_OPEN_NOT_MINE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
|
|
|
|
g_free(filebuf);
|
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
wth->file_type_subtype = json_file_type_subtype;
|
2015-01-19 18:54:41 +00:00
|
|
|
wth->file_encap = WTAP_ENCAP_JSON;
|
|
|
|
wth->file_tsprec = WTAP_TSPREC_SEC;
|
2018-11-10 22:03:16 +00:00
|
|
|
wth->subtype_read = wtap_full_file_read;
|
|
|
|
wth->subtype_seek_read = wtap_full_file_seek_read;
|
2015-01-19 18:54:41 +00:00
|
|
|
wth->snapshot_length = 0;
|
|
|
|
|
|
|
|
g_free(filebuf);
|
|
|
|
return WTAP_OPEN_MINE;
|
|
|
|
}
|
|
|
|
|
wiretap: register most built-in file types from its module.
Remove most of the built-in file types from the table in
wiretap/file_access.c and, instead, have the file types register
themselves, using wtap_register_file_type_subtypes().
This reduces the source code changes needed to add a new file type from
three (add the handler, add the file type to the table in file_access.c,
add a #define for the file type in wiretap/wtap.h) to one (add the
handler). (It also requires adding the handler's source file to
wiretap/CMakeLists.txt, but that's required in both cases.)
A few remain because the WTAP_FILE_TYPE_SUBTYPE_ #define is used
elsewhere; that needs to be fixed.
Fix the wiretap/CMakefile.txt file to scan k12text.l, as that now
contains a registration routine. In the process, avoid scanning files
that don't implement a file type and won't ever have a registration
routine.
Add a Lua routine to fetch the total number of file types; we use that
in some code to construct the wtap_filetypes table, which we need to do
in order to continue to have all the values that used to come from the
WTAP_FILE_TYPE_SUBTYPE_ types.
While we're at it, add modelines to a file that lacked them.
2021-02-14 08:34:10 +00:00
|
|
|
static const struct file_type_subtype_info json_info = {
|
|
|
|
/* WTAP_FILE_TYPE_SUBTYPE_JSON */
|
|
|
|
"JavaScript Object Notation", "json", "json", NULL,
|
|
|
|
FALSE, FALSE, 0,
|
|
|
|
NULL, NULL, NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
void register_json(void)
|
|
|
|
{
|
|
|
|
json_file_type_subtype = wtap_register_file_type_subtypes(&json_info,
|
|
|
|
WTAP_FILE_TYPE_SUBTYPE_UNKNOWN);
|
|
|
|
}
|
|
|
|
|
2015-01-19 18:54:41 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2015-01-19 18:54:41 +00:00
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
2016-02-25 21:34:07 +00:00
|
|
|
* tab-width: 8
|
2015-01-19 18:54:41 +00:00
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 expandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=true:
|
|
|
|
*/
|