/src/wireshark/wiretap/ruby_marshal.c
Line | Count | Source |
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 | | #include "ruby_marshal.h" |
12 | | |
13 | | #include <string.h> |
14 | | |
15 | | #include "wtap_module.h" |
16 | | #include "file_wrappers.h" |
17 | | |
18 | | static int ruby_marshal_file_type_subtype = -1; |
19 | | |
20 | | void register_ruby_marshal(void); |
21 | | |
22 | | static bool is_ruby_marshal(const uint8_t* filebuf) |
23 | 0 | { |
24 | 0 | if (filebuf[0] != RUBY_MARSHAL_MAJOR) |
25 | 0 | return false; |
26 | 0 | if (filebuf[1] != RUBY_MARSHAL_MINOR) |
27 | 0 | return false; |
28 | 0 | switch (filebuf[2]) { |
29 | 0 | case '0': |
30 | 0 | case 'T': |
31 | 0 | case 'F': |
32 | 0 | case 'i': |
33 | 0 | case ':': |
34 | 0 | case '"': |
35 | 0 | case 'I': |
36 | 0 | case '[': |
37 | 0 | case '{': |
38 | 0 | case 'f': |
39 | 0 | case 'c': |
40 | 0 | case 'm': |
41 | 0 | case 'S': |
42 | 0 | case '/': |
43 | 0 | case 'o': |
44 | 0 | case 'C': |
45 | 0 | case 'e': |
46 | 0 | case ';': |
47 | 0 | case '@': |
48 | 0 | return true; |
49 | 0 | default: |
50 | 0 | return false; |
51 | 0 | } |
52 | 0 | } |
53 | | |
54 | | wtap_open_return_val ruby_marshal_open(wtap *wth, int *err, char **err_info) |
55 | 0 | { |
56 | | /* The size of this buffer should match the expectations of is_ruby_marshal */ |
57 | 0 | uint8_t filebuf[3]; |
58 | 0 | int bytes_read; |
59 | |
|
60 | 0 | bytes_read = file_read(filebuf, sizeof(filebuf), wth->fh); |
61 | 0 | if (bytes_read < 0) { |
62 | | /* Read error. */ |
63 | 0 | *err = file_error(wth->fh, err_info); |
64 | 0 | return WTAP_OPEN_ERROR; |
65 | 0 | } |
66 | | |
67 | 0 | if (bytes_read != sizeof(filebuf) || !is_ruby_marshal(filebuf)) { |
68 | 0 | return WTAP_OPEN_NOT_MINE; |
69 | 0 | } |
70 | | |
71 | 0 | if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) { |
72 | 0 | return WTAP_OPEN_ERROR; |
73 | 0 | } |
74 | | |
75 | 0 | wth->file_type_subtype = ruby_marshal_file_type_subtype; |
76 | 0 | wth->file_encap = WTAP_ENCAP_RUBY_MARSHAL; |
77 | 0 | wth->file_tsprec = WTAP_TSPREC_SEC; |
78 | 0 | wth->subtype_read = wtap_full_file_read; |
79 | 0 | wth->subtype_seek_read = wtap_full_file_seek_read; |
80 | 0 | wth->snapshot_length = 0; |
81 | |
|
82 | 0 | return WTAP_OPEN_MINE; |
83 | 0 | } |
84 | | |
85 | | static const struct supported_block_type ruby_marshal_blocks_supported[] = { |
86 | | /* |
87 | | * We support packet blocks, with no comments or other options. |
88 | | */ |
89 | | { WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED } |
90 | | }; |
91 | | |
92 | | static const struct file_type_subtype_info ruby_marshal_info = { |
93 | | "Ruby marshal files", "ruby_marshal", NULL, NULL, |
94 | | false, BLOCKS_SUPPORTED(ruby_marshal_blocks_supported), |
95 | | NULL, NULL, NULL |
96 | | }; |
97 | | |
98 | | void register_ruby_marshal(void) |
99 | 14 | { |
100 | 14 | ruby_marshal_file_type_subtype = wtap_register_file_type_subtype(&ruby_marshal_info); |
101 | | |
102 | | /* |
103 | | * Register name for backwards compatibility with the |
104 | | * wtap_filetypes table in Lua. |
105 | | */ |
106 | 14 | wtap_register_backwards_compatibility_lua_name("RUBY_MARSHAL", |
107 | 14 | ruby_marshal_file_type_subtype); |
108 | 14 | } |
109 | | |
110 | | /* |
111 | | * Editor modelines - https://www.wireshark.org/tools/modelines.html |
112 | | * |
113 | | * Local variables: |
114 | | * c-basic-offset: 4 |
115 | | * tab-width: 8 |
116 | | * indent-tabs-mode: nil |
117 | | * End: |
118 | | * |
119 | | * vi: set shiftwidth=4 tabstop=8 expandtab: |
120 | | * :indentSize=4:tabSize=8:noTabs=true: |
121 | | */ |