From Tomasz Mon via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8503 :
[metze/wireshark/wip.git] / wiretap / cosine.c
index e43dc007d33aa89713a95fb25e9567dc0a36c353..183ca241a74a2823bf0c4f9801917a1d2670afa3 100644 (file)
@@ -3,7 +3,7 @@
  * $Id$
  *
  * CoSine IPNOS L2 debug output parsing
- * Copyright (c) 2002 by Motonori Shindo <mshindo@mshindo.net>
+ * Copyright (c) 2002 by Motonori Shindo <motonori@shin.do>
  *
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
  *
  * 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 "cosine.h"
 #define COSINE_MAX_PACKET_LEN  65536
 
 static gboolean empty_line(const gchar *line);
-static gint64 cosine_seek_next_packet(wtap *wth, int *err, char *hdr);
-static gboolean cosine_check_file_type(wtap *wth, int *err);
+static gint64 cosine_seek_next_packet(wtap *wth, int *err, gchar **err_info,
+       char *hdr);
+static gboolean cosine_check_file_type(wtap *wth, int *err, gchar **err_info);
 static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
        gint64 *data_offset);
 static gboolean cosine_seek_read(wtap *wth, gint64 seek_off,
-       union wtap_pseudo_header *pseudo_header, guint8 *pd,
+       struct wtap_pkthdr *phdr, guint8 *pd,
        int len, int *err, gchar **err_info);
-static int parse_cosine_rec_hdr(wtap *wth, const char *line,
-       union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
+static int parse_cosine_rec_hdr(struct wtap_pkthdr *phdr, const char *line,
+       int *err, gchar **err_info);
 static int parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf,
        int *err, gchar **err_info);
 static int parse_single_hex_dump_line(char* rec, guint8 *buf,
@@ -201,8 +200,10 @@ static gboolean empty_line(const gchar *line)
 
 /* Seeks to the beginning of the next packet, and returns the
    byte offset. Copy the header line to hdr. Returns -1 on failure,
-   and sets "*err" to the error and set hdr as NULL. */
-static gint64 cosine_seek_next_packet(wtap *wth, int *err, char *hdr)
+   and sets "*err" to the error and sets "*err_info" to null or an
+   additional error string. */
+static gint64 cosine_seek_next_packet(wtap *wth, int *err, gchar **err_info,
+       char *hdr)
 {
        gint64 cur_off;
        char buf[COSINE_LINE_LENGTH];
@@ -211,32 +212,19 @@ static gint64 cosine_seek_next_packet(wtap *wth, int *err, char *hdr)
                cur_off = file_tell(wth->fh);
                if (cur_off == -1) {
                        /* Error */
-                       *err = file_error(wth->fh);
-                       hdr = NULL;
+                       *err = file_error(wth->fh, err_info);
                        return -1;
                }
-               if (file_gets(buf, sizeof(buf), wth->fh) != NULL) {
-                       if (strstr(buf, COSINE_REC_MAGIC_STR1) ||
-                           strstr(buf, COSINE_REC_MAGIC_STR2)) {
-                               strncpy(hdr, buf, COSINE_LINE_LENGTH-1);
-                               hdr[COSINE_LINE_LENGTH-1] = '\0';
-                               return cur_off;
-                       }
-               } else {
-                       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);
-                       }
-                       break;
+               if (file_gets(buf, sizeof(buf), wth->fh) == NULL) {
+                       *err = file_error(wth->fh, err_info);
+                       return -1;
+               }
+               if (strstr(buf, COSINE_REC_MAGIC_STR1) ||
+                   strstr(buf, COSINE_REC_MAGIC_STR2)) {
+                       g_strlcpy(hdr, buf, COSINE_LINE_LENGTH);
+                       return cur_off;
                }
        }
-       hdr = NULL;
        return -1;
 }
 
@@ -244,62 +232,58 @@ static gint64 cosine_seek_next_packet(wtap *wth, int *err, char *hdr)
  * a CoSine L2 debug output.
  *
  * 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 cosine_check_file_type(wtap *wth, int *err)
+static gboolean cosine_check_file_type(wtap *wth, int *err, gchar **err_info)
 {
        char    buf[COSINE_LINE_LENGTH];
-       guint   reclen, line;
+       gsize   reclen;
+       guint   line;
 
        buf[COSINE_LINE_LENGTH-1] = '\0';
 
        for (line = 0; line < COSINE_HEADER_LINES_TO_CHECK; line++) {
-               if (file_gets(buf, COSINE_LINE_LENGTH, wth->fh) != NULL) {
-
-                       reclen = strlen(buf);
-                       if (reclen < strlen(COSINE_HDR_MAGIC_STR1) ||
-                               reclen < strlen(COSINE_HDR_MAGIC_STR2)) {
-                               continue;
-                       }
-
-                       if (strstr(buf, COSINE_HDR_MAGIC_STR1) ||
-                           strstr(buf, COSINE_HDR_MAGIC_STR2)) {
-                               return TRUE;
-                       }
-               } else {
+               if (file_gets(buf, COSINE_LINE_LENGTH, wth->fh) == NULL) {
                        /* EOF or error. */
-                       if (file_eof(wth->fh))
-                               *err = 0;
-                       else
-                               *err = file_error(wth->fh);
+                       *err = file_error(wth->fh, err_info);
                        return FALSE;
                }
+
+               reclen = strlen(buf);
+               if (reclen < strlen(COSINE_HDR_MAGIC_STR1) ||
+                       reclen < strlen(COSINE_HDR_MAGIC_STR2)) {
+                       continue;
+               }
+
+               if (strstr(buf, COSINE_HDR_MAGIC_STR1) ||
+                   strstr(buf, COSINE_HDR_MAGIC_STR2)) {
+                       return TRUE;
+               }
        }
        *err = 0;
        return FALSE;
 }
 
 
-int cosine_open(wtap *wth, int *err, gchar **err_info _U_)
+int cosine_open(wtap *wth, int *err, gchar **err_info)
 {
        /* Look for CoSine header */
-       if (!cosine_check_file_type(wth, err)) {
-               if (*err == 0)
-                       return 0;
-               else
+       if (!cosine_check_file_type(wth, err, err_info)) {
+               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
                        return -1;
+               return 0;
        }
 
        if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1)        /* rewind */
                return -1;
 
-       wth->data_offset = 0;
        wth->file_encap = WTAP_ENCAP_COSINE;
        wth->file_type = WTAP_FILE_COSINE;
        wth->snapshot_length = 0; /* not known */
        wth->subtype_read = cosine_read;
        wth->subtype_seek_read = cosine_seek_read;
-    wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
+       wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
 
        return 1;
 }
@@ -314,13 +298,12 @@ static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
        char    line[COSINE_LINE_LENGTH];
 
        /* Find the next packet */
-       offset = cosine_seek_next_packet(wth, err, line);
+       offset = cosine_seek_next_packet(wth, err, err_info, line);
        if (offset < 0)
                return FALSE;
 
        /* Parse the header */
-       pkt_len = parse_cosine_rec_hdr(wth, line, &wth->pseudo_header, err,
-           err_info);
+       pkt_len = parse_cosine_rec_hdr(&wth->phdr, line, err, err_info);
        if (pkt_len == -1)
                return FALSE;
 
@@ -333,7 +316,7 @@ static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
            err_info)) == -1)
                return FALSE;
 
-       wth->data_offset = offset;
+       wth->phdr.presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
        wth->phdr.caplen = caplen;
        *data_offset = offset;
        return TRUE;
@@ -342,7 +325,7 @@ static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
 /* Used to read packets in random-access fashion */
 static gboolean
 cosine_seek_read (wtap *wth, gint64 seek_off,
-       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
+       struct wtap_pkthdr *phdr, guint8 *pd, int len,
        int *err, gchar **err_info)
 {
        char    line[COSINE_LINE_LENGTH];
@@ -351,14 +334,14 @@ cosine_seek_read (wtap *wth, gint64 seek_off,
                return FALSE;
 
        if (file_gets(line, COSINE_LINE_LENGTH, wth->random_fh) == NULL) {
-               *err = file_error(wth->random_fh);
+               *err = file_error(wth->random_fh, err_info);
                if (*err == 0) {
                        *err = WTAP_ERR_SHORT_READ;
                }
                return FALSE;
        }
 
-       if (parse_cosine_rec_hdr(NULL, line, pseudo_header, err, err_info) == -1)
+       if (parse_cosine_rec_hdr(phdr, line, err, err_info) == -1)
                return FALSE;
 
        return parse_cosine_hex_dump(wth->random_fh, len, pd, err, err_info);
@@ -370,48 +353,49 @@ cosine_seek_read (wtap *wth, gint64 seek_off,
     2) output to PE without date and time
         l2-tx (FR:3/7/1:1), Length:18, Pro:0, Off:0, Pri:0, RM:0, Err:0 [0x4000, 0x0] */
 static int
-parse_cosine_rec_hdr(wtap *wth, const char *line,
-    union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
+parse_cosine_rec_hdr(struct wtap_pkthdr *phdr, const char *line,
+     int *err, gchar **err_info)
 {
+       union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
        int     num_items_scanned;
        int     yy, mm, dd, hr, min, sec, csec, pkt_len;
        int     pro, off, pri, rm, error;
        guint   code1, code2;
-       char    if_name[COSINE_MAX_IF_NAME_LEN], direction[6];
+       char    if_name[COSINE_MAX_IF_NAME_LEN] = "", direction[6] = "";
        struct  tm tm;
 
-       if (sscanf(line, "%d-%d-%d,%d:%d:%d.%d:",
+       if (sscanf(line, "%4d-%2d-%2d,%2d:%2d:%2d.%9d:",
                   &yy, &mm, &dd, &hr, &min, &sec, &csec) == 7) {
                /* appears to be output to a control blade */
                num_items_scanned = sscanf(line,
-                  "%d-%d-%d,%d:%d:%d.%d: %5s (%127[A-Za-z0-9/:]), Length:%d, Pro:%d, Off:%d, Pri:%d, RM:%d, Err:%d [%x, %x]",
+                  "%4d-%2d-%2d,%2d:%2d:%2d.%9d: %5s (%127[A-Za-z0-9/:]), Length:%9d, Pro:%9d, Off:%9d, Pri:%9d, RM:%9d, Err:%9d [%8x, %8x]",
                        &yy, &mm, &dd, &hr, &min, &sec, &csec,
                                   direction, if_name, &pkt_len,
                                   &pro, &off, &pri, &rm, &error,
                                   &code1, &code2);
 
                if (num_items_scanned != 17) {
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("cosine: purported control blade line doesn't have code values");
                        return -1;
                }
        } else {
                /* appears to be output to PE */
                num_items_scanned = sscanf(line,
-                  "%5s (%127[A-Za-z0-9/:]), Length:%d, Pro:%d, Off:%d, Pri:%d, RM:%d, Err:%d [%x, %x]",
+                  "%5s (%127[A-Za-z0-9/:]), Length:%9d, Pro:%9d, Off:%9d, Pri:%9d, RM:%9d, Err:%9d [%8x, %8x]",
                                   direction, if_name, &pkt_len,
                                   &pro, &off, &pri, &rm, &error,
                                   &code1, &code2);
 
                if (num_items_scanned != 10) {
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("cosine: header line is neither control blade nor PE output");
                        return -1;
                }
                yy = mm = dd = hr = min = sec = csec = 0;
        }
 
-       if (wth) {
+       {
                tm.tm_year = yy - 1900;
                tm.tm_mon = mm - 1;
                tm.tm_mday = dd;
@@ -419,9 +403,9 @@ parse_cosine_rec_hdr(wtap *wth, const char *line,
                tm.tm_min = min;
                tm.tm_sec = sec;
                tm.tm_isdst = -1;
-               wth->phdr.ts.secs = mktime(&tm);
-               wth->phdr.ts.nsecs = csec * 10000000;
-               wth->phdr.len = pkt_len;
+               phdr->ts.secs = mktime(&tm);
+               phdr->ts.nsecs = csec * 10000000;
+               phdr->len = pkt_len;
        }
        /* XXX need to handle other encapsulations like Cisco HDLC,
           Frame Relay and ATM */
@@ -449,8 +433,8 @@ parse_cosine_rec_hdr(wtap *wth, const char *line,
        } else if (strncmp(direction, "l2-rx", 5) == 0) {
                pseudo_header->cosine.direction = COSINE_DIR_RX;
        }
-       strncpy(pseudo_header->cosine.if_name, if_name,
-               COSINE_MAX_IF_NAME_LEN - 1);
+       g_strlcpy(pseudo_header->cosine.if_name, if_name,
+               COSINE_MAX_IF_NAME_LEN);
        pseudo_header->cosine.pro = pro;
        pseudo_header->cosine.off = off;
        pseudo_header->cosine.pri = pri;
@@ -475,7 +459,7 @@ parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err,
 
        for (i = 0; i < hex_lines; i++) {
                if (file_gets(line, COSINE_LINE_LENGTH, fh) == NULL) {
-                       *err = file_error(fh);
+                       *err = file_error(fh, err_info);
                        if (*err == 0) {
                                *err = WTAP_ERR_SHORT_READ;
                        }
@@ -485,7 +469,7 @@ parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err,
                        break;
                }
                if ((n = parse_single_hex_dump_line(line, buf, i*16)) == -1) {
-                       *err = WTAP_ERR_BAD_RECORD;
+                       *err = WTAP_ERR_BAD_FILE;
                        *err_info = g_strdup("cosine: hex dump line doesn't have 16 numbers");
                        return -1;
                }