Keep in the "wtap" structure the current offset into the file being

read, and maintain it ourselves as we read through the file, rather than
calling "ftell()" for every packet we read - "ftell()" may involve an
"lseek()" call, which could add a noticeable CPU overhead when reading a
large file.

svn path=/trunk/; revision=596
This commit is contained in:
Guy Harris 1999-08-28 01:19:45 +00:00
parent f7951bd593
commit ae53260d02
10 changed files with 82 additions and 25 deletions

View File

@ -1,6 +1,6 @@
/* file.c
*
* $Id: file.c,v 1.18 1999/08/22 02:29:38 guy Exp $
* $Id: file.c,v 1.19 1999/08/28 01:19:43 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -105,6 +105,7 @@ wtap* wtap_open_offline(const char *filename, int *err)
/* initialization */
wth->file_encap = WTAP_ENCAP_UNKNOWN;
wth->data_offset = 0;
/* Try all file types */
for (i = 0; i < N_FILE_TYPES; i++) {

View File

@ -1,6 +1,6 @@
/* iptrace.c
*
* $Id: iptrace.c,v 1.9 1999/08/24 03:19:34 guy Exp $
* $Id: iptrace.c,v 1.10 1999/08/28 01:19:43 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -39,6 +39,7 @@ int iptrace_open(wtap *wth, int *err)
char name[12];
fseek(wth->fh, 0, SEEK_SET);
wth->data_offset = 0;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(name, 1, 11, wth->fh);
if (bytes_read != 11) {
@ -48,6 +49,7 @@ int iptrace_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += 11;
name[11] = 0;
if (strcmp(name, "iptrace 2.0") != 0) {
return 0;
@ -81,12 +83,13 @@ static int iptrace_read(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += 40;
packet_size = pntohs(&header[2]) - 32;
/* Read the packet data */
buffer_assure_space(wth->frame_buffer, packet_size);
data_offset = ftell(wth->fh);
data_offset = wth->data_offset;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
packet_size, wth->fh);
@ -98,6 +101,7 @@ static int iptrace_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += packet_size;
wth->phdr.len = packet_size;
wth->phdr.caplen = packet_size;

View File

@ -1,6 +1,6 @@
/* lanalyzer.c
*
* $Id: lanalyzer.c,v 1.13 1999/08/22 02:29:40 guy Exp $
* $Id: lanalyzer.c,v 1.14 1999/08/28 01:19:43 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -61,6 +61,7 @@ int lanalyzer_open(wtap *wth, int *err)
struct tm tm;
fseek(wth->fh, 0, SEEK_SET);
wth->data_offset = 0;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(LE_record_type, 1, 2, wth->fh);
bytes_read += fread(LE_record_length, 1, 2, wth->fh);
@ -71,6 +72,7 @@ int lanalyzer_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += 4;
record_type = pletohs(LE_record_type);
record_length = pletohs(LE_record_length); /* make sure to do this for while() loop */
@ -89,6 +91,7 @@ int lanalyzer_open(wtap *wth, int *err)
/* Read records until we find the start of packets */
while (1) {
fseek(wth->fh, record_length, SEEK_CUR);
wth->data_offset += record_length;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(LE_record_type, 1, 2, wth->fh);
bytes_read += fread(LE_record_length, 1, 2, wth->fh);
@ -101,6 +104,7 @@ int lanalyzer_open(wtap *wth, int *err)
g_free(wth->capture.lanalyzer);
return 0;
}
wth->data_offset += 4;
record_type = pletohs(LE_record_type);
record_length = pletohs(LE_record_length);
@ -121,6 +125,7 @@ int lanalyzer_open(wtap *wth, int *err)
g_free(wth->capture.lanalyzer);
return 0;
}
wth->data_offset += sizeof summary;
/* Assume that the date of the creation of the trace file
* is the same date of the trace. Lanalyzer doesn't
@ -176,6 +181,7 @@ int lanalyzer_open(wtap *wth, int *err)
/* Go back header number ob ytes so that lanalyzer_read
* can read this header */
fseek(wth->fh, -bytes_read, SEEK_CUR);
wth->data_offset -= bytes_read;
return 1;
default:
@ -217,6 +223,7 @@ static int lanalyzer_read(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += 2;
bytes_read = fread(LE_record_length, 1, 2, wth->fh);
if (bytes_read != 2) {
if (ferror(wth->fh))
@ -225,6 +232,7 @@ static int lanalyzer_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += 2;
record_type = pletohs(LE_record_type);
record_length = pletohs(LE_record_length);
@ -252,10 +260,11 @@ static int lanalyzer_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += DESCRIPTOR_LEN;
/* Read the packet data */
buffer_assure_space(wth->frame_buffer, packet_size);
data_offset = ftell(wth->fh);
data_offset = wth->data_offset;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
packet_size, wth->fh);
@ -267,6 +276,7 @@ static int lanalyzer_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += packet_size;
true_size = pletohs(&descriptor[4]);
time_low = pletohs(&descriptor[8]);

View File

@ -1,6 +1,6 @@
/* libpcap.c
*
* $Id: libpcap.c,v 1.15 1999/08/24 03:19:34 guy Exp $
* $Id: libpcap.c,v 1.16 1999/08/28 01:19:44 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -147,6 +147,7 @@ int libpcap_open(wtap *wth, int *err)
/* Read in the number that should be at the start of a "libpcap" file */
fseek(wth->fh, 0, SEEK_SET);
wth->data_offset = 0;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(&magic, 1, sizeof magic, wth->fh);
if (bytes_read != sizeof magic) {
@ -156,6 +157,7 @@ int libpcap_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += sizeof magic;
if (magic == PCAP_SWAPPED_MAGIC) {
/* Host that wrote it has a byte order opposite to ours. */
@ -176,6 +178,7 @@ int libpcap_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += sizeof hdr;
if (byte_swapped) {
/* Byte-swap the header fields about which we care. */
@ -233,6 +236,7 @@ static int libpcap_read(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += sizeof hdr;
if (wth->capture.pcap->byte_swapped) {
/* Byte-swap the record header fields. */
@ -274,7 +278,7 @@ static int libpcap_read(wtap *wth, int *err)
}
buffer_assure_space(wth->frame_buffer, packet_size);
data_offset = ftell(wth->fh);
data_offset = wth->data_offset;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
packet_size, wth->fh);
@ -286,6 +290,7 @@ static int libpcap_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += packet_size;
wth->phdr.ts.tv_sec = hdr.ts_sec;
wth->phdr.ts.tv_usec = hdr.ts_usec;

View File

@ -1,6 +1,6 @@
/* netmon.c
*
* $Id: netmon.c,v 1.12 1999/08/24 03:19:33 guy Exp $
* $Id: netmon.c,v 1.13 1999/08/28 01:19:44 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -214,6 +214,7 @@ int netmon_open(wtap *wth, int *err)
/* Seek to the beginning of the data records. */
fseek(wth->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET);
wth->data_offset = CAPTUREFILE_HEADER_SIZE;
return 1;
}
@ -234,13 +235,11 @@ static int netmon_read(wtap *wth, int *err)
double t;
/* Have we reached the end of the packet data? */
data_offset = ftell(wth->fh);
if (data_offset >= wth->capture.netmon->end_offset) {
if (wth->data_offset >= wth->capture.netmon->end_offset) {
/* Yes. */
return 0;
}
/* Read record header. */
/* Read record header. */
switch (wth->capture.netmon->version_major) {
case 1:
@ -264,7 +263,7 @@ static int netmon_read(wtap *wth, int *err)
}
return 0;
}
data_offset += hdr_size;
wth->data_offset += hdr_size;
switch (wth->capture.netmon->version_major) {
@ -287,6 +286,7 @@ static int netmon_read(wtap *wth, int *err)
return -1;
}
buffer_assure_space(wth->frame_buffer, packet_size);
data_offset = wth->data_offset;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
packet_size, wth->fh);
@ -298,6 +298,7 @@ static int netmon_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += packet_size;
t = (double)wth->capture.netmon->start_usecs;
switch (wth->capture.netmon->version_major) {

View File

@ -1,6 +1,6 @@
/* netxray.c
*
* $Id: netxray.c,v 1.12 1999/08/24 03:19:33 guy Exp $
* $Id: netxray.c,v 1.13 1999/08/28 01:19:44 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -116,6 +116,7 @@ int netxray_open(wtap *wth, int *err)
/* Read in the string that should be at the start of a NetXRay
* file */
fseek(wth->fh, 0, SEEK_SET);
wth->data_offset = 0;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(magic, 1, sizeof magic, wth->fh);
if (bytes_read != sizeof magic) {
@ -125,6 +126,7 @@ int netxray_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += sizeof magic;
if (memcmp(magic, netxray_magic, sizeof netxray_magic) != 0) {
return 0;
@ -140,6 +142,7 @@ int netxray_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += sizeof hdr;
/* It appears that version 1.1 files (as produced by Windows
* Sniffer Pro 2.0.01) have the time stamp in microseconds,
@ -199,6 +202,7 @@ int netxray_open(wtap *wth, int *err)
/* Seek to the beginning of the data records. */
fseek(wth->fh, pletohl(&hdr.start_offset), SEEK_SET);
wth->data_offset = pletohl(&hdr.start_offset);
return 1;
}
@ -218,8 +222,7 @@ static int netxray_read(wtap *wth, int *err)
reread:
/* Have we reached the end of the packet data? */
data_offset = ftell(wth->fh);
if (data_offset == wth->capture.netxray->end_offset) {
if (wth->data_offset == wth->capture.netxray->end_offset) {
/* Yes. */
return 0;
}
@ -251,16 +254,18 @@ reread:
/* Yes. Remember that we did. */
wth->capture.netxray->wrapped = 1;
fseek(wth->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET);
wth->data_offset = CAPTUREFILE_HEADER_SIZE;
goto reread;
}
/* We've already wrapped - don't wrap again. */
return 0;
}
data_offset += hdr_size;
wth->data_offset += hdr_size;
packet_size = pletohs(&hdr.hdr_1_x.incl_len);
buffer_assure_space(wth->frame_buffer, packet_size);
data_offset = wth->data_offset;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
packet_size, wth->fh);
@ -272,6 +277,7 @@ reread:
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += packet_size;
t = (double)pletohl(&hdr.hdr_1_x.timelo)
+ (double)pletohl(&hdr.hdr_1_x.timehi)*4294967296.0;

View File

@ -1,6 +1,6 @@
/* ngsniffer.c
*
* $Id: ngsniffer.c,v 1.20 1999/08/24 03:19:32 guy Exp $
* $Id: ngsniffer.c,v 1.21 1999/08/28 01:19:44 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -269,6 +269,7 @@ int ngsniffer_open(wtap *wth, int *err)
/* Read in the string that should be at the start of a Sniffer file */
fseek(wth->fh, 0, SEEK_SET);
wth->data_offset = 0;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(magic, 1, 17, wth->fh);
if (bytes_read != 17) {
@ -278,6 +279,7 @@ int ngsniffer_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += 17;
magic[17] = 0;
@ -299,6 +301,7 @@ int ngsniffer_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += 6;
type = pletohs(record_type);
length = pletohs(record_length);
@ -318,6 +321,7 @@ int ngsniffer_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += sizeof version;
/* Make sure this is an uncompressed Sniffer file */
if (version.format != 1) {
@ -416,6 +420,7 @@ static int ngsniffer_read(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += 2;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(record_length, 1, 4, wth->fh);
if (bytes_read != 4) {
@ -425,6 +430,7 @@ static int ngsniffer_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += 4;
type = pletohs(record_type);
length = pletohs(record_length);
@ -452,6 +458,7 @@ static int ngsniffer_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += sizeof frame2;
time_low = pletohs(&frame2.time_low);
time_med = pletohs(&frame2.time_med);
time_high = pletohs(&frame2.time_high);
@ -488,6 +495,7 @@ static int ngsniffer_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += sizeof frame4;
time_low = pletohs(&frame4.time_low);
time_med = pletohs(&frame4.time_med);
time_high = frame4.time_high;
@ -539,6 +547,7 @@ static int ngsniffer_read(wtap *wth, int *err)
* portion, and keep looping.
*/
fseek(wth->fh, length, SEEK_CUR);
wth->data_offset += length;
}
found:
@ -549,7 +558,7 @@ found:
* Read the packet data.
*/
buffer_assure_space(wth->frame_buffer, length);
data_offset = ftell(wth->fh);
data_offset = wth->data_offset;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
length, wth->fh);
@ -561,6 +570,7 @@ found:
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += length;
t = t/1000000.0 * wth->capture.ngsniffer->timeunit; /* t = # of secs */
t += wth->capture.ngsniffer->start;

View File

@ -165,10 +165,13 @@ int radcom_open(wtap *wth, int *err)
}
}*/
if (wth->file_encap == WTAP_ENCAP_ETHERNET)
if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
fseek(wth->fh, 294, SEEK_CUR);
else if (wth->file_encap == WTAP_ENCAP_LAPB)
wth->data_offset = 294;
} else if (wth->file_encap == WTAP_ENCAP_LAPB) {
fseek(wth->fh, 297, SEEK_CUR);
wth->data_offset = 297;
}
return 1;
@ -193,6 +196,7 @@ static int radcom_read(wtap *wth, int *err)
char dce;
fseek(wth->fh, 4, SEEK_CUR);
wth->data_offset += 4;
/*
* Read the frame size
@ -210,6 +214,7 @@ static int radcom_read(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += 2;
if (wth->file_encap == WTAP_ENCAP_LAPB)
length -= 2; /* FCS */
@ -218,6 +223,7 @@ static int radcom_read(wtap *wth, int *err)
wth->phdr.caplen = length;
fseek(wth->fh, 5, SEEK_CUR);
wth->data_offset += 5;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(&date, 1, sizeof(struct frame_date), wth->fh);
if (bytes_read != sizeof(struct frame_date)) {
@ -227,6 +233,7 @@ static int radcom_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += sizeof(struct frame_date);
tm.tm_year = date.year-1900;
tm.tm_mon = date.month-1;
@ -239,6 +246,7 @@ static int radcom_read(wtap *wth, int *err)
wth->phdr.ts.tv_usec = date.usec;
fseek(wth->fh, 6, SEEK_CUR);
wth->data_offset += 6;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(&dce, 1, 1, wth->fh);
if (bytes_read != 1) {
@ -248,6 +256,7 @@ static int radcom_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += 1;
wth->phdr.pseudo_header.x25.flags = (dce & 0x1) ? 0x00 : 0x80;
fseek(wth->fh, 9, SEEK_CUR);
@ -256,7 +265,7 @@ static int radcom_read(wtap *wth, int *err)
* Read the packet data.
*/
buffer_assure_space(wth->frame_buffer, length);
data_offset = ftell(wth->fh);
data_offset = wth->data_offset;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
length, wth->fh);
@ -268,11 +277,14 @@ static int radcom_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += length;
wth->phdr.pkt_encap = wth->file_encap;
if (wth->file_encap == WTAP_ENCAP_LAPB)
if (wth->file_encap == WTAP_ENCAP_LAPB) {
fseek(wth->fh, 2, SEEK_CUR); /* FCS */
wth->data_offset += 2;
}
return data_offset;
}

View File

@ -1,6 +1,6 @@
/* snoop.c
*
* $Id: snoop.c,v 1.8 1999/08/24 03:19:32 guy Exp $
* $Id: snoop.c,v 1.9 1999/08/28 01:19:45 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -77,6 +77,7 @@ int snoop_open(wtap *wth, int *err)
/* Read in the string that should be at the start of a "snoop" file */
fseek(wth->fh, 0, SEEK_SET);
wth->data_offset = 0;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(magic, 1, sizeof magic, wth->fh);
if (bytes_read != sizeof magic) {
@ -86,6 +87,7 @@ int snoop_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += sizeof magic;
if (memcmp(magic, snoop_magic, sizeof snoop_magic) != 0) {
return 0;
@ -101,6 +103,7 @@ int snoop_open(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += sizeof hdr;
hdr.version = ntohl(hdr.version);
if (hdr.version != 2) {
@ -148,6 +151,7 @@ static int snoop_read(wtap *wth, int *err)
}
return 0;
}
wth->data_offset += sizeof hdr;
packet_size = ntohl(hdr.incl_len);
if (packet_size > WTAP_MAX_PACKET_SIZE) {
@ -161,7 +165,7 @@ static int snoop_read(wtap *wth, int *err)
return -1;
}
buffer_assure_space(wth->frame_buffer, packet_size);
data_offset = ftell(wth->fh);
data_offset = wth->data_offset;
errno = WTAP_ERR_CANT_READ;
bytes_read = fread(buffer_start_ptr(wth->frame_buffer), 1,
packet_size, wth->fh);
@ -173,6 +177,7 @@ static int snoop_read(wtap *wth, int *err)
*err = WTAP_ERR_SHORT_READ;
return -1;
}
wth->data_offset += packet_size;
wth->phdr.ts.tv_sec = ntohl(hdr.ts_sec);
wth->phdr.ts.tv_usec = ntohl(hdr.ts_usec);
@ -183,6 +188,7 @@ static int snoop_read(wtap *wth, int *err)
/* Skip over the padding. */
fseek(wth->fh, ntohl(hdr.rec_len) - (sizeof hdr + packet_size),
SEEK_CUR);
wth->data_offset += ntohl(hdr.rec_len) - (sizeof hdr + packet_size);
return data_offset;
}

View File

@ -1,6 +1,6 @@
/* wtap.h
*
* $Id: wtap.h,v 1.34 1999/08/24 03:19:34 guy Exp $
* $Id: wtap.h,v 1.35 1999/08/28 01:19:45 guy Exp $
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
@ -261,6 +261,8 @@ typedef struct wtap {
struct Buffer *frame_buffer;
struct wtap_pkthdr phdr;
long data_offset;
union {
libpcap_t *pcap;
lanalyzer_t *lanalyzer;