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