11.3Smartin/* $NetBSD: sparkcrc.c,v 1.3 2008/04/28 20:22:53 martin Exp $ */ 21.1Sabs 31.1Sabs/*- 41.1Sabs * Copyright (c) 2001 The NetBSD Foundation, Inc. 51.1Sabs * All rights reserved. 61.1Sabs * 71.1Sabs * This code is derived from software contributed to The NetBSD Foundation 81.1Sabs * by David Brownlee 91.1Sabs * 101.1Sabs * Redistribution and use in source and binary forms, with or without 111.1Sabs * modification, are permitted provided that the following conditions 121.1Sabs * are met: 131.1Sabs * 1. Redistributions of source code must retain the above copyright 141.1Sabs * notice, this list of conditions and the following disclaimer. 151.1Sabs * 2. Redistributions in binary form must reproduce the above copyright 161.1Sabs * notice, this list of conditions and the following disclaimer in the 171.1Sabs * documentation and/or other materials provided with the distribution. 181.1Sabs * 191.1Sabs * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 201.1Sabs * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 211.1Sabs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 221.1Sabs * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 231.1Sabs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 241.1Sabs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 251.1Sabs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 261.1Sabs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 271.1Sabs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 281.1Sabs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 291.1Sabs * POSSIBILITY OF SUCH DAMAGE. 301.1Sabs */ 311.1Sabs 321.2Sjmc#if HAVE_NBTOOL_CONFIG_H 331.2Sjmc#include "nbtool_config.h" 341.2Sjmc#endif 351.2Sjmc 361.1Sabs#include <err.h> 371.1Sabs#include <stdio.h> 381.1Sabs#include <stdlib.h> 391.1Sabs#include <sys/types.h> 401.1Sabs 411.1Sabs/* 421.1Sabs * Trivial progarm to generate a crc suitable for use in a sparkive file 431.1Sabs * Based on an algorithm by David Schwaderer 441.1Sabs */ 451.1Sabs 461.1Sabsuint16_t crclookup[256] = { 471.1Sabs 0, 49345, 49537, 320, 49921, 960, 640, 49729, 50689, 1728, 481.1Sabs 1920, 51009, 1280, 50625, 50305, 1088, 52225, 3264, 3456, 491.1Sabs 52545, 3840, 53185, 52865, 3648, 2560, 51905, 52097, 2880, 501.1Sabs 51457, 2496, 2176, 51265, 55297, 6336, 6528, 55617, 6912, 511.1Sabs 56257, 55937, 6720, 7680, 57025, 57217, 8000, 56577, 7616, 521.1Sabs 7296, 56385, 5120, 54465, 54657, 5440, 55041, 6080, 5760, 531.1Sabs 54849, 53761, 4800, 4992, 54081, 4352, 53697, 53377, 4160, 541.1Sabs 61441, 12480, 12672, 61761, 13056, 62401, 62081, 12864, 551.1Sabs 13824, 63169, 63361, 14144, 62721, 13760, 13440, 62529, 561.1Sabs 15360, 64705, 64897, 15680, 65281, 16320, 16000, 65089, 571.1Sabs 64001, 15040, 15232, 64321, 14592, 63937, 63617, 14400, 581.1Sabs 10240, 59585, 59777, 10560, 60161, 11200, 10880, 59969, 591.1Sabs 60929, 11968, 12160, 61249, 11520, 60865, 60545, 11328, 601.1Sabs 58369, 9408, 9600, 58689, 9984, 59329, 59009, 9792, 8704, 611.1Sabs 58049, 58241, 9024, 57601, 8640, 8320, 57409, 40961, 24768, 621.1Sabs 24960, 41281, 25344, 41921, 41601, 25152, 26112, 42689, 631.1Sabs 42881, 26432, 42241, 26048, 25728, 42049, 27648, 44225, 641.1Sabs 44417, 27968, 44801, 28608, 28288, 44609, 43521, 27328, 651.1Sabs 27520, 43841, 26880, 43457, 43137, 26688, 30720, 47297, 661.1Sabs 47489, 31040, 47873, 31680, 31360, 47681, 48641, 32448, 671.1Sabs 32640, 48961, 32000, 48577, 48257, 31808, 46081, 29888, 681.1Sabs 30080, 46401, 30464, 47041, 46721, 30272, 29184, 45761, 691.1Sabs 45953, 29504, 45313, 29120, 28800, 45121, 20480, 37057, 701.1Sabs 37249, 20800, 37633, 21440, 21120, 37441, 38401, 22208, 711.1Sabs 22400, 38721, 21760, 38337, 38017, 21568, 39937, 23744, 721.1Sabs 23936, 40257, 24320, 40897, 40577, 24128, 23040, 39617, 731.1Sabs 39809, 23360, 39169, 22976, 22656, 38977, 34817, 18624, 741.1Sabs 18816, 35137, 19200, 35777, 35457, 19008, 19968, 36545, 751.1Sabs 36737, 20288, 36097, 19904, 19584, 35905, 17408, 33985, 761.1Sabs 34177, 17728, 34561, 18368, 18048, 34369, 33281, 17088, 771.1Sabs 17280, 33601, 16640, 33217, 32897, 16448 781.1Sabs}; 791.1Sabs 801.1Sabsint 811.1Sabsmain(int argc, char **argv) 821.1Sabs{ 831.1Sabs unsigned char buf[1024]; 841.1Sabs FILE *fds; 851.1Sabs int len; 861.1Sabs int i; 871.1Sabs int crc = 0; 881.1Sabs 891.1Sabs if (argc != 2) { 901.1Sabs fprintf(stderr, "Usage: sparkcrc filename\n"); 911.1Sabs exit(0); 921.1Sabs } 931.1Sabs if (!(fds = fopen(argv[1], "r"))) 941.1Sabs err(1, "Unable to open file '%s'", argv[1]); 951.1Sabs while ((len = fread(buf, 1, sizeof(buf), fds))) 961.1Sabs for (i = 0; i < len; ++i) { 971.1Sabs crc = (crc >> 8) ^ crclookup[(crc & 0xff) ^ buf[i]]; 981.1Sabs } 991.1Sabs fclose(fds); 1001.1Sabs printf("%d\n", (int)crc); 1011.1Sabs return (0); 1021.1Sabs} 103