aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--wiretap/5views.c29
-rw-r--r--wiretap/aethra.c25
-rw-r--r--wiretap/ber.c7
-rw-r--r--wiretap/btsnoop.c22
-rw-r--r--wiretap/camins.c33
-rw-r--r--wiretap/commview.c54
-rw-r--r--wiretap/csids.c28
-rw-r--r--wiretap/erf.c95
-rw-r--r--wiretap/eyesdn.c63
-rw-r--r--wiretap/hcidump.c22
-rw-r--r--wiretap/i4btrace.c17
-rw-r--r--wiretap/ipfix.c12
-rw-r--r--wiretap/iptrace.c48
-rw-r--r--wiretap/iseries.c14
-rw-r--r--wiretap/k12.c16
-rw-r--r--wiretap/lanalyzer.c86
-rw-r--r--wiretap/libpcap.c24
-rw-r--r--wiretap/logcat.c139
-rw-r--r--wiretap/mp2t.c17
-rw-r--r--wiretap/mpeg.c83
-rw-r--r--wiretap/netmon.c48
-rw-r--r--wiretap/netscaler.c81
-rw-r--r--wiretap/nettl.c101
-rw-r--r--wiretap/network_instruments.c85
-rw-r--r--wiretap/netxray.c33
-rw-r--r--wiretap/ngsniffer.c60
-rw-r--r--wiretap/packetlogger.c20
-rw-r--r--wiretap/pcap-common.c133
-rw-r--r--wiretap/pcapng.c146
-rw-r--r--wiretap/peekclassic.c42
-rw-r--r--wiretap/peektagged.c26
-rw-r--r--wiretap/pppdump.c17
-rw-r--r--wiretap/radcom.c180
-rw-r--r--wiretap/snoop.c73
-rw-r--r--wiretap/stanag4607.c41
-rw-r--r--wiretap/tnef.c8
-rw-r--r--wiretap/visual.c30
-rw-r--r--wiretap/vwr.c30
-rw-r--r--wiretap/wtap-int.h64
-rw-r--r--wiretap/wtap.c72
40 files changed, 787 insertions, 1337 deletions
diff --git a/wiretap/5views.c b/wiretap/5views.c
index 8576c726d8..a2f5364283 100644
--- a/wiretap/5views.c
+++ b/wiretap/5views.c
@@ -112,15 +112,13 @@ static gboolean _5views_dump_close(wtap_dumper *wdh, int *err);
int _5views_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
t_5VW_Capture_Header Capture_Header;
int encap = WTAP_ENCAP_UNKNOWN;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&Capture_Header.Info_Header, sizeof(t_5VW_Info_Header), wth->fh);
- if (bytes_read != sizeof(t_5VW_Info_Header)) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &Capture_Header.Info_Header,
+ sizeof(t_5VW_Info_Header), err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -171,13 +169,9 @@ int _5views_open(wtap *wth, int *err, gchar **err_info)
}
/* read the remaining header information */
- bytes_read = file_read(&Capture_Header.HeaderDateCreation, sizeof (t_5VW_Capture_Header) - sizeof(t_5VW_Info_Header), wth->fh);
- if (bytes_read != sizeof (t_5VW_Capture_Header)- sizeof(t_5VW_Info_Header) ) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &Capture_Header.HeaderDateCreation,
+ sizeof (t_5VW_Capture_Header) - sizeof(t_5VW_Info_Header), err, err_info))
return -1;
- }
/* This is a 5views capture file */
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_5VIEWS;
@@ -270,19 +264,10 @@ static gboolean
_5views_read_header(wtap *wth, FILE_T fh, t_5VW_TimeStamped_Header *hdr,
struct wtap_pkthdr *phdr, int *err, gchar **err_info)
{
- int bytes_read, bytes_to_read;
-
- bytes_to_read = sizeof(t_5VW_TimeStamped_Header);
-
/* Read record header. */
- bytes_read = file_read(hdr, bytes_to_read, fh);
- if (bytes_read != bytes_to_read) {
- *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, (unsigned int)sizeof(t_5VW_TimeStamped_Header),
+ err, err_info))
return FALSE;
- }
hdr->Key = pletoh32(&hdr->Key);
if (hdr->Key != CST_5VW_RECORDS_HEADER_KEY) {
diff --git a/wiretap/aethra.c b/wiretap/aethra.c
index 6eb37202b9..87e5515037 100644
--- a/wiretap/aethra.c
+++ b/wiretap/aethra.c
@@ -121,17 +121,15 @@ static gboolean aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hd
int aethra_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
struct aethra_hdr hdr;
struct tm tm;
aethra_t *aethra;
/* Read in the string that should be at the start of a "aethra" file */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(hdr.magic, sizeof hdr.magic, wth->fh);
- if (bytes_read != sizeof hdr.magic) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, hdr.magic, sizeof hdr.magic, err,
+ err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -141,14 +139,9 @@ int aethra_open(wtap *wth, int *err, gchar **err_info)
/* Read the rest of the header. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read((char *)&hdr + sizeof hdr.magic,
- sizeof hdr - sizeof hdr.magic, wth->fh);
- if (bytes_read != sizeof hdr - sizeof hdr.magic) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, (char *)&hdr + sizeof hdr.magic,
+ sizeof hdr - sizeof hdr.magic, err, err_info))
return -1;
- }
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_AETHRA;
aethra = (aethra_t *)g_malloc(sizeof(aethra_t));
wth->priv = (void *)aethra;
@@ -303,20 +296,14 @@ aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
struct wtap_pkthdr *phdr, int *err, gchar **err_info)
{
aethra_t *aethra = (aethra_t *)wth->priv;
- int bytes_read;
guint32 rec_size;
guint32 packet_size;
guint32 msecs;
/* Read record header. */
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;
- }
rec_size = pletoh16(hdr->rec_size);
if (rec_size < (sizeof *hdr - sizeof hdr->rec_size)) {
diff --git a/wiretap/ber.c b/wiretap/ber.c
index cb1829bda3..bde6234763 100644
--- a/wiretap/ber.c
+++ b/wiretap/ber.c
@@ -107,7 +107,6 @@ int ber_open(wtap *wth, int *err, gchar **err_info)
{
#define BER_BYTES_TO_CHECK 8
guint8 bytes[BER_BYTES_TO_CHECK];
- int bytes_read;
guint8 ber_id;
gint8 ber_class;
gint8 ber_tag;
@@ -117,10 +116,8 @@ int ber_open(wtap *wth, int *err, gchar **err_info)
gint64 file_size;
int offset = 0, i;
- bytes_read = file_read(&bytes, BER_BYTES_TO_CHECK, wth->fh);
- if (bytes_read != BER_BYTES_TO_CHECK) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &bytes, BER_BYTES_TO_CHECK, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
diff --git a/wiretap/btsnoop.c b/wiretap/btsnoop.c
index fa0075b847..0e28449b5a 100644
--- a/wiretap/btsnoop.c
+++ b/wiretap/btsnoop.c
@@ -82,7 +82,6 @@ static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
int btsnoop_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic[sizeof btsnoop_magic];
struct btsnoop_hdr hdr;
@@ -90,10 +89,8 @@ int btsnoop_open(wtap *wth, int *err, gchar **err_info)
/* Read in the string that should be at the start of a "btsnoop" file */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(magic, sizeof magic, wth->fh);
- if (bytes_read != sizeof magic) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -104,13 +101,8 @@ int btsnoop_open(wtap *wth, int *err, gchar **err_info)
/* Read the rest of the header. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
- if (bytes_read != sizeof hdr) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info))
return -1;
- }
/*
* Make sure it's a version we support.
@@ -181,7 +173,6 @@ static gboolean btsnoop_seek_read(wtap *wth, gint64 seek_off,
static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
{
- int bytes_read;
struct btsnooprec_hdr hdr;
guint32 packet_size;
guint32 flags;
@@ -191,13 +182,8 @@ static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
/* Read record header. */
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;
- }
packet_size = g_ntohl(hdr.incl_len);
orig_size = g_ntohl(hdr.orig_len);
diff --git a/wiretap/camins.c b/wiretap/camins.c
index d5aafe9fcc..82657f2e2b 100644
--- a/wiretap/camins.c
+++ b/wiretap/camins.c
@@ -107,26 +107,6 @@ typedef enum {
#define DVB_CI_PSEUDO_HDR_HOST_TO_CAM 0xFE
-/* read a block of data from the camins file and handle the errors */
-static gboolean
-read_block(FILE_T fh, guint8 *buf, guint16 buf_len, int *err, gchar **err_info)
-{
- int bytes_read;
-
- bytes_read = file_read((void *)buf, buf_len, fh);
- if (bytes_read != buf_len) {
- *err = file_error(fh, err_info);
- /* bytes_read==0 is end of file */
- if (bytes_read>0 && *err == 0) {
- *err = WTAP_ERR_SHORT_READ;
- }
- return FALSE;
- }
-
- return TRUE;
-}
-
-
/* find the transaction type for the data bytes of the next packet
and the number of data bytes in that packet
the fd is moved such that it can be used in a subsequent call
@@ -146,7 +126,7 @@ find_next_pkt_dat_type_len(FILE_T fh,
RESET_STAT_VALS;
do {
- if (read_block(fh, block, sizeof(block), err, err_info) == FALSE) {
+ if (!wtap_read_bytes_or_eof(fh, block, sizeof(block), err, err_info)) {
RESET_STAT_VALS;
return FALSE;
}
@@ -212,7 +192,7 @@ read_packet_data(FILE_T fh, guint8 dat_trans_type, guint8 *buf, guint16 dat_len,
p = buf;
while (bytes_count < dat_len) {
- if (read_block(fh, block, sizeof(block), err, err_info) == FALSE)
+ if (!wtap_read_bytes_or_eof(fh, block, sizeof(block), err, err_info))
break;
if (block[1] == dat_trans_type) {
@@ -324,14 +304,15 @@ int camins_open(wtap *wth, int *err, gchar **err_info _U_)
guint8 found_start_blocks = 0;
guint8 count = 0;
guint8 block[2];
- int bytes_read;
/* all CAM Inspector files I've looked at have at least two blocks of
0x00 0xE1 within the first 20 bytes */
do {
- bytes_read = file_read(block, sizeof(block), wth->fh);
- if (bytes_read != sizeof(block))
- break;
+ if (!wtap_read_bytes(wth->fh, block, sizeof(block), err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ)
+ break;
+ return -1;
+ }
if (block[0]==0x00 && block[1] == 0xE1)
found_start_blocks++;
diff --git a/wiretap/commview.c b/wiretap/commview.c
index e0c24326d1..b9154d965d 100644
--- a/wiretap/commview.c
+++ b/wiretap/commview.c
@@ -208,24 +208,42 @@ static gboolean
commview_read_header(commview_header_t *cv_hdr, FILE_T fh, int *err,
gchar **err_info)
{
- wtap_file_read_expected_bytes(&cv_hdr->data_len, 2, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->source_data_len, 2, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->version, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->year, 2, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->month, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->day, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->hours, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->minutes, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->seconds, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->usecs, 4, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->flags, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->signal_level_percent, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->rate, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->band, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->channel, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->direction, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->signal_level_dbm, 1, fh, err, err_info);
- wtap_file_read_expected_bytes(&cv_hdr->noise_level, 1, fh, err, err_info);
+ if (!wtap_read_bytes_or_eof(fh, &cv_hdr->data_len, 2, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->source_data_len, 2, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->version, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->year, 2, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->month, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->day, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->hours, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->minutes, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->seconds, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->usecs, 4, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->flags, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->signal_level_percent, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->rate, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->band, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->channel, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->direction, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->signal_level_dbm, 1, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &cv_hdr->noise_level, 1, err, err_info))
+ return FALSE;
/* Convert multi-byte values from little endian to host endian format */
cv_hdr->data_len = GUINT16_FROM_LE(cv_hdr->data_len);
diff --git a/wiretap/csids.c b/wiretap/csids.c
index e99e47b788..72f4f940a8 100644
--- a/wiretap/csids.c
+++ b/wiretap/csids.c
@@ -68,17 +68,15 @@ int csids_open(wtap *wth, int *err, gchar **err_info)
* this will byteswap it. I need to fix this. XXX --mlh
*/
- int tmp,iplen,bytesRead;
+ int tmp,iplen;
gboolean byteswap = FALSE;
struct csids_header hdr;
csids_t *csids;
/* check the file to make sure it is a csids file. */
- bytesRead = file_read( &hdr, sizeof( struct csids_header), wth->fh );
- if( bytesRead != sizeof( struct csids_header) ) {
- *err = file_error( wth->fh, err_info );
- if( *err != 0 && *err != WTAP_ERR_SHORT_READ ) {
+ if( !wtap_read_bytes( wth->fh, &hdr, sizeof( struct csids_header), err, err_info ) ) {
+ if( *err != WTAP_ERR_SHORT_READ ) {
return -1;
}
return 0;
@@ -88,18 +86,14 @@ int csids_open(wtap *wth, int *err, gchar **err_info)
}
hdr.seconds = pntoh32( &hdr.seconds );
hdr.caplen = pntoh16( &hdr.caplen );
- bytesRead = file_read( &tmp, 2, wth->fh );
- if( bytesRead != 2 ) {
- *err = file_error( wth->fh, err_info );
- if( *err != 0 && *err != WTAP_ERR_SHORT_READ ) {
+ if( !wtap_read_bytes( wth->fh, &tmp, 2, err, err_info ) ) {
+ if( *err != WTAP_ERR_SHORT_READ ) {
return -1;
}
return 0;
}
- bytesRead = file_read( &iplen, 2, wth->fh );
- if( bytesRead != 2 ) {
- *err = file_error( wth->fh, err_info );
- if( *err != 0 && *err != WTAP_ERR_SHORT_READ ) {
+ if( !wtap_read_bytes(wth->fh, &iplen, 2, err, err_info ) ) {
+ if( *err != WTAP_ERR_SHORT_READ ) {
return -1;
}
return 0;
@@ -182,16 +176,10 @@ csids_read_packet(FILE_T fh, csids_t *csids, struct wtap_pkthdr *phdr,
Buffer *buf, int *err, gchar **err_info)
{
struct csids_header hdr;
- int bytesRead = 0;
guint8 *pd;
- bytesRead = file_read( &hdr, sizeof( struct csids_header), fh );
- if( bytesRead != sizeof( struct csids_header) ) {
- *err = file_error( fh, err_info );
- if (*err == 0 && bytesRead != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if( !wtap_read_bytes_or_eof( fh, &hdr, sizeof( struct csids_header), err, err_info ) )
return FALSE;
- }
hdr.seconds = pntoh32(&hdr.seconds);
hdr.caplen = pntoh16(&hdr.caplen);
diff --git a/wiretap/erf.c b/wiretap/erf.c
index ede93d5a5b..9172e667dd 100644
--- a/wiretap/erf.c
+++ b/wiretap/erf.c
@@ -57,13 +57,13 @@
#include "atm.h"
#include "erf.h"
-static int erf_read_header(FILE_T fh,
- struct wtap_pkthdr *phdr,
- erf_header_t *erf_header,
- int *err,
- gchar **err_info,
- guint32 *bytes_read,
- guint32 *packet_size);
+static gboolean erf_read_header(FILE_T fh,
+ struct wtap_pkthdr *phdr,
+ erf_header_t *erf_header,
+ int *err,
+ gchar **err_info,
+ guint32 *bytes_read,
+ guint32 *packet_size);
static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
gint64 *data_offset);
static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
@@ -99,7 +99,7 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
guint16 rlen;
guint64 erf_ext_header;
guint8 type;
- size_t r;
+ gboolean r;
gchar * buffer;
memset(&prevts, 0, sizeof(prevts));
@@ -119,14 +119,12 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
for (i = 0; i < records_for_erf_check; i++) { /* records_for_erf_check */
- r = file_read(&header,sizeof(header),wth->fh);
-
- if (r == 0 ) break;
- if (r != sizeof(header)) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ) {
- return -1;
- } else {
+ if (!wtap_read_bytes_or_eof(wth->fh,&header,sizeof(header),err,err_info)) {
+ if (*err == 0) {
+ /* EOF - all records have been successfully checked */
+ break;
+ }
+ if (*err == WTAP_ERR_SHORT_READ) {
/* ERF header too short accept the file,
only if the very first records have been successfully checked */
if (i < MIN_RECORDS_FOR_ERF_CHECK) {
@@ -135,6 +133,8 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
/* BREAK, the last record is too short, and will be ignored */
break;
}
+ } else {
+ return -1;
}
}
@@ -192,10 +192,11 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
/* Read over the extension headers */
type = header.type;
while (type & 0x80){
- if (file_read(&erf_ext_header, sizeof(erf_ext_header),wth->fh) != sizeof(erf_ext_header)) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh,&erf_ext_header,sizeof(erf_ext_header),err,err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /* Extension header missing, not an ERF file */
+ return 0;
+ }
return -1;
}
packet_size -= (guint32)sizeof(erf_ext_header);
@@ -213,8 +214,11 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
case ERF_TYPE_MC_AAL2:
case ERF_TYPE_COLOR_MC_HDLC_POS:
case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */
- if (file_read(&mc_hdr,sizeof(mc_hdr),wth->fh) != sizeof(mc_hdr)) {
- *err = file_error(wth->fh, err_info);
+ if (!wtap_read_bytes(wth->fh,&mc_hdr,sizeof(mc_hdr),err,err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /* Subheader missing, not an ERF file */
+ return 0;
+ }
return -1;
}
packet_size -= (guint32)sizeof(mc_hdr);
@@ -222,8 +226,11 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
case ERF_TYPE_ETH:
case ERF_TYPE_COLOR_ETH:
case ERF_TYPE_DSM_COLOR_ETH:
- if (file_read(&eth_hdr,sizeof(eth_hdr),wth->fh) != sizeof(eth_hdr)) {
- *err = file_error(wth->fh, err_info);
+ if (!wtap_read_bytes(wth->fh,&eth_hdr,sizeof(eth_hdr),err,err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /* Subheader missing, not an ERF file */
+ return 0;
+ }
return -1;
}
packet_size -= (guint32)sizeof(eth_hdr);
@@ -242,10 +249,14 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
return 0;
}
buffer=(gchar *)g_malloc(packet_size);
- r = file_read(buffer, packet_size, wth->fh);
+ r = wtap_read_bytes(wth->fh, buffer, packet_size, err, err_info);
g_free(buffer);
- if (r != packet_size) {
+ if (!r) {
+ if (*err != WTAP_ERR_SHORT_READ) {
+ /* A real error */
+ return -1;
+ }
/* ERF record too short, accept the file,
only if the very first records have been successfully checked */
if (i < MIN_RECORDS_FOR_ERF_CHECK) {
@@ -324,13 +335,13 @@ static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
err, err_info);
}
-static int erf_read_header(FILE_T fh,
- struct wtap_pkthdr *phdr,
- erf_header_t *erf_header,
- int *err,
- gchar **err_info,
- guint32 *bytes_read,
- guint32 *packet_size)
+static gboolean erf_read_header(FILE_T fh,
+ struct wtap_pkthdr *phdr,
+ erf_header_t *erf_header,
+ int *err,
+ gchar **err_info,
+ guint32 *bytes_read,
+ guint32 *packet_size)
{
union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
guint32 mc_hdr;
@@ -342,8 +353,9 @@ static int erf_read_header(FILE_T fh,
int i = 0;
int max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
- wtap_file_read_expected_bytes(erf_header, sizeof(*erf_header), fh, err,
- err_info);
+ if (!wtap_read_bytes_or_eof(fh, erf_header, sizeof(*erf_header), err, err_info)) {
+ return FALSE;
+ }
if (bytes_read != NULL) {
*bytes_read = sizeof(*erf_header);
}
@@ -399,8 +411,9 @@ static int erf_read_header(FILE_T fh,
/* Copy the ERF extension header into the pseudo header */
type = erf_header->type;
while (type & 0x80){
- wtap_file_read_expected_bytes(&erf_exhdr, sizeof(erf_exhdr), fh, err,
- err_info);
+ if (!wtap_read_bytes(fh, &erf_exhdr, sizeof(erf_exhdr),
+ err, err_info))
+ return FALSE;
if (bytes_read != NULL)
*bytes_read += (guint32)sizeof(erf_exhdr);
*packet_size -= (guint32)sizeof(erf_exhdr);
@@ -437,8 +450,8 @@ static int erf_read_header(FILE_T fh,
case ERF_TYPE_ETH:
case ERF_TYPE_COLOR_ETH:
case ERF_TYPE_DSM_COLOR_ETH:
- wtap_file_read_expected_bytes(&eth_hdr, sizeof(eth_hdr), fh, err,
- err_info);
+ if (!wtap_read_bytes(fh, &eth_hdr, sizeof(eth_hdr), err, err_info))
+ return FALSE;
if (bytes_read != NULL)
*bytes_read += (guint32)sizeof(eth_hdr);
*packet_size -= (guint32)sizeof(eth_hdr);
@@ -454,8 +467,8 @@ static int erf_read_header(FILE_T fh,
case ERF_TYPE_MC_AAL2:
case ERF_TYPE_COLOR_MC_HDLC_POS:
case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */
- wtap_file_read_expected_bytes(&mc_hdr, sizeof(mc_hdr), fh, err,
- err_info);
+ if (!wtap_read_bytes(fh, &mc_hdr, sizeof(mc_hdr), err, err_info))
+ return FALSE;
if (bytes_read != NULL)
*bytes_read += (guint32)sizeof(mc_hdr);
*packet_size -= (guint32)sizeof(mc_hdr);
diff --git a/wiretap/eyesdn.c b/wiretap/eyesdn.c
index 724400a1c8..0c024e9c76 100644
--- a/wiretap/eyesdn.c
+++ b/wiretap/eyesdn.c
@@ -53,28 +53,42 @@
*/
-static int esc_read(guint8 *buf, int len, FILE_T fh)
+static gboolean esc_read(FILE_T fh, guint8 *buf, int len, int *err, gchar **err_info)
{
int i;
int value;
for(i=0; i<len; i++) {
value=file_getc(fh);
- if(value==-1)
- return -2; /* EOF or error */
- if(value==0xff)
- return -1; /* error !!, read into next frame */
+ if(value==-1) {
+ /* EOF or error */
+ *err=file_error(fh, err_info);
+ if(*err==0)
+ *err=WTAP_ERR_SHORT_READ;
+ return FALSE;
+ }
+ if(value==0xff) {
+ /* error !!, read into next frame */
+ *err=WTAP_ERR_BAD_FILE;
+ *err_info=g_strdup("eyesdn: No flag character seen in frame");
+ return FALSE;
+ }
if(value==0xfe) {
/* we need to escape */
value=file_getc(fh);
- if(value==-1)
- return -2;
+ if(value==-1) {
+ /* EOF or error */
+ *err=file_error(fh, err_info);
+ if(*err==0)
+ *err=WTAP_ERR_SHORT_READ;
+ return FALSE;
+ }
value+=2;
}
buf[i]=value;
}
- return i;
+ return TRUE;
}
/* Magic text to check for eyesdn-ness of file */
@@ -123,15 +137,12 @@ static gint64 eyesdn_seek_next_packet(wtap *wth, int *err, gchar **err_info)
int eyesdn_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic[EYESDN_HDR_MAGIC_SIZE];
/* Look for eyesdn header */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&magic, sizeof magic, wth->fh);
- if (bytes_read != sizeof magic) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -187,19 +198,14 @@ read_eyesdn_rec(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err,
int usecs;
int pkt_len;
guint8 channel, direction;
- int bytes_read;
guint8 *pd;
/* Our file pointer should be at the summary information header
* for a packet. Read in that header and extract the useful
* information.
*/
- if (esc_read(hdr, EYESDN_HDR_LENGTH, fh) != EYESDN_HDR_LENGTH) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!esc_read(fh, hdr, EYESDN_HDR_LENGTH, err, err_info))
return FALSE;
- }
/* extract information from header */
usecs = pntoh24(&hdr[0]);
@@ -255,12 +261,8 @@ read_eyesdn_rec(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err,
}
cur_off = file_tell(fh);
- if (esc_read(cell, CELL_LEN, fh) != CELL_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!esc_read(fh, cell, CELL_LEN, err, err_info))
return FALSE;
- }
if (file_seek(fh, cur_off, SEEK_SET, err) == -1)
return FALSE;
phdr->pkt_encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
@@ -325,19 +327,8 @@ read_eyesdn_rec(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf, int *err,
errno = WTAP_ERR_CANT_READ;
pd = ws_buffer_start_ptr(buf);
- bytes_read = esc_read(pd, pkt_len, fh);
- if (bytes_read != pkt_len) {
- if (bytes_read == -2) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
- } else if (bytes_read == -1) {
- *err = WTAP_ERR_BAD_FILE;
- *err_info = g_strdup("eyesdn: No flag character seen in frame");
- } else
- *err = WTAP_ERR_SHORT_READ;
+ if (!esc_read(fh, pd, pkt_len, err, err_info))
return FALSE;
- }
return TRUE;
}
diff --git a/wiretap/hcidump.c b/wiretap/hcidump.c
index 1a508968d9..d4a6d2fc60 100644
--- a/wiretap/hcidump.c
+++ b/wiretap/hcidump.c
@@ -38,15 +38,10 @@ static gboolean hcidump_process_packet(FILE_T fh, struct wtap_pkthdr *phdr,
Buffer *buf, int *err, gchar **err_info)
{
struct dump_hdr dh;
- int bytes_read, packet_size;
+ int packet_size;
- bytes_read = file_read(&dh, DUMP_HDR_SIZE, fh);
- if (bytes_read != DUMP_HDR_SIZE) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes_or_eof(fh, &dh, DUMP_HDR_SIZE, err, err_info))
return FALSE;
- }
packet_size = GUINT16_FROM_LE(dh.len);
if (packet_size > WTAP_MAX_PACKET_SIZE) {
@@ -94,12 +89,9 @@ int hcidump_open(wtap *wth, int *err, gchar **err_info)
{
struct dump_hdr dh;
guint8 type;
- int bytes_read;
- bytes_read = file_read(&dh, DUMP_HDR_SIZE, wth->fh);
- if (bytes_read != DUMP_HDR_SIZE) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &dh, DUMP_HDR_SIZE, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -108,10 +100,8 @@ int hcidump_open(wtap *wth, int *err, gchar **err_info)
|| GUINT16_FROM_LE(dh.len) < 1)
return 0;
- bytes_read = file_read(&type, 1, wth->fh);
- if (bytes_read != 1) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &type, 1, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
diff --git a/wiretap/i4btrace.c b/wiretap/i4btrace.c
index d7fe2c7067..4f7f241b42 100644
--- a/wiretap/i4btrace.c
+++ b/wiretap/i4btrace.c
@@ -50,17 +50,14 @@ static int i4b_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
int i4btrace_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
i4b_trace_hdr_t hdr;
gboolean byte_swapped = FALSE;
i4btrace_t *i4btrace;
/* I4B trace files have no magic in the header... Sigh */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&hdr, sizeof(hdr), wth->fh);
- if (bytes_read != sizeof(hdr)) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &hdr, sizeof(hdr), err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -143,19 +140,11 @@ i4b_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
{
i4btrace_t *i4btrace = (i4btrace_t *)wth->priv;
i4b_trace_hdr_t hdr;
- int bytes_read;
guint32 length;
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) {
- /* Read something, but not enough */
- *err = WTAP_ERR_SHORT_READ;
- }
+ if (!wtap_read_bytes_or_eof(fh, &hdr, sizeof hdr, err, err_info))
return FALSE;
- }
if (i4btrace->byte_swapped) {
/*
diff --git a/wiretap/ipfix.c b/wiretap/ipfix.c
index 00466bea86..60adf9d6d5 100644
--- a/wiretap/ipfix.c
+++ b/wiretap/ipfix.c
@@ -123,7 +123,8 @@ typedef struct ipfix_set_header_s {
static gboolean
ipfix_read_message_header(ipfix_message_header_t *pfx_hdr, FILE_T fh, int *err, gchar **err_info)
{
- wtap_file_read_expected_bytes(pfx_hdr, IPFIX_MSG_HDR_SIZE, fh, err, err_info); /* macro which does a return if read fails */
+ if (!wtap_read_bytes_or_eof(fh, pfx_hdr, IPFIX_MSG_HDR_SIZE, err, err_info))
+ return FALSE;
/* fix endianess, because IPFIX files are always big-endian */
pfx_hdr->version = g_ntohs(pfx_hdr->version);
@@ -242,12 +243,9 @@ ipfix_open(wtap *wth, int *err, gchar **err_info)
/* check each Set in IPFIX Message for sanity */
while (checked_len < msg_hdr.message_length) {
- int bytes_read;
-
- bytes_read = file_read(&set_hdr, IPFIX_SET_HDR_SIZE, wth->fh);
- if (bytes_read != IPFIX_SET_HDR_SIZE) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0 || *err == WTAP_ERR_SHORT_READ) {
+ if (!wtap_read_bytes(wth->fh, &set_hdr, IPFIX_SET_HDR_SIZE,
+ err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
/* Not a valid IPFIX Set, so not an IPFIX file. */
ipfix_debug1("ipfix_open: error %d reading set", *err);
return 0;
diff --git a/wiretap/iptrace.c b/wiretap/iptrace.c
index 71ea09a0ee..caa13c0698 100644
--- a/wiretap/iptrace.c
+++ b/wiretap/iptrace.c
@@ -41,28 +41,25 @@ static gboolean iptrace_read_2_0(wtap *wth, int *err, gchar **err_info,
static gboolean iptrace_seek_read_2_0(wtap *wth, gint64 seek_off,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
-static int iptrace_read_rec_header(FILE_T fh, guint8 *header, int header_len,
- int *err, gchar **err_info);
static gboolean iptrace_read_rec_data(FILE_T fh, Buffer *buf,
struct wtap_pkthdr *phdr, int *err, gchar **err_info);
static void fill_in_pseudo_header(int encap,
union wtap_pseudo_header *pseudo_header, guint8 *header);
static int wtap_encap_ift(unsigned int ift);
+#define NAME_SIZE 11
+
int iptrace_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
- char name[12];
+ char name[NAME_SIZE+1];
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(name, 11, wth->fh);
- if (bytes_read != 11) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, name, NAME_SIZE, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
- name[11] = '\0';
+ name[NAME_SIZE] = '\0';
if (strcmp(name, "iptrace 1.0") == 0) {
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_IPTRACE_1_0;
@@ -123,13 +120,11 @@ iptrace_read_rec_1_0(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
int *err, gchar **err_info)
{
guint8 header[IPTRACE_1_0_PHDR_SIZE];
- int ret;
iptrace_1_0_phdr pkt_hdr;
guint32 packet_size;
- ret = iptrace_read_rec_header(fh, header, IPTRACE_1_0_PHDR_SIZE,
- err, err_info);
- if (ret <= 0) {
+ errno = WTAP_ERR_CANT_READ;
+ if (!wtap_read_bytes_or_eof(fh, header, sizeof header, err, err_info)) {
/* Read error or EOF */
return FALSE;
}
@@ -301,13 +296,11 @@ iptrace_read_rec_2_0(FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
int *err, gchar **err_info)
{
guint8 header[IPTRACE_2_0_PHDR_SIZE];
- int ret;
iptrace_2_0_phdr pkt_hdr;
guint32 packet_size;
- ret = iptrace_read_rec_header(fh, header, IPTRACE_2_0_PHDR_SIZE,
- err, err_info);
- if (ret <= 0) {
+ errno = WTAP_ERR_CANT_READ;
+ if (!wtap_read_bytes_or_eof(fh, header, sizeof header, err, err_info)) {
/* Read error or EOF */
return FALSE;
}
@@ -454,27 +447,6 @@ static gboolean iptrace_seek_read_2_0(wtap *wth, gint64 seek_off,
return TRUE;
}
-static int
-iptrace_read_rec_header(FILE_T fh, guint8 *header, int header_len, int *err,
- gchar **err_info)
-{
- int bytes_read;
-
- errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(header, header_len, fh);
- if (bytes_read != header_len) {
- *err = file_error(fh, err_info);
- if (*err != 0)
- return -1;
- if (bytes_read != 0) {
- *err = WTAP_ERR_SHORT_READ;
- return -1;
- }
- return 0;
- }
- return 1;
-}
-
static gboolean
iptrace_read_rec_data(FILE_T fh, Buffer *buf, struct wtap_pkthdr *phdr,
int *err, gchar **err_info)
diff --git a/wiretap/iseries.c b/wiretap/iseries.c
index 09c4f6c30e..b3074ca493 100644
--- a/wiretap/iseries.c
+++ b/wiretap/iseries.c
@@ -197,7 +197,6 @@ static gboolean iseries_parse_hex_string (const char * ascii, guint8 * buf,
int
iseries_open (wtap * wth, int *err, gchar ** err_info)
{
- int bytes_read;
gint offset;
char magic[ISERIES_LINE_LENGTH];
char unicodemagic[] =
@@ -211,13 +210,14 @@ iseries_open (wtap * wth, int *err, gchar ** err_info)
* by scanning for it in the first line
*/
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read (&magic, sizeof magic, wth->fh);
- if (bytes_read != sizeof magic)
+ if (!wtap_read_bytes (wth->fh, &magic, sizeof magic, err, err_info))
{
- *err = file_error (wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
- return -1;
- return 0;
+ if (*err == WTAP_ERR_SHORT_READ)
+ {
+ /* Not enough bytes for a magic string, so not an iSeries trace */
+ return 0;
+ }
+ return -1;
}
/*
diff --git a/wiretap/k12.c b/wiretap/k12.c
index ca0e270454..20d7790d59 100644
--- a/wiretap/k12.c
+++ b/wiretap/k12.c
@@ -852,18 +852,16 @@ int k12_open(wtap *wth, int *err, gchar **err_info) {
K12_DBG(1,("k12_open: ENTER debug_level=%u",debug_level));
#endif
- if ( file_read(header_buffer,K12_FILE_HDR_LEN,wth->fh) != K12_FILE_HDR_LEN ) {
+ if ( !wtap_read_bytes(wth->fh,header_buffer,K12_FILE_HDR_LEN,err,err_info) ) {
K12_DBG(1,("k12_open: FILE HEADER TOO SHORT OR READ ERROR"));
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
- }
return 0;
- } else {
- if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) {
- K12_DBG(1,("k12_open: BAD MAGIC"));
- return 0;
- }
+ }
+
+ if ( memcmp(header_buffer,k12_file_magic,8) != 0 ) {
+ K12_DBG(1,("k12_open: BAD MAGIC"));
+ return 0;
}
offset = K12_FILE_HDR_LEN;
diff --git a/wiretap/lanalyzer.c b/wiretap/lanalyzer.c
index 5db1d2030c..7fdbd4610f 100644
--- a/wiretap/lanalyzer.c
+++ b/wiretap/lanalyzer.c
@@ -278,7 +278,6 @@ static gboolean lanalyzer_dump_close(wtap_dumper *wdh, int *err);
int lanalyzer_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
LA_RecordHeader rec_header;
char header_fixed[2];
char *comment;
@@ -291,10 +290,9 @@ int lanalyzer_open(wtap *wth, int *err, gchar **err_info)
lanalyzer_t *lanalyzer;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&rec_header, LA_RecordHeaderSize, wth->fh);
- if (bytes_read != LA_RecordHeaderSize) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &rec_header, LA_RecordHeaderSize,
+ err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -313,24 +311,32 @@ int lanalyzer_open(wtap *wth, int *err, gchar **err_info)
*/
return 0;
}
- bytes_read = file_read(&header_fixed, sizeof header_fixed, wth->fh);
- if (bytes_read != sizeof header_fixed) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
- return -1;
- return 0;
+ if (!wtap_read_bytes(wth->fh, &header_fixed, sizeof header_fixed,
+ err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for the fixed-length part of
+ * the header, so not a LANAlyzer file.
+ */
+ return 0;
+ }
+ return -1;
}
record_length -= sizeof header_fixed;
if (record_length != 0) {
/* Read the rest of the record as a comment. */
comment = (char *)g_malloc(record_length + 1);
- bytes_read = file_read(comment, record_length, wth->fh);
- if (bytes_read != record_length) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
- return -1;
- return 0;
+ if (!wtap_read_bytes(wth->fh, comment, record_length,
+ err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for the rest of the
+ * record, so not a LANAlyzer file.
+ */
+ return 0;
+ }
+ return -1;
}
comment[record_length] = '\0';
wth->shb_hdr.opt_comment = comment;
@@ -350,11 +356,15 @@ int lanalyzer_open(wtap *wth, int *err, gchar **err_info)
/* Read records until we find the start of packets */
while (1) {
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&rec_header, LA_RecordHeaderSize, wth->fh);
- if (bytes_read != LA_RecordHeaderSize) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes_or_eof(wth->fh, &rec_header,
+ LA_RecordHeaderSize, err, err_info)) {
+ if (*err == 0) {
+ /*
+ * End of file and no packets;
+ * accept this file.
+ */
+ return 1;
+ }
return -1;
}
@@ -366,14 +376,9 @@ int lanalyzer_open(wtap *wth, int *err, gchar **err_info)
/* Trace Summary Record */
case RT_Summary:
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(summary, sizeof summary,
- wth->fh);
- if (bytes_read != sizeof summary) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, summary,
+ sizeof summary, err, err_info))
return -1;
- }
/* Assume that the date of the creation of the trace file
* is the same date of the trace. Lanalyzer doesn't
@@ -442,7 +447,6 @@ int lanalyzer_open(wtap *wth, int *err, gchar **err_info)
static gboolean lanalyzer_read_trace_record(wtap *wth, FILE_T fh,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
{
- int bytes_read;
char LE_record_type[2];
char LE_record_length[2];
guint16 record_type, record_length;
@@ -456,21 +460,10 @@ static gboolean lanalyzer_read_trace_record(wtap *wth, FILE_T fh,
/* read the record type and length. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(LE_record_type, 2, fh);
- if (bytes_read != 2) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0) {
- *err = WTAP_ERR_SHORT_READ;
- }
+ if (!wtap_read_bytes_or_eof(fh, LE_record_type, 2, err, err_info))
return FALSE;
- }
- bytes_read = file_read(LE_record_length, 2, fh);
- if (bytes_read != 2) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, LE_record_length, 2, err, err_info))
return FALSE;
- }
record_type = pletoh16(LE_record_type);
record_length = pletoh16(LE_record_length);
@@ -499,13 +492,8 @@ static gboolean lanalyzer_read_trace_record(wtap *wth, FILE_T fh,
/* Read the descriptor data */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(descriptor, DESCRIPTOR_LEN, fh);
- if (bytes_read != DESCRIPTOR_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, descriptor, DESCRIPTOR_LEN, err, err_info))
return FALSE;
- }
true_size = pletoh16(&descriptor[4]);
packet_size = pletoh16(&descriptor[6]);
diff --git a/wiretap/libpcap.c b/wiretap/libpcap.c
index 431c899516..1b0e2b4571 100644
--- a/wiretap/libpcap.c
+++ b/wiretap/libpcap.c
@@ -73,7 +73,6 @@ static int libpcap_read_header(wtap *wth, FILE_T fh, int *err, gchar **err_info,
int libpcap_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
guint32 magic;
struct pcap_hdr hdr;
gboolean byte_swapped;
@@ -107,10 +106,8 @@ int libpcap_open(wtap *wth, int *err, gchar **err_info)
/* Read in the number that should be at the start of a "libpcap" file */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&magic, sizeof magic, wth->fh);
- if (bytes_read != sizeof magic) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -177,13 +174,8 @@ int libpcap_open(wtap *wth, int *err, gchar **err_info)
/* Read the rest of the header. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
- if (bytes_read != sizeof hdr) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info))
return -1;
- }
if (byte_swapped) {
/* Byte-swap the header fields about which we care. */
@@ -745,7 +737,7 @@ libpcap_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
static int libpcap_read_header(wtap *wth, FILE_T fh, int *err, gchar **err_info,
struct pcaprec_ss990915_hdr *hdr)
{
- int bytes_to_read, bytes_read;
+ int bytes_to_read;
guint32 temp;
libpcap_t *libpcap;
@@ -775,14 +767,8 @@ static int libpcap_read_header(wtap *wth, FILE_T fh, int *err, gchar **err_info,
g_assert_not_reached();
bytes_to_read = 0;
}
- bytes_read = file_read(hdr, bytes_to_read, fh);
- if (bytes_read != bytes_to_read) {
- *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, bytes_to_read, err, err_info))
return FALSE;
- }
libpcap = (libpcap_t *)wth->priv;
if (libpcap->byte_swapped) {
diff --git a/wiretap/logcat.c b/wiretap/logcat.c
index d8c603cfdd..a0c414496c 100644
--- a/wiretap/logcat.c
+++ b/wiretap/logcat.c
@@ -38,9 +38,16 @@ static gchar get_priority(const guint8 priority) {
return priorities[priority];
}
-static gint detect_version(wtap *wth, int *err, gchar **err_info)
+/*
+ * Returns:
+ *
+ * -2 if we get an EOF at the beginning;
+ * -1 on an I/O error;
+ * 0 if the record doesn't appear to be valid;
+ * 1-{max gint} as a version number if we got a valid record.
+ */
+static gint detect_version(FILE_T fh, int *err, gchar **err_info)
{
- gint bytes_read;
guint16 payload_length;
guint16 hdr_size;
guint16 read_sofar;
@@ -56,32 +63,42 @@ static gint detect_version(wtap *wth, int *err, gchar **err_info)
guint16 msg_len;
/* 16-bit payload length */
- bytes_read = file_read(&tmp, 2, wth->fh);
- if (bytes_read != 2) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes_or_eof(fh, &tmp, 2, err, err_info)) {
+ if (*err == 0) {
+ /*
+ * Got an EOF at the beginning.
+ */
+ return -2;
+ }
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for a packet, so not a logcat file.
+ */
+ return 0;
+ }
return -1;
}
payload_length = pletoh16(&tmp);
- /* 16-bit header length (or padding, equal to 0x0000) */
- bytes_read = file_read(&tmp, 2, wth->fh);
- if (bytes_read != 2) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
- return -1;
- }
- hdr_size = pletoh16(&tmp);
- read_sofar = 4;
-
/* must contain at least priority and two nulls as separator */
if (payload_length < 3)
- return -1;
+ return 0;
/* payload length may not exceed the maximum payload size */
if (payload_length > LOGGER_ENTRY_MAX_PAYLOAD)
+ return 0;
+
+ /* 16-bit header length (or padding, equal to 0x0000) */
+ if (!wtap_read_bytes(fh, &tmp, 2, err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for a packet, so not a logcat file.
+ */
+ return 0;
+ }
return -1;
+ }
+ hdr_size = pletoh16(&tmp);
+ read_sofar = 4;
/* ensure buffer is large enough for all versions */
buffer = (guint8 *) g_malloc(sizeof(*log_entry_v2) + payload_length);
@@ -102,16 +119,17 @@ static gint detect_version(wtap *wth, int *err, gchar **err_info)
continue;
}
- bytes_read = file_read(buffer + read_sofar, entry_len - read_sofar,
- wth->fh);
- if (bytes_read != entry_len - read_sofar) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
- /* short read, end of file? Whatever, this cannot be valid. */
- break;
+ if (!wtap_read_bytes(fh, buffer + read_sofar, entry_len - read_sofar, err, err_info)) {
+ g_free(buffer);
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for a packet, so not a logcat file.
+ */
+ return 0;
+ }
+ return -1;
}
- read_sofar += bytes_read;
+ read_sofar += entry_len - read_sofar;
/* A v2 msg has a 32-bit userid instead of v1 priority */
if (get_priority(msg_payload[0]) == '?')
@@ -134,8 +152,9 @@ static gint detect_version(wtap *wth, int *err, gchar **err_info)
return version;
}
+ /* No version number is valid */
g_free(buffer);
- return -1;
+ return 0;
}
gint logcat_exported_pdu_length(const guint8 *pd) {
@@ -161,18 +180,13 @@ gint logcat_exported_pdu_length(const guint8 *pd) {
static gboolean logcat_read_packet(struct logcat_phdr *logcat, FILE_T fh,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
{
- gint bytes_read;
gint packet_size;
guint16 payload_length;
guint tmp[2];
guint8 *pd;
struct logger_entry *log_entry;
- bytes_read = file_read(&tmp, 2, fh);
- if (bytes_read != 2) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes_or_eof(fh, &tmp, 2, err, err_info)) {
return FALSE;
}
payload_length = pletoh16(tmp);
@@ -193,11 +207,7 @@ static gboolean logcat_read_packet(struct logcat_phdr *logcat, FILE_T fh,
memcpy(pd, tmp, 2);
/* Read the rest of the packet. */
- bytes_read = file_read(pd + 2, packet_size - 2, fh);
- if (bytes_read != packet_size - 2) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, pd + 2, packet_size - 2, err, err_info)) {
return FALSE;
}
@@ -238,29 +248,48 @@ static gboolean logcat_seek_read(wtap *wth, gint64 seek_off,
return TRUE;
}
-int logcat_open(wtap *wth, int *err, gchar **err_info _U_)
+int logcat_open(wtap *wth, int *err, gchar **err_info)
{
- int local_err;
- gchar *local_err_info;
gint version;
gint tmp_version;
struct logcat_phdr *logcat;
/* check first 3 packets (or 2 or 1 if EOF) versions to check file format is correct */
- version = detect_version(wth, &local_err, &local_err_info);
- if (version <= 0)
- return 0;
-
- tmp_version = detect_version(wth, &local_err, &local_err_info);
- if (tmp_version < 0 && !file_eof(wth->fh)) {
- return 0;
- } else if (tmp_version > 0) {
- if (tmp_version != version)
+ version = detect_version(wth->fh, err, err_info); /* first packet */
+ if (version == -1)
+ return -1; /* I/O error */
+ if (version == 0)
+ return 0; /* not a logcat file */
+ if (version == -2)
+ return 0; /* empty file, so not any type of file */
+
+ tmp_version = detect_version(wth->fh, err, err_info); /* second packet */
+ if (tmp_version == -1)
+ return -1; /* I/O error */
+ if (tmp_version == 0)
+ return 0; /* not a logcat file */
+ if (tmp_version != -2) {
+ /* we've read two packets; do they have the same version? */
+ if (tmp_version != version) {
+ /* no, so this is presumably not a logcat file */
return 0;
+ }
- tmp_version = detect_version(wth, &local_err, &local_err_info);
- if (tmp_version != version && !file_eof(wth->fh))
- return 0;
+ tmp_version = detect_version(wth->fh, err, err_info); /* third packet */
+ if (tmp_version < 0)
+ return -1; /* I/O error */
+ if (tmp_version == 0)
+ return 0; /* not a logcat file */
+ if (tmp_version != -2) {
+ /*
+ * we've read three packets and the first two have the same
+ * version; does the third have the same version?
+ */
+ if (tmp_version != version) {
+ /* no, so this is presumably not a logcat file */
+ return 0;
+ }
+ }
}
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
diff --git a/wiretap/mp2t.c b/wiretap/mp2t.c
index faf6b93598..324b1dfc2e 100644
--- a/wiretap/mp2t.c
+++ b/wiretap/mp2t.c
@@ -65,19 +65,11 @@ mp2t_read_packet(mp2t_filetype_t *mp2t, FILE_T fh, gint64 offset,
gchar **err_info)
{
guint64 tmp;
- int bytes_read;
ws_buffer_assure_space(buf, MP2T_SIZE);
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(ws_buffer_start_ptr(buf), MP2T_SIZE, fh);
- if (MP2T_SIZE != bytes_read) {
- *err = file_error(fh, err_info);
- /* bytes_read==0 is end of file, not a short read */
- if (bytes_read>0 && *err == 0) {
- *err = WTAP_ERR_SHORT_READ;
- }
+ if (!wtap_read_bytes_or_eof(fh, ws_buffer_start_ptr(buf), MP2T_SIZE, err, err_info))
return FALSE;
- }
phdr->rec_type = REC_TYPE_PACKET;
@@ -162,11 +154,8 @@ mp2t_open(wtap *wth, int *err, gchar **err_info)
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(buffer, MP2T_SIZE, wth->fh);
-
- if (MP2T_SIZE != bytes_read) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, buffer, MP2T_SIZE, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
diff --git a/wiretap/mpeg.c b/wiretap/mpeg.c
index 05ac414260..681e4baa50 100644
--- a/wiretap/mpeg.c
+++ b/wiretap/mpeg.c
@@ -70,25 +70,6 @@ mpeg_resync(FILE_T fh, int *err)
return count;
}
-static int
-mpeg_read_header(FILE_T fh, int *err, gchar **err_info, guint32 *n)
-{
- int bytes_read;
-
- errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(n, sizeof *n, fh);
- if (bytes_read != sizeof *n) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
- return -1;
- }
- *n = g_ntohl(*n);
- if (file_seek(fh, -(gint64)(sizeof *n), SEEK_CUR, err) == -1)
- return -1;
- return bytes_read;
-}
-
#define SCRHZ 27000000
static gboolean
@@ -97,25 +78,35 @@ mpeg_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
{
mpeg_t *mpeg = (mpeg_t *)wth->priv;
guint32 n;
- int bytes_read;
unsigned int packet_size;
nstime_t ts = mpeg->now;
- bytes_read = mpeg_read_header(fh, err, err_info, &n);
- if (bytes_read == -1)
+ /*
+ * All packets have at least 4 bytes in them. Read the first
+ * 4 bytes and determine whether it's a PES packet or not
+ * based on that.
+ *
+ * XXX - can an MPEG file contain a mixture of PES and non-PES
+ * packets? If not, can we determine whether the packets will
+ * be PES packets or not based on the magic number (i.e., if the
+ * file begins with 0x00 0x00 0x01, it contains PES packets,
+ * otherwise it doesn't)?
+ */
+ errno = WTAP_ERR_CANT_READ;
+ if (!wtap_read_bytes_or_eof(fh, &n, sizeof n, err, err_info))
return FALSE;
+ if (file_seek(fh, -(gint64)(sizeof n), SEEK_CUR, err) == -1)
+ return FALSE;
+ n = g_ntohl(n);
if (PES_VALID(n)) {
gint64 offset = file_tell(fh);
guint8 stream;
- if (file_seek(fh, 3, SEEK_CUR, err) == -1)
+ if (!file_skip(fh, 3, err))
return FALSE;
- bytes_read = file_read(&stream, sizeof stream, fh);
- if (bytes_read != sizeof stream) {
- *err = file_error(fh, err_info);
+ if (!wtap_read_bytes(fh, &stream, sizeof stream, err, err_info))
return FALSE;
- }
if (stream == 0xba) {
guint32 pack1;
@@ -123,32 +114,19 @@ mpeg_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
guint64 pack;
guint8 stuffing;
- bytes_read = file_read(&pack1, sizeof pack1, fh);
- if (bytes_read != sizeof pack1) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &pack1, sizeof pack1, err, err_info))
return FALSE;
- }
- bytes_read = file_read(&pack0, sizeof pack0, fh);
- if (bytes_read != sizeof pack0) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &pack0, sizeof pack0, err, err_info))
return FALSE;
- }
pack = (guint64)g_ntohl(pack1) << 32 | g_ntohl(pack0);
switch (pack >> 62) {
case 1:
- if (file_seek(fh, 1, SEEK_CUR, err) == -1)
+ if (!file_skip(fh, 1, err))
return FALSE;
- bytes_read = file_read(&stuffing,
- sizeof stuffing, fh);
- if (bytes_read != sizeof stuffing) {
- *err = file_error(fh, err_info);
+ if (!wtap_read_bytes(fh, &stuffing,
+ sizeof stuffing, err, err_info))
return FALSE;
- }
stuffing &= 0x07;
packet_size = 14 + stuffing;
@@ -173,13 +151,8 @@ mpeg_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
}
} else {
guint16 length;
- bytes_read = file_read(&length, sizeof length, fh);
- if (bytes_read != sizeof length) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &length, sizeof length, err, err_info))
return FALSE;
- }
length = g_ntohs(length);
packet_size = 6 + length;
}
@@ -262,16 +235,14 @@ struct _mpeg_magic {
int
mpeg_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic_buf[16];
struct _mpeg_magic* m;
mpeg_t *mpeg;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(magic_buf, sizeof magic_buf, wth->fh);
- if (bytes_read != (int) sizeof magic_buf) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, magic_buf, sizeof magic_buf,
+ err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
diff --git a/wiretap/netmon.c b/wiretap/netmon.c
index f2e7e5a5c5..65da2d8805 100644
--- a/wiretap/netmon.c
+++ b/wiretap/netmon.c
@@ -188,7 +188,6 @@ static gboolean netmon_dump_close(wtap_dumper *wdh, int *err);
int netmon_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic[MAGIC_SIZE];
struct netmon_hdr hdr;
int file_type;
@@ -205,10 +204,8 @@ int netmon_open(wtap *wth, int *err, gchar **err_info)
/* Read in the string that should be at the start of a Network
* Monitor file */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(magic, MAGIC_SIZE, wth->fh);
- if (bytes_read != MAGIC_SIZE) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, magic, MAGIC_SIZE, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -220,13 +217,8 @@ int netmon_open(wtap *wth, int *err, gchar **err_info)
/* Read the rest of the header. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
- if (bytes_read != sizeof hdr) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info))
return -1;
- }
switch (hdr.ver_major) {
@@ -367,11 +359,8 @@ int netmon_open(wtap *wth, int *err, gchar **err_info)
return -1;
}
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(frame_table, frame_table_length, wth->fh);
- if ((guint32)bytes_read != frame_table_length) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, frame_table, frame_table_length,
+ err, err_info)) {
g_free(frame_table);
return -1;
}
@@ -474,7 +463,6 @@ netmon_process_record(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
struct netmonrec_1_x_hdr hdr_1_x;
struct netmonrec_2_x_hdr hdr_2_x;
} hdr;
- int bytes_read;
gint64 delta = 0; /* signed - frame times can be before the nominal start */
gint64 t;
time_t secs;
@@ -502,15 +490,8 @@ netmon_process_record(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
break;
}
errno = WTAP_ERR_CANT_READ;
-
- bytes_read = file_read(&hdr, hdr_size, fh);
- if (bytes_read != hdr_size) {
- *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, hdr_size, err, err_info))
return FAILURE;
- }
switch (netmon->version_major) {
@@ -680,14 +661,8 @@ netmon_process_record(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
}
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&trlr, trlr_size, fh);
- if (bytes_read != trlr_size) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0) {
- *err = WTAP_ERR_SHORT_READ;
- }
+ if (!wtap_read_bytes(fh, &trlr, trlr_size, err, err_info))
return FAILURE;
- }
network = pletoh16(trlr.trlr_2_1.network);
if ((network & 0xF000) == NETMON_NET_PCAP_BASE) {
@@ -899,17 +874,12 @@ netmon_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
int *err, gchar **err_info)
{
struct netmon_atm_hdr atm_phdr;
- int bytes_read;
guint16 vpi, vci;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&atm_phdr, sizeof (struct netmon_atm_hdr), fh);
- if (bytes_read != sizeof (struct netmon_atm_hdr)) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &atm_phdr, sizeof (struct netmon_atm_hdr),
+ err, err_info))
return FALSE;
- }
vpi = g_ntohs(atm_phdr.vpi);
vci = g_ntohs(atm_phdr.vci);
diff --git a/wiretap/netscaler.c b/wiretap/netscaler.c
index f6e4346e76..7e93378451 100644
--- a/wiretap/netscaler.c
+++ b/wiretap/netscaler.c
@@ -673,7 +673,6 @@ int nstrace_open(wtap *wth, int *err, gchar **err_info)
gint64 file_size;
gint32 page_size;
nstrace_t *nstrace;
- int bytes_read;
errno = WTAP_ERR_CANT_READ;
@@ -713,12 +712,10 @@ int nstrace_open(wtap *wth, int *err, gchar **err_info)
return 0;
}
- bytes_read = file_read(nstrace_buf, page_size, wth->fh);
- if (bytes_read != page_size)
+ if (!wtap_read_bytes(wth->fh, nstrace_buf, page_size, err, err_info))
{
- *err = file_error(wth->fh, err_info);
g_free(nstrace_buf);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -761,20 +758,17 @@ int nstrace_open(wtap *wth, int *err, gchar **err_info)
/* Reset the read pointer to start of the file. */
if ((file_seek(wth->fh, 0, SEEK_SET, err)) == -1)
{
- *err = file_error(wth->fh, err_info);
g_free(nstrace->pnstrace_buf);
g_free(nstrace);
- return 0;
+ return -1;
}
/* Read the first page of data */
- bytes_read = file_read(nstrace_buf, page_size, wth->fh);
- if (bytes_read != page_size)
+ if (!wtap_read_bytes(wth->fh, nstrace_buf, page_size, err, err_info))
{
- *err = file_error(wth->fh, err_info);
g_free(nstrace->pnstrace_buf);
g_free(nstrace);
- return 0;
+ return -1;
}
/* reset the buffer offset */
@@ -1342,7 +1336,6 @@ static gboolean nstrace_seek_read_v10(wtap *wth, gint64 seek_off,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
{
nspr_hd_v10_t hdr;
- int bytes_read;
guint record_length;
guint8 *pd;
unsigned int bytes_to_read;
@@ -1357,13 +1350,9 @@ static gboolean nstrace_seek_read_v10(wtap *wth, gint64 seek_off,
/*
** Read the record header.
*/
- bytes_read = file_read((void *)&hdr, sizeof hdr, wth->random_fh);
- if (bytes_read != sizeof hdr) {
- *err = file_error(wth->random_fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->random_fh, (void *)&hdr, sizeof hdr,
+ err, err_info))
return FALSE;
- }
/*
** Get the record length.
@@ -1378,13 +1367,9 @@ static gboolean nstrace_seek_read_v10(wtap *wth, gint64 seek_off,
memcpy(pd, (void *)&hdr, sizeof hdr);
if (record_length > sizeof hdr) {
bytes_to_read = (unsigned int)(record_length - sizeof hdr);
- bytes_read = file_read(pd + sizeof hdr, bytes_to_read, wth->random_fh);
- if (bytes_read < 0 || (unsigned int)bytes_read != bytes_to_read) {
- *err = file_error(wth->random_fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->random_fh, pd + sizeof hdr, bytes_to_read,
+ err, err_info))
return FALSE;
- }
}
/*
@@ -1433,7 +1418,6 @@ static gboolean nstrace_seek_read_v20(wtap *wth, gint64 seek_off,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
{
nspr_hd_v20_t hdr;
- int bytes_read;
guint record_length;
guint hdrlen;
guint8 *pd;
@@ -1447,26 +1431,17 @@ static gboolean nstrace_seek_read_v20(wtap *wth, gint64 seek_off,
/*
** Read the first 2 bytes of the record header.
*/
- bytes_read = file_read((void *)&hdr, 2, wth->random_fh);
- if (bytes_read != 2) {
- *err = file_error(wth->random_fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->random_fh, (void *)&hdr, 2, err, err_info))
return FALSE;
- }
hdrlen = 2;
/*
** Is there a third byte? If so, read it.
*/
if (hdr.phd_RecordSizeLow & NSPR_V20RECORDSIZE_2BYTES) {
- bytes_read = file_read((void *)&hdr.phd_RecordSizeHigh, 1, wth->random_fh);
- if (bytes_read != 1) {
- *err = file_error(wth->random_fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->random_fh, (void *)&hdr.phd_RecordSizeHigh, 1,
+ err, err_info))
return FALSE;
- }
hdrlen = 3;
}
@@ -1483,13 +1458,9 @@ static gboolean nstrace_seek_read_v20(wtap *wth, gint64 seek_off,
memcpy(pd, (void *)&hdr, hdrlen);
if (record_length > hdrlen) {
bytes_to_read = (unsigned int)(record_length - hdrlen);
- bytes_read = file_read(pd + hdrlen, bytes_to_read, wth->random_fh);
- if (bytes_read < 0 || (unsigned int)bytes_read != bytes_to_read) {
- *err = file_error(wth->random_fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->random_fh, pd + hdrlen, bytes_to_read,
+ err, err_info))
return FALSE;
- }
}
#define GENERATE_CASE_FULL(phdr,type,acttype) \
@@ -1549,7 +1520,6 @@ static gboolean nstrace_seek_read_v30(wtap *wth, gint64 seek_off,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
{
nspr_hd_v20_t hdr;
- int bytes_read;
guint record_length;
guint hdrlen;
guint8 *pd;
@@ -1562,26 +1532,17 @@ static gboolean nstrace_seek_read_v30(wtap *wth, gint64 seek_off,
/*
** Read the first 2 bytes of the record header.
*/
- bytes_read = file_read((void *)&hdr, 2, wth->random_fh);
- if (bytes_read != 2) {
- *err = file_error(wth->random_fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->random_fh, (void *)&hdr, 2, err, err_info))
return FALSE;
- }
hdrlen = 2;
/*
** Is there a third byte? If so, read it.
*/
if (hdr.phd_RecordSizeLow & NSPR_V20RECORDSIZE_2BYTES) {
- bytes_read = file_read((void *)&hdr.phd_RecordSizeHigh, 1, wth->random_fh);
- if (bytes_read != 1) {
- *err = file_error(wth->random_fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->random_fh, (void *)&hdr.phd_RecordSizeHigh, 1,
+ err, err_info))
return FALSE;
- }
hdrlen = 3;
}
@@ -1598,13 +1559,9 @@ static gboolean nstrace_seek_read_v30(wtap *wth, gint64 seek_off,
memcpy(pd, (void *)&hdr, hdrlen);
if (record_length > hdrlen) {
bytes_to_read = (unsigned int)(record_length - hdrlen);
- bytes_read = file_read(pd + hdrlen, bytes_to_read, wth->random_fh);
- if (bytes_read < 0 || (unsigned int)bytes_read != bytes_to_read) {
- *err = file_error(wth->random_fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->random_fh, pd + hdrlen, bytes_to_read,
+ err, err_info))
return FALSE;
- }
}
#define GENERATE_CASE_V30(phdr,type,acttype) \
diff --git a/wiretap/nettl.c b/wiretap/nettl.c
index 14863848b3..7e6d1ec97d 100644
--- a/wiretap/nettl.c
+++ b/wiretap/nettl.c
@@ -190,19 +190,16 @@ int nettl_open(wtap *wth, int *err, gchar **err_info)
struct nettl_file_hdr file_hdr;
guint16 dummy[2];
int subsys;
- int bytes_read;
nettl_t *nettl;
memset(&file_hdr, 0, sizeof(file_hdr));
/* Read in the string that should be at the start of a HP file */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(file_hdr.magic, MAGIC_SIZE, wth->fh);
- if (bytes_read != MAGIC_SIZE) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
- return -1;
- return 0;
+ if (!wtap_read_bytes(wth->fh, file_hdr.magic, MAGIC_SIZE, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
+ return -1;
+ return 0;
}
if (memcmp(file_hdr.magic, nettl_magic_hpux9, MAGIC_SIZE) &&
@@ -211,14 +208,9 @@ int nettl_open(wtap *wth, int *err, gchar **err_info)
}
/* Read the rest of the file header */
- bytes_read = file_read(file_hdr.file_name, FILE_HDR_SIZE - MAGIC_SIZE,
- wth->fh);
- if (bytes_read != FILE_HDR_SIZE - MAGIC_SIZE) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, file_hdr.file_name, FILE_HDR_SIZE - MAGIC_SIZE,
+ err, err_info))
return -1;
- }
/* This is an nettl file */
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_NETTL;
@@ -233,17 +225,12 @@ int nettl_open(wtap *wth, int *err, gchar **err_info)
wth->snapshot_length = 0; /* not available */
/* read the first header to take a guess at the file encap */
- bytes_read = file_read(dummy, 4, wth->fh);
- if (bytes_read != 4) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0) {
- return -1;
+ if (!wtap_read_bytes_or_eof(wth->fh, dummy, 4, err, err_info)) {
+ if (*err == 0) {
+ /* EOF, so no records */
+ return 0;
}
- if (bytes_read != 0) {
- *err = WTAP_ERR_SHORT_READ;
- return -1;
- }
- return 0;
+ return -1;
}
subsys = g_ntohs(dummy[1]);
@@ -343,7 +330,6 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
nettl_t *nettl = (nettl_t *)wth->priv;
gboolean fddihack = FALSE;
- int bytes_read;
struct nettlrec_hdr rec_hdr;
guint16 hdr_len;
struct nettlrec_ns_ls_drv_eth_hdr drv_eth_hdr;
@@ -354,16 +340,11 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
guint8 dummyc[16];
int bytes_to_read;
guint8 *pd;
- guint8 dummy[3];
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&rec_hdr.hdr_len, sizeof rec_hdr.hdr_len, fh);
- if (bytes_read != sizeof rec_hdr.hdr_len) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes_or_eof(fh, &rec_hdr.hdr_len, sizeof rec_hdr.hdr_len,
+ err, err_info))
return FALSE;
- }
hdr_len = g_ntohs(rec_hdr.hdr_len);
if (hdr_len < NETTL_REC_HDR_LEN) {
*err = WTAP_ERR_BAD_FILE;
@@ -371,13 +352,9 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
hdr_len);
return FALSE;
}
- bytes_read = file_read(&rec_hdr.subsys, NETTL_REC_HDR_LEN - 2, fh);
- if (bytes_read != NETTL_REC_HDR_LEN - 2) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &rec_hdr.subsys, NETTL_REC_HDR_LEN - 2,
+ err, err_info))
return FALSE;
- }
subsys = g_ntohs(rec_hdr.subsys);
hdr_len -= NETTL_REC_HDR_LEN;
if (file_seek(fh, hdr_len, SEEK_CUR, err) == -1)
@@ -465,13 +442,8 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
padlen = 0;
} else {
/* outbound appears to have variable padding */
- bytes_read = file_read(dummyc, 9, fh);
- if (bytes_read != 9) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, dummyc, 9, err, err_info))
return FALSE;
- }
/* padding is usually either a total 11 or 16 bytes??? */
padlen = (int)dummyc[8];
if (file_seek(fh, padlen, SEEK_CUR, err) == -1)
@@ -514,13 +486,9 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
we assume everything is. We will crash and burn for anything else */
/* for encapsulated 100baseT we do this */
phdr->pkt_encap = WTAP_ENCAP_NETTL_ETHERNET;
- bytes_read = file_read(&drv_eth_hdr, NS_LS_DRV_ETH_HDR_LEN, fh);
- if (bytes_read != NS_LS_DRV_ETH_HDR_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &drv_eth_hdr, NS_LS_DRV_ETH_HDR_LEN,
+ err, err_info))
return FALSE;
- }
length = pntoh16(&drv_eth_hdr.length);
caplen = pntoh16(&drv_eth_hdr.caplen);
@@ -624,14 +592,9 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
bytes_to_read = 15;
if (bytes_to_read > datalen)
bytes_to_read = datalen;
- bytes_read = file_read(pd, bytes_to_read, fh);
- if (bytes_read != bytes_to_read) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, pd, bytes_to_read, err, err_info))
return FALSE;
- }
- datalen -= bytes_read;
+ datalen -= bytes_to_read;
if (datalen == 0) {
/* There's nothing past the FC, dest, src, DSAP and SSAP */
return TRUE;
@@ -641,29 +604,21 @@ nettl_read_rec(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr, Buffer *buf,
bytes_to_read = 3;
if (bytes_to_read > datalen)
bytes_to_read = datalen;
- bytes_read = file_read(dummy, bytes_to_read, fh);
- if (bytes_read != bytes_to_read) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!file_skip(fh, bytes_to_read, err))
return FALSE;
- }
- datalen -= bytes_read;
+ datalen -= bytes_to_read;
if (datalen == 0) {
/* There's nothing past the FC, dest, src, DSAP, SSAP, and 3 bytes to eat */
return TRUE;
}
}
- bytes_read = file_read(pd + 15, datalen, fh);
- } else
- bytes_read = file_read(pd, datalen, fh);
-
- if (bytes_read != datalen) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
- return FALSE;
+ if (!wtap_read_bytes(fh, pd + 15, datalen, err, err_info))
+ return FALSE;
+ } else {
+ if (!wtap_read_bytes(fh, pd, datalen, err, err_info))
+ return FALSE;
}
+
return TRUE;
}
diff --git a/wiretap/network_instruments.c b/wiretap/network_instruments.c
index d803c76e77..1ecd43fb10 100644
--- a/wiretap/network_instruments.c
+++ b/wiretap/network_instruments.c
@@ -103,8 +103,9 @@ static int read_packet_header(FILE_T fh, union wtap_pseudo_header *pseudo_header
static gboolean process_packet_header(wtap *wth,
packet_entry_header *packet_header, struct wtap_pkthdr *phdr, int *err,
gchar **err_info);
-static int read_packet_data(FILE_T fh, int offset_to_frame, int current_offset_from_packet_header,
- Buffer *buf, int length, int *err, char **err_info);
+static int read_packet_data(FILE_T fh, int offset_to_frame,
+ int current_offset_from_packet_header, Buffer *buf, int length,
+ int *err, char **err_info);
static gboolean skip_to_next_packet(wtap *wth, int offset_to_next_packet,
int current_offset_from_packet_header, int *err, char **err_info);
static gboolean observer_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
@@ -114,7 +115,6 @@ static gint wtap_to_observer_encap(int wtap_encap);
int network_instruments_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
int offset;
capture_file_header file_header;
guint i;
@@ -128,14 +128,13 @@ int network_instruments_open(wtap *wth, int *err, gchar **err_info)
offset = 0;
/* read in the buffer file header */
- bytes_read = file_read(&file_header, sizeof file_header, wth->fh);
- if (bytes_read != sizeof file_header) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &file_header, sizeof file_header,
+ err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
- offset += bytes_read;
+ offset += (int)sizeof file_header;
CAPTURE_FILE_HEADER_FROM_LE_IN_PLACE(file_header);
/* check if version info is present */
@@ -159,14 +158,9 @@ int network_instruments_open(wtap *wth, int *err, gchar **err_info)
break;
/* read the TLV header */
- bytes_read = file_read(&tlvh, sizeof tlvh, wth->fh);
- if (bytes_read != sizeof tlvh) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &tlvh, sizeof tlvh, err, err_info))
return -1;
- }
- offset += bytes_read;
+ offset += (int)sizeof tlvh;
TLV_HEADER_FROM_LE_IN_PLACE(tlvh);
if (tlvh.length < sizeof tlvh) {
@@ -179,15 +173,12 @@ int network_instruments_open(wtap *wth, int *err, gchar **err_info)
/* process (or skip over) the current TLV */
switch (tlvh.type) {
case INFORMATION_TYPE_TIME_INFO:
- bytes_read = file_read(&private_state->time_format, sizeof private_state->time_format, wth->fh);
- if (bytes_read != sizeof private_state->time_format) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &private_state->time_format,
+ sizeof private_state->time_format,
+ err, err_info))
return -1;
- }
private_state->time_format = GUINT32_FROM_LE(private_state->time_format);
- offset += bytes_read;
+ offset += (int)sizeof private_state->time_format;
break;
default:
seek_increment = tlvh.length - (int)sizeof tlvh;
@@ -213,13 +204,9 @@ int network_instruments_open(wtap *wth, int *err, gchar **err_info)
}
/* pull off the packet header */
- bytes_read = file_read(&packet_header, sizeof packet_header, wth->fh);
- if (bytes_read != sizeof packet_header) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &packet_header, sizeof packet_header,
+ err, err_info))
return -1;
- }
PACKET_ENTRY_HEADER_FROM_LE_IN_PLACE(packet_header);
/* check the packet's magic number */
@@ -341,7 +328,6 @@ read_packet_header(FILE_T fh, union wtap_pseudo_header *pseudo_header,
packet_entry_header *packet_header, int *err, gchar **err_info)
{
int offset;
- int bytes_read;
guint i;
tlv_header tlvh;
int seek_increment;
@@ -350,14 +336,13 @@ read_packet_header(FILE_T fh, union wtap_pseudo_header *pseudo_header,
offset = 0;
/* pull off the packet header */
- bytes_read = file_read(packet_header, sizeof *packet_header, fh);
- if (bytes_read != sizeof *packet_header) {
- *err = file_error(fh, err_info);
- if (*err != 0)
- return -1;
- return 0; /* EOF */
+ if (!wtap_read_bytes_or_eof(fh, packet_header, sizeof *packet_header,
+ err, err_info)) {
+ if (*err == 0)
+ return 0; /* EOF */
+ return -1;
}
- offset += bytes_read;
+ offset += (int)sizeof *packet_header;
PACKET_ENTRY_HEADER_FROM_LE_IN_PLACE(*packet_header);
/* check the packet's magic number */
@@ -388,14 +373,9 @@ read_packet_header(FILE_T fh, union wtap_pseudo_header *pseudo_header,
/* process extra information */
for (i = 0; i < packet_header->number_of_information_elements; i++) {
/* read the TLV header */
- bytes_read = file_read(&tlvh, sizeof tlvh, fh);
- if (bytes_read != sizeof tlvh) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &tlvh, sizeof tlvh, err, err_info))
return -1;
- }
- offset += bytes_read;
+ offset += (int)sizeof tlvh;
TLV_HEADER_FROM_LE_IN_PLACE(tlvh);
if (tlvh.length < sizeof tlvh) {
@@ -408,13 +388,9 @@ read_packet_header(FILE_T fh, union wtap_pseudo_header *pseudo_header,
/* process (or skip over) the current TLV */
switch (tlvh.type) {
case INFORMATION_TYPE_WIRELESS:
- bytes_read = file_read(&wireless_header, sizeof wireless_header, fh);
- if (bytes_read != sizeof wireless_header) {
- *err = file_error(fh, err_info);
- if(*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &wireless_header, sizeof wireless_header,
+ err, err_info))
return -1;
- }
/* update the pseudo header */
pseudo_header->ieee_802_11.fcs_len = 0;
/* set decryption status */
@@ -422,7 +398,7 @@ read_packet_header(FILE_T fh, union wtap_pseudo_header *pseudo_header,
pseudo_header->ieee_802_11.channel = wireless_header.frequency;
pseudo_header->ieee_802_11.data_rate = wireless_header.rate;
pseudo_header->ieee_802_11.signal_level = wireless_header.strengthPercent;
- offset += bytes_read;
+ offset += (int)sizeof wireless_header;
break;
default:
/* skip the TLV data */
@@ -522,8 +498,9 @@ process_packet_header(wtap *wth, packet_entry_header *packet_header,
}
static int
-read_packet_data(FILE_T fh, int offset_to_frame, int current_offset_from_packet_header, Buffer *buf,
- int length, int *err, char **err_info)
+read_packet_data(FILE_T fh, int offset_to_frame,
+ int current_offset_from_packet_header, Buffer *buf, int length,
+ int *err, char **err_info)
{
int seek_increment;
int bytes_consumed = 0;
@@ -557,8 +534,8 @@ read_packet_data(FILE_T fh, int offset_to_frame, int current_offset_from_packet_
}
static gboolean
-skip_to_next_packet(wtap *wth, int offset_to_next_packet, int current_offset_from_packet_header, int *err,
- char **err_info)
+skip_to_next_packet(wtap *wth, int offset_to_next_packet,
+ int current_offset_from_packet_header, int *err, char **err_info)
{
int seek_increment;
diff --git a/wiretap/netxray.c b/wiretap/netxray.c
index 5bb9732bde..419207ac5e 100644
--- a/wiretap/netxray.c
+++ b/wiretap/netxray.c
@@ -422,7 +422,6 @@ static gboolean netxray_dump_close_2_0(wtap_dumper *wdh, int *err);
int
netxray_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic[MAGIC_SIZE];
gboolean is_old;
struct netxray_hdr hdr;
@@ -462,10 +461,8 @@ netxray_open(wtap *wth, int *err, gchar **err_info)
/* Read in the string that should be at the start of a NetXRay
* file */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(magic, MAGIC_SIZE, wth->fh);
- if (bytes_read != MAGIC_SIZE) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, magic, MAGIC_SIZE, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -480,13 +477,8 @@ netxray_open(wtap *wth, int *err, gchar **err_info)
/* Read the rest of the header. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
- if (bytes_read != sizeof hdr) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info))
return -1;
- }
if (is_old) {
version_major = 0;
@@ -1127,7 +1119,6 @@ netxray_process_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
{
netxray_t *netxray = (netxray_t *)wth->priv;
union netxrayrec_hdr hdr;
- int bytes_read;
int hdr_size = 0;
double t;
int packet_size;
@@ -1149,19 +1140,13 @@ netxray_process_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
break;
}
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read((void *)&hdr, hdr_size, fh);
- if (bytes_read != hdr_size) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0)
- return -1;
- if (bytes_read != 0) {
- *err = WTAP_ERR_SHORT_READ;
- return -1;
- }
-
+ if (!wtap_read_bytes_or_eof(fh, (void *)&hdr, hdr_size, err, err_info)) {
/*
- * We're at EOF. "*err" is 0; we return -1 - that
- * combination tells our caller we're at EOF.
+ * If *err is 0, we're at EOF. *err being 0 and a return
+ * value of -1 tells our caller we're at EOF.
+ *
+ * Otherwise, we got an error, and *err *not* being 0
+ * and a return value tells our caller we have an error.
*/
return -1;
}
diff --git a/wiretap/ngsniffer.c b/wiretap/ngsniffer.c
index 2920fce384..9bc8f62c2d 100644
--- a/wiretap/ngsniffer.c
+++ b/wiretap/ngsniffer.c
@@ -544,7 +544,6 @@ static gboolean ng_file_seek_rand(wtap *wth, gint64 offset, int *err,
int
ngsniffer_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic[sizeof ngsniffer_magic];
char record_type[2];
char record_length[4]; /* only the first 2 bytes are length,
@@ -576,10 +575,8 @@ ngsniffer_open(wtap *wth, int *err, gchar **err_info)
/* Read in the string that should be at the start of a Sniffer file */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(magic, sizeof magic, wth->fh);
- if (bytes_read != sizeof magic) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -593,20 +590,10 @@ ngsniffer_open(wtap *wth, int *err, gchar **err_info)
* record.
*/
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(record_type, 2, wth->fh);
- if (bytes_read != 2) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, record_type, 2, err, err_info))
return -1;
- }
- bytes_read = file_read(record_length, 4, wth->fh);
- if (bytes_read != 4) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, record_length, 4, err, err_info))
return -1;
- }
type = pletoh16(record_type);
@@ -617,13 +604,8 @@ ngsniffer_open(wtap *wth, int *err, gchar **err_info)
}
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&version, sizeof version, wth->fh);
- if (bytes_read != sizeof version) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &version, sizeof version, err, err_info))
return -1;
- }
/* Check the data link type. */
if (version.network >= NUM_NGSNIFF_ENCAPS
@@ -798,7 +780,6 @@ static int
process_header_records(wtap *wth, int *err, gchar **err_info, gint16 maj_vers,
guint8 network)
{
- int bytes_read;
char record_type[2];
char record_length[4]; /* only the first 2 bytes are length,
the last 2 are "reserved" and are thrown away */
@@ -808,16 +789,10 @@ process_header_records(wtap *wth, int *err, gchar **err_info, gint16 maj_vers,
for (;;) {
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(record_type, 2, wth->fh);
- if (bytes_read != 2) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0)
- return -1;
- if (bytes_read != 0) {
- *err = WTAP_ERR_SHORT_READ;
- return -1;
- }
- return 0; /* EOF */
+ if (!wtap_read_bytes_or_eof(wth->fh, record_type, 2, err, err_info)) {
+ if (*err == 0)
+ return 0; /* EOF */
+ return -1;
}
type = pletoh16(record_type);
@@ -839,13 +814,9 @@ process_header_records(wtap *wth, int *err, gchar **err_info, gint16 maj_vers,
}
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(record_length, 4, wth->fh);
- if (bytes_read != 4) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, record_length, 4,
+ err, err_info))
return -1;
- }
length = pletoh16(record_length);
@@ -866,14 +837,9 @@ process_header_records(wtap *wth, int *err, gchar **err_info, gint16 maj_vers,
* record data.
*/
bytes_to_read = MIN(length, (int)sizeof buffer);
- bytes_read = file_read(buffer, bytes_to_read,
- wth->fh);
- if (bytes_read != bytes_to_read) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, buffer,
+ bytes_to_read, err, err_info))
return -1;
- }
switch (maj_vers) {
diff --git a/wiretap/packetlogger.c b/wiretap/packetlogger.c
index 065ad4aaed..8526cddada 100644
--- a/wiretap/packetlogger.c
+++ b/wiretap/packetlogger.c
@@ -66,11 +66,15 @@ int packetlogger_open(wtap *wth, int *err, gchar **err_info)
return 0;
}
- if (file_read(&type, 1, wth->fh) <= 0) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
- return -1;
- return 0;
+ if (!wtap_read_bytes(wth->fh, &type, 1, err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for the packet, so not a
+ * packetlogger file.
+ */
+ return 0;
+ }
+ return -1;
}
/* Verify this file belongs to us */
@@ -122,8 +126,10 @@ static gboolean
packetlogger_read_header(packetlogger_header_t *pl_hdr, FILE_T fh, int *err,
gchar **err_info)
{
- wtap_file_read_expected_bytes(&pl_hdr->len, 4, fh, err, err_info);
- wtap_file_read_expected_bytes(&pl_hdr->ts, 8, fh, err, err_info);
+ if (!wtap_read_bytes_or_eof(fh, &pl_hdr->len, 4, err, err_info))
+ return FALSE;
+ if (!wtap_read_bytes(fh, &pl_hdr->ts, 8, err, err_info))
+ return FALSE;
/* Convert multi-byte values from big endian to host endian */
pl_hdr->len = GUINT32_FROM_BE(pl_hdr->len);
diff --git a/wiretap/pcap-common.c b/wiretap/pcap-common.c
index d3e41a37e7..a34651334d 100644
--- a/wiretap/pcap-common.c
+++ b/wiretap/pcap-common.c
@@ -810,18 +810,12 @@ pcap_read_sunatm_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
guint8 atm_phdr[SUNATM_LEN];
- int bytes_read;
guint8 vpi;
guint16 vci;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(atm_phdr, SUNATM_LEN, fh);
- if (bytes_read != SUNATM_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, atm_phdr, SUNATM_LEN, err, err_info))
return FALSE;
- }
vpi = atm_phdr[SUNATM_VPI];
vci = pntoh16(&atm_phdr[SUNATM_VCI]);
@@ -898,18 +892,12 @@ pcap_read_nokiaatm_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
guint8 atm_phdr[NOKIAATM_LEN];
- int bytes_read;
guint8 vpi;
guint16 vci;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(atm_phdr, NOKIAATM_LEN, fh);
- if (bytes_read != NOKIAATM_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, atm_phdr, NOKIAATM_LEN, err, err_info))
return FALSE;
- }
vpi = atm_phdr[NOKIAATM_VPI];
vci = pntoh16(&atm_phdr[NOKIAATM_VCI]);
@@ -933,7 +921,6 @@ pcap_read_nokia_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
guint8 phdr[NOKIA_LEN];
- int bytes_read;
errno = WTAP_ERR_CANT_READ;
@@ -948,13 +935,8 @@ pcap_read_nokia_pseudoheader(FILE_T fh,
return FALSE;
}
- bytes_read = file_read(phdr, NOKIA_LEN, fh);
- if (bytes_read != NOKIA_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, phdr, NOKIA_LEN, err, err_info))
return FALSE;
- }
memcpy(pseudo_header->nokia.stuff, phdr, NOKIA_LEN);
@@ -966,16 +948,10 @@ pcap_read_irda_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
int *err, gchar **err_info)
{
guint8 irda_phdr[IRDA_SLL_LEN];
- int bytes_read;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(irda_phdr, IRDA_SLL_LEN, fh);
- if (bytes_read != IRDA_SLL_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, irda_phdr, IRDA_SLL_LEN, err, err_info))
return FALSE;
- }
if (pntoh16(&irda_phdr[IRDA_SLL_PROTOCOL_OFFSET]) != 0x0017) {
*err = WTAP_ERR_BAD_FILE;
@@ -993,16 +969,10 @@ static gboolean
pcap_read_mtp2_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
guint8 mtp2_hdr[MTP2_HDR_LEN];
- int bytes_read;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(mtp2_hdr, MTP2_HDR_LEN, fh);
- if (bytes_read != MTP2_HDR_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, mtp2_hdr, MTP2_HDR_LEN, err, err_info))
return FALSE;
- }
pseudo_header->mtp2.sent = mtp2_hdr[MTP2_SENT_OFFSET];
pseudo_header->mtp2.annex_a_used = mtp2_hdr[MTP2_ANNEX_A_USED_OFFSET];
@@ -1016,16 +986,10 @@ pcap_read_lapd_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
int *err, gchar **err_info)
{
guint8 lapd_phdr[LAPD_SLL_LEN];
- int bytes_read;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(lapd_phdr, LAPD_SLL_LEN, fh);
- if (bytes_read != LAPD_SLL_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, lapd_phdr, LAPD_SLL_LEN, err, err_info))
return FALSE;
- }
if (pntoh16(&lapd_phdr[LAPD_SLL_PROTOCOL_OFFSET]) != ETH_P_LAPD) {
*err = WTAP_ERR_BAD_FILE;
@@ -1044,16 +1008,10 @@ static gboolean
pcap_read_sita_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
guint8 sita_phdr[SITA_HDR_LEN];
- int bytes_read;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(sita_phdr, SITA_HDR_LEN, fh);
- if (bytes_read != SITA_HDR_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, sita_phdr, SITA_HDR_LEN, err, err_info))
return FALSE;
- }
pseudo_header->sita.sita_flags = sita_phdr[SITA_FLAGS_OFFSET];
pseudo_header->sita.sita_signals = sita_phdr[SITA_SIGNALS_OFFSET];
@@ -1386,18 +1344,12 @@ static gboolean
pcap_read_bt_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
- int bytes_read;
struct libpcap_bt_phdr phdr;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&phdr,
- sizeof (struct libpcap_bt_phdr), fh);
- if (bytes_read != sizeof (struct libpcap_bt_phdr)) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &phdr, sizeof (struct libpcap_bt_phdr),
+ err, err_info))
return FALSE;
- }
pseudo_header->p2p.sent = ((g_ntohl(phdr.direction) & LIBPCAP_BT_PHDR_RECV) == 0)? TRUE: FALSE;
return TRUE;
}
@@ -1406,18 +1358,12 @@ static gboolean
pcap_read_bt_monitor_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
- int bytes_read;
struct libpcap_bt_monitor_phdr phdr;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&phdr,
- sizeof (struct libpcap_bt_monitor_phdr), fh);
- if (bytes_read != sizeof (struct libpcap_bt_monitor_phdr)) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &phdr, sizeof (struct libpcap_bt_monitor_phdr),
+ err, err_info))
return FALSE;
- }
pseudo_header->btmon.adapter_id = g_ntohs(phdr.adapter_id);
pseudo_header->btmon.opcode = g_ntohs(phdr.opcode);
@@ -1428,17 +1374,11 @@ static gboolean
pcap_read_llcp_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
- int bytes_read;
guint8 phdr[LLCP_HEADER_LEN];
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(phdr, LLCP_HEADER_LEN, fh);
- if (bytes_read != LLCP_HEADER_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, phdr, LLCP_HEADER_LEN, err, err_info))
return FALSE;
- }
pseudo_header->llcp.adapter = phdr[LLCP_ADAPTER_OFFSET];
pseudo_header->llcp.flags = phdr[LLCP_FLAGS_OFFSET];
return TRUE;
@@ -1448,18 +1388,12 @@ static gboolean
pcap_read_ppp_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
- int bytes_read;
struct libpcap_ppp_phdr phdr;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&phdr,
- sizeof (struct libpcap_ppp_phdr), fh);
- if (bytes_read != sizeof (struct libpcap_ppp_phdr)) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &phdr, sizeof (struct libpcap_ppp_phdr),
+ err, err_info))
return FALSE;
- }
pseudo_header->p2p.sent = (phdr.direction == LIBPCAP_PPP_PHDR_SENT) ? TRUE: FALSE;
return TRUE;
}
@@ -1469,16 +1403,10 @@ pcap_read_erf_pseudoheader(FILE_T fh, struct wtap_pkthdr *whdr,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
guint8 erf_hdr[sizeof(struct erf_phdr)];
- int bytes_read;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(erf_hdr, sizeof(struct erf_phdr), fh);
- if (bytes_read != sizeof(struct erf_phdr)) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, erf_hdr, sizeof(struct erf_phdr), err, err_info))
return FALSE;
- }
pseudo_header->erf.phdr.ts = pletoh64(&erf_hdr[0]); /* timestamp */
pseudo_header->erf.phdr.type = erf_hdr[8];
pseudo_header->erf.phdr.flags = erf_hdr[9];
@@ -1511,7 +1439,6 @@ static gboolean
pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
int *err, gchar **err_info, guint * psize)
{
- int bytes_read = 0;
guint8 erf_exhdr[8];
guint64 erf_exhdr_sw;
int i = 0, max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
@@ -1520,13 +1447,8 @@ pcap_read_erf_exheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
if (pseudo_header->erf.phdr.type & 0x80){
do{
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(erf_exhdr, 8, fh);
- if (bytes_read != 8 ) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, erf_exhdr, 8, err, err_info))
return FALSE;
- }
type = erf_exhdr[0];
erf_exhdr_sw = pntoh64(erf_exhdr);
if (i < max)
@@ -1547,7 +1469,6 @@ pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
int *err, gchar **err_info, guint * psize)
{
guint8 erf_subhdr[sizeof(union erf_subhdr)];
- int bytes_read;
*psize=0;
switch(pseudo_header->erf.phdr.type & 0x7F) {
@@ -1560,13 +1481,8 @@ pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
case ERF_TYPE_COLOR_MC_HDLC_POS:
/* Extract the Multi Channel header to include it in the pseudo header part */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(erf_subhdr, sizeof(erf_mc_header_t), fh);
- if (bytes_read != sizeof(erf_mc_header_t) ) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, erf_subhdr, sizeof(erf_mc_header_t), err, err_info))
return FALSE;
- }
pseudo_header->erf.subhdr.mc_hdr = pntoh32(&erf_subhdr[0]);
*psize = sizeof(erf_mc_header_t);
break;
@@ -1575,13 +1491,8 @@ pcap_read_erf_subheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
case ERF_TYPE_DSM_COLOR_ETH:
/* Extract the Ethernet additional header to include it in the pseudo header part */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(erf_subhdr, sizeof(erf_eth_header_t), fh);
- if (bytes_read != sizeof(erf_eth_header_t) ) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, erf_subhdr, sizeof(erf_eth_header_t), err, err_info))
return FALSE;
- }
pseudo_header->erf.subhdr.eth_hdr = pntoh16(&erf_subhdr[0]);
*psize = sizeof(erf_eth_header_t);
break;
@@ -1596,16 +1507,10 @@ static gboolean
pcap_read_i2c_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info)
{
struct i2c_file_hdr i2c_hdr;
- int bytes_read;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&i2c_hdr, sizeof (i2c_hdr), fh);
- if (bytes_read != sizeof (i2c_hdr)) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &i2c_hdr, sizeof (i2c_hdr), err, err_info))
return FALSE;
- }
pseudo_header->i2c.is_event = i2c_hdr.bus & 0x80 ? 1 : 0;
pseudo_header->i2c.bus = i2c_hdr.bus & 0x7f;
diff --git a/wiretap/pcapng.c b/wiretap/pcapng.c
index 39738805cc..eae44ddabe 100644
--- a/wiretap/pcapng.c
+++ b/wiretap/pcapng.c
@@ -425,7 +425,6 @@ pcapng_read_option(FILE_T fh, pcapng_t *pn, pcapng_option_header_t *oh,
char *content, guint len, guint to_read,
int *err, gchar **err_info)
{
- int bytes_read;
int block_read;
/* sanity check: don't run past the end of the block */
@@ -437,12 +436,8 @@ pcapng_read_option(FILE_T fh, pcapng_t *pn, pcapng_option_header_t *oh,
/* read option header */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(oh, sizeof (*oh), fh);
- if (bytes_read != sizeof (*oh)) {
+ if (!wtap_read_bytes(fh, oh, sizeof (*oh), err, err_info)) {
pcapng_debug0("pcapng_read_option: failed to read option");
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
block_read = sizeof (*oh);
@@ -467,12 +462,8 @@ pcapng_read_option(FILE_T fh, pcapng_t *pn, pcapng_option_header_t *oh,
/* read option content */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(content, oh->option_length, fh);
- if (bytes_read != oh->option_length) {
+ if (!wtap_read_bytes(fh, content, oh->option_length, err, err_info)) {
pcapng_debug1("pcapng_read_option: failed to read content of option %u", oh->option_code);
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
block_read += oh->option_length;
@@ -531,10 +522,8 @@ pcapng_read_section_header_block(FILE_T fh, gboolean first_block,
/* read block content */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&shb, sizeof shb, fh);
- if (bytes_read != sizeof shb) {
- *err = file_error(fh, err_info);
- if (*err == 0) {
+ if (!wtap_read_bytes(fh, &shb, sizeof shb, err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
if (first_block) {
/*
* We're reading this as part of an open,
@@ -544,15 +533,10 @@ pcapng_read_section_header_block(FILE_T fh, gboolean first_block,
*/
return -2;
}
-
- /*
- * Otherwise, just report this as an error.
- */
- *err = WTAP_ERR_SHORT_READ;
}
return -1;
}
- block_read = bytes_read;
+ block_read = (guint)sizeof shb;
/* is the magic number one we expect? */
switch (shb.magic) {
@@ -754,15 +738,11 @@ pcapng_read_if_descr_block(wtap *wth, FILE_T fh, pcapng_block_header_t *bh,
/* read block content */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&idb, sizeof idb, fh);
- if (bytes_read != sizeof idb) {
+ if (!wtap_read_bytes(fh, &idb, sizeof idb, err, err_info)) {
pcapng_debug0("pcapng_read_if_descr_block: failed to read IDB");
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
- block_read = bytes_read;
+ block_read = (guint)sizeof idb;
/* mandatory values */
if (pn->byte_swapped) {
@@ -1067,15 +1047,11 @@ pcapng_read_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wta
bh->block_total_length, MIN_EPB_SIZE);
return -1;
}
- bytes_read = file_read(&epb, sizeof epb, fh);
- if (bytes_read != sizeof epb) {
+ if (!wtap_read_bytes(fh, &epb, sizeof epb, err, err_info)) {
pcapng_debug0("pcapng_read_packet_block: failed to read packet data");
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
- block_read = bytes_read;
+ block_read = (guint)sizeof epb;
if (pn->byte_swapped) {
packet.interface_id = GUINT32_SWAP_LE_BE(epb.interface_id);
@@ -1107,15 +1083,11 @@ pcapng_read_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wta
bh->block_total_length, MIN_PB_SIZE);
return -1;
}
- bytes_read = file_read(&pb, sizeof pb, fh);
- if (bytes_read != sizeof pb) {
+ if (!wtap_read_bytes(fh, &pb, sizeof pb, err, err_info)) {
pcapng_debug0("pcapng_read_packet_block: failed to read packet data");
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;;
return -1;
}
- block_read = bytes_read;
+ block_read = (guint)sizeof pb;
if (pn->byte_swapped) {
packet.interface_id = GUINT16_SWAP_LE_BE(pb.interface_id);
@@ -1361,7 +1333,6 @@ pcapng_read_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wta
static int
pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wtapng_block_t *wblock, int *err, gchar **err_info)
{
- int bytes_read;
guint block_read;
interface_info_t iface_info;
pcapng_simple_packet_block_t spb;
@@ -1400,15 +1371,11 @@ pcapng_read_simple_packet_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *
/* "Simple Packet Block" read fixed part */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&spb, sizeof spb, fh);
- if (bytes_read != sizeof spb) {
+ if (!wtap_read_bytes(fh, &spb, sizeof spb, err, err_info)) {
pcapng_debug0("pcapng_read_simple_packet_block: failed to read packet data");
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
- block_read = bytes_read;
+ block_read = (guint)sizeof spb;
if (0 >= pn->interfaces->len) {
*err = WTAP_ERR_BAD_FILE;
@@ -1579,7 +1546,6 @@ name_resolution_block_find_name_end(const char *p, guint record_len, int *err,
static int
pcapng_read_name_resolution_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t *pn, wtapng_block_t *wblock _U_,int *err, gchar **err_info)
{
- int bytes_read = 0;
int block_read = 0;
int to_read;
pcapng_name_resolution_block_t nrb;
@@ -1640,16 +1606,12 @@ pcapng_read_name_resolution_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t
(guint)sizeof nrb);
return -1;
}
- bytes_read = file_read(&nrb, sizeof nrb, fh);
- if (bytes_read != sizeof nrb) {
+ if (!wtap_read_bytes(fh, &nrb, sizeof nrb, err, err_info)) {
ws_buffer_free(&nrb_rec);
pcapng_debug0("pcapng_read_name_resolution_block: failed to read record header");
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
- block_read += bytes_read;
+ block_read += (int)sizeof nrb;
if (pn->byte_swapped) {
nrb.record_type = GUINT16_SWAP_LE_BE(nrb.record_type);
@@ -1692,17 +1654,13 @@ pcapng_read_name_resolution_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t
return -1;
}
ws_buffer_assure_space(&nrb_rec, nrb.record_len);
- bytes_read = file_read(ws_buffer_start_ptr(&nrb_rec),
- nrb.record_len, fh);
- if (bytes_read != nrb.record_len) {
+ if (!wtap_read_bytes(fh, ws_buffer_start_ptr(&nrb_rec),
+ nrb.record_len, err, err_info)) {
ws_buffer_free(&nrb_rec);
pcapng_debug0("pcapng_read_name_resolution_block: failed to read IPv4 record data");
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
- block_read += bytes_read;
+ block_read += nrb.record_len;
if (pn->add_new_ipv4) {
/*
@@ -1765,16 +1723,12 @@ pcapng_read_name_resolution_block(FILE_T fh, pcapng_block_header_t *bh, pcapng_t
return -1;
}
ws_buffer_assure_space(&nrb_rec, nrb.record_len);
- bytes_read = file_read(ws_buffer_start_ptr(&nrb_rec),
- nrb.record_len, fh);
- if (bytes_read != nrb.record_len) {
+ if (!wtap_read_bytes(fh, ws_buffer_start_ptr(&nrb_rec),
+ nrb.record_len, err, err_info)) {
ws_buffer_free(&nrb_rec);
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
- block_read += bytes_read;
+ block_read += nrb.record_len;
if (pn->add_new_ipv6) {
for (namep = (char *)ws_buffer_start_ptr(&nrb_rec) + 16, record_len = nrb.record_len - 16;
@@ -1855,15 +1809,11 @@ pcapng_read_interface_statistics_block(FILE_T fh, pcapng_block_header_t *bh, pca
/* "Interface Statistics Block" read fixed part */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&isb, sizeof isb, fh);
- if (bytes_read != sizeof isb) {
+ if (!wtap_read_bytes(fh, &isb, sizeof isb, err, err_info)) {
pcapng_debug0("pcapng_read_interface_statistics_block: failed to read packet data");
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
- block_read = bytes_read;
+ block_read = (guint)sizeof isb;
if (pn->byte_swapped) {
wblock->data.if_stats.interface_id = GUINT32_SWAP_LE_BE(isb.interface_id);
@@ -2106,23 +2056,21 @@ pcapng_read_block(wtap *wth, FILE_T fh, gboolean first_block, pcapng_t *pn, wtap
/* Try to read the (next) block header */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&bh, sizeof bh, fh);
- if (bytes_read != sizeof bh) {
- *err = file_error(fh, err_info);
- pcapng_debug3("pcapng_read_block: file_read() returned %d instead of %u, err = %d.", bytes_read, (unsigned int)sizeof bh, *err);
- if (*err != 0)
- return -1;
- if (first_block) {
- /* short read or EOF, probably not a pcap-ng file */
- return -2;
+ if (!wtap_read_bytes_or_eof(fh, &bh, sizeof bh, err, err_info)) {
+ pcapng_debug1("pcapng_read_block: wtap_read_bytes_or_eof() failed, err = %d.", *err);
+ if (*err == 0 || *err == WTAP_ERR_SHORT_READ) {
+ if (first_block) {
+ /* short read or EOF, probably not a pcap-ng file */
+ return -2;
+ }
+ if (*err == 0) {
+ return 0; /* EOF */
+ }
}
- if (bytes_read == 0)
- return 0; /* EOF */
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
- block_read = bytes_read;
+ block_read = sizeof bh;
if (pn->byte_swapped) {
bh.block_type = GUINT32_SWAP_LE_BE(bh.block_type);
bh.block_total_length = GUINT32_SWAP_LE_BE(bh.block_total_length);
@@ -2181,12 +2129,9 @@ pcapng_read_block(wtap *wth, FILE_T fh, gboolean first_block, pcapng_t *pn, wtap
/* sanity check: first and second block lengths must match */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&block_total_length, sizeof block_total_length, fh);
- if (bytes_read != sizeof block_total_length) {
+ if (!wtap_read_bytes(fh, &block_total_length, sizeof block_total_length,
+ err, err_info)) {
pcapng_debug0("pcapng_read_block: couldn't read second block length");
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
block_read += bytes_read;
@@ -2327,16 +2272,13 @@ pcapng_open(wtap *wth, int *err, gchar **err_info)
/* Try to read the (next) block header */
saved_offset = file_tell(wth->fh);
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&bh, sizeof bh, wth->fh);
- if (bytes_read == 0) {
- pcapng_debug0("No more IDBs available...");
- break;
- }
- if (bytes_read != sizeof bh) {
- *err = file_error(wth->fh, err_info);
- pcapng_debug3("pcapng_open: Check for more IDB:s, file_read() returned %d instead of %u, err = %d.", bytes_read, (unsigned int)sizeof bh, *err);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes_or_eof(wth->fh, &bh, sizeof bh, err, err_info)) {
+ if (*err == 0) {
+ /* EOF */
+ pcapng_debug0("No more IDBs available...");
+ break;
+ }
+ pcapng_debug1("pcapng_open: Check for more IDB:s, wtap_read_bytes_or_eof() failed, err = %d.", *err);
return -1;
}
@@ -2367,7 +2309,7 @@ pcapng_open(wtap *wth, int *err, gchar **err_info)
}
pcapng_process_idb(wth, pcapng, &wblock);
pcapng_debug2("pcapng_open: Read IDB number_of_interfaces %u, wtap_encap %i",
- wth->interface_data->len, *wblock.file_encap);
+ wth->interface_data->len, wth->file_encap);
}
return 1;
}
diff --git a/wiretap/peekclassic.c b/wiretap/peekclassic.c
index 48454addf6..f1e109bbe1 100644
--- a/wiretap/peekclassic.c
+++ b/wiretap/peekclassic.c
@@ -154,7 +154,6 @@ static gboolean peekclassic_read_packet_v56(wtap *wth, FILE_T fh,
int peekclassic_open(wtap *wth, int *err, gchar **err_info)
{
peekclassic_header_t ep_hdr;
- int bytes_read;
struct timeval reference_time;
int file_encap;
peekclassic_t *peekclassic;
@@ -168,11 +167,9 @@ int peekclassic_open(wtap *wth, int *err, gchar **err_info)
* and we may have to add more checks at some point.
*/
g_assert(sizeof(ep_hdr.master) == PEEKCLASSIC_MASTER_HDR_SIZE);
- bytes_read = file_read(&ep_hdr.master, (int)sizeof(ep_hdr.master),
- wth->fh);
- if (bytes_read != sizeof(ep_hdr.master)) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &ep_hdr.master,
+ (int)sizeof(ep_hdr.master), err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -200,13 +197,16 @@ int peekclassic_open(wtap *wth, int *err, gchar **err_info)
/* get the secondary header */
g_assert(sizeof(ep_hdr.secondary.v567) ==
PEEKCLASSIC_V567_HDR_SIZE);
- bytes_read = file_read(&ep_hdr.secondary.v567,
- (int)sizeof(ep_hdr.secondary.v567), wth->fh);
- if (bytes_read != sizeof(ep_hdr.secondary.v567)) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
- return -1;
- return 0;
+ if (!wtap_read_bytes(wth->fh, &ep_hdr.secondary.v567,
+ (int)sizeof(ep_hdr.secondary.v567), err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for a secondary
+ * header, so not a Peek classic file.
+ */
+ return 0;
+ }
+ return -1;
}
if ((0 != ep_hdr.secondary.v567.reserved[0]) ||
@@ -407,7 +407,6 @@ static int peekclassic_read_packet_v7(wtap *wth, FILE_T fh,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
{
guint8 ep_pkt[PEEKCLASSIC_V7_PKT_SIZE];
- int bytes_read;
#if 0
guint16 protoNum;
#endif
@@ -421,13 +420,8 @@ static int peekclassic_read_packet_v7(wtap *wth, FILE_T fh,
time_t tsecs;
guint32 tusecs;
- bytes_read = file_read(ep_pkt, sizeof(ep_pkt), fh);
- if (bytes_read != (int) sizeof(ep_pkt)) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read > 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes_or_eof(fh, ep_pkt, sizeof(ep_pkt), err, err_info))
return -1;
- }
/* Extract the fields from the packet */
#if 0
@@ -532,7 +526,6 @@ static gboolean peekclassic_read_packet_v56(wtap *wth, FILE_T fh,
{
peekclassic_t *peekclassic = (peekclassic_t *)wth->priv;
guint8 ep_pkt[PEEKCLASSIC_V56_PKT_SIZE];
- int bytes_read;
guint16 length;
guint16 sliceLength;
#if 0
@@ -550,13 +543,8 @@ static gboolean peekclassic_read_packet_v56(wtap *wth, FILE_T fh,
#endif
unsigned int i;
- bytes_read = file_read(ep_pkt, sizeof(ep_pkt), fh);
- if (bytes_read != (int) sizeof(ep_pkt)) {
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read > 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes_or_eof(fh, ep_pkt, sizeof(ep_pkt), err, err_info))
return FALSE;
- }
/* Extract the fields from the packet */
length = pntoh16(&ep_pkt[PEEKCLASSIC_V56_LENGTH_OFFSET]);
diff --git a/wiretap/peektagged.c b/wiretap/peektagged.c
index c7a695e14b..d436d60871 100644
--- a/wiretap/peektagged.c
+++ b/wiretap/peektagged.c
@@ -215,7 +215,6 @@ static int wtap_file_read_number (wtap *wth, guint32 *num, int *err,
int peektagged_open(wtap *wth, int *err, gchar **err_info)
{
peektagged_section_header_t ap_hdr;
- int bytes_read;
int ret;
guint32 fileVersion;
guint32 mediaType;
@@ -230,10 +229,8 @@ int peektagged_open(wtap *wth, int *err, gchar **err_info)
#define NUM_PEEKTAGGED_ENCAPS (sizeof peektagged_encap / sizeof peektagged_encap[0])
peektagged_t *peektagged;
- bytes_read = file_read(&ap_hdr, (int)sizeof(ap_hdr), wth->fh);
- if (bytes_read != (int)sizeof(ap_hdr)) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &ap_hdr, (int)sizeof(ap_hdr), err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -400,7 +397,6 @@ peektagged_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
peektagged_t *peektagged = (peektagged_t *)wth->priv;
hdr_info_t hdr_info;
int header_len = 0;
- int bytes_read;
guint8 tag_value[6];
guint16 tag;
gboolean saw_length = FALSE;
@@ -415,20 +411,14 @@ peektagged_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
do {
/* Get the tag and value.
XXX - this assumes all values are 4 bytes long. */
- bytes_read = file_read(tag_value, sizeof tag_value, fh);
- if (bytes_read != (int) sizeof tag_value) {
- *err = file_error(fh, err_info);
+ if (!wtap_read_bytes_or_eof(fh, tag_value, sizeof tag_value, err, err_info)) {
if (*err == 0) {
- if (bytes_read > 0)
+ /*
+ * Short read if we've read something already;
+ * just an EOF if we haven't.
+ */
+ if (header_len != 0)
*err = WTAP_ERR_SHORT_READ;
- else if (bytes_read == 0) {
- /*
- * Short read if we've read something already;
- * just an EOF if we haven't.
- */
- if (header_len != 0)
- *err = WTAP_ERR_SHORT_READ;
- }
}
return -1;
}
diff --git a/wiretap/pppdump.c b/wiretap/pppdump.c
index 0ca9201ff5..978bc5862c 100644
--- a/wiretap/pppdump.c
+++ b/wiretap/pppdump.c
@@ -246,7 +246,6 @@ int
pppdump_open(wtap *wth, int *err, gchar **err_info)
{
guint8 buffer[6]; /* Looking for: 0x07 t3 t2 t1 t0 ID */
- int bytes_read;
pppdump_t *state;
/* There is no file header, only packet records. Fortunately for us,
@@ -257,10 +256,9 @@ pppdump_open(wtap *wth, int *err, gchar **err_info)
* representing the timestamp.
*/
- bytes_read = file_read(buffer, sizeof(buffer), wth->fh);
- if (bytes_read != (int) sizeof(buffer)) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, buffer, sizeof(buffer),
+ err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -659,14 +657,16 @@ collate(pppdump_t* state, FILE_T fh, int *err, gchar **err_info, guint8 *pd,
break;
case PPPD_RESET_TIME:
- wtap_file_read_unknown_bytes(&time_long, sizeof(guint32), fh, err, err_info);
+ if (!wtap_read_bytes(fh, &time_long, sizeof(guint32), err, err_info))
+ return FALSE;
state->offset += sizeof(guint32);
state->timestamp = pntoh32(&time_long);
state->tenths = 0;
break;
case PPPD_TIME_STEP_LONG:
- wtap_file_read_unknown_bytes(&time_long, sizeof(guint32), fh, err, err_info);
+ if (!wtap_read_bytes(fh, &time_long, sizeof(guint32), err, err_info))
+ return FALSE;
state->offset += sizeof(guint32);
state->tenths += pntoh32(&time_long);
@@ -678,7 +678,8 @@ collate(pppdump_t* state, FILE_T fh, int *err, gchar **err_info, guint8 *pd,
break;
case PPPD_TIME_STEP_SHORT:
- wtap_file_read_unknown_bytes(&time_short, sizeof(guint8), fh, err, err_info);
+ if (!wtap_read_bytes(fh, &time_short, sizeof(guint8), err, err_info))
+ return FALSE;
state->offset += sizeof(guint8);
state->tenths += time_short;
diff --git a/wiretap/radcom.c b/wiretap/radcom.c
index 9b0072c1ff..bc19af68b4 100644
--- a/wiretap/radcom.c
+++ b/wiretap/radcom.c
@@ -54,7 +54,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).
@@ -90,12 +90,9 @@ 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(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,10 +102,8 @@ int radcom_open(wtap *wth, int *err, gchar **err_info)
/* 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, wth->fh);
- if (bytes_read != 8) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, r_magic, 8, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -126,37 +121,89 @@ int radcom_open(wtap *wth, int *err, gchar **err_info)
/* 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, wth->fh);
- if (bytes_read != 11) {
- *err = file_error(wth->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) {
+ /*
+ * Not enough bytes for the active time string,
+ * so not a RADCOM file.
+ */
+ return 0;
+ }
+ return -1;
}
while (memcmp(t_magic, active_time_magic, 11) != 0)
{
if (file_seek(wth->fh, -10, SEEK_CUR, err) == -1)
return -1;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(t_magic, 11, wth->fh);
- if (bytes_read != 11) {
- *err = file_error(wth->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) {
+ /*
+ * Not enough bytes for the active time string,
+ * so not a RADCOM file.
+ */
+ return 0;
+ }
+ return -1;
}
}
if (file_seek(wth->fh, -43, SEEK_CUR, err) == -1) return -1;
/* Get capture start time */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&start_date, sizeof(struct frame_date),
- wth->fh);
- if (bytes_read != sizeof(struct frame_date)) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, &start_date, sizeof(struct frame_date),
+ err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for the start time, so not
+ * a RADCOM file.
+ */
+ return 0;
+ }
+ return -1;
+ }
+
+ if (file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR, err) == -1)
+ return -1;
+
+ for (;;) {
+ errno = WTAP_ERR_CANT_READ;
+ if (!wtap_read_bytes(wth->fh, search_encap, 4,
+ err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for the encapsulation,
+ * so not a RADCOM file.
+ */
+ return 0;
+ }
+ return -1;
+ }
+
+ 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 -1;
- return 0;
+ }
+ if (file_seek(wth->fh, 12, SEEK_CUR, err) == -1)
+ return -1;
+ errno = WTAP_ERR_CANT_READ;
+ if (!wtap_read_bytes(wth->fh, search_encap, 4, err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ) {
+ /*
+ * Not enough bytes for the start time, so not
+ * a RADCOM file.
+ */
+ return 0;
+ }
+ return -1;
}
/* This is a radcom file */
@@ -176,30 +223,7 @@ int radcom_open(wtap *wth, int *err, gchar **err_info)
tm.tm_sec = sec%60;
tm.tm_isdst = -1;
#endif
- if (file_seek(wth->fh, sizeof(struct frame_date), SEEK_CUR, err) == -1)
- return -1;
- errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(search_encap, 4, wth->fh);
- if (bytes_read != 4) {
- goto read_error;
- }
- while (memcmp(encap_magic, search_encap, 4)) {
- if (file_seek(wth->fh, -3, SEEK_CUR, err) == -1)
- return -1;
- errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(search_encap, 4, wth->fh);
- if (bytes_read != 4) {
- goto read_error;
- }
- }
- if (file_seek(wth->fh, 12, SEEK_CUR, err) == -1)
- return -1;
- errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(search_encap, 4, wth->fh);
- if (bytes_read != 4) {
- goto read_error;
- }
if (memcmp(search_encap, "LAPB", 4) == 0)
wth->file_encap = WTAP_ENCAP_LAPB;
else if (memcmp(search_encap, "Ethe", 4) == 0)
@@ -213,21 +237,18 @@ int radcom_open(wtap *wth, int *err, gchar **err_info)
}
#if 0
- bytes_read = file_read(&next_date, sizeof(struct frame_date), wth->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 -1;
while (memcmp(&start_date, &next_date, 4)) {
if (file_seek(wth->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),
- wth->fh);
- 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 -1;
}
#endif
@@ -243,19 +264,12 @@ int radcom_open(wtap *wth, int *err, gchar **err_info)
}
return 1;
-
-read_error:
- *err = file_error(wth->fh, err_info);
- if (*err != 0)
- return -1;
- return 0;
}
/* Read the next packet */
static gboolean radcom_read(wtap *wth, int *err, gchar **err_info,
gint64 *data_offset)
{
- int bytes_read;
char fcs[2];
*data_offset = file_tell(wth->fh);
@@ -273,13 +287,8 @@ static gboolean radcom_read(wtap *wth, int *err, gchar **err_info,
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, wth->fh);
- if (bytes_read != sizeof fcs) {
- *err = file_error(wth->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;
@@ -311,20 +320,14 @@ 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) {
@@ -372,7 +375,8 @@ radcom_read_rec(wtap *wth, 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(fh, atmhdr, sizeof atmhdr, err,
+ errno = WTAP_ERR_CANT_READ;
+ if (!wtap_read_bytes(fh, atmhdr, sizeof atmhdr, err,
err_info))
return FALSE; /* Read error */
length -= 8;
@@ -391,21 +395,3 @@ radcom_read_rec(wtap *wth, 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;
-}
diff --git a/wiretap/snoop.c b/wiretap/snoop.c
index 0318a07257..9d17570389 100644
--- a/wiretap/snoop.c
+++ b/wiretap/snoop.c
@@ -179,7 +179,6 @@ static gboolean snoop_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
*/
int snoop_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic[sizeof snoop_magic];
struct snoop_hdr hdr;
struct snooprec_hdr rec_hdr;
@@ -255,10 +254,8 @@ int snoop_open(wtap *wth, int *err, gchar **err_info)
/* Read in the string that should be at the start of a "snoop" file */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(magic, sizeof magic, wth->fh);
- if (bytes_read != sizeof magic) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -269,13 +266,8 @@ int snoop_open(wtap *wth, int *err, gchar **err_info)
/* Read the rest of the header. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&hdr, sizeof hdr, wth->fh);
- if (bytes_read != sizeof hdr) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info))
return -1;
- }
/*
* Make sure it's a version we support.
@@ -330,12 +322,9 @@ int snoop_open(wtap *wth, int *err, gchar **err_info)
/* Read first record header. */
saved_offset = file_tell(wth->fh);
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&rec_hdr, sizeof rec_hdr, wth->fh);
- if (bytes_read != sizeof rec_hdr) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
- return -1;
+ if (!wtap_read_bytes_or_eof(wth->fh, &rec_hdr, sizeof rec_hdr, err, err_info)) {
+ if (*err != 0)
+ return -1;
/*
* The file ends after the record header, which means this
@@ -448,9 +437,6 @@ static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
gint64 *data_offset)
{
int padbytes;
- int bytes_read;
- char padbuf[4];
- int bytes_to_read;
*data_offset = file_tell(wth->fh);
@@ -460,28 +446,11 @@ static gboolean snoop_read(wtap *wth, int *err, gchar **err_info,
return FALSE;
/*
- * Skip over the padding (don't "fseek()", as the standard
- * I/O library on some platforms discards buffered data if
- * you do that, which means it does a lot more reads).
- *
- * XXX - is that still true?
- *
- * There's probably not much padding (it's probably padded only
- * to a 4-byte boundary), so we probably need only do one read.
+ * Skip over the padding, if any.
*/
- while (padbytes != 0) {
- bytes_to_read = padbytes;
- if ((unsigned)bytes_to_read > sizeof padbuf)
- bytes_to_read = sizeof padbuf;
- errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(padbuf, bytes_to_read, wth->fh);
- if (bytes_read != bytes_to_read) {
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (padbytes != 0) {
+ if (!file_skip(wth->fh, padbytes, err))
return FALSE;
- }
- padbytes -= bytes_read;
}
return TRUE;
@@ -507,7 +476,6 @@ snoop_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
Buffer *buf, int *err, gchar **err_info)
{
struct snooprec_hdr hdr;
- int bytes_read;
guint32 rec_size;
guint32 packet_size;
guint32 orig_size;
@@ -515,13 +483,8 @@ snoop_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
/* Read record header. */
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 -1;
- }
rec_size = g_ntohl(hdr.rec_len);
orig_size = g_ntohl(hdr.orig_len);
@@ -663,18 +626,12 @@ snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
int *err, gchar **err_info)
{
struct snoop_atm_hdr atm_phdr;
- int bytes_read;
guint8 vpi;
guint16 vci;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&atm_phdr, sizeof (struct snoop_atm_hdr), fh);
- if (bytes_read != sizeof (struct snoop_atm_hdr)) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &atm_phdr, sizeof atm_phdr, err, err_info))
return FALSE;
- }
vpi = atm_phdr.vpi;
vci = pntoh16(&atm_phdr.vci);
@@ -757,17 +714,11 @@ snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
int *header_size)
{
shomiti_wireless_header whdr;
- int bytes_read;
int rsize;
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&whdr, sizeof (shomiti_wireless_header), fh);
- if (bytes_read != sizeof (shomiti_wireless_header)) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, &whdr, sizeof whdr, err, err_info))
return FALSE;
- }
/* the 4th byte of the pad is actually a header length,
* we've already read 8 bytes of it, and it must never
diff --git a/wiretap/stanag4607.c b/wiretap/stanag4607.c
index bdf458fe35..24b29d7518 100644
--- a/wiretap/stanag4607.c
+++ b/wiretap/stanag4607.c
@@ -55,16 +55,14 @@ static gboolean stanag4607_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *p
guint32 millisecs, secs, nsecs;
gint64 offset = 0;
guint8 stanag_pkt_hdr[37];
- int bytes_read;
guint32 packet_size;
*err = 0;
/* Combined packet header and segment header */
- bytes_read = file_read(stanag_pkt_hdr, sizeof stanag_pkt_hdr, fh);
- if (bytes_read != sizeof stanag_pkt_hdr)
- goto fail;
- offset += bytes_read;
+ if (!wtap_read_bytes_or_eof(fh, stanag_pkt_hdr, sizeof stanag_pkt_hdr, err, err_info))
+ return FALSE;
+ offset += sizeof stanag_pkt_hdr;
if (!is_valid_id(pntoh16(&stanag_pkt_hdr[0]))) {
*err = WTAP_ERR_BAD_FILE;
@@ -96,10 +94,9 @@ static gboolean stanag4607_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *p
guint8 mseg[39];
struct tm tm;
- bytes_read = file_read(&mseg, sizeof mseg, fh);
- if (bytes_read != sizeof mseg)
- goto fail;
- offset += bytes_read;
+ if (!wtap_read_bytes(fh, &mseg, sizeof mseg, err, err_info))
+ return FALSE;
+ offset += sizeof mseg;
tm.tm_year = pntoh16(&mseg[35]) - 1900;
tm.tm_mon = mseg[37] - 1;
@@ -112,18 +109,16 @@ static gboolean stanag4607_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *p
phdr->ts.secs = stanag4607->base_secs;
}
else if (PLATFORM_LOCATION_SEGMENT == stanag_pkt_hdr[32]) {
- bytes_read = file_read(&millisecs, sizeof millisecs, fh);
- if (bytes_read != sizeof millisecs)
- goto fail;
- offset += bytes_read;
+ if (!wtap_read_bytes(fh, &millisecs, sizeof millisecs, err, err_info))
+ return FALSE;
+ offset += sizeof millisecs;
millisecs = g_ntohl(millisecs);
}
else if (DWELL_SEGMENT == stanag_pkt_hdr[32]) {
guint8 dseg[19];
- bytes_read = file_read(&dseg, sizeof dseg, fh);
- if (bytes_read != sizeof dseg)
- goto fail;
- offset += bytes_read;
+ if (!wtap_read_bytes(fh, &dseg, sizeof dseg, err, err_info))
+ return FALSE;
+ offset += sizeof dseg;
millisecs = pntoh32(&dseg[15]);
}
if (0 != millisecs) {
@@ -138,10 +133,6 @@ static gboolean stanag4607_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *p
return FALSE;
return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
-
-fail:
- *err = file_error(wth->fh, err_info);
- return FALSE;
}
static gboolean stanag4607_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
@@ -169,15 +160,11 @@ static gboolean stanag4607_seek_read(wtap *wth, gint64 seek_off,
int stanag4607_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
guint16 version_id;
stanag4607_t *stanag4607;
- bytes_read = file_read(&version_id, sizeof version_id, wth->fh);
- if (bytes_read != sizeof version_id) {
- *err = file_error(wth->fh, err_info);
- return (*err != 0) ? -1 : 0;
- }
+ if (!wtap_read_bytes(wth->fh, &version_id, sizeof version_id, err, err_info))
+ return (*err != WTAP_ERR_SHORT_READ) ? -1 : 0;
if (!is_valid_id(GUINT16_TO_BE(version_id)))
/* Not a stanag4607 file */
diff --git a/wiretap/tnef.c b/wiretap/tnef.c
index 2e8b565485..3dcc5e40ca 100644
--- a/wiretap/tnef.c
+++ b/wiretap/tnef.c
@@ -98,14 +98,10 @@ static gboolean tnef_seek_read(wtap *wth, gint64 seek_off,
int tnef_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
guint32 magic;
- bytes_read = file_read(&magic, sizeof magic, wth->fh);
- if (bytes_read != sizeof magic) {
- *err = file_error(wth->fh, err_info);
- return (*err != 0) ? -1 : 0;
- }
+ if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info))
+ return (*err != WTAP_ERR_SHORT_READ) ? -1 : 0;
if (GUINT32_TO_LE(magic) != TNEF_SIGNATURE)
/* Not a tnef file */
diff --git a/wiretap/visual.c b/wiretap/visual.c
index aab7e4a9c4..0aedeb3a73 100644
--- a/wiretap/visual.c
+++ b/wiretap/visual.c
@@ -173,7 +173,6 @@ static void visual_dump_free(wtap_dumper *wdh);
/* Open a file for reading */
int visual_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic[sizeof visual_magic];
struct visual_file_hdr vfile_hdr;
struct visual_read_info * visual;
@@ -181,11 +180,9 @@ int visual_open(wtap *wth, int *err, gchar **err_info)
/* Check the magic string at the start of the file */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(magic, sizeof magic, wth->fh);
- if (bytes_read != sizeof magic)
+ if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info))
{
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
+ if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
@@ -196,12 +193,8 @@ int visual_open(wtap *wth, int *err, gchar **err_info)
/* Read the rest of the file header. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&vfile_hdr, sizeof vfile_hdr, wth->fh);
- if (bytes_read != sizeof vfile_hdr)
+ if (!wtap_read_bytes(wth->fh, &vfile_hdr, sizeof vfile_hdr, err, err_info))
{
- *err = file_error(wth->fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
return -1;
}
@@ -325,7 +318,6 @@ visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
{
struct visual_read_info *visual = (struct visual_read_info *)wth->priv;
struct visual_pkt_hdr vpkt_hdr;
- int bytes_read;
guint32 packet_size;
struct visual_atm_hdr vatm_hdr;
double t;
@@ -336,14 +328,8 @@ visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
/* Read the packet header. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&vpkt_hdr, (unsigned int)sizeof vpkt_hdr, fh);
- if (bytes_read < 0 || (size_t)bytes_read != sizeof vpkt_hdr)
+ if (!wtap_read_bytes_or_eof(fh, &vpkt_hdr, (unsigned int)sizeof vpkt_hdr, err, err_info))
{
- *err = file_error(fh, err_info);
- if (*err == 0 && bytes_read != 0)
- {
- *err = WTAP_ERR_SHORT_READ;
- }
return FALSE;
}
@@ -460,14 +446,8 @@ visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
ATM packets have an additional packet header; read and
process it. */
errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&vatm_hdr, (unsigned int)sizeof vatm_hdr, fh);
- if (bytes_read < 0 || (size_t)bytes_read != sizeof vatm_hdr)
+ if (!wtap_read_bytes(fh, &vatm_hdr, (unsigned int)sizeof vatm_hdr, err, err_info))
{
- *err = file_error(fh, err_info);
- if (*err == 0)
- {
- *err = WTAP_ERR_SHORT_READ;
- }
return FALSE;
}
diff --git a/wiretap/vwr.c b/wiretap/vwr.c
index 64ab7d32c9..1a76c6c759 100644
--- a/wiretap/vwr.c
+++ b/wiretap/vwr.c
@@ -644,10 +644,8 @@ static gboolean vwr_read_rec_header(vwr_t *vwr, FILE_T fh, int *rec_size, int *I
/* variable-length item, we read the variable length item out and discard it. */
/* If we find a frame, we return (with the header in the passed buffer). */
while (1) {
- if (file_read(header, VW_RECORD_HEADER_LENGTH, fh) != VW_RECORD_HEADER_LENGTH) {
- *err = file_error(fh, err_info);
+ if (!wtap_read_bytes_or_eof(fh, header, VW_RECORD_HEADER_LENGTH, err, err_info))
return FALSE;
- }
/* Got a header; invoke decode-message function to parse and process it. */
/* If the function returns a length, then a frame or variable-length message */
@@ -660,7 +658,7 @@ static gboolean vwr_read_rec_header(vwr_t *vwr, FILE_T fh, int *rec_size, int *I
return FALSE;
}
else if (v_type != VT_FRAME) {
- if (file_seek(fh, f_len, SEEK_CUR, err) < 0)
+ if (!file_skip(fh, f_len, err))
return FALSE;
}
else {
@@ -705,7 +703,7 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info)
/* Each 16-byte message is decoded; if we run across a non-frame message followed by a */
/* variable-length item, we read the variable length item out and discard it. */
/* If we find a frame, we return (with the header in the passed buffer). */
- while ((file_read(header, VW_RECORD_HEADER_LENGTH, wth->fh)) == VW_RECORD_HEADER_LENGTH) {
+ while (wtap_read_bytes(wth->fh, header, VW_RECORD_HEADER_LENGTH, err, err_info)) {
/* Got a header; invoke decode-message function to parse and process it. */
/* If the function returns a length, then a frame or variable-length message */
/* follows the 16-byte message. */
@@ -726,11 +724,10 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info)
rec_size = f_len;
/* Got a frame record; read over entire record (frame + trailer) into a local buffer */
/* If we don't get it all, assume this isn't a vwr file */
- if (file_read(rec, rec_size, wth->fh) != rec_size) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
- return -1;
- return UNKNOWN_FPGA; /* short read - not a vwr file */
+ if (!wtap_read_bytes(wth->fh, rec, rec_size, err, err_info)) {
+ if (*err == WTAP_ERR_SHORT_READ)
+ return UNKNOWN_FPGA; /* short read - not a vwr file */
+ return -1;
}
@@ -820,10 +817,9 @@ static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info)
if (valid_but_empty_file > 0)
return(S3_W_FPGA);
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
- return -1;
- return UNKNOWN_FPGA; /* short read - not a vwr file */
+ if (*err == WTAP_ERR_SHORT_READ)
+ return UNKNOWN_FPGA; /* short read - not a vwr file */
+ return -1;
}
/* Copy the actual packet data from the capture file into the target data block. */
@@ -2161,12 +2157,8 @@ vwr_process_rec_data(FILE_T fh, int rec_size,
/* Read over the entire record (frame + trailer) into a local buffer. */
/* If we don't get it all, then declare an error, we can't process the frame. */
- if (file_read(rec, rec_size, fh) != rec_size) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!wtap_read_bytes(fh, rec, rec_size, err, err_info))
return FALSE;
- }
/* now format up the frame data */
switch (vwr->FPGA_VERSION)
diff --git a/wiretap/wtap-int.h b/wiretap/wtap-int.h
index fa95119dfe..c550502b81 100644
--- a/wiretap/wtap-int.h
+++ b/wiretap/wtap-int.h
@@ -245,33 +245,6 @@ extern gint wtap_num_file_types;
}
#endif
-#define wtap_file_read_unknown_bytes(target, num_bytes, fh, err, err_info) \
- G_STMT_START \
- { \
- int _bytes_read; \
- _bytes_read = file_read((target), (num_bytes), (fh)); \
- if (_bytes_read != (int) (num_bytes)) { \
- *(err) = file_error((fh), (err_info)); \
- return FALSE; \
- } \
- } \
- G_STMT_END
-
-#define wtap_file_read_expected_bytes(target, num_bytes, fh, err, err_info) \
- G_STMT_START \
- { \
- int _bytes_read; \
- _bytes_read = file_read((target), (num_bytes), (fh)); \
- if (_bytes_read != (int) (num_bytes)) { \
- *(err) = file_error((fh), (err_info)); \
- if (*(err) == 0 && _bytes_read > 0) { \
- *(err) = WTAP_ERR_SHORT_READ; \
- } \
- return FALSE; \
- } \
- } \
- G_STMT_END
-
/* glib doesn't have g_ptr_array_len of all things!*/
#ifndef g_ptr_array_len
#define g_ptr_array_len(a) ((a)->len)
@@ -281,6 +254,43 @@ extern gint wtap_num_file_types;
GSList *wtap_get_compressed_file_extensions(void);
/*
+ * Read a given number of bytes from a file.
+ *
+ * If we succeed, return TRUE.
+ *
+ * If we get an EOF, return FALSE with *err set to 0, reporting this
+ * as an EOF.
+ *
+ * If we get fewer bytes than the specified number, return FALSE with
+ * *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
+ * error.
+ *
+ * If we get a read error, return FALSE with *err and *err_info set
+ * appropriately.
+ */
+WS_DLL_PUBLIC
+gboolean
+wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
+ gchar **err_info);
+
+/*
+ * Read a given number of bytes from a file.
+ *
+ * If we succeed, return TRUE.
+ *
+ * If we get fewer bytes than the specified number, including getting
+ * an EOF, return FALSE with *err set to WTAP_ERR_SHORT_READ, reporting
+ * this as a short read error.
+ *
+ * If we get a read error, return FALSE with *err and *err_info set
+ * appropriately.
+ */
+WS_DLL_PUBLIC
+gboolean
+wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
+ gchar **err_info);
+
+/*
* Read packet data into a Buffer, growing the buffer as necessary.
*
* This returns an error on a short read, even if the short read hit
diff --git a/wiretap/wtap.c b/wiretap/wtap.c
index a4fb756059..95d6ef11af 100644
--- a/wiretap/wtap.c
+++ b/wiretap/wtap.c
@@ -1036,25 +1036,56 @@ wtap_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
}
/*
- * Read packet data into a Buffer, growing the buffer as necessary.
+ * Read a given number of bytes from a file.
*
- * This returns an error on a short read, even if the short read hit
- * the EOF immediately. (The assumption is that each packet has a
- * header followed by raw packet data, and that we've already read the
- * header, so if we get an EOF trying to read the packet data, the file
- * has been cut short, even if the read didn't read any data at all.)
+ * If we succeed, return TRUE.
+ *
+ * If we get an EOF, return FALSE with *err set to 0, reporting this
+ * as an EOF.
+ *
+ * If we get fewer bytes than the specified number, return FALSE with
+ * *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
+ * error.
+ *
+ * If we get a read error, return FALSE with *err and *err_info set
+ * appropriately.
*/
gboolean
-wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
+wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
gchar **err_info)
{
int bytes_read;
- ws_buffer_assure_space(buf, length);
- errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(ws_buffer_start_ptr(buf), length, fh);
+ bytes_read = file_read(buf, count, fh);
+ if (bytes_read < 0 || (guint)bytes_read != count) {
+ *err = file_error(fh, err_info);
+ if (*err == 0 && bytes_read > 0)
+ *err = WTAP_ERR_SHORT_READ;
+ return FALSE;
+ }
+ return TRUE;
+}
- if (bytes_read < 0 || (guint)bytes_read != length) {
+/*
+ * Read a given number of bytes from a file.
+ *
+ * If we succeed, return TRUE.
+ *
+ * If we get fewer bytes than the specified number, including getting
+ * an EOF, return FALSE with *err set to WTAP_ERR_SHORT_READ, reporting
+ * this as a short read error.
+ *
+ * If we get a read error, return FALSE with *err and *err_info set
+ * appropriately.
+ */
+gboolean
+wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
+ gchar **err_info)
+{
+ int bytes_read;
+
+ bytes_read = file_read(buf, count, fh);
+ if (bytes_read < 0 || (guint)bytes_read != count) {
*err = file_error(fh, err_info);
if (*err == 0)
*err = WTAP_ERR_SHORT_READ;
@@ -1064,6 +1095,25 @@ wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
}
/*
+ * Read packet data into a Buffer, growing the buffer as necessary.
+ *
+ * This returns an error on a short read, even if the short read hit
+ * the EOF immediately. (The assumption is that each packet has a
+ * header followed by raw packet data, and that we've already read the
+ * header, so if we get an EOF trying to read the packet data, the file
+ * has been cut short, even if the read didn't read any data at all.)
+ */
+gboolean
+wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
+ gchar **err_info)
+{
+ ws_buffer_assure_space(buf, length);
+ errno = WTAP_ERR_CANT_READ;
+ return wtap_read_bytes(fh, ws_buffer_start_ptr(buf), length, err,
+ err_info);
+}
+
+/*
* Return an approximation of the amount of data we've read sequentially
* from the file so far. (gint64, in case that's 64 bits.)
*/