ofpbuf: New function ofpbuf_put_hex().
[cascardo/ovs.git] / lib / ofpbuf.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofpbuf.h"
19 #include <assert.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include "dynamic-string.h"
23 #include "util.h"
24
25 /* Initializes 'b' as an empty ofpbuf that contains the 'allocated' bytes of
26  * memory starting at 'base'.
27  *
28  * 'base' should ordinarily be the first byte of a region obtained from
29  * malloc(), but in circumstances where it can be guaranteed that 'b' will
30  * never need to be expanded or freed, it can be a pointer into arbitrary
31  * memory. */
32 void
33 ofpbuf_use(struct ofpbuf *b, void *base, size_t allocated)
34 {
35     b->base = b->data = base;
36     b->allocated = allocated;
37     b->size = 0;
38     b->l2 = b->l3 = b->l4 = b->l7 = NULL;
39     list_poison(&b->list_node);
40     b->private_p = NULL;
41 }
42
43 /* Initializes 'b' as an ofpbuf whose data starts at 'data' and continues for
44  * 'size' bytes.  This is appropriate for an ofpbuf that will be used to
45  * inspect existing data, without moving it around or reallocating it, and
46  * generally without modifying it at all. */
47 void
48 ofpbuf_use_const(struct ofpbuf *b, const void *data, size_t size)
49 {
50     ofpbuf_use(b, (void *) data, size);
51     b->size = size;
52 }
53
54 /* Initializes 'b' as an empty ofpbuf with an initial capacity of 'size'
55  * bytes. */
56 void
57 ofpbuf_init(struct ofpbuf *b, size_t size)
58 {
59     ofpbuf_use(b, size ? xmalloc(size) : NULL, size);
60 }
61
62 /* Frees memory that 'b' points to. */
63 void
64 ofpbuf_uninit(struct ofpbuf *b)
65 {
66     if (b) {
67         free(b->base);
68     }
69 }
70
71 /* Frees memory that 'b' points to and allocates a new ofpbuf */
72 void
73 ofpbuf_reinit(struct ofpbuf *b, size_t size)
74 {
75     ofpbuf_uninit(b);
76     ofpbuf_init(b, size);
77 }
78
79 /* Creates and returns a new ofpbuf with an initial capacity of 'size'
80  * bytes. */
81 struct ofpbuf *
82 ofpbuf_new(size_t size)
83 {
84     struct ofpbuf *b = xmalloc(sizeof *b);
85     ofpbuf_init(b, size);
86     return b;
87 }
88
89 /* Creates and returns a new ofpbuf with an initial capacity of 'size +
90  * headroom' bytes, reserving the first 'headroom' bytes as headroom. */
91 struct ofpbuf *
92 ofpbuf_new_with_headroom(size_t size, size_t headroom)
93 {
94     struct ofpbuf *b = ofpbuf_new(size + headroom);
95     ofpbuf_reserve(b, headroom);
96     return b;
97 }
98
99 struct ofpbuf *
100 ofpbuf_clone(const struct ofpbuf *buffer)
101 {
102     return ofpbuf_clone_data(buffer->data, buffer->size);
103 }
104
105 /* Creates and returns a new ofpbuf whose data are copied from 'buffer'.   The
106  * returned ofpbuf will additionally have 'headroom' bytes of headroom. */
107 struct ofpbuf *
108 ofpbuf_clone_with_headroom(const struct ofpbuf *buffer, size_t headroom)
109 {
110     struct ofpbuf *b = ofpbuf_new_with_headroom(buffer->size, headroom);
111     ofpbuf_put(b, buffer->data, buffer->size);
112     return b;
113 }
114
115 struct ofpbuf *
116 ofpbuf_clone_data(const void *data, size_t size)
117 {
118     struct ofpbuf *b = ofpbuf_new(size);
119     ofpbuf_put(b, data, size);
120     return b;
121 }
122
123 /* Frees memory that 'b' points to, as well as 'b' itself. */
124 void
125 ofpbuf_delete(struct ofpbuf *b)
126 {
127     if (b) {
128         ofpbuf_uninit(b);
129         free(b);
130     }
131 }
132
133 /* Returns the number of bytes of headroom in 'b', that is, the number of bytes
134  * of unused space in ofpbuf 'b' before the data that is in use.  (Most
135  * commonly, the data in a ofpbuf is at its beginning, and thus the ofpbuf's
136  * headroom is 0.) */
137 size_t
138 ofpbuf_headroom(const struct ofpbuf *b)
139 {
140     return (char*)b->data - (char*)b->base;
141 }
142
143 /* Returns the number of bytes that may be appended to the tail end of ofpbuf
144  * 'b' before the ofpbuf must be reallocated. */
145 size_t
146 ofpbuf_tailroom(const struct ofpbuf *b)
147 {
148     return (char*)ofpbuf_end(b) - (char*)ofpbuf_tail(b);
149 }
150
151 /* Changes 'b->base' to 'new_base' and adjusts all of 'b''s internal pointers
152  * to reflect the change. */
153 static void
154 ofpbuf_rebase__(struct ofpbuf *b, void *new_base)
155 {
156     if (b->base != new_base) {
157         uintptr_t base_delta = (char*)new_base - (char*)b->base;
158         b->base = new_base;
159         b->data = (char*)b->data + base_delta;
160         if (b->l2) {
161             b->l2 = (char*)b->l2 + base_delta;
162         }
163         if (b->l3) {
164             b->l3 = (char*)b->l3 + base_delta;
165         }
166         if (b->l4) {
167             b->l4 = (char*)b->l4 + base_delta;
168         }
169         if (b->l7) {
170             b->l7 = (char*)b->l7 + base_delta;
171         }
172     }
173 }
174
175 /* Reallocates 'b' so that it has exactly 'new_tailroom' bytes of tailroom. */
176 static void
177 ofpbuf_resize_tailroom__(struct ofpbuf *b, size_t new_tailroom)
178 {
179     b->allocated = ofpbuf_headroom(b) + b->size + new_tailroom;
180     ofpbuf_rebase__(b, xrealloc(b->base, b->allocated));
181 }
182
183 /* Ensures that 'b' has room for at least 'size' bytes at its tail end,
184  * reallocating and copying its data if necessary.  Its headroom, if any, is
185  * preserved. */
186 void
187 ofpbuf_prealloc_tailroom(struct ofpbuf *b, size_t size)
188 {
189     if (size > ofpbuf_tailroom(b)) {
190         ofpbuf_resize_tailroom__(b, MAX(size, 64));
191     }
192 }
193
194 void
195 ofpbuf_prealloc_headroom(struct ofpbuf *b, size_t size)
196 {
197     assert(size <= ofpbuf_headroom(b));
198 }
199
200 /* Trims the size of 'b' to fit its actual content, reducing its tailroom to
201  * 0.  Its headroom, if any, is preserved. */
202 void
203 ofpbuf_trim(struct ofpbuf *b)
204 {
205     if (ofpbuf_tailroom(b) > 0) {
206         ofpbuf_resize_tailroom__(b, 0);
207     }
208 }
209
210 /* Appends 'size' bytes of data to the tail end of 'b', reallocating and
211  * copying its data if necessary.  Returns a pointer to the first byte of the
212  * new data, which is left uninitialized. */
213 void *
214 ofpbuf_put_uninit(struct ofpbuf *b, size_t size)
215 {
216     void *p;
217     ofpbuf_prealloc_tailroom(b, size);
218     p = ofpbuf_tail(b);
219     b->size += size;
220     return p;
221 }
222
223 /* Appends 'size' zeroed bytes to the tail end of 'b'.  Data in 'b' is
224  * reallocated and copied if necessary.  Returns a pointer to the first byte of
225  * the data's location in the ofpbuf. */
226 void *
227 ofpbuf_put_zeros(struct ofpbuf *b, size_t size)
228 {
229     void *dst = ofpbuf_put_uninit(b, size);
230     memset(dst, 0, size);
231     return dst;
232 }
233
234 /* Appends the 'size' bytes of data in 'p' to the tail end of 'b'.  Data in 'b'
235  * is reallocated and copied if necessary.  Returns a pointer to the first
236  * byte of the data's location in the ofpbuf. */
237 void *
238 ofpbuf_put(struct ofpbuf *b, const void *p, size_t size)
239 {
240     void *dst = ofpbuf_put_uninit(b, size);
241     memcpy(dst, p, size);
242     return dst;
243 }
244
245 /* Parses as many pairs of hex digits as possible (possibly separated by
246  * spaces) from the beginning of 's', appending bytes for their values to 'b'.
247  * Returns the first character of 's' that is not the first of a pair of hex
248  * digits.  If 'n' is nonnull, stores the number of bytes added to 'b' in
249  * '*n'. */
250 char *
251 ofpbuf_put_hex(struct ofpbuf *b, const char *s, size_t *n)
252 {
253     size_t initial_size = b->size;
254     for (;;) {
255         uint8_t byte;
256         bool ok;
257
258         s += strspn(s, " ");
259         byte = hexits_value(s, 2, &ok);
260         if (!ok) {
261             if (n) {
262                 *n = b->size - initial_size;
263             }
264             return (char *) s;
265         }
266
267         ofpbuf_put(b, &byte, 1);
268         s += 2;
269     }
270 }
271
272 /* Reserves 'size' bytes of headroom so that they can be later allocated with
273  * ofpbuf_push_uninit() without reallocating the ofpbuf. */
274 void
275 ofpbuf_reserve(struct ofpbuf *b, size_t size)
276 {
277     assert(!b->size);
278     ofpbuf_prealloc_tailroom(b, size);
279     b->data = (char*)b->data + size;
280 }
281
282 void *
283 ofpbuf_push_uninit(struct ofpbuf *b, size_t size)
284 {
285     ofpbuf_prealloc_headroom(b, size);
286     b->data = (char*)b->data - size;
287     b->size += size;
288     return b->data;
289 }
290
291 /* Prefixes 'size' zeroed bytes to the head end of 'b'.  'b' must have at least
292  * 'size' bytes of headroom.  Returns a pointer to the first byte of the data's
293  * location in the ofpbuf. */
294 void *
295 ofpbuf_push_zeros(struct ofpbuf *b, size_t size)
296 {
297     void *dst = ofpbuf_push_uninit(b, size);
298     memset(dst, 0, size);
299     return dst;
300 }
301
302 void *
303 ofpbuf_push(struct ofpbuf *b, const void *p, size_t size)
304 {
305     void *dst = ofpbuf_push_uninit(b, size);
306     memcpy(dst, p, size);
307     return dst;
308 }
309
310 /* If 'b' contains at least 'offset + size' bytes of data, returns a pointer to
311  * byte 'offset'.  Otherwise, returns a null pointer. */
312 void *
313 ofpbuf_at(const struct ofpbuf *b, size_t offset, size_t size)
314 {
315     return offset + size <= b->size ? (char *) b->data + offset : NULL;
316 }
317
318 /* Returns a pointer to byte 'offset' in 'b', which must contain at least
319  * 'offset + size' bytes of data. */
320 void *
321 ofpbuf_at_assert(const struct ofpbuf *b, size_t offset, size_t size)
322 {
323     assert(offset + size <= b->size);
324     return ((char *) b->data) + offset;
325 }
326
327 /* Returns the byte following the last byte of data in use in 'b'. */
328 void *
329 ofpbuf_tail(const struct ofpbuf *b)
330 {
331     return (char *) b->data + b->size;
332 }
333
334 /* Returns the byte following the last byte allocated for use (but not
335  * necessarily in use) by 'b'. */
336 void *
337 ofpbuf_end(const struct ofpbuf *b)
338 {
339     return (char *) b->base + b->allocated;
340 }
341
342 /* Clears any data from 'b'. */
343 void
344 ofpbuf_clear(struct ofpbuf *b)
345 {
346     b->data = b->base;
347     b->size = 0;
348 }
349
350 /* Removes 'size' bytes from the head end of 'b', which must contain at least
351  * 'size' bytes of data.  Returns the first byte of data removed. */
352 void *
353 ofpbuf_pull(struct ofpbuf *b, size_t size)
354 {
355     void *data = b->data;
356     assert(b->size >= size);
357     b->data = (char*)b->data + size;
358     b->size -= size;
359     return data;
360 }
361
362 /* If 'b' has at least 'size' bytes of data, removes that many bytes from the
363  * head end of 'b' and returns the first byte removed.  Otherwise, returns a
364  * null pointer without modifying 'b'. */
365 void *
366 ofpbuf_try_pull(struct ofpbuf *b, size_t size)
367 {
368     return b->size >= size ? ofpbuf_pull(b, size) : NULL;
369 }
370
371 /* Returns a string that describes some of 'b''s metadata plus a hex dump of up
372  * to 'maxbytes' from the start of the buffer. */
373 char *
374 ofpbuf_to_string(const struct ofpbuf *b, size_t maxbytes)
375 {
376     struct ds s;
377
378     ds_init(&s);
379     ds_put_format(&s, "size=%zu, allocated=%zu, head=%zu, tail=%zu\n",
380                   b->size, b->allocated,
381                   ofpbuf_headroom(b), ofpbuf_tailroom(b));
382     ds_put_hex_dump(&s, b->data, MIN(b->size, maxbytes), 0, false);
383     return ds_cstr(&s);
384 }
385
386 /* Removes each of the "struct ofpbuf"s on 'list' from the list and frees
387  * them.  */
388 void
389 ofpbuf_list_delete(struct list *list)
390 {
391     struct ofpbuf *b, *next;
392
393     LIST_FOR_EACH_SAFE (b, next, list_node, list) {
394         list_remove(&b->list_node);
395         ofpbuf_delete(b);
396     }
397 }