/* mime_file.c * * MIME file format decoder for the Wiretap library. * * This is for use with Wireshark dissectors that handle file * formats (e.g., because they handle a particular MIME media type). * It breaks the file into chunks of at most WTAP_MAX_PACKET_SIZE_STANDARD, * each of which is reported as a packet, so that files larger than * WTAP_MAX_PACKET_SIZE_STANDARD can be handled by reassembly. * * The "MIME file" dissector does the reassembly, and hands the result * off to heuristic dissectors to try to identify the file's contents. * * Wiretap Library * * SPDX-License-Identifier: GPL-2.0-or-later */ #include "config.h" #include #ifdef HAVE_UNISTD_H #include #endif #include #include #include #include #include "wtap-int.h" #include "file_wrappers.h" #include #include "mime_file.h" typedef struct { const guint8 *magic; guint magic_len; } mime_files_t; /* * Written by Marton Nemeth * Copyright 2009 Marton Nemeth * The JPEG and JFIF specification can be found at: * * http://www.jpeg.org/public/jfif.pdf * http://www.w3.org/Graphics/JPEG/itu-t81.pdf */ static const guint8 jpeg_jfif_magic[] = { 0xFF, 0xD8, /* SOF */ 0xFF /* start of the next marker */ }; /* fh); if (bytes_read < 0) { *err = file_error(wth->fh, err_info); return WTAP_OPEN_ERROR; } if (bytes_read == 0) return WTAP_OPEN_NOT_MINE; found_file = FALSE; for (i = 0; i < N_MAGIC_TYPES; i++) { if ((guint) bytes_read >= magic_files[i].magic_len && !memcmp(magic_buf, magic_files[i].magic, MIN(magic_files[i].magic_len, (guint) bytes_read))) { if (!found_file) { if (magic_files[i].magic == pcapng_premagic) { if (memcmp(magic_buf + 8, pcapng_xmagic, sizeof(pcapng_xmagic)) && memcmp(magic_buf + 8, pcapng_swapped_xmagic, sizeof(pcapng_swapped_xmagic))) continue; } found_file = TRUE; } else return WTAP_OPEN_NOT_MINE; /* many files matched, bad file */ } } if (!found_file) return WTAP_OPEN_NOT_MINE; if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) return WTAP_OPEN_ERROR; wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MIME; wth->file_encap = WTAP_ENCAP_MIME; wth->file_tsprec = WTAP_TSPREC_SEC; wth->subtype_read = wtap_full_file_read; wth->subtype_seek_read = wtap_full_file_seek_read; wth->snapshot_length = 0; return WTAP_OPEN_MINE; } /* * Editor modelines - http://www.wireshark.org/tools/modelines.html * * Local variables: * c-basic-offset: 8 * tab-width: 8 * indent-tabs-mode: t * End: * * vi: set shiftwidth=8 tabstop=8 noexpandtab: * :indentSize=8:tabSize=8:noTabs=false: */