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/netxray.c | |
parent | 6397ad43c2374ebde388041f2bd7ac925606a51e (diff) |
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/netxray.c')
-rw-r--r-- | wiretap/netxray.c | 33 |
1 files changed, 9 insertions, 24 deletions
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; } |