Avoid undefined behavior in overflow check
[metze/wireshark/wip.git] / wiretap / wtap-int.h
index fa22a8df652e850e19bdad73ffa175e63cfdb105..332d836c57813c9049785fd14864bf6c3ba7d856 100644 (file)
@@ -1,6 +1,4 @@
 /* wtap-int.h
- *
- * $Id$
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
 #ifndef __WTAP_INT_H__
 #define __WTAP_INT_H__
 
-#ifdef HAVE_SYS_TIME_H
-#include <sys/time.h>
-#endif
-
 #include <glib.h>
-#include <stdio.h>
 #include <time.h>
 
 #ifdef HAVE_WINSOCK2_H
@@ -45,22 +38,23 @@ int wtap_fstat(wtap *wth, ws_statb64 *statb, int *err);
 typedef gboolean (*subtype_read_func)(struct wtap*, int*, char**, gint64*);
 typedef gboolean (*subtype_seek_read_func)(struct wtap*, gint64,
                                            struct wtap_pkthdr *, Buffer *buf,
-                                           int, int *, char **);
+                                           int *, char **);
 /**
  * Struct holding data of the currently read file.
  */
 struct wtap {
     FILE_T                      fh;
     FILE_T                      random_fh;              /**< Secondary FILE_T for random access */
-    int                         file_type;
+    int                         file_type_subtype;
     guint                       snapshot_length;
     struct Buffer               *frame_buffer;
     struct wtap_pkthdr          phdr;
     struct wtapng_section_s     shb_hdr;
-    guint                       number_of_interfaces;   /**< The number of interfaces a capture was made on, number of IDB:s in a pcapng file or equivalent(?)*/
     GArray                      *interface_data;        /**< An array holding the interface data from pcapng IDB:s or equivalent(?)*/
+    wtapng_name_res_t           *nrb_hdr;               /**< holds the Name Res Block's comment/custom_opts, or NULL */
 
-    void                        *priv;
+    void                        *priv;          /* this one holds per-file state and is free'd automatically by wtap_close() */
+    void                        *wslua_data;    /* this one holds wslua state info and is not free'd */
 
     subtype_read_func           subtype_read;
     subtype_seek_read_func      subtype_seek_read;
@@ -69,10 +63,18 @@ struct wtap {
     int                         file_encap;    /* per-file, for those
                                                 * file formats that have
                                                 * per-file encapsulation
-                                                * types
+                                                * types rather than per-packet
+                                                * encapsulation types
                                                 */
-    int                         tsprecision;   /* timestamp precision of the lower 32bits
-                                                * e.g. WTAP_FILE_TSPREC_USEC
+    int                         file_tsprec;   /* per-file timestamp precision
+                                                * of the fractional part of
+                                                * the time stamp, for those
+                                                * file formats that have
+                                                * per-file timestamp
+                                                * precision rather than
+                                                * per-packet timestamp
+                                                * precision
+                                                * e.g. WTAP_TSPREC_USEC
                                                 */
     wtap_new_ipv4_callback_t    add_new_ipv4;
     wtap_new_ipv6_callback_t    add_new_ipv6;
@@ -88,57 +90,42 @@ typedef void *WFILE_T;
 
 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
                                        const struct wtap_pkthdr*,
-                                       const guint8*, int*);
-typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
+                                       const guint8*, int*, gchar**);
+typedef gboolean (*subtype_finish_func)(struct wtap_dumper*, int*);
 
 struct wtap_dumper {
     WFILE_T                 fh;
-    int                     file_type;
+    gboolean                is_stdout;      /* TRUE if we're writing to the standard output */
+    int                     file_type_subtype;
     int                     snaplen;
     int                     encap;
     gboolean                compressed;
     gint64                  bytes_dumped;
 
-    void                    *priv;
+    void                    *priv;          /* this one holds per-file state and is free'd automatically by wtap_dump_close() */
+    void                    *wslua_data;    /* this one holds wslua state info and is not free'd */
 
-    subtype_write_func      subtype_write;
-    subtype_close_func      subtype_close;
+    subtype_write_func      subtype_write;  /* write out a record */
+    subtype_finish_func     subtype_finish; /* write out information to finish writing file */
 
     int                     tsprecision;    /**< timestamp precision of the lower 32bits
-                                             * e.g. WTAP_FILE_TSPREC_USEC
+                                             * e.g. WTAP_TSPREC_USEC
                                              */
-    struct addrinfo         *addrinfo_list;
-    struct wtapng_section_s *shb_hdr;
-    guint                   number_of_interfaces;   /**< The number of interfaces a capture was made on, number of IDB:s in a pcapng file or equivalent(?)*/
-    GArray                  *interface_data;        /**< An array holding the interface data from pcapng IDB:s or equivalent(?) NULL if not present.*/
+    addrinfo_lists_t        *addrinfo_lists; /**< Struct containing lists of resolved addresses */
+    wtapng_section_t        *shb_hdr;
+    wtapng_name_res_t       *nrb_hdr;        /**< name resolution comment/custom_opt, or NULL */
+    GArray                  *interface_data; /**< An array holding the interface data from pcapng IDB:s or equivalent(?) NULL if not present.*/
 };
 
-gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
+WS_DLL_PUBLIC gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
     size_t bufsize, int *err);
-gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err);
-gint64 wtap_dump_file_tell(wtap_dumper *wdh, int *err);
+WS_DLL_PUBLIC gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err);
+WS_DLL_PUBLIC gint64 wtap_dump_file_tell(wtap_dumper *wdh, int *err);
 
 
 extern gint wtap_num_file_types;
 
-/* Macros to byte-swap 64-bit, 32-bit and 16-bit quantities. */
-#define BSWAP64(x) \
-    ((((x)&G_GINT64_CONSTANT(0xFF00000000000000U))>>56) |    \
-         (((x)&G_GINT64_CONSTANT(0x00FF000000000000U))>>40) |    \
-     (((x)&G_GINT64_CONSTANT(0x0000FF0000000000U))>>24) |    \
-     (((x)&G_GINT64_CONSTANT(0x000000FF00000000U))>>8) |    \
-     (((x)&G_GINT64_CONSTANT(0x00000000FF000000U))<<8) |    \
-     (((x)&G_GINT64_CONSTANT(0x0000000000FF0000U))<<24) |    \
-     (((x)&G_GINT64_CONSTANT(0x000000000000FF00U))<<40) |    \
-     (((x)&G_GINT64_CONSTANT(0x00000000000000FFU))<<56))
-#define    BSWAP32(x) \
-    ((((x)&0xFF000000)>>24) | \
-     (((x)&0x00FF0000)>>8) | \
-     (((x)&0x0000FF00)<<8) | \
-     (((x)&0x000000FF)<<24))
-#define    BSWAP16(x) \
-     ((((x)&0xFF00)>>8) | \
-      (((x)&0x00FF)<<8))
+#include <wsutil/pint.h>
 
 /* Macros to byte-swap possibly-unaligned 64-bit, 32-bit and 16-bit quantities;
  * they take a pointer to the quantity, and byte-swap it in place.
@@ -177,82 +164,6 @@ extern gint wtap_num_file_types;
         (p)[0] = tmp;      \
     }
 
-/* Turn host-byte-order values into little-endian values. */
-#define htoles(s) GUINT16_TO_LE(s)
-#define htolel(l) GUINT32_TO_LE(l)
-#define htolell(ll) GUINT64_TO_LE(ll)
-
-/* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
- * byte array, returns the value of the two or four bytes at the pointer.
- * The pletoh[sl] versions return the little-endian representation.
- * We also provide pntohll and pletohll, which extract 64-bit integral
- * quantities.
- *
- * These will work regardless of the byte alignment of the pointer.
- */
-
-#ifndef pntohs
-#define pntohs(p)  ((guint16)                       \
-                    ((guint16)*((const guint8 *)(p)+0)<<8|  \
-                     (guint16)*((const guint8 *)(p)+1)<<0))
-#endif
-
-#ifndef pntoh24
-#define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16| \
-                     (guint32)*((const guint8 *)(p)+1)<<8|  \
-                     (guint32)*((const guint8 *)(p)+2)<<0)
-#endif
-
-#ifndef pntohl
-#define pntohl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
-                    (guint32)*((const guint8 *)(p)+1)<<16|  \
-                    (guint32)*((const guint8 *)(p)+2)<<8|   \
-                    (guint32)*((const guint8 *)(p)+3)<<0)
-#endif
-
-#ifndef pntohll
-#define pntohll(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
-                     (guint64)*((const guint8 *)(p)+1)<<48|  \
-                     (guint64)*((const guint8 *)(p)+2)<<40|  \
-                     (guint64)*((const guint8 *)(p)+3)<<32|  \
-                     (guint64)*((const guint8 *)(p)+4)<<24|  \
-                     (guint64)*((const guint8 *)(p)+5)<<16|  \
-                     (guint64)*((const guint8 *)(p)+6)<<8|   \
-                     (guint64)*((const guint8 *)(p)+7)<<0)
-#endif
-
-
-#ifndef pletohs
-#define pletohs(p) ((guint16)                       \
-                    ((guint16)*((const guint8 *)(p)+1)<<8|  \
-                     (guint16)*((const guint8 *)(p)+0)<<0))
-#endif
-
-#ifndef pletoh24
-#define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
-                     (guint32)*((const guint8 *)(p)+1)<<8|  \
-                     (guint32)*((const guint8 *)(p)+0)<<0)
-#endif
-
-
-#ifndef pletohl
-#define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
-                    (guint32)*((const guint8 *)(p)+2)<<16|  \
-                    (guint32)*((const guint8 *)(p)+1)<<8|   \
-                    (guint32)*((const guint8 *)(p)+0)<<0)
-#endif
-
-
-#ifndef pletohll
-#define pletohll(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
-                     (guint64)*((const guint8 *)(p)+6)<<48|  \
-                     (guint64)*((const guint8 *)(p)+5)<<40|  \
-                     (guint64)*((const guint8 *)(p)+4)<<32|  \
-                     (guint64)*((const guint8 *)(p)+3)<<24|  \
-                     (guint64)*((const guint8 *)(p)+2)<<16|  \
-                     (guint64)*((const guint8 *)(p)+1)<<8|   \
-                     (guint64)*((const guint8 *)(p)+0)<<0)
-#endif
 
 /* Pointer routines to put items out in a particular byte order.
  * These will work regardless of the byte alignment of the pointer.
@@ -331,40 +242,53 @@ extern gint wtap_num_file_types;
     }
 #endif
 
-#define wtap_file_read_unknown_bytes(target, num_bytes, fh, err, err_info) \
-    G_STMT_START \
-    { \
-        int _bytes_read; \
-        _bytes_read = file_read((target), (num_bytes), (fh)); \
-        if (_bytes_read != (int) (num_bytes)) { \
-            *(err) = file_error((fh), (err_info)); \
-            return FALSE; \
-        } \
-    } \
-    G_STMT_END
-
-#define wtap_file_read_expected_bytes(target, num_bytes, fh, err, err_info) \
-    G_STMT_START \
-    { \
-        int _bytes_read; \
-        _bytes_read = file_read((target), (num_bytes), (fh)); \
-        if (_bytes_read != (int) (num_bytes)) { \
-            *(err) = file_error((fh), (err_info)); \
-            if (*(err) == 0 && _bytes_read > 0) { \
-                *(err) = WTAP_ERR_SHORT_READ; \
-            } \
-            return FALSE; \
-        } \
-    } \
-    G_STMT_END
-
 /* glib doesn't have g_ptr_array_len of all things!*/
 #ifndef g_ptr_array_len
 #define g_ptr_array_len(a)      ((a)->len)
 #endif
 
-/*** get GSList of all compressed file extensions ***/
-GSList *wtap_get_compressed_file_extensions(void);
+/*
+ * Table of extensions for compressed file types we support.
+ * Last pointer in the list is null.
+ */
+extern const char *compressed_file_extension_table[];
+
+/*
+ * Read a given number of bytes from a file.
+ *
+ * If we succeed, return TRUE.
+ *
+ * If we get an EOF, return FALSE with *err set to 0, reporting this
+ * as an EOF.
+ *
+ * If we get fewer bytes than the specified number, return FALSE with
+ * *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
+ * error.
+ *
+ * If we get a read error, return FALSE with *err and *err_info set
+ * appropriately.
+ */
+WS_DLL_PUBLIC
+gboolean
+wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
+    gchar **err_info);
+
+/*
+ * Read a given number of bytes from a file.
+ *
+ * If we succeed, return TRUE.
+ *
+ * If we get fewer bytes than the specified number, including getting
+ * an EOF, return FALSE with *err set to WTAP_ERR_SHORT_READ, reporting
+ * this as a short read error.
+ *
+ * If we get a read error, return FALSE with *err and *err_info set
+ * appropriately.
+ */
+WS_DLL_PUBLIC
+gboolean
+wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
+    gchar **err_info);
 
 /*
  * Read packet data into a Buffer, growing the buffer as necessary.
@@ -375,6 +299,7 @@ GSList *wtap_get_compressed_file_extensions(void);
  * 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.)
  */
+WS_DLL_PUBLIC
 gboolean
 wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
     gchar **err_info);