Skip to content

Commit

Permalink
Add some higher-level file-read APIs and use them.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
guyharris committed Oct 7, 2014
1 parent 6397ad4 commit 670ebda
Show file tree
Hide file tree
Showing 40 changed files with 787 additions and 1,337 deletions.
29 changes: 7 additions & 22 deletions wiretap/5views.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,13 @@ static gboolean _5views_dump_close(wtap_dumper *wdh, int *err);

int _5views_open(wtap *wth, int *err, gchar **err_info)
{
int bytes_read;
t_5VW_Capture_Header Capture_Header;
int encap = WTAP_ENCAP_UNKNOWN;

errno = WTAP_ERR_CANT_READ;
bytes_read = file_read(&Capture_Header.Info_Header, sizeof(t_5VW_Info_Header), wth->fh);
if (bytes_read != sizeof(t_5VW_Info_Header)) {
*err = file_error(wth->fh, err_info);
if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
if (!wtap_read_bytes(wth->fh, &Capture_Header.Info_Header,
sizeof(t_5VW_Info_Header), err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
Expand Down Expand Up @@ -171,13 +169,9 @@ int _5views_open(wtap *wth, int *err, gchar **err_info)
}

/* read the remaining header information */
bytes_read = file_read(&Capture_Header.HeaderDateCreation, sizeof (t_5VW_Capture_Header) - sizeof(t_5VW_Info_Header), wth->fh);
if (bytes_read != sizeof (t_5VW_Capture_Header)- sizeof(t_5VW_Info_Header) ) {
*err = file_error(wth->fh, err_info);
if (*err == 0)
*err = WTAP_ERR_SHORT_READ;
if (!wtap_read_bytes(wth->fh, &Capture_Header.HeaderDateCreation,
sizeof (t_5VW_Capture_Header) - sizeof(t_5VW_Info_Header), err, err_info))
return -1;
}

/* This is a 5views capture file */
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_5VIEWS;
Expand Down Expand Up @@ -270,19 +264,10 @@ static gboolean
_5views_read_header(wtap *wth, FILE_T fh, t_5VW_TimeStamped_Header *hdr,
struct wtap_pkthdr *phdr, int *err, gchar **err_info)
{
int bytes_read, bytes_to_read;

bytes_to_read = sizeof(t_5VW_TimeStamped_Header);

/* Read record header. */
bytes_read = file_read(hdr, bytes_to_read, fh);
if (bytes_read != bytes_to_read) {
*err = file_error(fh, err_info);
if (*err == 0 && bytes_read != 0) {
*err = WTAP_ERR_SHORT_READ;
}
if (!wtap_read_bytes_or_eof(fh, hdr, (unsigned int)sizeof(t_5VW_TimeStamped_Header),
err, err_info))
return FALSE;
}

hdr->Key = pletoh32(&hdr->Key);
if (hdr->Key != CST_5VW_RECORDS_HEADER_KEY) {
Expand Down
25 changes: 6 additions & 19 deletions wiretap/aethra.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,15 @@ static gboolean aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hd

int aethra_open(wtap *wth, int *err, gchar **err_info)
{
int bytes_read;
struct aethra_hdr hdr;
struct tm tm;
aethra_t *aethra;

/* Read in the string that should be at the start of a "aethra" file */
errno = WTAP_ERR_CANT_READ;
bytes_read = file_read(hdr.magic, sizeof hdr.magic, wth->fh);
if (bytes_read != sizeof hdr.magic) {
*err = file_error(wth->fh, err_info);
if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
if (!wtap_read_bytes(wth->fh, hdr.magic, sizeof hdr.magic, err,
err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
Expand All @@ -141,14 +139,9 @@ int aethra_open(wtap *wth, int *err, gchar **err_info)

/* Read the rest of the header. */
errno = WTAP_ERR_CANT_READ;
bytes_read = file_read((char *)&hdr + sizeof hdr.magic,
sizeof hdr - sizeof hdr.magic, wth->fh);
if (bytes_read != sizeof hdr - sizeof hdr.magic) {
*err = file_error(wth->fh, err_info);
if (*err == 0)
*err = WTAP_ERR_SHORT_READ;
if (!wtap_read_bytes(wth->fh, (char *)&hdr + sizeof hdr.magic,
sizeof hdr - sizeof hdr.magic, err, err_info))
return -1;
}
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_AETHRA;
aethra = (aethra_t *)g_malloc(sizeof(aethra_t));
wth->priv = (void *)aethra;
Expand Down Expand Up @@ -303,20 +296,14 @@ aethra_read_rec_header(wtap *wth, FILE_T fh, struct aethrarec_hdr *hdr,
struct wtap_pkthdr *phdr, int *err, gchar **err_info)
{
aethra_t *aethra = (aethra_t *)wth->priv;
int bytes_read;
guint32 rec_size;
guint32 packet_size;
guint32 msecs;

/* Read record header. */
errno = WTAP_ERR_CANT_READ;
bytes_read = file_read(hdr, sizeof *hdr, fh);
if (bytes_read != sizeof *hdr) {
*err = file_error(fh, err_info);
if (*err == 0 && bytes_read != 0)
*err = WTAP_ERR_SHORT_READ;
if (!wtap_read_bytes_or_eof(fh, hdr, sizeof *hdr, err, err_info))
return FALSE;
}

rec_size = pletoh16(hdr->rec_size);
if (rec_size < (sizeof *hdr - sizeof hdr->rec_size)) {
Expand Down
7 changes: 2 additions & 5 deletions wiretap/ber.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ int ber_open(wtap *wth, int *err, gchar **err_info)
{
#define BER_BYTES_TO_CHECK 8
guint8 bytes[BER_BYTES_TO_CHECK];
int bytes_read;
guint8 ber_id;
gint8 ber_class;
gint8 ber_tag;
Expand All @@ -117,10 +116,8 @@ int ber_open(wtap *wth, int *err, gchar **err_info)
gint64 file_size;
int offset = 0, i;

bytes_read = file_read(&bytes, BER_BYTES_TO_CHECK, wth->fh);
if (bytes_read != BER_BYTES_TO_CHECK) {
*err = file_error(wth->fh, err_info);
if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
if (!wtap_read_bytes(wth->fh, &bytes, BER_BYTES_TO_CHECK, err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
Expand Down
22 changes: 4 additions & 18 deletions wiretap/btsnoop.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,15 @@ static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,

int btsnoop_open(wtap *wth, int *err, gchar **err_info)
{
int bytes_read;
char magic[sizeof btsnoop_magic];
struct btsnoop_hdr hdr;

int file_encap=WTAP_ENCAP_UNKNOWN;

/* Read in the string that should be at the start of a "btsnoop" file */
errno = WTAP_ERR_CANT_READ;
bytes_read = file_read(magic, sizeof magic, wth->fh);
if (bytes_read != sizeof magic) {
*err = file_error(wth->fh, err_info);
if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return -1;
return 0;
}
Expand All @@ -104,13 +101,8 @@ int btsnoop_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;
}

/*
* Make sure it's a version we support.
Expand Down Expand Up @@ -181,7 +173,6 @@ static gboolean btsnoop_seek_read(wtap *wth, gint64 seek_off,
static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
{
int bytes_read;
struct btsnooprec_hdr hdr;
guint32 packet_size;
guint32 flags;
Expand All @@ -191,13 +182,8 @@ static gboolean btsnoop_read_record(wtap *wth, FILE_T fh,
/* Read record header. */

errno = WTAP_ERR_CANT_READ;
bytes_read = file_read(&hdr, sizeof hdr, fh);
if (bytes_read != sizeof hdr) {
*err = file_error(fh, err_info);
if (*err == 0 && bytes_read != 0)
*err = WTAP_ERR_SHORT_READ;
if (!wtap_read_bytes_or_eof(fh, &hdr, sizeof hdr, err, err_info))
return FALSE;
}

packet_size = g_ntohl(hdr.incl_len);
orig_size = g_ntohl(hdr.orig_len);
Expand Down
33 changes: 7 additions & 26 deletions wiretap/camins.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,26 +107,6 @@ typedef enum {
#define DVB_CI_PSEUDO_HDR_HOST_TO_CAM 0xFE


/* read a block of data from the camins file and handle the errors */
static gboolean
read_block(FILE_T fh, guint8 *buf, guint16 buf_len, int *err, gchar **err_info)
{
int bytes_read;

bytes_read = file_read((void *)buf, buf_len, fh);
if (bytes_read != buf_len) {
*err = file_error(fh, err_info);
/* bytes_read==0 is end of file */
if (bytes_read>0 && *err == 0) {
*err = WTAP_ERR_SHORT_READ;
}
return FALSE;
}

return TRUE;
}


/* find the transaction type for the data bytes of the next packet
and the number of data bytes in that packet
the fd is moved such that it can be used in a subsequent call
Expand All @@ -146,7 +126,7 @@ find_next_pkt_dat_type_len(FILE_T fh,
RESET_STAT_VALS;

do {
if (read_block(fh, block, sizeof(block), err, err_info) == FALSE) {
if (!wtap_read_bytes_or_eof(fh, block, sizeof(block), err, err_info)) {
RESET_STAT_VALS;
return FALSE;
}
Expand Down Expand Up @@ -212,7 +192,7 @@ read_packet_data(FILE_T fh, guint8 dat_trans_type, guint8 *buf, guint16 dat_len,

p = buf;
while (bytes_count < dat_len) {
if (read_block(fh, block, sizeof(block), err, err_info) == FALSE)
if (!wtap_read_bytes_or_eof(fh, block, sizeof(block), err, err_info))
break;

if (block[1] == dat_trans_type) {
Expand Down Expand Up @@ -324,14 +304,15 @@ int camins_open(wtap *wth, int *err, gchar **err_info _U_)
guint8 found_start_blocks = 0;
guint8 count = 0;
guint8 block[2];
int bytes_read;

/* all CAM Inspector files I've looked at have at least two blocks of
0x00 0xE1 within the first 20 bytes */
do {
bytes_read = file_read(block, sizeof(block), wth->fh);
if (bytes_read != sizeof(block))
break;
if (!wtap_read_bytes(wth->fh, block, sizeof(block), err, err_info)) {
if (*err == WTAP_ERR_SHORT_READ)
break;
return -1;
}

if (block[0]==0x00 && block[1] == 0xE1)
found_start_blocks++;
Expand Down
54 changes: 36 additions & 18 deletions wiretap/commview.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,24 +208,42 @@ static gboolean
commview_read_header(commview_header_t *cv_hdr, FILE_T fh, int *err,
gchar **err_info)
{
wtap_file_read_expected_bytes(&cv_hdr->data_len, 2, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->source_data_len, 2, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->version, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->year, 2, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->month, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->day, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->hours, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->minutes, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->seconds, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->usecs, 4, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->flags, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->signal_level_percent, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->rate, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->band, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->channel, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->direction, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->signal_level_dbm, 1, fh, err, err_info);
wtap_file_read_expected_bytes(&cv_hdr->noise_level, 1, fh, err, err_info);
if (!wtap_read_bytes_or_eof(fh, &cv_hdr->data_len, 2, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->source_data_len, 2, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->version, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->year, 2, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->month, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->day, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->hours, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->minutes, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->seconds, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->usecs, 4, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->flags, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->signal_level_percent, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->rate, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->band, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->channel, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->direction, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->signal_level_dbm, 1, err, err_info))
return FALSE;
if (!wtap_read_bytes(fh, &cv_hdr->noise_level, 1, err, err_info))
return FALSE;

/* Convert multi-byte values from little endian to host endian format */
cv_hdr->data_len = GUINT16_FROM_LE(cv_hdr->data_len);
Expand Down
28 changes: 8 additions & 20 deletions wiretap/csids.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
Loading

0 comments on commit 670ebda

Please sign in to comment.