From Tobias Witek:
[obnox/wireshark/wip.git] / wiretap / cosine.c
index 64e0a6395c073579e4597ad5062661f7006f3024..3f8fafedb4d0f95a646f04c7dc131ae0ffac4869 100644 (file)
@@ -1,6 +1,6 @@
 /* cosine.c
  *
- * $Id: cosine.c,v 1.6 2003/11/25 05:58:56 guy Exp $
+ * $Id$
  *
  * CoSine IPNOS L2 debug output parsing
  * Copyright (c) 2002 by Motonori Shindo <mshindo@mshindo.net>
 
 #define COSINE_MAX_PACKET_LEN  65536
 
-static gboolean empty_line(const guchar *line);
-static long cosine_seek_next_packet(wtap *wth, int *err, char *hdr);
+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 gboolean cosine_read(wtap *wth, int *err, long *data_offset);
-static gboolean cosine_seek_read(wtap *wth, long seek_off,
+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,
-       int len, int *err);
+       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);
+       union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
 static int parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf,
-       int *err);
+       int *err, gchar **err_info);
 static int parse_single_hex_dump_line(char* rec, guint8 *buf,
        guint byte_offset);
 
 /* Returns TRUE if the line appears to be an empty line. Otherwise it
    returns FALSE. */
-static gboolean empty_line(const guchar *line)
+static gboolean empty_line(const gchar *line)
 {
        while (*line) {
-               if (isspace(*line)) {
+               if (isspace((guchar)*line)) {
                        line++;
                        continue;
                } else {
@@ -201,9 +202,9 @@ static gboolean empty_line(const guchar *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 long cosine_seek_next_packet(wtap *wth, int *err, char *hdr)
+static gint64 cosine_seek_next_packet(wtap *wth, int *err, char *hdr)
 {
-       long cur_off;
+       gint64 cur_off;
        char buf[COSINE_LINE_LENGTH];
 
        while (1) {
@@ -217,8 +218,7 @@ static long cosine_seek_next_packet(wtap *wth, int *err, char *hdr)
                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';
+                               g_strlcpy(hdr, buf, COSINE_LINE_LENGTH);
                                return cur_off;
                        }
                } else {
@@ -248,7 +248,8 @@ static long cosine_seek_next_packet(wtap *wth, int *err, char *hdr)
 static gboolean cosine_check_file_type(wtap *wth, int *err)
 {
        char    buf[COSINE_LINE_LENGTH];
-       guint   reclen, line;
+       gsize   reclen;
+       guint   line;
 
        buf[COSINE_LINE_LENGTH-1] = '\0';
 
@@ -279,7 +280,7 @@ static gboolean cosine_check_file_type(wtap *wth, int *err)
 }
 
 
-int cosine_open(wtap *wth, int *err)
+int cosine_open(wtap *wth, int *err, gchar **err_info _U_)
 {
        /* Look for CoSine header */
        if (!cosine_check_file_type(wth, err)) {
@@ -298,14 +299,16 @@ int cosine_open(wtap *wth, int *err)
        wth->snapshot_length = 0; /* not known */
        wth->subtype_read = cosine_read;
        wth->subtype_seek_read = cosine_seek_read;
+    wth->tsprecision = WTAP_FILE_TSPREC_CSEC;
 
        return 1;
 }
 
-/* Find the next packet and parse it; called from wtap_loop(). */
-static gboolean cosine_read(wtap *wth, int *err, long *data_offset)
+/* Find the next packet and parse it; called from wtap_read(). */
+static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
+    gint64 *data_offset)
 {
-       long    offset;
+       gint64  offset;
        guint8  *buf;
        int     pkt_len, caplen;
        char    line[COSINE_LINE_LENGTH];
@@ -316,7 +319,8 @@ static gboolean cosine_read(wtap *wth, int *err, long *data_offset)
                return FALSE;
 
        /* Parse the header */
-       pkt_len = parse_cosine_rec_hdr(wth, line, &wth->pseudo_header, err);
+       pkt_len = parse_cosine_rec_hdr(wth, line, &wth->pseudo_header, err,
+           err_info);
        if (pkt_len == -1)
                return FALSE;
 
@@ -325,7 +329,8 @@ static gboolean cosine_read(wtap *wth, int *err, long *data_offset)
        buf = buffer_start_ptr(wth->frame_buffer);
 
        /* Convert the ASCII hex dump to binary data */
-       if ((caplen = parse_cosine_hex_dump(wth->fh, pkt_len, buf, err)) == -1)
+       if ((caplen = parse_cosine_hex_dump(wth->fh, pkt_len, buf, err,
+           err_info)) == -1)
                return FALSE;
 
        wth->data_offset = offset;
@@ -336,8 +341,9 @@ static gboolean cosine_read(wtap *wth, int *err, long *data_offset)
 
 /* Used to read packets in random-access fashion */
 static gboolean
-cosine_seek_read (wtap *wth, long seek_off,
-       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len, int *err)
+cosine_seek_read (wtap *wth, gint64 seek_off,
+       union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
+       int *err, gchar **err_info)
 {
        char    line[COSINE_LINE_LENGTH];
 
@@ -352,10 +358,10 @@ cosine_seek_read (wtap *wth, long seek_off,
                return FALSE;
        }
 
-       if (parse_cosine_rec_hdr(NULL, line, pseudo_header, err) == -1)
+       if (parse_cosine_rec_hdr(NULL, line, pseudo_header, err, err_info) == -1)
                return FALSE;
 
-       return parse_cosine_hex_dump(wth->random_fh, len, pd, err);
+       return parse_cosine_hex_dump(wth->random_fh, len, pd, err, err_info);
 }
 
 /* Parses a packet record header. There are two possible formats:
@@ -364,11 +370,13 @@ cosine_seek_read (wtap *wth, long 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)
+parse_cosine_rec_hdr(wtap *wth, const char *line,
+    union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
 {
        int     num_items_scanned;
        int     yy, mm, dd, hr, min, sec, csec, pkt_len;
-       int     pro, off, pri, rm, error, code1, code2;
+       int     pro, off, pri, rm, error;
+       guint   code1, code2;
        char    if_name[COSINE_MAX_IF_NAME_LEN], direction[6];
        struct  tm tm;
 
@@ -384,6 +392,7 @@ parse_cosine_rec_hdr(wtap *wth, const char *line, union wtap_pseudo_header *pseu
 
                if (num_items_scanned != 17) {
                        *err = WTAP_ERR_BAD_RECORD;
+                       *err_info = g_strdup("cosine: purported control blade line doesn't have code values");
                        return -1;
                }
        } else {
@@ -396,6 +405,7 @@ parse_cosine_rec_hdr(wtap *wth, const char *line, union wtap_pseudo_header *pseu
 
                if (num_items_scanned != 10) {
                        *err = WTAP_ERR_BAD_RECORD;
+                       *err_info = g_strdup("cosine: header line is neither control blade nor PE output");
                        return -1;
                }
                yy = mm = dd = hr = min = sec = csec = 0;
@@ -409,10 +419,9 @@ parse_cosine_rec_hdr(wtap *wth, const char *line, union wtap_pseudo_header *pseu
                tm.tm_min = min;
                tm.tm_sec = sec;
                tm.tm_isdst = -1;
-               wth->phdr.ts.tv_sec = mktime(&tm);
-               wth->phdr.ts.tv_usec = csec * 10000;
+               wth->phdr.ts.secs = mktime(&tm);
+               wth->phdr.ts.nsecs = csec * 10000000;
                wth->phdr.len = pkt_len;
-               wth->phdr.pkt_encap = WTAP_ENCAP_COSINE;
        }
        /* XXX need to handle other encapsulations like Cisco HDLC,
           Frame Relay and ATM */
@@ -440,8 +449,8 @@ parse_cosine_rec_hdr(wtap *wth, const char *line, union wtap_pseudo_header *pseu
        } 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;
@@ -454,9 +463,10 @@ parse_cosine_rec_hdr(wtap *wth, const char *line, union wtap_pseudo_header *pseu
 /* Converts ASCII hex dump to binary data. Returns the capture length.
    If any error is encountered, -1 is returned. */
 static int
-parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err)
+parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err,
+    gchar **err_info)
 {
-       char    line[COSINE_LINE_LENGTH];
+       gchar   line[COSINE_LINE_LENGTH];
        int     i, hex_lines, n, caplen = 0;
 
        /* Calculate the number of hex dump lines, each
@@ -476,6 +486,7 @@ parse_cosine_hex_dump(FILE_T fh, int pkt_len, guint8* buf, int *err)
                }
                if ((n = parse_single_hex_dump_line(line, buf, i*16)) == -1) {
                        *err = WTAP_ERR_BAD_RECORD;
+                       *err_info = g_strdup("cosine: hex dump line doesn't have 16 numbers");
                        return -1;
                }
                caplen += n;