Added methods to bio_reader_t to read data from end of buffer
[strongswan.git] / src / libstrongswan / bio / bio_reader.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_reader bio_reader
21 * @{ @ingroup bio
22 */
23
24 #ifndef BIO_READER_H_
25 #define BIO_READER_H_
26
27 typedef struct bio_reader_t bio_reader_t;
28
29 #include <library.h>
30
31 /**
32 * Buffered input parser.
33 *
34 * @note Integers are returned in host byte order.
35 */
36 struct bio_reader_t {
37
38 /**
39 * Get the number of remaining bytes.
40 *
41 * @return number of remaining bytes in buffer
42 */
43 u_int32_t (*remaining)(bio_reader_t *this);
44
45 /**
46 * Peek the remaining data, not consuming any bytes.
47 *
48 * @return remaining data
49 */
50 chunk_t (*peek)(bio_reader_t *this);
51
52 /**
53 * Read a 8-bit integer from the buffer, advance.
54 *
55 * @param res pointer to result
56 * @return TRUE if integer read successfully
57 */
58 bool (*read_uint8)(bio_reader_t *this, u_int8_t *res);
59
60 /**
61 * Read a 16-bit integer from the buffer, advance.
62 *
63 * @param res pointer to result
64 * @return TRUE if integer read successfully
65 */
66 bool (*read_uint16)(bio_reader_t *this, u_int16_t *res);
67
68 /**
69 * Read a 24-bit integer from the buffer, advance.
70 *
71 * @param res pointer to result
72 * @return TRUE if integer read successfully
73 */
74 bool (*read_uint24)(bio_reader_t *this, u_int32_t *res);
75
76 /**
77 * Read a 32-bit integer from the buffer, advance.
78 *
79 * @param res pointer to result
80 * @return TRUE if integer read successfully
81 */
82 bool (*read_uint32)(bio_reader_t *this, u_int32_t *res);
83
84 /**
85 * Read a 64-bit integer from the buffer, advance.
86 *
87 * @param res pointer to result
88 * @return TRUE if integer read successfully
89 */
90 bool (*read_uint64)(bio_reader_t *this, u_int64_t *res);
91
92 /**
93 * Read a chunk of len bytes, advance.
94 *
95 * @param len number of bytes to read
96 * @param res pointer to result, not cloned
97 * @return TRUE if data read successfully
98 */
99 bool (*read_data)(bio_reader_t *this, u_int32_t len, chunk_t *res);
100
101 /**
102 * Read a 8-bit integer from the end of the buffer, reduce remaining.
103 *
104 * @param res pointer to result
105 * @return TRUE if integer read successfully
106 */
107 bool (*read_uint8_end)(bio_reader_t *this, u_int8_t *res);
108
109 /**
110 * Read a 16-bit integer from the end of the buffer, reduce remaining.
111 *
112 * @param res pointer to result
113 * @return TRUE if integer read successfully
114 */
115 bool (*read_uint16_end)(bio_reader_t *this, u_int16_t *res);
116
117 /**
118 * Read a 24-bit integer from the end of the buffer, reduce remaining.
119 *
120 * @param res pointer to result
121 * @return TRUE if integer read successfully
122 */
123 bool (*read_uint24_end)(bio_reader_t *this, u_int32_t *res);
124
125 /**
126 * Read a 32-bit integer from the end of the buffer, reduce remaining.
127 *
128 * @param res pointer to result
129 * @return TRUE if integer read successfully
130 */
131 bool (*read_uint32_end)(bio_reader_t *this, u_int32_t *res);
132
133 /**
134 * Read a 64-bit integer from the end of the buffer, reduce remaining.
135 *
136 * @param res pointer to result
137 * @return TRUE if integer read successfully
138 */
139 bool (*read_uint64_end)(bio_reader_t *this, u_int64_t *res);
140
141 /**
142 * Read a chunk of len bytes from the end of the buffer, reduce remaining.
143 *
144 * @param len number of bytes to read
145 * @param res ponter to result, not cloned
146 * @return TRUE if data read successfully
147 */
148 bool (*read_data_end)(bio_reader_t *this, u_int32_t len, chunk_t *res);
149
150 /**
151 * Read a chunk of bytes with a 8-bit length header, advance.
152 *
153 * @param res pointer to result, not cloned
154 * @return TRUE if data read successfully
155 */
156 bool (*read_data8)(bio_reader_t *this, chunk_t *res);
157
158 /**
159 * Read a chunk of bytes with a 16-bit length header, advance.
160 *
161 * @param res pointer to result, not cloned
162 * @return TRUE if data read successfully
163 */
164 bool (*read_data16)(bio_reader_t *this, chunk_t *res);
165
166 /**
167 * Read a chunk of bytes with a 24-bit length header, advance.
168 *
169 * @param res pointer to result, not cloned
170 * @return TRUE if data read successfully
171 */
172 bool (*read_data24)(bio_reader_t *this, chunk_t *res);
173
174 /**
175 * Read a chunk of bytes with a 32-bit length header, advance.
176 *
177 * @param res pointer to result, not cloned
178 * @return TRUE if data read successfully
179 */
180 bool (*read_data32)(bio_reader_t *this, chunk_t *res);
181
182 /**
183 * Destroy a bio_reader_t.
184 */
185 void (*destroy)(bio_reader_t *this);
186 };
187
188 /**
189 * Create a bio_reader instance.
190 */
191 bio_reader_t *bio_reader_create(chunk_t data);
192
193 #endif /** bio_reader_H_ @}*/