opa: Add dissectors for Intel’s Omni-Path Architecture (OPA)
[metze/wireshark/wip.git] / wiretap / mime_file.c
index 3601f23952ace4955afdfe3cfb33820cfae80c22..17bb9608a5c34f5d43248fb47f2eac39c541a0a9 100644 (file)
@@ -44,7 +44,7 @@
 
 #include "wtap-int.h"
 #include "file_wrappers.h"
-#include "buffer.h"
+#include <wsutil/buffer.h>
 #include "mime_file.h"
 
 typedef struct {
@@ -70,6 +70,14 @@ static const guint8 png_magic[]    = { 0x89, 'P', 'N', 'G', '\r', '\n', 0x1A, '\
 static const guint8 gif87a_magic[] = { 'G', 'I', 'F', '8', '7', 'a'};
 static const guint8 gif89a_magic[] = { 'G', 'I', 'F', '8', '9', 'a'};
 static const guint8 elf_magic[]    = { 0x7F, 'E', 'L', 'F'};
+static const guint8 btsnoop_magic[]    = { 'b', 't', 's', 'n', 'o', 'o', 'p', 0};
+static const guint8 pcap_magic[]           = { 0xA1, 0xB2, 0xC3, 0xD4 };
+static const guint8 pcap_swapped_magic[]   = { 0xD4, 0xC3, 0xB2, 0xA1 };
+static const guint8 pcapng_premagic[]      = { 0x0A, 0x0D, 0x0D, 0x0A };
+
+/* File does not start with it */
+static const guint8 pcapng_xmagic[]         = { 0x1A, 0x2B, 0x3C, 0x4D };
+static const guint8 pcapng_swapped_xmagic[] = { 0x4D, 0x3C, 0x2B, 0x1A };
 
 static const mime_files_t magic_files[] = {
        { jpeg_jfif_magic, sizeof(jpeg_jfif_magic) },
@@ -77,7 +85,11 @@ static const mime_files_t magic_files[] = {
        { png_magic, sizeof(png_magic) },
        { gif87a_magic, sizeof(gif87a_magic) },
        { gif89a_magic, sizeof(gif89a_magic) },
-       { elf_magic, sizeof(elf_magic) }
+       { elf_magic, sizeof(elf_magic) },
+       { btsnoop_magic, sizeof(btsnoop_magic) },
+       { pcap_magic, sizeof(pcap_magic) },
+       { pcap_swapped_magic, sizeof(pcap_swapped_magic) },
+       { pcapng_premagic, sizeof(pcapng_premagic) }
 };
 
 #define        N_MAGIC_TYPES   (sizeof(magic_files) / sizeof(magic_files[0]))
@@ -89,10 +101,8 @@ static const mime_files_t magic_files[] = {
  * reassembly, it would *still* have to allocate a buffer the size of
  * the file, so it's not as if we'd neve try to allocate a buffer the
  * size of the file.
- *
- * For now, go for 16MB.
  */
-#define MAX_FILE_SIZE  (16*1024*1024)
+#define MAX_FILE_SIZE  G_MAXINT
 
 static gboolean
 mime_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
@@ -116,6 +126,7 @@ mime_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
        }
        packet_size = (int)file_size;
 
+       phdr->rec_type = REC_TYPE_PACKET;
        phdr->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */
 
        phdr->caplen = packet_size;
@@ -160,7 +171,7 @@ mime_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr, Buffer *buf
        return mime_read_file(wth, wth->random_fh, phdr, buf, err, err_info);
 }
 
-int
+wtap_open_return_val
 mime_file_open(wtap *wth, int *err, gchar **err_info)
 {
        char magic_buf[128]; /* increase buffer size when needed */
@@ -169,7 +180,7 @@ mime_file_open(wtap *wth, int *err, gchar **err_info)
        /* guint file_ok; */
        guint i;
 
-       guint read_bytes = 0;
+       guint read_bytes = 12;
 
        for (i = 0; i < N_MAGIC_TYPES; i++)
                read_bytes = MAX(read_bytes, magic_files[i].magic_len);
@@ -179,34 +190,51 @@ mime_file_open(wtap *wth, int *err, gchar **err_info)
 
        if (bytes_read < 0) {
                *err = file_error(wth->fh, err_info);
-               return -1;
+               return WTAP_OPEN_ERROR;
        }
        if (bytes_read == 0)
-               return 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;
-                               /* file_ok = i; */
                        } else
-                               return 0;       /* many files matched, bad file */
+                               return WTAP_OPEN_NOT_MINE;      /* many files matched, bad file */
                }
        }
 
        if (!found_file)
-               return 0;
+               return WTAP_OPEN_NOT_MINE;
 
        if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
-               return -1;
+               return WTAP_OPEN_ERROR;
 
        wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_MIME;
        wth->file_encap = WTAP_ENCAP_MIME;
-       wth->tsprecision = WTAP_FILE_TSPREC_SEC;
+       wth->file_tsprec = WTAP_TSPREC_SEC;
        wth->subtype_read = mime_read;
        wth->subtype_seek_read = mime_seek_read;
        wth->snapshot_length = 0;
 
-       return 1;
+       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:
+ */