Move the new files to the same places as in automake.
[obnox/wireshark/wip.git] / wiretap / buffer.c
index d796858f5e002c2bf3318c74e573400e275486e7..bd5a94e3a665e7d16328351967c7dc46bdcf9c4a 100644 (file)
@@ -1,41 +1,40 @@
 /* buffer.c
  *
- * $Id: buffer.c,v 1.2 1998/11/12 06:01:18 gram Exp $
+ * $Id$
  *
  * Wiretap Library
- * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
- * 
+ * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  *
  */
-
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
 
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <glib.h>
 
 #include "buffer.h"
 
-/*#define DEBUG*/
-#define DEBUG_PROGRAM_NAME "buffer.c"
-#include "debug.h"
-
 /* Initializes a buffer with a certain amount of allocated space */
-void buffer_init(Buffer* buffer, unsigned int space)
+void buffer_init(Buffer* buffer, gsize space)
 {
-       debug("buffer_init\n");
-       buffer->data = (char*)g_malloc(space);
+       buffer->data = (guint8*)g_malloc(space);
        buffer->allocated = space;
        buffer->start = 0;
        buffer->first_free = 0;
@@ -44,21 +43,19 @@ void buffer_init(Buffer* buffer, unsigned int space)
 /* Frees the memory used by a buffer, and the buffer struct */
 void buffer_free(Buffer* buffer)
 {
-       debug("buffer_free\n");
-       free(buffer->data);
+       g_free(buffer->data);
 }
 
 /* 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, unsigned int space)
+void buffer_assure_space(Buffer* buffer, gsize space)
 {
-       unsigned int available_at_end = buffer->allocated - buffer->first_free;
-       unsigned int space_used;
-       int space_at_beginning;
+       gsize available_at_end = buffer->allocated - buffer->first_free;
+       gsize space_used;
+       gboolean space_at_beginning;
 
-       debug("buffer_assure_space %d bytes\n", space);
        /* If we've got the space already, good! */
        if (space <= available_at_end) {
                return;
@@ -87,23 +84,23 @@ void buffer_assure_space(Buffer* buffer, unsigned int space)
 
        /* We'll allocate more space */
        buffer->allocated += space + 1024;
-       buffer->data = (char*)g_realloc(buffer->data, buffer->allocated);
+       buffer->data = (guint8*)g_realloc(buffer->data, buffer->allocated);
 }
 
-void buffer_append(Buffer* buffer, char *from, unsigned int bytes)
+void buffer_append(Buffer* buffer, guint8 *from, gsize bytes)
 {
-       debug("buffer_append %d bytes\n", bytes);
        buffer_assure_space(buffer, bytes);
        memcpy(buffer->data + buffer->first_free, from, bytes);
        buffer->first_free += bytes;
 }
 
-void buffer_remove_start(Buffer* buffer, unsigned int bytes)
+void buffer_remove_start(Buffer* buffer, gsize bytes)
 {
-       debug("buffer_remove_start %d bytes\n", bytes);
        if (buffer->start + bytes > buffer->first_free) {
-               die("buffer_remove_start trying to remove %d bytes. s=%d ff=%d!\n",
-                       bytes, buffer->start, 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;
 
@@ -115,33 +112,43 @@ void buffer_remove_start(Buffer* buffer, unsigned int bytes)
 
 
 #ifndef SOME_FUNCTIONS_ARE_DEFINES
-void buffer_increase_length(Buffer* buffer, unsigned int bytes)
+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)
 {
-       debug("buffer_increase_length %d bytes\n", bytes);
        buffer->first_free += bytes;
 }
 #endif
 
 #ifndef SOME_FUNCTIONS_ARE_DEFINES
-unsigned int buffer_length(Buffer* buffer)
+gsize buffer_length(Buffer* buffer)
 {
-       debug("buffer_length\n");
        return buffer->first_free - buffer->start;
 }
 #endif
 
 #ifndef SOME_FUNCTIONS_ARE_DEFINES
-char* buffer_start_ptr(Buffer* buffer)
+guint8* buffer_start_ptr(Buffer* buffer)
 {
-       debug("buffer_start_ptr\n");
        return buffer->data + buffer->start;
 }
 #endif
 
 #ifndef SOME_FUNCTIONS_ARE_DEFINES
-char* buffer_end_ptr(Buffer* buffer)
+guint8* buffer_end_ptr(Buffer* buffer)
 {
-       debug("buffer_end_ptr\n");
        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