summaryrefslogtreecommitdiffstats
path: root/tools/scramble.c
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-03-28 14:34:13 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-03-28 14:34:13 +0000
commite1715c27e08de52f38ff2bcc5fefe97fa16664dc (patch)
tree0207f01f7a040f69ae581a49b371843e22a38f1c /tools/scramble.c
parent68ee35c679b680cd7083e40a630ca065f14c7c75 (diff)
downloadrockbox-e1715c27e08de52f38ff2bcc5fefe97fa16664dc.tar.gz
rockbox-e1715c27e08de52f38ff2bcc5fefe97fa16664dc.tar.bz2
rockbox-e1715c27e08de52f38ff2bcc5fefe97fa16664dc.zip
First checkin
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@55 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'tools/scramble.c')
-rw-r--r--tools/scramble.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/tools/scramble.c b/tools/scramble.c
new file mode 100644
index 0000000000..5b59cf3d64
--- /dev/null
+++ b/tools/scramble.c
@@ -0,0 +1,101 @@
+/***************************************************************************
+ * __________ __ ___.
+ * Open \______ \ ____ ____ | | _\_ |__ _______ ___
+ * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
+ * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
+ * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
+ * \/ \/ \/ \/ \/
+ * $Id$
+ *
+ * Copyright (C) 2002 by Bjrn Stenberg
+ *
+ * All files in this archive are subject to the GNU General Public License.
+ * See the file COPYING in the source tree root for full license agreement.
+ *
+ * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
+ * KIND, either express or implied.
+ *
+ ****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+
+int main (int argc, char** argv)
+{
+ unsigned long length,i,slen;
+ unsigned char *inbuf,*outbuf;
+ unsigned short crc=0;
+ unsigned char header[6];
+ FILE* file;
+
+ if (argc < 3) {
+ printf("usage: %s <input file> <output file>\n",argv[0]);
+ return -1;
+ }
+
+ /* open file */
+ file = fopen(argv[1],"rb");
+ if (!file) {
+ perror(argv[1]);
+ return -1;
+ }
+ fseek(file,0,SEEK_END);
+ length = ftell(file);
+ fseek(file,0,SEEK_SET);
+ inbuf = malloc(length);
+ outbuf = malloc(length);
+ if ( !inbuf || !outbuf ) {
+ printf("out of memory!\n");
+ return -1;
+ }
+
+ /* read file */
+ i=fread(inbuf,1,length,file);
+ if ( !i ) {
+ perror(argv[1]);
+ return -1;
+ }
+ fclose(file);
+
+ /* scramble */
+ slen = length/4;
+ for (i = 0; i < length; i++) {
+ unsigned long addr = (i >> 2) + ((i % 4) * slen);
+ unsigned char data = inbuf[i];
+ data = ~((data << 1) | ((data >> 7) & 1)); /* poor man's ROL */
+ outbuf[addr] = data;
+ }
+
+ /* calculate checksum */
+ for (i=0;i<length;i++)
+ crc += inbuf[i];
+
+ /* make header */
+ header[0] = (length >> 24) & 0xff;
+ header[1] = (length >> 16) & 0xff;
+ header[2] = (length >> 8) & 0xff;
+ header[3] = length & 0xff;
+ header[4] = (crc >> 8) & 0xff;
+ header[5] = crc & 0xff;
+
+ /* write file */
+ file = fopen(argv[2],"wb");
+ if ( !file ) {
+ perror(argv[2]);
+ return -1;
+ }
+ if ( !fwrite(header,6,1,file) ) {
+ perror(argv[2]);
+ return -1;
+ }
+ if ( !fwrite(outbuf,length,1,file) ) {
+ perror(argv[2]);
+ return -1;
+ }
+ fclose(file);
+
+ free(inbuf);
+ free(outbuf);
+
+ return 0;
+}