Add myself to docs/COMMITTERS and move on to the next chapter of Advanced SVN for...
[kugel-rb.git] / tools / gigabeat.c
blob554b8ff05268a678fe70207e30224ccaf3bcf1c5
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2006 by Christian Hack
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #include <stdio.h>
23 #include <stdlib.h>
25 extern void int2le(unsigned int val, unsigned char* addr);
26 extern unsigned int le2int(unsigned char* buf);
28 static FILE * openinfile( const char * filename )
30 FILE * F = fopen( filename, "rb" );
31 if( F == NULL )
33 fprintf( stderr, "Couldn't open input file %s\n", filename );
34 perror( "Error was " );
35 exit( -1 );
37 return F;
40 static FILE * openoutfile( const char * filename )
42 FILE * F = fopen( filename, "wb" );
43 if( F == NULL )
45 fprintf( stderr, "Couldn't open output file %s\n", filename );
46 perror( "Error was " );
47 exit( -1 );
49 return F;
52 int gigabeat_code(char *infile, char *outfile)
54 FILE *in, *out;
55 unsigned long size = 0;
56 unsigned long bytes_read;
57 unsigned char buf[4];
58 unsigned long data;
59 unsigned long key = 0x19751217;
61 in = openinfile(infile);
62 out = openoutfile(outfile);
64 while (!feof(in)) {
65 bytes_read = fread(buf, 1, 4, in);
67 /* Read in little-endian */
68 data = le2int(buf);
70 data = data ^ key;
72 key = key + (key << 1);
73 key = key + 0x19751217;
75 size += bytes_read;
77 /* Write out little-endian */
78 int2le(data, buf);
80 fwrite(buf, 1, bytes_read, out);
83 fprintf(stderr, "File processed successfully\n" );
85 fclose(in);
86 fclose(out);
87 return(0);