From Tomasz Mon via https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8503 :
[metze/wireshark/wip.git] / wiretap / cosine.c
index 73f305fd918421d99d340f3130c3de0503b136ca..183ca241a74a2823bf0c4f9801917a1d2670afa3 100644 (file)
@@ -173,8 +173,8 @@ static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
 static gboolean cosine_seek_read(wtap *wth, gint64 seek_off,
        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,
@@ -200,8 +200,8 @@ 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, sets "*err_info" to null or an
-   additional error string, and sets hdr to NULL. */
+   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)
 {
@@ -213,27 +213,18 @@ static gint64 cosine_seek_next_packet(wtap *wth, int *err, gchar **err_info,
                if (cur_off == -1) {
                        /* Error */
                        *err = file_error(wth->fh, err_info);
-                       hdr = NULL;
                        return -1;
                }
-               if (file_gets(buf, sizeof(buf), wth->fh) != NULL) {
-                       if (strstr(buf, COSINE_REC_MAGIC_STR1) ||
-                           strstr(buf, COSINE_REC_MAGIC_STR2)) {
-                               g_strlcpy(hdr, buf, COSINE_LINE_LENGTH);
-                               return cur_off;
-                       }
-               } else {
-                       if (file_eof(wth->fh)) {
-                               /* We got an EOF. */
-                               *err = 0;
-                       } else {
-                               /* We got an error. */
-                               *err = file_error(wth->fh, err_info);
-                       }
-                       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;
 }
 
@@ -253,26 +244,22 @@ static gboolean cosine_check_file_type(wtap *wth, int *err, gchar **err_info)
        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_info);
+                       *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;
@@ -283,10 +270,9 @@ int cosine_open(wtap *wth, int *err, gchar **err_info)
 {
        /* Look for CoSine header */
        if (!cosine_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;
        }
 
        if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1)        /* rewind */
@@ -317,8 +303,7 @@ static gboolean cosine_read(wtap *wth, int *err, gchar **err_info,
                return FALSE;
 
        /* Parse the header */
-       pkt_len = parse_cosine_rec_hdr(wth, line, &wth->phdr.pseudo_header, err,
-           err_info);
+       pkt_len = parse_cosine_rec_hdr(&wth->phdr, line, err, err_info);
        if (pkt_len == -1)
                return FALSE;
 
@@ -343,7 +328,6 @@ cosine_seek_read (wtap *wth, gint64 seek_off,
        struct wtap_pkthdr *phdr, guint8 *pd, int len,
        int *err, gchar **err_info)
 {
-       union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
        char    line[COSINE_LINE_LENGTH];
 
        if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
@@ -357,7 +341,7 @@ cosine_seek_read (wtap *wth, gint64 seek_off,
                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);
@@ -369,9 +353,10 @@ 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;
@@ -410,7 +395,7 @@ parse_cosine_rec_hdr(wtap *wth, const char *line,
                yy = mm = dd = hr = min = sec = csec = 0;
        }
 
-       if (wth) {
+       {
                tm.tm_year = yy - 1900;
                tm.tm_mon = mm - 1;
                tm.tm_mday = dd;
@@ -418,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 */