MSWSP: use GuidPropertySet_find_guid() in parse_CFullPropSpec()
[wireshark-wip.git] / wiretap / buffer.c
blobfc12c6a9870bc101b7d90415d28edc9e5e9e32bf
1 /* buffer.c
3 * $Id$
5 * Wiretap Library
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
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.
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.
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.
23 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib.h>
30 #include "buffer.h"
32 /* Initializes a buffer with a certain amount of allocated space */
33 void
34 buffer_init(Buffer* buffer, gsize space)
36 buffer->data = (guint8*)g_malloc(space);
37 buffer->allocated = space;
38 buffer->start = 0;
39 buffer->first_free = 0;
42 /* Frees the memory used by a buffer, and the buffer struct */
43 void
44 buffer_free(Buffer* buffer)
46 g_free(buffer->data);
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)
56 gsize available_at_end = buffer->allocated - buffer->first_free;
57 gsize space_used;
58 gboolean space_at_beginning;
60 /* If we've got the space already, good! */
61 if (space <= available_at_end) {
62 return;
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. */
71 /* or maybe there's just no more room. */
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;
81 /*if (buffer->start >= space) {*/
82 if (space_at_beginning) {
83 return;
86 /* We'll allocate more space */
87 buffer->allocated += space + 1024;
88 buffer->data = (guint8*)g_realloc(buffer->data, buffer->allocated);
91 void
92 buffer_append(Buffer* buffer, guint8 *from, gsize bytes)
94 buffer_assure_space(buffer, bytes);
95 memcpy(buffer->data + buffer->first_free, from, bytes);
96 buffer->first_free += bytes;
99 void
100 buffer_remove_start(Buffer* buffer, gsize bytes)
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 **/
108 buffer->start += bytes;
110 if (buffer->start == buffer->first_free) {
111 buffer->start = 0;
112 buffer->first_free = 0;
117 #ifndef SOME_FUNCTIONS_ARE_DEFINES
118 void
119 buffer_clean(Buffer* buffer)
121 buffer_remove_start(buffer, buffer_length(buffer));
123 #endif
125 #ifndef SOME_FUNCTIONS_ARE_DEFINES
126 void
127 buffer_increase_length(Buffer* buffer, gsize bytes)
129 buffer->first_free += bytes;
131 #endif
133 #ifndef SOME_FUNCTIONS_ARE_DEFINES
134 gsize
135 buffer_length(Buffer* buffer)
137 return buffer->first_free - buffer->start;
139 #endif
141 #ifndef SOME_FUNCTIONS_ARE_DEFINES
142 guint8 *
143 buffer_start_ptr(Buffer* buffer)
145 return buffer->data + buffer->start;
147 #endif
149 #ifndef SOME_FUNCTIONS_ARE_DEFINES
150 guint8 *
151 buffer_end_ptr(Buffer* buffer)
153 return buffer->data + buffer->first_free;
155 #endif
157 #ifndef SOME_FUNCTIONS_ARE_DEFINES
158 void
159 buffer_append_buffer(Buffer* buffer, Buffer* src_buffer)
161 buffer_append(buffer, buffer_start_ptr(src_buffer), buffer_length(src_buffer));
163 #endif