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
);
31 printf("usage: descramble [options] <input file> <output file>\n");
33 "\t-fm Archos FM recorder format\n"
34 "\t-v2 Archos V2 recorder format\n"
35 "\t-mm=X Archos Multimedia format (X values: A=JBMM, B=AV1xx, C=AV3xx)\n"
36 "\t-iriver iRiver format\n"
37 "\t-gigabeat Toshiba Gigabeat format\n"
38 "\t-iaudio iAudio format\n"
39 "\nNo option assumes Archos standard player/recorder format.\n");
43 int main (int argc
, char** argv
)
45 unsigned long length
,i
,slen
;
46 unsigned char *inbuf
,*outbuf
;
47 char *iname
= argv
[1];
48 char *oname
= argv
[2];
49 unsigned char header
[32];
58 if (!strcmp(argv
[1], "-fm") || !strcmp(argv
[1], "-v2")) {
64 if (!strcmp(argv
[1], "-mm")) {
71 if(!strcmp(argv
[1], "-iriver")) {
72 /* iRiver code dealt with in the iriver.c code */
75 iriver_decode(iname
, oname
, FALSE
, STRIP_NONE
);
78 if(!strcmp(argv
[1], "-gigabeat")) {
81 gigabeat_code(iname
, oname
);
85 if(!strcmp(argv
[1], "-iaudio")) {
88 return iaudio_decode(iname
, oname
);
91 /* open file and check size */
92 file
= fopen(iname
,"rb");
97 fseek(file
,0,SEEK_END
);
98 length
= ftell(file
) - headerlen
; /* skip header */
99 fseek(file
,0,SEEK_SET
);
100 i
= fread(header
, 1, headerlen
, file
);
106 inbuf
= malloc(length
);
107 outbuf
= malloc(length
);
108 if ( !inbuf
|| !outbuf
) {
109 printf("out of memory!\n");
114 i
=fread(inbuf
,1,length
,file
);
124 for (i
= 0; i
< length
; i
++) {
125 unsigned long addr
= ((i
% slen
) << 2) + i
/slen
;
126 unsigned char data
= inbuf
[i
];
127 data
= ~((data
>> 1) | ((data
<< 7) & 0x80)); /* poor man's ROR */
136 unsigned char xorstring
[32];
138 unpackedsize
= header
[4] | header
[5] << 8;
139 unpackedsize
|= header
[6] << 16 | header
[7] << 24;
141 length
= header
[8] | header
[9] << 8;
142 length
|= header
[10] << 16 | header
[11] << 24;
144 /* calculate the xor string used */
145 for (i
=0; i
<stringlen
; i
++) {
146 int top
=0, topchar
=0, c
;
148 memset(bytecount
, 0, sizeof(bytecount
));
150 /* gather byte frequency statistics */
151 for (c
=i
; c
<length
; c
+=stringlen
)
152 bytecount
[inbuf
[c
]]++;
154 /* find the most frequent byte */
155 for (c
=0; c
<256; c
++) {
156 if (bytecount
[c
] > top
) {
161 xorstring
[i
] = topchar
;
163 printf("XOR string: %.*s\n", stringlen
, xorstring
);
166 for (i
=0; i
<length
; i
++)
167 outbuf
[i
] = inbuf
[i
] ^ xorstring
[i
& (stringlen
-1)];
170 tmpptr
= realloc(inbuf
, unpackedsize
);
171 memset(tmpptr
, 0, unpackedsize
);
175 for (i
=0; i
<length
;) {
177 int head
= inbuf
[i
++];
179 for (bit
=0; bit
<8 && i
<length
; bit
++) {
180 if (head
& (1 << (bit
))) {
181 outbuf
[j
++] = inbuf
[i
++];
185 int byte1
= inbuf
[i
];
186 int byte2
= inbuf
[i
+1];
187 int count
= (byte2
& 0x0f) + 3;
189 (j
& 0xfffff000) + (byte1
| ((byte2
& 0xf0)<<4)) + 18;
193 for (x
=0; x
<count
; x
++)
194 outbuf
[j
++] = outbuf
[src
+x
];
203 file
= fopen(oname
,"wb");
208 if ( !fwrite(outbuf
,length
,1,file
) ) {
220 int iaudio_decode(char *iname
, char *oname
)
227 unsigned char sum
= 0;
228 unsigned char filesum
;
230 file
= fopen(iname
, "rb");
235 fseek(file
,0,SEEK_END
);
236 length
= ftell(file
);
238 fseek(file
,0,SEEK_SET
);
239 outbuf
= malloc(length
);
242 printf("out of memory!\n");
246 len
= fread(outbuf
, 1, length
, file
);
254 for(i
= 0; i
< length
-0x1030;i
++)
255 sum
+= outbuf
[0x1030 + i
];
257 filesum
= outbuf
[0x102b];
260 printf("Checksum mismatch!\n");
264 file
= fopen(oname
, "wb");
270 len
= fwrite(outbuf
+0x1030, 1, length
-0x1030, file
);
271 if(len
< length
-0x1030) {