d796858f5e002c2bf3318c74e573400e275486e7
[obnox/wireshark/wip.git] / wiretap / buffer.c
1 /* buffer.c
2  *
3  * $Id: buffer.c,v 1.2 1998/11/12 06:01:18 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 <string.h>
27
28 #include "buffer.h"
29
30 /*#define DEBUG*/
31 #define DEBUG_PROGRAM_NAME "buffer.c"
32 #include "debug.h"
33
34 /* Initializes a buffer with a certain amount of allocated space */
35 void buffer_init(Buffer* buffer, unsigned int space)
36 {
37         debug("buffer_init\n");
38         buffer->data = (char*)g_malloc(space);
39         buffer->allocated = space;
40         buffer->start = 0;
41         buffer->first_free = 0;
42 }
43
44 /* Frees the memory used by a buffer, and the buffer struct */
45 void buffer_free(Buffer* buffer)
46 {
47         debug("buffer_free\n");
48         free(buffer->data);
49 }
50
51 /* Assures that there are 'space' bytes at the end of the used space
52         so that another routine can copy directly into the buffer space. After
53         doing that, the routine will also want to run
54         buffer_increase_length(). */
55 void buffer_assure_space(Buffer* buffer, unsigned int space)
56 {
57         unsigned int available_at_end = buffer->allocated - buffer->first_free;
58         unsigned int space_used;
59         int space_at_beginning;
60
61         debug("buffer_assure_space %d bytes\n", space);
62         /* If we've got the space already, good! */
63         if (space <= available_at_end) {
64                 return;
65         }
66
67         /* Maybe we don't have the space available at the end, but we would
68                 if we moved the used space back to the beginning of the
69                 allocation. The buffer could have become fragmented through lots
70                 of calls to buffer_remove_start(). I'm using buffer->start as the
71                 same as 'available_at_start' in this comparison. */
72
73         /* or maybe there's just no more room. */
74
75         space_at_beginning = buffer->start >= space;
76         if (space_at_beginning || buffer->start > 0) {
77                 space_used = buffer->first_free - buffer->start;
78                 /* this memory copy better be safe for overlapping memory regions! */
79                 memmove(buffer->data, buffer->data + buffer->start, space_used);
80                 buffer->start = 0;
81                 buffer->first_free = space_used;
82         }
83         /*if (buffer->start >= space) {*/
84         if (space_at_beginning) {
85                 return;
86         }
87
88         /* We'll allocate more space */
89         buffer->allocated += space + 1024;
90         buffer->data = (char*)g_realloc(buffer->data, buffer->allocated);
91 }
92
93 void buffer_append(Buffer* buffer, char *from, unsigned int bytes)
94 {
95         debug("buffer_append %d bytes\n", bytes);
96         buffer_assure_space(buffer, bytes);
97         memcpy(buffer->data + buffer->first_free, from, bytes);
98         buffer->first_free += bytes;
99 }
100
101 void buffer_remove_start(Buffer* buffer, unsigned int bytes)
102 {
103         debug("buffer_remove_start %d bytes\n", bytes);
104         if (buffer->start + bytes > buffer->first_free) {
105                 die("buffer_remove_start trying to remove %d bytes. s=%d ff=%d!\n",
106                         bytes, buffer->start, buffer->first_free);
107         }
108         buffer->start += bytes;
109
110         if (buffer->start == buffer->first_free) {
111                 buffer->start = 0;
112                 buffer->first_free = 0;
113         }
114 }
115
116
117 #ifndef SOME_FUNCTIONS_ARE_DEFINES
118 void buffer_increase_length(Buffer* buffer, unsigned int bytes)
119 {
120         debug("buffer_increase_length %d bytes\n", bytes);
121         buffer->first_free += bytes;
122 }
123 #endif
124
125 #ifndef SOME_FUNCTIONS_ARE_DEFINES
126 unsigned int buffer_length(Buffer* buffer)
127 {
128         debug("buffer_length\n");
129         return buffer->first_free - buffer->start;
130 }
131 #endif
132
133 #ifndef SOME_FUNCTIONS_ARE_DEFINES
134 char* buffer_start_ptr(Buffer* buffer)
135 {
136         debug("buffer_start_ptr\n");
137         return buffer->data + buffer->start;
138 }
139 #endif
140
141 #ifndef SOME_FUNCTIONS_ARE_DEFINES
142 char* buffer_end_ptr(Buffer* buffer)
143 {
144         debug("buffer_end_ptr\n");
145         return buffer->data + buffer->first_free;
146 }
147 #endif