epan: use json_dumper for json outputs.
[metze/wireshark/wip.git] / wiretap / ruby_marshal.c
1 /* ruby_marshal.c
2  *
3  * Routines for reading a binary file containing a ruby marshal object
4  *
5  * Copyright 2018, Dario Lombardo <lomato@gmail.com>
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  */
9
10 #include "config.h"
11
12 #include <string.h>
13
14 #include "wtap-int.h"
15 #include "file_wrappers.h"
16
17 #include "ruby_marshal.h"
18
19 static gboolean is_ruby_marshal(const guint8* filebuf)
20 {
21     if (filebuf[0] != RUBY_MARSHAL_MAJOR)
22         return FALSE;
23     if (filebuf[1] != RUBY_MARSHAL_MINOR)
24         return FALSE;
25     switch (filebuf[2]) {
26         case '0':
27         case 'T':
28         case 'F':
29         case 'i':
30         case ':':
31         case '"':
32         case 'I':
33         case '[':
34         case '{':
35         case 'f':
36         case 'c':
37         case 'm':
38         case 'S':
39         case '/':
40         case 'o':
41         case 'C':
42         case 'e':
43         case ';':
44         case '@':
45             return TRUE;
46             break;
47         default:
48             return FALSE;
49     }
50 }
51
52 wtap_open_return_val ruby_marshal_open(wtap *wth, int *err, gchar **err_info)
53 {
54     /* The size of this buffer should match the expectations of is_ruby_marshal */
55     guint8 filebuf[3];
56     int bytes_read;
57
58     bytes_read = file_read(filebuf, sizeof(filebuf), wth->fh);
59     if (bytes_read < 0) {
60         /* Read error. */
61         *err = file_error(wth->fh, err_info);
62         return WTAP_OPEN_ERROR;
63     }
64
65     if (bytes_read != sizeof(filebuf) || !is_ruby_marshal(filebuf)) {
66         return WTAP_OPEN_NOT_MINE;
67     }
68
69     if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
70         return WTAP_OPEN_ERROR;
71     }
72
73     wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_RUBY_MARSHAL;
74     wth->file_encap = WTAP_ENCAP_RUBY_MARSHAL;
75     wth->file_tsprec = WTAP_TSPREC_SEC;
76     wth->subtype_read = wtap_full_file_read;
77     wth->subtype_seek_read = wtap_full_file_seek_read;
78     wth->snapshot_length = 0;
79
80     return WTAP_OPEN_MINE;
81 }
82
83 /*
84  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
85  *
86  * Local variables:
87  * c-basic-offset: 4
88  * tab-width: 8
89  * indent-tabs-mode: nil
90  * End:
91  *
92  * vi: set shiftwidth=4 tabstop=8 expandtab:
93  * :indentSize=4:tabSize=8:noTabs=true:
94  */