2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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>.
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
16 #include "tls_reader.h"
20 typedef struct private_tls_reader_t private_tls_reader_t
;
23 * Private data of an tls_reader_t object.
25 struct private_tls_reader_t
{
28 * Public tls_reader_t interface.
33 * Remaining data to process
38 METHOD(tls_reader_t
, remaining
, u_int32_t
,
39 private_tls_reader_t
*this)
44 METHOD(tls_reader_t
, peek
, chunk_t
,
45 private_tls_reader_t
*this)
50 METHOD(tls_reader_t
, read_uint8
, bool,
51 private_tls_reader_t
*this, u_int8_t
*res
)
53 if (this->buf
.len
< 1)
55 DBG1(DBG_TLS
, "%d bytes insufficient to parse u_int8 data",
59 *res
= this->buf
.ptr
[0];
60 this->buf
= chunk_skip(this->buf
, 1);
64 METHOD(tls_reader_t
, read_uint16
, bool,
65 private_tls_reader_t
*this, u_int16_t
*res
)
67 if (this->buf
.len
< 2)
69 DBG1(DBG_TLS
, "%d bytes insufficient to parse u_int16 data",
73 *res
= untoh16(this->buf
.ptr
);
74 this->buf
= chunk_skip(this->buf
, 2);
78 METHOD(tls_reader_t
, read_uint24
, bool,
79 private_tls_reader_t
*this, u_int32_t
*res
)
81 if (this->buf
.len
< 3)
83 DBG1(DBG_TLS
, "%d bytes insufficient to parse u_int24 data",
87 *res
= untoh32(this->buf
.ptr
) >> 8;
88 this->buf
= chunk_skip(this->buf
, 3);
92 METHOD(tls_reader_t
, read_uint32
, bool,
93 private_tls_reader_t
*this, u_int32_t
*res
)
95 if (this->buf
.len
< 4)
97 DBG1(DBG_TLS
, "%d bytes insufficient to parse u_int32 data",
101 *res
= untoh32(this->buf
.ptr
);
102 this->buf
= chunk_skip(this->buf
, 4);
106 METHOD(tls_reader_t
, read_data
, bool,
107 private_tls_reader_t
*this, u_int32_t len
, chunk_t
*res
)
109 if (this->buf
.len
< len
)
111 DBG1(DBG_TLS
, "%d bytes insufficient to parse %d bytes of data",
115 *res
= chunk_create(this->buf
.ptr
, len
);
116 this->buf
= chunk_skip(this->buf
, len
);
120 METHOD(tls_reader_t
, read_data8
, bool,
121 private_tls_reader_t
*this, chunk_t
*res
)
125 if (!read_uint8(this, &len
))
129 return read_data(this, len
, res
);
132 METHOD(tls_reader_t
, read_data16
, bool,
133 private_tls_reader_t
*this, chunk_t
*res
)
137 if (!read_uint16(this, &len
))
141 return read_data(this, len
, res
);
144 METHOD(tls_reader_t
, read_data24
, bool,
145 private_tls_reader_t
*this, chunk_t
*res
)
149 if (!read_uint24(this, &len
))
153 return read_data(this, len
, res
);
156 METHOD(tls_reader_t
, read_data32
, bool,
157 private_tls_reader_t
*this, chunk_t
*res
)
161 if (!read_uint32(this, &len
))
165 return read_data(this, len
, res
);
168 METHOD(tls_reader_t
, destroy
, void,
169 private_tls_reader_t
*this)
177 tls_reader_t
*tls_reader_create(chunk_t data
)
179 private_tls_reader_t
*this;
183 .remaining
= _remaining
,
185 .read_uint8
= _read_uint8
,
186 .read_uint16
= _read_uint16
,
187 .read_uint24
= _read_uint24
,
188 .read_uint32
= _read_uint32
,
189 .read_data
= _read_data
,
190 .read_data8
= _read_data8
,
191 .read_data16
= _read_data16
,
192 .read_data24
= _read_data24
,
193 .read_data32
= _read_data32
,
199 return &this->public;