From 670ebda4a6af0d30e033b0af48cfd15ce52c10eb Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Mon, 6 Oct 2014 18:00:57 -0700 Subject: 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 --- wiretap/csids.c | 28 ++++++++-------------------- 1 file changed, 8 insertions(+), 20 deletions(-) (limited to 'wiretap/csids.c') 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); -- cgit v1.2.1