9cbcfb19d4f4938f22774d1bc6deef28b8878e5c
[platform/upstream/nettle.git] / buffer.h
1 /* buffer.h
2
3    A bare-bones string stream.
4
5    Copyright (C) 2002 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
12      * the GNU Lesser General Public License as published by the Free
13        Software Foundation; either version 3 of the License, or (at your
14        option) any later version.
15
16    or
17
18      * the GNU General Public License as published by the Free
19        Software Foundation; either version 2 of the License, or (at your
20        option) any later version.
21
22    or both in parallel, as here.
23
24    GNU Nettle is distributed in the hope that it will be useful,
25    but WITHOUT ANY WARRANTY; without even the implied warranty of
26    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27    General Public License for more details.
28
29    You should have received copies of the GNU General Public License and
30    the GNU Lesser General Public License along with this program.  If
31    not, see http://www.gnu.org/licenses/.
32 */
33  
34 #ifndef NETTLE_BUFFER_H_INCLUDED
35 #define NETTLE_BUFFER_H_INCLUDED
36
37 #include "realloc.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 struct nettle_buffer
44 {
45   uint8_t *contents;
46   /* Allocated size */
47   size_t alloc;
48
49   void *realloc_ctx;
50   nettle_realloc_func *realloc;
51
52   /* Current size */
53   size_t size;
54 };
55
56 /* Initializes a buffer that uses plain realloc */
57 void
58 nettle_buffer_init(struct nettle_buffer *buffer);
59
60 void
61 nettle_buffer_init_realloc(struct nettle_buffer *buffer,
62                            void *realloc_ctx,
63                            nettle_realloc_func *realloc);
64
65 /* Initializes a buffer of fix size */
66 void
67 nettle_buffer_init_size(struct nettle_buffer *buffer,
68                         size_t length, uint8_t *space);
69
70 void
71 nettle_buffer_clear(struct nettle_buffer *buffer);
72
73 /* Resets the buffer, without freeing the buffer space. */
74 void
75 nettle_buffer_reset(struct nettle_buffer *buffer);
76
77 int
78 nettle_buffer_grow(struct nettle_buffer *buffer,
79                    size_t length);
80
81 #define NETTLE_BUFFER_PUTC(buffer, c) \
82 ( (((buffer)->size < (buffer)->alloc) || nettle_buffer_grow((buffer), 1)) \
83   && ((buffer)->contents[(buffer)->size++] = (c), 1) )
84
85 int
86 nettle_buffer_write(struct nettle_buffer *buffer,
87                     size_t length, const uint8_t *data);
88
89 /* Like nettle_buffer_write, but instead of copying data to the
90  * buffer, it returns a pointer to the area where the caller can copy
91  * the data. The pointer is valid only until the next call that can
92  * reallocate the buffer. */
93 uint8_t *
94 nettle_buffer_space(struct nettle_buffer *buffer,
95                     size_t length);
96
97 /* Copy the contents of SRC to the end of DST. */
98 int
99 nettle_buffer_copy(struct nettle_buffer *dst,
100                    const struct nettle_buffer *src);
101
102 #ifdef __cplusplus
103 }
104 #endif
105
106 #endif /* NETTLE_BUFFER_H_INCLUDED */