Add svn:eol-style.
[metze/wireshark/wip.git] / wiretap / buffer.c
1 /* buffer.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  */
23 #include "config.h"
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
34 buffer_init(Buffer* buffer, gsize space)
35 {
36         buffer->data = (guint8*)g_malloc(space);
37         buffer->allocated = space;
38         buffer->start = 0;
39         buffer->first_free = 0;
40 }
41
42 /* Frees the memory used by a buffer, and the buffer struct */
43 void
44 buffer_free(Buffer* buffer)
45 {
46         g_free(buffer->data);
47 }
48
49 /* Assures that there are 'space' bytes at the end of the used space
50         so that another routine can copy directly into the buffer space. After
51         doing that, the routine will also want to run
52         buffer_increase_length(). */
53 void
54 buffer_assure_space(Buffer* buffer, gsize space)
55 {
56         gsize available_at_end = buffer->allocated - buffer->first_free;
57         gsize space_used;
58         gboolean space_at_beginning;
59
60         /* If we've got the space already, good! */
61         if (space <= available_at_end) {
62                 return;
63         }
64
65         /* Maybe we don't have the space available at the end, but we would
66                 if we moved the used space back to the beginning of the
67                 allocation. The buffer could have become fragmented through lots
68                 of calls to buffer_remove_start(). I'm using buffer->start as the
69                 same as 'available_at_start' in this comparison. */
70
71         /* or maybe there's just no more room. */
72
73         space_at_beginning = buffer->start >= space;
74         if (space_at_beginning || buffer->start > 0) {
75                 space_used = buffer->first_free - buffer->start;
76                 /* this memory copy better be safe for overlapping memory regions! */
77                 memmove(buffer->data, buffer->data + buffer->start, space_used);
78                 buffer->start = 0;
79                 buffer->first_free = space_used;
80         }
81         /*if (buffer->start >= space) {*/
82         if (space_at_beginning) {
83                 return;
84         }
85
86         /* We'll allocate more space */
87         buffer->allocated += space + 1024;
88         buffer->data = (guint8*)g_realloc(buffer->data, buffer->allocated);
89 }
90
91 void
92 buffer_append(Buffer* buffer, guint8 *from, gsize bytes)
93 {
94         buffer_assure_space(buffer, bytes);
95         memcpy(buffer->data + buffer->first_free, from, bytes);
96         buffer->first_free += bytes;
97 }
98
99 void
100 buffer_remove_start(Buffer* buffer, gsize bytes)
101 {
102         if (buffer->start + bytes > buffer->first_free) {
103                 g_error("buffer_remove_start trying to remove %" G_GINT64_MODIFIER "u bytes. s=%" G_GINT64_MODIFIER "u ff=%" G_GINT64_MODIFIER "u!\n",
104                         (guint64)bytes, (guint64)buffer->start,
105                         (guint64)buffer->first_free);
106                 /** g_error() does an abort() and thus never returns **/
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
119 buffer_clean(Buffer* buffer)
120 {
121         buffer_remove_start(buffer, buffer_length(buffer));
122 }
123 #endif
124
125 #ifndef SOME_FUNCTIONS_ARE_DEFINES
126 void
127 buffer_increase_length(Buffer* buffer, gsize bytes)
128 {
129         buffer->first_free += bytes;
130 }
131 #endif
132
133 #ifndef SOME_FUNCTIONS_ARE_DEFINES
134 gsize
135 buffer_length(Buffer* buffer)
136 {
137         return buffer->first_free - buffer->start;
138 }
139 #endif
140
141 #ifndef SOME_FUNCTIONS_ARE_DEFINES
142 guint8 *
143 buffer_start_ptr(Buffer* buffer)
144 {
145         return buffer->data + buffer->start;
146 }
147 #endif
148
149 #ifndef SOME_FUNCTIONS_ARE_DEFINES
150 guint8 *
151 buffer_end_ptr(Buffer* buffer)
152 {
153         return buffer->data + buffer->first_free;
154 }
155 #endif
156
157 #ifndef SOME_FUNCTIONS_ARE_DEFINES
158 void
159 buffer_append_buffer(Buffer* buffer, Buffer* src_buffer)
160 {
161         buffer_append(buffer, buffer_start_ptr(src_buffer), buffer_length(src_buffer));
162 }
163 #endif