1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
10 * Copyright (C) 2002 by Björn Stenberg
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.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
27 int iaudio_decode(char *iname
, char *oname
);
29 unsigned int le2int(unsigned char* buf
)
31 unsigned int res
= (buf
[3] << 24) | (buf
[2] << 16) | (buf
[1] << 8) | buf
[0];
36 void int2le(unsigned int val
, unsigned char* addr
)
39 addr
[1] = (val
>> 8) & 0xff;
40 addr
[2] = (val
>> 16) & 0xff;
41 addr
[3] = (val
>> 24) & 0xff;
46 printf("usage: descramble [options] <input file> <output file>\n");
48 "\t-fm Archos FM recorder format\n"
49 "\t-v2 Archos V2 recorder format\n"
50 "\t-mm Archos Multimedia format\n"
51 "\t-iriver iRiver format\n"
52 "\t-gigabeat Toshiba Gigabeat format\n"
53 "\t-iaudio iAudio format\n"
54 "\nNo option assumes Archos standard player/recorder format.\n");
58 int main (int argc
, char** argv
)
60 unsigned long length
,i
,slen
;
61 unsigned char *inbuf
,*outbuf
;
62 char *iname
= argv
[1];
63 char *oname
= argv
[2];
64 unsigned char header
[32];
73 if (!strcmp(argv
[1], "-fm") || !strcmp(argv
[1], "-v2")) {
79 if (!strcmp(argv
[1], "-mm")) {
86 if(!strcmp(argv
[1], "-iriver")) {
87 /* iRiver code dealt with in the iriver.c code */
90 iriver_decode(iname
, oname
, FALSE
, STRIP_NONE
);
93 if(!strcmp(argv
[1], "-gigabeat")) {
96 gigabeat_code(iname
, oname
);
100 if(!strcmp(argv
[1], "-iaudio")) {
103 return iaudio_decode(iname
, oname
);
106 /* open file and check size */
107 file
= fopen(iname
,"rb");
112 fseek(file
,0,SEEK_END
);
113 length
= ftell(file
) - headerlen
; /* skip header */
114 fseek(file
,0,SEEK_SET
);
115 i
= fread(header
, 1, headerlen
, file
);
121 inbuf
= malloc(length
);
122 outbuf
= malloc(length
);
123 if ( !inbuf
|| !outbuf
) {
124 printf("out of memory!\n");
129 i
=fread(inbuf
,1,length
,file
);
139 for (i
= 0; i
< length
; i
++) {
140 unsigned long addr
= ((i
% slen
) << 2) + i
/slen
;
141 unsigned char data
= inbuf
[i
];
142 data
= ~((data
>> 1) | ((data
<< 7) & 0x80)); /* poor man's ROR */
151 unsigned char xorstring
[32];
153 unpackedsize
= header
[4] | header
[5] << 8;
154 unpackedsize
|= header
[6] << 16 | header
[7] << 24;
156 length
= header
[8] | header
[9] << 8;
157 length
|= header
[10] << 16 | header
[11] << 24;
159 /* calculate the xor string used */
160 for (i
=0; i
<stringlen
; i
++) {
161 int top
=0, topchar
=0, c
;
163 memset(bytecount
, 0, sizeof(bytecount
));
165 /* gather byte frequency statistics */
166 for (c
=i
; c
<length
; c
+=stringlen
)
167 bytecount
[inbuf
[c
]]++;
169 /* find the most frequent byte */
170 for (c
=0; c
<256; c
++) {
171 if (bytecount
[c
] > top
) {
176 xorstring
[i
] = topchar
;
178 printf("XOR string: %.*s\n", stringlen
, xorstring
);
181 for (i
=0; i
<length
; i
++)
182 outbuf
[i
] = inbuf
[i
] ^ xorstring
[i
& (stringlen
-1)];
185 tmpptr
= realloc(inbuf
, unpackedsize
);
186 memset(tmpptr
, 0, unpackedsize
);
190 for (i
=0; i
<length
;) {
192 int head
= inbuf
[i
++];
194 for (bit
=0; bit
<8 && i
<length
; bit
++) {
195 if (head
& (1 << (bit
))) {
196 outbuf
[j
++] = inbuf
[i
++];
200 int byte1
= inbuf
[i
];
201 int byte2
= inbuf
[i
+1];
202 int count
= (byte2
& 0x0f) + 3;
204 (j
& 0xfffff000) + (byte1
| ((byte2
& 0xf0)<<4)) + 18;
208 for (x
=0; x
<count
; x
++)
209 outbuf
[j
++] = outbuf
[src
+x
];
218 file
= fopen(oname
,"wb");
223 if ( !fwrite(outbuf
,length
,1,file
) ) {
235 int iaudio_decode(char *iname
, char *oname
)
242 unsigned char sum
= 0;
243 unsigned char filesum
;
245 file
= fopen(iname
, "rb");
250 fseek(file
,0,SEEK_END
);
251 length
= ftell(file
);
253 fseek(file
,0,SEEK_SET
);
254 outbuf
= malloc(length
);
257 printf("out of memory!\n");
261 len
= fread(outbuf
, 1, length
, file
);
269 for(i
= 0; i
< length
-0x1030;i
++)
270 sum
+= outbuf
[0x1030 + i
];
272 filesum
= outbuf
[0x102b];
275 printf("Checksum mismatch!\n");
279 file
= fopen(oname
, "wb");
285 len
= fwrite(outbuf
+0x1030, 1, length
-0x1030, file
);
286 if(len
< length
-0x1030) {