File: | wiretap/file_access.c |
Warning: | line 2275, column 13 Potential leak of memory pointed to by 'wdh' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | /* file_access.c | |||
2 | * | |||
3 | * Wiretap Library | |||
4 | * Copyright (c) 1998 by Gilbert Ramirez <[email protected]> | |||
5 | * | |||
6 | * SPDX-License-Identifier: GPL-2.0-or-later | |||
7 | */ | |||
8 | ||||
9 | #include "config.h" | |||
10 | #define WS_LOG_DOMAIN"Wiretap" LOG_DOMAIN_WIRETAP"Wiretap" | |||
11 | #include "wtap-int.h" | |||
12 | ||||
13 | #include <assert.h> | |||
14 | ||||
15 | #include <string.h> | |||
16 | #include <stdlib.h> | |||
17 | ||||
18 | #include <errno(*__errno_location ()).h> | |||
19 | ||||
20 | #include <wsutil/file_util.h> | |||
21 | #include <wsutil/tempfile.h> | |||
22 | #ifdef HAVE_PLUGINS1 | |||
23 | #include <wsutil/plugins.h> | |||
24 | #endif | |||
25 | #include <wsutil/ws_assert.h> | |||
26 | ||||
27 | #include "wtap_modules.h" | |||
28 | #include "file_wrappers.h" | |||
29 | #include "required_file_handlers.h" | |||
30 | #include <wsutil/application_flavor.h> | |||
31 | #include <wsutil/buffer.h> | |||
32 | #include <wsutil/str_util.h> | |||
33 | ||||
34 | #include "lanalyzer.h" | |||
35 | #include "ngsniffer.h" | |||
36 | #include "radcom.h" | |||
37 | #include "ascendtext.h" | |||
38 | #include "nettl.h" | |||
39 | #include "libpcap.h" | |||
40 | #include "snoop.h" | |||
41 | #include "iptrace.h" | |||
42 | #include "iseries.h" | |||
43 | #include "netmon.h" | |||
44 | #include "netxray.h" | |||
45 | #include "toshiba.h" | |||
46 | #include "eyesdn.h" | |||
47 | #include "i4btrace.h" | |||
48 | #include "csids.h" | |||
49 | #include "pppdump.h" | |||
50 | #include "peekclassic.h" | |||
51 | #include "peektagged.h" | |||
52 | #include "vms.h" | |||
53 | #include "dbs-etherwatch.h" | |||
54 | #include "visual.h" | |||
55 | #include "cosine.h" | |||
56 | #include "5views.h" | |||
57 | #include "erf.h" | |||
58 | #include "hcidump.h" | |||
59 | #include "logcat.h" | |||
60 | #include "logcat_text.h" | |||
61 | #include "json.h" | |||
62 | #include "observer.h" | |||
63 | #include "k12.h" | |||
64 | #include "ber.h" | |||
65 | #include "catapult_dct2000.h" | |||
66 | #include "mp4.h" | |||
67 | #include "mp2t.h" | |||
68 | #include "mpeg.h" | |||
69 | #include "netscreen.h" | |||
70 | #include "commview.h" | |||
71 | #include "pcapng.h" | |||
72 | #include "aethra.h" | |||
73 | #include "btsnoop.h" | |||
74 | #include "tnef.h" | |||
75 | #include "dct3trace.h" | |||
76 | #include "packetlogger.h" | |||
77 | #include "daintree-sna.h" | |||
78 | #include "netscaler.h" | |||
79 | #include "mime_file.h" | |||
80 | #include "ipfix.h" | |||
81 | #include "vwr.h" | |||
82 | #include "camins.h" | |||
83 | #include "stanag4607.h" | |||
84 | #include "capsa.h" | |||
85 | #include "nettrace_3gpp_32_423.h" | |||
86 | #include "mplog.h" | |||
87 | #include "dpa400.h" | |||
88 | #include "rfc7468.h" | |||
89 | #include "ruby_marshal.h" | |||
90 | #include "systemd_journal.h" | |||
91 | #include "log3gpp.h" | |||
92 | #include "candump.h" | |||
93 | #include "busmaster.h" | |||
94 | #include "cllog.h" | |||
95 | #include "blf.h" | |||
96 | #include "eri_enb_log.h" | |||
97 | #include "autosar_dlt.h" | |||
98 | #include "rtpdump.h" | |||
99 | #include "ems.h" | |||
100 | #include "ttl.h" | |||
101 | #include "peak-trc.h" | |||
102 | #include "netlog.h" | |||
103 | ||||
104 | /* | |||
105 | * Add an extension, and all compressed versions thereof if requested, | |||
106 | * to a GSList of extensions. | |||
107 | */ | |||
108 | static GSList * | |||
109 | add_extensions(GSList *extensions, const char *extension, | |||
110 | GSList *compression_type_extensions) | |||
111 | { | |||
112 | /* | |||
113 | * Add the specified extension. | |||
114 | */ | |||
115 | extensions = g_slist_prepend(extensions, g_strdup(extension)g_strdup_inline (extension)); | |||
116 | ||||
117 | /* | |||
118 | * Add whatever compressed versions we were supplied. | |||
119 | */ | |||
120 | for (GSList *compression_type_extension = compression_type_extensions; | |||
121 | compression_type_extension != NULL((void*)0); | |||
122 | compression_type_extension = g_slist_next(compression_type_extension)((compression_type_extension) ? (((GSList *)(compression_type_extension ))->next) : ((void*)0))) { | |||
123 | extensions = g_slist_prepend(extensions, | |||
124 | ws_strdup_printf("%s.%s", extension,wmem_strdup_printf(((void*)0), "%s.%s", extension, (const char *)compression_type_extension->data) | |||
125 | (const char *)compression_type_extension->data)wmem_strdup_printf(((void*)0), "%s.%s", extension, (const char *)compression_type_extension->data)); | |||
126 | } | |||
127 | ||||
128 | return extensions; | |||
129 | } | |||
130 | ||||
131 | /* | |||
132 | * File types that can be identified by file extensions. | |||
133 | * | |||
134 | * These are used in file open dialogs to offer choices of extensions | |||
135 | * for which to filter. Note that the first field can list more than | |||
136 | * one type of file, because, for example, ".cap" is a popular | |||
137 | * extension used by a number of capture file types. | |||
138 | * | |||
139 | * File types that *don't* have a file extension used for them should | |||
140 | * *not* be placed here; if there's nothing to put in the last field | |||
141 | * of the structure, don't put an entry here, not even one with an | |||
142 | * empty string for the extensions list. | |||
143 | * | |||
144 | * All added file types, regardless of extension or lack thereof, | |||
145 | * must also be added open_info_base[] below. | |||
146 | */ | |||
147 | static const struct file_extension_info wireshark_file_type_extensions_base[] = { | |||
148 | { "Wireshark/tcpdump/... - pcap", true1, "pcap;cap;dmp" }, | |||
149 | { "Wireshark/... - pcapng", true1, "pcapng;ntar" }, | |||
150 | { "Network Monitor, Surveyor, NetScaler", true1, "cap" }, | |||
151 | { "Sun snoop", true1, "snoop" }, | |||
152 | { "InfoVista 5View capture", true1, "5vw" }, | |||
153 | { "Sniffer (DOS)", true1, "cap;enc;trc;fdc;syc" }, | |||
154 | { "Cinco NetXRay, Sniffer (Windows)", true1, "cap;caz" }, | |||
155 | { "Endace ERF capture", true1, "erf" }, | |||
156 | { "EyeSDN USB S0/E1 ISDN trace format", true1, "trc" }, | |||
157 | { "HP-UX nettl trace", true1, "trc0;trc1" }, | |||
158 | { "Viavi Observer", true1, "bfr" }, | |||
159 | { "Colasoft Capsa", true1, "cscpkt" }, | |||
160 | { "Novell LANalyzer", true1, "tr1" }, | |||
161 | { "Tektronix K12xx 32-bit .rf5 format", true1, "rf5" }, | |||
162 | { "Savvius *Peek", true1, "pkt;tpc;apc;wpz" }, | |||
163 | { "Catapult DCT2000 trace (.out format)", true1, "out" }, | |||
164 | { "Micropross mplog", true1, "mplog" }, | |||
165 | { "TamoSoft CommView NCF", true1, "ncf" }, | |||
166 | { "TamoSoft CommView NCFX", true1, "ncfx" }, | |||
167 | { "Symbian OS btsnoop", true1, "log" }, | |||
168 | { "XML files (including Gammu DCT3 traces)", true1, "xml" }, | |||
169 | { "macOS PacketLogger", true1, "pklg" }, | |||
170 | { "Daintree SNA", true1, "dcf" }, | |||
171 | { "IPFIX File Format", true1, "pfx;ipfix" }, | |||
172 | { "Aethra .aps file", true1, "aps" }, | |||
173 | { "MPEG2 transport stream", true1, "mp2t;ts;m2ts;mpg" }, | |||
174 | { "Ixia IxVeriWave .vwr Raw 802.11 Capture", true1, "vwr" }, | |||
175 | { "CAM Inspector file", true1, "camins" }, | |||
176 | { "BLF file", true1, "blf" }, | |||
177 | { "AUTOSAR DLT file", true1, "dlt" }, | |||
178 | { "TTL file", true1, "ttl" }, | |||
179 | { "MPEG files", false0, "mpeg;mpg;mp3" }, | |||
180 | { "Transport-Neutral Encapsulation Format", false0, "tnef" }, | |||
181 | { "JPEG/JFIF files", false0, "jpg;jpeg;jfif" }, | |||
182 | { "NetLog file", true1, "json" }, | |||
183 | { "JavaScript Object Notation file", false0, "json" }, | |||
184 | { "MP4 file", false0, "mp4" }, | |||
185 | { "RTPDump file", false0, "rtp;rtpdump" }, | |||
186 | { "EMS file", false0, "ems" }, | |||
187 | { "ASN.1 Basic Encoding Rules", false0, "cer;crl;csr;p10;p12;p772;p7c;p7s;p7m;p8;pfx;tsq;tsr" }, | |||
188 | { "RFC 7468 files", false0, "crt;pem" }, | |||
189 | { "PEAK CAN TRC log", true1, "trc" }, | |||
190 | }; | |||
191 | ||||
192 | #define N_WIRESHARK_FILE_TYPE_EXTENSIONS(sizeof (wireshark_file_type_extensions_base) / sizeof (wireshark_file_type_extensions_base )[0]) array_length(wireshark_file_type_extensions_base)(sizeof (wireshark_file_type_extensions_base) / sizeof (wireshark_file_type_extensions_base )[0]) | |||
193 | ||||
194 | static const struct file_extension_info stratoshark_file_type_extensions_base[] = { | |||
195 | {"Stratoshark/... - scap", true1, "scap"}, | |||
196 | }; | |||
197 | ||||
198 | #define N_STRATOSHARK_FILE_TYPE_EXTENSIONS(sizeof (stratoshark_file_type_extensions_base) / sizeof (stratoshark_file_type_extensions_base )[0]) array_length(stratoshark_file_type_extensions_base)(sizeof (stratoshark_file_type_extensions_base) / sizeof (stratoshark_file_type_extensions_base )[0]) | |||
199 | ||||
200 | static const struct file_extension_info* file_type_extensions; | |||
201 | ||||
202 | static GArray* file_type_extensions_arr; | |||
203 | ||||
204 | /* initialize the extensions array if it has not been initialized yet */ | |||
205 | static void | |||
206 | init_file_type_extensions(void) | |||
207 | { | |||
208 | ||||
209 | if (file_type_extensions_arr) return; | |||
210 | ||||
211 | file_type_extensions_arr = g_array_new(false0,true1,sizeof(struct file_extension_info)); | |||
212 | ||||
213 | if (application_flavor_is_wireshark()) { | |||
214 | g_array_append_vals(file_type_extensions_arr, wireshark_file_type_extensions_base, N_WIRESHARK_FILE_TYPE_EXTENSIONS(sizeof (wireshark_file_type_extensions_base) / sizeof (wireshark_file_type_extensions_base )[0])); | |||
215 | } else { | |||
216 | g_array_append_vals(file_type_extensions_arr, stratoshark_file_type_extensions_base, N_STRATOSHARK_FILE_TYPE_EXTENSIONS(sizeof (stratoshark_file_type_extensions_base) / sizeof (stratoshark_file_type_extensions_base )[0])); | |||
217 | } | |||
218 | ||||
219 | file_type_extensions = (struct file_extension_info*)(void *)file_type_extensions_arr->data; | |||
220 | } | |||
221 | ||||
222 | void | |||
223 | wtap_register_file_type_extension(const struct file_extension_info *ei) | |||
224 | { | |||
225 | init_file_type_extensions(); | |||
226 | ||||
227 | g_array_append_val(file_type_extensions_arr,*ei)g_array_append_vals (file_type_extensions_arr, &(*ei), 1); | |||
228 | ||||
229 | file_type_extensions = (const struct file_extension_info*)(void *)file_type_extensions_arr->data; | |||
230 | } | |||
231 | ||||
232 | int | |||
233 | wtap_get_num_file_type_extensions(void) | |||
234 | { | |||
235 | return file_type_extensions_arr->len; | |||
236 | } | |||
237 | ||||
238 | const char * | |||
239 | wtap_get_file_extension_type_name(int extension_type) | |||
240 | { | |||
241 | return file_type_extensions[extension_type].name; | |||
242 | } | |||
243 | ||||
244 | static GSList * | |||
245 | add_extensions_for_file_extensions_type(int extension_type, GSList *extensions, | |||
246 | GSList *compression_type_extensions) | |||
247 | { | |||
248 | char **extensions_set, **extensionp, *extension; | |||
249 | ||||
250 | /* | |||
251 | * Split the extension-list string into a set of extensions. | |||
252 | */ | |||
253 | extensions_set = g_strsplit(file_type_extensions[extension_type].extensions, | |||
254 | ";", 0); | |||
255 | ||||
256 | /* | |||
257 | * Add each of those extensions to the list. | |||
258 | */ | |||
259 | for (extensionp = extensions_set; *extensionp != NULL((void*)0); extensionp++) { | |||
260 | extension = *extensionp; | |||
261 | ||||
262 | /* | |||
263 | * Add the extension, and all compressed variants | |||
264 | * of it. | |||
265 | */ | |||
266 | extensions = add_extensions(extensions, extension, | |||
267 | compression_type_extensions); | |||
268 | } | |||
269 | ||||
270 | g_strfreev(extensions_set); | |||
271 | return extensions; | |||
272 | } | |||
273 | ||||
274 | /* Return a list of file extensions that are used by the specified file | |||
275 | * extension type. | |||
276 | * | |||
277 | * All strings in the list are allocated with g_malloc() and must be freed | |||
278 | * with g_free(). | |||
279 | */ | |||
280 | GSList * | |||
281 | wtap_get_file_extension_type_extensions(unsigned extension_type) | |||
282 | { | |||
283 | GSList *extensions, *compression_type_extensions; | |||
284 | ||||
285 | if (extension_type >= file_type_extensions_arr->len) | |||
286 | return NULL((void*)0); /* not a valid extension type */ | |||
287 | ||||
288 | extensions = NULL((void*)0); /* empty list, to start with */ | |||
289 | ||||
290 | /* | |||
291 | * Get compression-type extensions, if any. | |||
292 | */ | |||
293 | compression_type_extensions = wtap_get_all_compression_type_extensions_list(); | |||
294 | ||||
295 | /* | |||
296 | * Add all this file extension type's extensions, with compressed | |||
297 | * variants. | |||
298 | */ | |||
299 | extensions = add_extensions_for_file_extensions_type(extension_type, | |||
300 | extensions, compression_type_extensions); | |||
301 | ||||
302 | g_slist_free(compression_type_extensions); | |||
303 | ||||
304 | return g_slist_reverse(extensions); | |||
305 | } | |||
306 | ||||
307 | /* | |||
308 | * The open_file_* routines must return: | |||
309 | * | |||
310 | * WTAP_OPEN_ERROR on an I/O error; | |||
311 | * | |||
312 | * WTAP_OPEN_MINE if the file they're reading is one of the types | |||
313 | * it handles; | |||
314 | * | |||
315 | * WTAP_OPEN_NOT_MINE if the file they're reading isn't the type | |||
316 | * they're checking for. | |||
317 | * | |||
318 | * If the routine handles this type of file, it must set the "file_type" | |||
319 | * field in the "struct wtap" to the type of the file. | |||
320 | * | |||
321 | * Note that the routine does *not* have to free the private data pointer on | |||
322 | * error. The caller takes care of that by calling wtap_close on error. | |||
323 | * (See https://gitlab.com/wireshark/wireshark/-/issues/8518) | |||
324 | * | |||
325 | * However, the caller *does* have to free the private data pointer when | |||
326 | * returning WTAP_OPEN_NOT_MINE, since the next file type will be called | |||
327 | * and will likely just overwrite the pointer. | |||
328 | * | |||
329 | * The names are used in file open dialogs to select, for files that | |||
330 | * don't have magic numbers and that could potentially be files of | |||
331 | * more than one type based on the heuristics, a particular file | |||
332 | * type to interpret it as, if the file name has no extension, the | |||
333 | * extension isn't sufficient to determine the appropriate file type, | |||
334 | * or the extension is wrong. | |||
335 | * | |||
336 | * NOTE: when adding file formats to this list you may also want to add them | |||
337 | * to the following files so that the various desktop environments will | |||
338 | * know that Wireshark can open the file: | |||
339 | * 1) resources/freedesktop/org.wireshark.Wireshark-mime.xml (for freedesktop.org environments) | |||
340 | * 2) packaging/macosx/WiresharkInfo.plist.in (for macOS) | |||
341 | * | |||
342 | * If your file format has a commonly-used extension (e.g., ".pcap") then you | |||
343 | * should probably also add it to file_type_extensions_base[] (in this file), | |||
344 | * to the list of "<glob pattern=...>" entries for this file format in | |||
345 | * resources/freedesktop/org.wireshark.Wireshark-mime.xml, to the | |||
346 | * CFBundleTypeExtensions array for this file format in | |||
347 | * packaging/macosx/WiresharkInfo.plist, and to the PushFileExtensions macro | |||
348 | * in packaging/nsis/wireshark-common.nsh and the File Associations in | |||
349 | * packaging/wix/ComponentGroups.wxi (for Windows). | |||
350 | */ | |||
351 | static const struct open_info open_info_base[] = { | |||
352 | /* Open routines that look for magic numbers */ | |||
353 | { "Wireshark/tcpdump/... - pcap", OPEN_INFO_MAGIC, libpcap_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
354 | { "Wireshark/... - pcapng", OPEN_INFO_MAGIC, pcapng_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
355 | { "Sniffer (DOS)", OPEN_INFO_MAGIC, ngsniffer_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
356 | { "Snoop, Shomiti/Finisar Surveyor", OPEN_INFO_MAGIC, snoop_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
357 | { "AIX iptrace", OPEN_INFO_MAGIC, iptrace_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
358 | { "Microsoft Network Monitor", OPEN_INFO_MAGIC, netmon_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
359 | { "Cinco NetXray/Sniffer (Windows)", OPEN_INFO_MAGIC, netxray_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
360 | { "RADCOM WAN/LAN analyzer", OPEN_INFO_MAGIC, radcom_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
361 | { "HP-UX nettl trace", OPEN_INFO_MAGIC, nettl_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
362 | { "Visual Networks traffic capture", OPEN_INFO_MAGIC, visual_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
363 | { "InfoVista 5View capture", OPEN_INFO_MAGIC, _5views_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
364 | { "Viavi Observer", OPEN_INFO_MAGIC, observer_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
365 | { "Savvius tagged", OPEN_INFO_MAGIC, peektagged_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
366 | { "Colasoft Capsa", OPEN_INFO_MAGIC, capsa_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
367 | { "DBS Etherwatch (VMS)", OPEN_INFO_MAGIC, dbs_etherwatch_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
368 | { "Tektronix K12xx 32-bit .rf5 format", OPEN_INFO_MAGIC, k12_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
369 | { "Catapult DCT2000 trace (.out format)", OPEN_INFO_MAGIC, catapult_dct2000_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
370 | { "Aethra .aps file", OPEN_INFO_MAGIC, aethra_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
371 | { "Symbian OS btsnoop", OPEN_INFO_MAGIC, btsnoop_open, "log", NULL((void*)0), NULL((void*)0) }, | |||
372 | { "EyeSDN USB S0/E1 ISDN trace format", OPEN_INFO_MAGIC, eyesdn_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
373 | { "Transport-Neutral Encapsulation Format", OPEN_INFO_MAGIC, tnef_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
374 | /* 3GPP TS 32.423 Trace must come before MIME Files as it's XML based*/ | |||
375 | { "3GPP TS 32.423 Trace format", OPEN_INFO_MAGIC, nettrace_3gpp_32_423_file_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
376 | /* Gammu DCT3 trace must come before MIME files as it's XML based*/ | |||
377 | { "Gammu DCT3 trace", OPEN_INFO_MAGIC, dct3trace_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
378 | { "BLF Logfile", OPEN_INFO_MAGIC, blf_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
379 | { "AUTOSAR DLT Logfile", OPEN_INFO_MAGIC, autosar_dlt_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
380 | { "TTL Logfile", OPEN_INFO_MAGIC, ttl_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
381 | { "RTPDump files", OPEN_INFO_MAGIC, rtpdump_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
382 | { "MIME Files Format", OPEN_INFO_MAGIC, mime_file_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
383 | { "Micropross mplog", OPEN_INFO_MAGIC, mplog_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
384 | { "Unigraf DPA-400 capture", OPEN_INFO_MAGIC, dpa400_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
385 | { "RFC 7468 files", OPEN_INFO_MAGIC, rfc7468_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
386 | ||||
387 | /* Open routines that have no magic numbers and require heuristics. */ | |||
388 | { "Novell LANalyzer", OPEN_INFO_HEURISTIC, lanalyzer_open, "tr1", NULL((void*)0), NULL((void*)0) }, | |||
389 | /* | |||
390 | * PacketLogger must come before MPEG, because its files | |||
391 | * are sometimes grabbed by mpeg_open. | |||
392 | */ | |||
393 | { "macOS PacketLogger", OPEN_INFO_HEURISTIC, packetlogger_open, "pklg", NULL((void*)0), NULL((void*)0) }, | |||
394 | /* Some MPEG files have magic numbers, others just have heuristics. */ | |||
395 | { "MPEG", OPEN_INFO_HEURISTIC, mpeg_open, "mpeg;mpg;mp3", NULL((void*)0), NULL((void*)0) }, | |||
396 | { "Daintree SNA", OPEN_INFO_HEURISTIC, daintree_sna_open, "dcf", NULL((void*)0), NULL((void*)0) }, | |||
397 | { "STANAG 4607 Format", OPEN_INFO_HEURISTIC, stanag4607_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
398 | { "ASN.1 Basic Encoding Rules", OPEN_INFO_HEURISTIC, ber_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
399 | /* | |||
400 | * I put NetScreen *before* erf, because there were some | |||
401 | * false positives with my test-files (Sake Blok, July 2007) | |||
402 | * | |||
403 | * I put VWR *after* ERF, because there were some cases where | |||
404 | * ERF files were misidentified as vwr files (Stephen | |||
405 | * Donnelly, August 2013; see bug 9054) | |||
406 | * | |||
407 | * I put VWR *after* Peek Classic, CommView, iSeries text, | |||
408 | * Toshiba text, K12 text, VMS tcpiptrace text, and NetScaler, | |||
409 | * because there were some cases where files of those types were | |||
410 | * misidentified as vwr files (Guy Harris, December 2013) | |||
411 | */ | |||
412 | { "NetScreen snoop text file", OPEN_INFO_HEURISTIC, netscreen_open, "txt", NULL((void*)0), NULL((void*)0) }, | |||
413 | { "Endace ERF capture", OPEN_INFO_HEURISTIC, erf_open, "erf", NULL((void*)0), NULL((void*)0) }, | |||
414 | { "IPFIX File Format", OPEN_INFO_HEURISTIC, ipfix_open, "pfx;ipfix",NULL((void*)0), NULL((void*)0) }, | |||
415 | { "K12 text file", OPEN_INFO_HEURISTIC, k12text_open, "txt", NULL((void*)0), NULL((void*)0) }, | |||
416 | { "Savvius classic", OPEN_INFO_HEURISTIC, peekclassic_open, "pkt;tpc;apc;wpz", NULL((void*)0), NULL((void*)0) }, | |||
417 | { "pppd log (pppdump format)", OPEN_INFO_HEURISTIC, pppdump_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
418 | { "IBM iSeries comm. trace", OPEN_INFO_HEURISTIC, iseries_open, "txt", NULL((void*)0), NULL((void*)0) }, | |||
419 | { "I4B ISDN trace", OPEN_INFO_HEURISTIC, i4btrace_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
420 | { "MPEG2 transport stream", OPEN_INFO_HEURISTIC, mp2t_open, "mp2t;ts;mpg", NULL((void*)0), NULL((void*)0) }, | |||
421 | { "CSIDS IPLog", OPEN_INFO_HEURISTIC, csids_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
422 | { "TCPIPtrace (VMS)", OPEN_INFO_HEURISTIC, vms_open, "txt", NULL((void*)0), NULL((void*)0) }, | |||
423 | { "CoSine IPSX L2 capture", OPEN_INFO_HEURISTIC, cosine_open, "txt", NULL((void*)0), NULL((void*)0) }, | |||
424 | { "Bluetooth HCI dump", OPEN_INFO_HEURISTIC, hcidump_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
425 | { "TamoSoft CommView NCF", OPEN_INFO_HEURISTIC, commview_ncf_open, "ncf", NULL((void*)0), NULL((void*)0) }, | |||
426 | { "TamoSoft CommView NCFX", OPEN_INFO_HEURISTIC, commview_ncfx_open, "ncfx", NULL((void*)0), NULL((void*)0) }, | |||
427 | { "NetScaler", OPEN_INFO_HEURISTIC, nstrace_open, "cap", NULL((void*)0), NULL((void*)0) }, | |||
428 | { "Android Logcat Binary format", OPEN_INFO_HEURISTIC, logcat_open, "logcat", NULL((void*)0), NULL((void*)0) }, | |||
429 | { "Android Logcat Text formats", OPEN_INFO_HEURISTIC, logcat_text_open, "txt", NULL((void*)0), NULL((void*)0) }, | |||
430 | { "Candump log", OPEN_INFO_HEURISTIC, candump_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
431 | { "Busmaster log", OPEN_INFO_HEURISTIC, busmaster_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
432 | { "CSS Electronics CLX000 CAN log", OPEN_INFO_MAGIC, cllog_open, "txt", NULL((void*)0), NULL((void*)0) }, | |||
433 | { "Ericsson eNode-B raw log", OPEN_INFO_MAGIC, eri_enb_log_open, NULL((void*)0), NULL((void*)0), NULL((void*)0) }, | |||
434 | { "Systemd Journal", OPEN_INFO_HEURISTIC, systemd_journal_open, "log;jnl;journal", NULL((void*)0), NULL((void*)0) }, | |||
435 | { "PEAK CAN TRC log", OPEN_INFO_HEURISTIC, peak_trc_open, "trc", NULL((void*)0), NULL((void*)0) }, | |||
436 | ||||
437 | /* ASCII trace files from Telnet sessions. */ | |||
438 | { "Lucent/Ascend access server trace", OPEN_INFO_HEURISTIC, ascend_open, "txt", NULL((void*)0), NULL((void*)0) }, | |||
439 | { "Toshiba Compact ISDN Router snoop", OPEN_INFO_HEURISTIC, toshiba_open, "txt", NULL((void*)0), NULL((void*)0) }, | |||
440 | ||||
441 | { "EGNOS Message Server (EMS) file", OPEN_INFO_HEURISTIC, ems_open, "ems", NULL((void*)0), NULL((void*)0) }, | |||
442 | ||||
443 | /* Extremely weak heuristics - put them at the end. */ | |||
444 | { "Ixia IxVeriWave .vwr Raw Capture", OPEN_INFO_HEURISTIC, vwr_open, "vwr", NULL((void*)0), NULL((void*)0) }, | |||
445 | { "CAM Inspector file", OPEN_INFO_HEURISTIC, camins_open, "camins", NULL((void*)0), NULL((void*)0) }, | |||
446 | /* NetLog needs to be before JSON open because it is a specifically formatted JSON file */ | |||
447 | { "NetLog", OPEN_INFO_HEURISTIC, netlog_open, "json", NULL((void*)0), NULL((void*)0) }, | |||
448 | { "JavaScript Object Notation", OPEN_INFO_HEURISTIC, json_open, "json", NULL((void*)0), NULL((void*)0) }, | |||
449 | { "Ruby Marshal Object", OPEN_INFO_HEURISTIC, ruby_marshal_open, "", NULL((void*)0), NULL((void*)0) }, | |||
450 | { "3gpp phone log", OPEN_INFO_MAGIC, log3gpp_open, "log", NULL((void*)0), NULL((void*)0) }, | |||
451 | { "MP4 media file", OPEN_INFO_MAGIC, mp4_open, "mp4", NULL((void*)0), NULL((void*)0) }, | |||
452 | }; | |||
453 | ||||
454 | /* this is only used to build the dynamic array on load, do NOT use this | |||
455 | * for anything else, because the size of the actual array will change if | |||
456 | * Lua scripts register a new file reader. | |||
457 | */ | |||
458 | #define N_OPEN_INFO_ROUTINES(sizeof (open_info_base) / sizeof (open_info_base)[0]) array_length(open_info_base)(sizeof (open_info_base) / sizeof (open_info_base)[0]) | |||
459 | ||||
460 | static GArray *open_info_arr; | |||
461 | ||||
462 | /* this always points to the top of the created array */ | |||
463 | struct open_info *open_routines; | |||
464 | ||||
465 | /* this points to the first OPEN_INFO_HEURISTIC type in the array */ | |||
466 | static unsigned heuristic_open_routine_idx; | |||
467 | ||||
468 | static void | |||
469 | set_heuristic_routine(void) | |||
470 | { | |||
471 | unsigned i; | |||
472 | ws_assert(open_info_arr != NULL)do { if ((1) && !(open_info_arr != ((void*)0))) ws_log_fatal_full ("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c", 472, __func__ , "assertion failed: %s", "open_info_arr != ((void*)0)"); } while (0); | |||
473 | ||||
474 | for (i = 0; i < open_info_arr->len; i++) { | |||
475 | if (open_routines[i].type == OPEN_INFO_HEURISTIC) { | |||
476 | heuristic_open_routine_idx = i; | |||
477 | break; | |||
478 | } | |||
479 | /* sanity check */ | |||
480 | ws_assert(open_routines[i].type == OPEN_INFO_MAGIC)do { if ((1) && !(open_routines[i].type == OPEN_INFO_MAGIC )) ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 480, __func__, "assertion failed: %s", "open_routines[i].type == OPEN_INFO_MAGIC" ); } while (0); | |||
481 | } | |||
482 | ||||
483 | ws_assert(heuristic_open_routine_idx > 0)do { if ((1) && !(heuristic_open_routine_idx > 0)) ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 483, __func__, "assertion failed: %s", "heuristic_open_routine_idx > 0" ); } while (0); | |||
484 | } | |||
485 | ||||
486 | void | |||
487 | init_open_routines(void) | |||
488 | { | |||
489 | unsigned int i; | |||
490 | struct open_info *i_open; | |||
491 | ||||
492 | if (open_info_arr) | |||
493 | return; | |||
494 | ||||
495 | open_info_arr = g_array_new(true1,true1,sizeof(struct open_info)); | |||
496 | ||||
497 | g_array_append_vals(open_info_arr, open_info_base, N_OPEN_INFO_ROUTINES(sizeof (open_info_base) / sizeof (open_info_base)[0])); | |||
498 | ||||
499 | open_routines = (struct open_info *)(void*) open_info_arr->data; | |||
500 | ||||
501 | /* Populate the extensions_set list now */ | |||
502 | for (i = 0, i_open = open_routines; i < open_info_arr->len; i++, i_open++) { | |||
503 | if (i_open->extensions != NULL((void*)0)) | |||
504 | i_open->extensions_set = g_strsplit(i_open->extensions, ";", 0); | |||
505 | } | |||
506 | ||||
507 | set_heuristic_routine(); | |||
508 | } | |||
509 | ||||
510 | /* | |||
511 | * Registers a new file reader - currently only called by wslua code for | |||
512 | * Lua readers and by compiled file reader plugins. | |||
513 | * | |||
514 | * If first_routine is true, the reader added before other readers of its | |||
515 | * type (magic or heuristic). This should be done only in cases where | |||
516 | * this reader's open test must be performed early, to avoid false | |||
517 | * positives for other readers' tests treating files for this reader | |||
518 | * as being for another reader. | |||
519 | * | |||
520 | * XXX - given that there is no guarantee that registration routines will | |||
521 | * be called in a given order, all this really does is divide readers for | |||
522 | * a given type (magic or heuristic) into two categories, with open routines | |||
523 | * for readers in the first category (first_routine true) all being called | |||
524 | * before readers in the second category; it does not guarantee a particular | |||
525 | * total order for open routines. | |||
526 | * | |||
527 | * Checks for an existing reader of the same name and errors if it finds one; | |||
528 | * if you want to handle that condition more gracefully, call | |||
529 | * wtap_has_open_info() first. | |||
530 | */ | |||
531 | void | |||
532 | wtap_register_open_info(struct open_info *oi, const bool_Bool first_routine) | |||
533 | { | |||
534 | if (!oi || !oi->name) { | |||
535 | ws_error("No open_info name given to register")ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 535, __func__, "No open_info name given to register"); | |||
536 | return; | |||
537 | } | |||
538 | ||||
539 | /* verify name doesn't already exist */ | |||
540 | if (wtap_has_open_info(oi->name)) { | |||
541 | ws_error("Name given to register_open_info already exists")ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 541, __func__, "Name given to register_open_info already exists" ); | |||
542 | return; | |||
543 | } | |||
544 | ||||
545 | if (oi->extensions != NULL((void*)0)) | |||
546 | oi->extensions_set = g_strsplit(oi->extensions, ";", 0); | |||
547 | ||||
548 | /* if it's magic and first, prepend it; if it's heuristic and not first, | |||
549 | append it; if it's anything else, stick it in the middle */ | |||
550 | if (first_routine && oi->type == OPEN_INFO_MAGIC) { | |||
551 | g_array_prepend_val(open_info_arr, *oi)g_array_prepend_vals (open_info_arr, &(*oi), 1); | |||
552 | } else if (!first_routine && oi->type == OPEN_INFO_HEURISTIC) { | |||
553 | g_array_append_val(open_info_arr, *oi)g_array_append_vals (open_info_arr, &(*oi), 1); | |||
554 | } else { | |||
555 | g_array_insert_val(open_info_arr, heuristic_open_routine_idx, *oi)g_array_insert_vals (open_info_arr, heuristic_open_routine_idx , &(*oi), 1); | |||
556 | } | |||
557 | ||||
558 | open_routines = (struct open_info *)(void*) open_info_arr->data; | |||
559 | set_heuristic_routine(); | |||
560 | } | |||
561 | ||||
562 | /* De-registers a file reader by removing it from the GArray based on its name. | |||
563 | * This function must NOT be called during wtap_open_offline(), since it changes the array. | |||
564 | * Note: this function will error if it doesn't find the given name; if you want to handle | |||
565 | * that condition more gracefully, call wtap_has_open_info() first. | |||
566 | */ | |||
567 | void | |||
568 | wtap_deregister_open_info(const char *name) | |||
569 | { | |||
570 | unsigned i; | |||
571 | ||||
572 | if (!name) { | |||
573 | ws_error("Missing open_info name to de-register")ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 573, __func__, "Missing open_info name to de-register"); | |||
574 | return; | |||
575 | } | |||
576 | ||||
577 | for (i = 0; i < open_info_arr->len; i++) { | |||
578 | if (open_routines[i].name && strcmp(open_routines[i].name, name) == 0) { | |||
579 | g_strfreev(open_routines[i].extensions_set); | |||
580 | open_info_arr = g_array_remove_index(open_info_arr, i); | |||
581 | set_heuristic_routine(); | |||
582 | return; | |||
583 | } | |||
584 | } | |||
585 | ||||
586 | ws_error("deregister_open_info: name not found")ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 586, __func__, "deregister_open_info: name not found"); | |||
587 | } | |||
588 | ||||
589 | /* Determines if a open routine short name already exists | |||
590 | */ | |||
591 | bool_Bool | |||
592 | wtap_has_open_info(const char *name) | |||
593 | { | |||
594 | unsigned i; | |||
595 | ||||
596 | if (!name) { | |||
597 | ws_error("No name given to wtap_has_open_info!")ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 597, __func__, "No name given to wtap_has_open_info!"); | |||
598 | return false0; | |||
599 | } | |||
600 | ||||
601 | ||||
602 | for (i = 0; i < open_info_arr->len; i++) { | |||
603 | if (open_routines[i].name && strcmp(open_routines[i].name, name) == 0) { | |||
604 | return true1; | |||
605 | } | |||
606 | } | |||
607 | ||||
608 | return false0; | |||
609 | } | |||
610 | ||||
611 | bool_Bool | |||
612 | wtap_uses_lua_filehandler(const wtap* wth) | |||
613 | { | |||
614 | if (wth && wth->wslua_data != NULL((void*)0)) { | |||
615 | /* | |||
616 | * Currently, wslua_data is set if and only if using a Lua | |||
617 | * file handler. | |||
618 | */ | |||
619 | return true1; | |||
620 | } | |||
621 | ||||
622 | return false0; | |||
623 | } | |||
624 | ||||
625 | /* | |||
626 | * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't | |||
627 | * define them either.) | |||
628 | * | |||
629 | * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO. | |||
630 | */ | |||
631 | #ifndef S_ISREG | |||
632 | #define S_ISREG(mode)((((mode)) & 0170000) == (0100000)) (((mode) & S_IFMT0170000) == S_IFREG0100000) | |||
633 | #endif | |||
634 | #ifndef S_IFIFO0010000 | |||
635 | #define S_IFIFO0010000 _S_IFIFO | |||
636 | #endif | |||
637 | #ifndef S_ISFIFO | |||
638 | #define S_ISFIFO(mode)((((mode)) & 0170000) == (0010000)) (((mode) & S_IFMT0170000) == S_IFIFO0010000) | |||
639 | #endif | |||
640 | #ifndef S_ISDIR | |||
641 | #define S_ISDIR(mode)((((mode)) & 0170000) == (0040000)) (((mode) & S_IFMT0170000) == S_IFDIR0040000) | |||
642 | #endif | |||
643 | ||||
644 | /* returns the 'type' number to use for wtap_open_offline based on the | |||
645 | * passed-in name (the name in the open_info struct). It returns WTAP_TYPE_AUTO | |||
646 | * on failure, which is the number 0. The 'type' number is the entry's index+1, | |||
647 | * because that's what wtap_open_offline() expects it to be. | |||
648 | */ | |||
649 | unsigned int | |||
650 | open_info_name_to_type(const char *name) | |||
651 | { | |||
652 | unsigned int i; | |||
653 | ||||
654 | if (!name) | |||
655 | return WTAP_TYPE_AUTO0; | |||
656 | ||||
657 | for (i = 0; i < open_info_arr->len; i++) { | |||
658 | if (open_routines[i].name != NULL((void*)0) && | |||
659 | strcmp(name, open_routines[i].name) == 0) | |||
660 | return i+1; | |||
661 | } | |||
662 | ||||
663 | return WTAP_TYPE_AUTO0; /* no such file type */ | |||
664 | } | |||
665 | ||||
666 | static char * | |||
667 | get_file_extension(const char *pathname) | |||
668 | { | |||
669 | char *filename; | |||
670 | char **components; | |||
671 | size_t ncomponents; | |||
672 | char *extensionp; | |||
673 | ||||
674 | /* | |||
675 | * Is the pathname empty? | |||
676 | */ | |||
677 | if (strcmp(pathname, "") == 0) | |||
678 | return NULL((void*)0); /* no extension */ | |||
679 | ||||
680 | /* | |||
681 | * Find the last component of the pathname. | |||
682 | */ | |||
683 | filename = g_path_get_basename(pathname); | |||
684 | ||||
685 | /* | |||
686 | * Does it have an extension? | |||
687 | */ | |||
688 | if (strchr(filename, '.') == NULL((void*)0)) { | |||
689 | g_free(filename); | |||
690 | return NULL((void*)0); /* no extension whatsoever */ | |||
691 | } | |||
692 | ||||
693 | /* | |||
694 | * Yes. Fold it to lowercase, since open_routines[] has | |||
695 | * its extensions in lowercase. | |||
696 | */ | |||
697 | ascii_strdown_inplace(filename); | |||
698 | ||||
699 | /* | |||
700 | * Split it into components separated by ".". | |||
701 | */ | |||
702 | components = g_strsplit(filename, ".", 0); | |||
703 | g_free(filename); | |||
704 | ||||
705 | /* | |||
706 | * Count the components. | |||
707 | */ | |||
708 | for (ncomponents = 0; components[ncomponents] != NULL((void*)0); ncomponents++) | |||
709 | ; | |||
710 | ||||
711 | if (ncomponents == 0) { | |||
712 | g_strfreev(components); | |||
713 | return NULL((void*)0); /* no components */ | |||
714 | } | |||
715 | if (ncomponents == 1) { | |||
716 | g_strfreev(components); | |||
717 | return NULL((void*)0); /* only one component, with no "." */ | |||
718 | } | |||
719 | ||||
720 | /* | |||
721 | * Get compression-type extensions, if any. | |||
722 | */ | |||
723 | GSList *compression_type_extensions = wtap_get_all_compression_type_extensions_list(); | |||
724 | ||||
725 | /* | |||
726 | * Is the last component one of the extensions used for compressed | |||
727 | * files? | |||
728 | */ | |||
729 | extensionp = components[ncomponents - 1]; | |||
730 | for (GSList *compression_type_extension = compression_type_extensions; | |||
731 | compression_type_extension != NULL((void*)0); | |||
732 | compression_type_extension = g_slist_next(compression_type_extension)((compression_type_extension) ? (((GSList *)(compression_type_extension ))->next) : ((void*)0))) { | |||
733 | if (strcmp(extensionp, (const char *)compression_type_extension->data) == 0) { | |||
734 | /* | |||
735 | * Yes, so it's one of the compressed-file extensions. | |||
736 | * Is there an extension before that? | |||
737 | */ | |||
738 | if (ncomponents == 2) { | |||
739 | g_slist_free(compression_type_extensions); | |||
740 | g_strfreev(components); | |||
741 | return NULL((void*)0); /* no, only two components */ | |||
742 | } | |||
743 | ||||
744 | /* | |||
745 | * Yes, return that extension. | |||
746 | */ | |||
747 | g_slist_free(compression_type_extensions); | |||
748 | extensionp = g_strdup(components[ncomponents - 2])g_strdup_inline (components[ncomponents - 2]); | |||
749 | g_strfreev(components); | |||
750 | return extensionp; | |||
751 | } | |||
752 | } | |||
753 | ||||
754 | g_slist_free(compression_type_extensions); | |||
755 | ||||
756 | /* | |||
757 | * The extension isn't one of the compressed-file extensions; | |||
758 | * return it. | |||
759 | */ | |||
760 | extensionp = g_strdup(extensionp)g_strdup_inline (extensionp); | |||
761 | g_strfreev(components); | |||
762 | return extensionp; | |||
763 | } | |||
764 | ||||
765 | /* | |||
766 | * Check if file extension is used in this heuristic | |||
767 | */ | |||
768 | static bool_Bool | |||
769 | heuristic_uses_extension(unsigned int i, const char *extension) | |||
770 | { | |||
771 | char **extensionp; | |||
772 | ||||
773 | /* | |||
774 | * Does this file type *have* any extensions? | |||
775 | */ | |||
776 | if (open_routines[i].extensions == NULL((void*)0)) | |||
777 | return false0; /* no */ | |||
778 | ||||
779 | /* | |||
780 | * Check each of them against the specified extension. | |||
781 | */ | |||
782 | for (extensionp = open_routines[i].extensions_set; *extensionp != NULL((void*)0); | |||
783 | extensionp++) { | |||
784 | if (strcmp(extension, *extensionp) == 0) { | |||
785 | return true1; /* it's one of them */ | |||
786 | } | |||
787 | } | |||
788 | ||||
789 | return false0; /* it's not one of them */ | |||
790 | } | |||
791 | ||||
792 | /* | |||
793 | * Attempt to open the file corresponding to "wth" using the file format | |||
794 | * handler in "candidate". | |||
795 | * | |||
796 | * Returns WTAP_OPEN_MINE upon success, WTAP_OPEN_NOT_MINE if the file is not | |||
797 | * in a suitable format for "candidate", or WTAP_OPEN_ERROR if a failure | |||
798 | * occurred while reading the input. | |||
799 | */ | |||
800 | static int | |||
801 | try_one_open(wtap *wth, const struct open_info *candidate, int *err, char **err_info) | |||
802 | { | |||
803 | /* Seek back to the beginning of the file; the open routine for the | |||
804 | * previous file type may have left the file position somewhere other | |||
805 | * than the beginning, and the open routine for this file type will | |||
806 | * probably want to start reading at the beginning. | |||
807 | * | |||
808 | * Initialize the data offset while we're at it. | |||
809 | */ | |||
810 | if (file_seek(wth->fh, 0, SEEK_SET0, err) == -1) { | |||
811 | /* Error - give up */ | |||
812 | return WTAP_OPEN_ERROR; | |||
813 | } | |||
814 | ||||
815 | /* Set wth with wslua data if any - this is how we pass the data to the | |||
816 | * file reader, kinda like the priv member but not free'd later. | |||
817 | * It's ok for this to copy a NULL. | |||
818 | */ | |||
819 | wth->wslua_data = candidate->wslua_data; | |||
820 | ||||
821 | return candidate->open_routine(wth, err, err_info); | |||
822 | } | |||
823 | ||||
824 | /* | |||
825 | * Attempt to open the file corresponding to "wth". If "type" is supplied | |||
826 | * (i.e. other than WTAP_TYPE_AUTO), that will be the only type attempted. | |||
827 | * Otherwise, heuristic detection of the file format will be performed, | |||
828 | * possibly guided by the extension part of "filename". | |||
829 | * | |||
830 | * Returns WTAP_OPEN_MINE upon success, WTAP_OPEN_NOT_MINE if it was not | |||
831 | * possible to determine a suitable format for the file, or WTAP_OPEN_ERROR if | |||
832 | * a failure occurred while reading the input. | |||
833 | */ | |||
834 | static int | |||
835 | try_open(wtap *wth, unsigned int type, int *err, char **err_info) | |||
836 | { | |||
837 | int result = WTAP_OPEN_NOT_MINE; | |||
838 | unsigned i; | |||
839 | char *extension; | |||
840 | ||||
841 | /* 'type' is 1-based. */ | |||
842 | if (type != WTAP_TYPE_AUTO0 && type <= open_info_arr->len) { | |||
843 | /* Try only the specified type. */ | |||
844 | return try_one_open(wth, &open_routines[type - 1], err, err_info); | |||
845 | } | |||
846 | ||||
847 | /* First, all file types that support magic numbers. */ | |||
848 | for (i = 0; i < heuristic_open_routine_idx && result == WTAP_OPEN_NOT_MINE; i++) { | |||
849 | result = try_one_open(wth, &open_routines[i], err, err_info); | |||
850 | } | |||
851 | ||||
852 | if (result != WTAP_OPEN_NOT_MINE) { | |||
853 | return result; | |||
854 | } | |||
855 | ||||
856 | /* Does this file's name have an extension? */ | |||
857 | extension = get_file_extension(wth->pathname); | |||
858 | if (extension != NULL((void*)0)) { | |||
859 | unsigned pass; | |||
860 | ||||
861 | /* | |||
862 | * Yes, the filename has an extension. | |||
863 | * | |||
864 | * The heuristic types fall into one of three categories, which | |||
865 | * we attempt in order by scanning for each category in turn. | |||
866 | * | |||
867 | * First pass selects the heuristic types that list this file's | |||
868 | * extension, as these are most likely to be the correct choice | |||
869 | * for this file. | |||
870 | * | |||
871 | * Second pass selects heuristic types which have no | |||
872 | * extensions. We try those before the ones that have | |||
873 | * extensions that *don't* match this file's extension, on the | |||
874 | * theory that files of those types generally have one of the | |||
875 | * type's extensions, and, as this file *doesn't* have one of | |||
876 | * those extensions, it's probably *not* one of those files. | |||
877 | * | |||
878 | * Third pass selects heuristic types which support extensions | |||
879 | * but where none of them matches this file's extension. | |||
880 | * | |||
881 | * We need only continue searching until we find a match or an | |||
882 | * error occurs. | |||
883 | */ | |||
884 | ||||
885 | for (pass = 0; pass < 3 && result == WTAP_OPEN_NOT_MINE; pass++) { | |||
886 | for (i = heuristic_open_routine_idx; i < open_info_arr->len && result == WTAP_OPEN_NOT_MINE; i++) { | |||
887 | if ( (pass == 0 && heuristic_uses_extension(i, extension)) | |||
888 | || (pass == 1 && open_routines[i].extensions == NULL((void*)0)) | |||
889 | || (pass == 2 && open_routines[i].extensions != NULL((void*)0) | |||
890 | && !heuristic_uses_extension(i, extension))) { | |||
891 | result = try_one_open(wth, &open_routines[i], err, err_info); | |||
892 | } | |||
893 | } | |||
894 | } | |||
895 | ||||
896 | g_free(extension); | |||
897 | } else { | |||
898 | /* No extension. Try all the heuristic types in order. */ | |||
899 | for (i = heuristic_open_routine_idx; i < open_info_arr->len && result == WTAP_OPEN_NOT_MINE; i++) { | |||
900 | result = try_one_open(wth, &open_routines[i], err, err_info); | |||
901 | } | |||
902 | } | |||
903 | ||||
904 | return result; | |||
905 | } | |||
906 | ||||
907 | /* Opens a file and prepares a wtap struct. | |||
908 | * If "do_random" is true, it opens the file twice; the second open | |||
909 | * allows the application to do random-access I/O without moving | |||
910 | * the seek offset for sequential I/O, which is used by Wireshark | |||
911 | * so that it can do sequential I/O to a capture file that's being | |||
912 | * written to as new packets arrive independently of random I/O done | |||
913 | * to display protocol trees for packets when they're selected. | |||
914 | */ | |||
915 | wtap * | |||
916 | wtap_open_offline(const char *filename, unsigned int type, int *err, char **err_info, | |||
917 | bool_Bool do_random) | |||
918 | { | |||
919 | int fd; | |||
920 | ws_statb64struct stat statb; | |||
921 | bool_Bool ispipe = false0; | |||
922 | wtap *wth; | |||
923 | bool_Bool use_stdin = false0; | |||
924 | wtap_block_t shb; | |||
925 | ||||
926 | *err = 0; | |||
927 | *err_info = NULL((void*)0); | |||
928 | ||||
929 | /* open standard input if filename is '-' */ | |||
930 | if (strcmp(filename, "-") == 0) | |||
931 | use_stdin = true1; | |||
932 | ||||
933 | /* First, make sure the file is valid */ | |||
934 | if (use_stdin) { | |||
935 | if (ws_fstat64fstat(0, &statb) < 0) { | |||
936 | *err = errno(*__errno_location ()); | |||
937 | return NULL((void*)0); | |||
938 | } | |||
939 | } else { | |||
940 | if (ws_stat64stat(filename, &statb) < 0) { | |||
941 | *err = errno(*__errno_location ()); | |||
942 | return NULL((void*)0); | |||
943 | } | |||
944 | } | |||
945 | if (S_ISFIFO(statb.st_mode)((((statb.st_mode)) & 0170000) == (0010000))) { | |||
946 | /* | |||
947 | * Opens of FIFOs are allowed only when not opening | |||
948 | * for random access. | |||
949 | * | |||
950 | * Currently, we do seeking when trying to find out | |||
951 | * the file type, but our I/O routines do some amount | |||
952 | * of buffering, and do backward seeks within the buffer | |||
953 | * if possible, so at least some file types can be | |||
954 | * opened from pipes, so we don't completely disallow opens | |||
955 | * of pipes. | |||
956 | */ | |||
957 | if (do_random) { | |||
958 | *err = WTAP_ERR_RANDOM_OPEN_PIPE-2; | |||
959 | return NULL((void*)0); | |||
960 | } | |||
961 | ispipe = true1; | |||
962 | } else if (S_ISDIR(statb.st_mode)((((statb.st_mode)) & 0170000) == (0040000))) { | |||
963 | /* | |||
964 | * Return different errors for "this is a directory" | |||
965 | * and "this is some random special file type", so | |||
966 | * the user can get a potentially more helpful error. | |||
967 | */ | |||
968 | *err = EISDIR21; | |||
969 | return NULL((void*)0); | |||
970 | } else if (! S_ISREG(statb.st_mode)((((statb.st_mode)) & 0170000) == (0100000))) { | |||
971 | *err = WTAP_ERR_NOT_REGULAR_FILE-1; | |||
972 | return NULL((void*)0); | |||
973 | } | |||
974 | ||||
975 | /* | |||
976 | * We need two independent descriptors for random access, so | |||
977 | * they have different file positions. If we're opening the | |||
978 | * standard input, we can only dup it to get additional | |||
979 | * descriptors, so we can't have two independent descriptors, | |||
980 | * and thus can't do random access. | |||
981 | */ | |||
982 | if (use_stdin && do_random) { | |||
983 | *err = WTAP_ERR_RANDOM_OPEN_STDIN-16; | |||
984 | return NULL((void*)0); | |||
985 | } | |||
986 | ||||
987 | errno(*__errno_location ()) = ENOMEM12; | |||
988 | wth = g_new0(wtap, 1)((wtap *) g_malloc0_n ((1), sizeof (wtap))); | |||
989 | ||||
990 | /* Open the file */ | |||
991 | errno(*__errno_location ()) = WTAP_ERR_CANT_OPEN-6; | |||
992 | if (use_stdin) { | |||
993 | /* | |||
994 | * We dup FD 0, so that we don't have to worry about | |||
995 | * a file_close of wth->fh closing the standard | |||
996 | * input of the process. | |||
997 | */ | |||
998 | fd = ws_dupdup(0); | |||
999 | if (fd < 0) { | |||
1000 | *err = errno(*__errno_location ()); | |||
1001 | g_free(wth); | |||
1002 | return NULL((void*)0); | |||
1003 | } | |||
1004 | #ifdef _WIN32 | |||
1005 | if (_setmode(fd, O_BINARY0) == -1) { | |||
1006 | /* "Shouldn't happen" */ | |||
1007 | *err = errno(*__errno_location ()); | |||
1008 | g_free(wth); | |||
1009 | return NULL((void*)0); | |||
1010 | } | |||
1011 | #endif | |||
1012 | if (!(wth->fh = file_fdopen(fd))) { | |||
1013 | *err = errno(*__errno_location ()); | |||
1014 | ws_closeclose(fd); | |||
1015 | g_free(wth); | |||
1016 | return NULL((void*)0); | |||
1017 | } | |||
1018 | } else { | |||
1019 | if (!(wth->fh = file_open(filename))) { | |||
1020 | *err = errno(*__errno_location ()); | |||
1021 | g_free(wth); | |||
1022 | return NULL((void*)0); | |||
1023 | } | |||
1024 | } | |||
1025 | ||||
1026 | if (do_random) { | |||
1027 | if (!(wth->random_fh = file_open(filename))) { | |||
1028 | *err = errno(*__errno_location ()); | |||
1029 | file_close(wth->fh); | |||
1030 | g_free(wth); | |||
1031 | return NULL((void*)0); | |||
1032 | } | |||
1033 | } else | |||
1034 | wth->random_fh = NULL((void*)0); | |||
1035 | ||||
1036 | /* initialization */ | |||
1037 | wth->ispipe = ispipe; | |||
1038 | wth->file_encap = WTAP_ENCAP_UNKNOWN0; | |||
1039 | wth->subtype_sequential_close = NULL((void*)0); | |||
1040 | wth->subtype_close = NULL((void*)0); | |||
1041 | wth->file_tsprec = WTAP_TSPREC_USEC6; | |||
1042 | wth->pathname = g_strdup(filename)g_strdup_inline (filename); | |||
1043 | wth->priv = NULL((void*)0); | |||
1044 | wth->wslua_data = NULL((void*)0); | |||
1045 | wth->shb_hdrs = g_array_new(false0, false0, sizeof(wtap_block_t)); | |||
1046 | shb = wtap_block_create(WTAP_BLOCK_SECTION); | |||
1047 | if (shb) | |||
1048 | g_array_append_val(wth->shb_hdrs, shb)g_array_append_vals (wth->shb_hdrs, &(shb), 1); | |||
1049 | ||||
1050 | /* Initialize the array containing a list of interfaces. pcapng_open and | |||
1051 | * erf_open needs this (and libpcap_open for ERF encapsulation types). | |||
1052 | * Always initing it here saves checking for a NULL ptr later. */ | |||
1053 | wth->interface_data = g_array_new(false0, false0, sizeof(wtap_block_t)); | |||
1054 | /* | |||
1055 | * Next interface data that wtap_get_next_interface_description() | |||
1056 | * will return. | |||
1057 | */ | |||
1058 | wth->next_interface_data = 0; | |||
1059 | ||||
1060 | wth->shb_iface_to_global = g_array_new(false0, false0, sizeof(unsigned)); | |||
1061 | g_array_append_val(wth->shb_iface_to_global, wth->interface_data->len)g_array_append_vals (wth->shb_iface_to_global, &(wth-> interface_data->len), 1); | |||
1062 | ||||
1063 | if (wth->random_fh) { | |||
1064 | wth->fast_seek = g_ptr_array_new(); | |||
1065 | ||||
1066 | file_set_random_access(wth->fh, false0, wth->fast_seek); | |||
1067 | file_set_random_access(wth->random_fh, true1, wth->fast_seek); | |||
1068 | } | |||
1069 | ||||
1070 | /* Find a file format handler which can read the file. */ | |||
1071 | switch (try_open(wth, type, err, err_info)) { | |||
1072 | case WTAP_OPEN_NOT_MINE: | |||
1073 | /* Well, it's not one of the types of file we know about. */ | |||
1074 | *err = WTAP_ERR_FILE_UNKNOWN_FORMAT-3; | |||
1075 | /* FALLTHROUGH */ | |||
1076 | ||||
1077 | case WTAP_OPEN_ERROR: | |||
1078 | wtap_close(wth); | |||
1079 | wth = NULL((void*)0); | |||
1080 | } | |||
1081 | ||||
1082 | return wth; | |||
1083 | } | |||
1084 | ||||
1085 | /* | |||
1086 | * Given the pathname of the file we just closed with wtap_fdclose(), attempt | |||
1087 | * to reopen that file and assign the new file descriptor(s) to the sequential | |||
1088 | * stream and, if do_random is true, to the random stream. Used on Windows | |||
1089 | * after the rename of a file we had open was done or if the rename of a | |||
1090 | * file on top of a file we had open failed. | |||
1091 | * | |||
1092 | * This is only required by Wireshark, not TShark, and, at the point that | |||
1093 | * Wireshark is doing this, the sequential stream is closed, and the | |||
1094 | * random stream is open, so this refuses to open pipes, and only | |||
1095 | * reopens the random stream. | |||
1096 | */ | |||
1097 | bool_Bool | |||
1098 | wtap_fdreopen(wtap *wth, const char *filename, int *err) | |||
1099 | { | |||
1100 | ws_statb64struct stat statb; | |||
1101 | ||||
1102 | /* | |||
1103 | * We need two independent descriptors for random access, so | |||
1104 | * they have different file positions. If we're opening the | |||
1105 | * standard input, we can only dup it to get additional | |||
1106 | * descriptors, so we can't have two independent descriptors, | |||
1107 | * and thus can't do random access. | |||
1108 | */ | |||
1109 | if (strcmp(filename, "-") == 0) { | |||
1110 | *err = WTAP_ERR_RANDOM_OPEN_STDIN-16; | |||
1111 | return false0; | |||
1112 | } | |||
1113 | ||||
1114 | /* First, make sure the file is valid */ | |||
1115 | if (ws_stat64stat(filename, &statb) < 0) { | |||
1116 | *err = errno(*__errno_location ()); | |||
1117 | return false0; | |||
1118 | } | |||
1119 | if (S_ISFIFO(statb.st_mode)((((statb.st_mode)) & 0170000) == (0010000))) { | |||
1120 | /* | |||
1121 | * Opens of FIFOs are not allowed; see above. | |||
1122 | */ | |||
1123 | *err = WTAP_ERR_RANDOM_OPEN_PIPE-2; | |||
1124 | return false0; | |||
1125 | } else if (S_ISDIR(statb.st_mode)((((statb.st_mode)) & 0170000) == (0040000))) { | |||
1126 | /* | |||
1127 | * Return different errors for "this is a directory" | |||
1128 | * and "this is some random special file type", so | |||
1129 | * the user can get a potentially more helpful error. | |||
1130 | */ | |||
1131 | *err = EISDIR21; | |||
1132 | return false0; | |||
1133 | } else if (! S_ISREG(statb.st_mode)((((statb.st_mode)) & 0170000) == (0100000))) { | |||
1134 | *err = WTAP_ERR_NOT_REGULAR_FILE-1; | |||
1135 | return false0; | |||
1136 | } | |||
1137 | ||||
1138 | /* Open the file */ | |||
1139 | errno(*__errno_location ()) = WTAP_ERR_CANT_OPEN-6; | |||
1140 | if (!file_fdreopen(wth->random_fh, filename)) { | |||
1141 | *err = errno(*__errno_location ()); | |||
1142 | return false0; | |||
1143 | } | |||
1144 | if (strcmp(filename, wth->pathname) != 0) { | |||
1145 | g_free(wth->pathname); | |||
1146 | wth->pathname = g_strdup(filename)g_strdup_inline (filename); | |||
1147 | } | |||
1148 | return true1; | |||
1149 | } | |||
1150 | ||||
1151 | /* Table of the file types and subtypes for which we have support. */ | |||
1152 | ||||
1153 | /* | |||
1154 | * Pointer to the GArray holding the registered file types. | |||
1155 | */ | |||
1156 | static GArray* file_type_subtype_table_arr; | |||
1157 | ||||
1158 | /* | |||
1159 | * Pointer to the table of registered file types in that GArray. | |||
1160 | */ | |||
1161 | static const struct file_type_subtype_info* file_type_subtype_table; | |||
1162 | ||||
1163 | /* | |||
1164 | * Number of elements in the table for builtin file types/subtypes. | |||
1165 | */ | |||
1166 | static unsigned wtap_num_builtin_file_types_subtypes; | |||
1167 | ||||
1168 | /* | |||
1169 | * Required builtin types. | |||
1170 | */ | |||
1171 | int pcap_file_type_subtype = -1; | |||
1172 | int pcap_nsec_file_type_subtype = -1; | |||
1173 | int pcapng_file_type_subtype = -1; | |||
1174 | ||||
1175 | /* | |||
1176 | * Table for mapping old file type/subtype names to new ones for | |||
1177 | * backwards compatibility. | |||
1178 | */ | |||
1179 | static GHashTable *type_subtype_name_map; | |||
1180 | ||||
1181 | /* | |||
1182 | * Initialize the table of file types/subtypes with all the builtin | |||
1183 | * types/subtypes. | |||
1184 | */ | |||
1185 | void | |||
1186 | wtap_init_file_type_subtypes(void) | |||
1187 | { | |||
1188 | /* Don't do this twice. */ | |||
1189 | ws_assert(file_type_subtype_table_arr == NULL)do { if ((1) && !(file_type_subtype_table_arr == ((void *)0))) ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 1189, __func__, "assertion failed: %s", "file_type_subtype_table_arr == ((void*)0)" ); } while (0); | |||
1190 | ||||
1191 | /* | |||
1192 | * Estimate the number of file types/subtypes as twice the | |||
1193 | * number of modules; that's probably an overestimate, as | |||
1194 | * the average number of file types/subtypes registered by | |||
1195 | * a module is > 1 but probably < 2, but that shouldn't | |||
1196 | * waste too much memory. | |||
1197 | * | |||
1198 | * Add on 7 more for pcapng, pcap, nanosecond pcap, and the | |||
1199 | * extra modified flavors of pcap. | |||
1200 | */ | |||
1201 | file_type_subtype_table_arr = g_array_sized_new(false0, true1, | |||
1202 | sizeof(struct file_type_subtype_info), wtap_module_count*2 + 7); | |||
1203 | file_type_subtype_table = (const struct file_type_subtype_info*)(void *)file_type_subtype_table_arr->data; | |||
1204 | ||||
1205 | /* | |||
1206 | * Initialize the hash table for mapping old file type/subtype | |||
1207 | * names to the corresponding new names. | |||
1208 | */ | |||
1209 | type_subtype_name_map = g_hash_table_new_full(g_str_hash, | |||
1210 | g_str_equal, g_free, g_free); | |||
1211 | ||||
1212 | /* No entries yet, so no builtin entries yet. */ | |||
1213 | wtap_num_builtin_file_types_subtypes = 0; | |||
1214 | ||||
1215 | /* | |||
1216 | * Register the builtin entries that aren't in the table. | |||
1217 | * First, do the required ones; register pcapng first, then | |||
1218 | * pcap, so, at the beginning of the table, we have pcapng, | |||
1219 | * pcap, nanosecond pcap, and the weird modified pcaps, so | |||
1220 | * searches for file types that can write a file format | |||
1221 | * start with pcapng, pcap, and nanosecond pcap. | |||
1222 | */ | |||
1223 | register_pcapng(); | |||
1224 | register_pcap(); | |||
1225 | ||||
1226 | /* Now register the ones found by the build process */ | |||
1227 | for (unsigned i = 0; i < wtap_module_count; i++) | |||
1228 | wtap_module_reg[i].cb_func(); | |||
1229 | ||||
1230 | /* Update the number of builtin entries. */ | |||
1231 | wtap_num_builtin_file_types_subtypes = file_type_subtype_table_arr->len; | |||
1232 | } | |||
1233 | ||||
1234 | /* | |||
1235 | * Attempt to register a new file type/subtype; fails if a type/subtype | |||
1236 | * with that name is already registered. | |||
1237 | */ | |||
1238 | int | |||
1239 | wtap_register_file_type_subtype(const struct file_type_subtype_info* fi) | |||
1240 | { | |||
1241 | struct file_type_subtype_info* finfo; | |||
1242 | unsigned file_type_subtype; | |||
1243 | ||||
1244 | /* | |||
1245 | * Check for required fields (description and name). | |||
1246 | */ | |||
1247 | if (!fi || !fi->description || !fi->name) { | |||
1248 | ws_warning("no file type info")do { if (1) { ws_log_full("Wiretap", LOG_LEVEL_WARNING, "wiretap/file_access.c" , 1248, __func__, "no file type info"); } } while (0); | |||
1249 | return -1; | |||
1250 | } | |||
1251 | ||||
1252 | /* | |||
1253 | * There must be at least one block type that this file | |||
1254 | * type/subtype supports. | |||
1255 | */ | |||
1256 | if (fi->num_supported_blocks == 0 || fi->supported_blocks == NULL((void*)0)) { | |||
1257 | ws_warning("no blocks supported by file type \"%s\"", fi->name)do { if (1) { ws_log_full("Wiretap", LOG_LEVEL_WARNING, "wiretap/file_access.c" , 1257, __func__, "no blocks supported by file type \"%s\"", fi ->name); } } while (0); | |||
1258 | return -1; | |||
1259 | } | |||
1260 | ||||
1261 | /* | |||
1262 | * Is this type already registered? | |||
1263 | */ | |||
1264 | if (wtap_name_to_file_type_subtype(fi->name) != -1) { | |||
1265 | /* | |||
1266 | * Yes. You don't get to replace an existing handler. | |||
1267 | */ | |||
1268 | ws_warning("file type \"%s\" is already registered", fi->name)do { if (1) { ws_log_full("Wiretap", LOG_LEVEL_WARNING, "wiretap/file_access.c" , 1268, __func__, "file type \"%s\" is already registered", fi ->name); } } while (0); | |||
1269 | return -1; | |||
1270 | } | |||
1271 | ||||
1272 | /* | |||
1273 | * Is there a freed entry in the array, due to a file type | |||
1274 | * being de-registered? | |||
1275 | * | |||
1276 | * Skip the built-in entries, as they're never deregistered. | |||
1277 | */ | |||
1278 | for (file_type_subtype = wtap_num_builtin_file_types_subtypes; | |||
1279 | file_type_subtype < file_type_subtype_table_arr->len; | |||
1280 | file_type_subtype++) { | |||
1281 | if (file_type_subtype_table[file_type_subtype].name == NULL((void*)0)) { | |||
1282 | /* | |||
1283 | * We found such an entry. | |||
1284 | * | |||
1285 | * Get the pointer from the GArray, so that we get a | |||
1286 | * non-const pointer. | |||
1287 | */ | |||
1288 | finfo = &g_array_index(file_type_subtype_table_arr, struct file_type_subtype_info, file_type_subtype)(((struct file_type_subtype_info*) (void *) (file_type_subtype_table_arr )->data) [(file_type_subtype)]); | |||
1289 | ||||
1290 | /* | |||
1291 | * Fill in the entry with the new values. | |||
1292 | */ | |||
1293 | *finfo = *fi; | |||
1294 | ||||
1295 | return (int)file_type_subtype; | |||
1296 | } | |||
1297 | } | |||
1298 | ||||
1299 | /* | |||
1300 | * There aren't any free slots, so add a new entry. | |||
1301 | * Get the number of current number of entries, which will | |||
1302 | * be the index of the new entry, then append this entry | |||
1303 | * to the end of the array, change file_type_subtype_table | |||
1304 | * in case the array had to get reallocated, and return | |||
1305 | * the index of the new entry. | |||
1306 | */ | |||
1307 | file_type_subtype = file_type_subtype_table_arr->len; | |||
1308 | g_array_append_val(file_type_subtype_table_arr, *fi)g_array_append_vals (file_type_subtype_table_arr, &(*fi), 1); | |||
1309 | file_type_subtype_table = (const struct file_type_subtype_info*)(void *)file_type_subtype_table_arr->data; | |||
1310 | return file_type_subtype; | |||
1311 | } | |||
1312 | ||||
1313 | /* De-registers a file writer - they can never be removed from the GArray, but we can "clear" an entry. | |||
1314 | */ | |||
1315 | void | |||
1316 | wtap_deregister_file_type_subtype(const int subtype) | |||
1317 | { | |||
1318 | struct file_type_subtype_info* finfo; | |||
1319 | ||||
1320 | if (subtype < 0 || subtype >= (int)file_type_subtype_table_arr->len) { | |||
1321 | ws_error("invalid file type to de-register")ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 1321, __func__, "invalid file type to de-register"); | |||
1322 | return; | |||
1323 | } | |||
1324 | if ((unsigned)subtype < wtap_num_builtin_file_types_subtypes) { | |||
1325 | ws_error("built-in file types cannot be de-registered")ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 1325, __func__, "built-in file types cannot be de-registered" ); | |||
1326 | return; | |||
1327 | } | |||
1328 | ||||
1329 | /* | |||
1330 | * Get the pointer from the GArray, so that we get a non-const | |||
1331 | * pointer. | |||
1332 | */ | |||
1333 | finfo = &g_array_index(file_type_subtype_table_arr, struct file_type_subtype_info, subtype)(((struct file_type_subtype_info*) (void *) (file_type_subtype_table_arr )->data) [(subtype)]); | |||
1334 | /* | |||
1335 | * Clear out this entry. | |||
1336 | */ | |||
1337 | finfo->description = NULL((void*)0); | |||
1338 | finfo->name = NULL((void*)0); | |||
1339 | finfo->default_file_extension = NULL((void*)0); | |||
1340 | finfo->additional_file_extensions = NULL((void*)0); | |||
1341 | finfo->writing_must_seek = false0; | |||
1342 | finfo->num_supported_blocks = 0; | |||
1343 | finfo->supported_blocks = NULL((void*)0); | |||
1344 | finfo->can_write_encap = NULL((void*)0); | |||
1345 | finfo->dump_open = NULL((void*)0); | |||
1346 | finfo->wslua_info = NULL((void*)0); | |||
1347 | } | |||
1348 | ||||
1349 | /* | |||
1350 | * Given a GArray of WTAP_ENCAP_ types, return the per-file encapsulation | |||
1351 | * type that would be needed to write out a file with those types. If | |||
1352 | * there's only one type, it's that type, otherwise it's | |||
1353 | * WTAP_ENCAP_PER_PACKET. | |||
1354 | */ | |||
1355 | int | |||
1356 | wtap_dump_required_file_encap_type(const GArray *file_encaps) | |||
1357 | { | |||
1358 | int encap; | |||
1359 | ||||
1360 | encap = WTAP_ENCAP_PER_PACKET-1; | |||
1361 | if (file_encaps->len == 1) { | |||
1362 | /* OK, use the one-and-only encapsulation type. */ | |||
1363 | encap = g_array_index(file_encaps, int, 0)(((int*) (void *) (file_encaps)->data) [(0)]); | |||
1364 | } | |||
1365 | return encap; | |||
1366 | } | |||
1367 | ||||
1368 | bool_Bool | |||
1369 | wtap_dump_can_write_encap(int file_type_subtype, int encap) | |||
1370 | { | |||
1371 | int result = 0; | |||
1372 | ||||
1373 | if (file_type_subtype < 0 || | |||
1374 | file_type_subtype >= (int)file_type_subtype_table_arr->len || | |||
1375 | file_type_subtype_table[file_type_subtype].can_write_encap == NULL((void*)0)) | |||
1376 | return false0; | |||
1377 | ||||
1378 | result = (*file_type_subtype_table[file_type_subtype].can_write_encap)(encap); | |||
1379 | ||||
1380 | if (result != 0) { | |||
1381 | /* if the err said to check wslua's can_write_encap, try that */ | |||
1382 | if (result == WTAP_ERR_CHECK_WSLUA-23 | |||
1383 | && file_type_subtype_table[file_type_subtype].wslua_info != NULL((void*)0) | |||
1384 | && file_type_subtype_table[file_type_subtype].wslua_info->wslua_can_write_encap != NULL((void*)0)) { | |||
1385 | ||||
1386 | result = (*file_type_subtype_table[file_type_subtype].wslua_info->wslua_can_write_encap)(encap, file_type_subtype_table[file_type_subtype].wslua_info->wslua_data); | |||
1387 | ||||
1388 | } | |||
1389 | ||||
1390 | if (result != 0) | |||
1391 | return false0; | |||
1392 | } | |||
1393 | ||||
1394 | return true1; | |||
1395 | } | |||
1396 | ||||
1397 | /* | |||
1398 | * Return true if a capture with a given GArray of encapsulation types | |||
1399 | * and a given bitset of comment types can be written in a specified | |||
1400 | * format, and false if it can't. | |||
1401 | */ | |||
1402 | static bool_Bool | |||
1403 | wtap_dump_can_write_format(int ft, const GArray *file_encaps, | |||
1404 | uint32_t required_comment_types) | |||
1405 | { | |||
1406 | unsigned i; | |||
1407 | ||||
1408 | /* | |||
1409 | * Can we write in this format? | |||
1410 | */ | |||
1411 | if (!wtap_dump_can_open(ft)) { | |||
1412 | /* No. */ | |||
1413 | return false0; | |||
1414 | } | |||
1415 | ||||
1416 | /* | |||
1417 | * Yes. Can we write out all the required comments in this | |||
1418 | * format? | |||
1419 | */ | |||
1420 | if (required_comment_types & WTAP_COMMENT_PER_SECTION0x00000001) { | |||
1421 | if (wtap_file_type_subtype_supports_option(ft, | |||
1422 | WTAP_BLOCK_SECTION, OPT_COMMENT1) == OPTION_NOT_SUPPORTED) { | |||
1423 | /* Not section comments. */ | |||
1424 | return false0; | |||
1425 | } | |||
1426 | } | |||
1427 | if (required_comment_types & WTAP_COMMENT_PER_INTERFACE0x00000002) { | |||
1428 | if (wtap_file_type_subtype_supports_option(ft, | |||
1429 | WTAP_BLOCK_IF_ID_AND_INFO, OPT_COMMENT1) == OPTION_NOT_SUPPORTED) { | |||
1430 | /* Not interface comments. */ | |||
1431 | return false0; | |||
1432 | } | |||
1433 | } | |||
1434 | if (required_comment_types & WTAP_COMMENT_PER_PACKET0x00000004) { | |||
1435 | if (wtap_file_type_subtype_supports_option(ft, | |||
1436 | WTAP_BLOCK_PACKET, OPT_COMMENT1) == OPTION_NOT_SUPPORTED) { | |||
1437 | /* Not packet comments. */ | |||
1438 | return false0; | |||
1439 | } | |||
1440 | } | |||
1441 | ||||
1442 | /* | |||
1443 | * Yes. Is the required per-file encapsulation type supported? | |||
1444 | * This might be WTAP_ENCAP_PER_PACKET. | |||
1445 | */ | |||
1446 | if (!wtap_dump_can_write_encap(ft, wtap_dump_required_file_encap_type(file_encaps))) { | |||
1447 | /* No. */ | |||
1448 | return false0; | |||
1449 | } | |||
1450 | ||||
1451 | /* | |||
1452 | * Yes. Are all the individual encapsulation types supported? | |||
1453 | */ | |||
1454 | for (i = 0; i < file_encaps->len; i++) { | |||
1455 | if (!wtap_dump_can_write_encap(ft, | |||
1456 | g_array_index(file_encaps, int, i)(((int*) (void *) (file_encaps)->data) [(i)]))) { | |||
1457 | /* No - one of them isn't. */ | |||
1458 | return false0; | |||
1459 | } | |||
1460 | } | |||
1461 | ||||
1462 | /* Yes - we're OK. */ | |||
1463 | return true1; | |||
1464 | } | |||
1465 | ||||
1466 | /* | |||
1467 | * Return true if we can write a file with the given GArray of | |||
1468 | * encapsulation types and the given bitmask of comment types. | |||
1469 | */ | |||
1470 | bool_Bool | |||
1471 | wtap_dump_can_write(const GArray *file_encaps, uint32_t required_comment_types) | |||
1472 | { | |||
1473 | int ft; | |||
1474 | ||||
1475 | for (ft = 0; ft < (int)file_type_subtype_table_arr->len; ft++) { | |||
1476 | /* To save a file with Wiretap, Wiretap has to handle that format, | |||
1477 | * and its code to handle that format must be able to write a file | |||
1478 | * with this file's encapsulation types. | |||
1479 | */ | |||
1480 | if (wtap_dump_can_write_format(ft, file_encaps, required_comment_types)) { | |||
1481 | /* OK, we can write it out in this type. */ | |||
1482 | return true1; | |||
1483 | } | |||
1484 | } | |||
1485 | ||||
1486 | /* No, we couldn't save it in any format. */ | |||
1487 | return false0; | |||
1488 | } | |||
1489 | ||||
1490 | /* | |||
1491 | * Sort by file type/subtype name. | |||
1492 | */ | |||
1493 | static int | |||
1494 | compare_file_type_subtypes_by_name(const void *a, const void *b) | |||
1495 | { | |||
1496 | int file_type_subtype_a = *(const int *)a; | |||
1497 | int file_type_subtype_b = *(const int *)b; | |||
1498 | ||||
1499 | return strcmp(wtap_file_type_subtype_name(file_type_subtype_a), | |||
1500 | wtap_file_type_subtype_name(file_type_subtype_b)); | |||
1501 | } | |||
1502 | ||||
1503 | /* | |||
1504 | * Sort by file type/subtype description. | |||
1505 | */ | |||
1506 | static int | |||
1507 | compare_file_type_subtypes_by_description(const void *a, const void *b) | |||
1508 | { | |||
1509 | int file_type_subtype_a = *(const int *)a; | |||
1510 | int file_type_subtype_b = *(const int *)b; | |||
1511 | ||||
1512 | return strcmp(wtap_file_type_subtype_description(file_type_subtype_a), | |||
1513 | wtap_file_type_subtype_description(file_type_subtype_b)); | |||
1514 | } | |||
1515 | ||||
1516 | /* | |||
1517 | * Get a GArray of file type/subtype values for file types/subtypes | |||
1518 | * that can be used to save a file of a given type/subtype with a given | |||
1519 | * GArray of encapsulation types and the given bitmask of comment types. | |||
1520 | */ | |||
1521 | GArray * | |||
1522 | wtap_get_savable_file_types_subtypes_for_file(int file_type_subtype, | |||
1523 | const GArray *file_encaps, uint32_t required_comment_types, | |||
1524 | ft_sort_order sort_order) | |||
1525 | { | |||
1526 | GArray *savable_file_types_subtypes; | |||
1527 | int ft; | |||
1528 | int default_file_type_subtype = -1; | |||
1529 | int other_file_type_subtype = -1; | |||
1530 | ||||
1531 | /* Can we save this file in its own file type/subtype? */ | |||
1532 | if (wtap_dump_can_write_format(file_type_subtype, file_encaps, | |||
1533 | required_comment_types)) { | |||
1534 | /* Yes - make that the default file type/subtype. */ | |||
1535 | default_file_type_subtype = file_type_subtype; | |||
1536 | } else if (wtap_dump_can_write_format(pcap_file_type_subtype, | |||
1537 | file_encaps, | |||
1538 | required_comment_types)) { | |||
1539 | /* | |||
1540 | * No, but we can write it as a pcap file; make that | |||
1541 | * the default file type/subtype. | |||
1542 | */ | |||
1543 | default_file_type_subtype = pcap_file_type_subtype; | |||
1544 | } else if (wtap_dump_can_write_format(pcapng_file_type_subtype, | |||
1545 | file_encaps, | |||
1546 | required_comment_types)) { | |||
1547 | /* | |||
1548 | * No, but we can write it as a pcapng file; make that | |||
1549 | * the default file type/subtype. | |||
1550 | */ | |||
1551 | default_file_type_subtype = pcapng_file_type_subtype; | |||
1552 | } else { | |||
1553 | /* OK, find the first file type/subtype we *can* save it as. */ | |||
1554 | default_file_type_subtype = -1; | |||
1555 | for (ft = 0; ft < (int)file_type_subtype_table_arr->len; ft++) { | |||
1556 | if (wtap_dump_can_write_format(ft, file_encaps, | |||
1557 | required_comment_types)) { | |||
1558 | /* OK, got it. */ | |||
1559 | default_file_type_subtype = ft; | |||
1560 | break; | |||
1561 | } | |||
1562 | } | |||
1563 | } | |||
1564 | ||||
1565 | if (default_file_type_subtype == -1) { | |||
1566 | /* We don't support writing this file as any file type/subtype. */ | |||
1567 | return NULL((void*)0); | |||
1568 | } | |||
1569 | ||||
1570 | /* | |||
1571 | * If the default is pcap, put pcapng right after it if we can | |||
1572 | * also write it in pcapng format; otherwise, if the default is | |||
1573 | * pcapng, put pcap right after it if we can also write it in | |||
1574 | * pcap format. | |||
1575 | */ | |||
1576 | if (default_file_type_subtype == pcap_file_type_subtype) { | |||
1577 | if (wtap_dump_can_write_format(pcapng_file_type_subtype, | |||
1578 | file_encaps, | |||
1579 | required_comment_types)) | |||
1580 | other_file_type_subtype = pcapng_file_type_subtype; | |||
1581 | } else if (default_file_type_subtype == pcapng_file_type_subtype) { | |||
1582 | if (wtap_dump_can_write_format(pcap_file_type_subtype, | |||
1583 | file_encaps, | |||
1584 | required_comment_types)) | |||
1585 | other_file_type_subtype = pcap_file_type_subtype; | |||
1586 | } | |||
1587 | ||||
1588 | /* Allocate the array. */ | |||
1589 | savable_file_types_subtypes = g_array_new(false0, false0, | |||
1590 | sizeof (int)); | |||
1591 | ||||
1592 | /* | |||
1593 | * First, add the types we don't want to force to the | |||
1594 | * beginning of the list. | |||
1595 | */ | |||
1596 | for (ft = 0; ft < (int)file_type_subtype_table_arr->len; ft++) { | |||
1597 | if (ft == default_file_type_subtype || | |||
1598 | ft == other_file_type_subtype) | |||
1599 | continue; /* we will done this one later */ | |||
1600 | if (wtap_dump_can_write_format(ft, file_encaps, | |||
1601 | required_comment_types)) { | |||
1602 | /* OK, we can write it out in this type. */ | |||
1603 | g_array_append_val(savable_file_types_subtypes, ft)g_array_append_vals (savable_file_types_subtypes, &(ft), 1 ); | |||
1604 | } | |||
1605 | } | |||
1606 | ||||
1607 | /* Now, sort the list. */ | |||
1608 | g_array_sort(savable_file_types_subtypes, | |||
1609 | (sort_order == FT_SORT_BY_NAME) ? compare_file_type_subtypes_by_name : | |||
1610 | compare_file_type_subtypes_by_description); | |||
1611 | ||||
1612 | /* | |||
1613 | * If we have a type/subtype to put above the default one, | |||
1614 | * do so. | |||
1615 | * | |||
1616 | * We put this type at the beginning before putting the | |||
1617 | * default there, so the default is at the top. | |||
1618 | */ | |||
1619 | if (other_file_type_subtype != -1) | |||
1620 | g_array_prepend_val(savable_file_types_subtypes,g_array_prepend_vals (savable_file_types_subtypes, &(other_file_type_subtype ), 1) | |||
1621 | other_file_type_subtype)g_array_prepend_vals (savable_file_types_subtypes, &(other_file_type_subtype ), 1); | |||
1622 | ||||
1623 | /* Put the default file type/subtype first in the list. */ | |||
1624 | g_array_prepend_val(savable_file_types_subtypes,g_array_prepend_vals (savable_file_types_subtypes, &(default_file_type_subtype ), 1) | |||
1625 | default_file_type_subtype)g_array_prepend_vals (savable_file_types_subtypes, &(default_file_type_subtype ), 1); | |||
1626 | ||||
1627 | return savable_file_types_subtypes; | |||
1628 | } | |||
1629 | ||||
1630 | /* | |||
1631 | * Get a GArray of all writable file type/subtype values. | |||
1632 | */ | |||
1633 | GArray * | |||
1634 | wtap_get_writable_file_types_subtypes(ft_sort_order sort_order) | |||
1635 | { | |||
1636 | GArray *writable_file_types_subtypes; | |||
1637 | int ft; | |||
1638 | ||||
1639 | /* | |||
1640 | * Allocate the array. | |||
1641 | * Pre-allocate room enough for all types. | |||
1642 | * XXX - that's overkill; just scan the table to find all the | |||
1643 | * writable types and count them. | |||
1644 | */ | |||
1645 | writable_file_types_subtypes = g_array_sized_new(false0, false0, | |||
1646 | sizeof (int), file_type_subtype_table_arr->len); | |||
1647 | ||||
1648 | /* | |||
1649 | * First, add the types we don't want to force to the | |||
1650 | * beginning of the list. | |||
1651 | */ | |||
1652 | for (ft = 0; ft < (int)file_type_subtype_table_arr->len; ft++) { | |||
1653 | if (ft == pcap_file_type_subtype || | |||
1654 | ft == pcapng_file_type_subtype) | |||
1655 | continue; /* we've already done these two */ | |||
1656 | if (wtap_dump_can_open(ft)) { | |||
1657 | /* OK, we can write this type. */ | |||
1658 | g_array_append_val(writable_file_types_subtypes, ft)g_array_append_vals (writable_file_types_subtypes, &(ft), 1); | |||
1659 | } | |||
1660 | } | |||
1661 | ||||
1662 | /* Now, sort the list. */ | |||
1663 | g_array_sort(writable_file_types_subtypes, | |||
1664 | (sort_order == FT_SORT_BY_NAME) ? compare_file_type_subtypes_by_name : | |||
1665 | compare_file_type_subtypes_by_description); | |||
1666 | ||||
1667 | /* | |||
1668 | * Now, put pcap and pcapng at the beginning, as they're | |||
1669 | * our "native" formats. Put pcapng there first, and | |||
1670 | * pcap before it. | |||
1671 | */ | |||
1672 | if (pcapng_file_type_subtype != -1 && | |||
1673 | wtap_dump_can_open(pcapng_file_type_subtype)) { | |||
1674 | /* | |||
1675 | * We can write pcapng. (If we can't, that's a huge | |||
1676 | * mistake.) | |||
1677 | */ | |||
1678 | g_array_prepend_val(writable_file_types_subtypes,g_array_prepend_vals (writable_file_types_subtypes, &(pcapng_file_type_subtype ), 1) | |||
1679 | pcapng_file_type_subtype)g_array_prepend_vals (writable_file_types_subtypes, &(pcapng_file_type_subtype ), 1); | |||
1680 | } | |||
1681 | if (pcap_file_type_subtype != -1 && | |||
1682 | wtap_dump_can_open(pcap_file_type_subtype)) { | |||
1683 | /* | |||
1684 | * We can write pcap. (If we can't, that's a huge | |||
1685 | * mistake.) | |||
1686 | */ | |||
1687 | g_array_prepend_val(writable_file_types_subtypes,g_array_prepend_vals (writable_file_types_subtypes, &(pcap_file_type_subtype ), 1) | |||
1688 | pcap_file_type_subtype)g_array_prepend_vals (writable_file_types_subtypes, &(pcap_file_type_subtype ), 1); | |||
1689 | } | |||
1690 | ||||
1691 | return writable_file_types_subtypes; | |||
1692 | } | |||
1693 | ||||
1694 | /* | |||
1695 | * String describing the file type/subtype. | |||
1696 | */ | |||
1697 | const char * | |||
1698 | wtap_file_type_subtype_description(int file_type_subtype) | |||
1699 | { | |||
1700 | if (file_type_subtype < 0 || | |||
1701 | file_type_subtype >= (int)file_type_subtype_table_arr->len) | |||
1702 | return NULL((void*)0); | |||
1703 | else | |||
1704 | return file_type_subtype_table[file_type_subtype].description; | |||
1705 | } | |||
1706 | ||||
1707 | /* | |||
1708 | * Name to use in, say, a command-line flag specifying the type/subtype. | |||
1709 | */ | |||
1710 | const char * | |||
1711 | wtap_file_type_subtype_name(int file_type_subtype) | |||
1712 | { | |||
1713 | if (file_type_subtype < 0 || | |||
1714 | file_type_subtype >= (int)file_type_subtype_table_arr->len) | |||
1715 | return NULL((void*)0); | |||
1716 | else | |||
1717 | return file_type_subtype_table[file_type_subtype].name; | |||
1718 | } | |||
1719 | ||||
1720 | /* | |||
1721 | * Register a backwards-compatibility name. | |||
1722 | */ | |||
1723 | void | |||
1724 | wtap_register_compatibility_file_subtype_name(const char *old_name, | |||
1725 | const char *new_name) | |||
1726 | { | |||
1727 | g_hash_table_insert(type_subtype_name_map, g_strdup(old_name)g_strdup_inline (old_name), | |||
1728 | g_strdup(new_name)g_strdup_inline (new_name)); | |||
1729 | } | |||
1730 | ||||
1731 | /* | |||
1732 | * Translate a name to a capture file type/subtype. | |||
1733 | */ | |||
1734 | int | |||
1735 | wtap_name_to_file_type_subtype(const char *name) | |||
1736 | { | |||
1737 | char *new_name; | |||
1738 | int file_type_subtype; | |||
1739 | ||||
1740 | /* | |||
1741 | * Is this name a backwards-compatibility name? | |||
1742 | */ | |||
1743 | new_name = (char *)g_hash_table_lookup(type_subtype_name_map, | |||
1744 | (void *)name); | |||
1745 | if (new_name != NULL((void*)0)) { | |||
1746 | /* | |||
1747 | * Yes, and new_name is the name to which it should | |||
1748 | * be mapped. | |||
1749 | */ | |||
1750 | name = new_name; | |||
1751 | } | |||
1752 | for (file_type_subtype = 0; | |||
1753 | file_type_subtype < (int)file_type_subtype_table_arr->len; | |||
1754 | file_type_subtype++) { | |||
1755 | if (file_type_subtype_table[file_type_subtype].name != NULL((void*)0) && | |||
1756 | strcmp(name, file_type_subtype_table[file_type_subtype].name) == 0) | |||
1757 | return file_type_subtype; | |||
1758 | } | |||
1759 | ||||
1760 | return -1; /* no such file type, or we can't write it */ | |||
1761 | } | |||
1762 | ||||
1763 | /* | |||
1764 | * Provide the file type/subtype for pcap. | |||
1765 | */ | |||
1766 | int | |||
1767 | wtap_pcap_file_type_subtype(void) | |||
1768 | { | |||
1769 | /* | |||
1770 | * Make sure pcap was registered as a file type/subtype; | |||
1771 | * it's one of our "native" formats. | |||
1772 | */ | |||
1773 | ws_assert(pcap_file_type_subtype != -1)do { if ((1) && !(pcap_file_type_subtype != -1)) ws_log_fatal_full ("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c", 1773, __func__ , "assertion failed: %s", "pcap_file_type_subtype != -1"); } while (0); | |||
1774 | return pcap_file_type_subtype; | |||
1775 | } | |||
1776 | ||||
1777 | /* | |||
1778 | * Provide the file type/subtype for nanosecond-resolution pcap. | |||
1779 | */ | |||
1780 | int | |||
1781 | wtap_pcap_nsec_file_type_subtype(void) | |||
1782 | { | |||
1783 | /* | |||
1784 | * Make sure nanosecond-resolution pcap was registered | |||
1785 | * as a file type/subtype; it's one of our "native" formats. | |||
1786 | */ | |||
1787 | ws_assert(pcap_nsec_file_type_subtype != -1)do { if ((1) && !(pcap_nsec_file_type_subtype != -1)) ws_log_fatal_full("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c" , 1787, __func__, "assertion failed: %s", "pcap_nsec_file_type_subtype != -1" ); } while (0); | |||
1788 | return pcap_nsec_file_type_subtype; | |||
1789 | } | |||
1790 | ||||
1791 | /* | |||
1792 | * Provide the file type/subtype for pcapng. | |||
1793 | */ | |||
1794 | int | |||
1795 | wtap_pcapng_file_type_subtype(void) | |||
1796 | { | |||
1797 | /* | |||
1798 | * Make sure pcapng was registered as a file type/subtype; | |||
1799 | * it's one of our "native" formats. | |||
1800 | */ | |||
1801 | ws_assert(pcapng_file_type_subtype != -1)do { if ((1) && !(pcapng_file_type_subtype != -1)) ws_log_fatal_full ("Wiretap", LOG_LEVEL_ERROR, "wiretap/file_access.c", 1801, __func__ , "assertion failed: %s", "pcapng_file_type_subtype != -1"); } while (0); | |||
1802 | return pcapng_file_type_subtype; | |||
1803 | } | |||
1804 | ||||
1805 | /* | |||
1806 | * Determine if a file type/subtype can write a block of the given type. | |||
1807 | */ | |||
1808 | block_support_t | |||
1809 | wtap_file_type_subtype_supports_block(int file_type_subtype, | |||
1810 | wtap_block_type_t type) | |||
1811 | { | |||
1812 | size_t num_supported_blocks; | |||
1813 | const struct supported_block_type *supported_blocks; | |||
1814 | ||||
1815 | if (file_type_subtype < 0 || | |||
1816 | file_type_subtype >= (int)file_type_subtype_table_arr->len) { | |||
1817 | /* | |||
1818 | * There's no such file type, so it can't support any | |||
1819 | * blocks. | |||
1820 | */ | |||
1821 | return BLOCK_NOT_SUPPORTED; | |||
1822 | } | |||
1823 | ||||
1824 | num_supported_blocks = file_type_subtype_table[file_type_subtype].num_supported_blocks; | |||
1825 | supported_blocks = file_type_subtype_table[file_type_subtype].supported_blocks; | |||
1826 | ||||
1827 | for (size_t block_idx = 0; block_idx < num_supported_blocks; | |||
1828 | block_idx++) { | |||
1829 | if (supported_blocks[block_idx].type == type) | |||
1830 | return supported_blocks[block_idx].support; | |||
1831 | } | |||
1832 | ||||
1833 | /* | |||
1834 | * Not found, which means not supported. | |||
1835 | */ | |||
1836 | return BLOCK_NOT_SUPPORTED; | |||
1837 | } | |||
1838 | ||||
1839 | /* | |||
1840 | * Determine if a file type/subtype, when writing a block of the given type, | |||
1841 | * can support adding the given option to the block. | |||
1842 | */ | |||
1843 | option_support_t | |||
1844 | wtap_file_type_subtype_supports_option(int file_type_subtype, | |||
1845 | wtap_block_type_t type, unsigned option) | |||
1846 | { | |||
1847 | size_t num_supported_blocks; | |||
1848 | const struct supported_block_type *supported_blocks; | |||
1849 | ||||
1850 | if (file_type_subtype < 0 || | |||
1851 | file_type_subtype >= (int)file_type_subtype_table_arr->len) { | |||
1852 | /* | |||
1853 | * There's no such file type, so it can't support any | |||
1854 | * blocks, and thus can't support any options. | |||
1855 | */ | |||
1856 | return OPTION_NOT_SUPPORTED; | |||
1857 | } | |||
1858 | ||||
1859 | num_supported_blocks = file_type_subtype_table[file_type_subtype].num_supported_blocks; | |||
1860 | supported_blocks = file_type_subtype_table[file_type_subtype].supported_blocks; | |||
1861 | ||||
1862 | for (size_t block_idx = 0; block_idx < num_supported_blocks; | |||
1863 | block_idx++) { | |||
1864 | if (supported_blocks[block_idx].type == type) { | |||
1865 | /* | |||
1866 | * OK, that block is known. | |||
1867 | * Is it supported? | |||
1868 | */ | |||
1869 | if (supported_blocks[block_idx].support == BLOCK_NOT_SUPPORTED) { | |||
1870 | /* | |||
1871 | * No, so clearly the option isn't | |||
1872 | * supported in that block. | |||
1873 | */ | |||
1874 | return OPTION_NOT_SUPPORTED; | |||
1875 | } | |||
1876 | ||||
1877 | /* | |||
1878 | * Yes, so check the options. | |||
1879 | */ | |||
1880 | size_t num_supported_options; | |||
1881 | const struct supported_option_type *supported_options; | |||
1882 | ||||
1883 | num_supported_options = supported_blocks[block_idx].num_supported_options; | |||
1884 | supported_options = supported_blocks[block_idx].supported_options; | |||
1885 | for (size_t opt_idx = 0; opt_idx < num_supported_options; | |||
1886 | opt_idx++) { | |||
1887 | if (supported_options[opt_idx].opt == option) | |||
1888 | return supported_options[opt_idx].support; | |||
1889 | } | |||
1890 | ||||
1891 | /* | |||
1892 | * Not found, which means not supported. | |||
1893 | */ | |||
1894 | return OPTION_NOT_SUPPORTED; | |||
1895 | } | |||
1896 | } | |||
1897 | ||||
1898 | /* | |||
1899 | * The block type wasn't found, which means it's not supported, | |||
1900 | * which means the option isn't supported in that block. | |||
1901 | */ | |||
1902 | return OPTION_NOT_SUPPORTED; | |||
1903 | } | |||
1904 | ||||
1905 | static GSList * | |||
1906 | add_extensions_for_file_type_subtype(int file_type_subtype, GSList *extensions, | |||
1907 | GSList *compression_type_extensions) | |||
1908 | { | |||
1909 | char **extensions_set, **extensionp; | |||
1910 | char *extension; | |||
1911 | ||||
1912 | if (file_type_subtype < 0 || | |||
1913 | file_type_subtype >= (int)file_type_subtype_table_arr->len) { | |||
1914 | /* | |||
1915 | * There's no such file type, so it has no extensions | |||
1916 | * to add. | |||
1917 | */ | |||
1918 | return extensions; | |||
1919 | } | |||
1920 | ||||
1921 | /* | |||
1922 | * Add the default extension, and all of the compressed variants | |||
1923 | * from the list of compressed-file extensions, if there is a | |||
1924 | * default extension. | |||
1925 | */ | |||
1926 | if (file_type_subtype_table[file_type_subtype].default_file_extension != NULL((void*)0)) { | |||
1927 | extensions = add_extensions(extensions, | |||
1928 | file_type_subtype_table[file_type_subtype].default_file_extension, | |||
1929 | compression_type_extensions); | |||
1930 | } | |||
1931 | ||||
1932 | if (file_type_subtype_table[file_type_subtype].additional_file_extensions != NULL((void*)0)) { | |||
1933 | /* | |||
1934 | * We have additional extensions; add them. | |||
1935 | * | |||
1936 | * First, split the extension-list string into a set of | |||
1937 | * extensions. | |||
1938 | */ | |||
1939 | extensions_set = g_strsplit(file_type_subtype_table[file_type_subtype].additional_file_extensions, | |||
1940 | ";", 0); | |||
1941 | ||||
1942 | /* | |||
1943 | * Add each of those extensions to the list. | |||
1944 | */ | |||
1945 | for (extensionp = extensions_set; *extensionp != NULL((void*)0); | |||
1946 | extensionp++) { | |||
1947 | extension = *extensionp; | |||
1948 | ||||
1949 | /* | |||
1950 | * Add the extension, and all compressed variants | |||
1951 | * of it if requested. | |||
1952 | */ | |||
1953 | extensions = add_extensions(extensions, extension, | |||
1954 | compression_type_extensions); | |||
1955 | } | |||
1956 | ||||
1957 | g_strfreev(extensions_set); | |||
1958 | } | |||
1959 | return extensions; | |||
1960 | } | |||
1961 | ||||
1962 | /* Return a list of file extensions that are used by the specified file | |||
1963 | * type/subtype. | |||
1964 | * | |||
1965 | * If include_compressed is true, the list will include compressed | |||
1966 | * extensions, e.g. not just "pcap" but also "pcap.gz" if we can read | |||
1967 | * gzipped files. | |||
1968 | * | |||
1969 | * All strings in the list are allocated with g_malloc() and must be freed | |||
1970 | * with g_free(). | |||
1971 | */ | |||
1972 | GSList * | |||
1973 | wtap_get_file_extensions_list(int file_type_subtype, bool_Bool include_compressed) | |||
1974 | { | |||
1975 | GSList *extensions, *compression_type_extensions; | |||
1976 | ||||
1977 | if (file_type_subtype < 0 || | |||
1978 | file_type_subtype >= (int)file_type_subtype_table_arr->len) | |||
1979 | return NULL((void*)0); /* not a valid file type */ | |||
1980 | ||||
1981 | if (file_type_subtype_table[file_type_subtype].default_file_extension == NULL((void*)0)) | |||
1982 | return NULL((void*)0); /* valid, but no extensions known */ | |||
1983 | ||||
1984 | extensions = NULL((void*)0); /* empty list, to start with */ | |||
1985 | ||||
1986 | /* | |||
1987 | * Add all this file type's extensions, with compressed | |||
1988 | * variants if include_compressed is true. | |||
1989 | */ | |||
1990 | if (include_compressed) { | |||
1991 | /* | |||
1992 | * Get compression-type extensions, if any. | |||
1993 | */ | |||
1994 | compression_type_extensions = wtap_get_all_compression_type_extensions_list(); | |||
1995 | } else { | |||
1996 | /* | |||
1997 | * We don't want the compressed file extensions. | |||
1998 | */ | |||
1999 | compression_type_extensions = NULL((void*)0); | |||
2000 | } | |||
2001 | extensions = add_extensions_for_file_type_subtype(file_type_subtype, extensions, | |||
2002 | compression_type_extensions); | |||
2003 | ||||
2004 | g_slist_free(compression_type_extensions); | |||
2005 | ||||
2006 | return g_slist_reverse(extensions); | |||
2007 | } | |||
2008 | ||||
2009 | /* Return a list of all extensions that are used by all capture file | |||
2010 | * types, including compressed extensions, e.g. not just "pcap" but | |||
2011 | * also "pcap.gz" if we can read gzipped files. | |||
2012 | * | |||
2013 | * "Capture files" means "include file types that correspond to | |||
2014 | * collections of network packets, but not file types that | |||
2015 | * store data that just happens to be transported over protocols | |||
2016 | * such as HTTP but that aren't collections of network packets", | |||
2017 | * so that it could be used for "All Capture Files" without picking | |||
2018 | * up JPEG files or files such as that - those aren't capture files, | |||
2019 | * and we *do* have them listed in the long list of individual file | |||
2020 | * types, so omitting them from "All Capture Files" is the right | |||
2021 | * thing to do. | |||
2022 | * | |||
2023 | * All strings in the list are allocated with g_malloc() and must be freed | |||
2024 | * with g_free(). | |||
2025 | * | |||
2026 | * This is used to generate a list of extensions to look for if the user | |||
2027 | * chooses "All Capture Files" in a file open dialog. | |||
2028 | */ | |||
2029 | GSList * | |||
2030 | wtap_get_all_capture_file_extensions_list(void) | |||
2031 | { | |||
2032 | GSList *extensions, *compression_type_extensions; | |||
2033 | unsigned int i; | |||
2034 | ||||
2035 | init_file_type_extensions(); | |||
2036 | ||||
2037 | extensions = NULL((void*)0); /* empty list, to start with */ | |||
2038 | ||||
2039 | /* | |||
2040 | * Get compression-type extensions, if any. | |||
2041 | */ | |||
2042 | compression_type_extensions = wtap_get_all_compression_type_extensions_list(); | |||
2043 | ||||
2044 | for (i = 0; i < file_type_extensions_arr->len; i++) { | |||
2045 | /* | |||
2046 | * Is this a capture file, rather than one of the | |||
2047 | * other random file types we can read? | |||
2048 | */ | |||
2049 | if (file_type_extensions[i].is_capture_file) { | |||
2050 | /* | |||
2051 | * Yes. Add all this file extension type's | |||
2052 | * extensions, with compressed variants. | |||
2053 | */ | |||
2054 | extensions = add_extensions_for_file_extensions_type(i, | |||
2055 | extensions, compression_type_extensions); | |||
2056 | } | |||
2057 | } | |||
2058 | ||||
2059 | g_slist_free(compression_type_extensions); | |||
2060 | ||||
2061 | return g_slist_reverse(extensions); | |||
2062 | } | |||
2063 | ||||
2064 | /* Return a list of all extensions that are used by all file types that | |||
2065 | * we can read, including compressed extensions, e.g. not just "pcap" but | |||
2066 | * also "pcap.gz" if we can read gzipped files. | |||
2067 | * | |||
2068 | * "File type" means "include file types that correspond to collections | |||
2069 | * of network packets, as well as file types that store data that just | |||
2070 | * happens to be transported over protocols such as HTTP but that aren't | |||
2071 | * collections of network packets, and plain text files". | |||
2072 | * | |||
2073 | * All strings in the list are allocated with g_malloc() and must be freed | |||
2074 | * with g_free(). | |||
2075 | * | |||
2076 | * This is used to get the "base name" for a file, by stripping off | |||
2077 | * compressed-file extensions and extensions that correspond to file | |||
2078 | * types that we know about. | |||
2079 | */ | |||
2080 | GSList * | |||
2081 | wtap_get_all_file_extensions_list(void) | |||
2082 | { | |||
2083 | GSList *extensions, *compression_type_extensions; | |||
2084 | ||||
2085 | extensions = NULL((void*)0); /* empty list, to start with */ | |||
2086 | ||||
2087 | /* | |||
2088 | * Get compression-type extensions, if any. | |||
2089 | */ | |||
2090 | compression_type_extensions = wtap_get_all_compression_type_extensions_list(); | |||
2091 | ||||
2092 | for (int ft = 0; ft < (int)file_type_subtype_table_arr->len; ft++) { | |||
2093 | extensions = add_extensions_for_file_type_subtype(ft, extensions, | |||
2094 | compression_type_extensions); | |||
2095 | } | |||
2096 | ||||
2097 | g_slist_free(compression_type_extensions); | |||
2098 | ||||
2099 | return g_slist_reverse(extensions); | |||
2100 | } | |||
2101 | ||||
2102 | /* | |||
2103 | * Free a list returned by wtap_get_file_extension_type_extensions(), | |||
2104 | * wtap_get_all_capture_file_extensions_list, wtap_get_file_extensions_list(), | |||
2105 | * or wtap_get_all_file_extensions_list(). | |||
2106 | */ | |||
2107 | void | |||
2108 | wtap_free_extensions_list(GSList *extensions) | |||
2109 | { | |||
2110 | GSList *extension; | |||
2111 | ||||
2112 | for (extension = extensions; extension != NULL((void*)0); | |||
2113 | extension = g_slist_next(extension)((extension) ? (((GSList *)(extension))->next) : ((void*)0 ))) { | |||
2114 | g_free(extension->data); | |||
2115 | } | |||
2116 | g_slist_free(extensions); | |||
2117 | } | |||
2118 | ||||
2119 | /* | |||
2120 | * Return the default file extension to use with the specified file type | |||
2121 | * and subtype; that's just the extension, without any ".". | |||
2122 | */ | |||
2123 | const char * | |||
2124 | wtap_default_file_extension(int file_type_subtype) | |||
2125 | { | |||
2126 | if (file_type_subtype < 0 || | |||
2127 | file_type_subtype >= (int)file_type_subtype_table_arr->len) | |||
2128 | return NULL((void*)0); | |||
2129 | else | |||
2130 | return file_type_subtype_table[file_type_subtype].default_file_extension; | |||
2131 | } | |||
2132 | ||||
2133 | /* | |||
2134 | * Return whether we know how to write the specified file type. | |||
2135 | */ | |||
2136 | bool_Bool | |||
2137 | wtap_dump_can_open(int file_type_subtype) | |||
2138 | { | |||
2139 | if (file_type_subtype < 0 || | |||
2140 | file_type_subtype >= (int)file_type_subtype_table_arr->len || | |||
2141 | file_type_subtype_table[file_type_subtype].dump_open == NULL((void*)0)) | |||
2142 | return false0; | |||
2143 | ||||
2144 | return true1; | |||
2145 | } | |||
2146 | ||||
2147 | /* | |||
2148 | * Return whether we know how to write a compressed file of the specified | |||
2149 | * file type. | |||
2150 | */ | |||
2151 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) || defined (HAVE_LZ4FRAME_H1) | |||
2152 | bool_Bool | |||
2153 | wtap_dump_can_compress(int file_type_subtype) | |||
2154 | { | |||
2155 | /* | |||
2156 | * If this is an unknown file type, or if we have to | |||
2157 | * seek when writing out a file with this file type, | |||
2158 | * return false. | |||
2159 | */ | |||
2160 | if (file_type_subtype < 0 || | |||
2161 | file_type_subtype >= (int)file_type_subtype_table_arr->len || | |||
2162 | file_type_subtype_table[file_type_subtype].writing_must_seek) | |||
2163 | return false0; | |||
2164 | ||||
2165 | return true1; | |||
2166 | } | |||
2167 | #else | |||
2168 | bool_Bool | |||
2169 | wtap_dump_can_compress(int file_type_subtype _U___attribute__((unused))) | |||
2170 | { | |||
2171 | return false0; | |||
2172 | } | |||
2173 | #endif | |||
2174 | ||||
2175 | static bool_Bool wtap_dump_open_finish(wtap_dumper *wdh, int *err, | |||
2176 | char **err_info); | |||
2177 | ||||
2178 | static WFILE_T wtap_dump_file_open(const wtap_dumper *wdh, const char *filename); | |||
2179 | static WFILE_T wtap_dump_file_fdopen(const wtap_dumper *wdh, int fd); | |||
2180 | static int wtap_dump_file_close(wtap_dumper *wdh); | |||
2181 | static bool_Bool wtap_dump_fix_idb(wtap_dumper *wdh, wtap_block_t idb, int *err); | |||
2182 | ||||
2183 | static wtap_dumper * | |||
2184 | wtap_dump_init_dumper(int file_type_subtype, wtap_compression_type compression_type, | |||
2185 | const wtap_dump_params *params, int *err) | |||
2186 | { | |||
2187 | wtap_dumper *wdh; | |||
2188 | wtap_block_t descr, file_int_data; | |||
2189 | wtapng_if_descr_mandatory_t *descr_mand, *file_int_data_mand; | |||
2190 | GArray *interfaces = params->idb_inf ? params->idb_inf->interface_data : NULL((void*)0); | |||
2191 | ||||
2192 | /* Can we write files of this file type/subtype? | |||
2193 | * | |||
2194 | * This will fail if file_type_subtype isn't a valid | |||
2195 | * file type/subtype value, so, if it doesn't fail, | |||
2196 | * we know file_type_subtype is within the bounds of | |||
2197 | * the table of file types/subtypes. | |||
2198 | */ | |||
2199 | if (!wtap_dump_can_open(file_type_subtype)) { | |||
2200 | /* Invalid type, or type we don't know how to write. */ | |||
2201 | *err = WTAP_ERR_UNWRITABLE_FILE_TYPE-7; | |||
2202 | return NULL((void*)0); | |||
2203 | } | |||
2204 | ||||
2205 | /* OK, we know how to write that file type/subtype; can we write | |||
2206 | * the specified encapsulation type in that file type/subtype? | |||
2207 | */ | |||
2208 | *err = (*file_type_subtype_table[file_type_subtype].can_write_encap)(params->encap); | |||
2209 | /* if the err said to check wslua's can_write_encap, try that */ | |||
2210 | if (*err == WTAP_ERR_CHECK_WSLUA-23 | |||
2211 | && file_type_subtype_table[file_type_subtype].wslua_info != NULL((void*)0) | |||
2212 | && file_type_subtype_table[file_type_subtype].wslua_info->wslua_can_write_encap != NULL((void*)0)) { | |||
2213 | ||||
2214 | *err = (*file_type_subtype_table[file_type_subtype].wslua_info->wslua_can_write_encap)(params->encap, file_type_subtype_table[file_type_subtype].wslua_info->wslua_data); | |||
2215 | } | |||
2216 | ||||
2217 | if (*err != 0) { | |||
2218 | /* No, we can't. */ | |||
2219 | return NULL((void*)0); | |||
2220 | } | |||
2221 | ||||
2222 | /* Check whether we can open a capture file with that file type | |||
2223 | * and that encapsulation, and, if the compression type isn't | |||
2224 | * "uncompressed", whether we can write a *compressed* file | |||
2225 | * of that file type. | |||
2226 | * If we're doing compression, can this file type/subtype be | |||
2227 | written in compressed form? | |||
2228 | * | |||
2229 | * (The particular type doesn't matter - if the file can't | |||
2230 | * be written 100% sequentially, we can't compress it, | |||
2231 | * because we can't go back and overwrite something we've | |||
2232 | * already written. | |||
2233 | */ | |||
2234 | if (compression_type != WTAP_UNCOMPRESSED && | |||
2235 | !wtap_dump_can_compress(file_type_subtype)) { | |||
2236 | *err = WTAP_ERR_COMPRESSION_NOT_SUPPORTED-17; | |||
2237 | return NULL((void*)0); | |||
2238 | } | |||
2239 | ||||
2240 | /* Allocate a data structure for the output stream. */ | |||
2241 | wdh = g_new0(wtap_dumper, 1)((wtap_dumper *) g_malloc0_n ((1), sizeof (wtap_dumper))); | |||
2242 | if (wdh == NULL((void*)0)) { | |||
2243 | *err = errno(*__errno_location ()); | |||
2244 | return NULL((void*)0); | |||
2245 | } | |||
2246 | ||||
2247 | wdh->file_type_subtype = file_type_subtype; | |||
2248 | wdh->snaplen = params->snaplen; | |||
2249 | wdh->file_encap = params->encap; | |||
2250 | wdh->compression_type = compression_type; | |||
2251 | wdh->wslua_data = NULL((void*)0); | |||
2252 | wdh->shb_iface_to_global = params->shb_iface_to_global; | |||
2253 | wdh->interface_data = g_array_new(false0, false0, sizeof(wtap_block_t)); | |||
2254 | ||||
2255 | /* Set Section Header Block data */ | |||
2256 | wdh->shb_hdrs = params->shb_hdrs; | |||
2257 | /* Set Name Resolution Block data */ | |||
2258 | wdh->nrbs_growing = params->nrbs_growing; | |||
2259 | /* Set Interface Description Block data */ | |||
2260 | if (interfaces && interfaces->len) { | |||
2261 | if (!params->dont_copy_idbs) { /* XXX */ | |||
2262 | unsigned itf_count; | |||
2263 | ||||
2264 | /* Note: this memory is owned by wtap_dumper and will become | |||
2265 | * invalid after wtap_dump_close. */ | |||
2266 | for (itf_count = 0; itf_count
| |||
2267 | file_int_data = g_array_index(interfaces, wtap_block_t, itf_count)(((wtap_block_t*) (void *) (interfaces)->data) [(itf_count )]); | |||
2268 | file_int_data_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(file_int_data); | |||
2269 | descr = wtap_block_make_copy(file_int_data); | |||
2270 | if ((params->encap != WTAP_ENCAP_PER_PACKET-1) && (params->encap != file_int_data_mand->wtap_encap)) { | |||
2271 | descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(descr); | |||
2272 | descr_mand->wtap_encap = params->encap; | |||
2273 | } | |||
2274 | if (!wtap_dump_fix_idb(wdh, descr, err)) { | |||
2275 | return NULL((void*)0); | |||
| ||||
2276 | } | |||
2277 | g_array_append_val(wdh->interface_data, descr)g_array_append_vals (wdh->interface_data, &(descr), 1); | |||
2278 | } | |||
2279 | } | |||
2280 | } else if (params->encap != WTAP_ENCAP_NONE-2 && params->encap != WTAP_ENCAP_PER_PACKET-1) { | |||
2281 | /* Generate a fake IDB if we don't have one, unless the | |||
2282 | * file encapsulation is none. (WTAP_ENCAP_NONE either | |||
2283 | * means that there are no interfaces, or they will be | |||
2284 | * provided later when reading the file in single-pass mode.) | |||
2285 | * | |||
2286 | * For WTAP_ENCAP_PER_PACKET, we'll have to generate IDBs | |||
2287 | * from packet records as they come in. (pcapng does this now.) | |||
2288 | * | |||
2289 | * XXX File types should provide their own IDBs (possibly | |||
2290 | * fake ones generated by wtap_add_generated_idb()), in | |||
2291 | * order to support being used as inputs for mergecap where | |||
2292 | * pcapng is the output. | |||
2293 | */ | |||
2294 | descr = wtap_dump_params_generate_idb(params); | |||
2295 | g_array_append_val(wdh->interface_data, descr)g_array_append_vals (wdh->interface_data, &(descr), 1); | |||
2296 | } | |||
2297 | /* Set Decryption Secrets Blocks */ | |||
2298 | wdh->dsbs_initial = params->dsbs_initial; | |||
2299 | wdh->dsbs_growing = params->dsbs_growing; | |||
2300 | /* Set Sysdig meta events */ | |||
2301 | wdh->mevs_growing = params->mevs_growing; | |||
2302 | /* Set DPIBs */ | |||
2303 | wdh->dpibs_growing = params->dpibs_growing; | |||
2304 | return wdh; | |||
2305 | } | |||
2306 | ||||
2307 | wtap_dumper * | |||
2308 | wtap_dump_open(const char *filename, int file_type_subtype, | |||
2309 | wtap_compression_type compression_type, const wtap_dump_params *params, | |||
2310 | int *err, char **err_info) | |||
2311 | { | |||
2312 | wtap_dumper *wdh; | |||
2313 | WFILE_T fh; | |||
2314 | ||||
2315 | *err = 0; | |||
2316 | *err_info = NULL((void*)0); | |||
2317 | ||||
2318 | /* Allocate and initialize a data structure for the output stream. */ | |||
2319 | wdh = wtap_dump_init_dumper(file_type_subtype, compression_type, params, | |||
2320 | err); | |||
2321 | if (wdh == NULL((void*)0)) | |||
2322 | return NULL((void*)0); | |||
2323 | ||||
2324 | /* In case "fopen()" fails but doesn't set "errno", set "errno" | |||
2325 | to a generic "the open failed" error. */ | |||
2326 | errno(*__errno_location ()) = WTAP_ERR_CANT_OPEN-6; | |||
2327 | fh = wtap_dump_file_open(wdh, filename); | |||
2328 | if (fh == NULL((void*)0)) { | |||
2329 | *err = errno(*__errno_location ()); | |||
2330 | g_free(wdh); | |||
2331 | return NULL((void*)0); /* can't create file */ | |||
2332 | } | |||
2333 | wdh->fh = fh; | |||
2334 | ||||
2335 | if (!wtap_dump_open_finish(wdh, err, err_info)) { | |||
2336 | /* Get rid of the file we created; we couldn't finish | |||
2337 | opening it. */ | |||
2338 | wtap_dump_file_close(wdh); | |||
2339 | ws_unlinkunlink(filename); | |||
2340 | g_free(wdh); | |||
2341 | return NULL((void*)0); | |||
2342 | } | |||
2343 | return wdh; | |||
2344 | } | |||
2345 | ||||
2346 | wtap_dumper * | |||
2347 | wtap_dump_open_tempfile(const char *tmpdir, char **filenamep, const char *pfx, | |||
2348 | int file_type_subtype, wtap_compression_type compression_type, | |||
2349 | const wtap_dump_params *params, int *err, char **err_info) | |||
2350 | { | |||
2351 | int fd; | |||
2352 | const char *ext; | |||
2353 | char sfx[16]; | |||
2354 | wtap_dumper *wdh; | |||
2355 | WFILE_T fh; | |||
2356 | ||||
2357 | /* No path name for the temporary file yet. */ | |||
2358 | *filenamep = NULL((void*)0); | |||
2359 | ||||
2360 | *err = 0; | |||
2361 | *err_info = NULL((void*)0); | |||
2362 | ||||
2363 | /* Allocate and initialize a data structure for the output stream. */ | |||
2364 | wdh = wtap_dump_init_dumper(file_type_subtype, compression_type, params, | |||
2365 | err); | |||
2366 | if (wdh == NULL((void*)0)) | |||
2367 | return NULL((void*)0); | |||
2368 | ||||
2369 | /* Choose an appropriate suffix for the file */ | |||
2370 | ext = wtap_default_file_extension(file_type_subtype); | |||
2371 | if (ext == NULL((void*)0)) | |||
2372 | ext = "tmp"; | |||
2373 | sfx[0] = '.'; | |||
2374 | sfx[1] = '\0'; | |||
2375 | (void) g_strlcat(sfx, ext, 16); | |||
2376 | ||||
2377 | /* Choose a random name for the file */ | |||
2378 | fd = create_tempfile(tmpdir, filenamep, pfx, sfx, NULL((void*)0)); | |||
2379 | if (fd == -1) { | |||
2380 | *err = WTAP_ERR_CANT_OPEN-6; | |||
2381 | g_free(wdh); | |||
2382 | return NULL((void*)0); /* can't create file */ | |||
2383 | } | |||
2384 | ||||
2385 | /* In case "fopen()" fails but doesn't set "errno", set "errno" | |||
2386 | to a generic "the open failed" error. */ | |||
2387 | errno(*__errno_location ()) = WTAP_ERR_CANT_OPEN-6; | |||
2388 | fh = wtap_dump_file_fdopen(wdh, fd); | |||
2389 | if (fh == NULL((void*)0)) { | |||
2390 | *err = errno(*__errno_location ()); | |||
2391 | ws_closeclose(fd); | |||
2392 | g_free(wdh); | |||
2393 | return NULL((void*)0); /* can't create file */ | |||
2394 | } | |||
2395 | wdh->fh = fh; | |||
2396 | ||||
2397 | if (!wtap_dump_open_finish(wdh, err, err_info)) { | |||
2398 | /* Get rid of the file we created; we couldn't finish | |||
2399 | opening it. */ | |||
2400 | wtap_dump_file_close(wdh); | |||
2401 | ws_unlinkunlink(*filenamep); | |||
2402 | g_free(wdh); | |||
2403 | return NULL((void*)0); | |||
2404 | } | |||
2405 | return wdh; | |||
2406 | } | |||
2407 | ||||
2408 | wtap_dumper * | |||
2409 | wtap_dump_fdopen(int fd, int file_type_subtype, wtap_compression_type compression_type, | |||
2410 | const wtap_dump_params *params, int *err, char **err_info) | |||
2411 | { | |||
2412 | wtap_dumper *wdh; | |||
2413 | WFILE_T fh; | |||
2414 | ||||
2415 | *err = 0; | |||
2416 | *err_info = NULL((void*)0); | |||
2417 | ||||
2418 | /* Allocate and initialize a data structure for the output stream. */ | |||
2419 | wdh = wtap_dump_init_dumper(file_type_subtype, compression_type, params, | |||
2420 | err); | |||
2421 | if (wdh == NULL((void*)0)) | |||
2422 | return NULL((void*)0); | |||
2423 | ||||
2424 | /* In case "fopen()" fails but doesn't set "errno", set "errno" | |||
2425 | to a generic "the open failed" error. */ | |||
2426 | errno(*__errno_location ()) = WTAP_ERR_CANT_OPEN-6; | |||
2427 | fh = wtap_dump_file_fdopen(wdh, fd); | |||
2428 | if (fh == NULL((void*)0)) { | |||
2429 | *err = errno(*__errno_location ()); | |||
2430 | g_free(wdh); | |||
2431 | return NULL((void*)0); /* can't create standard I/O stream */ | |||
2432 | } | |||
2433 | wdh->fh = fh; | |||
2434 | ||||
2435 | if (!wtap_dump_open_finish(wdh, err, err_info)) { | |||
2436 | wtap_dump_file_close(wdh); | |||
2437 | g_free(wdh); | |||
2438 | return NULL((void*)0); | |||
2439 | } | |||
2440 | return wdh; | |||
2441 | } | |||
2442 | ||||
2443 | wtap_dumper * | |||
2444 | wtap_dump_open_stdout(int file_type_subtype, wtap_compression_type compression_type, | |||
2445 | const wtap_dump_params *params, int *err, char **err_info) | |||
2446 | { | |||
2447 | int new_fd; | |||
2448 | wtap_dumper *wdh; | |||
2449 | ||||
2450 | /* | |||
2451 | * Duplicate the file descriptor, so that we can close the | |||
2452 | * wtap_dumper handle the same way we close any other | |||
2453 | * wtap_dumper handle, without closing the standard output. | |||
2454 | */ | |||
2455 | new_fd = ws_dupdup(1); | |||
2456 | if (new_fd == -1) { | |||
| ||||
2457 | /* dup failed */ | |||
2458 | *err = errno(*__errno_location ()); | |||
2459 | return NULL((void*)0); | |||
2460 | } | |||
2461 | #ifdef _WIN32 | |||
2462 | /* | |||
2463 | * Put the new descriptor into binary mode. | |||
2464 | * | |||
2465 | * XXX - even if the file format we're writing is a text | |||
2466 | * format? | |||
2467 | */ | |||
2468 | if (_setmode(new_fd, O_BINARY0) == -1) { | |||
2469 | /* "Should not happen" */ | |||
2470 | *err = errno(*__errno_location ()); | |||
2471 | ws_closeclose(new_fd); | |||
2472 | return NULL((void*)0); | |||
2473 | } | |||
2474 | #endif | |||
2475 | ||||
2476 | wdh = wtap_dump_fdopen(new_fd, file_type_subtype, compression_type, | |||
2477 | params, err, err_info); | |||
2478 | if (wdh == NULL((void*)0)) { | |||
2479 | /* Failed; close the new FD */ | |||
2480 | ws_closeclose(new_fd); | |||
2481 | return NULL((void*)0); | |||
2482 | } | |||
2483 | return wdh; | |||
2484 | } | |||
2485 | ||||
2486 | static bool_Bool | |||
2487 | wtap_dump_open_finish(wtap_dumper *wdh, int *err, char **err_info) | |||
2488 | { | |||
2489 | int fd; | |||
2490 | bool_Bool cant_seek; | |||
2491 | ||||
2492 | /* Can we do a seek on the file descriptor? | |||
2493 | If not, note that fact. */ | |||
2494 | if (wdh->compression_type != WTAP_UNCOMPRESSED) { | |||
2495 | cant_seek = true1; | |||
2496 | } else { | |||
2497 | fd = ws_filenofileno((FILE *)wdh->fh); | |||
2498 | if (ws_lseek64lseek(fd, 1, SEEK_CUR1) == (off_t) -1) | |||
2499 | cant_seek = true1; | |||
2500 | else { | |||
2501 | /* Undo the seek. */ | |||
2502 | ws_lseek64lseek(fd, 0, SEEK_SET0); | |||
2503 | cant_seek = false0; | |||
2504 | } | |||
2505 | } | |||
2506 | ||||
2507 | /* If this file type requires seeking, and we can't seek, fail. */ | |||
2508 | if (file_type_subtype_table[wdh->file_type_subtype].writing_must_seek && cant_seek) { | |||
2509 | *err = WTAP_ERR_CANT_WRITE_TO_PIPE-5; | |||
2510 | return false0; | |||
2511 | } | |||
2512 | ||||
2513 | /* Set wdh with wslua data if any - this is how we pass the data | |||
2514 | * to the file writer. | |||
2515 | */ | |||
2516 | if (file_type_subtype_table[wdh->file_type_subtype].wslua_info) | |||
2517 | wdh->wslua_data = file_type_subtype_table[wdh->file_type_subtype].wslua_info->wslua_data; | |||
2518 | ||||
2519 | /* Now try to open the file for writing. */ | |||
2520 | if (!(*file_type_subtype_table[wdh->file_type_subtype].dump_open)(wdh, err, | |||
2521 | err_info)) { | |||
2522 | return false0; | |||
2523 | } | |||
2524 | ||||
2525 | return true1; /* success! */ | |||
2526 | } | |||
2527 | ||||
2528 | /* XXX - Temporary hack to deal with nstime_t, and thus wtap_rec and libwiretap | |||
2529 | * in general, not storing precision greater than nanoseconds yet. Despite | |||
2530 | * whatever precision an IDB claims, we can't write finer than nanosecond. | |||
2531 | * Eventually this should be removed. | |||
2532 | * Fix a given IDB to indicate no greater than nanosecond precision. */ | |||
2533 | static bool_Bool | |||
2534 | wtap_dump_fix_idb(wtap_dumper *wdb _U___attribute__((unused)), wtap_block_t idb, int *err) | |||
2535 | { | |||
2536 | wtapng_if_descr_mandatory_t *idb_mand; | |||
2537 | int64_t tsoffset; | |||
2538 | idb_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(idb); | |||
2539 | ||||
2540 | /* | |||
2541 | * nstime_t only stores nanoseconds, so instead of claiming false | |||
2542 | * precision, fix things up. | |||
2543 | */ | |||
2544 | if (idb_mand->time_units_per_second > 1000000000) { | |||
2545 | ws_warning("original time precision reduced to nanoseconds")do { if (1) { ws_log_full("Wiretap", LOG_LEVEL_WARNING, "wiretap/file_access.c" , 2545, __func__, "original time precision reduced to nanoseconds" ); } } while (0); | |||
2546 | idb_mand->time_units_per_second = 1000000000; | |||
2547 | switch (wtap_block_set_uint8_option_value(idb, OPT_IDB_TSRESOL9, 9)) { | |||
2548 | case WTAP_OPTTYPE_SUCCESS: | |||
2549 | break; | |||
2550 | case WTAP_OPTTYPE_NOT_FOUND: | |||
2551 | // This "should not" happen, because no if_tsresol means 6, | |||
2552 | // and time_units_per_second should be 1000000; | |||
2553 | if (wtap_block_add_uint8_option(idb, OPT_IDB_TSRESOL9, 9) == WTAP_OPTTYPE_SUCCESS) { | |||
2554 | break; | |||
2555 | } | |||
2556 | /* FALLTHROUGH */ | |||
2557 | default: | |||
2558 | // These "should not" happen either. | |||
2559 | *err = WTAP_ERR_INTERNAL-21; | |||
2560 | return false0; | |||
2561 | } | |||
2562 | } | |||
2563 | ||||
2564 | /* | |||
2565 | * Since we're not writing more than nanosecond resolution, if_tsoffset | |||
2566 | * isn't necessary, as anything we can store in absolute timestamps in | |||
2567 | * Wireshark records we can write to a file without an offset (unless | |||
2568 | * we have 32-bit time_t, but that's rare these days.) So we remove it | |||
2569 | * here, though in the long term we should keep it. | |||
2570 | */ | |||
2571 | if (wtap_block_get_int64_option_value(idb, OPT_IDB_TSOFFSET14, &tsoffset) == WTAP_OPTTYPE_SUCCESS) { | |||
2572 | wtap_block_remove_option(idb, OPT_IDB_TSOFFSET14); | |||
2573 | } | |||
2574 | ||||
2575 | return true1; | |||
2576 | } | |||
2577 | ||||
2578 | bool_Bool | |||
2579 | wtap_dump_add_idb(wtap_dumper *wdh, wtap_block_t idb, int *err, | |||
2580 | char **err_info) | |||
2581 | { | |||
2582 | wtap_block_t idb_copy; | |||
2583 | ||||
2584 | if (wdh->subtype_add_idb == NULL((void*)0)) { | |||
2585 | /* Not supported. */ | |||
2586 | *err = WTAP_ERR_UNWRITABLE_REC_TYPE-24; | |||
2587 | *err_info = g_strdup("Adding IDBs isn't supported by this file type")g_strdup_inline ("Adding IDBs isn't supported by this file type" ); | |||
2588 | return false0; | |||
2589 | } | |||
2590 | ||||
2591 | /* | |||
2592 | * Add a copy of this IDB to our array of IDBs. | |||
2593 | */ | |||
2594 | idb_copy = wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO); | |||
2595 | wtap_block_copy(idb_copy, idb); | |||
2596 | g_array_append_val(wdh->interface_data, idb_copy)g_array_append_vals (wdh->interface_data, &(idb_copy), 1); | |||
2597 | ||||
2598 | if (!wtap_dump_fix_idb(wdh, idb_copy, err)) { | |||
2599 | *err_info = ws_strdup_printf("pcapng: failed to lower time resolution to nanoseconds")wmem_strdup_printf(((void*)0), "pcapng: failed to lower time resolution to nanoseconds" ); | |||
2600 | return false0; | |||
2601 | } | |||
2602 | ||||
2603 | *err = 0; | |||
2604 | *err_info = NULL((void*)0); | |||
2605 | return (wdh->subtype_add_idb)(wdh, idb, err, err_info); | |||
2606 | } | |||
2607 | ||||
2608 | bool_Bool | |||
2609 | wtap_dump(wtap_dumper *wdh, const wtap_rec *rec, int *err, char **err_info) | |||
2610 | { | |||
2611 | *err = 0; | |||
2612 | *err_info = NULL((void*)0); | |||
2613 | return (wdh->subtype_write)(wdh, rec, err, err_info); | |||
2614 | } | |||
2615 | ||||
2616 | bool_Bool | |||
2617 | wtap_dump_flush(wtap_dumper *wdh, int *err) | |||
2618 | { | |||
2619 | switch (wdh->compression_type) { | |||
2620 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) | |||
2621 | case WTAP_GZIP_COMPRESSED: | |||
2622 | if (gzwfile_flush((GZWFILE_T)wdh->fh) == -1) { | |||
2623 | *err = gzwfile_geterr((GZWFILE_T)wdh->fh); | |||
2624 | return false0; | |||
2625 | } | |||
2626 | break; | |||
2627 | #endif | |||
2628 | #ifdef HAVE_LZ4FRAME_H1 | |||
2629 | case WTAP_LZ4_COMPRESSED: | |||
2630 | if (lz4wfile_flush((LZ4WFILE_T)wdh->fh) == -1) { | |||
2631 | *err = lz4wfile_geterr((LZ4WFILE_T)wdh->fh); | |||
2632 | return false0; | |||
2633 | } | |||
2634 | break; | |||
2635 | #endif /* HAVE_LZ4FRAME_H */ | |||
2636 | default: | |||
2637 | if (fflush((FILE *)wdh->fh) == EOF(-1)) { | |||
2638 | *err = errno(*__errno_location ()); | |||
2639 | return false0; | |||
2640 | } | |||
2641 | } | |||
2642 | return true1; | |||
2643 | } | |||
2644 | ||||
2645 | bool_Bool | |||
2646 | wtap_dump_close(wtap_dumper *wdh, bool_Bool *needs_reload, | |||
2647 | int *err, char **err_info) | |||
2648 | { | |||
2649 | bool_Bool ret = true1; | |||
2650 | ||||
2651 | *err = 0; | |||
2652 | *err_info = NULL((void*)0); | |||
2653 | if (wdh->subtype_finish != NULL((void*)0)) { | |||
2654 | /* There's a finish routine for this dump stream. */ | |||
2655 | if (!(wdh->subtype_finish)(wdh, err, err_info)) | |||
2656 | ret = false0; | |||
2657 | } | |||
2658 | errno(*__errno_location ()) = WTAP_ERR_CANT_CLOSE-11; | |||
2659 | if (wtap_dump_file_close(wdh) == EOF(-1)) { | |||
2660 | if (ret) { | |||
2661 | /* The per-format finish function succeeded, | |||
2662 | but the stream close didn't. Save the | |||
2663 | reason why, if our caller asked for it. */ | |||
2664 | if (err != NULL((void*)0)) | |||
2665 | *err = errno(*__errno_location ()); | |||
2666 | } | |||
2667 | ret = false0; | |||
2668 | } | |||
2669 | if (needs_reload != NULL((void*)0)) | |||
2670 | *needs_reload = wdh->needs_reload; | |||
2671 | g_free(wdh->priv); | |||
2672 | wtap_block_array_free(wdh->interface_data); | |||
2673 | wtap_block_array_unref(wdh->dsbs_initial); | |||
2674 | g_free(wdh); | |||
2675 | return ret; | |||
2676 | } | |||
2677 | ||||
2678 | int | |||
2679 | wtap_dump_file_type_subtype(const wtap_dumper *wdh) | |||
2680 | { | |||
2681 | return wdh->file_type_subtype; | |||
2682 | } | |||
2683 | ||||
2684 | int64_t | |||
2685 | wtap_get_bytes_dumped(const wtap_dumper *wdh) | |||
2686 | { | |||
2687 | return wdh->bytes_dumped; | |||
2688 | } | |||
2689 | ||||
2690 | void | |||
2691 | wtap_set_bytes_dumped(wtap_dumper *wdh, int64_t bytes_dumped) | |||
2692 | { | |||
2693 | wdh->bytes_dumped = bytes_dumped; | |||
2694 | } | |||
2695 | ||||
2696 | bool_Bool | |||
2697 | wtap_addrinfo_list_empty(const addrinfo_lists_t *addrinfo_lists) | |||
2698 | { | |||
2699 | return (addrinfo_lists == NULL((void*)0)) || | |||
2700 | ((addrinfo_lists->ipv4_addr_list == NULL((void*)0)) && | |||
2701 | (addrinfo_lists->ipv6_addr_list == NULL((void*)0))); | |||
2702 | } | |||
2703 | ||||
2704 | bool_Bool | |||
2705 | wtap_dump_set_addrinfo_list(wtap_dumper *wdh, addrinfo_lists_t *addrinfo_lists) | |||
2706 | { | |||
2707 | if (!wdh || wdh->file_type_subtype < 0 || | |||
2708 | wdh->file_type_subtype >= (int)file_type_subtype_table_arr->len || | |||
2709 | wtap_file_type_subtype_supports_block(wdh->file_type_subtype, WTAP_BLOCK_NAME_RESOLUTION) == BLOCK_NOT_SUPPORTED) | |||
2710 | return false0; | |||
2711 | wdh->addrinfo_lists = addrinfo_lists; | |||
2712 | return true1; | |||
2713 | } | |||
2714 | ||||
2715 | void | |||
2716 | wtap_dump_discard_name_resolution(wtap_dumper *wdh) | |||
2717 | { | |||
2718 | /* As below for DSBs. */ | |||
2719 | if (wdh->nrbs_growing) { | |||
2720 | /* | |||
2721 | * Pretend we've written all of them. | |||
2722 | */ | |||
2723 | wdh->nrbs_growing_written = wdh->nrbs_growing->len; | |||
2724 | } | |||
2725 | } | |||
2726 | ||||
2727 | void | |||
2728 | wtap_dump_discard_decryption_secrets(wtap_dumper *wdh) | |||
2729 | { | |||
2730 | /* | |||
2731 | * This doesn't free the data, as it might be pointed to | |||
2732 | * from other structures; it merely marks all of them as | |||
2733 | * having been written to the file, so that they don't | |||
2734 | * get written by wtap_dump(). | |||
2735 | * | |||
2736 | * XXX - our APIs for dealing with some metadata, such as | |||
2737 | * resolved names, decryption secrets, and interface | |||
2738 | * statistics is not very well oriented towards one-pass | |||
2739 | * programs; this needs to be cleaned up. See bug 15502. | |||
2740 | */ | |||
2741 | if (wdh->dsbs_growing) { | |||
2742 | /* | |||
2743 | * Pretend we've written all of them. | |||
2744 | */ | |||
2745 | wdh->dsbs_growing_written = wdh->dsbs_growing->len; | |||
2746 | } | |||
2747 | } | |||
2748 | ||||
2749 | void | |||
2750 | wtap_dump_discard_sysdig_meta_events(wtap_dumper *wdh) | |||
2751 | { | |||
2752 | /* As above for DSBs. */ | |||
2753 | if (wdh->mevs_growing) { | |||
2754 | /* | |||
2755 | * Pretend we've written all of them. | |||
2756 | */ | |||
2757 | wdh->mevs_growing_written = wdh->mevs_growing->len; | |||
2758 | } | |||
2759 | } | |||
2760 | ||||
2761 | /* internally open a file for writing (compressed or not) */ | |||
2762 | static WFILE_T | |||
2763 | wtap_dump_file_open(const wtap_dumper *wdh, const char *filename) | |||
2764 | { | |||
2765 | switch (wdh->compression_type) { | |||
2766 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) | |||
2767 | case WTAP_GZIP_COMPRESSED: | |||
2768 | return gzwfile_open(filename); | |||
2769 | #endif /* defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG) */ | |||
2770 | #ifdef HAVE_LZ4FRAME_H1 | |||
2771 | case WTAP_LZ4_COMPRESSED: | |||
2772 | return lz4wfile_open(filename); | |||
2773 | #endif /* HAVE_LZ4FRAME_H */ | |||
2774 | default: | |||
2775 | return ws_fopenfopen(filename, "wb"); | |||
2776 | } | |||
2777 | } | |||
2778 | ||||
2779 | /* internally open a file for writing (compressed or not) */ | |||
2780 | static WFILE_T | |||
2781 | wtap_dump_file_fdopen(const wtap_dumper *wdh, int fd) | |||
2782 | { | |||
2783 | switch (wdh->compression_type) { | |||
2784 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) | |||
2785 | case WTAP_GZIP_COMPRESSED: | |||
2786 | return gzwfile_fdopen(fd); | |||
2787 | #endif /* defined (HAVE_ZLIB) || defined (HAVE_ZLIBNG) */ | |||
2788 | #ifdef HAVE_LZ4FRAME_H1 | |||
2789 | case WTAP_LZ4_COMPRESSED: | |||
2790 | return lz4wfile_fdopen(fd); | |||
2791 | #endif /* HAVE_LZ4FRAME_H */ | |||
2792 | default: | |||
2793 | return ws_fdopenfdopen(fd, "wb"); | |||
2794 | } | |||
2795 | } | |||
2796 | ||||
2797 | /* internally writing raw bytes (compressed or not). Updates wdh->bytes_dumped on success */ | |||
2798 | bool_Bool | |||
2799 | wtap_dump_file_write(wtap_dumper *wdh, const void *buf, size_t bufsize, int *err) | |||
2800 | { | |||
2801 | size_t nwritten; | |||
2802 | ||||
2803 | switch (wdh->compression_type) { | |||
2804 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) | |||
2805 | case WTAP_GZIP_COMPRESSED: | |||
2806 | nwritten = gzwfile_write((GZWFILE_T)wdh->fh, buf, (unsigned int) bufsize); | |||
2807 | /* | |||
2808 | * gzwfile_write() returns 0 on error. | |||
2809 | */ | |||
2810 | if (nwritten == 0) { | |||
2811 | *err = gzwfile_geterr((GZWFILE_T)wdh->fh); | |||
2812 | return false0; | |||
2813 | } | |||
2814 | break; | |||
2815 | #endif | |||
2816 | #ifdef HAVE_LZ4FRAME_H1 | |||
2817 | case WTAP_LZ4_COMPRESSED: | |||
2818 | nwritten = lz4wfile_write((LZ4WFILE_T)wdh->fh, buf, bufsize); | |||
2819 | /* | |||
2820 | * lz4wfile_write() returns 0 on error. | |||
2821 | */ | |||
2822 | if (nwritten == 0) { | |||
2823 | *err = lz4wfile_geterr((LZ4WFILE_T)wdh->fh); | |||
2824 | return false0; | |||
2825 | } | |||
2826 | break; | |||
2827 | #endif /* HAVE_LZ4FRAME_H */ | |||
2828 | default: | |||
2829 | errno(*__errno_location ()) = WTAP_ERR_CANT_WRITE-10; | |||
2830 | nwritten = fwrite(buf, 1, bufsize, (FILE *)wdh->fh); | |||
2831 | /* | |||
2832 | * At least according to the macOS man page, | |||
2833 | * this can return a short count on an error. | |||
2834 | */ | |||
2835 | if (nwritten != bufsize) { | |||
2836 | if (ferror((FILE *)wdh->fh)) | |||
2837 | *err = errno(*__errno_location ()); | |||
2838 | else | |||
2839 | *err = WTAP_ERR_SHORT_WRITE-14; | |||
2840 | return false0; | |||
2841 | } | |||
2842 | } | |||
2843 | wdh->bytes_dumped += bufsize; | |||
2844 | return true1; | |||
2845 | } | |||
2846 | ||||
2847 | /* internally close a file for writing (compressed or not) */ | |||
2848 | static int | |||
2849 | wtap_dump_file_close(wtap_dumper *wdh) | |||
2850 | { | |||
2851 | switch (wdh->compression_type) { | |||
2852 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) | |||
2853 | case WTAP_GZIP_COMPRESSED: | |||
2854 | return gzwfile_close((GZWFILE_T)wdh->fh); | |||
2855 | #endif | |||
2856 | #ifdef HAVE_LZ4FRAME_H1 | |||
2857 | case WTAP_LZ4_COMPRESSED: | |||
2858 | return lz4wfile_close((LZ4WFILE_T)wdh->fh); | |||
2859 | #endif /* HAVE_LZ4FRAME_H */ | |||
2860 | default: | |||
2861 | return fclose((FILE *)wdh->fh); | |||
2862 | } | |||
2863 | } | |||
2864 | ||||
2865 | int64_t | |||
2866 | wtap_dump_file_seek(wtap_dumper *wdh, int64_t offset, int whence, int *err) | |||
2867 | { | |||
2868 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) || defined (HAVE_LZ4FRAME_H1) | |||
2869 | if (wdh->compression_type != WTAP_UNCOMPRESSED) { | |||
2870 | *err = WTAP_ERR_CANT_SEEK_COMPRESSED-19; | |||
2871 | return -1; | |||
2872 | } else | |||
2873 | #endif | |||
2874 | { | |||
2875 | if (-1 == ws_fseek64fseeko((FILE *)wdh->fh, offset, whence)) { | |||
2876 | *err = errno(*__errno_location ()); | |||
2877 | return -1; | |||
2878 | } else | |||
2879 | { | |||
2880 | return 0; | |||
2881 | } | |||
2882 | } | |||
2883 | } | |||
2884 | ||||
2885 | int64_t | |||
2886 | wtap_dump_file_tell(wtap_dumper *wdh, int *err) | |||
2887 | { | |||
2888 | int64_t rval; | |||
2889 | #if defined (HAVE_ZLIB1) || defined (HAVE_ZLIBNG) || defined (HAVE_LZ4FRAME_H1) | |||
2890 | if (wdh->compression_type != WTAP_UNCOMPRESSED) { | |||
2891 | *err = WTAP_ERR_CANT_SEEK_COMPRESSED-19; | |||
2892 | return -1; | |||
2893 | } else | |||
2894 | #endif | |||
2895 | { | |||
2896 | if (-1 == (rval = ws_ftell64ftello((FILE *)wdh->fh))) { | |||
2897 | *err = errno(*__errno_location ()); | |||
2898 | return -1; | |||
2899 | } else | |||
2900 | { | |||
2901 | return rval; | |||
2902 | } | |||
2903 | } | |||
2904 | } | |||
2905 | ||||
2906 | void | |||
2907 | cleanup_open_routines(void) | |||
2908 | { | |||
2909 | unsigned i; | |||
2910 | struct open_info *i_open; | |||
2911 | ||||
2912 | if (open_routines != NULL((void*)0) && open_info_arr) { | |||
2913 | for (i = 0, i_open = open_routines; i < open_info_arr->len; i++, i_open++) { | |||
2914 | if (i_open->extensions != NULL((void*)0)) | |||
2915 | g_strfreev(i_open->extensions_set); | |||
2916 | } | |||
2917 | ||||
2918 | g_array_free(open_info_arr, true1); | |||
2919 | open_info_arr = NULL((void*)0); | |||
2920 | } | |||
2921 | } | |||
2922 | ||||
2923 | /* | |||
2924 | * Allow built-in file handlers (but *not* plugin file handlers!) to | |||
2925 | * register a "backwards-compatibility" name and file type value, to | |||
2926 | * put in the Lua wtap_filetypes table. | |||
2927 | * | |||
2928 | * This is only to be used as long as we have that table; new Lua | |||
2929 | * code should use wtap_name_to_file_type_subtype() to look up | |||
2930 | * file types by their name, just as C code should. | |||
2931 | * | |||
2932 | * The backwards-ccmpatibility names are the old WTAP_FILE_TYPE_SUBTYPE_ | |||
2933 | * #define name, with WTAP_FILE_TYPE_SUBTYPE_ removed. | |||
2934 | */ | |||
2935 | ||||
2936 | static GArray *backwards_compatibility_lua_names; | |||
2937 | ||||
2938 | void | |||
2939 | // NOLINTNEXTLINE(misc-no-recursion) | |||
2940 | wtap_register_backwards_compatibility_lua_name(const char *name, int ft) | |||
2941 | { | |||
2942 | struct backwards_compatibiliity_lua_name entry; | |||
2943 | ||||
2944 | /* | |||
2945 | * Create the table if it doesn't already exist. | |||
2946 | * Use the same size as we do for the file type/subtype table. | |||
2947 | */ | |||
2948 | if (backwards_compatibility_lua_names == NULL((void*)0)) { | |||
2949 | backwards_compatibility_lua_names = g_array_sized_new(false0, | |||
2950 | true1, sizeof(struct backwards_compatibiliity_lua_name), | |||
2951 | wtap_module_count*2); | |||
2952 | ||||
2953 | /* | |||
2954 | * Extra backwards compatibility hack - add entries | |||
2955 | * for time stamp precision values(!), as well as | |||
2956 | * for "UNKNOWN" and types that don't yet register | |||
2957 | * themselves. | |||
2958 | * | |||
2959 | * If new WS_TSPREC_ value are added, don't bother | |||
2960 | * adding them to this table; any Lua program that | |||
2961 | * would use them should use the wtap_tsprecs type. | |||
2962 | * | |||
2963 | * (Recursion: see "recursion".) | |||
2964 | */ | |||
2965 | wtap_register_backwards_compatibility_lua_name("TSPREC_SEC", | |||
2966 | WTAP_TSPREC_SEC0); | |||
2967 | wtap_register_backwards_compatibility_lua_name("TSPREC_DSEC", | |||
2968 | WTAP_TSPREC_100_MSEC1); | |||
2969 | wtap_register_backwards_compatibility_lua_name("TSPREC_CSEC", | |||
2970 | WTAP_TSPREC_10_MSEC2); | |||
2971 | wtap_register_backwards_compatibility_lua_name("TSPREC_MSEC", | |||
2972 | WTAP_TSPREC_MSEC3); | |||
2973 | wtap_register_backwards_compatibility_lua_name("TSPREC_USEC", | |||
2974 | WTAP_TSPREC_USEC6); | |||
2975 | wtap_register_backwards_compatibility_lua_name("TSPREC_NSEC", | |||
2976 | WTAP_TSPREC_NSEC9); | |||
2977 | wtap_register_backwards_compatibility_lua_name("UNKNOWN", | |||
2978 | WTAP_FILE_TYPE_SUBTYPE_UNKNOWN-1); | |||
2979 | } | |||
2980 | entry.name = name; | |||
2981 | entry.ft = ft; | |||
2982 | g_array_append_val(backwards_compatibility_lua_names, entry)g_array_append_vals (backwards_compatibility_lua_names, & (entry), 1); | |||
2983 | } | |||
2984 | ||||
2985 | const GArray * | |||
2986 | get_backwards_compatibility_lua_table(void) | |||
2987 | { | |||
2988 | return backwards_compatibility_lua_names; | |||
2989 | } | |||
2990 | ||||
2991 | /* | |||
2992 | * Editor modelines - https://www.wireshark.org/tools/modelines.html | |||
2993 | * | |||
2994 | * Local variables: | |||
2995 | * c-basic-offset: 8 | |||
2996 | * tab-width: 8 | |||
2997 | * indent-tabs-mode: t | |||
2998 | * End: | |||
2999 | * | |||
3000 | * vi: set shiftwidth=8 tabstop=8 noexpandtab: | |||
3001 | * :indentSize=8:tabSize=8:noTabs=false: | |||
3002 | */ |