Remove Nmake build system
[metze/wireshark/wip.git] / wiretap / vms.c
index 794a2e76dd0d9c37631e8fb96dea1b8aab93da7b..e9fc9a423a86e46066cc46680e09550a51be60d1 100644 (file)
@@ -1,9 +1,7 @@
 /* vms.c
- *
- * $Id: vms.c,v 1.6 2002/02/08 10:07:41 guy Exp $
  *
  * Wiretap Library
- * Copyright (c) 2001 by Marc Milgram <mmilgram@arrayinc.com>
+ * Copyright (c) 2001 by Marc Milgram <ethereal@mmilgram.NOSPAMmail.net>
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  *
  * 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.
+ */
+
+/* Notes:
+ *   TCPIPtrace TCP fragments don't have the header line.  So, we are never
+ *   to look for that line for the first line of a packet except the first
+ *   packet.  This allows us to read fragmented packets.  Define
+ *   TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE to expect the first line to be
+ *   at the start of every packet.
  */
-#ifdef HAVE_CONFIG_H
 #include "config.h"
-#endif
 #include "wtap-int.h"
-#include "buffer.h"
 #include "vms.h"
 #include "file_wrappers.h"
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <ctype.h>
 
-/* This module reads the output of the 'TCPIPTRACE' command in VMS
- * It was initially based on toshiba.c.
- */
+/* This module reads the output of the various VMS TCPIP trace utilities
+ * such as TCPIPTRACE, TCPTRACE and UCX$TRACE
+ *
+ * It was initially based on toshiba.c and refined with code from cosine.c
+
+--------------------------------------------------------------------------------
+   Example TCPIPTRACE TCPTRACE output data:
 
-/*
-   Example 'TCPIPTRACE' output data:
    TCPIPtrace full display RCV packet 8 at 10-JUL-2001 14:54:19.56
 
    IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 84 = ^x0054
    00000000   00000000   00000000   03000000    0030    ................
    06000000   01000000   A5860100   00000000    0040    ................
                                     00000000    0050    ....
+--------------------------------------------------------------------------------
+
+   Example UCX$TRACE output data:
+
+    UCX INTERnet trace RCV packet seq # = 1 at 14-MAY-2003 11:32:10.93
+
+   IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 583 = ^x0247
+   IP Identifier  = ^x702E,  Flags (0=0,DF=0,MF=0),
+         Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000
+   IP TTL = 128 = ^x80,  Protocol = 17 = ^x11,  Header Checksum = ^x70EC
+   IP Source Address      = 10.20.4.159
+   IP Destination Address = 10.20.4.255
+
+   UDP Source Port = 138,   UDP Destination Port = 138
+   UDP Header and Datagram Length = 563 = ^x0233,   Checksum = ^xB913
+
+   9F04140A   70EC1180   0000702E   47020045    0000    E..G.p.....p....
+   B1B80E11 | B9133302   8A008A00 | FF04140A    0010    .........3......
+   46484648   45200000   1D028A00   9F04140A    0020    ...........EHFHF
+   43414341   4341434D   454D4546   45454550    0030    PEEEFEMEMCACACAC
 
 --------------------------------------------------------------------------------
 
- */
+   Alternate UCX$TRACE type output data:
 
-/* Magic text to check for VMS-ness of file */
-static const char vms_hdr_magic[]  =
-{ 'T', 'C', 'P', 'I', 'P', 't', 'r', 'a', 'c', 'e', ' '};
-#define VMS_HDR_MAGIC_SIZE  (sizeof vms_hdr_magic  / sizeof vms_hdr_magic[0])
+   TCPIP INTERnet trace RCV packet seq # = 1 at 23-OCT-1998 15:19:33.29
 
-/* Magic text for start of packet */
-#define vms_rec_magic vms_hdr_magic
-#define VMS_REC_MAGIC_SIZE  (sizeof vms_rec_magic  / sizeof vms_rec_magic[0])
+   IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 217 = ^x00D9
+   IP Identifier  = ^x0065,  Flags (0=0,DF=0,MF=0),
+         Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000
+   IP TTL = 32 = ^x20,  Protocol = 17 = ^x11,  Header Checksum = ^x8F6C
+   IP Source Address      = 16.20.168.93
+   IP Destination Address = 16.20.255.255
+
+   UDP Source Port = 138,   UDP Destination Port = 138
+   UDP Header and Datagram Length = 197 = ^x00C5,   Checksum = ^x0E77
 
-static gboolean vms_read(wtap *wth, int *err, long *data_offset);
-static int vms_seek_read(wtap *wth, long seek_off,
-    union wtap_pseudo_header *pseudo_header, guint8 *pd, int len);
-static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset, int in_off, int remaining_bytes);
-static int parse_vms_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err);
-static int parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err);
+   5DA81410   8F6C1120   00000065   D9000045    0000    E...awe.....l....]
+            | 0E77C500   8A008A00 | FFFF1410    0010    ..........w.
+
+--------------------------------------------------------------------------------
 
+The only difference between the utilities is the Packet header line, primarily
+the utility identifier and the packet sequence formats.
 
+There appear to be 2 formats for packet seqencing
+
+Format 1:
+
+ ... packet nn at DD-MMM-YYYY hh:mm:ss.ss
+
+Format 2:
+
+ ... packet seq # = nn at DD-MMM-YYYY hh:mm:ss.ss
+
+If there are other formats then code will have to be written in parse_vms_packet()
+to handle them.
+
+--------------------------------------------------------------------------------
+
+ */
+
+/* Magic text to check for VMS-ness of file using possible utility names
+ *
+ */
+#define VMS_HDR_MAGIC_STR1      "TCPIPtrace"
+#define VMS_HDR_MAGIC_STR2      "TCPtrace"
+#define VMS_HDR_MAGIC_STR3      "INTERnet trace"
+
+/* Magic text for start of packet */
+#define VMS_REC_MAGIC_STR1      VMS_HDR_MAGIC_STR1
+#define VMS_REC_MAGIC_STR2      VMS_HDR_MAGIC_STR2
+#define VMS_REC_MAGIC_STR3      VMS_HDR_MAGIC_STR3
+
+#define VMS_HEADER_LINES_TO_CHECK    200
+#define VMS_LINE_LENGTH              240
+
+static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
+    gint64 *data_offset);
+static gboolean vms_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,
+    long byte_offset, int in_off, int remaining_bytes);
+static gboolean parse_vms_packet(FILE_T fh, struct wtap_pkthdr *phdr,
+    Buffer *buf, int *err, gchar **err_info);
+
+#ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
 /* Seeks to the beginning of the next packet, and returns the
-   byte offset.  Returns -1 on failure. */
-/* XXX - Handle I/O errors. */
-static long vms_seek_next_packet(wtap *wth)
+   byte offset.  Returns -1 on failure, and sets "*err" to the error
+   and sets "*err_info" to null or an additional error string. */
+static long vms_seek_next_packet(wtap *wth, int *err, gchar **err_info)
 {
-  int byte;
-  unsigned int level = 0;
-
-  while ((byte = file_getc(wth->fh)) != EOF) {
-    if ((level == 3) && (byte != vms_rec_magic[level]))
-      level += 2;  /* Accept TCPtrace as well as TCPIPtrace */
-    if (byte == vms_rec_magic[level]) {
-      level++;
-      if (level >= VMS_REC_MAGIC_SIZE) {
-          /* note: we're leaving file pointer right after the magic characters */
-        return file_tell(wth->fh) + 1;
-      }
-    } else {
-      level = 0;
+    long cur_off;
+    char buf[VMS_LINE_LENGTH];
+
+    while (1) {
+        cur_off = file_tell(wth->fh);
+        if (cur_off == -1) {
+            /* Error */
+            *err = file_error(wth->fh, err_info);
+            return -1;
+        }
+        if (file_gets(buf, sizeof(buf), wth->fh) == NULL) {
+            /* EOF or error. */
+            *err = file_error(wth->fh, err_info);
+            break;
+        }
+        if (strstr(buf, VMS_REC_MAGIC_STR1) ||
+            strstr(buf, VMS_REC_MAGIC_STR2) ||
+            strstr(buf, VMS_REC_MAGIC_STR2)) {
+            g_strlcpy(hdr, buf,VMS_LINE_LENGTH);
+            return cur_off;
+        }
     }
-  }
-  return -1;
+    return -1;
 }
-
-#define VMS_HEADER_LINES_TO_CHECK    200
-#define VMS_LINE_LENGTH        240
+#endif /* TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE */
 
 /* Look through the first part of a file to see if this is
  * a VMS trace file.
  *
- * Returns TRUE if it is, FALSE if it isn't.
+ * 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 and
+ * "*err_info will be set to null or an additional error string.
  *
- * Leaves file handle at begining of line that contains the VMS Magic
+ * Leaves file handle at beginning of line that contains the VMS Magic
  * identifier.
  */
-static gboolean vms_check_file_type(wtap *wth)
+static gboolean vms_check_file_type(wtap *wth, int *err, gchar **err_info)
 {
-    char    buf[VMS_LINE_LENGTH];
-    int    line, byte;
-    unsigned int reclen, i, level;
-    long mpos;
-   
-    buf[VMS_LINE_LENGTH-1] = 0;
+    char buf[VMS_LINE_LENGTH];
+    guint reclen, line;
+    gint64 mpos;
+
+    buf[VMS_LINE_LENGTH-1] = '\0';
 
     for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
         mpos = file_tell(wth->fh);
-        if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) != NULL) {
-
-            reclen = strlen(buf);
-            if (reclen < VMS_HDR_MAGIC_SIZE)
-                continue;
-
-            level = 0;
-            for (i = 0; i < reclen; i++) {
-                byte = buf[i];
-               if ((level == 3) && (byte != vms_hdr_magic[level]))
-                   level += 2; /* Accept TCPIPtrace as well as TCPtrace */
-                if (byte == vms_hdr_magic[level]) {
-                    level++;
-                    if (level >= VMS_HDR_MAGIC_SIZE) {
-                        file_seek(wth->fh, mpos, SEEK_SET);
-                        return TRUE;
-                    }
-                }
-                else
-                    level = 0;
-            }
+        if (mpos == -1) {
+            /* Error. */
+            *err = file_error(wth->fh, err_info);
+            return FALSE;
         }
-        else
+        if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) == NULL) {
+            /* EOF or error. */
+            *err = file_error(wth->fh, err_info);
             return FALSE;
+        }
+
+        reclen = (guint) strlen(buf);
+        if (reclen < strlen(VMS_HDR_MAGIC_STR1) ||
+            reclen < strlen(VMS_HDR_MAGIC_STR2) ||
+            reclen < strlen(VMS_HDR_MAGIC_STR3)) {
+            continue;
+        }
+
+        if (strstr(buf, VMS_HDR_MAGIC_STR1) ||
+            strstr(buf, VMS_HDR_MAGIC_STR2) ||
+            strstr(buf, VMS_HDR_MAGIC_STR3)) {
+            /* Go back to the beginning of this line, so we will
+             * re-read it. */
+            if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) {
+                /* Error. */
+                return FALSE;
+            }
+            return TRUE;
+        }
     }
+    *err = 0;
     return FALSE;
 }
 
 
-/* XXX - return -1 on I/O error and actually do something with 'err'. */
-int vms_open(wtap *wth, int *err)
+wtap_open_return_val vms_open(wtap *wth, int *err, gchar **err_info)
 {
     /* Look for VMS header */
-    if (!vms_check_file_type(wth)) {
-        return 0;
+    if (!vms_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_RAW_IP;
-    wth->file_type = WTAP_FILE_VMS;
+    wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_VMS;
     wth->snapshot_length = 0; /* not known */
     wth->subtype_read = vms_read;
     wth->subtype_seek_read = vms_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 vms_read(wtap *wth, int *err, long *data_offset)
+/* Find the next packet and parse it; called from wtap_read(). */
+static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
+    gint64 *data_offset)
 {
-    long   offset = 0;
-    guint8    *buf;
-    int    pkt_len;
+    gint64   offset = 0;
 
     /* Find the next packet */
-    offset = vms_seek_next_packet(wth);
+#ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
+    offset = vms_seek_next_packet(wth, err, err_info);
+#else
+    offset = file_tell(wth->fh);
+#endif
     if (offset < 1) {
-        *err = 0;    /* XXX - assume, for now, that it's an EOF */
+        *err = file_error(wth->fh, err_info);
         return FALSE;
     }
-
-    /* Parse the header */
-    pkt_len = parse_vms_rec_hdr(wth, wth->fh, err);
-
-    if (pkt_len == -1)
-       return FALSE;
-
-    /* Make sure we have enough room for the packet */
-    buffer_assure_space(wth->frame_buffer, pkt_len);
-    buf = buffer_start_ptr(wth->frame_buffer);
-
-    /* Convert the ASCII hex dump to binary data */
-    parse_vms_hex_dump(wth->fh, pkt_len, buf, err);
-
-    wth->data_offset = offset;
     *data_offset = offset;
-    return TRUE;
+
+    /* Parse the packet */
+    return parse_vms_packet(wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
 }
 
 /* Used to read packets in random-access fashion */
-static int
-vms_seek_read (wtap *wth, long seek_off, union wtap_pseudo_header *pseudo_header,
-    guint8 *pd, int len)
+static gboolean
+vms_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *phdr,
+    Buffer *buf, int *err, gchar **err_info)
 {
-    int    pkt_len;
-    int    err;
-
-    file_seek(wth->random_fh, seek_off - 1, SEEK_SET);
-
-    pkt_len = parse_vms_rec_hdr(NULL, wth->random_fh, &err);
+    if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
+        return FALSE;
 
-    if (pkt_len != len) {
-        return -1;
+    if (!parse_vms_packet(wth->random_fh, phdr, buf, err, err_info)) {
+        if (*err == 0)
+            *err = WTAP_ERR_SHORT_READ;
+        return FALSE;
     }
-
-    parse_vms_hex_dump(wth->random_fh, pkt_len, pd, &err);
-
-    return 0;
+    return TRUE;
 }
 
-/* isdumpline assumes that dump lines start with some spaces followed by a
- * hex number.
+/* isdumpline assumes that dump lines start with some non-alphanumerics
+ * followed by 4 hex numbers - each 8 digits long, each hex number followed
+ * by 3 spaces.
  */
 static int
-isdumpline( guchar *line )
+isdumpline( gchar *line )
 {
-    int i = 0;
+    int i, j;
 
-    while (i<VMS_LINE_LENGTH && !isalnum(line[i]))
-        i++;
+    while (*line && !g_ascii_isalnum(*line))
+        line++;
 
-    if (! isxdigit(line[i]))
-        return 0;
+    for (j=0; j<4; j++) {
+        for (i=0; i<8; i++, line++)
+            if (! g_ascii_isxdigit(*line))
+                return FALSE;
 
-    while (i<VMS_LINE_LENGTH && isxdigit(line[i]))
-        i++;
+        for (i=0; i<3; i++, line++)
+            if (*line != ' ')
+                return FALSE;
+    }
 
-    return isspace(line[i]);
+    return g_ascii_isspace(*line);
 }
 
-/* Parses a packet record header. */
-static int
-parse_vms_rec_hdr(wtap *wth, FILE_T fh, int *err)
+/* Parses a packet record. */
+static gboolean
+parse_vms_packet(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
 {
-    char    line[VMS_LINE_LENGTH];
+    char   line[VMS_LINE_LENGTH + 1];
     int    num_items_scanned;
-    int           pkt_len = 0;
-    int           pktnum;
-    int           csec = 101;
-    struct tm time;
+    int    pkt_len = 0;
+    int    pktnum;
+    int    csec = 101;
+    struct tm tm;
     char mon[4] = {'J', 'A', 'N', 0};
-    guchar *p;
-    static guchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
-
-    time.tm_year = 1970;
-    time.tm_hour = 1;
-    time.tm_min = 1;
-    time.tm_sec = 1;
+    gchar *p;
+    static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
+    int    i;
+    int    offset = 0;
+    guint8 *pd;
 
+    tm.tm_year = 1970;
+    tm.tm_mon = 0;
+    tm.tm_mday = 1;
+    tm.tm_hour = 1;
+    tm.tm_min = 1;
+    tm.tm_sec = 1;
 
     /* Skip lines until one starts with a hex number */
     do {
         if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
-            *err = file_error(fh);
-           if ((*err == 0) && (csec != 101)) {
-               *err = WTAP_ERR_SHORT_READ;
+            *err = file_error(fh, err_info);
+            if ((*err == 0) && (csec != 101)) {
+                *err = WTAP_ERR_SHORT_READ;
+            }
+            return FALSE;
+        }
+        line[VMS_LINE_LENGTH] = '\0';
+
+        if ((csec == 101) && (p = strstr(line, "packet ")) != NULL
+            && (! strstr(line, "could not save "))) {
+            /* Find text in line starting with "packet ". */
+
+            /* First look for the Format 1 type sequencing */
+            num_items_scanned = sscanf(p,
+                                       "packet %9d at %2d-%3s-%4d %2d:%2d:%2d.%9d",
+                                       &pktnum, &tm.tm_mday, mon,
+                                       &tm.tm_year, &tm.tm_hour,
+                                       &tm.tm_min, &tm.tm_sec, &csec);
+            /* Next look for the Format 2 type sequencing */
+            if (num_items_scanned != 8) {
+              num_items_scanned = sscanf(p,
+                                         "packet seq # = %9d at %2d-%3s-%4d %2d:%2d:%2d.%9d",
+                                         &pktnum, &tm.tm_mday, mon,
+                                         &tm.tm_year, &tm.tm_hour,
+                                         &tm.tm_min, &tm.tm_sec, &csec);
+            }
+            /* if unknown format then exit with error        */
+            /* We will need to add code to handle new format */
+            if (num_items_scanned != 8) {
+                *err = WTAP_ERR_BAD_FILE;
+                *err_info = g_strdup("vms: header line not valid");
+                return FALSE;
             }
-            return -1;
         }
-       if ((csec == 101) && (p = strstr(line, "packet "))
-           && (! strstr(line, "could not save "))) {
-           /* Find text in line starting with "packet ". */
-           num_items_scanned = sscanf(p,
-                                      "packet %d at %d-%3s-%d %d:%d:%d.%d",
-                                      &pktnum, &time.tm_mday, mon,
-                                      &time.tm_year, &time.tm_hour,
-                                      &time.tm_min, &time.tm_sec, &csec);
-
-           if (num_items_scanned != 8) {
-               *err = WTAP_ERR_BAD_RECORD;
-               return -1;
-           }
-       }
         if ( (! pkt_len) && (p = strstr(line, "Length"))) {
             p += sizeof("Length ");
-            while (*p && ! isdigit(*p))
+            while (*p && ! g_ascii_isdigit(*p))
                 p++;
 
             if ( !*p ) {
-                *err = WTAP_ERR_BAD_RECORD;
-                return -1;
+                *err = WTAP_ERR_BAD_FILE;
+                *err_info = g_strdup("vms: Length field not valid");
+                return FALSE;
             }
 
             pkt_len = atoi(p);
-           break;
+            break;
         }
     } while (! isdumpline(line));
-
-    if (wth) {
-        p = strstr(months, mon);
-        if (p)
-            time.tm_mon = (p - months) / 3;
-        time.tm_year -= 1900;
-
-        wth->phdr.ts.tv_sec = mktime(&time);
-
-        wth->phdr.ts.tv_usec = csec * 10000;
-        wth->phdr.caplen = pkt_len;
-        wth->phdr.len = pkt_len;
-        wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
+    if (pkt_len > WTAP_MAX_PACKET_SIZE) {
+        /*
+         * Probably a corrupt capture file; return an error,
+         * so that our caller doesn't blow up trying to allocate
+         * space for an immensely-large packet.
+         */
+        *err = WTAP_ERR_BAD_FILE;
+        *err_info = g_strdup_printf("vms: File has %u-byte packet, bigger than maximum of %u",
+                                    pkt_len, WTAP_MAX_PACKET_SIZE);
+        return FALSE;
     }
 
-    return pkt_len;
-}
+    p = strstr(months, mon);
+    if (p)
+        tm.tm_mon = (int) (p - months) / 3;
+    tm.tm_year -= 1900;
+    tm.tm_isdst = -1;
 
-/* Converts ASCII hex dump to binary data */
-static int
-parse_vms_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err)
-{
-    guchar line[VMS_LINE_LENGTH];
-    int    i;
-    int    offset = 0;
+    phdr->rec_type = REC_TYPE_PACKET;
+    phdr->presence_flags = WTAP_HAS_TS;
+    phdr->ts.secs = mktime(&tm);
+    phdr->ts.nsecs = csec * 10000000;
+    phdr->caplen = pkt_len;
+    phdr->len = pkt_len;
 
+    /* Make sure we have enough room for the packet */
+    ws_buffer_assure_space(buf, pkt_len);
+    pd = ws_buffer_start_ptr(buf);
+
+    /* Convert the ASCII hex dump to binary data */
     for (i = 0; i < pkt_len; i += 16) {
         if (file_gets(line, VMS_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;
         }
+        line[VMS_LINE_LENGTH] = '\0';
         if (i == 0) {
-           while (! isdumpline(line)) /* advance to start of hex data */
-               if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
-                   *err = file_error(fh);
-                   if (*err == 0) {
-                       *err = WTAP_ERR_SHORT_READ;
-                   }
-                   return -1;
-               }
-            while (line[offset] && !isxdigit(line[offset]))
+            while (! isdumpline(line)) { /* advance to start of hex data */
+                if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
+                    *err = file_error(fh, err_info);
+                    if (*err == 0) {
+                        *err = WTAP_ERR_SHORT_READ;
+                    }
+                    return FALSE;
+                }
+                line[VMS_LINE_LENGTH] = '\0';
+            }
+            while (line[offset] && !g_ascii_isxdigit(line[offset]))
                 offset++;
-       }
-       if (!parse_single_hex_dump_line(line, buf, i,
-                                       offset, pkt_len - i)) {
-            *err = WTAP_ERR_BAD_RECORD;
-            return -1;
+        }
+        if (!parse_single_hex_dump_line(line, pd, i,
+                                        offset, pkt_len - i)) {
+            *err = WTAP_ERR_BAD_FILE;
+            *err_info = g_strdup("vms: hex dump not valid");
+            return FALSE;
         }
     }
     /* Avoid TCPIPTRACE-W-BUFFERSFUL, TCPIPtrace could not save n packets.
-     * errors. */
-    file_gets(line, VMS_LINE_LENGTH, fh);
-    return 0;
+     * errors.
+     *
+     * XXX - when we support packet drop report information in the
+     * Wiretap API, we should parse those lines and return "n" as
+     * a packet drop count. */
+    if (!file_gets(line, VMS_LINE_LENGTH, fh)) {
+        *err = file_error(fh, err_info);
+        if (*err == 0) {
+            /* There is no next line, so there's no "TCPIPtrace could not
+             * save n packets" line; not an error. */
+            return TRUE;
+        }
+        return FALSE;
+    }
+    return TRUE;
 }
 
 /*
@@ -384,20 +492,20 @@ parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset,
     int        i;
     char        *s;
     int        value;
-    static int offsets[16] = {39,37,35,33,28,26,24,22,17,15,13,11,6,4,2,0};
+    static const int offsets[16] = {39,37,35,33,28,26,24,22,17,15,13,11,6,4,2,0};
     char lbuf[3] = {0,0,0};
-   
+
 
     /* Get the byte_offset directly from the record */
     s = rec;
-    value = strtoul(s + 45 + in_off, NULL, 16);
-   
+    value = (int)strtoul(s + 45 + in_off, NULL, 16);    /* XXX - error check? */
+
     if (value != byte_offset) {
         return FALSE;
     }
 
     if (remaining > 16)
-       remaining = 16;
+        remaining = 16;
 
     /* Read the octets right to left, as that is how they are displayed
      * in VMS.
@@ -412,3 +520,16 @@ parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset,
 
     return TRUE;
 }
+
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 4
+ * tab-width: 8
+ * indent-tabs-mode: nil
+ * End:
+ *
+ * vi: set shiftwidth=4 tabstop=8 expandtab:
+ * :indentSize=4:tabSize=8:noTabs=true:
+ */