-
Notifications
You must be signed in to change notification settings - Fork 6
/
databuffer.c
247 lines (219 loc) · 8.79 KB
/
databuffer.c
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* QTC: databuffer.c (c) 2011, 2012 50m30n3
*
* This file is part of QTC.
*
* QTC 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 3 of the License, or
* (at your option) any later version.
*
* QTC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QTC. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include "databuffer.h"
/*******************************************************************************
* Function to create a new databuffer *
* *
* size is the initial size of the databuffer in bytes *
* *
* Returns a new databuffer or NULL on failure *
*******************************************************************************/
struct databuffer *databuffer_create( unsigned int size )
{
struct databuffer *buffer = malloc( sizeof( struct databuffer ) );
if( buffer == NULL )
{
perror( "databuffer_create: malloc" );
return NULL;
}
buffer->size = 0;
buffer->bits = 0;
buffer->pos = 0;
buffer->bitpos = 8;
buffer->datasize = size<=0?1:size;
buffer->data = malloc( sizeof( unsigned char ) * buffer->datasize );
if( buffer->data == NULL )
{
perror( "databuffer_create: malloc" );
free( buffer );
return NULL;
}
buffer->data[ 0 ] = 0;
buffer->wbuffer = 0;
buffer->rbuffer = 0;
return buffer;
}
/*******************************************************************************
* Function to free the internal structures of a databuffer *
* *
* buffer is the databuffer to free *
* *
* Modifies databuffer *
*******************************************************************************/
void databuffer_free( struct databuffer *buffer )
{
free( buffer->data );
free( buffer );
}
/*******************************************************************************
* Function to add a number of bits to a databuffer *
* *
* data is the data to be added *
* buffer is the databuffer to add to *
* bits is the number of bits to take from data and add *
* *
* Modifies databuffer *
* *
* Returns 0 on failure, 1 on success *
*******************************************************************************/
int databuffer_add_bits( unsigned int data, struct databuffer *buffer, int bits )
{
int i;
for( i=0; i<bits; i++ )
{
buffer->wbuffer |= (data&0x01)<<buffer->bits++;
data >>= 1;
if( buffer->bits >= 8 )
{
buffer->data[ buffer->size++ ] = buffer->wbuffer;
if( buffer->size >= buffer->datasize )
{
buffer->datasize *= 2;
buffer->data = realloc( buffer->data, buffer->datasize );
if( buffer->data == NULL )
{
perror( "databuffer_add_bits: realloc" );
return 0;
}
}
buffer->wbuffer = 0;
buffer->bits = 0;
}
}
return 1;
}
/*******************************************************************************
* Function to add a single byte to a databuffer *
* Always skips to the next full byte boundary *
* *
* data is the data to be added *
* buffer is the databuffer to add to *
* *
* Modifies databuffer *
* *
* Returns 0 on failure, 1 on success *
*******************************************************************************/
int databuffer_add_byte( unsigned char data, struct databuffer *buffer )
{
if( buffer->bits != 0 )
{
buffer->data[ buffer->size++ ] = buffer->wbuffer;
if( buffer->size >= buffer->datasize )
{
buffer->datasize *= 2;
buffer->data = realloc( buffer->data, buffer->datasize );
if( buffer->data == NULL )
{
perror( "databuffer_add_byte: realloc" );
return 0;
}
}
buffer->wbuffer = 0;
buffer->bits = 0;
}
buffer->data[ buffer->size++ ] = data;
if( buffer->size >= buffer->datasize )
{
buffer->datasize *= 2;
buffer->data = realloc( buffer->data, buffer->datasize );
if( buffer->data == NULL )
{
fprintf( stderr, "%i\n", buffer->datasize );
perror( "databuffer_add_byte: realloc" );
return 0;
}
}
return 1;
}
/*******************************************************************************
* Function to pad the data in a databuffer to a full byte *
* *
* buffer is the databuffer to add to *
* *
* Modifies databuffer *
* *
* Returns 0 on failure, 1 on success *
*******************************************************************************/
int databuffer_pad( struct databuffer *buffer )
{
if( buffer->bits != 0 )
{
buffer->data[ buffer->size++ ] = buffer->wbuffer;
if( buffer->size >= buffer->datasize )
{
buffer->datasize *= 2;
buffer->data = realloc( buffer->data, buffer->datasize );
if( buffer->data == NULL )
{
perror( "databuffer_pad: realloc" );
return 0;
}
}
buffer->wbuffer = 0;
buffer->bits = 0;
}
return 1;
}
/*******************************************************************************
* Function to get a number of bits from a databuffer *
* *
* buffer is the databuffer to add to *
* *
* Modifies databuffer *
* *
* Returns the bits in an unsigned int *
*******************************************************************************/
unsigned int databuffer_get_bits( struct databuffer *buffer, int bits )
{
unsigned int data = 0;
int i;
for( i=0; i<bits; i++ )
{
if( buffer->bitpos >= 8 )
{
buffer->rbuffer = buffer->data[ buffer->pos++ ];
buffer->bitpos = 0;
}
data |= ((buffer->rbuffer>>buffer->bitpos++)&0x01)<<i;
}
return data;
}
/*******************************************************************************
* Function to get a single byte from a databuffer *
* Alwas skipd to the next full byte, discards previous bits *
* *
* buffer is the databuffer to add to *
* *
* Modifies databuffer *
* *
* Returns the byte as unsigned char *
*******************************************************************************/
unsigned char databuffer_get_byte( struct databuffer *buffer )
{
unsigned char data;
if( buffer->bitpos != 0 )
{
buffer->rbuffer = buffer->data[ buffer->pos++ ];
}
data = buffer->rbuffer;
buffer->bitpos = 8;
return data;
}