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