1 /* test_libFLAC - Unit tester for libFLAC
2 * Copyright (C) 2000-2009 Josh Coalson
3 * Copyright (C) 2011-2023 Xiph.Org Foundation
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "FLAC/assert.h"
25 #include "share/compat.h"
26 #include "private/bitreader.h" /* from the libFLAC private include area */
27 #include "bitreader.h"
28 #include <stdio.h>
29 #include <string.h> /* for memcpy() */
30
31 /*
32 * WATCHOUT! Since FLAC__BitReader is a private structure, we use a copy of
33 * the definition here to get at the internals. Make sure this is kept up
34 * to date with what is in ../libFLAC/bitreader.c
35 */
36 #if (ENABLE_64_BIT_WORDS == 0)
37
38 typedef FLAC__uint32 brword;
39 #define FLAC__BYTES_PER_WORD 4
40 #define FLAC__BITS_PER_WORD 32
41
42 #else
43
44 typedef FLAC__uint64 brword;
45 #define FLAC__BYTES_PER_WORD 8
46 #define FLAC__BITS_PER_WORD 64
47
48 #endif
49
50 struct FLAC__BitReader {
51 /* any partially-consumed word at the head will stay right-justified as bits are consumed from the left */
52 /* any incomplete word at the tail will be left-justified, and bytes from the read callback are added on the right */
53 brword *buffer;
54 uint32_t capacity; /* in words */
55 uint32_t words; /* # of completed words in buffer */
56 uint32_t bytes; /* # of bytes in incomplete word at buffer[words] */
57 uint32_t consumed_words; /* #words ... */
58 uint32_t consumed_bits; /* ... + (#bits of head word) already consumed from the front of buffer */
59 uint32_t read_crc16; /* the running frame CRC */
60 uint32_t crc16_offset; /* the number of words in the current buffer that should not be CRC'd */
61 uint32_t crc16_align; /* the number of bits in the current consumed word that should not be CRC'd */
62 FLAC__bool read_limit_set; /* whether reads are limited */
63 uint32_t read_limit; /* the remaining size of what can be read */
64 uint32_t last_seen_framesync; /* the location of the last seen framesync, if it is in the buffer, in bits from front of buffer */
65 FLAC__BitReaderReadCallback read_callback;
66 void *client_data;
67 };
68
69 static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data);
70
FLAC__bitreader_dump(const FLAC__BitReader * br,FILE * out)71 static void FLAC__bitreader_dump(const FLAC__BitReader *br, FILE *out)
72 {
73 uint32_t i, j;
74 if(br == 0) {
75 fprintf(out, "bitreader is NULL\n");
76 }
77 else {
78 fprintf(out, "bitreader: capacity=%u words=%u bytes=%u consumed: words=%u, bits=%u\n", br->capacity, br->words, br->bytes, br->consumed_words, br->consumed_bits);
79
80 for(i = 0; i < br->words; i++) {
81 fprintf(out, "%08X: ", i);
82 for(j = 0; j < FLAC__BITS_PER_WORD; j++)
83 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
84 fprintf(out, ".");
85 else
86 fprintf(out, "%01d", br->buffer[i] & ((brword)1 << (FLAC__BITS_PER_WORD-j-1)) ? 1:0);
87 fprintf(out, "\n");
88 }
89 if(br->bytes > 0) {
90 fprintf(out, "%08X: ", i);
91 for(j = 0; j < br->bytes*8; j++)
92 if(i < br->consumed_words || (i == br->consumed_words && j < br->consumed_bits))
93 fprintf(out, ".");
94 else
95 fprintf(out, "%01d", br->buffer[i] & ((brword)1 << (br->bytes*8-j-1)) ? 1:0);
96 fprintf(out, "\n");
97 }
98 }
99 }
100
test_bitreader(void)101 FLAC__bool test_bitreader(void)
102 {
103 FLAC__BitReader *br;
104 FLAC__bool ok;
105 uint32_t i;
106 uint32_t words, bits; /* what we think br->consumed_words and br->consumed_bits should be */
107
108 FLAC__uint16 crc,expected_crcs[4] = { 0x5e4c, 0x7f6b, 0x2272, 0x42bf };
109 FLAC__byte data[32];
110
111 FLAC__uint32 val_uint32;
112 FLAC__uint64 val_uint64;
113
114 for (i = 0; i < 32; i++)
115 data[i] = i * 8 + 7;
116
117 printf("\n+++ libFLAC unit test: bitreader\n\n");
118
119 /*
120 * test new -> delete
121 */
122 printf("testing new... ");
123 br = FLAC__bitreader_new();
124 if(0 == br) {
125 printf("FAILED, returned NULL\n");
126 return false;
127 }
128 printf("OK\n");
129
130 printf("testing delete... ");
131 FLAC__bitreader_delete(br);
132 printf("OK\n");
133
134 /*
135 * test new -> init -> delete
136 */
137 printf("testing new... ");
138 br = FLAC__bitreader_new();
139 if(0 == br) {
140 printf("FAILED, returned NULL\n");
141 return false;
142 }
143 printf("OK\n");
144
145 printf("testing init... ");
146 if(!FLAC__bitreader_init(br, read_callback, data)) {
147 printf("FAILED, returned false\n");
148 return false;
149 }
150 printf("OK\n");
151
152 printf("testing delete... ");
153 FLAC__bitreader_delete(br);
154 printf("OK\n");
155
156 /*
157 * test new -> init -> clear -> delete
158 */
159 printf("testing new... ");
160 br = FLAC__bitreader_new();
161 if(0 == br) {
162 printf("FAILED, returned NULL\n");
163 return false;
164 }
165 printf("OK\n");
166
167 printf("testing init... ");
168 if(!FLAC__bitreader_init(br, read_callback, data)) {
169 printf("FAILED, returned false\n");
170 return false;
171 }
172 printf("OK\n");
173
174 printf("testing clear... ");
175 if(!FLAC__bitreader_clear(br)) {
176 printf("FAILED, returned false\n");
177 return false;
178 }
179 printf("OK\n");
180
181 printf("testing delete... ");
182 FLAC__bitreader_delete(br);
183 printf("OK\n");
184
185 /*
186 * test normal usage
187 */
188 printf("testing new... ");
189 br = FLAC__bitreader_new();
190 if(0 == br) {
191 printf("FAILED, returned NULL\n");
192 return false;
193 }
194 printf("OK\n");
195
196 printf("testing init... ");
197 if(!FLAC__bitreader_init(br, read_callback, data)) {
198 printf("FAILED, returned false\n");
199 return false;
200 }
201 printf("OK\n");
202
203 printf("testing clear... ");
204 if(!FLAC__bitreader_clear(br)) {
205 printf("FAILED, returned false\n");
206 return false;
207 }
208 printf("OK\n");
209
210 words = bits = 0;
211
212 printf("capacity = %u\n", br->capacity);
213
214 printf("testing raw reads... ");
215 ok =
216 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
217 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
218 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
219 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
220 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 10) &&
221 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) &&
222 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32) &&
223 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 4) &&
224 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
225 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
226 FLAC__bitreader_read_raw_uint64(br, &val_uint64, 64) &&
227 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 12)
228 ;
229 if(!ok) {
230 printf("FAILED\n");
231 FLAC__bitreader_dump(br, stdout);
232 return false;
233 }
234 /* we read 152 bits (=19 bytes) from the bitreader */
235 words = 152 / FLAC__BITS_PER_WORD;
236 bits = 152 - words*FLAC__BITS_PER_WORD;
237
238 if(br->consumed_words != words) {
239 printf("FAILED word count %u != %u\n", br->consumed_words, words);
240 FLAC__bitreader_dump(br, stdout);
241 return false;
242 }
243 if(br->consumed_bits != bits) {
244 printf("FAILED bit count %u != %u\n", br->consumed_bits, bits);
245 FLAC__bitreader_dump(br, stdout);
246 return false;
247 }
248 crc = FLAC__bitreader_get_read_crc16(br);
249 if(crc != expected_crcs[0]) {
250 printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[0]);
251 FLAC__bitreader_dump(br, stdout);
252 return false;
253 }
254 printf("OK\n");
255 FLAC__bitreader_dump(br, stdout);
256
257 printf("testing CRC reset... ");
258 FLAC__bitreader_clear(br);
259 FLAC__bitreader_reset_read_crc16(br, 0xFFFF);
260 crc = FLAC__bitreader_get_read_crc16(br);
261 if(crc != 0xFFFF) {
262 printf("FAILED reported CRC 0x%04x does not match expected 0xFFFF\n", crc);
263 FLAC__bitreader_dump(br, stdout);
264 return false;
265 }
266 FLAC__bitreader_reset_read_crc16(br, 0);
267 crc = FLAC__bitreader_get_read_crc16(br);
268 if(crc != 0) {
269 printf("FAILED reported CRC 0x%04x does not match expected 0x0000\n", crc);
270 FLAC__bitreader_dump(br, stdout);
271 return false;
272 }
273 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 16);
274 FLAC__bitreader_reset_read_crc16(br, 0);
275 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32);
276 crc = FLAC__bitreader_get_read_crc16(br);
277 if(crc != expected_crcs[1]) {
278 printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[1]);
279 FLAC__bitreader_dump(br, stdout);
280 return false;
281 }
282 printf("OK\n");
283
284 printf("testing unaligned < 32 bit reads... ");
285 FLAC__bitreader_clear(br);
286 FLAC__bitreader_skip_bits_no_crc(br, 8);
287 FLAC__bitreader_reset_read_crc16(br, 0);
288 ok =
289 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
290 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
291 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
292 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8)
293 ;
294 if(!ok) {
295 printf("FAILED\n");
296 FLAC__bitreader_dump(br, stdout);
297 return false;
298 }
299 crc = FLAC__bitreader_get_read_crc16(br);
300 if(crc != expected_crcs[2]) {
301 printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[2]);
302 FLAC__bitreader_dump(br, stdout);
303 return false;
304 }
305 printf("OK\n");
306 FLAC__bitreader_dump(br, stdout);
307
308 printf("testing unaligned < 64 bit reads... ");
309 FLAC__bitreader_clear(br);
310 FLAC__bitreader_skip_bits_no_crc(br, 8);
311 FLAC__bitreader_reset_read_crc16(br, 0);
312 ok =
313 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 1) &&
314 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 2) &&
315 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 5) &&
316 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 8) &&
317 FLAC__bitreader_read_raw_uint32(br, &val_uint32, 32)
318 ;
319 if(!ok) {
320 printf("FAILED\n");
321 FLAC__bitreader_dump(br, stdout);
322 return false;
323 }
324 crc = FLAC__bitreader_get_read_crc16(br);
325 if(crc != expected_crcs[3]) {
326 printf("FAILED reported CRC 0x%04x does not match expected 0x%04x\n", crc, expected_crcs[3]);
327 FLAC__bitreader_dump(br, stdout);
328 return false;
329 }
330 printf("OK\n");
331 FLAC__bitreader_dump(br, stdout);
332
333 printf("testing free... ");
334 FLAC__bitreader_free(br);
335 printf("OK\n");
336
337 printf("testing delete... ");
338 FLAC__bitreader_delete(br);
339 printf("OK\n");
340
341 printf("\nPASSED!\n");
342 return true;
343 }
344
345 /*----------------------------------------------------------------------------*/
346
read_callback(FLAC__byte buffer[],size_t * bytes,void * data)347 static FLAC__bool read_callback(FLAC__byte buffer[], size_t *bytes, void *data)
348 {
349 if (*bytes > 32)
350 *bytes = 32;
351
352 memcpy(buffer, data, *bytes);
353
354 return true;
355 }
356