Removed trailing whitespaces from .h and .c files using the
[obnox/wireshark/wip.git] / wiretap / ngsniffer.c
index 8758fb8d9e312416184672c4ff568157c5924be2..8d45b07666049efe0c3c733512a2ae399a7b289c 100644 (file)
@@ -1,20 +1,20 @@
 /* ngsniffer.c
  *
- * $Id: ngsniffer.c,v 1.79 2002/04/30 08:48:27 guy Exp $
+ * $Id: ngsniffer.c,v 1.85 2002/08/28 20:30:45 jmayer Exp $
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
@@ -286,7 +286,7 @@ static double Usec[] = { 15.0, 0.838096, 15.0, 0.5, 2.0, 1.0, 0.1 };
 static int skip_header_records(wtap *wth, int *err, gint16 version);
 static gboolean ngsniffer_read(wtap *wth, int *err, long *data_offset);
 static gboolean ngsniffer_seek_read(wtap *wth, long seek_off,
-    union wtap_pseudo_header *pseudo_header, u_char *pd, int packet_size,
+    union wtap_pseudo_header *pseudo_header, guchar *pd, int packet_size,
     int *err);
 static int ngsniffer_read_rec_header(wtap *wth, gboolean is_random,
     guint16 *typep, guint16 *lengthp, int *err);
@@ -303,13 +303,13 @@ static gboolean ngsniffer_read_frame6(wtap *wth, gboolean is_random,
 static void set_pseudo_header_frame6(union wtap_pseudo_header *pseudo_header,
     struct frame6_rec *frame6);
 static gboolean ngsniffer_read_rec_data(wtap *wth, gboolean is_random,
-    u_char *pd, int length, int *err);
+    guchar *pd, int length, int *err);
 static void fix_pseudo_header(wtap *wth,
     union wtap_pseudo_header *pseudo_header);
 static void ngsniffer_sequential_close(wtap *wth);
 static void ngsniffer_close(wtap *wth);
 static gboolean ngsniffer_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-       const union wtap_pseudo_header *pseudo_header, const u_char *pd, int *err);
+       const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err);
 static gboolean ngsniffer_dump_close(wtap_dumper *wdh, int *err);
 static int SnifferDecompress( unsigned char * inbuf, size_t inlen,
         unsigned char * outbuf, size_t outlen, int *err );
@@ -458,10 +458,8 @@ int ngsniffer_open(wtap *wth, int *err)
         * "ngsniffer_read()".
         */
        if (wth->random_fh != NULL) {
-               if (file_seek(wth->random_fh, wth->data_offset, SEEK_SET) == -1) {
-                       *err = file_error(wth->random_fh);
+               if (file_seek(wth->random_fh, wth->data_offset, SEEK_SET, err) == -1)
                        return -1;
-               }
        }
 
        /* This is a ngsniffer file */
@@ -564,10 +562,8 @@ skip_header_records(wtap *wth, int *err, gint16 version)
                         * which implies data. Seek backwards over the
                         * two bytes we read, and return.
                         */
-                       if (file_seek(wth->fh, -2, SEEK_CUR) == -1) {
-                               *err = file_error(wth->fh);
+                       if (file_seek(wth->fh, -2, SEEK_CUR, err) == -1)
                                return -1;
-                       }
                        return 0;
                }
 
@@ -612,15 +608,13 @@ skip_header_records(wtap *wth, int *err, gint16 version)
                         */
                        if (length > sizeof buffer) {
                                if (file_seek(wth->fh, length - sizeof buffer,
-                                   SEEK_CUR) == -1) {
-                                       *err = file_error(wth->fh);
+                                   SEEK_CUR, err) == -1)
                                        return -1;
-                               }
                        }
 
                        /*
                         * XXX - what about LAPB and LAPD?  At least one
-                        * X.25 capture has a type of NET_HDLC, but one 
+                        * X.25 capture has a type of NET_HDLC, but one
                         * might also consider LAPD to be an HDLC
                         * variant; if it also has a type of NET_HDLC,
                         * we'd have to look at some other data to
@@ -648,10 +642,8 @@ skip_header_records(wtap *wth, int *err, gint16 version)
 
                } else {
                        /* Nope, just skip over the data. */
-                       if (file_seek(wth->fh, length, SEEK_CUR) == -1) {
-                               *err = file_error(wth->fh);
+                       if (file_seek(wth->fh, length, SEEK_CUR, err) == -1)
                                return -1;
-                       }
                }
                wth->data_offset += length;
        }
@@ -667,7 +659,7 @@ static gboolean ngsniffer_read(wtap *wth, int *err, long *data_offset)
        struct frame6_rec frame6;
        double  t;
        guint16 time_low, time_med, time_high, true_size, size;
-       u_char  *pd;
+       guchar  *pd;
 
        for (;;) {
                /*
@@ -878,7 +870,7 @@ found:
 }
 
 static gboolean ngsniffer_seek_read(wtap *wth, long seek_off,
-    union wtap_pseudo_header *pseudo_header, u_char *pd, int packet_size,
+    union wtap_pseudo_header *pseudo_header, guchar *pd, int packet_size,
     int *err)
 {
        int     ret;
@@ -1317,7 +1309,7 @@ static void set_pseudo_header_frame6(
 }
 
 static gboolean ngsniffer_read_rec_data(wtap *wth, gboolean is_random,
-    u_char *pd, int length, int *err)
+    guchar *pd, int length, int *err)
 {
        int     bytes_read;
 
@@ -1360,10 +1352,12 @@ static void free_blob(gpointer data, gpointer user_data _U_)
        g_free(data);
 }
 
+/* Close stuff used by the random I/O stream, if any, and free up any
+   private data structures.  (If there's a "sequential_close" routine
+   for a capture file type, it'll be called before the "close" routine
+   is called, so we don't have to free the sequential buffer here.) */
 static void ngsniffer_close(wtap *wth)
 {
-       if (wth->capture.ngsniffer->seq.buf != NULL)
-               g_free(wth->capture.ngsniffer->seq.buf);
        if (wth->capture.ngsniffer->rand.buf != NULL)
                g_free(wth->capture.ngsniffer->rand.buf);
        if (wth->capture.ngsniffer->first_blob != NULL) {
@@ -1407,7 +1401,7 @@ int ngsniffer_dump_can_write_encap(int encap)
 
 /* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
    failure */
-gboolean ngsniffer_dump_open(wtap_dumper *wdh, int *err)
+gboolean ngsniffer_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
 {
     size_t nwritten;
     char buf[6] = {REC_VERS, 0x00, 0x12, 0x00, 0x00, 0x00}; /* version record */
@@ -1444,7 +1438,7 @@ gboolean ngsniffer_dump_open(wtap_dumper *wdh, int *err)
 /* Write a record for a packet to a dump file.
    Returns TRUE on success, FALSE on failure. */
 static gboolean ngsniffer_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
-    const union wtap_pseudo_header *pseudo_header, const u_char *pd, int *err)
+    const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err)
 {
     ngsniffer_dump_t *priv = wdh->dump.ngsniffer;
     struct frame2_rec rec_hdr;
@@ -1559,10 +1553,12 @@ static gboolean ngsniffer_dump_close(wtap_dumper *wdh, int *err)
 
     nwritten = fwrite(buf, 1, 6, wdh->fh);
     if (nwritten != 6) {
-       if (nwritten == 0 && ferror(wdh->fh))
-           *err = errno;
-       else
-           *err = WTAP_ERR_SHORT_WRITE;
+       if (err != NULL) {
+           if (nwritten == 0 && ferror(wdh->fh))
+               *err = errno;
+           else
+               *err = WTAP_ERR_SHORT_WRITE;
+       }
        return FALSE;
     }
     return TRUE;
@@ -1585,7 +1581,7 @@ static gboolean ngsniffer_dump_close(wtap_dumper *wdh, int *err)
    Return value is the number of bytes in outbuf on return.
 */
 static int
-SnifferDecompress( unsigned char * inbuf, size_t inlen, 
+SnifferDecompress( unsigned char * inbuf, size_t inlen,
                        unsigned char * outbuf, size_t outlen, int *err )
 {
    unsigned char * pin = inbuf;
@@ -1646,7 +1642,7 @@ SnifferDecompress( unsigned char * inbuf, size_t inlen,
                     Run length is the low nybble of the first code byte.
                     Byte to repeat immediately follows.
                     Total code size: 2 bytes.
-                */    
+                */
                 length = code_low + 3;
                 /* If length would put us past end of output, avoid overflow */
                 if ( pout + length > pout_end )
@@ -1661,12 +1657,12 @@ SnifferDecompress( unsigned char * inbuf, size_t inlen,
                 break;
             case 1  :   /* RLE long runs */
                 /*
-                    Low 4 bits of run length is the low nybble of the 
-                    first code byte, upper 8 bits of run length is in 
+                    Low 4 bits of run length is the low nybble of the
+                    first code byte, upper 8 bits of run length is in
                     the next byte.
                     Byte to repeat immediately follows.
                     Total code size: 3 bytes.
-                */    
+                */
                 length = code_low + ((unsigned int)(*pin++) << 4) + 19;
                 /* If we are already at end of input, there is no byte
                    to repeat */
@@ -1688,12 +1684,12 @@ SnifferDecompress( unsigned char * inbuf, size_t inlen,
                 break;
             case 2  :   /* LZ77 long strings */
                 /*
-                    Low 4 bits of offset to string is the low nybble of the 
-                    first code byte, upper 8 bits of offset is in 
+                    Low 4 bits of offset to string is the low nybble of the
+                    first code byte, upper 8 bits of offset is in
                     the next byte.
                     Length of string immediately follows.
                     Total code size: 3 bytes.
-                */    
+                */
                 offset = code_low + ((unsigned int)(*pin++) << 4) + 3;
                 /* If we are already at end of input, there is no byte
                    to repeat */
@@ -1724,12 +1720,12 @@ SnifferDecompress( unsigned char * inbuf, size_t inlen,
                 break;
             default :   /* (3 to 15): LZ77 short strings */
                 /*
-                    Low 4 bits of offset to string is the low nybble of the 
-                    first code byte, upper 8 bits of offset is in 
+                    Low 4 bits of offset to string is the low nybble of the
+                    first code byte, upper 8 bits of offset is in
                     the next byte.
                     Length of string to repeat is overloaded into code_type.
                     Total code size: 2 bytes.
-                */    
+                */
                 offset = code_low + ((unsigned int)(*pin++) << 4) + 3;
                 /* Check if offset would put us back past begin of buffer */
                 if ( pout - offset < outbuf )
@@ -1867,7 +1863,7 @@ ng_file_read(void *buffer, size_t elementsize, size_t numelements, wtap *wth,
                return -1;
            bytes_left = comp_stream->nbytes - comp_stream->nextout;
        }
-           
+
        bytes_to_copy = copybytes;
        if (bytes_to_copy > bytes_left)
            bytes_to_copy = bytes_left;
@@ -1944,17 +1940,12 @@ read_blob(FILE_T infile, ngsniffer_comp_stream_t *comp_stream, int *err)
 static long
 ng_file_seek_seq(wtap *wth, long offset, int whence, int *err)
 {
-    long ret;
     long delta;
     char buf[65536];
     long amount_to_read;
 
-    if (wth->file_type == WTAP_FILE_NGSNIFFER_UNCOMPRESSED) {
-       ret = file_seek(wth->fh, offset, whence);
-       if (ret == -1)
-               *err = file_error(wth->fh);
-       return ret;
-    }
+    if (wth->file_type == WTAP_FILE_NGSNIFFER_UNCOMPRESSED)
+       return file_seek(wth->fh, offset, whence, err);
 
     switch (whence) {
 
@@ -1996,18 +1987,13 @@ ng_file_seek_seq(wtap *wth, long offset, int whence, int *err)
 static long
 ng_file_seek_rand(wtap *wth, long offset, int whence, int *err)
 {
-    long ret;
     ngsniffer_t *ngsniffer;
     long delta;
     GList *new, *next;
     blob_info_t *next_blob, *new_blob;
 
-    if (wth->file_type == WTAP_FILE_NGSNIFFER_UNCOMPRESSED) {
-       ret = file_seek(wth->random_fh, offset, whence);
-       if (ret == -1)
-               *err = file_error(wth->random_fh);
-       return ret;
-    }
+    if (wth->file_type == WTAP_FILE_NGSNIFFER_UNCOMPRESSED)
+       return file_seek(wth->random_fh, offset, whence, err);
 
     ngsniffer = wth->capture.ngsniffer;
 
@@ -2082,10 +2068,8 @@ ng_file_seek_rand(wtap *wth, long offset, int whence, int *err)
 
        /* Seek in the compressed file to the offset in the compressed file
           of the beginning of that blob. */
-       if (file_seek(wth->random_fh, new_blob->blob_comp_offset, SEEK_SET) == -1) {
-           *err = file_error(wth->random_fh);
+       if (file_seek(wth->random_fh, new_blob->blob_comp_offset, SEEK_SET, err) == -1)
            return -1;
-       }
 
        /* Make the blob we found the current one. */
        ngsniffer->current_blob = new;