Coverage Report

Created: 2026-05-14 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/wireshark/wiretap/json.c
Line
Count
Source
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_module.h"
14
#include "file_wrappers.h"
15
16
#include <wsutil/wsjson.h>
17
18
/*
19
 * This reads an arbitrary, possibly large JSON object.
20
 * For JSON log data, see json_log.c.
21
 */
22
23
/* Maximum size of json file. */
24
0
#define MAX_FILE_SIZE  (50*1024*1024)
25
26
static int json_file_type_subtype = -1;
27
28
void register_json(void);
29
30
wtap_open_return_val json_open(wtap *wth, int *err, char **err_info)
31
0
{
32
0
    int64_t filesize;
33
0
    uint8_t* filebuf;
34
0
    int bytes_read;
35
36
    /* XXX checking the full file contents might be a bit expensive, maybe
37
     * resort to simpler heuristics like '{' or '[' (with some other chars)? */
38
0
    if ((filesize = wtap_file_size(wth, err)) == -1)
39
0
        return WTAP_OPEN_ERROR;
40
41
0
    if (filesize > MAX_FILE_SIZE) {
42
        /* Avoid allocating space for an immensely-large file. */
43
0
        filesize = MAX_FILE_SIZE;
44
0
    }
45
46
0
    filebuf = (uint8_t*)g_malloc0(filesize);
47
0
    if (!filebuf)
48
0
        return WTAP_OPEN_ERROR;
49
50
0
    bytes_read = file_read(filebuf, (unsigned int) filesize, wth->fh);
51
0
    if (bytes_read < 0) {
52
        /* Read error. */
53
0
        *err = file_error(wth->fh, err_info);
54
0
        g_free(filebuf);
55
0
        return WTAP_OPEN_ERROR;
56
0
    }
57
0
    if (bytes_read == 0) {
58
        /* empty file, not *anybody's* */
59
0
        g_free(filebuf);
60
0
        return WTAP_OPEN_NOT_MINE;
61
0
    }
62
63
    /* We could reduce the maximum size to read and accept if the parser
64
     * returns JSMN_ERROR_PART (i.e., only fail on JSMN_ERROR_INVAL as we
65
     * shouldn't get JSMN_ERROR_NOMEM if tokens is NULL.) That way we
66
     * could handle bigger files without testing the entire file.
67
     * packet-json shows excess unparsed data at the end with the
68
     * data-text-lines dissector.
69
     */
70
0
    int num_tokens = json_parse_len((const char*)filebuf, bytes_read, NULL, 0);
71
0
    if (num_tokens <= 0) {
72
        /* 0 tokens needed is a degenerate case, e.g., nothing but whitespace
73
         * until the first NUL. Reject that too. */
74
0
        g_free(filebuf);
75
0
        return WTAP_OPEN_NOT_MINE;
76
0
    }
77
78
0
    if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
79
0
        g_free(filebuf);
80
0
        return WTAP_OPEN_ERROR;
81
0
    }
82
83
0
    wth->file_type_subtype = json_file_type_subtype;
84
0
    wth->file_encap = WTAP_ENCAP_JSON;
85
0
    wth->file_tsprec = WTAP_TSPREC_SEC;
86
0
    wth->subtype_read = wtap_full_file_read;
87
0
    wth->subtype_seek_read = wtap_full_file_seek_read;
88
0
    wth->snapshot_length = 0;
89
90
0
    g_free(filebuf);
91
0
    return WTAP_OPEN_MINE;
92
0
}
93
94
static const struct supported_block_type json_blocks_supported[] = {
95
    /*
96
     * This is a file format that we dissect, so we provide only one
97
     * "packet" with the file's contents, and don't support any
98
     * options.
99
     */
100
    { WTAP_BLOCK_PACKET, ONE_BLOCK_SUPPORTED, NO_OPTIONS_SUPPORTED }
101
};
102
103
static const struct file_type_subtype_info json_info = {
104
    "JavaScript Object Notation", "json", "json", NULL,
105
    false, BLOCKS_SUPPORTED(json_blocks_supported),
106
    NULL, NULL, NULL
107
};
108
109
void register_json(void)
110
15
{
111
15
    json_file_type_subtype = wtap_register_file_type_subtype(&json_info);
112
113
    /*
114
     * Register name for backwards compatibility with the
115
     * wtap_filetypes table in Lua.
116
     */
117
15
    wtap_register_backwards_compatibility_lua_name("JSON",
118
15
                                                   json_file_type_subtype);
119
15
}
120
121
/*
122
 * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
123
 *
124
 * Local variables:
125
 * c-basic-offset: 4
126
 * tab-width: 8
127
 * indent-tabs-mode: nil
128
 * End:
129
 *
130
 * vi: set shiftwidth=4 tabstop=8 expandtab:
131
 * :indentSize=4:tabSize=8:noTabs=true:
132
 */