Make sure the packet length isn't > WTAP_MAX_PACKET_SIZE.
[metze/wireshark/wip.git] / wiretap / toshiba.c
index dea3d7c6d6aa7428d9649018850ea457dddd1f8f..091b7908840d80c0f482ee0c32b60cc4fee6b933 100644 (file)
@@ -1,6 +1,4 @@
 /* toshiba.c
- *
- * $Id$
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
 
 #include "config.h"
 #include "wtap-int.h"
-#include "buffer.h"
 #include "toshiba.h"
 #include "file_wrappers.h"
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -102,57 +98,44 @@ static const char toshiba_hdr_magic[]  =
 static const char toshiba_rec_magic[]  = { '[', 'N', 'o', '.' };
 #define TOSHIBA_REC_MAGIC_SIZE  (sizeof toshiba_rec_magic  / sizeof toshiba_rec_magic[0])
 
-/*
- * XXX - is this the biggest packet we can get?
- */
-#define TOSHIBA_MAX_PACKET_LEN 16384
-
 static gboolean toshiba_read(wtap *wth, int *err, gchar **err_info,
        gint64 *data_offset);
 static gboolean toshiba_seek_read(wtap *wth, gint64 seek_off,
-       struct wtap_pkthdr *phdr, guint8 *pd, int len,
-       int *err, gchar **err_info);
+       struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
 static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf,
        guint byte_offset);
-static gboolean parse_toshiba_hex_dump(FILE_T fh, int pkt_len, guint8* buf,
-       int *err, gchar **err_info);
-static int parse_toshiba_rec_hdr(struct wtap_pkthdr *phdr, FILE_T fh,
-    int *err, gchar **err_info);
+static gboolean parse_toshiba_packet(FILE_T fh, struct wtap_pkthdr *phdr,
+       Buffer *buf, int *err, gchar **err_info);
 
 /* Seeks to the beginning of the next packet, and returns the
    byte offset.  Returns -1 on failure, and sets "*err" to the error
    and "*err_info" to null or an additional error string. */
 static gint64 toshiba_seek_next_packet(wtap *wth, int *err, gchar **err_info)
 {
-  int byte;
-  guint level = 0;
-  gint64 cur_off;
-
-  while ((byte = file_getc(wth->fh)) != EOF) {
-    if (byte == toshiba_rec_magic[level]) {
-      level++;
-      if (level >= TOSHIBA_REC_MAGIC_SIZE) {
-             /* note: we're leaving file pointer right after the magic characters */
-        cur_off = file_tell(wth->fh);
-        if (cur_off == -1) {
-          /* Error. */
-          *err = file_error(wth->fh, err_info);
-          return -1;
-        }
-        return cur_off + 1;
-      }
-    } else {
-      level = 0;
-    }
-  }
-  if (file_eof(wth->fh)) {
-    /* We got an EOF. */
-    *err = 0;
-  } else {
-    /* We got an error. */
-    *err = file_error(wth->fh, err_info);
-  }
-  return -1;
+       int byte;
+       guint level = 0;
+       gint64 cur_off;
+
+       while ((byte = file_getc(wth->fh)) != EOF) {
+               if (byte == toshiba_rec_magic[level]) {
+                       level++;
+                       if (level >= TOSHIBA_REC_MAGIC_SIZE) {
+                               /* note: we're leaving file pointer right after the magic characters */
+                               cur_off = file_tell(wth->fh);
+                               if (cur_off == -1) {
+                                       /* Error. */
+                                       *err = file_error(wth->fh, err_info);
+                                       return -1;
+                               }
+                               return cur_off + 1;
+                       }
+               } else {
+                       level = 0;
+               }
+       }
+       /* EOF or error. */
+       *err = file_error(wth->fh, err_info);
+       return -1;
 }
 
 #define TOSHIBA_HEADER_LINES_TO_CHECK  200
@@ -204,23 +187,23 @@ static gboolean toshiba_check_file_type(wtap *wth, int *err, gchar **err_info)
 }
 
 
-int toshiba_open(wtap *wth, int *err, gchar **err_info)
+wtap_open_return_val toshiba_open(wtap *wth, int *err, gchar **err_info)
 {
        /* Look for Toshiba header */
        if (!toshiba_check_file_type(wth, err, err_info)) {
                if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-                       return -1;
-               return 0;
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
        }
 
        wth->file_encap = WTAP_ENCAP_PER_PACKET;
-       wth->file_type = WTAP_FILE_TOSHIBA;
+       wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_TOSHIBA;
        wth->snapshot_length = 0; /* not known */
        wth->subtype_read = toshiba_read;
        wth->subtype_seek_read = toshiba_seek_read;
-       wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
+       wth->file_tsprec = WTAP_TSPREC_CSEC;
 
-       return 1;
+       return WTAP_OPEN_MINE;
 }
 
 /* Find the next packet and parse it; called from wtap_read(). */
@@ -228,59 +211,38 @@ static gboolean toshiba_read(wtap *wth, int *err, gchar **err_info,
     gint64 *data_offset)
 {
        gint64  offset;
-       guint8  *buf;
-       int     pkt_len;
 
        /* Find the next packet */
        offset = toshiba_seek_next_packet(wth, err, err_info);
        if (offset < 1)
                return FALSE;
-
-       /* Parse the header */
-       pkt_len = parse_toshiba_rec_hdr(&wth->phdr, wth->fh, err, err_info);
-       if (pkt_len == -1)
-               return FALSE;
-
-       /* Make sure we have enough room for the packet */
-       buffer_assure_space(wth->frame_buffer, TOSHIBA_MAX_PACKET_LEN);
-       buf = buffer_start_ptr(wth->frame_buffer);
-
-       /* Convert the ASCII hex dump to binary data */
-       if (!parse_toshiba_hex_dump(wth->fh, pkt_len, buf, err, err_info))
-               return FALSE;
-
        *data_offset = offset;
-       return TRUE;
+
+       /* Parse the packet */
+       return parse_toshiba_packet(wth->fh, &wth->phdr, wth->frame_buffer,
+           err, err_info);
 }
 
 /* Used to read packets in random-access fashion */
 static gboolean
-toshiba_seek_read (wtap *wth, gint64 seek_off,
-       struct wtap_pkthdr *phdr, guint8 *pd, int len,
+toshiba_seek_read(wtap *wth, gint64 seek_off,
+       struct wtap_pkthdr *phdr, Buffer *buf,
        int *err, gchar **err_info)
 {
-       int     pkt_len;
-
        if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
                return FALSE;
 
-       pkt_len = parse_toshiba_rec_hdr(phdr, wth->random_fh, err, err_info);
-
-       if (pkt_len != len) {
-               if (pkt_len != -1) {
-                       *err = WTAP_ERR_BAD_FILE;
-                       *err_info = g_strdup_printf("toshiba: requested length %d doesn't match record length %d",
-                           len, pkt_len);
-               }
+       if (!parse_toshiba_packet(wth->random_fh, phdr, buf, err, err_info)) {
+               if (*err == 0)
+                       *err = WTAP_ERR_SHORT_READ;
                return FALSE;
        }
-
-       return parse_toshiba_hex_dump(wth->random_fh, pkt_len, pd, err, err_info);
+       return TRUE;
 }
 
-/* Parses a packet record header. */
-static int
-parse_toshiba_rec_hdr(struct wtap_pkthdr *phdr, FILE_T fh,
+/* Parses a packet. */
+static gboolean
+parse_toshiba_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
     int *err, gchar **err_info)
 {
        union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
@@ -288,6 +250,8 @@ parse_toshiba_rec_hdr(struct wtap_pkthdr *phdr, FILE_T fh,
        int     num_items_scanned;
        int     pkt_len, pktnum, hr, min, sec, csec;
        char    channel[10], direction[10];
+       int     i, hex_lines;
+       guint8  *pd;
 
        /* Our file pointer should be on the line containing the
         * summary information for a packet. Read in that line and
@@ -298,7 +262,7 @@ parse_toshiba_rec_hdr(struct wtap_pkthdr *phdr, FILE_T fh,
                if (*err == 0) {
                        *err = WTAP_ERR_SHORT_READ;
                }
-               return -1;
+               return FALSE;
        }
 
        /* Find text in line after "[No.". Limit the length of the
@@ -310,7 +274,7 @@ parse_toshiba_rec_hdr(struct wtap_pkthdr *phdr, FILE_T fh,
        if (num_items_scanned != 7) {
                *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup("toshiba: record header isn't valid");
-               return -1;
+               return FALSE;
        }
 
        /* Scan lines until we find the OFFSET line. In a "telnet" trace,
@@ -328,7 +292,7 @@ parse_toshiba_rec_hdr(struct wtap_pkthdr *phdr, FILE_T fh,
                        if (*err == 0) {
                                *err = WTAP_ERR_SHORT_READ;
                        }
-                       return -1;
+                       return FALSE;
                }
 
                /* Check for "OFFSET 0001-0203" at beginning of line */
@@ -340,16 +304,31 @@ parse_toshiba_rec_hdr(struct wtap_pkthdr *phdr, FILE_T fh,
        if (num_items_scanned != 1) {
                *err = WTAP_ERR_BAD_FILE;
                *err_info = g_strdup("toshiba: OFFSET line doesn't have valid LEN item");
-               return -1;
+               return FALSE;
        }
-
-       {
-               phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
-               phdr->ts.secs = hr * 3600 + min * 60 + sec;
-               phdr->ts.nsecs = csec * 10000000;
-               phdr->caplen = pkt_len;
-               phdr->len = pkt_len;
+       if (pkt_len < 0) {
+               *err = WTAP_ERR_BAD_FILE;
+               *err_info = g_strdup("toshiba: packet header has a negative packet length");
+               return FALSE;
+       }
+       if (pkt_len > WTAP_MAX_PACKET_SIZE) {
+               /*
+                * Probably a corrupt capture file; don't blow up trying
+                * to allocate space for an immensely-large packet.
+                */
+               *err = WTAP_ERR_BAD_FILE;
+               *err_info = g_strdup_printf("toshiba: File has %u-byte packet, bigger than maximum of %u",
+                   pkt_len, WTAP_MAX_PACKET_SIZE);
+               return FALSE;
        }
+
+       phdr->rec_type = REC_TYPE_PACKET;
+       phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
+       phdr->ts.secs = hr * 3600 + min * 60 + sec;
+       phdr->ts.nsecs = csec * 10000000;
+       phdr->caplen = pkt_len;
+       phdr->len = pkt_len;
+
        switch (channel[0]) {
                case 'B':
                        phdr->pkt_encap = WTAP_ENCAP_ISDN;
@@ -370,16 +349,10 @@ parse_toshiba_rec_hdr(struct wtap_pkthdr *phdr, FILE_T fh,
                        pseudo_header->eth.fcs_len = -1;
                        break;
        }
-       return pkt_len;
-}
 
-/* Converts ASCII hex dump to binary data */
-static gboolean
-parse_toshiba_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err,
-    gchar **err_info)
-{
-       char    line[TOSHIBA_LINE_LENGTH];
-       int     i, hex_lines;
+       /* Make sure we have enough room for the packet */
+       ws_buffer_assure_space(buf, pkt_len);
+       pd = ws_buffer_start_ptr(buf);
 
        /* Calculate the number of hex dump lines, each
         * containing 16 bytes of data */
@@ -393,7 +366,7 @@ parse_toshiba_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err,
                        }
                        return FALSE;
                }
-               if (!parse_single_hex_dump_line(line, buf, i * 16)) {
+               if (!parse_single_hex_dump_line(line, pd, i * 16)) {
                        *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("toshiba: hex dump not valid");
                        return FALSE;
@@ -440,8 +413,8 @@ parse_single_hex_dump_line(char* rec, guint8 *buf, guint byte_offset) {
        }
 
        /* Go through the substring representing the values and:
-        *      1. Replace any spaces with '0's
-        *      2. Place \0's every 5 bytes (to terminate the string)
+        *      1. Replace any spaces with '0's
+        *      2. Place \0's every 5 bytes (to terminate the string)
         *
         * Then read the eight sets of hex bytes
         */
@@ -464,3 +437,16 @@ parse_single_hex_dump_line(char* rec, guint8 *buf, guint byte_offset) {
 
        return TRUE;
 }
+
+/*
+ * 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:
+ */