diff options
author | Guy Harris <guy@alum.mit.edu> | 2014-10-06 18:00:57 -0700 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2014-10-07 01:01:59 +0000 |
commit | 670ebda4a6af0d30e033b0af48cfd15ce52c10eb (patch) | |
tree | b092e44c944c4eb7566964da4cfb914e6002bd6d /wiretap/stanag4607.c | |
parent | 6397ad43c2374ebde388041f2bd7ac925606a51e (diff) | |
download | wireshark-670ebda4a6af0d30e033b0af48cfd15ce52c10eb.tar.gz |
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'wiretap/stanag4607.c')
-rw-r--r-- | wiretap/stanag4607.c | 41 |
1 files changed, 14 insertions, 27 deletions
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 */ |