Only one buffer.c, please.

Otherwise, if you link with both libwiretap and libfiletap, it's
anybody's guess which one you get.  That means you're wasting memory
with two copies of its routines if they're identical, and means
surprising behavior if they're not (which showed up when I was debugging
a double-free crash - fixing libwiretap's buffer_free() didn't fix the
problem, because Wireshark happened to be calling libfiletap' unfixed
buffer_free()).

There's nothing *tap-specific about Buffers, anyway, so it really
belongs in wsutil.

Change-Id: I91537e46917e91277981f8f3365a2c0873152870
Reviewed-on: https://code.wireshark.org/review/3066
Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
Guy Harris 2014-07-15 16:40:46 -07:00
parent 61ac815681
commit d4dab16a3f
65 changed files with 58 additions and 295 deletions

View File

@ -22,7 +22,6 @@
include(UseABICheck)
set(CLEAN_FILES
buffer.c
file_access.c
ft_file_wrappers.c
ftap.c

View File

@ -27,14 +27,12 @@
# generated from YACC or Lex files (as Automake doesn't want them in
# _SOURCES variables).
NONGENERATED_C_FILES = \
buffer.c \
file_access.c \
ft_file_wrappers.c \
ftap.c
# Header files that are not generated from other files
NONGENERATED_HEADER_FILES = \
buffer.h \
ft_file_wrappers.h \
ftap.h \
ftap-int.h

View File

@ -38,7 +38,7 @@
#include "ftap-int.h"
#include "ft_file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
/*
* Add an extension, and all compressed versions thereof, to a GSList

View File

@ -39,7 +39,7 @@
#include "ft_file_wrappers.h"
#include <wsutil/file_util.h>
#include "buffer.h"
#include <wsutil/buffer.h>
#ifdef HAVE_PLUGINS

View File

@ -27,7 +27,7 @@
#include <glib.h>
#include <time.h>
#include <filetap/buffer.h>
#include <wsutil/buffer.h>
#include <wsutil/nstime.h>
#include "ws_symbol_export.h"

View File

@ -25,7 +25,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "5views.h"

View File

@ -28,7 +28,6 @@ set(CLEAN_FILES
atm.c
ber.c
btsnoop.c
buffer.c
camins.c
catapult_dct2000.c
commview.c

View File

@ -33,7 +33,6 @@ NONGENERATED_C_FILES = \
atm.c \
ber.c \
btsnoop.c \
buffer.c \
camins.c \
catapult_dct2000.c \
commview.c \
@ -90,7 +89,6 @@ NONGENERATED_HEADER_FILES = \
ascend-int.h \
atm.h \
ber.h \
buffer.h \
btsnoop.h \
camins.h \
catapult_dct2000.h \

View File

@ -23,7 +23,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "aethra.h"
/* Magic number in Aethra PC108 files. */

View File

@ -133,7 +133,7 @@ XMIT-Max7:20: (task "_brouterControlTask" at 0xb094ac20, time: 1481.51) 20 octet
#include <string.h>
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "ascendtext.h"
#include "ascend-int.h"
#include "file_wrappers.h"

View File

@ -20,7 +20,7 @@
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "ascendtext.h"
#include "ascend-int.h"
#include "file_wrappers.h"

View File

@ -27,7 +27,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "ber.h"

View File

@ -23,7 +23,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "atm.h"
#include "btsnoop.h"

View File

@ -1,162 +0,0 @@
/* buffer.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include "buffer.h"
/* Initializes a buffer with a certain amount of allocated space */
void
buffer_init(Buffer* buffer, gsize space)
{
buffer->data = (guint8*)g_malloc(space);
buffer->allocated = space;
buffer->start = 0;
buffer->first_free = 0;
}
/* Frees the memory used by a buffer */
void
buffer_free(Buffer* buffer)
{
g_free(buffer->data);
buffer->data = NULL;
}
/* Assures that there are 'space' bytes at the end of the used space
so that another routine can copy directly into the buffer space. After
doing that, the routine will also want to run
buffer_increase_length(). */
void
buffer_assure_space(Buffer* buffer, gsize space)
{
gsize available_at_end = buffer->allocated - buffer->first_free;
gsize space_used;
gboolean space_at_beginning;
/* If we've got the space already, good! */
if (space <= available_at_end) {
return;
}
/* Maybe we don't have the space available at the end, but we would
if we moved the used space back to the beginning of the
allocation. The buffer could have become fragmented through lots
of calls to buffer_remove_start(). I'm using buffer->start as the
same as 'available_at_start' in this comparison. */
/* or maybe there's just no more room. */
space_at_beginning = buffer->start >= space;
if (space_at_beginning || buffer->start > 0) {
space_used = buffer->first_free - buffer->start;
/* this memory copy better be safe for overlapping memory regions! */
memmove(buffer->data, buffer->data + buffer->start, space_used);
buffer->start = 0;
buffer->first_free = space_used;
}
/*if (buffer->start >= space) {*/
if (space_at_beginning) {
return;
}
/* We'll allocate more space */
buffer->allocated += space + 1024;
buffer->data = (guint8*)g_realloc(buffer->data, buffer->allocated);
}
void
buffer_append(Buffer* buffer, guint8 *from, gsize bytes)
{
buffer_assure_space(buffer, bytes);
memcpy(buffer->data + buffer->first_free, from, bytes);
buffer->first_free += bytes;
}
void
buffer_remove_start(Buffer* buffer, gsize bytes)
{
if (buffer->start + bytes > buffer->first_free) {
g_error("buffer_remove_start trying to remove %" G_GINT64_MODIFIER "u bytes. s=%" G_GINT64_MODIFIER "u ff=%" G_GINT64_MODIFIER "u!\n",
(guint64)bytes, (guint64)buffer->start,
(guint64)buffer->first_free);
/** g_error() does an abort() and thus never returns **/
}
buffer->start += bytes;
if (buffer->start == buffer->first_free) {
buffer->start = 0;
buffer->first_free = 0;
}
}
#ifndef SOME_FUNCTIONS_ARE_DEFINES
void
buffer_clean(Buffer* buffer)
{
buffer_remove_start(buffer, buffer_length(buffer));
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
void
buffer_increase_length(Buffer* buffer, gsize bytes)
{
buffer->first_free += bytes;
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
gsize
buffer_length(Buffer* buffer)
{
return buffer->first_free - buffer->start;
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
guint8 *
buffer_start_ptr(Buffer* buffer)
{
return buffer->data + buffer->start;
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
guint8 *
buffer_end_ptr(Buffer* buffer)
{
return buffer->data + buffer->first_free;
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
void
buffer_append_buffer(Buffer* buffer, Buffer* src_buffer)
{
buffer_append(buffer, buffer_start_ptr(src_buffer), buffer_length(src_buffer));
}
#endif

View File

@ -1,72 +0,0 @@
/* buffer.h
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef __W_BUFFER_H__
#define __W_BUFFER_H__
#include <glib.h>
#include "ws_symbol_export.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define SOME_FUNCTIONS_ARE_DEFINES
typedef struct Buffer {
guint8 *data;
gsize allocated;
gsize start;
gsize first_free;
} Buffer;
WS_DLL_PUBLIC
void buffer_init(Buffer* buffer, gsize space);
WS_DLL_PUBLIC
void buffer_free(Buffer* buffer);
WS_DLL_PUBLIC
void buffer_assure_space(Buffer* buffer, gsize space);
WS_DLL_PUBLIC
void buffer_append(Buffer* buffer, guint8 *from, gsize bytes);
WS_DLL_PUBLIC
void buffer_remove_start(Buffer* buffer, gsize bytes);
#ifdef SOME_FUNCTIONS_ARE_DEFINES
# define buffer_clean(buffer) buffer_remove_start((buffer), buffer_length(buffer))
# define buffer_increase_length(buffer,bytes) (buffer)->first_free += (bytes)
# define buffer_length(buffer) ((buffer)->first_free - (buffer)->start)
# define buffer_start_ptr(buffer) ((buffer)->data + (buffer)->start)
# define buffer_end_ptr(buffer) ((buffer)->data + (buffer)->first_free)
# define buffer_append_buffer(buffer,src_buffer) buffer_append((buffer), buffer_start_ptr(src_buffer), buffer_length(src_buffer))
#else
void buffer_clean(Buffer* buffer);
void buffer_increase_length(Buffer* buffer, unsigned int bytes);
unsigned int buffer_length(Buffer* buffer);
guint8* buffer_start_ptr(Buffer* buffer);
guint8* buffer_end_ptr(Buffer* buffer);
void buffer_append_buffer(Buffer* buffer, Buffer* src_buffer);
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif

View File

@ -64,7 +64,7 @@
#include <wtap.h>
#include <wtap-int.h>
#include <file_wrappers.h>
#include <buffer.h>
#include <wsutil/buffer.h>
#include "camins.h"

View File

@ -26,7 +26,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "catapult_dct2000.h"

View File

@ -38,7 +38,7 @@
#include "wtap.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "file_wrappers.h"
#include "commview.h"

View File

@ -23,7 +23,7 @@
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "cosine.h"
#include "file_wrappers.h"

View File

@ -20,7 +20,7 @@
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "csids.h"
#include "file_wrappers.h"

View File

@ -55,7 +55,7 @@
#include "wtap.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "file_wrappers.h"
#include "daintree-sna.h"

View File

@ -20,7 +20,7 @@
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "dbs-etherwatch.h"
#include "file_wrappers.h"

View File

@ -26,7 +26,7 @@
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "dct3trace.h"
#include "file_wrappers.h"

View File

@ -52,7 +52,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "pcap-encap.h"
#include "atm.h"
#include "erf.h"

View File

@ -20,7 +20,7 @@
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "eyesdn.h"
#include "file_wrappers.h"

View File

@ -38,7 +38,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "lanalyzer.h"
#include "ngsniffer.h"
#include "radcom.h"

View File

@ -21,7 +21,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "hcidump.h"
struct dump_hdr {

View File

@ -25,7 +25,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "i4b_trace.h"
#include "i4btrace.h"

View File

@ -65,7 +65,7 @@
#include <errno.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "libpcap.h"
#include "pcap-common.h"
#include "pcap-encap.h"

View File

@ -24,7 +24,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "atm.h"
#include "iptrace.h"

View File

@ -150,7 +150,7 @@ Number S/R Length Timer MAC Address MAC Address
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "iseries.h"
#include "file_wrappers.h"

View File

@ -31,7 +31,7 @@
#include "wtap-int.h"
#include "wtap.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "k12.h"
#include <wsutil/str_util.h>

View File

@ -75,7 +75,7 @@
#include "wtap-int.h"
#include "wtap.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "k12.h"
#include "k12text_lex.h"

View File

@ -23,7 +23,7 @@
#include <errno.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "lanalyzer.h"
/* The LANalyzer format is documented (at least in part) in Novell document

View File

@ -25,7 +25,7 @@
#include <errno.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "pcap-common.h"
#include "pcap-encap.h"
#include "libpcap.h"

View File

@ -24,7 +24,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "logcat.h"

View File

@ -44,7 +44,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "mime_file.h"
typedef struct {

View File

@ -33,7 +33,7 @@
#include "mp2t.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "file_wrappers.h"
#include <errno.h>
#include <stdlib.h>

View File

@ -34,7 +34,7 @@
#include "wsutil/mpeg-audio.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "file_wrappers.h"
#include <errno.h>
#include <stdlib.h>

View File

@ -23,7 +23,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "atm.h"
#include "pcap-encap.h"
#include "netmon.h"

View File

@ -23,7 +23,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "netscaler.h"
/* Defines imported from netscaler code: nsperfrc.h */

View File

@ -24,7 +24,7 @@
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "netscreen.h"
#include "file_wrappers.h"

View File

@ -28,7 +28,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "nettl.h"
/* HP nettl file header */

View File

@ -22,7 +22,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "network_instruments.h"
static const char network_instruments_magic[] = {"ObserverPktBufferVersion=15.00"};

View File

@ -25,7 +25,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "netxray.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "atm.h"
/* Capture file header, *including* magic number, is padded to 128 bytes. */

View File

@ -58,7 +58,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "atm.h"
#include "ngsniffer.h"

View File

@ -35,7 +35,7 @@
#include "wtap.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "file_wrappers.h"
#include "packetlogger.h"

View File

@ -37,7 +37,7 @@
#include "wtap-int.h"
#include <epan/addr_resolv.h>
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "libpcap.h"
#include "pcap-common.h"
#include "pcap-encap.h"

View File

@ -38,7 +38,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "peekclassic.h"
/* CREDITS
*

View File

@ -36,7 +36,7 @@
#include <stdlib.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "peektagged.h"
/* CREDITS

View File

@ -19,7 +19,7 @@
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "pppdump.h"
#include "file_wrappers.h"

View File

@ -24,7 +24,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "radcom.h"
struct frame_date {

View File

@ -23,7 +23,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "atm.h"
#include "snoop.h"
/* See RFC 1761 for a description of the "snoop" file format. */

View File

@ -30,7 +30,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "stanag4607.h"
typedef struct {

View File

@ -27,7 +27,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "tnef.h"
static gboolean tnef_read_file(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,

View File

@ -20,7 +20,7 @@
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "toshiba.h"
#include "file_wrappers.h"

View File

@ -25,7 +25,7 @@
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "visual.h"
/*

View File

@ -27,7 +27,7 @@
*/
#include "config.h"
#include "wtap-int.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "vms.h"
#include "file_wrappers.h"

View File

@ -26,7 +26,7 @@
#include "wtap-int.h"
#include "file_wrappers.h"
#include "buffer.h"
#include <wsutil/buffer.h>
#include "vwr.h"

View File

@ -39,7 +39,7 @@
#include "file_wrappers.h"
#include <wsutil/file_util.h>
#include "buffer.h"
#include <wsutil/buffer.h>
#ifdef HAVE_PLUGINS

View File

@ -27,7 +27,7 @@
#include <glib.h>
#include <time.h>
#include <wiretap/buffer.h>
#include <wsutil/buffer.h>
#include <wsutil/nstime.h>
#include "ws_symbol_export.h"

View File

@ -44,6 +44,7 @@ set(WSUTIL_FILES
airpdcap_wep.c
base64.c
bitswap.c
buffer.c
cfutils.c
clopts_common.c
cmdarg_err.c

View File

@ -32,6 +32,7 @@ LIBWSUTIL_SRC = \
airpdcap_wep.c \
base64.c \
bitswap.c \
buffer.c \
cfutils.c \
clopts_common.c \
cmdarg_err.c \
@ -83,6 +84,7 @@ LIBWSUTIL_INCLUDES = \
bits_ctz.h \
bits_count_ones.h \
bitswap.h \
buffer.h \
cfutils.h \
clopts_common.h \
cmdarg_err.h \