The "file types" we have are actually combinations of types and
[metze/wireshark/wip.git] / wiretap / toshiba.c
index 921bd988aa9bc2282a46200bd87e6670a5c7de45..d35b2c8023b1068bc5160e5671ffd346264540a8 100644 (file)
  *
  * 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"
@@ -112,14 +110,12 @@ static const char toshiba_rec_magic[]  = { '[', 'N', 'o', '.' };
 static gboolean toshiba_read(wtap *wth, int *err, gchar **err_info,
        gint64 *data_offset);
 static gboolean toshiba_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);
 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(wtap *wth, FILE_T fh,
-    union wtap_pseudo_header *pseudo_header, 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
@@ -147,13 +143,8 @@ static gint64 toshiba_seek_next_packet(wtap *wth, int *err, gchar **err_info)
       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);
-  }
+  /* EOF or error. */
+  *err = file_error(wth->fh, err_info);
   return -1;
 }
 
@@ -176,34 +167,29 @@ static gboolean toshiba_check_file_type(wtap *wth, int *err, gchar **err_info)
        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 = (guint) 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, err_info);
-                       return FALSE;
+                       else {
+                               level = 0;
+                       }
                }
        }
        *err = 0;
@@ -215,15 +201,13 @@ int 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)
-                       return 0;
-               else
+               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
                        return -1;
+               return 0;
        }
 
-       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;
@@ -237,68 +221,47 @@ 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;
+       *data_offset = offset;
 
-       /* Parse the header */
-       pkt_len = parse_toshiba_rec_hdr(wth, wth->fh, &wth->pseudo_header,
+       /* Parse the packet */
+       return parse_toshiba_packet(wth->fh, &wth->phdr, wth->frame_buffer,
            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;
-
-       wth->data_offset = offset;
-       *data_offset = offset;
-       return TRUE;
 }
 
 /* Used to read packets in random-access fashion */
 static gboolean
-toshiba_seek_read (wtap *wth, gint64 seek_off,
-       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
+toshiba_seek_read(wtap *wth, gint64 seek_off,
+       struct wtap_pkthdr *phdr, Buffer *buf, int len _U_,
        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(NULL, wth->random_fh, pseudo_header,
-           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(wtap *wth, FILE_T fh,
-    union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
+/* 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
@@ -309,7 +272,7 @@ parse_toshiba_rec_hdr(wtap *wth, 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
@@ -321,7 +284,7 @@ parse_toshiba_rec_hdr(wtap *wth, 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,
@@ -339,7 +302,7 @@ parse_toshiba_rec_hdr(wtap *wth, FILE_T fh,
                        if (*err == 0) {
                                *err = WTAP_ERR_SHORT_READ;
                        }
-                       return -1;
+                       return FALSE;
                }
 
                /* Check for "OFFSET 0001-0203" at beginning of line */
@@ -351,48 +314,39 @@ parse_toshiba_rec_hdr(wtap *wth, 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;
        }
 
-       if (wth) {
-               wth->phdr.ts.secs = hr * 3600 + min * 60 + sec;
-               wth->phdr.ts.nsecs = csec * 10000000;
-               wth->phdr.caplen = pkt_len;
-               wth->phdr.len = pkt_len;
-       }
+       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_ISDN;
+                       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_ISDN;
+                       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 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 */
+       buffer_assure_space(buf, TOSHIBA_MAX_PACKET_LEN);
+       pd = buffer_start_ptr(buf);
 
        /* Calculate the number of hex dump lines, each
         * containing 16 bytes of data */
@@ -406,7 +360,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;