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/visual.c | |
parent | 6397ad43c2374ebde388041f2bd7ac925606a51e (diff) | |
download | wireshark-670ebda4a6af0d30e033b0af48cfd15ce52c10eb.tar.gz wireshark-670ebda4a6af0d30e033b0af48cfd15ce52c10eb.tar.bz2 wireshark-670ebda4a6af0d30e033b0af48cfd15ce52c10eb.zip |
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/visual.c')
-rw-r--r-- | wiretap/visual.c | 30 |
1 files changed, 5 insertions, 25 deletions
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; } |