* Modified Makefile.am and configure.in so that wiretap isn't built unless
[obnox/wireshark/wip.git] / wiretap / buffer.c
1 /* buffer.c
2  *
3  * $Id: buffer.c,v 1.3 1998/11/12 23:29:33 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
7  * 
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  */
23
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "buffer.h"
30 #include "glib.h"
31
32 /*#define DEBUG*/
33 #define DEBUG_PROGRAM_NAME "buffer.c"
34 #include "debug.h"
35
36 /* Initializes a buffer with a certain amount of allocated space */
37 void buffer_init(Buffer* buffer, unsigned int space)
38 {
39         debug("buffer_init\n");
40         buffer->data = (char*)g_malloc(space);
41         buffer->allocated = space;
42         buffer->start = 0;
43         buffer->first_free = 0;
44 }
45
46 /* Frees the memory used by a buffer, and the buffer struct */
47 void buffer_free(Buffer* buffer)
48 {
49         debug("buffer_free\n");
50         free(buffer->data);
51 }
52
53 /* Assures that there are 'space' bytes at the end of the used space
54         so that another routine can copy directly into the buffer space. After
55         doing that, the routine will also want to run
56         buffer_increase_length(). */
57 void buffer_assure_space(Buffer* buffer, unsigned int space)
58 {
59         unsigned int available_at_end = buffer->allocated - buffer->first_free;
60         unsigned int space_used;
61         int space_at_beginning;
62
63         debug("buffer_assure_space %d bytes\n", space);
64         /* If we've got the space already, good! */
65         if (space <= available_at_end) {
66                 return;
67         }
68
69         /* Maybe we don't have the space available at the end, but we would
70                 if we moved the used space back to the beginning of the
71                 allocation. The buffer could have become fragmented through lots
72                 of calls to buffer_remove_start(). I'm using buffer->start as the
73                 same as 'available_at_start' in this comparison. */
74
75         /* or maybe there's just no more room. */
76
77         space_at_beginning = buffer->start >= space;
78         if (space_at_beginning || buffer->start > 0) {
79                 space_used = buffer->first_free - buffer->start;
80                 /* this memory copy better be safe for overlapping memory regions! */
81                 memmove(buffer->data, buffer->data + buffer->start, space_used);
82                 buffer->start = 0;
83                 buffer->first_free = space_used;
84         }
85         /*if (buffer->start >= space) {*/
86         if (space_at_beginning) {
87                 return;
88         }
89
90         /* We'll allocate more space */
91         buffer->allocated += space + 1024;
92         buffer->data = (char*)g_realloc(buffer->data, buffer->allocated);
93 }
94
95 void buffer_append(Buffer* buffer, char *from, unsigned int bytes)
96 {
97         debug("buffer_append %d bytes\n", bytes);
98         buffer_assure_space(buffer, bytes);
99         memcpy(buffer->data + buffer->first_free, from, bytes);
100         buffer->first_free += bytes;
101 }
102
103 void buffer_remove_start(Buffer* buffer, unsigned int bytes)
104 {
105         debug("buffer_remove_start %d bytes\n", bytes);
106         if (buffer->start + bytes > buffer->first_free) {
107                 die("buffer_remove_start trying to remove %d bytes. s=%d ff=%d!\n",
108                         bytes, buffer->start, buffer->first_free);
109         }
110         buffer->start += bytes;
111
112         if (buffer->start == buffer->first_free) {
113                 buffer->start = 0;
114                 buffer->first_free = 0;
115         }
116 }
117
118
119 #ifndef SOME_FUNCTIONS_ARE_DEFINES
120 void buffer_increase_length(Buffer* buffer, unsigned int bytes)
121 {
122         debug("buffer_increase_length %d bytes\n", bytes);
123         buffer->first_free += bytes;
124 }
125 #endif
126
127 #ifndef SOME_FUNCTIONS_ARE_DEFINES
128 unsigned int buffer_length(Buffer* buffer)
129 {
130         debug("buffer_length\n");
131         return buffer->first_free - buffer->start;
132 }
133 #endif
134
135 #ifndef SOME_FUNCTIONS_ARE_DEFINES
136 char* buffer_start_ptr(Buffer* buffer)
137 {
138         debug("buffer_start_ptr\n");
139         return buffer->data + buffer->start;
140 }
141 #endif
142
143 #ifndef SOME_FUNCTIONS_ARE_DEFINES
144 char* buffer_end_ptr(Buffer* buffer)
145 {
146         debug("buffer_end_ptr\n");
147         return buffer->data + buffer->first_free;
148 }
149 #endif