sparkcrc.c revision 1.2
1/*	$NetBSD: sparkcrc.c,v 1.2 2004/11/10 03:57:23 jmc Exp $	*/
2
3/*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by David Brownlee
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *        This product includes software developed by the NetBSD
21 *        Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#if HAVE_NBTOOL_CONFIG_H
40#include "nbtool_config.h"
41#endif
42
43#include <err.h>
44#include <stdio.h>
45#include <stdlib.h>
46#include <sys/types.h>
47
48/*
49 * Trivial progarm to generate a crc suitable for use in a sparkive file
50 * Based on an algorithm by David Schwaderer
51 */
52
53uint16_t crclookup[256] = {
54	0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728,
55	1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456,
56	52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880,
57	51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912,
58	56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616,
59	7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760,
60	54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160,
61	61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864,
62	13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529,
63	15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089,
64	64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400,
65	10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969,
66	60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328,
67	58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704,
68	58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768,
69	24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689,
70	42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225,
71	44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328,
72	27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297,
73	47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448,
74	32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888,
75	30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761,
76	45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057,
77	37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208,
78	22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744,
79	23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617,
80	39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624,
81	18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545,
82	36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985,
83	34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088,
84	17280, 33601, 16640, 33217, 32897, 16448
85};
86
87int
88main(int argc, char **argv)
89{
90	unsigned char buf[1024];
91	FILE *fds;
92	int len;
93	int i;
94	int crc = 0;
95
96	if (argc != 2) {
97		fprintf(stderr, "Usage: sparkcrc filename\n");
98		exit(0);
99	}
100	if (!(fds = fopen(argv[1], "r")))
101		err(1, "Unable to open file '%s'", argv[1]);
102	while ((len = fread(buf, 1, sizeof(buf), fds)))
103		for (i = 0; i < len; ++i) {
104			crc = (crc >> 8) ^ crclookup[(crc & 0xff) ^ buf[i]];
105		}
106	fclose(fds);
107	printf("%d\n", (int)crc);
108	return (0);
109}
110