fix endianness bug in MODE SENSE 10 response
[kugel-rb.git] / tools / descramble.c
blob3d09bfad75ffe3a9c33445a2651f054aace5d2c1
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
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 ****************************************************************************/
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
24 #include "iriver.h"
25 #include "gigabeat.h"
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];
33 return res;
36 void int2le(unsigned int val, unsigned char* addr)
38 addr[0] = val & 0xFF;
39 addr[1] = (val >> 8) & 0xff;
40 addr[2] = (val >> 16) & 0xff;
41 addr[3] = (val >> 24) & 0xff;
44 void usage(void)
46 printf("usage: descramble [options] <input file> <output file>\n");
47 printf("options:\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");
55 exit(1);
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];
65 int headerlen = 6;
66 int descramble = 1;
67 FILE* file;
69 if (argc < 3) {
70 usage();
73 if (!strcmp(argv[1], "-fm") || !strcmp(argv[1], "-v2")) {
74 headerlen = 24;
75 iname = argv[2];
76 oname = argv[3];
79 if (!strcmp(argv[1], "-mm")) {
80 headerlen = 16;
81 iname = argv[2];
82 oname = argv[3];
83 descramble = 0;
86 if(!strcmp(argv[1], "-iriver")) {
87 /* iRiver code dealt with in the iriver.c code */
88 iname = argv[2];
89 oname = argv[3];
90 iriver_decode(iname, oname, FALSE, STRIP_NONE);
91 return 0;
93 if(!strcmp(argv[1], "-gigabeat")) {
94 iname = argv[2];
95 oname = argv[3];
96 gigabeat_code(iname, oname);
97 return 0;
100 if(!strcmp(argv[1], "-iaudio")) {
101 iname = argv[2];
102 oname = argv[3];
103 return iaudio_decode(iname, oname);
106 /* open file and check size */
107 file = fopen(iname,"rb");
108 if (!file) {
109 perror(iname);
110 return -1;
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);
116 if ( !i ) {
117 perror(iname);
118 return -1;
121 inbuf = malloc(length);
122 outbuf = malloc(length);
123 if ( !inbuf || !outbuf ) {
124 printf("out of memory!\n");
125 return -1;
128 /* read file */
129 i=fread(inbuf,1,length,file);
130 if ( !i ) {
131 perror(iname);
132 return -1;
134 fclose(file);
136 if (descramble) {
137 /* descramble */
138 slen = length/4;
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 */
143 outbuf[addr] = data;
146 else {
147 void* tmpptr;
148 unsigned int j=0;
149 int stringlen = 32;
150 int unpackedsize;
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;
162 int bytecount[256];
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) {
172 top = bytecount[c];
173 topchar = c;
176 xorstring[i] = topchar;
178 printf("XOR string: %.*s\n", stringlen, xorstring);
180 /* xor the buffer */
181 for (i=0; i<length; i++)
182 outbuf[i] = inbuf[i] ^ xorstring[i & (stringlen-1)];
184 /* unpack */
185 tmpptr = realloc(inbuf, unpackedsize);
186 memset(tmpptr, 0, unpackedsize);
187 inbuf = outbuf;
188 outbuf = tmpptr;
190 for (i=0; i<length;) {
191 int bit;
192 int head = inbuf[i++];
194 for (bit=0; bit<8 && i<length; bit++) {
195 if (head & (1 << (bit))) {
196 outbuf[j++] = inbuf[i++];
198 else {
199 int x;
200 int byte1 = inbuf[i];
201 int byte2 = inbuf[i+1];
202 int count = (byte2 & 0x0f) + 3;
203 int src =
204 (j & 0xfffff000) + (byte1 | ((byte2 & 0xf0)<<4)) + 18;
205 if (src > j)
206 src -= 0x1000;
208 for (x=0; x<count; x++)
209 outbuf[j++] = outbuf[src+x];
210 i += 2;
214 length = j;
217 /* write file */
218 file = fopen(oname,"wb");
219 if ( !file ) {
220 perror(argv[2]);
221 return -1;
223 if ( !fwrite(outbuf,length,1,file) ) {
224 perror(argv[2]);
225 return -1;
227 fclose(file);
229 free(inbuf);
230 free(outbuf);
232 return 0;
235 int iaudio_decode(char *iname, char *oname)
237 size_t len;
238 int length;
239 FILE *file;
240 char *outbuf;
241 int i;
242 unsigned char sum = 0;
243 unsigned char filesum;
245 file = fopen(iname, "rb");
246 if (!file) {
247 perror(iname);
248 return -1;
250 fseek(file,0,SEEK_END);
251 length = ftell(file);
253 fseek(file,0,SEEK_SET);
254 outbuf = malloc(length);
256 if ( !outbuf ) {
257 printf("out of memory!\n");
258 return -1;
261 len = fread(outbuf, 1, length, file);
262 if(len < length) {
263 perror(iname);
264 return -2;
267 fclose(file);
269 for(i = 0; i < length-0x1030;i++)
270 sum += outbuf[0x1030 + i];
272 filesum = outbuf[0x102b];
274 if(filesum != sum) {
275 printf("Checksum mismatch!\n");
276 return -1;
279 file = fopen(oname, "wb");
280 if (!file) {
281 perror(oname);
282 return -3;
285 len = fwrite(outbuf+0x1030, 1, length-0x1030, file);
286 if(len < length-0x1030) {
287 perror(oname);
288 return -4;
291 fclose(file);
292 return 0;