summaryrefslogtreecommitdiff
path: root/src/test_libFLAC/crc.c
blob: c743406642e1812cc6af34dbb61a2d1b401eef01 (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
/* 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);

#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;

	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;
}