extended bio_reader and bio_writer to handle u_int64_t
[strongswan.git] / src / libstrongswan / bio / bio_reader.c
1 /*
2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "bio_reader.h"
17
18 #include <debug.h>
19
20 typedef struct private_bio_reader_t private_bio_reader_t;
21
22 /**
23 * Private data of an bio_reader_t object.
24 */
25 struct private_bio_reader_t {
26
27 /**
28 * Public bio_reader_t interface.
29 */
30 bio_reader_t public;
31
32 /**
33 * Remaining data to process
34 */
35 chunk_t buf;
36 };
37
38 METHOD(bio_reader_t, remaining, u_int32_t,
39 private_bio_reader_t *this)
40 {
41 return this->buf.len;
42 }
43
44 METHOD(bio_reader_t, peek, chunk_t,
45 private_bio_reader_t *this)
46 {
47 return this->buf;
48 }
49
50 METHOD(bio_reader_t, read_uint8, bool,
51 private_bio_reader_t *this, u_int8_t *res)
52 {
53 if (this->buf.len < 1)
54 {
55 DBG1(DBG_LIB, "%d bytes insufficient to parse u_int8 data",
56 this->buf.len);
57 return FALSE;
58 }
59 *res = this->buf.ptr[0];
60 this->buf = chunk_skip(this->buf, 1);
61 return TRUE;
62 }
63
64 METHOD(bio_reader_t, read_uint16, bool,
65 private_bio_reader_t *this, u_int16_t *res)
66 {
67 if (this->buf.len < 2)
68 {
69 DBG1(DBG_LIB, "%d bytes insufficient to parse u_int16 data",
70 this->buf.len);
71 return FALSE;
72 }
73 *res = untoh16(this->buf.ptr);
74 this->buf = chunk_skip(this->buf, 2);
75 return TRUE;
76 }
77
78 METHOD(bio_reader_t, read_uint24, bool,
79 private_bio_reader_t *this, u_int32_t *res)
80 {
81 if (this->buf.len < 3)
82 {
83 DBG1(DBG_LIB, "%d bytes insufficient to parse u_int24 data",
84 this->buf.len);
85 return FALSE;
86 }
87 *res = untoh32(this->buf.ptr) >> 8;
88 this->buf = chunk_skip(this->buf, 3);
89 return TRUE;
90 }
91
92 METHOD(bio_reader_t, read_uint32, bool,
93 private_bio_reader_t *this, u_int32_t *res)
94 {
95 if (this->buf.len < 4)
96 {
97 DBG1(DBG_LIB, "%d bytes insufficient to parse u_int32 data",
98 this->buf.len);
99 return FALSE;
100 }
101 *res = untoh32(this->buf.ptr);
102 this->buf = chunk_skip(this->buf, 4);
103 return TRUE;
104 }
105
106 METHOD(bio_reader_t, read_uint64, bool,
107 private_bio_reader_t *this, u_int64_t *res)
108 {
109 if (this->buf.len < 8)
110 {
111 DBG1(DBG_LIB, "%d bytes insufficient to parse u_int64 data",
112 this->buf.len);
113 return FALSE;
114 }
115 *res = untoh64(this->buf.ptr);
116 this->buf = chunk_skip(this->buf, 8);
117 return TRUE;
118 }
119
120 METHOD(bio_reader_t, read_data, bool,
121 private_bio_reader_t *this, u_int32_t len, chunk_t *res)
122 {
123 if (this->buf.len < len)
124 {
125 DBG1(DBG_LIB, "%d bytes insufficient to parse %d bytes of data",
126 this->buf.len, len);
127 return FALSE;
128 }
129 *res = chunk_create(this->buf.ptr, len);
130 this->buf = chunk_skip(this->buf, len);
131 return TRUE;
132 }
133
134 METHOD(bio_reader_t, read_data8, bool,
135 private_bio_reader_t *this, chunk_t *res)
136 {
137 u_int8_t len;
138
139 if (!read_uint8(this, &len))
140 {
141 return FALSE;
142 }
143 return read_data(this, len, res);
144 }
145
146 METHOD(bio_reader_t, read_data16, bool,
147 private_bio_reader_t *this, chunk_t *res)
148 {
149 u_int16_t len;
150
151 if (!read_uint16(this, &len))
152 {
153 return FALSE;
154 }
155 return read_data(this, len, res);
156 }
157
158 METHOD(bio_reader_t, read_data24, bool,
159 private_bio_reader_t *this, chunk_t *res)
160 {
161 u_int32_t len;
162
163 if (!read_uint24(this, &len))
164 {
165 return FALSE;
166 }
167 return read_data(this, len, res);
168 }
169
170 METHOD(bio_reader_t, read_data32, bool,
171 private_bio_reader_t *this, chunk_t *res)
172 {
173 u_int32_t len;
174
175 if (!read_uint32(this, &len))
176 {
177 return FALSE;
178 }
179 return read_data(this, len, res);
180 }
181
182 METHOD(bio_reader_t, destroy, void,
183 private_bio_reader_t *this)
184 {
185 free(this);
186 }
187
188 /**
189 * See header
190 */
191 bio_reader_t *bio_reader_create(chunk_t data)
192 {
193 private_bio_reader_t *this;
194
195 INIT(this,
196 .public = {
197 .remaining = _remaining,
198 .peek = _peek,
199 .read_uint8 = _read_uint8,
200 .read_uint16 = _read_uint16,
201 .read_uint24 = _read_uint24,
202 .read_uint32 = _read_uint32,
203 .read_uint64 = _read_uint64,
204 .read_data = _read_data,
205 .read_data8 = _read_data8,
206 .read_data16 = _read_data16,
207 .read_data24 = _read_data24,
208 .read_data32 = _read_data32,
209 .destroy = _destroy,
210 },
211 .buf = data,
212 );
213
214 return &this->public;
215 }