summaryrefslogtreecommitdiffstats
path: root/tools/descramble.c
blob: d9a4bf59d9ae54c2dc901d398965db360dd9b875 (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) 2002 by Björn Stenberg
 *
 * 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>

int main (int argc, char** argv)
{
    unsigned long length,i,slen;
    unsigned char *inbuf,*outbuf;
    FILE* file;

    if (argc < 3) {
       printf("usage: %s <input file> <output file>\n",argv[0]);
       return -1;
    }
    
    /* open file and check size */
    file = fopen(argv[1],"rb");
    if (!file) {
       perror(argv[1]);
       return -1;
    }
    fseek(file,0,SEEK_END);
    length = ftell(file) - 6; /* skip 6-byte header */
    fseek(file,6,SEEK_SET); 
    inbuf = malloc(length);
    outbuf = malloc(length);
    if ( !inbuf || !outbuf ) {
       printf("out of memory!\n");
       return -1;
    }

    /* read file */
    i=fread(inbuf,1,length,file);
    if ( !i ) {
       perror(argv[1]);
       return -1;
    }
    fclose(file);

    /* descramble */
    slen = length/4;
    for (i = 0; i < length; i++) {
      unsigned long addr = ((i % slen) << 2) + i/slen;
      unsigned char data = inbuf[i];
      data = ~((data >> 1) | ((data << 7) & 0x80)); /* poor man's ROR */
      outbuf[addr] = data;
    }
    
    /* write file */
    file = fopen(argv[2],"wb");
    if ( !file ) {
       perror(argv[2]);
       return -1;
    }
    if ( !fwrite(outbuf,length,1,file) ) {
       perror(argv[2]);
       return -1;
    }
    fclose(file);
    
    free(inbuf);
    free(outbuf);
    
    return 0;	
}