8f84d050024eadbf8009d3ac173cfc0a09f06ec2
[strongswan.git] / src / libstrongswan / bio / bio_writer.h
1 /*
2 * Copyright (C) 2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2010 Martin Willi
6 * Copyright (C) 2010 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 /**
20 * @defgroup bio_writer bio_writer
21 * @{ @ingroup bio
22 */
23
24 #ifndef BIO_WRITER_H_
25 #define BIO_WRITER_H_
26
27 typedef struct bio_writer_t bio_writer_t;
28
29 #include <library.h>
30
31 /**
32 * Buffered output generator.
33 *
34 * @note Integers are converted to network byte order before writing.
35 */
36 struct bio_writer_t {
37
38 /**
39 * Append a 8-bit integer to the buffer.
40 *
41 * @param value value to append
42 */
43 void (*write_uint8)(bio_writer_t *this, u_int8_t value);
44
45 /**
46 * Append a 16-bit integer to the buffer.
47 *
48 * @param value value to append
49 */
50 void (*write_uint16)(bio_writer_t *this, u_int16_t value);
51
52 /**
53 * Append a 24-bit integer to the buffer.
54 *
55 * @param value value to append
56 */
57 void (*write_uint24)(bio_writer_t *this, u_int32_t value);
58
59 /**
60 * Append a 32-bit integer to the buffer.
61 *
62 * @param value value to append
63 */
64 void (*write_uint32)(bio_writer_t *this, u_int32_t value);
65
66 /**
67 * Append a 64-bit integer to the buffer.
68 *
69 * @param value value to append
70 */
71 void (*write_uint64)(bio_writer_t *this, u_int64_t value);
72
73 /**
74 * Append a chunk of data without a length header.
75 *
76 * @param value value to append
77 */
78 void (*write_data)(bio_writer_t *this, chunk_t value);
79
80 /**
81 * Append a chunk of data with a 8-bit length header.
82 *
83 * @param value value to append
84 */
85 void (*write_data8)(bio_writer_t *this, chunk_t value);
86
87 /**
88 * Append a chunk of data with a 16-bit length header.
89 *
90 * @param value value to append
91 */
92 void (*write_data16)(bio_writer_t *this, chunk_t value);
93
94 /**
95 * Append a chunk of data with a 24-bit length header.
96 *
97 * @param value value to append
98 */
99 void (*write_data24)(bio_writer_t *this, chunk_t value);
100
101 /**
102 * Append a chunk of data with a 32-bit length header.
103 *
104 * @param value value to append
105 */
106 void (*write_data32)(bio_writer_t *this, chunk_t value);
107
108 /**
109 * Prepend a 8-bit length header to existing data.
110 */
111 void (*wrap8)(bio_writer_t *this);
112
113 /**
114 * Prepend a 16-bit length header to existing data.
115 */
116 void (*wrap16)(bio_writer_t *this);
117
118 /**
119 * Prepend a 24-bit length header to existing data.
120 */
121 void (*wrap24)(bio_writer_t *this);
122
123 /**
124 * Prepend a 32-bit length header to existing data.
125 */
126 void (*wrap32)(bio_writer_t *this);
127
128 /**
129 * Get the encoded data buffer.
130 *
131 * @return chunk to internal buffer
132 */
133 chunk_t (*get_buf)(bio_writer_t *this);
134
135 /**
136 * Return the encoded data buffer and detach it from the writer (resets
137 * the internal buffer).
138 *
139 * @return chunk to internal buffer (has to be freed)
140 */
141 chunk_t (*extract_buf)(bio_writer_t *this);
142
143 /**
144 * Destroy a bio_writer_t.
145 */
146 void (*destroy)(bio_writer_t *this);
147 };
148
149 /**
150 * Create a bio_writer instance.
151 *
152 * The size of the internal buffer is increased automatically by bufsize (or a
153 * default if not given) if the initial size does not suffice.
154 *
155 * @param bufsize initially allocated buffer size
156 */
157 bio_writer_t *bio_writer_create(u_int32_t bufsize);
158
159 #endif /** BIO_WRITER_H_ @}*/