From beroset:

implemented wtap_dump_file_seek() and _tell()

implemented the previously declared but unimplemented wtap_dump_file_seek() and wtap_dump_file_tell() functions and used them in the seven files that had previously used a plain ftell or fseek and added error checking as appropriate.  I also added a new error WTAP_ERR_CANT_SEEK_COMPRESSED and put it next to WTAP_ERR_CANT_SEEK causing renumbering of two of the existing error codes.

https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=8416

svn path=/trunk/; revision=48348
This commit is contained in:
Anders Broman 2013-03-17 09:20:13 +00:00
parent 2e52e2ac99
commit 05a8c94ddf
10 changed files with 88 additions and 54 deletions

View File

@ -381,10 +381,8 @@ gboolean _5views_dump_open(wtap_dumper *wdh, int *err)
haven't yet written any packets. As we'll have to rewrite
the header when we've written out all the packets, we just
skip over the header for now. */
if (fseek(wdh->fh, sizeof(t_5VW_Capture_Header), SEEK_SET) == -1) {
*err = errno;
if (wtap_dump_file_seek(wdh, sizeof(t_5VW_Capture_Header), SEEK_SET, err) == -1)
return FALSE;
}
/* This is a 5Views file */
wdh->subtype_write = _5views_dump;
@ -439,10 +437,8 @@ static gboolean _5views_dump_close(wtap_dumper *wdh, int *err)
_5views_dump_t *_5views = (_5views_dump_t *)wdh->priv;
t_5VW_Capture_Header file_hdr;
if (fseek(wdh->fh, 0, SEEK_SET) == -1) {
*err = errno;
if (wtap_dump_file_seek(wdh, 0, SEEK_SET, err) == -1)
return FALSE;
}
/* fill in the Info_Header */
file_hdr.Info_Header.Signature = htolel(CST_5VW_INFO_HEADER_KEY);

View File

@ -1513,3 +1513,41 @@ static int wtap_dump_file_close(wtap_dumper *wdh)
return fclose((FILE *)wdh->fh);
}
}
gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err)
{
#ifdef HAVE_LIBZ
if(wdh->compressed) {
*err = WTAP_ERR_CANT_SEEK_COMPRESSED;
return -1;
} else
#endif
{
if (-1 == fseek((FILE *)wdh->fh, (long)offset, whence)) {
*err = errno;
return -1;
} else
{
return 0;
}
}
}
gint64 wtap_dump_file_tell(wtap_dumper *wdh, int *err)
{
gint64 rval;
#ifdef HAVE_LIBZ
if(wdh->compressed) {
*err = WTAP_ERR_CANT_SEEK_COMPRESSED;
return -1;
} else
#endif
{
if (-1 == (rval = ftell((FILE *)wdh->fh))) {
*err = errno;
return -1;
} else
{
return rval;
}
}
}

View File

@ -466,7 +466,7 @@ static gboolean k12_read(wtap *wth, int *err, gchar **err_info, gint64 *data_off
buffer_assure_space(&(k12->extra_info), extra_len);
memcpy(buffer_start_ptr(&(k12->extra_info)),
buffer + K12_PACKET_FRAME + wth->phdr.caplen, extra_len);
wth->phdr.pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(k12->extra_info));
wth->phdr.pseudo_header.k12.extra_info = (guint8*)buffer_start_ptr(&(k12->extra_info));
wth->phdr.pseudo_header.k12.extra_length = extra_len;
wth->phdr.pseudo_header.k12.input = src_id;
@ -531,10 +531,10 @@ static gboolean k12_seek_read(wtap *wth, gint64 seek_off, struct wtap_pkthdr *ph
buffer_assure_space(&(k12->extra_info), extra_len);
memcpy(buffer_start_ptr(&(k12->extra_info)),
buffer + K12_PACKET_FRAME + length, extra_len);
wth->phdr.pseudo_header.k12.extra_info = (void*)buffer_start_ptr(&(k12->extra_info));
wth->phdr.pseudo_header.k12.extra_info = (guint8*)buffer_start_ptr(&(k12->extra_info));
wth->phdr.pseudo_header.k12.extra_length = extra_len;
if (pseudo_header) {
pseudo_header->k12.extra_info = (void*)buffer_start_ptr(&(k12->extra_info));
pseudo_header->k12.extra_info = (guint8*)buffer_start_ptr(&(k12->extra_info));
pseudo_header->k12.extra_length = extra_len;
}
@ -1042,10 +1042,8 @@ static gboolean k12_dump_close(wtap_dumper *wdh, int *err) {
if (! wtap_dump_file_write(wdh, k12_eof, 2, err))
return FALSE;
if (fseek(wdh->fh, 8, SEEK_SET) == -1) {
*err = errno;
if (wtap_dump_file_seek(wdh, 8, SEEK_SET, err) == -1)
return FALSE;
}
d.u = g_htonl(k12->file_len);
@ -1068,10 +1066,8 @@ gboolean k12_dump_open(wtap_dumper *wdh, int *err) {
return FALSE;
}
if (fseek(wdh->fh, 0x200, SEEK_SET) == -1) {
*err = errno;
if (wtap_dump_file_seek(wdh, 0x200, SEEK_SET, err) == -1)
return FALSE;
}
wdh->subtype_write = k12_dump;
wdh->subtype_close = k12_dump_close;

View File

@ -261,6 +261,13 @@ static const guint8 LA_CyclicInformationFake[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
static const guint8 z64[64] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
typedef struct {
time_t start;
} lanalyzer_t;
@ -617,7 +624,6 @@ static gboolean lanalyzer_seek_read(wtap *wth, gint64 seek_off,
*---------------------------------------------------*/
static gboolean s0write(wtap_dumper *wdh, size_t cnt, int *err)
{
static const guint8 z64[64];
size_t snack;
while (cnt) {
@ -808,10 +814,9 @@ gboolean lanalyzer_dump_open(wtap_dumper *wdh, int *err)
+ sizeof (LA_CyclicInformationFake)
+ LA_IndexRecordSize;
if (fseek(wdh->fh, jump, SEEK_SET) == -1) {
*err = errno;
if (wtap_dump_file_seek(wdh, jump, SEEK_SET, err) == -1)
return FALSE;
}
wdh->bytes_dumped = jump;
return TRUE;
}
@ -839,7 +844,8 @@ static gboolean lanalyzer_dump_header(wtap_dumper *wdh, int *err)
if (fT == NULL)
return FALSE;
fseek(wdh->fh, 0, SEEK_SET);
if (wtap_dump_file_seek(wdh, 0, SEEK_SET, err) == -1)
return FALSE;
if (!wtap_dump_file_write(wdh, &LA_HeaderRegularFake,
sizeof LA_HeaderRegularFake, err))

View File

@ -983,10 +983,8 @@ gboolean netmon_dump_open(wtap_dumper *wdh, int *err)
haven't yet written any packets. As we'll have to rewrite
the header when we've written out all the packets, we just
skip over the header for now. */
if (fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET) == -1) {
*err = errno;
if (wtap_dump_file_seek(wdh, CAPTUREFILE_HEADER_SIZE, SEEK_SET, err) == -1)
return FALSE;
}
wdh->subtype_write = netmon_dump;
wdh->subtype_close = netmon_dump_close;
@ -1234,7 +1232,8 @@ static gboolean netmon_dump_close(wtap_dumper *wdh, int *err)
return FALSE;
/* Now go fix up the file header. */
fseek(wdh->fh, 0, SEEK_SET);
if (wtap_dump_file_seek(wdh, 0, SEEK_SET, err) == -1)
return FALSE;
memset(&file_hdr, '\0', sizeof file_hdr);
switch (wdh->file_type) {

View File

@ -1415,11 +1415,8 @@ static gboolean nstrace_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
if (nstrace->page_offset + phdr->caplen >= nstrace->page_len)
{
/* Start on the next page */
if (fseek(wdh->fh, (nstrace->page_len - nstrace->page_offset), SEEK_CUR) == -1)
{
*err = errno;
if (wtap_dump_file_seek(wdh, (nstrace->page_len - nstrace->page_offset), SEEK_CUR, err) == -1)
return FALSE;
}
nstrace->page_offset = 0;
@ -1456,11 +1453,8 @@ static gboolean nstrace_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
if (nstrace->page_offset + phdr->caplen >= nstrace->page_len)
{
/* Start on the next page */
if (fseek(wdh->fh, (nstrace->page_len - nstrace->page_offset), SEEK_CUR) == -1)
{
*err = errno;
if (wtap_dump_file_seek(wdh, (nstrace->page_len - nstrace->page_offset), SEEK_CUR, err) == -1)
return FALSE;
}
nstrace->page_offset = 0;

View File

@ -1520,10 +1520,8 @@ gboolean netxray_dump_open_1_1(wtap_dumper *wdh, int *err)
haven't yet written any packets. As we'll have to rewrite
the header when we've written out all the packets, we just
skip over the header for now. */
if (fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET) == -1) {
*err = errno;
if (wtap_dump_file_seek(wdh, CAPTUREFILE_HEADER_SIZE, SEEK_SET, err) == -1)
return FALSE;
}
wdh->bytes_dumped += CAPTUREFILE_HEADER_SIZE;
netxray = (netxray_dump_t *)g_malloc(sizeof(netxray_dump_t));
@ -1593,13 +1591,15 @@ static gboolean netxray_dump_close_1_1(wtap_dumper *wdh, int *err)
{
char hdr_buf[CAPTUREFILE_HEADER_SIZE - sizeof(netxray_magic)];
netxray_dump_t *netxray = (netxray_dump_t *)wdh->priv;
guint32 filelen;
gint64 filelen;
struct netxray_hdr file_hdr;
filelen = (guint32)ftell(wdh->fh); /* XXX - large files? */
if (-1 == (filelen = wtap_dump_file_tell(wdh, err)))
return FALSE;
/* Go back to beginning */
fseek(wdh->fh, 0, SEEK_SET);
if (wtap_dump_file_seek(wdh, 0, SEEK_SET, err) == -1)
return FALSE;
/* Rewrite the file header. */
if (!wtap_dump_file_write(wdh, netxray_magic, sizeof netxray_magic, err))
@ -1611,7 +1611,8 @@ static gboolean netxray_dump_close_1_1(wtap_dumper *wdh, int *err)
file_hdr.start_time = htolel(netxray->start.secs);
file_hdr.nframes = htolel(netxray->nframes);
file_hdr.start_offset = htolel(CAPTUREFILE_HEADER_SIZE);
file_hdr.end_offset = htolel(filelen);
/* XXX - large files? */
file_hdr.end_offset = htolel((guint32)filelen);
file_hdr.network = wtap_encap_to_netxray_1_1_encap(wdh->encap);
file_hdr.timelo = htolel(0);
file_hdr.timehi = htolel(0);
@ -1679,10 +1680,9 @@ gboolean netxray_dump_open_2_0(wtap_dumper *wdh, int *err)
haven't yet written any packets. As we'll have to rewrite
the header when we've written out all the packets, we just
skip over the header for now. */
if (fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET) == -1) {
*err = errno;
if (wtap_dump_file_seek(wdh, CAPTUREFILE_HEADER_SIZE, SEEK_SET, err) == -1)
return FALSE;
}
wdh->bytes_dumped += CAPTUREFILE_HEADER_SIZE;
netxray = (netxray_dump_t *)g_malloc(sizeof(netxray_dump_t));
@ -1771,13 +1771,15 @@ static gboolean netxray_dump_close_2_0(wtap_dumper *wdh, int *err)
{
char hdr_buf[CAPTUREFILE_HEADER_SIZE - sizeof(netxray_magic)];
netxray_dump_t *netxray = (netxray_dump_t *)wdh->priv;
guint32 filelen;
gint64 filelen;
struct netxray_hdr file_hdr;
filelen = (guint32)ftell(wdh->fh); /* XXX - large files? */
if (-1 == (filelen = wtap_dump_file_tell(wdh, err)))
return FALSE;
/* Go back to beginning */
fseek(wdh->fh, 0, SEEK_SET);
if (wtap_dump_file_seek(wdh, 0, SEEK_SET, err) == -1)
return FALSE;
/* Rewrite the file header. */
if (!wtap_dump_file_write(wdh, netxray_magic, sizeof netxray_magic, err))
@ -1789,7 +1791,8 @@ static gboolean netxray_dump_close_2_0(wtap_dumper *wdh, int *err)
file_hdr.start_time = htolel(netxray->start.secs);
file_hdr.nframes = htolel(netxray->nframes);
file_hdr.start_offset = htolel(CAPTUREFILE_HEADER_SIZE);
file_hdr.end_offset = htolel(filelen);
/* XXX - large files? */
file_hdr.end_offset = htolel((guint32)filelen);
file_hdr.network = wtap_encap_to_netxray_2_0_encap(wdh->encap);
file_hdr.timelo = htolel(0);
file_hdr.timehi = htolel(0);

View File

@ -684,10 +684,8 @@ gboolean visual_dump_open(wtap_dumper *wdh, int *err)
/* All of the fields in the file header aren't known yet so
just skip over it for now. It will be created after all
of the packets have been written. */
if (fseek(wdh->fh, CAPTUREFILE_HEADER_SIZE, SEEK_SET) == -1) {
*err = errno;
if (wtap_dump_file_seek(wdh, CAPTUREFILE_HEADER_SIZE, SEEK_SET, err) == -1)
return FALSE;
}
return TRUE;
}
@ -834,7 +832,8 @@ static gboolean visual_dump_close(wtap_dumper *wdh, int *err)
}
/* Write the magic number at the start of the file. */
fseek(wdh->fh, 0, SEEK_SET);
if (wtap_dump_file_seek(wdh, 0, SEEK_SET, err) == -1)
return FALSE;
magicp = visual_magic;
magic_size = sizeof visual_magic;
if (!wtap_dump_file_write(wdh, magicp, magic_size, err))

View File

@ -115,8 +115,8 @@ struct wtap_dumper {
gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
size_t bufsize, int *err);
extern gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err);
extern gint64 wtap_dump_file_tell(wtap_dumper *wdh);
gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err);
gint64 wtap_dump_file_tell(wtap_dumper *wdh, int *err);
extern gint wtap_num_file_types;

View File

@ -1392,10 +1392,13 @@ int wtap_register_encap_type(const char* name, const char* short_name);
#define WTAP_ERR_CANT_SEEK -20
/** An attempt to seek failed, reason unknown */
#define WTAP_ERR_DECOMPRESS -21
#define WTAP_ERR_CANT_SEEK_COMPRESSED -21
/** An attempt to seek on a compressed stream */
#define WTAP_ERR_DECOMPRESS -22
/** Error decompressing */
#define WTAP_ERR_INTERNAL -22
#define WTAP_ERR_INTERNAL -23
/** "Shouldn't happen" internal errors */
#ifdef __cplusplus