blob: 5b59cf3d64880b70e6222569a4d225ebce9accf1 [file] [log] [blame]
Björn Stenberge1715c22002-03-28 14:34:13 +00001/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 2002 by Björn Stenberg
11 *
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
14 *
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
17 *
18 ****************************************************************************/
19
20#include <stdio.h>
21#include <stdlib.h>
22
23int main (int argc, char** argv)
24{
25 unsigned long length,i,slen;
26 unsigned char *inbuf,*outbuf;
27 unsigned short crc=0;
28 unsigned char header[6];
29 FILE* file;
30
31 if (argc < 3) {
32 printf("usage: %s <input file> <output file>\n",argv[0]);
33 return -1;
34 }
35
36 /* open file */
37 file = fopen(argv[1],"rb");
38 if (!file) {
39 perror(argv[1]);
40 return -1;
41 }
42 fseek(file,0,SEEK_END);
43 length = ftell(file);
44 fseek(file,0,SEEK_SET);
45 inbuf = malloc(length);
46 outbuf = malloc(length);
47 if ( !inbuf || !outbuf ) {
48 printf("out of memory!\n");
49 return -1;
50 }
51
52 /* read file */
53 i=fread(inbuf,1,length,file);
54 if ( !i ) {
55 perror(argv[1]);
56 return -1;
57 }
58 fclose(file);
59
60 /* scramble */
61 slen = length/4;
62 for (i = 0; i < length; i++) {
63 unsigned long addr = (i >> 2) + ((i % 4) * slen);
64 unsigned char data = inbuf[i];
65 data = ~((data << 1) | ((data >> 7) & 1)); /* poor man's ROL */
66 outbuf[addr] = data;
67 }
68
69 /* calculate checksum */
70 for (i=0;i<length;i++)
71 crc += inbuf[i];
72
73 /* make header */
74 header[0] = (length >> 24) & 0xff;
75 header[1] = (length >> 16) & 0xff;
76 header[2] = (length >> 8) & 0xff;
77 header[3] = length & 0xff;
78 header[4] = (crc >> 8) & 0xff;
79 header[5] = crc & 0xff;
80
81 /* write file */
82 file = fopen(argv[2],"wb");
83 if ( !file ) {
84 perror(argv[2]);
85 return -1;
86 }
87 if ( !fwrite(header,6,1,file) ) {
88 perror(argv[2]);
89 return -1;
90 }
91 if ( !fwrite(outbuf,length,1,file) ) {
92 perror(argv[2]);
93 return -1;
94 }
95 fclose(file);
96
97 free(inbuf);
98 free(outbuf);
99
100 return 0;
101}