forked from len0rd/rockbox
Added support for multimedia .ajz files
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@4199 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
parent
a55bfd9204
commit
7f749b4689
1 changed files with 95 additions and 10 deletions
|
|
@ -26,11 +26,14 @@ int main (int argc, char** argv)
|
||||||
unsigned char *inbuf,*outbuf;
|
unsigned char *inbuf,*outbuf;
|
||||||
unsigned char *iname = argv[1];
|
unsigned char *iname = argv[1];
|
||||||
unsigned char *oname = argv[2];
|
unsigned char *oname = argv[2];
|
||||||
|
unsigned char header[32];
|
||||||
int headerlen = 6;
|
int headerlen = 6;
|
||||||
|
int descramble = 1;
|
||||||
FILE* file;
|
FILE* file;
|
||||||
|
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
printf("usage: %s [-fm] [-v2] <input file> <output file>\n",argv[0]);
|
printf("usage: %s [-fm] [-v2] [-mm] <input file> <output file>\n",
|
||||||
|
argv[0]);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -39,6 +42,13 @@ int main (int argc, char** argv)
|
||||||
iname = argv[2];
|
iname = argv[2];
|
||||||
oname = argv[3];
|
oname = argv[3];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!strcmp(argv[1], "-mm")) {
|
||||||
|
headerlen = 16;
|
||||||
|
iname = argv[2];
|
||||||
|
oname = argv[3];
|
||||||
|
descramble = 0;
|
||||||
|
}
|
||||||
|
|
||||||
/* open file and check size */
|
/* open file and check size */
|
||||||
file = fopen(iname,"rb");
|
file = fopen(iname,"rb");
|
||||||
|
|
@ -48,7 +58,13 @@ int main (int argc, char** argv)
|
||||||
}
|
}
|
||||||
fseek(file,0,SEEK_END);
|
fseek(file,0,SEEK_END);
|
||||||
length = ftell(file) - headerlen; /* skip header */
|
length = ftell(file) - headerlen; /* skip header */
|
||||||
fseek(file,headerlen,SEEK_SET);
|
fseek(file,0,SEEK_SET);
|
||||||
|
i = fread(header, 1, headerlen, file);
|
||||||
|
if ( !i ) {
|
||||||
|
perror(iname);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
inbuf = malloc(length);
|
inbuf = malloc(length);
|
||||||
outbuf = malloc(length);
|
outbuf = malloc(length);
|
||||||
if ( !inbuf || !outbuf ) {
|
if ( !inbuf || !outbuf ) {
|
||||||
|
|
@ -64,15 +80,84 @@ int main (int argc, char** argv)
|
||||||
}
|
}
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
/* descramble */
|
if (descramble) {
|
||||||
slen = length/4;
|
/* descramble */
|
||||||
for (i = 0; i < length; i++) {
|
slen = length/4;
|
||||||
unsigned long addr = ((i % slen) << 2) + i/slen;
|
for (i = 0; i < length; i++) {
|
||||||
unsigned char data = inbuf[i];
|
unsigned long addr = ((i % slen) << 2) + i/slen;
|
||||||
data = ~((data >> 1) | ((data << 7) & 0x80)); /* poor man's ROR */
|
unsigned char data = inbuf[i];
|
||||||
outbuf[addr] = data;
|
data = ~((data >> 1) | ((data << 7) & 0x80)); /* poor man's ROR */
|
||||||
|
outbuf[addr] = data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
void* tmpptr;
|
||||||
|
unsigned int j=0;
|
||||||
|
int stringlen = 32;
|
||||||
|
int unpackedsize;
|
||||||
|
unsigned char xorstring[32];
|
||||||
|
|
||||||
|
unpackedsize = ((unsigned int*)header)[1];
|
||||||
|
length = ((unsigned int*)header)[2];
|
||||||
|
|
||||||
|
/* calculate the xor string used */
|
||||||
|
for (i=0; i<stringlen; i++) {
|
||||||
|
int top=0, topchar=0, c;
|
||||||
|
int bytecount[256];
|
||||||
|
memset(bytecount, 0, sizeof(bytecount));
|
||||||
|
|
||||||
|
/* gather byte frequency statistics */
|
||||||
|
for (c=i; c<length; c+=stringlen)
|
||||||
|
bytecount[inbuf[c]]++;
|
||||||
|
|
||||||
|
/* find the most frequent byte */
|
||||||
|
for (c=0; c<256; c++) {
|
||||||
|
if (bytecount[c] > top) {
|
||||||
|
top = bytecount[c];
|
||||||
|
topchar = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
xorstring[i] = topchar;
|
||||||
|
}
|
||||||
|
printf("XOR string: %.*s\n", stringlen, xorstring);
|
||||||
|
|
||||||
|
/* xor the buffer */
|
||||||
|
for (i=0; i<length; i++)
|
||||||
|
outbuf[i] = inbuf[i] ^ xorstring[i & (stringlen-1)];
|
||||||
|
|
||||||
|
/* unpack */
|
||||||
|
tmpptr = realloc(inbuf, unpackedsize);
|
||||||
|
memset(tmpptr, 0, unpackedsize);
|
||||||
|
inbuf = outbuf;
|
||||||
|
outbuf = tmpptr;
|
||||||
|
|
||||||
|
for (i=0; i<length;) {
|
||||||
|
int bit;
|
||||||
|
int head = inbuf[i++];
|
||||||
|
|
||||||
|
for (bit=0; bit<8 && i<length; bit++) {
|
||||||
|
if (head & (1 << (bit))) {
|
||||||
|
outbuf[j++] = inbuf[i++];
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
int x;
|
||||||
|
int byte1 = inbuf[i];
|
||||||
|
int byte2 = inbuf[i+1];
|
||||||
|
int count = (byte2 & 0x0f) + 3;
|
||||||
|
int src =
|
||||||
|
(j & 0xfffff000) + (byte1 | ((byte2 & 0xf0)<<4)) + 18;
|
||||||
|
if (src > j)
|
||||||
|
src -= 0x1000;
|
||||||
|
|
||||||
|
for (x=0; x<count; x++)
|
||||||
|
outbuf[j++] = outbuf[src+x];
|
||||||
|
i += 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
length = j;
|
||||||
|
}
|
||||||
|
|
||||||
/* write file */
|
/* write file */
|
||||||
file = fopen(oname,"wb");
|
file = fopen(oname,"wb");
|
||||||
if ( !file ) {
|
if ( !file ) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue