summaryrefslogtreecommitdiffstats
path: root/tools/gigabeat.c
blob: f4d64ea511fd5599dcf4de22414bd7fd5855a41a (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
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2006 by Christian Hack
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <stdio.h>
#include <stdlib.h>

static FILE * openinfile( const char * filename )
{
    FILE * F = fopen( filename, "rb" );
    if( F == NULL )
    {
        fprintf( stderr, "Couldn't open input file %s\n", filename );
        perror( "Error was " );
        exit( -1 );
    };
    return F;
};

static FILE * openoutfile( const char * filename )
{
    FILE * F = fopen( filename, "wb" );
    if( F == NULL )
    {
        fprintf( stderr, "Couldn't open output file %s\n", filename );
        perror( "Error was " );
        exit( -1 );
    };
    return F;
};

int gigabeat_code(char *infile, char *outfile)
{
    FILE *in, *out;
    unsigned long size = 0;
    unsigned long bytes_read;
    unsigned char buf[4];
    unsigned long data;
    unsigned long key = 0x19751217;

    in = openinfile(infile);
    out = openoutfile(outfile);

    while (!feof(in)) {
        bytes_read = fread(buf, 1, 4, in);

        /* Read in little-endian */
        data = le2int(buf);
        
        data = data ^ key;

        key = key + (key << 1);
        key = key + 0x19751217;

        size += bytes_read;

        /* Write out little-endian */
        int2le(data, buf);

        fwrite(buf, 1, bytes_read, out);
    }

    fprintf(stderr, "File processed successfully\n" );

    fclose(in);
    fclose(out);
    return(0);
}