Make sure the packet length isn't > WTAP_MAX_PACKET_SIZE.
[metze/wireshark/wip.git] / wiretap / toshiba.c
index c356959286e021e62cfa2130c40bc03a281c6abf..091b7908840d80c0f482ee0c32b60cc4fee6b933 100644 (file)
@@ -1,34 +1,28 @@
 /* toshiba.c
- *
- * $Id: toshiba.c,v 1.22 2002/03/05 05:58:41 guy 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.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 #include "wtap-int.h"
-#include "buffer.h"
 #include "toshiba.h"
 #include "file_wrappers.h"
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
    Example 'snoop' output data:
 
 Script started on Thu Sep  9 21:48:49 1999
-\e]0;gram@nirvana:/tmp\a$ telnet 10.0.0.254\r
-Trying 10.0.0.254...\r
-Connected to 10.0.0.254.\r
-Escape character is '^]'.\r
-\r
-\r
-TR-600(tr600) System Console\r
-\r
-Login:admin\r
-Password:*******\r
-*--------------------------------------------------------*\r
-|             T O S H I B A    T R - 6 0 0               |\r
-|                 <  Compact Router >                    |\r
-|                       V1.02.02                         |\r
-|                                                        |\r
-|  (C) Copyright TOSHIBA Corp. 1997 All rights reserved. |\r
-*--------------------------------------------------------*\r
-\r
-tr600>snoop dump b1\r
- Trace start?(on/off/dump/dtl)->dump\r
- IP Address?->b1\r
-B1 Port Filetering\r
-Trace start(Dump Mode)...\r
-\r
-tr600>[No.1] 00:00:09.14 B1:1 Tx 207.193.26.136->151.164.1.8 DNS  SPORT=1028 LEN=38 CHKSUM=4FD4 ID=2390 Query RD QCNT=1 pow.zing.org?\r
-OFFSET 0001-0203-0405-0607-0809-0A0B-0C0D-0E0F 0123456789ABCDEF LEN=67\r
-0000 : FF03 003D C000 0008 2145 0000 3A12 6500 ...=....!E..:.e.\r
-0010 : 003F 11E6 58CF C11A 8897 A401 0804 0400 .?..X...........\r
-0020 : 3500 264F D409 5601 0000 0100 0000 0000 5.&O..V.........\r
-0030 : 0003 706F 7704 7A69 6E67 036F 7267 0000 ..pow.zing.org..\r
-0040 : 0100 01                                 ...\r
-\r
-[No.2] 00:00:09.25 B1:1 Rx 151.164.1.8->207.193.26.136 DNS  DPORT=1028 LEN=193 CHKSUM=3E06 ID=2390 Answer RD RA QCNT=1 pow.zing.org? ANCNT=1 pow.zing.org=206.57.36.90 TTL=2652\r
-OFFSET 0001-0203-0405-0607-0809-0A0B-0C0D-0E0F 0123456789ABCDEF LEN=222\r
-0000 : FF03 003D C000 0013 2145 0000 D590 9340 ...=....!E.....@\r
-0010 : 00F7 116F 8E97 A401 08CF C11A 8800 3504 ...o..........5.\r
-0020 : 0400 C13E 0609 5681 8000 0100 0100 0300 ...>..V.........\r
-0030 : 0303 706F 7704 7A69 6E67 036F 7267 0000 ..pow.zing.org..\r
-0040 : 0100 01C0 0C00 0100 0100 000A 5C00 04CE ............\...\r
-0050 : 3924 5A04 5A49 4E47 036F 7267 0000 0200 9$Z.ZING.org....\r
-0060 : 0100 016F 5B00 0D03 4841 4E03 5449 5703 ...o[...HAN.TIW.\r
-0070 : 4E45 5400 C02E 0002 0001 0001 6F5B 0006 NET.........o[..\r
-0080 : 034E 5331 C02E C02E 0002 0001 0001 6F5B .NS1..........o[\r
-0090 : 001C 0854 414C 4945 5349 4E0D 434F 4E46 ...TALIESIN.CONF\r
-00A0 : 4142 554C 4154 494F 4E03 434F 4D00 C042 ABULATION.COM..B\r
-00B0 : 0001 0001 0001 51EC 0004 CE39 2406 C05B ......Q....9$..[\r
-00C0 : 0001 0001 0001 6F5B 0004 CE39 245A C06D ......o[...9$Z.m\r
-00D0 : 0001 0001 0001 4521 0004 187C 1F01      ......E!...|..\r
-\r
+\e]0;gram@nirvana:/tmp\a$ telnet 10.0.0.254
+Trying 10.0.0.254...
+Connected to 10.0.0.254.
+Escape character is '^]'.
+
+
+TR-600(tr600) System Console
+
+Login:admin
+Password:*******
+*--------------------------------------------------------*
+|             T O S H I B A    T R - 6 0 0               |
+|                 <  Compact Router >                    |
+|                       V1.02.02                         |
+|                                                        |
+|  (C) Copyright TOSHIBA Corp. 1997 All rights reserved. |
+*--------------------------------------------------------*
+
+tr600>snoop dump b1
+ Trace start?(on/off/dump/dtl)->dump
+ IP Address?->b1
+B1 Port Filetering
+Trace start(Dump Mode)...
+
+tr600>[No.1] 00:00:09.14 B1:1 Tx 207.193.26.136->151.164.1.8 DNS  SPORT=1028 LEN=38 CHKSUM=4FD4 ID=2390 Query RD QCNT=1 pow.zing.org?
+OFFSET 0001-0203-0405-0607-0809-0A0B-0C0D-0E0F 0123456789ABCDEF LEN=67
+0000 : FF03 003D C000 0008 2145 0000 3A12 6500 ...=....!E..:.e.
+0010 : 003F 11E6 58CF C11A 8897 A401 0804 0400 .?..X...........
+0020 : 3500 264F D409 5601 0000 0100 0000 0000 5.&O..V.........
+0030 : 0003 706F 7704 7A69 6E67 036F 7267 0000 ..pow.zing.org..
+0040 : 0100 01                                 ...
+
+[No.2] 00:00:09.25 B1:1 Rx 151.164.1.8->207.193.26.136 DNS  DPORT=1028 LEN=193 CHKSUM=3E06 ID=2390 Answer RD RA QCNT=1 pow.zing.org? ANCNT=1 pow.zing.org=206.57.36.90 TTL=2652
+OFFSET 0001-0203-0405-0607-0809-0A0B-0C0D-0E0F 0123456789ABCDEF LEN=222
+0000 : FF03 003D C000 0013 2145 0000 D590 9340 ...=....!E.....@
+0010 : 00F7 116F 8E97 A401 08CF C11A 8800 3504 ...o..........5.
+0020 : 0400 C13E 0609 5681 8000 0100 0100 0300 ...>..V.........
+0030 : 0303 706F 7704 7A69 6E67 036F 7267 0000 ..pow.zing.org..
+0040 : 0100 01C0 0C00 0100 0100 000A 5C00 04CE ............\...
+0050 : 3924 5A04 5A49 4E47 036F 7267 0000 0200 9$Z.ZING.org....
+0060 : 0100 016F 5B00 0D03 4841 4E03 5449 5703 ...o[...HAN.TIW.
+0070 : 4E45 5400 C02E 0002 0001 0001 6F5B 0006 NET.........o[..
+0080 : 034E 5331 C02E C02E 0002 0001 0001 6F5B .NS1..........o[
+0090 : 001C 0854 414C 4945 5349 4E0D 434F 4E46 ...TALIESIN.CONF
+00A0 : 4142 554C 4154 494F 4E03 434F 4D00 C042 ABULATION.COM..B
+00B0 : 0001 0001 0001 51EC 0004 CE39 2406 C05B ......Q....9$..[
+00C0 : 0001 0001 0001 6F5B 0004 CE39 245A C06D ......o[...9$Z.m
+00D0 : 0001 0001 0001 4521 0004 187C 1F01      ......E!...|..
+
  */
 
 /* Magic text to check for toshiba-ness of file */
@@ -104,53 +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, long *data_offset);
-static int toshiba_seek_read(wtap *wth, long seek_off,
-       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len, int *err);
-static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf, guint byte_offset);
-static int parse_toshiba_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err);
-static int parse_toshiba_rec_hdr(wtap *wth, FILE_T fh,
-    union wtap_pseudo_header *pseudo_header, int *err);
+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, 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_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. */
-static long toshiba_seek_next_packet(wtap *wth, int *err)
+   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;
-  long 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);
-          return -1;
-        }
-        return cur_off + 1;
-      }
-    } else {
-      level = 0;
-    }
-  }
-  if (file_eof(wth->fh)) {
-    /* We got an EOF. */
-    *err = 0;
-  } else {
-    /* We (presumably) got an error (there's no equivalent to "ferror()"
-       in zlib, alas, so we don't have a wrapper to check for an error). */
-    *err = file_error(wth->fh);
-  }
-  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
@@ -160,9 +145,10 @@ static long toshiba_seek_next_packet(wtap *wth, int *err)
  * a Toshiba trace file.
  *
  * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
- * if we get an I/O error, "*err" will be set to a non-zero value.
+ * if we get an I/O error, "*err" will be set to a non-zero value and
+ * "*err_info" will be set to null or an additional error string.
  */
-static gboolean toshiba_check_file_type(wtap *wth, int *err)
+static gboolean toshiba_check_file_type(wtap *wth, int *err, gchar **err_info)
 {
        char    buf[TOSHIBA_LINE_LENGTH];
        guint   i, reclen, level, line;
@@ -171,34 +157,29 @@ static gboolean toshiba_check_file_type(wtap *wth, int *err)
        buf[TOSHIBA_LINE_LENGTH-1] = 0;
 
        for (line = 0; line < TOSHIBA_HEADER_LINES_TO_CHECK; line++) {
-               if (file_gets(buf, TOSHIBA_LINE_LENGTH, wth->fh) != NULL) {
+               if (file_gets(buf, TOSHIBA_LINE_LENGTH, wth->fh) == NULL) {
+                       /* EOF or error. */
+                       *err = file_error(wth->fh, err_info);
+                       return FALSE;
+               }
 
-                       reclen = strlen(buf);
-                       if (reclen < TOSHIBA_HDR_MAGIC_SIZE) {
-                               continue;
-                       }
+               reclen = (guint) strlen(buf);
+               if (reclen < TOSHIBA_HDR_MAGIC_SIZE) {
+                       continue;
+               }
 
-                       level = 0;
-                       for (i = 0; i < reclen; i++) {
-                               byte = buf[i];
-                               if (byte == toshiba_hdr_magic[level]) {
-                                       level++;
-                                       if (level >= TOSHIBA_HDR_MAGIC_SIZE) {
-                                               return TRUE;
-                                       }
-                               }
-                               else {
-                                       level = 0;
+               level = 0;
+               for (i = 0; i < reclen; i++) {
+                       byte = buf[i];
+                       if (byte == toshiba_hdr_magic[level]) {
+                               level++;
+                               if (level >= TOSHIBA_HDR_MAGIC_SIZE) {
+                                       return TRUE;
                                }
                        }
-               }
-               else {
-                       /* EOF or error. */
-                       if (file_eof(wth->fh))
-                               *err = 0;
-                       else
-                               *err = file_error(wth->fh);
-                       return FALSE;
+                       else {
+                               level = 0;
+                       }
                }
        }
        *err = 0;
@@ -206,114 +187,94 @@ static gboolean toshiba_check_file_type(wtap *wth, int *err)
 }
 
 
-int toshiba_open(wtap *wth, int *err)
+wtap_open_return_val toshiba_open(wtap *wth, int *err, gchar **err_info)
 {
        /* Look for Toshiba header */
-       if (!toshiba_check_file_type(wth, err)) {
-               if (*err == 0)
-                       return 0;
-               else
-                       return -1;
+       if (!toshiba_check_file_type(wth, err, err_info)) {
+               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
        }
 
-       wth->data_offset = 0;
        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->file_tsprec = WTAP_TSPREC_CSEC;
 
-       return 1;
+       return WTAP_OPEN_MINE;
 }
 
-/* Find the next packet and parse it; called from wtap_loop(). */
-static gboolean toshiba_read(wtap *wth, int *err, long *data_offset)
+/* Find the next packet and parse it; called from wtap_read(). */
+static gboolean toshiba_read(wtap *wth, int *err, gchar **err_info,
+    gint64 *data_offset)
 {
-       long    offset;
-       guint8  *buf;
-       int     pkt_len;
+       gint64  offset;
 
        /* Find the next packet */
-       offset = toshiba_seek_next_packet(wth, err);
+       offset = toshiba_seek_next_packet(wth, err, err_info);
        if (offset < 1)
                return FALSE;
-
-       /* Parse the header */
-       pkt_len = parse_toshiba_rec_hdr(wth, wth->fh, &wth->pseudo_header, err);
-       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) == -1)
-               return FALSE;
-
-       wth->data_offset = offset;
        *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 int
-toshiba_seek_read (wtap *wth, long seek_off,
-       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len, int *err)
+static gboolean
+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) == -1) {
-               *err = file_error(wth->random_fh);
-               return -1;
-       }
-
-       pkt_len = parse_toshiba_rec_hdr(NULL, wth->random_fh, pseudo_header,
-           err);
+       if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
+               return FALSE;
 
-       if (pkt_len != len) {
-               if (pkt_len != -1)
-                       *err = WTAP_ERR_BAD_RECORD;
-               return -1;
+       if (!parse_toshiba_packet(wth->random_fh, phdr, buf, err, err_info)) {
+               if (*err == 0)
+                       *err = WTAP_ERR_SHORT_READ;
+               return FALSE;
        }
-
-       if (parse_toshiba_hex_dump(wth->random_fh, pkt_len, pd, err) == -1)
-               return -1;
-
-       return 0;
+       return TRUE;
 }
 
-/* Parses a packet record header. */
-static int
-parse_toshiba_rec_hdr(wtap *wth, FILE_T fh,
-    union wtap_pseudo_header *pseudo_header, int *err)
+/* 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;
        char    line[TOSHIBA_LINE_LENGTH];
        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
         * extract the useful information
         */
        if (file_gets(line, TOSHIBA_LINE_LENGTH, fh) == NULL) {
-               *err = file_error(fh);
+               *err = file_error(fh, err_info);
                if (*err == 0) {
                        *err = WTAP_ERR_SHORT_READ;
                }
-               return -1;
+               return FALSE;
        }
 
        /* Find text in line after "[No.". Limit the length of the
         * two strings since we have fixed buffers for channel[] and
         * direction[] */
-       num_items_scanned = sscanf(line, "%d] %d:%d:%d.%d %9s %9s",
+       num_items_scanned = sscanf(line, "%9d] %2d:%2d:%2d.%9d %9s %9s",
                        &pktnum, &hr, &min, &sec, &csec, channel, direction);
 
        if (num_items_scanned != 7) {
-               *err = WTAP_ERR_BAD_RECORD;
-               return -1;
+               *err = WTAP_ERR_BAD_FILE;
+               *err_info = g_strdup("toshiba: record header isn't valid");
+               return FALSE;
        }
 
        /* Scan lines until we find the OFFSET line. In a "telnet" trace,
@@ -327,11 +288,11 @@ parse_toshiba_rec_hdr(wtap *wth, FILE_T fh,
         */
        do {
                if (file_gets(line, TOSHIBA_LINE_LENGTH, fh) == NULL) {
-                       *err = file_error(fh);
+                       *err = file_error(fh, err_info);
                        if (*err == 0) {
                                *err = WTAP_ERR_SHORT_READ;
                        }
-                       return -1;
+                       return FALSE;
                }
 
                /* Check for "OFFSET 0001-0203" at beginning of line */
@@ -339,72 +300,87 @@ parse_toshiba_rec_hdr(wtap *wth, FILE_T fh,
 
        } while (strcmp(line, "OFFSET 0001-0203") != 0);
 
-       num_items_scanned = sscanf(line+64, "LEN=%d", &pkt_len);
+       num_items_scanned = sscanf(line+64, "LEN=%9d", &pkt_len);
        if (num_items_scanned != 1) {
-               *err = WTAP_ERR_BAD_RECORD;
-               return -1;
+               *err = WTAP_ERR_BAD_FILE;
+               *err_info = g_strdup("toshiba: OFFSET line doesn't have valid LEN item");
+               return FALSE;
        }
-
-       if (wth) {
-               wth->phdr.ts.tv_sec = hr * 3600 + min * 60 + sec;
-               wth->phdr.ts.tv_usec = csec * 10000;
-               wth->phdr.caplen = pkt_len;
-               wth->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':
-                       if (wth)
-                               wth->phdr.pkt_encap = WTAP_ENCAP_PPP;
+                       phdr->pkt_encap = WTAP_ENCAP_ISDN;
+                       pseudo_header->isdn.uton = (direction[0] == 'T');
+                       pseudo_header->isdn.channel = (guint8)
+                           strtol(&channel[1], NULL, 10);
                        break;
 
                case 'D':
-                       if (wth)
-                               wth->phdr.pkt_encap = WTAP_ENCAP_LAPD;
-                       pseudo_header->p2p.sent = 
-                               (direction[0] == 'T' ? TRUE : FALSE );
+                       phdr->pkt_encap = WTAP_ENCAP_ISDN;
+                       pseudo_header->isdn.uton = (direction[0] == 'T');
+                       pseudo_header->isdn.channel = 0;
                        break;
-                       
+
                default:
-                       if (wth)
-                               wth->phdr.pkt_encap = WTAP_ENCAP_ETHERNET;
+                       phdr->pkt_encap = WTAP_ENCAP_ETHERNET;
+                       /* XXX - is there an FCS in the frame? */
+                       pseudo_header->eth.fcs_len = -1;
                        break;
        }
-       return pkt_len;
-}
 
-/* Converts ASCII hex dump to binary data */
-static int
-parse_toshiba_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err)
-{
-       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 */
        hex_lines = pkt_len / 16 + ((pkt_len % 16) ? 1 : 0);
 
        for (i = 0; i < hex_lines; i++) {
                if (file_gets(line, TOSHIBA_LINE_LENGTH, fh) == NULL) {
-                       *err = file_error(fh);
+                       *err = file_error(fh, err_info);
                        if (*err == 0) {
                                *err = WTAP_ERR_SHORT_READ;
                        }
-                       return -1;
+                       return FALSE;
                }
-               if (!parse_single_hex_dump_line(line, buf, i * 16)) {
-                       *err = WTAP_ERR_BAD_RECORD;
-                       return -1;
+               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;
                }
        }
-       return 0;
+       return TRUE;
 }
 
 /*
           1         2         3         4
 0123456789012345678901234567890123456789012345
-0000 : FF03 003D C000 0008 2145 0000 3A12 6500 ...=....!E..:.e.\r
-0010 : 003F 11E6 58CF C11A 8897 A401 0804 0400 .?..X...........\r
-0020 : 0100 01                                 ...\r
+0000 : FF03 003D C000 0008 2145 0000 3A12 6500 ...=....!E..:.e.
+0010 : 003F 11E6 58CF C11A 8897 A401 0804 0400 .?..X...........
+0020 : 0100 01                                 ...
 */
 
 #define START_POS      7
@@ -437,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
         */
@@ -461,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:
+ */