Add svn:eol-style.
[metze/wireshark/wip.git] / wiretap / wtap.c
index 204c1e2b003f280ce792c77172d6ff667f39dd3b..4b9a32677a406f22f8fa5e7e72b7f778bc538727 100644 (file)
@@ -20,9 +20,7 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  */
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 
 #include <string.h>
 #include <errno.h>
@@ -73,9 +71,9 @@ wtap_fstat(wtap *wth, ws_statb64 *statb, int *err)
 }
 
 int
-wtap_file_type(wtap *wth)
+wtap_file_type_subtype(wtap *wth)
 {
-       return wth->file_type;
+       return wth->file_type_subtype;
 }
 
 gboolean
@@ -580,7 +578,7 @@ static struct encap_type_info encap_table_base[] = {
 
        /* WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR */
        { "BACnet MS/TP with Directional Info", "bacnet-ms-tp-with-direction" },
+
        /* WTAP_ENCAP_IXVERIWAVE */
        { "IxVeriWave header and stats block", "ixveriwave" },
 
@@ -598,11 +596,35 @@ static struct encap_type_info encap_table_base[] = {
 
        /* WTAP_ENCAP_AX25 */
        { "Amateur Radio AX.25", "ax25" },
+
+       /* WTAP_ENCAP_SCTP */
+       { "SCTP", "sctp" },
+
+       /* WTAP_ENCAP_INFINIBAND */
+       { "InfiniBand", "infiniband" },
+
+       /* WTAP_ENCAP_JUNIPER_SVCS */
+       { "Juniper Services", "juniper-svcs" },
+
+       /* WTAP_ENCAP_USBPCAP */
+       { "USB packets with USBPcap header", "usb-usbpcap" },
+
+       /* WTAP_ENCAP_RTAC_SERIAL */
+       { "RTAC serial-line", "rtac-serial" },
+
+       /* WTAP_ENCAP_BLUETOOTH_LE_LL */
+       { "Bluetooth Low Energy Link Layer", "bluetooth-le-ll" },
+
+       /* WTAP_ENCAP_WIRESHARK_UPPER_PDU */
+       { "Wireshark Upper PDU export", "wireshark-upper-pdu" },
 };
 
+WS_DLL_LOCAL
 gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
 static GArray* encap_table_arr = NULL;
-static const struct encap_type_info* encap_table = NULL;
+
+#define encap_table_entry(encap)       \
+       g_array_index(encap_table_arr, struct encap_type_info, encap)
 
 static void wtap_init_encap_types(void) {
 
@@ -611,8 +633,6 @@ static void wtap_init_encap_types(void) {
        encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
 
        g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
-
-       encap_table = (void*)encap_table_arr->data;
 }
 
 int wtap_get_num_encap_types(void) {
@@ -621,7 +641,7 @@ int wtap_get_num_encap_types(void) {
 }
 
 
-int wtap_register_encap_type(char* name, char* short_name) {
+int wtap_register_encap_type(const char* name, const char* short_name) {
        struct encap_type_info e;
        wtap_init_encap_types();
 
@@ -630,8 +650,6 @@ int wtap_register_encap_type(char* name, char* short_name) {
 
        g_array_append_val(encap_table_arr,e);
 
-       encap_table = (void*)encap_table_arr->data;
-
        return wtap_num_encap_types++;
 }
 
@@ -645,7 +663,7 @@ wtap_encap_string(int encap)
        else if (encap == WTAP_ENCAP_PER_PACKET)
                return "Per packet";
        else
-               return encap_table[encap].name;
+               return encap_table_entry(encap).name;
 }
 
 /* Name to use in, say, a command-line flag specifying the type. */
@@ -657,7 +675,7 @@ wtap_encap_short_string(int encap)
        else if (encap == WTAP_ENCAP_PER_PACKET)
                return "per-packet";
        else
-               return encap_table[encap].short_name;
+               return encap_table_entry(encap).short_name;
 }
 
 /* Translate a short name to a capture file type. */
@@ -667,8 +685,8 @@ wtap_short_string_to_encap(const char *short_name)
        int encap;
 
        for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
-               if (encap_table[encap].short_name != NULL &&
-                   strcmp(short_name, encap_table[encap].short_name) == 0)
+               if (encap_table_entry(encap).short_name != NULL &&
+                   strcmp(short_name, encap_table_entry(encap).short_name) == 0)
                        return encap;
        }
        return -1;      /* no such encapsulation type */
@@ -695,6 +713,7 @@ static const char *wtap_errlist[] = {
        "The standard input cannot be opened for random access",
        "That file format doesn't support compression",
        NULL,
+       NULL,
        "Uncompression error",
        "Internal error"
 };
@@ -886,6 +905,34 @@ wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
        return TRUE;    /* success */
 }
 
+/*
+ * Read packet data into a Buffer, growing the buffer as necessary.
+ *
+ * This returns an error on a short read, even if the short read hit
+ * the EOF immediately.  (The assumption is that each packet has a
+ * header followed by raw packet data, and that we've already read the
+ * header, so if we get an EOF trying to read the packet data, the file
+ * has been cut short, even if the read didn't read any data at all.)
+ */
+gboolean
+wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
+    gchar **err_info)
+{
+       int     bytes_read;
+
+       buffer_assure_space(buf, length);
+       errno = WTAP_ERR_CANT_READ;
+       bytes_read = file_read(buffer_start_ptr(buf), length, fh);
+
+       if (bytes_read < 0 || (guint)bytes_read != length) {
+               *err = file_error(fh, err_info);
+               if (*err == 0)
+                       *err = WTAP_ERR_SHORT_READ;
+               return FALSE;
+       }
+       return TRUE;
+}
+
 /*
  * Return an approximation of the amount of data we've read sequentially
  * from the file so far.  (gint64, in case that's 64 bits.)
@@ -902,12 +949,6 @@ wtap_phdr(wtap *wth)
        return &wth->phdr;
 }
 
-union wtap_pseudo_header *
-wtap_pseudoheader(wtap *wth)
-{
-       return &wth->pseudo_header;
-}
-
 guint8 *
 wtap_buf_ptr(wtap *wth)
 {
@@ -916,9 +957,9 @@ wtap_buf_ptr(wtap *wth)
 
 gboolean
 wtap_seek_read(wtap *wth, gint64 seek_off,
-       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
+       struct wtap_pkthdr *phdr, Buffer *buf, int len,
        int *err, gchar **err_info)
 {
-       return wth->subtype_seek_read(wth, seek_off, pseudo_header, pd, len,
+       return wth->subtype_seek_read(wth, seek_off, phdr, buf, len,
                err, err_info);
 }