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/iptrace.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/iptrace.c')
-rw-r--r-- | wiretap/iptrace.c | 48 |
1 files changed, 10 insertions, 38 deletions
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) |