summaryrefslogtreecommitdiff
path: root/src/libstrongswan/plugins/bliss/bliss_bitpacker.c
blob: 4d84461191cceb8b7e83d003b53df5b32d29f778 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Copyright (C) 2014 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY;https://www.hsr.ch/HSR-intern-Anmeldung.4409.0.html?&no_cache=1 without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "bliss_bitpacker.h"

typedef struct private_bliss_bitpacker_t private_bliss_bitpacker_t;

/**
 * Private data structure for bliss_bitpacker_t object
 */
struct private_bliss_bitpacker_t {
	/**
	 * Public interface.
	 */
	bliss_bitpacker_t public;

	/**
	 * Current number of bits written to buffer
	 */
	size_t bits;

	/**
	 * Bit buffer for up to 32 bits
	 */
	uint32_t bits_buf;

	/**
	 * Bits left in the bit buffer
	 */
	size_t bits_left;

	/**
	 * Buffer
	 */
	chunk_t buf;

	/**
	 * Read/Write pointer into buffer
	 */
	chunk_t pos;

};

METHOD(bliss_bitpacker_t, get_bits, size_t,
	private_bliss_bitpacker_t *this)
{
	return this->bits;
}

METHOD(bliss_bitpacker_t, write_bits, bool,
	private_bliss_bitpacker_t *this, uint32_t value, size_t bits)
{
	if (bits == 0)
	{
		return TRUE;
	}
	if (bits > 32)
	{
		return FALSE;
	}
	if (bits < 32)
	{
		value &= (1 << bits) - 1;
	}
	this->bits += bits;

	while (TRUE)
	{
		if (bits <= this->bits_left)
		{
			this->bits_buf |= value << (this->bits_left - bits);
			this->bits_left -= bits;
			return TRUE;
		}

		this->bits_buf |= value >> (bits - this->bits_left);
		value &= (1 << (bits - this->bits_left)) - 1;
		bits -= this->bits_left;

		if (this->pos.len < 8)
		{
			return FALSE;
		}
		htoun32(this->pos.ptr, this->bits_buf);
		this->pos = chunk_skip(this->pos, 4);
		this->bits_buf = 0;
		this->bits_left = 32;
	}
}

METHOD(bliss_bitpacker_t, read_bits, bool,
	private_bliss_bitpacker_t *this, uint32_t *value, size_t bits)
{
	if (bits > 32)
	{
		return FALSE;
	}
	*value = 0;

	while (TRUE)
	{
		if (this->bits_left == 0)
		{
			if (this->pos.len < 4)
			{
				return FALSE;
			}
			this->bits_buf = untoh32(this->pos.ptr);
			this->pos = chunk_skip(this->pos, 4);
			this->bits_left = 32;
		}
		if (bits <= this->bits_left)
		{
			*value |= this->bits_buf >> (this->bits_left - bits);
			this->bits_buf &= (1 << (this->bits_left - bits)) - 1;
			this->bits_left -= bits;

			return TRUE;
		}
		*value |= this->bits_buf << (bits - this->bits_left);
		bits -= this->bits_left;
		this->bits_left = 0;
	}
}

METHOD(bliss_bitpacker_t, extract_buf, chunk_t,
	private_bliss_bitpacker_t *this)
{
	chunk_t buf;

	htoun32(this->pos.ptr, this->bits_buf);
	this->pos.len -= 4;
	buf = this->buf;
	buf.len = this->buf.len - this->pos.len - this->bits_left/8;
	this->buf = this->pos = chunk_empty;

	return buf;
}

METHOD(bliss_bitpacker_t, destroy, void,
	private_bliss_bitpacker_t *this)
{
	free(this->buf.ptr);
	free(this);
}

/**
 * See header.
 */
bliss_bitpacker_t *bliss_bitpacker_create(uint16_t max_bits)
{
	private_bliss_bitpacker_t *this;

	INIT(this,
		.public = {
			.get_bits = _get_bits,
			.write_bits = _write_bits,
			.read_bits = _read_bits,
			.extract_buf = _extract_buf,
			.destroy = _destroy,
		},
		.bits_left = 32,
		.buf = chunk_alloc(round_up(max_bits, 32)/8),
	);

	this->pos = this->buf;

	return &this->public;
}

/**
 * See header.
 */
bliss_bitpacker_t *bliss_bitpacker_create_from_data(chunk_t data)
{
	private_bliss_bitpacker_t *this;

	INIT(this,
		.public = {
			.get_bits = _get_bits,
			.write_bits = _write_bits,
			.read_bits = _read_bits,
			.extract_buf = _extract_buf,
			.destroy = _destroy,
		},
		.bits = 8 * data.len,
		.buf = chunk_alloc(round_up(data.len, 4)),
	);

	memset(this->buf.ptr + this->buf.len - 4, 0x00, 4);
	memcpy(this->buf.ptr, data.ptr, data.len);
	this->pos = this->buf;

	return &this->public;
}