replace SPDX identifier GPL-2.0+ with GPL-2.0-or-later.
[metze/wireshark/wip.git] / wiretap / radcom.c
index 582c819f7f6a602dd799bdac78fe52b37e200c55..8583ef55a695ac2c54ea0da7d887d116e424cf81 100644 (file)
@@ -3,29 +3,15 @@
  * Wiretap Library
  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
  *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 #include "config.h"
 
 #include <errno.h>
 #include <string.h>
-#include "wftap-int.h"
 #include "wtap-int.h"
 #include "file_wrappers.h"
-#include "buffer.h"
 #include "radcom.h"
 
 struct frame_date {
@@ -55,7 +41,7 @@ static const guint8 encap_magic[4] = {
 };
 
 static const guint8 active_time_magic[11] = {
-       0x41, 0x63, 0x74, 0x69, 0x76, 0x65, 0x20, 0x54, 0x69, 0x6d, 0x65
+       'A', 'c', 't', 'i', 'v', 'e', ' ', 'T', 'i', 'm', 'e'
 };
 
 /* RADCOM record header - followed by frame data (perhaps including FCS).
@@ -85,18 +71,15 @@ struct radcomrec_hdr {
        char    xxw[9];         /* unknown */
 };
 
-static gboolean radcom_read(wftap *wfth, int *err, gchar **err_info,
+static gboolean radcom_read(wtap *wth, int *err, gchar **err_info,
        gint64 *data_offset);
-static gboolean radcom_seek_read(wftap *wfth, gint64 seek_off,
-       void* header, Buffer *buf, int *err, gchar **err_info);
-static gboolean radcom_read_rec(wftap *wfth, FILE_T fh, struct wtap_pkthdr *phdr,
+static gboolean radcom_seek_read(wtap *wth, gint64 seek_off,
+       struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
+static gboolean radcom_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
        Buffer *buf, int *err, gchar **err_info);
-static gboolean radcom_read_rec_data(FILE_T fh, guint8 *pd, int length,
-       int *err, gchar **err_info);
 
-int radcom_open(wftap *wfth, int *err, gchar **err_info)
+wtap_open_return_val radcom_open(wtap *wth, int *err, gchar **err_info)
 {
-       int bytes_read;
        guint8 r_magic[8], t_magic[11], search_encap[7];
        struct frame_date start_date;
 #if 0
@@ -105,13 +88,10 @@ int radcom_open(wftap *wfth, int *err, gchar **err_info)
 #endif
 
        /* Read in the string that should be at the start of a RADCOM file */
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(r_magic, 8, wfth->fh);
-       if (bytes_read != 8) {
-               *err = file_error(wfth->fh, err_info);
-               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-                       return -1;
-               return 0;
+       if (!wtap_read_bytes(wth->fh, r_magic, 8, err, err_info)) {
+               if (*err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
        }
 
        /* XXX: bytes 2 and 3 of the "magic" header seem to be different in some
@@ -121,50 +101,82 @@ int radcom_open(wftap *wfth, int *err, gchar **err_info)
        r_magic[1] = 0xD2;
        r_magic[2] = 0x00;
        if (memcmp(r_magic, radcom_magic, 8) != 0) {
-               return 0;
+               return WTAP_OPEN_NOT_MINE;
        }
 
        /* Look for the "Active Time" string. The "frame_date" structure should
         * be located 32 bytes before the beginning of this string */
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(t_magic, 11, wfth->fh);
-       if (bytes_read != 11) {
-               *err = file_error(wfth->fh, err_info);
-               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-                       return -1;
-               return 0;
+       if (!wtap_read_bytes(wth->fh, t_magic, 11, err, err_info)) {
+               if (*err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
        }
        while (memcmp(t_magic, active_time_magic, 11) != 0)
        {
-               if (file_seek(wfth->fh, -10, SEEK_CUR, err) == -1)
-                       return -1;
-               errno = WTAP_ERR_CANT_READ;
-               bytes_read = file_read(t_magic, 11, wfth->fh);
-               if (bytes_read != 11) {
-                       *err = file_error(wfth->fh, err_info);
-                       if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-                               return -1;
-                       return 0;
+               if (file_seek(wth->fh, -10, SEEK_CUR, err) == -1)
+                       return WTAP_OPEN_ERROR;
+               if (!wtap_read_bytes(wth->fh, t_magic, 11, err, err_info)) {
+                       if (*err != WTAP_ERR_SHORT_READ)
+                               return WTAP_OPEN_ERROR;
+                       return WTAP_OPEN_NOT_MINE;
                }
        }
-       if (file_seek(wfth->fh, -43, SEEK_CUR, err) == -1) return -1;
+       if (file_seek(wth->fh, -43, SEEK_CUR, err) == -1)
+               return WTAP_OPEN_ERROR;
 
        /* Get capture start time */
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&start_date, sizeof(struct frame_date), wfth->fh);
-       if (bytes_read != sizeof(struct frame_date)) {
-               *err = file_error(wfth->fh, err_info);
-               if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
-                       return -1;
-               return 0;
+       if (!wtap_read_bytes(wth->fh, &start_date, sizeof(struct frame_date),
+           err, err_info)) {
+               if (*err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
+       }
+
+       /* So what time is this? */
+       if (!wtap_read_bytes(wth->fh, NULL, sizeof(struct frame_date),
+           err, err_info)) {
+               if (*err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
+       }
+
+       for (;;) {
+               if (!wtap_read_bytes(wth->fh, search_encap, 4,
+                   err, err_info)) {
+                       if (*err != WTAP_ERR_SHORT_READ)
+                               return WTAP_OPEN_ERROR;
+                       return WTAP_OPEN_NOT_MINE;
+               }
+
+               if (memcmp(encap_magic, search_encap, 4) == 0)
+                       break;
+
+               /*
+                * OK, that's not it, go forward 1 byte - reading
+                * the magic moved us forward 4 bytes, so seeking
+                * backward 3 bytes moves forward 1 byte - and
+                * try the 4 bytes at that offset.
+                */
+               if (file_seek(wth->fh, -3, SEEK_CUR, err) == -1)
+                       return WTAP_OPEN_ERROR;
+       }
+       if (!wtap_read_bytes(wth->fh, NULL, 12, err, err_info)) {
+               if (*err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
+       }
+       if (!wtap_read_bytes(wth->fh, search_encap, 4, err, err_info)) {
+               if (*err != WTAP_ERR_SHORT_READ)
+                       return WTAP_OPEN_ERROR;
+               return WTAP_OPEN_NOT_MINE;
        }
 
        /* This is a radcom file */
-       wfth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_RADCOM;
-       wfth->subtype_read = radcom_read;
-       wfth->subtype_seek_read = radcom_seek_read;
-       wfth->snapshot_length = 0; /* not available in header, only in frame */
-       wfth->tsprecision = WTAP_FILE_TSPREC_USEC;
+       wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_RADCOM;
+       wth->subtype_read = radcom_read;
+       wth->subtype_seek_read = radcom_seek_read;
+       wth->snapshot_length = 0; /* not available in header, only in frame */
+       wth->file_tsprec = WTAP_TSPREC_USEC;
 
 #if 0
        tm.tm_year = pletoh16(&start_date.year)-1900;
@@ -176,127 +188,84 @@ int radcom_open(wftap *wfth, int *err, gchar **err_info)
        tm.tm_sec = sec%60;
        tm.tm_isdst = -1;
 #endif
-       if (file_seek(wfth->fh, sizeof(struct frame_date), SEEK_CUR, err) == -1)
-               return -1;
 
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(search_encap, 4, wfth->fh);
-       if (bytes_read != 4) {
-               goto read_error;
-       }
-       while (memcmp(encap_magic, search_encap, 4)) {
-               if (file_seek(wfth->fh, -3, SEEK_CUR, err) == -1)
-                       return -1;
-               errno = WTAP_ERR_CANT_READ;
-               bytes_read = file_read(search_encap, 4, wfth->fh);
-               if (bytes_read != 4) {
-                       goto read_error;
-               }
-       }
-       if (file_seek(wfth->fh, 12, SEEK_CUR, err) == -1)
-               return -1;
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(search_encap, 4, wfth->fh);
-       if (bytes_read != 4) {
-               goto read_error;
-       }
        if (memcmp(search_encap, "LAPB", 4) == 0)
-               wfth->file_encap = WTAP_ENCAP_LAPB;
+               wth->file_encap = WTAP_ENCAP_LAPB;
        else if (memcmp(search_encap, "Ethe", 4) == 0)
-               wfth->file_encap = WTAP_ENCAP_ETHERNET;
+               wth->file_encap = WTAP_ENCAP_ETHERNET;
        else if (memcmp(search_encap, "ATM/", 4) == 0)
-               wfth->file_encap = WTAP_ENCAP_ATM_RFC1483;
+               wth->file_encap = WTAP_ENCAP_ATM_RFC1483;
        else {
-               *err = WTAP_ERR_UNSUPPORTED_ENCAP;
+               *err = WTAP_ERR_UNSUPPORTED;
                *err_info = g_strdup_printf("radcom: network type \"%.4s\" unknown", search_encap);
-               return -1;
+               return WTAP_OPEN_ERROR;
        }
 
 #if 0
-       bytes_read = file_read(&next_date, sizeof(struct frame_date), wfth->fh);
-       errno = WTAP_ERR_CANT_READ;
-       if (bytes_read != sizeof(struct frame_date)) {
-               goto read_error;
-       }
+       if (!wtap_read_bytes(wth->fh, &next_date, sizeof(struct frame_date),
+           err, err_info))
+               return WTAP_OPEN_ERROR;
 
        while (memcmp(&start_date, &next_date, 4)) {
-               if (file_seek(wfth->fh, 1-sizeof(struct frame_date), SEEK_CUR, err) == -1)
-                       return -1;
-               errno = WTAP_ERR_CANT_READ;
-               bytes_read = file_read(&next_date, sizeof(struct frame_date),
-                                  wfth->fh);
-               if (bytes_read != sizeof(struct frame_date)) {
-                       goto read_error;
-               }
+               if (file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR, err) == -1)
+                       return WTAP_OPEN_ERROR;
+               if (!wtap_read_bytes(wth->fh, &next_date, sizeof(struct frame_date),
+                   err, err_info))
+                       return WTAP_OPEN_ERROR;
        }
 #endif
 
-       if (wfth->file_encap == WTAP_ENCAP_ETHERNET) {
-               if (file_seek(wfth->fh, 294, SEEK_CUR, err) == -1)
-                       return -1;
-       } else if (wfth->file_encap == WTAP_ENCAP_LAPB) {
-               if (file_seek(wfth->fh, 297, SEEK_CUR, err) == -1)
-                       return -1;
-       } else if (wfth->file_encap == WTAP_ENCAP_ATM_RFC1483) {
-               if (file_seek(wfth->fh, 504, SEEK_CUR, err) == -1)
-                       return -1;
+       if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
+               if (!wtap_read_bytes(wth->fh, NULL, 294, err, err_info))
+                       return WTAP_OPEN_ERROR;
+       } else if (wth->file_encap == WTAP_ENCAP_LAPB) {
+               if (!wtap_read_bytes(wth->fh, NULL, 297, err, err_info))
+                       return WTAP_OPEN_ERROR;
+       } else if (wth->file_encap == WTAP_ENCAP_ATM_RFC1483) {
+               if (!wtap_read_bytes(wth->fh, NULL, 504, err, err_info))
+                       return WTAP_OPEN_ERROR;
        }
 
-       return 1;
-
-read_error:
-       *err = file_error(wfth->fh, err_info);
-       if (*err != 0)
-               return -1;
-       return 0;
+       return WTAP_OPEN_MINE;
 }
 
 /* Read the next packet */
-static gboolean radcom_read(wftap *wfth, int *err, gchar **err_info,
+static gboolean radcom_read(wtap *wth, int *err, gchar **err_info,
                            gint64 *data_offset)
 {
-       int     bytes_read;
        char    fcs[2];
-       wtap* wth = (wtap*)wfth->tap_specific_data;
 
-       *data_offset = file_tell(wfth->fh);
+       *data_offset = file_tell(wth->fh);
 
-       /* Read record header. */
-       if (!radcom_read_rec(wfth, wfth->fh, &wth->phdr, wfth->frame_buffer,
+       /* Read record. */
+       if (!radcom_read_rec(wth, wth->fh, &wth->phdr, wth->frame_buffer,
            err, err_info)) {
                /* Read error or EOF */
                return FALSE;
        }
 
-       if (wfth->file_encap == WTAP_ENCAP_LAPB) {
+       if (wth->file_encap == WTAP_ENCAP_LAPB) {
                /* Read the FCS.
                   XXX - should we have some way of indicating the
                   presence and size of an FCS to our caller?
                   That'd let us handle other file types as well. */
-               errno = WTAP_ERR_CANT_READ;
-               bytes_read = file_read(&fcs, sizeof fcs, wfth->fh);
-               if (bytes_read != sizeof fcs) {
-                       *err = file_error(wfth->fh, err_info);
-                       if (*err == 0)
-                               *err = WTAP_ERR_SHORT_READ;
+               if (!wtap_read_bytes(wth->fh, &fcs, sizeof fcs, err, err_info))
                        return FALSE;
-               }
        }
 
        return TRUE;
 }
 
 static gboolean
-radcom_seek_read(wftap *wfth, gint64 seek_off,
-                void* header, Buffer *buf,
+radcom_seek_read(wtap *wth, gint64 seek_off,
+                struct wtap_pkthdr *phdr, Buffer *buf,
                 int *err, gchar **err_info)
 {
-       struct wtap_pkthdr *phdr = (struct wtap_pkthdr *)header;
-       if (file_seek(wfth->random_fh, seek_off, SEEK_SET, err) == -1)
+       if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
                return FALSE;
 
        /* Read record. */
-       if (!radcom_read_rec(wfth, wfth->random_fh, phdr, buf, err,
+       if (!radcom_read_rec(wth, wth->random_fh, phdr, buf, err,
            err_info)) {
                /* Read error or EOF */
                if (*err == 0) {
@@ -309,24 +278,17 @@ radcom_seek_read(wftap *wfth, gint64 seek_off,
 }
 
 static gboolean
-radcom_read_rec(wftap *wfth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
+radcom_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
                int *err, gchar **err_info)
 {
        struct radcomrec_hdr hdr;
-       int     bytes_read;
        guint16 data_length, real_length, length;
        guint32 sec;
        struct tm tm;
        guint8  atmhdr[8];
 
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(&hdr, sizeof hdr, fh);
-       if (bytes_read != sizeof hdr) {
-               *err = file_error(fh, err_info);
-               if (*err == 0 && bytes_read != 0)
-                       *err = WTAP_ERR_SHORT_READ;
+       if (!wtap_read_bytes_or_eof(fh, &hdr, sizeof hdr, err, err_info))
                return FALSE;
-       }
 
        data_length = pletoh16(&hdr.data_length);
        if (data_length == 0) {
@@ -340,7 +302,13 @@ radcom_read_rec(wftap *wfth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
        }
        length = pletoh16(&hdr.length);
        real_length = pletoh16(&hdr.real_length);
+       /*
+        * The maximum value of length is 65535, which is less than
+        * WTAP_MAX_PACKET_SIZE_STANDARD will ever be, so we don't need to check
+        * it.
+        */
 
+       phdr->rec_type = REC_TYPE_PACKET;
        phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
 
        tm.tm_year = pletoh16(&hdr.date.year)-1900;
@@ -354,7 +322,7 @@ radcom_read_rec(wftap *wfth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
        phdr->ts.secs = mktime(&tm);
        phdr->ts.nsecs = pletoh32(&hdr.date.usec) * 1000;
 
-       switch (wfth->file_encap) {
+       switch (wth->file_encap) {
 
        case WTAP_ENCAP_ETHERNET:
                /* XXX - is there an FCS? */
@@ -373,7 +341,7 @@ radcom_read_rec(wftap *wfth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
                 * XXX - is this stuff a pseudo-header?
                 * The direction appears to be in the "hdr.dce" field.
                 */
-               if (!radcom_read_rec_data(wfth->fh, atmhdr, sizeof atmhdr, err,
+               if (!wtap_read_bytes(fh, atmhdr, sizeof atmhdr, err,
                    err_info))
                        return FALSE;   /* Read error */
                length -= 8;
@@ -393,20 +361,15 @@ radcom_read_rec(wftap *wfth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
        return TRUE;
 }
 
-static gboolean
-radcom_read_rec_data(FILE_T fh, guint8 *pd, int length, int *err,
-                    gchar **err_info)
-{
-       int     bytes_read;
-
-       errno = WTAP_ERR_CANT_READ;
-       bytes_read = file_read(pd, length, fh);
-
-       if (bytes_read != length) {
-               *err = file_error(fh, err_info);
-               if (*err == 0)
-                       *err = WTAP_ERR_SHORT_READ;
-               return FALSE;
-       }
-       return TRUE;
-}
+/*
+ * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
+ *
+ * Local variables:
+ * c-basic-offset: 8
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * End:
+ *
+ * vi: set shiftwidth=8 tabstop=8 noexpandtab:
+ * :indentSize=8:tabSize=8:noTabs=false:
+ */