From 149a7bab5bcd835a7370c3ae19a6d193a0d92e84 Mon Sep 17 00:00:00 2001 From: Barry Wardell Date: Thu, 22 Mar 2007 15:53:37 +0000 Subject: Make Gigabeat's scramble endian-safe. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@12892 a1c6a512-1295-4272-9138-f99709370657 --- tools/descramble.c | 15 +++++++++++++++ tools/gigabeat.c | 11 +++++++++-- tools/scramble.c | 7 +++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/tools/descramble.c b/tools/descramble.c index c993c9f25e..9d45b8c0dc 100644 --- a/tools/descramble.c +++ b/tools/descramble.c @@ -26,6 +26,21 @@ int iaudio_decode(char *iname, char *oname); +unsigned int le2int(unsigned char* buf) +{ + int32_t res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; + + return res; +} + +void int2le(unsigned int val, unsigned char* addr) +{ + addr[0] = val & 0xFF; + addr[1] = (val >> 8) & 0xff; + addr[2] = (val >> 16) & 0xff; + addr[3] = (val >> 24) & 0xff; +} + void usage(void) { printf("usage: descramble [options] \n"); diff --git a/tools/gigabeat.c b/tools/gigabeat.c index 863d0741f2..f4d64ea511 100644 --- a/tools/gigabeat.c +++ b/tools/gigabeat.c @@ -49,6 +49,7 @@ int gigabeat_code(char *infile, char *outfile) FILE *in, *out; unsigned long size = 0; unsigned long bytes_read; + unsigned char buf[4]; unsigned long data; unsigned long key = 0x19751217; @@ -56,8 +57,11 @@ int gigabeat_code(char *infile, char *outfile) out = openoutfile(outfile); while (!feof(in)) { - bytes_read = fread(&data, 1, 4, in); + bytes_read = fread(buf, 1, 4, in); + /* Read in little-endian */ + data = le2int(buf); + data = data ^ key; key = key + (key << 1); @@ -65,7 +69,10 @@ int gigabeat_code(char *infile, char *outfile) size += bytes_read; - fwrite(&data, 1, bytes_read, out); + /* Write out little-endian */ + int2le(data, buf); + + fwrite(buf, 1, bytes_read, out); } fprintf(stderr, "File processed successfully\n" ); diff --git a/tools/scramble.c b/tools/scramble.c index 7e6ca1f51c..555bf84291 100644 --- a/tools/scramble.c +++ b/tools/scramble.c @@ -52,6 +52,13 @@ void short2le(unsigned short val, unsigned char* addr) addr[1] = (val >> 8) & 0xff; } +unsigned int le2int(unsigned char* buf) +{ + int32_t res = (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]; + + return res; +} + void int2le(unsigned int val, unsigned char* addr) { addr[0] = val & 0xFF; -- cgit v1.2.3