summaryrefslogtreecommitdiff
path: root/src/test_libFLAC/crc.c
blob: 800f3fa4fa67d50d03ba33720f80bc1f29b489c7 (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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/* test_libFLAC - Unit tester for libFLAC
 * Copyright (C) 2014-2018  Xiph.Org Foundation
 *
 * 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.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>

#include "FLAC/assert.h"
#include "share/compat.h"
#include "private/crc.h"
#include "crc.h"

static FLAC__uint8 crc8_update_ref(FLAC__byte byte, FLAC__uint8 crc);
static FLAC__uint16 crc16_update_ref(FLAC__byte byte, FLAC__uint16 crc);

static FLAC__bool test_crc8(const FLAC__byte *data, size_t size);
static FLAC__bool test_crc16(const FLAC__byte *data, size_t size);
static FLAC__bool test_crc16_update(const FLAC__byte *data, size_t size);
static FLAC__bool test_crc16_32bit_words(const FLAC__uint32 *words, size_t size);
static FLAC__bool test_crc16_64bit_words(const FLAC__uint64 *words, size_t size);

#define DATA_SIZE 32768

FLAC__bool test_crc(void)
{
	uint32_t i;
	FLAC__byte data[DATA_SIZE] = { 0 };

	/* Initialize data reproducibly with pseudo-random values. */
	for (i = 1; i < DATA_SIZE; i++)
		data[i] = crc8_update_ref(i % 256, data[i - 1]);

	printf("\n+++ libFLAC unit test: crc\n\n");

	if (! test_crc8(data, DATA_SIZE))
		return false;

	if (! test_crc16(data, DATA_SIZE))
		return false;

	if (! test_crc16_update(data, DATA_SIZE))
		return false;

	if (! test_crc16_32bit_words((FLAC__uint32 *)data, DATA_SIZE / 4))
		return false;

	if (! test_crc16_64bit_words((FLAC__uint64 *)data, DATA_SIZE / 8))
		return false;

	printf("\nPASSED!\n");
	return true;
}

/*----------------------------------------------------------------------------*/

/* Reference implementations of CRC-8 and CRC-16 to check against. */

#define CRC8_POLYNOMIAL 0x07

static FLAC__uint8 crc8_update_ref(FLAC__byte byte, FLAC__uint8 crc)
{
    uint32_t i;

    crc ^= byte;

    for (i = 0; i < 8; i++) {
        crc = (crc << 1) ^ ((crc >> 7) ? CRC8_POLYNOMIAL : 0);
    }

    return crc;
}

#define CRC16_POLYNOMIAL 0x8005

static FLAC__uint16 crc16_update_ref(FLAC__byte byte, FLAC__uint16 crc)
{
    uint32_t i;

    crc ^= byte << 8;

    for (i = 0; i < 8; i++) {
        crc = (crc << 1) ^ ((crc >> 15) ? CRC16_POLYNOMIAL : 0);
    }

    return crc;
}

/*----------------------------------------------------------------------------*/

static FLAC__bool test_crc8(const FLAC__byte *data, size_t size)
{
	uint32_t i;
	FLAC__uint8 crc0,crc1;

	printf("testing FLAC__crc8 ... ");

	crc0 = 0;
	crc1 = FLAC__crc8(data, 0);

	if (crc1 != crc0) {
		printf("FAILED, FLAC__crc8 returned non-zero CRC for zero bytes of data\n");
		return false;
	}

	for (i = 0; i < size; i++) {
		crc0 = crc8_update_ref(data[i], crc0);
		crc1 = FLAC__crc8(data, i + 1);

		if (crc1 != crc0) {
			printf("FAILED, FLAC__crc8 result did not match reference CRC for %i bytes of test data\n", i + 1);
			return false;
		}
	}

	printf("OK\n");

	return true;
}

static FLAC__bool test_crc16(const FLAC__byte *data, size_t size)
{
	uint32_t i;
	FLAC__uint16 crc0,crc1;

	printf("testing FLAC__crc16 ... ");

	crc0 = 0;
	crc1 = FLAC__crc16(data, 0);

	if (crc1 != crc0) {
		printf("FAILED, FLAC__crc16 returned non-zero CRC for zero bytes of data\n");
		return false;
	}

	for (i = 0; i < size; i++) {
		crc0 = crc16_update_ref(data[i], crc0);
		crc1 = FLAC__crc16(data, i + 1);

		if (crc1 != crc0) {
			printf("FAILED, FLAC__crc16 result did not match reference CRC for %i bytes of test data\n", i + 1);
			return false;
		}
	}

	printf("OK\n");

	return true;
}

static FLAC__bool test_crc16_update(const FLAC__byte *data, size_t size)
{
	uint32_t i;
	FLAC__uint16 crc0,crc1;

	printf("testing FLAC__CRC16_UPDATE macro ... ");

	crc0 = 0;
	crc1 = 0;

	for (i = 0; i < size; i++) {
		crc0 = crc16_update_ref(data[i], crc0);
		crc1 = FLAC__CRC16_UPDATE(data[i], crc1);

		if (crc1 != crc0) {
			printf("FAILED, FLAC__CRC16_UPDATE result did not match reference CRC after %i bytes of test data\n", i + 1);
			return false;
		}
	}

	printf("OK\n");

	return true;
}

static FLAC__bool test_crc16_32bit_words(const FLAC__uint32 *words, size_t size)
{
	uint32_t n,i,k;
	FLAC__uint16 crc0,crc1;

	for (n = 1; n <= 16; n++) {
		printf("testing FLAC__crc16_update_words32 (length=%i) ... ", n);

		crc0 = 0;
		crc1 = 0;

		for (i = 0; i <= size - n; i += n) {
			for (k = 0; k < n; k++) {
				crc0 = crc16_update_ref( words[i + k] >> 24,         crc0);
				crc0 = crc16_update_ref((words[i + k] >> 16) & 0xFF, crc0);
				crc0 = crc16_update_ref((words[i + k] >>  8) & 0xFF, crc0);
				crc0 = crc16_update_ref( words[i + k]        & 0xFF, crc0);
			}

			crc1 = FLAC__crc16_update_words32(words + i, n, crc1);

			if (crc1 != crc0) {
				printf("FAILED, FLAC__crc16_update_words32 result did not match reference CRC after %i words of test data\n", i + n);
				return false;
			}
		}

		crc1 = FLAC__crc16_update_words32(words, 0, crc1);

		if (crc1 != crc0) {
			printf("FAILED, FLAC__crc16_update_words32 called with zero bytes changed CRC value\n");
			return false;
		}

		printf("OK\n");
	}

	return true;
}

static FLAC__bool test_crc16_64bit_words(const FLAC__uint64 *words, size_t size)
{
	uint32_t n,i,k;
	FLAC__uint16 crc0,crc1;

	for (n = 1; n <= 16; n++) {
		printf("testing FLAC__crc16_update_words64 (length=%i) ... ", n);

		crc0 = 0;
		crc1 = 0;

		for (i = 0; i <= size - n; i += n) {
			for (k = 0; k < n; k++) {
				crc0 = crc16_update_ref( words[i + k] >> 56,         crc0);
				crc0 = crc16_update_ref((words[i + k] >> 48) & 0xFF, crc0);
				crc0 = crc16_update_ref((words[i + k] >> 40) & 0xFF, crc0);
				crc0 = crc16_update_ref((words[i + k] >> 32) & 0xFF, crc0);
				crc0 = crc16_update_ref((words[i + k] >> 24) & 0xFF, crc0);
				crc0 = crc16_update_ref((words[i + k] >> 16) & 0xFF, crc0);
				crc0 = crc16_update_ref((words[i + k] >>  8) & 0xFF, crc0);
				crc0 = crc16_update_ref( words[i + k]        & 0xFF, crc0);
			}

			crc1 = FLAC__crc16_update_words64(words + i, n, crc1);

			if (crc1 != crc0) {
				printf("FAILED, FLAC__crc16_update_words64 result did not match reference CRC after %i words of test data\n", i + n);
				return false;
			}
		}

		crc1 = FLAC__crc16_update_words64(words, 0, crc1);

		if (crc1 != crc0) {
			printf("FAILED, FLAC__crc16_update_words64 called with zero bytes changed CRC value\n");
			return false;
		}

		printf("OK\n");
	}

	return true;
}