]> xmof Git - DeDRM.git/commitdiff
skindle 6 with Topaz support
authorBart Simpson <bart.simpson@gmail.com>
Sat, 2 Jan 2010 21:21:06 +0000 (21:21 +0000)
committerApprentice Alf <apprenticealf@gmail.com>
Sat, 28 Feb 2015 10:21:44 +0000 (10:21 +0000)
15 files changed:
skindle/Makefile
skindle/README.txt
skindle/cbuf.c [new file with mode: 0644]
skindle/cbuf.h [new file with mode: 0644]
skindle/libz.a [new file with mode: 0644]
skindle/mobi.c [new file with mode: 0644]
skindle/mobi.h [new file with mode: 0644]
skindle/skindle.c
skindle/skindle.exe
skindle/skinutils.c [new file with mode: 0644]
skindle/skinutils.h [new file with mode: 0644]
skindle/tpz.c [new file with mode: 0644]
skindle/tpz.h [new file with mode: 0644]
skindle/zconf.h [new file with mode: 0644]
skindle/zlib.h [new file with mode: 0644]

index 2eb8f3caa5e40aa1547a4c876f0347c5028e8082..d4f69388b7ad1a502df3e70a79400a8f98628391 100644 (file)
@@ -1,21 +1,23 @@
 
-OBJS=skindle.o md5.o sha1.o b64.o
+OBJS=skindle.o md5.o sha1.o b64.o skinutils.o cbuf.o mobi.o tpz.o
 
 CC=gcc
 LD=gcc
 EXE=skindle
-EXTRALIBS=-lCrypt32
+EXTRALIBS=libz.a -lCrypt32 -lAdvapi32
+CFLAGS=-mno-cygwin
 
 #use the following to strip your binary
-LDFLAGS=-s
+LDFLAGS=-s -mno-cygwin
+#LDFLAGS=-mno-cygwin
 
 all: $(EXE)
 
 %.o: %.c
-       $(CC) -c $(CFLAGS) $(INC) $< -o $@
+       $(CC) -c $(CFLAGS) -g $(INC) $< -o $@
 
 $(EXE): $(OBJS)
-       $(LD) $(LDFLAGS) -o $@ $(OBJS) $(EXTRALIBS)
+       $(LD) $(LDFLAGS) -o $@ -g $(OBJS) $(EXTRALIBS)
 
 clean:
        -@rm *.o
index 132844c07d7fcd34a9370d30b3fb299e5d6e53f0..bf0a889ef010d5c313121bc846b2d927972c018c 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
-   Copyright 2010 BartSimpson
+   Copyright 2010 BartSimpson aka skindle
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
 */
 
 /*
- * Dependencies: none
- * build on cygwin: gcc -o skindle skindle.c md5.c sha1.c b64.c -lCrypt32
- * Under cygwin, you can just type make to build it.
- * While the binary builds if you use the -mno-cygwin switch, it fails to
- * work for some reason.  The code should compile with Visual Studio, just
- * add all the files to a project and add the Crypt32.lib dependency and
- * it should build as a Win32 console app.
+ * Dependencies: zlib (included)
+ * build on cygwin using make and the included make file
+ * A fully functionaly windows executable is included
  */
 
 /*
@@ -31,6 +27,7 @@
  * Requires your kindle.info file which can be found in something like:
  * <User home>\...\Amazon\Kindle For PC\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}
  * where ... varies by platform but is "Local Settings\Application Data" on XP
+ * skindle will attempt to find this file automatically.
  */
 
 /*
@@ -49,7 +46,9 @@
        You guys shouldn't need to spend all your time responding to all the
        changes Amazon is going to force you to make in unswindle each time
        the release a new version.
+       CMBDTC - nice work on the topaz break!
        Lawrence Lessig - You are my hero. 'Nuff said.
+       Cory Doctorow - A voice of reason in a sea of insanity
    Thumbs down: Disney, MPAA, RIAA - you guys suck.  Making billions off 
        of the exploitation of works out of copyright while vigourously
        pushing copyright extension to prevent others from doing the same
@@ -67,12 +66,20 @@ file and the data and algorthims that are used to derive per book
 PID values.
 
 Installing:
-A cygwin compatable binary is included.  You need a minimal cygwin
-installation in order to run it.  To build from source, you will need
-cygwin with gcc and make.  This has not been tested with Visual Studio.
+A compiled binary is included.  Though it was built using cygwin, it
+should not require a cygwin installation in order to run it.  To build
+from source, you will need cygwin with gcc and make.  
+This has not been tested with Visual Studio, though you may be able to
+pile all the files into a project and add the Crypt32.lib, Advapi32 and
+zlib1 dependencies to build it.
 
-Usage:
-./skindle <drm'ed prc file> <name of output file> <kindle.info path>
-You need to locate your kindle.info file somewhere on your system.
-You can copy it to a local directory, but you need to refer to it
-each time you run skindle.
+usage: ./skindle [-d] [-v] -i <ebook file> -o <output file> [-k kindle.info file] [-p pid]
+    -d optional, for topaz files only, produce a decompressed output file
+    -i required name of the input mobi or topaz file
+    -o required name of the output file to generate
+    -k optional kindle.info path
+    -v dump the contents of kindle.info
+    -p additional PID values to attempt (can specifiy multiple times)
+
+You only need to specify a kindle.info path if skindle can't find
+your kindle.info file automatically
\ No newline at end of file
diff --git a/skindle/cbuf.c b/skindle/cbuf.c
new file mode 100644 (file)
index 0000000..60112e2
--- /dev/null
@@ -0,0 +1,85 @@
+/*
+   Copyright 2010 BartSimpson aka skindle
+   
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+#include <stdlib.h>
+#include <string.h>
+#include "cbuf.h"
+
+cbuf *b_new(unsigned int size) {
+   cbuf *b = (cbuf*)calloc(sizeof(cbuf), 1);
+   if (b) {
+      b->buf = (unsigned char *)malloc(size);
+      b->size = b->buf ? size : 0;
+   }
+   return b;
+}
+
+void b_free(cbuf *b) {
+   if (b) {
+      free(b->buf);
+      free(b);
+   }
+}
+
+void b_add_byte(cbuf *b, unsigned char ch) {
+   if (b == NULL) return;
+   if (b->idx == b->size) {
+      unsigned char *p = realloc(b->buf, b->size * 2);
+      if (p) {
+         b->buf = p;
+         b->size = b->size * 2;
+      }
+   }
+   if (b->idx < b->size) {
+      b->buf[b->idx++] = ch;
+   }
+}
+
+void b_add_buf(cbuf *b, unsigned char *buf, unsigned int len) {
+   if (b == NULL) return;
+   unsigned int new_sz = b->idx + len;
+   while (b->size < new_sz) {
+      unsigned char *p = realloc(b->buf, b->size * 2);
+      if (p) {
+         b->buf = p;
+         b->size = b->size * 2;
+      }
+      else break;
+   }
+   if ((b->idx + len) <= b->size) {
+      memcpy(b->buf + b->idx, buf, len);
+      b->idx += len;
+   }
+}
+
+void b_add_str(cbuf *b, const char *buf) {
+   if (b == NULL) return;
+   unsigned int len = strlen(buf);
+   unsigned int new_sz = b->idx + len;
+   while (b->size < new_sz) {
+      unsigned char *p = realloc(b->buf, b->size * 2);
+      if (p) {
+         b->buf = p;
+         b->size = b->size * 2;
+      }
+      else break;
+   }
+   if ((b->idx + len) <= b->size) {
+      memcpy(b->buf + b->idx, buf, len);
+      b->idx += len;
+   }
+}
+
diff --git a/skindle/cbuf.h b/skindle/cbuf.h
new file mode 100644 (file)
index 0000000..738dfd2
--- /dev/null
@@ -0,0 +1,32 @@
+/*
+   Copyright 2010 BartSimpson aka skindle
+   
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+#ifndef __CBUF_H
+#define __CBUF_H
+
+typedef struct _cbuf {
+   unsigned int size;  //current size
+   unsigned int idx;   //current position
+   unsigned char *buf;
+} cbuf;
+
+cbuf *b_new(unsigned int size);
+void b_free(cbuf *b);
+void b_add_byte(cbuf *b, unsigned char ch);
+void b_add_buf(cbuf *b, unsigned char *buf, unsigned int len);
+void b_add_str(cbuf *b, const char *buf);
+
+#endif
diff --git a/skindle/libz.a b/skindle/libz.a
new file mode 100644 (file)
index 0000000..0a2e3b5
Binary files /dev/null and b/skindle/libz.a differ
diff --git a/skindle/mobi.c b/skindle/mobi.c
new file mode 100644 (file)
index 0000000..25de87c
--- /dev/null
@@ -0,0 +1,365 @@
+/*
+   Copyright 2010 BartSimpson aka skindle
+   
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "mobi.h"
+
+unsigned char *getExthData(MobiFile *book, unsigned int type, unsigned int *len) {
+   unsigned int i;
+   unsigned int exthRecords = bswap_l(book->exth->recordCount);
+   ExthRecHeader *erh = book->exth->records;
+
+   *len = 0;
+
+   for (i = 0; i < exthRecords; i++) {
+      unsigned int recType = bswap_l(erh->type);
+      unsigned int recLen = bswap_l(erh->len);
+
+      if (recLen < 8) {
+         fprintf(stderr, "Invalid exth record length %d\n", i);
+         return NULL;
+      }
+
+      if (recType == type) {
+         *len = recLen - 8;
+         return (unsigned char*)(erh + 1);
+      }
+      erh = (ExthRecHeader*)(recLen  + (char*)erh);
+   }
+   return NULL;
+}
+
+void enumExthRecords(ExthHeader *eh) {
+   unsigned int exthRecords = bswap_l(eh->recordCount);
+   unsigned int i;
+   unsigned char *data;
+   ExthRecHeader *erh = eh->records;
+
+   for (i = 0; i < exthRecords; i++) {
+      unsigned int recType = bswap_l(erh->type);
+      unsigned int recLen = bswap_l(erh->len);
+
+      fprintf(stderr, "%d: type - %d, len %d\n", i, recType, recLen);
+
+      if (recLen < 8) {
+         fprintf(stderr, "Invalid exth record length %d\n", i);
+         return;
+      }
+
+      data = (unsigned char*)(erh + 1);
+      switch (recType) {
+         case 1: //drm_server_id
+            fprintf(stderr, "drm_server_id: %s\n", data);
+            break;
+         case 2: //drm_commerce_id
+            fprintf(stderr, "drm_commerce_id: %s\n", data);
+            break;
+         case 3: //drm_ebookbase_book_id
+            fprintf(stderr, "drm_ebookbase_book_id: %s\n", data);
+            break;
+         case 100: //author
+            fprintf(stderr, "author: %s\n", data);
+            break;
+         case 101: //publisher
+            fprintf(stderr, "publisher: %s\n", data);
+            break;
+         case 106: //publishingdate
+            fprintf(stderr, "publishingdate: %s\n", data);
+            break;
+         case 113: //asin
+            fprintf(stderr, "asin: %s\n", data);
+            break;
+         case 208: //book unique drm key
+            fprintf(stderr, "book drm key: %s\n", data);
+            break;
+         case 503: //updatedtitle
+            fprintf(stderr, "updatedtitle: %s\n", data);
+            break;
+         default:
+            break;
+      }
+      erh = (ExthRecHeader*)(recLen  + (char*)erh);
+   }
+
+}
+
+//implementation of Pukall Cipher 1
+unsigned char *PC1(unsigned char *key, unsigned int klen, unsigned char *src,
+                   unsigned char *dest, unsigned int len, int decryption) {
+    unsigned int sum1 = 0;
+    unsigned int sum2 = 0;
+    unsigned int keyXorVal = 0;
+    unsigned short wkey[8];
+    unsigned int i;
+    if (klen != 16) {
+        fprintf(stderr, "Bad key length!\n");
+        return NULL;
+    }
+    for (i = 0; i < 8; i++) {
+        wkey[i] = (key[i * 2] << 8) | key[i * 2 + 1];
+    }
+    for (i = 0; i < len; i++) {
+        unsigned int temp1 = 0;
+        unsigned int byteXorVal = 0;
+        unsigned int j, curByte;
+        for (j = 0; j < 8; j++) {
+            temp1 ^= wkey[j];
+            sum2 = (sum2 + j) * 20021 + sum1;
+            sum1 = (temp1 * 346) & 0xFFFF;
+            sum2 = (sum2 + sum1) & 0xFFFF;
+            temp1 = (temp1 * 20021 + 1) & 0xFFFF;
+            byteXorVal ^= temp1 ^ sum2;
+        }
+        curByte = src[i];
+        if (!decryption) {
+            keyXorVal = curByte * 257;
+        }
+        curByte = ((curByte ^ (byteXorVal >> 8)) ^ byteXorVal) & 0xFF;
+        if (decryption) {
+            keyXorVal = curByte * 257;
+        }
+        for (j = 0; j < 8; j++) {
+            wkey[j] ^= keyXorVal;
+        }
+        dest[i] = curByte;
+    }
+    return dest;
+}
+
+unsigned int getSizeOfTrailingDataEntry(unsigned char *ptr, unsigned int size) {
+   unsigned int bitpos = 0;
+   unsigned int result = 0;
+   if (size <= 0) {
+      return result;
+   }
+   while (1) {
+      unsigned int v = ptr[size - 1];
+      result |= (v & 0x7F) << bitpos;
+      bitpos += 7;
+      size -= 1;
+      if ((v & 0x80) != 0 || (bitpos >= 28) || (size == 0)) {
+         return result;
+      }
+   }
+}
+
+unsigned int getSizeOfTrailingDataEntries(unsigned char *ptr, unsigned int size, unsigned int flags) {
+   unsigned int num = 0;
+   unsigned int testflags = flags >> 1;
+   while (testflags) {
+      if (testflags & 1) {
+         num += getSizeOfTrailingDataEntry(ptr, size - num);
+      }
+      testflags >>= 1;
+   }
+   if (flags & 1) {
+      num += (ptr[size - num - 1] & 0x3) + 1;
+   }
+   return num;
+}
+
+unsigned char *parseDRM(unsigned char *data, unsigned int count, unsigned char *pid, unsigned int pidlen) {
+   unsigned int i;
+   unsigned char temp_key_sum = 0;
+   unsigned char *found_key = NULL;
+   unsigned char *keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96";
+   unsigned char temp_key[16];
+
+   memset(temp_key, 0, 16);
+   memcpy(temp_key, pid, 8);
+   PC1(keyvec1, 16, temp_key, temp_key, 16, 0);
+
+   for (i = 0; i < 16; i++) {
+      temp_key_sum += temp_key[i];
+   }
+
+   for (i = 0; i < count; i++) {
+      unsigned char kk[32];
+      vstruct *v = (vstruct*)(data + i * 0x30);
+      kstruct *k = (kstruct*)PC1(temp_key, 16, v->cookie, kk, 32, 1);
+
+      if (v->verification == k->ver && v->cksum[0] == temp_key_sum &&
+          (bswap_l(k->flags) & 0x1F) == 1) {
+         found_key = (unsigned char*)malloc(16);
+         memcpy(found_key, k->finalkey, 16);
+         break;
+      }
+   }
+   return found_key;
+}
+
+void freeMobiFile(MobiFile *book) {
+   free(book->hr);
+   free(book->record0);
+   free(book);
+}
+
+MobiFile *parseMobiHeader(FILE *f) {
+   unsigned int numRecs, i, magic;
+   MobiFile *book = (MobiFile*)calloc(sizeof(MobiFile), 1);
+   book->f = f;
+   if (fread(&book->pdb, sizeof(PDB), 1, f) != 1) {
+      fprintf(stderr, "Failed to read Palm headers\n");
+      free(book);
+      return NULL;
+   }
+
+   //do BOOKMOBI check
+   if (book->pdb.type != 0x4b4f4f42 || book->pdb.creator != 0x49424f4d) {
+      fprintf(stderr, "Invalid header type or creator\n");
+      free(book);
+      return NULL;
+   }
+
+   book->recs = bswap_s(book->pdb.numRecs);
+
+   book->hr = (HeaderRec*)malloc(book->recs * sizeof(HeaderRec));
+   if (fread(book->hr, sizeof(HeaderRec), book->recs, f) != book->recs) {
+      fprintf(stderr, "Failed read of header record\n");
+      freeMobiFile(book);
+      return NULL;
+   }
+
+   book->record0_offset = bswap_l(book->hr[0].offset);
+   book->record0_size = bswap_l(book->hr[1].offset) - book->record0_offset;
+
+   if (fseek(f, book->record0_offset, SEEK_SET) == -1) {
+      fprintf(stderr, "bad seek to header record offset\n");
+      freeMobiFile(book);
+      return NULL;
+   }
+
+   book->record0 = (unsigned char*)malloc(book->record0_size);
+
+   if (fread(book->record0, book->record0_size, 1, f) != 1) {
+      fprintf(stderr, "bad read of record0\n");
+      freeMobiFile(book);
+      return NULL;
+   }
+
+   book->pdh = (PalmDocHeader*)(book->record0);
+   if (bswap_s(book->pdh->encryptionType) != 2) {
+      fprintf(stderr, "MOBI BOOK is not encrypted\n");
+      freeMobiFile(book);
+      return NULL;
+   }
+
+   book->textRecs = bswap_s(book->pdh->recordCount);
+
+   book->mobi = (MobiHeader*)(book->pdh + 1);
+   if (book->mobi->id != 0x49424f4d) {
+      fprintf(stderr, "MOBI header not found\n");
+      freeMobiFile(book);
+      return NULL;
+   }
+
+   book->mobiLen = bswap_l(book->mobi->hdrLen);
+   book->extra_data_flags = 0;
+   
+   if (book->mobiLen >= 0xe4) {
+      book->extra_data_flags = bswap_s(book->mobi->extra_flags);
+   }
+
+   if ((bswap_l(book->mobi->exthFlags) & 0x40) == 0) {
+      fprintf(stderr, "Missing exth header\n");
+      freeMobiFile(book);
+      return NULL;
+   }
+
+   book->exth = (ExthHeader*)(book->mobiLen + (char*)(book->mobi));
+   if (book->exth->id != 0x48545845) {
+      fprintf(stderr, "EXTH header not found\n");
+      freeMobiFile(book);
+      return NULL;
+   }
+  
+   //if you want a list of EXTH records, uncomment the following
+//   enumExthRecords(exth);
+
+   book->drmCount = bswap_l(book->mobi->drmCount);
+
+   if (book->drmCount == 0) {
+      fprintf(stderr, "no PIDs found in this file\n");
+      freeMobiFile(book);
+      return NULL;
+   }
+
+   return book;
+}
+
+int writeMobiOutputFile(MobiFile *book, FILE *out, unsigned char *key, 
+                        unsigned int drmOffset, unsigned int drm_len) {
+   int i;
+   struct stat statbuf;
+
+   fstat(fileno(book->f), &statbuf);
+
+   // kill the drm keys
+   memset(book->record0 + drmOffset, 0, drm_len);
+   // kill the drm pointers
+   book->mobi->drmOffset = 0xffffffff;
+   book->mobi->drmCount = book->mobi->drmSize = book->mobi->drmFlags = 0;
+   // clear the crypto type
+   book->pdh->encryptionType = 0;
+
+   fwrite(&book->pdb, sizeof(PDB), 1, out);
+   fwrite(book->hr, sizeof(HeaderRec), book->recs, out);
+   fwrite("\x00\x00", 1, 2, out);
+   fwrite(book->record0, book->record0_size, 1, out);
+
+   //need to zero out exth 209 data
+   for (i = 1; i < book->recs; i++) {
+      unsigned int offset = bswap_l(book->hr[i].offset);
+      unsigned int len, extra_size = 0;
+      unsigned char *rec;
+      if (i == (book->recs - 1)) {  //last record extends to end of file
+         len = statbuf.st_size - offset;
+      }
+      else {
+         len = bswap_l(book->hr[i + 1].offset) - offset;
+      }
+      //make sure we are at proper offset
+      while (ftell(out) < offset) {
+         fwrite("\x00", 1, 1, out);
+      }
+      rec = (unsigned char *)malloc(len);
+      if (fseek(book->f, offset, SEEK_SET) != 0) {
+         fprintf(stderr, "Failed record seek on input\n");
+         freeMobiFile(book);
+         free(rec);
+         return 0;
+      }
+      if (fread(rec, len, 1, book->f) != 1) {
+         fprintf(stderr, "Failed record read on input\n");
+         freeMobiFile(book);
+         free(rec);
+         return 0;
+      }
+
+      if (i <= book->textRecs) { //decrypt if necessary
+         extra_size = getSizeOfTrailingDataEntries(rec, len, book->extra_data_flags);
+         PC1(key, 16, rec, rec, len - extra_size, 1);
+      }
+      fwrite(rec, len, 1, out);
+      free(rec);
+   }
+   return 1;
+}
diff --git a/skindle/mobi.h b/skindle/mobi.h
new file mode 100644 (file)
index 0000000..61758c6
--- /dev/null
@@ -0,0 +1,147 @@
+/*
+   Copyright 2010 BartSimpson aka skindle
+   
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+#ifndef __MOBI_H
+#define __MOBI_H
+
+#include <stdio.h>
+#include "skinutils.h"
+
+#pragma pack(2)
+typedef struct _PDB {
+   char name[32];
+   unsigned short attrib;
+   unsigned short version;
+   unsigned int created;
+   unsigned int modified;
+   unsigned int backup;
+   unsigned int modNum;
+   unsigned int appInfoID;
+   unsigned int sortInfoID;
+   unsigned int type;
+   unsigned int creator;
+   unsigned int uniqueIDseed;
+   unsigned int nextRecordListID;
+   unsigned short numRecs;
+} PDB;
+
+typedef struct _HeaderRec {
+   unsigned int offset;
+   unsigned int attribId;
+} HeaderRec;
+
+#define attrib(x) ((x)&0xFF)
+#define id(x) (bswap_l((x) & 0xFFFFFF00))
+
+typedef struct _PalmDocHeader {
+   unsigned short compression;
+   unsigned short reserverd1;
+   unsigned int textLength;
+   unsigned short recordCount;
+   unsigned short recordSize;
+   unsigned short encryptionType;
+   unsigned short reserved2;
+} PalmDocHeader;
+
+
+//checked lengths are 24, 116, 208, 228
+typedef struct _MobiHeader {
+   unsigned int id;
+   unsigned int hdrLen;
+   unsigned int type;
+   unsigned int encoding;
+   unsigned int uniqueId;
+   unsigned int generator;
+   unsigned char reserved1[40];
+   unsigned int firstNonBookIdx;
+   unsigned int nameOffset;
+   unsigned int nameLength;
+   unsigned int language;
+   unsigned int inputLang;
+   unsigned int outputLang;
+   unsigned int formatVersion;
+   unsigned int firstImageIdx;
+   unsigned char unknown1[16];
+   unsigned int exthFlags;
+   unsigned char unknown2[36];
+   unsigned int drmOffset;
+   unsigned int drmCount;
+   unsigned int drmSize;
+   unsigned int drmFlags;
+   unsigned char unknown3[58];
+   unsigned short extra_flags;
+} MobiHeader;
+
+typedef struct _ExthRecHeader {
+   unsigned int type;
+   unsigned int len;
+} ExthRecHeader;
+
+typedef struct _ExthHeader {
+   unsigned int id;
+   unsigned int hdrLen;
+   unsigned int recordCount;
+   ExthRecHeader records[1];
+} ExthHeader;
+
+typedef struct _vstruct {
+   unsigned int verification;
+   unsigned int size;
+   unsigned int type;
+   unsigned char cksum[4];
+   unsigned char cookie[32];
+} vstruct;
+
+typedef struct _kstruct {
+   unsigned int ver;
+   unsigned int flags;
+   unsigned char finalkey[16];
+   unsigned int expiry;
+   unsigned int expiry2;
+} kstruct;
+
+typedef struct _MobiFile {
+   FILE *f;
+   PDB pdb;
+   HeaderRec *hr;
+   PalmDocHeader *pdh;
+   MobiHeader *mobi;
+   ExthHeader *exth; 
+   unsigned char *record0; 
+   unsigned int record0_offset;
+   unsigned int record0_size;
+   unsigned int mobiLen;
+   unsigned int extra_data_flags;
+   unsigned int recs;
+   unsigned int drmCount;
+   unsigned int textRecs;
+   PidList *pids;    //extra pids to try from command line
+} MobiFile;
+
+unsigned char *getExthData(MobiFile *book, unsigned int type, unsigned int *len);
+void enumExthRecords(ExthHeader *eh);
+unsigned char *PC1(unsigned char *key, unsigned int klen, unsigned char *src,
+                   unsigned char *dest, unsigned int len, int decryption);
+unsigned int getSizeOfTrailingDataEntry(unsigned char *ptr, unsigned int size);
+unsigned int getSizeOfTrailingDataEntries(unsigned char *ptr, unsigned int size, unsigned int flags);
+unsigned char *parseDRM(unsigned char *data, unsigned int count, unsigned char *pid, unsigned int pidlen);
+
+void freeMobiFile(MobiFile *book);
+MobiFile *parseMobiHeader(FILE *f);
+int writeMobiOutputFile(MobiFile *book, FILE *out, unsigned char *key, 
+                        unsigned int drmOffset, unsigned int drm_len);
+
+#endif
index ee669b99f3e90440345d67acdb36621bd4dec8d2..f9637aa4844748363eb619dbcbceceb5bf4d84da 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
-   Copyright 2010 BartSimpson
+   Copyright 2010 BartSimpson aka skindle
    
    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
 
 /*
  * Dependencies: none
- * build on cygwin: gcc -o skindle skindle.c md5.c sha1.c b64.c -lCrypt32
+ * build on cygwin: 
+ *     gcc -o skindle skindle.c md5.c sha1.c b64.c -lCrypt32
+ * or  gcc -o skindle skindle.c md5.c sha1.c b64.c -lCrypt32 -mno-cygwin
  * Under cygwin, you can just type make to build it.
- * While the binary builds if you use the -mno-cygwin switch, it fails to
- * work for some reason.  The code should compile with Visual Studio, just
- * add all the files to a project and add the Crypt32.lib dependency and
- * it should build as a Win32 console app.
+ * The code should compile with Visual Studio, just add all the files to
+ * a project and add the Crypt32.lib dependency and it should build as a
+ * Win32 console app.
  */
 
 /*
 #include <sys/types.h>
 #include <sys/stat.h>
 
-//If you prefer to use openssl uncomment the following
-//#include <openssl/sha.h>
-//#include <openssl/md5.h>
+#include "skinutils.h"
+#include "cbuf.h"
+#include "mobi.h"
+#include "tpz.h"
 
-//If you prefer to use openssl remove the following 2 line
-#include "md5.h"
-#include "sha1.h"
+#include "zlib.h"
 
-unsigned int base64(unsigned char *inbuf, unsigned int len, unsigned char *outbuf);
-
-/* The kindle.info file created when you install KindleForPC is a set
- * of key:value pairs delimited by '{'.  The keys and values are encoded
- * in a variety of ways.  Keys are the mazama64 encoded md5 hash of the
- * key name, while values are the mazama64 encoding of the blob returned
- * by the Windows CryptProtectData function.  The use of CryptProtectData
- * is what locks things to a particular user/machine
-
- * kindle.info layout
-
- * Key:AbaZZ6z4a7ZxzLzkZcaqauZMZjZ_Ztz6   ("kindle.account.tokens")
- * Value: mazama64Encode(CryptProtectData(some sha1 hash))
-
- * Key:AsAWa4ZJAQaCZ7A3zrZSaZavZMarZFAw  ("kindle.cookie.item")
- * Value: mazama64Encode(CryptProtectData(base64(144 bytes of data)))
-
- * Key:ZHatAla4a-zTzWA-AvaeAvZQzKZ-agAz   ("eulaVersionAccepted")
- * Value: mazama64Encode(CryptProtectData(kindle version?))
-
- * Key:ZiajZga7Z9zjZRz7AfZ-zRzUANZNZJzP   ("login_date")
- * Value: mazama64Encode(CryptProtectData(registration date))
-
- * Key:ZkzeAUA-Z2ZYA2Z_ayA_ahZEATaEAOaG  ("kindle.token.item")
- * Value: mazama64Encode(CryptProtectData(multi-field crypto data))
- * {enc:xxx}{iv:xxx}{key:xxx}{name:xxx}{serial:xxx}
- * enc:base64(binary blob)
- * iv:base64(16 bytes)
- * key:base64(256 bytes)
- * name:base64("ADPTokenEncryptionKey")
- * serial:base64("1")
-
- * Key:aVzrzRAFZ7aIzmASZOzVzIAGAKawzwaU    ("login")
- * Value: mazama64Encode(CryptProtectData(your amazon email))
-
- * Key:avalzbzkAcAPAQA5ApZgaOZPzQZzaiaO   mazama64Encode(md5("MazamaRandomNumber"))
- * Value: mazama64Encode(CryptProtectData(mazama32Encode(32 bytes random data)))
-
- * Key:zgACzqAjZ2zzAmAJa6ZFaZALaYAlZrz-   ("kindle.key.item")
- * Value: mazama64Encode(CryptProtectData(RSA private key)) no password
-
- * Key:zga-aIANZPzbzfZ1zHZWZcA4afZMZcA_   ("kindle.name.info")
- * Value: mazama64Encode(CryptProtectData(your name))
-
- * Key:zlZ9afz1AfAVZjacaqa-ZHa1aIa_ajz7   ("kindle.device.info");
- * Value: mazama64Encode(CryptProtectData(the name of your kindle))
-*/
-
-typedef struct _SimpleMapNode {
-   char *key;
-   char *value;
-} SimpleMapNode;
-
-typedef struct _MapList {
-   SimpleMapNode *node;
-   struct _MapList *next;
-} MapList;
-
-MapList *kindleMap;
-
-#pragma pack(2)
-typedef struct _PDB {
-   char name[32];
-   unsigned short attrib;
-   unsigned short version;
-   unsigned int created;
-   unsigned int modified;
-   unsigned int backup;
-   unsigned int modNum;
-   unsigned int appInfoID;
-   unsigned int sortInfoID;
-   unsigned int type;
-   unsigned int creator;
-   unsigned int uniqueIDseed;
-   unsigned int nextRecordListID;
-   unsigned short numRecs;
-} PDB;
-
-typedef struct _HeaderRec {
-   unsigned int offset;
-   unsigned int attribId;
-} HeaderRec;
-
-#define attrib(x) ((x)&0xFF)
-#define id(x) (bswap_l((x) & 0xFFFFFF00))
-
-typedef struct _PalmDocHeader {
-   unsigned short compression;
-   unsigned short reserverd1;
-   unsigned int textLength;
-   unsigned short recordCount;
-   unsigned short recordSize;
-   unsigned short encryptionType;
-   unsigned short reserved2;
-} PalmDocHeader;
-
-
-//checked lengths are 24, 116, 208, 228
-typedef struct _MobiHeader {
-   unsigned int id;
-   unsigned int hdrLen;
-   unsigned int type;
-   unsigned int encoding;
-   unsigned int uniqueId;
-   unsigned int generator;
-   unsigned char reserved1[40];
-   unsigned int firstNonBookIdx;
-   unsigned int nameOffset;
-   unsigned int nameLength;
-   unsigned int language;
-   unsigned int inputLang;
-   unsigned int outputLang;
-   unsigned int formatVersion;
-   unsigned int firstImageIdx;
-   unsigned char unknown1[16];
-   unsigned int exthFlags;
-   unsigned char unknown2[36];
-   unsigned int drmOffset;
-   unsigned int drmCount;
-   unsigned int drmSize;
-   unsigned int drmFlags;
-   unsigned char unknown3[58];
-   unsigned short extra_flags;
-} MobiHeader;
-
-typedef struct _ExthRecHeader {
-   unsigned int type;
-   unsigned int len;
-} ExthRecHeader;
-
-typedef struct _ExthHeader {
-   unsigned int id;
-   unsigned int hdrLen;
-   unsigned int recordCount;
-   ExthRecHeader records[1];
-} ExthHeader;
-
-
-//#define bswap_l ntohl
-//#define bswap_s ntohs
-
-unsigned short bswap_s(unsigned short s) {
-   return (s >> 8) | (s << 8);
-}
-
-unsigned int bswap_l(unsigned int s) {
-   unsigned int u = bswap_s(s);
-   unsigned int l = bswap_s(s >> 16);
-   return (u << 16) | l;
-}
+int processTopaz(FILE *in, char *outFile, int explode, PidList *extraPids) {
+   //had to pile all these up here to please VS2009
+   cbuf *tpzHeaders, *tpzBody;
+   struct stat statbuf;
+   FILE *out;
+   unsigned int i;
+   char *keysRecord, *keysRecordRecord;
+   TopazFile *topaz;
+   char *pid;
+   
+   fstat(fileno(in), &statbuf);
 
-MapList *findNode(char *key) {
-   MapList *l;
-   for (l = kindleMap; l; l = l->next) {
-      if (strcmp(key, l->node->key) == 0) {
-         return l;
-      }
+   topaz = parseTopazHeader(in);
+   if (topaz == NULL) {
+      fprintf(stderr, "Failed to parse topaz headers\n");
+      return 0;
    }
-   return NULL;
-}
+   topaz->pids = extraPids;
+   
+   tpzHeaders = b_new(topaz->bodyOffset);
+   tpzBody = b_new(statbuf.st_size);
+   
+   parseMetadata(topaz);
+   
+//   dumpMap(bookMetadata);
 
-void addMapNode(char *key, char *value) {
-   MapList *ml = findNode(key);
-   if (ml) {
-      free(ml->node->value);
-      ml->node->value = value;
+   keysRecord = getMetadata(topaz, "keys");
+   if (keysRecord == NULL) {
+      //fail
    }
-   else {
-      SimpleMapNode *smn = (SimpleMapNode*)malloc(sizeof(SimpleMapNode));
-      smn->key = key;
-      smn->value = value;
-      ml = (MapList*)malloc(sizeof(MapList));
-      ml->node = smn;
-      ml->next = kindleMap;
-      kindleMap = ml;
+   keysRecordRecord = getMetadata(topaz, keysRecord);
+   if (keysRecordRecord == NULL) {
+      //fail
    }
-}
 
-void dumpMap() {
-   MapList *l;
-   for (l = kindleMap; l; l = l->next) {
-      fprintf(stderr, "%s:%s\n", l->node->key, l->node->value);
-   }
-}
+   pid = getBookPid(keysRecord, strlen(keysRecord), keysRecordRecord, strlen(keysRecordRecord));
 
-void parseLine(char *line) {
-   char *colon = strchr(line, ':');
-   if (colon) {
-      char *key, *value;
-      int len = colon - line;
-      key = (char*)malloc(len + 1);
-      *colon++ = 0;
-      strcpy(key, line);
-      len = strlen(colon);
-      value = (char*)malloc(len + 1);
-      strcpy(value, colon);
-      value[len] = 0;
-      addMapNode(key, value);
+   if (pid == NULL) {
+      fprintf(stderr, "Failed to extract pid automatically\n");
    }
-}
+   else {
+      char *title = getMetadata(topaz, "Title");   
+      fprintf(stderr, "PID for %s is: %s\n", title ? title : "UNK", pid);
+   }
+   
+/*
+   unique pid is computed as:
+   base64(sha1(idArray . kindleToken . 209_data . 209_tokens))
+*/
 
-int buildKindleMap(char *infoFile) {
-   int result = 0;
-   struct stat statbuf;
-   if (stat(infoFile, &statbuf) == 0) {
-      FILE *fd = fopen(infoFile, "rb");
-      char *infoBuf = (char*)malloc(statbuf.st_size + 1);
-      infoBuf[statbuf.st_size] = 0;
-      if (fread(infoBuf, statbuf.st_size, 1, fd) == 1) {
-         char *end = infoBuf + statbuf.st_size;
-         char *b = infoBuf, *e;
-         while (e = strchr(b, '{')) {
-            *e = 0;
-            if ((e - b) > 2) {
-               parseLine(b);
-            }
-            e++;
-            b = e;
-         }
-         if (b < end) {
-            parseLine(b);
+   //
+   //  Decrypt book key
+   //
+   Payload *dkey = getBookPayloadRecord(topaz, "dkey", 0, 0);
+   
+   if (dkey == NULL) {
+      fprintf(stderr, "No dkey record found\n");
+      freeTopazFile(topaz);
+      return 0;
+   }
+   
+   if (pid) {
+      topaz->bookKey = decryptDkeyRecords(dkey, pid);     
+      free(pid);
+   }
+   if (topaz->bookKey == NULL) {
+      if (extraPids) {
+         int p;
+         freePayload(dkey);
+         for (p = 0; p < extraPids->numPids; p++) {
+            dkey = getBookPayloadRecord(topaz, "dkey", 0, 0);
+            topaz->bookKey = decryptDkeyRecords(dkey, extraPids->pidList[p]);
+            if (topaz->bookKey) break;
          }
       }
-      else {
-         fprintf(stderr, "short read on info file\n");
+      if (topaz->bookKey == NULL) {
+         fprintf(stderr, "No valid pids available, failed to find DRM key\n");
+         freeTopazFile(topaz);
+         freePayload(dkey);
+         return 0;
       }
-      free(infoBuf);
-      fclose(fd);
-      return 1;
    }
-   return 0;
-}
-
-png_crc_table_init(unsigned int *crc_table) {
-   unsigned int i;
-   for (i = 0; i < 256; i++) {
-      unsigned int n = i;
-      unsigned int j;
-      for (j = 0; j < 8; j++) {
-         if (n & 1) {
-            n = 0xEDB88320 ^ (n >> 1);
-         }
-         else {
-            n >>= 1;
-         }
-      }
-      crc_table[i] = n;
+      
+   fprintf(stderr, "Found a DRM key!\n");
+   for (i = 0; i < 8; i++) {
+      fprintf(stderr, "%02x", topaz->bookKey[i]);
    }
-}
+   fprintf(stderr, "\n");
 
-unsigned int compute_png_crc(unsigned char *input, unsigned int len, unsigned int *crc_table) {
-   unsigned int crc = 0;
-   unsigned int i;
-   for (i = 0; i < len; i++) {
-      unsigned int v = (input[i] ^ crc) & 0xff;
-      crc = crc_table[v] ^ (crc >> 8);
+   out = fopen(outFile, "wb");
+   if (out == NULL) {
+      fprintf(stderr, "Failed to open output file, quitting\n");
+      freeTopazFile(topaz);
+      freePayload(dkey);
+      return 0;
    }
-   return crc;
-}
 
-char *decodeString = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
+   writeTopazOutputFile(topaz, out, tpzHeaders, tpzBody, explode);
+   fwrite(tpzHeaders->buf, tpzHeaders->idx, 1, out);
+   fwrite(tpzBody->buf, tpzBody->idx, 1, out);
+   fclose(out);
+   b_free(tpzHeaders);
+   b_free(tpzBody);
 
-void doPngDecode(unsigned char *input, unsigned int len, unsigned char *output) {
-   unsigned int crc_table[256];
-   unsigned int crc, i, x = 0;
-   unsigned int *out = (unsigned int*)output;
-   png_crc_table_init(crc_table);
-   crc = bswap_l(compute_png_crc(input, len, crc_table));
-   memset(output, 0, 8);
-   for (i = 0; i < len; i++) {
-      output[x++] ^= input[i];
-      if (x == 8) x = 0;
-   }
-   out[0] ^= crc;
-   out[1] ^= crc;
-   for (i = 0; i < 8; i++) {
-      unsigned char v = output[i];
-      output[i] = decodeString[((((v >> 5) & 3) ^ v) & 0x1F) + (v >> 7)];
-   }
+   freePayload(dkey);
+
+   freeTopazFile(topaz);
+   return 1;
 }
 
-char *string_20 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M";
-char *string_40 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_";
+int processMobi(FILE *prc, char *outFile, PidList *extraPids) {
+   //had to pile all these up here to please VS2009
+   PDB header;
+   cbuf *keyBuf;
+   char *pid;
+   FILE *out;
+   unsigned int i, keyPtrLen;
+   unsigned char *keyPtr;
+   unsigned int drmOffset, drm_len;
+   unsigned char *drm, *found_key = NULL;
+   MobiFile *book;
+   int result;
+   
+   book = parseMobiHeader(prc);
 
-char *mazamaEncode(unsigned char *input, unsigned int len, unsigned char choice) {
-   unsigned int i;
-   char *enc, *out;
-   if (choice == 0x20) enc = string_20;
-   else if (choice == 0x40) enc = string_40;
-   else return NULL;
-   out = (char*)malloc(len * 2 + 1);
-   out[len * 2] = 0;
-   for (i = 0; i < len; i++) {
-      unsigned char v = input[i] + 128;
-      unsigned char q = v / choice;
-      unsigned char m = v % choice;
-      out[i * 2] = enc[q];
-      out[i * 2 + 1] = enc[m];
+   if (book == NULL) {
+      fprintf(stderr, "Failed to read mobi headers\n");
+      return 0;
    }
-   return out;
-}
 
-unsigned char *mazamaDecode(char *input, int *outlen) {
-   unsigned char *out;
-   int len = strlen(input);
-   char *dec = NULL;
-   int i, choice = 0x20;
-   *outlen = 0;
-   for (i = 0; i < 8 && i < len; i++) {
-      if (*input == string_20[i]) {
-         dec = string_20;
-         break;
-      }
-   }
-   if (dec == NULL) {
-      for (i = 0; i < 4 && i < len; i++) {
-         if (*input == string_40[i]) {
-            dec = string_40;
-            choice = 0x40;
-            break;
+   book->pids = extraPids;
+   keyPtr = getExthData(book, 209, &keyPtrLen);
+
+   keyBuf = b_new(128);
+   if (keyPtr != NULL) {
+      unsigned int idx;
+      for (idx = 0; idx < keyPtrLen; idx += 5) {
+         unsigned char *rec;
+         unsigned int dlen;
+         unsigned int rtype = bswap_l(*(unsigned int*)(keyPtr + idx + 1));
+         rec = getExthData(book, rtype, &dlen);
+         if (rec != NULL) {
+            b_add_buf(keyBuf, rec, dlen);
          }
       }
    }
-   if (dec == NULL) {
-      return NULL;
-   }
-   out = (unsigned char*)malloc(len / 2 + 1);
-   out[len / 2] = 0;
-   for (i = 0; i < len; i += 2) {
-      int q, m, v;
-      char *p = strchr(dec, input[i]);
-      if (p == NULL) break;
-      q = p - dec;
-      p = strchr(dec, input[i + 1]);
-      if (p == NULL) break;
-      m = p - dec;
-      v = (choice * q + m) - 128;
-      out[(*outlen)++] = (unsigned char)v;
-   }
-   return out;
-}
 
-unsigned char *getExthData(ExthHeader *eh, unsigned int type, unsigned int *len) {
-   unsigned int i;
-   unsigned int exthRecords = bswap_l(eh->recordCount);
-   ExthRecHeader *erh = eh->records;
-
-   *len = 0;
+   pid = getBookPid(keyPtr, keyPtrLen, keyBuf->buf, keyBuf->idx);
 
-   for (i = 0; i < exthRecords; i++) {
-      unsigned int recType = bswap_l(erh->type);
-      unsigned int recLen = bswap_l(erh->len);
-
-      if (recLen < 8) {
-         fprintf(stderr, "Invalid exth record length %d\n", i);
-         return NULL;
-      }
+   b_free(keyBuf);
 
-      if (recType == type) {
-         *len = recLen - 8;
-         return (unsigned char*)(erh + 1);
-      }
-      erh = (ExthRecHeader*)(recLen  + (char*)erh);
+   if (pid == NULL) {
+      fprintf(stderr, "Failed to extract pid automatically\n");
    }
-   return NULL;
-}
-
-void enumExthRecords(ExthHeader *eh) {
-   unsigned int exthRecords = bswap_l(eh->recordCount);
-   unsigned int i;
-   unsigned char *data;
-   ExthRecHeader *erh = eh->records;
-
-   for (i = 0; i < exthRecords; i++) {
-      unsigned int recType = bswap_l(erh->type);
-      unsigned int recLen = bswap_l(erh->len);
-
-      fprintf(stderr, "%d: type - %d, len %d\n", i, recType, recLen);
-
-      if (recLen < 8) {
-         fprintf(stderr, "Invalid exth record length %d\n", i);
-         return;
-      }
-
-      data = (unsigned char*)(erh + 1);
-      switch (recType) {
-         case 1: //drm_server_id
-            fprintf(stderr, "drm_server_id: %s\n", data);
-            break;
-         case 2: //drm_commerce_id
-            fprintf(stderr, "drm_commerce_id: %s\n", data);
-            break;
-         case 3: //drm_ebookbase_book_id
-            fprintf(stderr, "drm_ebookbase_book_id: %s\n", data);
-            break;
-         case 100: //author
-            fprintf(stderr, "author: %s\n", data);
-            break;
-         case 101: //publisher
-            fprintf(stderr, "publisher: %s\n", data);
-            break;
-         case 106: //publishingdate
-            fprintf(stderr, "publishingdate: %s\n", data);
-            break;
-         case 113: //asin
-            fprintf(stderr, "asin: %s\n", data);
-            break;
-         case 208: //book unique drm key
-            fprintf(stderr, "book drm key: %s\n", data);
-            break;
-         case 503: //updatedtitle
-            fprintf(stderr, "updatedtitle: %s\n", data);
-            break;
-         default:
-            break;
-      }
-      erh = (ExthRecHeader*)(recLen  + (char*)erh);
+   else {
+      fprintf(stderr, "PID for %s is: %s\n", book->pdb.name, pid);
    }
 
-}
+/*
+   unique pid is computed as:
+   base64(sha1(idArray . kindleToken . 209_data . 209_tokens))
+*/
 
-//implementation of Pukall Cipher 1
-unsigned char *PC1(unsigned char *key, unsigned int klen, unsigned char *src,
-                   unsigned char *dest, unsigned int len, int decryption) {
-    unsigned int sum1 = 0;
-    unsigned int sum2 = 0;
-    unsigned int keyXorVal = 0;
-    unsigned short wkey[8];
-    unsigned int i;
-    if (klen != 16) {
-        fprintf(stderr, "Bad key length!\n");
-        return NULL;
-    }
-    for (i = 0; i < 8; i++) {
-        wkey[i] = (key[i * 2] << 8) | key[i * 2 + 1];
-    }
-    for (i = 0; i < len; i++) {
-        unsigned int temp1 = 0;
-        unsigned int byteXorVal = 0;
-        unsigned int j, curByte;
-        for (j = 0; j < 8; j++) {
-            temp1 ^= wkey[j];
-            sum2 = (sum2 + j) * 20021 + sum1;
-            sum1 = (temp1 * 346) & 0xFFFF;
-            sum2 = (sum2 + sum1) & 0xFFFF;
-            temp1 = (temp1 * 20021 + 1) & 0xFFFF;
-            byteXorVal ^= temp1 ^ sum2;
-        }
-        curByte = src[i];
-        if (!decryption) {
-            keyXorVal = curByte * 257;
-        }
-        curByte = ((curByte ^ (byteXorVal >> 8)) ^ byteXorVal) & 0xFF;
-        if (decryption) {
-            keyXorVal = curByte * 257;
-        }
-        for (j = 0; j < 8; j++) {
-            wkey[j] ^= keyXorVal;
-        }
-        dest[i] = curByte;
-    }
-    return dest;
-}
+   drmOffset = bswap_l(book->mobi->drmOffset);
+
+   drm_len = bswap_l(book->mobi->drmSize);
+   drm = book->record0 + drmOffset;
 
-unsigned int getSizeOfTrailingDataEntry(unsigned char *ptr, unsigned int size) {
-   unsigned int bitpos = 0;
-   unsigned int result = 0;
-   if (size <= 0) {
-      return result;
+   if (pid) {
+      found_key = parseDRM(drm, book->drmCount, pid, 8);
+      free(pid);
    }
-   while (1) {
-      unsigned int v = ptr[size - 1];
-      result |= (v & 0x7F) << bitpos;
-      bitpos += 7;
-      size -= 1;
-      if ((v & 0x80) != 0 || (bitpos >= 28) || (size == 0)) {
-         return result;
+   if (found_key == NULL) {
+      if (extraPids) {
+         int p;
+         for (p = 0; p < extraPids->numPids; p++) {
+            found_key = parseDRM(drm, book->drmCount, extraPids->pidList[p], 8);
+            if (found_key) break;
+         }
       }
-   }
-}
-
-unsigned int getSizeOfTrailingDataEntries(unsigned char *ptr, unsigned int size, unsigned int flags) {
-   unsigned int num = 0;
-   unsigned int testflags = flags >> 1;
-   while (testflags) {
-      if (testflags & 1) {
-         num += getSizeOfTrailingDataEntry(ptr, size - num);
+      if (found_key == NULL) {
+         fprintf(stderr, "No valid pids available, failed to find DRM key\n");
+         freeMobiFile(book);
+         return 0;
       }
-      testflags >>= 1;
    }
-   if (flags & 1) {
-      num += (ptr[size - num - 1] & 0x3) + 1;
-   }
-   return num;
-}
-
-typedef struct _vstruct {
-   unsigned int verification;
-   unsigned int size;
-   unsigned int type;
-   unsigned char cksum[4];
-   unsigned char cookie[32];
-} vstruct;
-
-typedef struct _kstruct {
-   unsigned int ver;
-   unsigned int flags;
-   unsigned char finalkey[16];
-   unsigned int expiry;
-   unsigned int expiry2;
-} kstruct;
-
-unsigned char *parseDRM(unsigned char *data, unsigned int count, unsigned char *pid, unsigned int pidlen) {
-   unsigned int i;
-   unsigned char temp_key_sum = 0;
-   unsigned char *found_key = NULL;
-   unsigned char *keyvec1 = "\x72\x38\x33\xB0\xB4\xF2\xE3\xCA\xDF\x09\x01\xD6\xE2\xE0\x3F\x96";
-   unsigned char temp_key[16];
+   fprintf(stderr, "Found a DRM key!\n");
 
-   memset(temp_key, 0, 16);
-   memcpy(temp_key, pid, 8);
-   PC1(keyvec1, 16, temp_key, temp_key, 16, 0);
-
-   for (i = 0; i < 16; i++) {
-      temp_key_sum += temp_key[i];
+   out = fopen(outFile, "wb");
+   if (out == NULL) {
+      fprintf(stderr, "Failed to open output file, quitting\n");
+      freeMobiFile(book);
+      free(found_key);
+      return 0;
    }
 
-   for (i = 0; i < count; i++) {
-      unsigned char kk[32];
-      vstruct *v = (vstruct*)(data + i * 0x30);
-      kstruct *k = (kstruct*)PC1(temp_key, 16, v->cookie, kk, 32, 1);
+   result = writeMobiOutputFile(book, out, found_key, drmOffset, drm_len);
 
-      if (v->verification == k->ver && v->cksum[0] == temp_key_sum &&
-          (bswap_l(k->flags) & 0x1F) == 1) {
-         found_key = (unsigned char*)malloc(16);
-         memcpy(found_key, k->finalkey, 16);
-         break;
-      }
+   fclose(out);
+   if (result == 0) {
+      _unlink(outFile);
    }
-   return found_key;
+   freeMobiFile(book);
+   free(found_key);
+   return result;
 }
 
-#ifndef HEADER_MD5_H
-
-#define MD5_DIGEST_LENGTH 16
-
-#define MD5_CTX md5_state_t
-#define MD5_Init md5_init
-#define MD5_Update md5_append
-#define MD5_Final(x, y) md5_finish(y, x)
-#define MD5 md5
-
-void md5(unsigned char *in, int len, unsigned char *md) {
-   MD5_CTX s;
-   MD5_Init(&s);
-       MD5_Update(&s, in, len);
-       MD5_Final(md, &s);
+enum {
+   FileTypeUnk,
+   FileTypeMobi,
+   FileTypeTopaz
+};
+
+int getFileType(FILE *in) {
+   PDB p;
+   int type = FileTypeUnk;
+   fseek(in, 0, SEEK_SET);
+   fread(&p, sizeof(p), 1, in);
+   if (p.type == 0x4b4f4f42 && p.creator == 0x49424f4d) {
+      type = FileTypeMobi;
+   }
+   else if (strncmp(p.name, "TPZ0", 4) == 0) {
+      type = FileTypeTopaz;
+   }
+   fseek(in, 0, SEEK_SET);
+   return type;
 }
-#endif
-
-#ifndef HEADER_SHA_H
 
-#define SHA_DIGEST_LENGTH 20
-#define SHA_CTX sha1_state_s
-#define SHA1_Init sha1_init
-#define SHA1_Update sha1_update
-#define SHA1_Final(x, y) sha1_finish(y, x)
-#define SHA1 sha1
-
-void sha1(unsigned char *in, int len, unsigned char *md) {
-   SHA_CTX s;
-   SHA1_Init(&s);
-       SHA1_Update(&s, in, len);
-       SHA1_Final(md, &s);
+void usage() {
+   fprintf(stderr, "usage: ./skindle [-d] [-v] -i <ebook file> -o <output file> [-k kindle.info file] [-p pid]\n");
+   fprintf(stderr, "    -d optional, for topaz files only, produce a decompressed output file\n");
+   fprintf(stderr, "    -i required name of the input mobi or topaz file\n");
+   fprintf(stderr, "    -o required name of the output file to generate\n");
+   fprintf(stderr, "    -k optional kindle.info path\n");   
+   fprintf(stderr, "    -v dump the contents of kindle.info\n");   
+   fprintf(stderr, "    -p additional PID values to attempt (can specifiy multiple times)\n");   
 }
-#endif
+
+extern char *optarg;
+extern int optind;
 
 int main(int argc, char **argv) {
    //had to pile all these up here to please VS2009
-   PDB header;
-   struct stat statbuf;
-   FILE *prc, *out;
-   HeaderRec *hr;
-   PalmDocHeader *pdh;
-   MobiHeader *mobi;
-   ExthHeader *exth;
-   long record0_offset;
-   unsigned int record0_size, mobiLen, extra_data_flags;
-   unsigned int recs, i, drmCount, len209;
-   unsigned char *record0, *d209;
-   unsigned char *vsn, *username, *mrn_key, *kat_key;
-   char drive[256];
-   char name[256];
-   DWORD nlen = sizeof(name);
-   char *d;
-   char volumeName[256];
-   DWORD volumeSerialNumber;
-   char fileSystemNameBuffer[256];
-   char volumeID[32];
-   unsigned char md5sum[MD5_DIGEST_LENGTH];
-   unsigned char sha1sum[SHA_DIGEST_LENGTH];
-   unsigned char pid_base64[SHA_DIGEST_LENGTH * 2];
-   unsigned int outl;
-   SHA_CTX sha1_ctx;
-   MapList *ml;
-   unsigned int drmOffset, drm_len;
-   unsigned char *drm, *found_key;
-      
-   if (argc != 4) {
-      fprintf(stderr, "usage: %s\n <prc file> <output file> <kindle.info file>", argv[0]);
-      exit(1);
-   }
-
-   if (stat(argv[1], &statbuf) != 0) {
-      fprintf(stderr, "Unable to stat %s, quitting\n", argv[1]);
-      exit(1);
-   }
-
-   prc = fopen(argv[1], "rb");
-   if (prc == NULL) {
-      fprintf(stderr, "%s bad open, quitting\n", argv[1]);
-      exit(1);
-   }
-
-   if (fread(&header, sizeof(header), 1, prc) != 1) {
-      fprintf(stderr, "%s bad header read, quitting\n", argv[1]);
-      fclose(prc);
-      exit(1);
-   }
-
-   //do BOOKMOBI check
-   if (header.type != 0x4b4f4f42 || header.creator != 0x49424f4d) {
-      fprintf(stderr, "Invalid header type or creator, quitting\n");
-      fclose(prc);
-      exit(1);
+   FILE *in;
+   int type, explode = 0;
+   int result = 0;
+   int firstArg = 1;
+   int opt;
+   PidList *pids = NULL;
+   char *infile = NULL, *outfile = NULL, *kinfo = NULL;
+   int dump = 0;
+   
+   while ((opt = getopt(argc, argv, "vdp:i:o:k:")) != -1) {
+      switch (opt) {
+         case 'v':
+            dump = 1;
+            break;
+         case 'd':
+            explode = 1;
+            break;
+         case 'p': {
+            int l = strlen(optarg);
+            if (l == 10) {
+               if (!verifyPidChecksum(optarg)) {
+                  fprintf(stderr, "Invalid pid %s, skipping\n", optarg);
+                  break;
+               }
+               optarg[8] = 0;
+            }
+            else if (l != 8) {
+               fprintf(stderr, "Invalid pid length for %s, skipping\n", optarg);
+               break;
+            }
+            if (pids == NULL) {
+               pids = (PidList*)malloc(sizeof(PidList));
+               pids->numPids = 1;
+               pids->pidList[0] = optarg;
+            }
+            else {
+               pids = (PidList*)realloc(pids, sizeof(PidList) + pids->numPids * sizeof(unsigned char*));
+               pids->pidList[pids->numPids++] = optarg;
+            }
+            break;
+         }
+         case 'k':
+            kinfo = optarg;
+            break;
+         case 'i':
+            infile = optarg;
+            break;
+         case 'o':
+            outfile = optarg;
+            break;
+         default: /* '?' */
+            usage();
+            exit(1);
+      }
    }
 
-   if (!buildKindleMap(argv[3])) {
-      fprintf(stderr, "buildMap failed\n");
-      fclose(prc);
-      exit(1);
+   if (optind != argc) {
+      fprintf(stderr, "Extra options ignored\n");
    }
 
-   recs = bswap_s(header.numRecs);
-
-   hr = (HeaderRec*)malloc(recs * sizeof(HeaderRec));
-   if (fread(hr, sizeof(HeaderRec), recs, prc) != recs) {
-      fprintf(stderr, "Failed read of header record, quitting\n");
-      fclose(prc);
+   if (!buildKindleMap(kinfo)) {
+      fprintf(stderr, "buildKindleMap failed\n");
+      usage();
       exit(1);
    }
 
-   record0_offset = bswap_l(hr[0].offset);
-   record0_size = bswap_l(hr[1].offset) - record0_offset;
-
-   if (fseek(prc, record0_offset, SEEK_SET) == -1) {
-      fprintf(stderr, "bad seek to header record offset, quitting\n");
-      fclose(prc);
-      exit(1);
+   //The following loop dumps the contents of your kindle.info file
+   if (dump) {
+      MapList *ml;
+//     dumpKindleMap();
+      fprintf(stderr, "\nDumping kindle.info contents:\n");
+      for (ml = kindleMap; ml; ml = ml->next) {
+         DATA_BLOB DataIn;
+         DATA_BLOB DataOut;
+         DataIn.pbData = mazamaDecode(ml->value, (int*)&DataIn.cbData);
+         if (CryptUnprotectData(&DataIn, NULL, NULL, NULL, NULL, 1, &DataOut)) {
+            fprintf(stderr, "%s ==> %s\n", ml->key, translateKindleKey(ml->key));
+            fwrite(DataOut.pbData, DataOut.cbData, 1, stderr);
+            fprintf(stderr, "\n\n");
+            LocalFree(DataOut.pbData);
+         }
+         else {
+            fprintf(stderr, "CryptUnprotectData failed\n");
+         }
+         free(DataIn.pbData);
+      }
    }
 
-   record0 = (unsigned char*)malloc(record0_size);
-
-   if (fread(record0, record0_size, 1, prc) != 1) {
-      fprintf(stderr, "bad read of record0, quitting\n");
-      free(record0);
-      fclose(prc);
+   if (infile == NULL && outfile == NULL) {
+      //special case, user just wants to see kindle.info
+      freeMap(kindleMap);
       exit(1);
    }
 
-   pdh = (PalmDocHeader*)record0;
-   if (bswap_s(pdh->encryptionType) != 2) {
-      fprintf(stderr, "MOBI BOOK is not encrypted, quitting\n");
-      free(record0);
-      fclose(prc);
+   if (infile == NULL) {
+      fprintf(stderr, "Missing input file name\n");
+      usage();
+      freeMap(kindleMap);
       exit(1);
    }
 
-   mobi = (MobiHeader*)(pdh + 1);
-   if (mobi->id != 0x49424f4d) {
-      fprintf(stderr, "MOBI header not found, quitting\n");
-      free(record0);
-      fclose(prc);
+   if (outfile == NULL) {
+      fprintf(stderr, "Missing output file name\n");
+      usage();
+      freeMap(kindleMap);
       exit(1);
    }
-
-   mobiLen = bswap_l(mobi->hdrLen);
-   extra_data_flags = 0;
    
-   if (mobiLen >= 0xe4) {
-      extra_data_flags = bswap_s(mobi->extra_flags);
-   }
-
-   if ((bswap_l(mobi->exthFlags) & 0x40) == 0) {
-      fprintf(stderr, "Missing exth header, quitting\n");
-      free(record0);
-      fclose(prc);
+   in = fopen(infile, "rb");
+   if (in == NULL) {
+      fprintf(stderr, "%s bad open, quitting\n", infile);
+      freeMap(kindleMap);
       exit(1);
    }
 
-   exth = (ExthHeader*)(mobiLen + (char*)mobi);
-   if (exth->id != 0x48545845) {
-      fprintf(stderr, "EXTH header not found\n");
-      free(record0);
-      fclose(prc);
-      exit(1);
-   }
-  
-   //if you want a list of EXTH records, uncomment the following
-//   enumExthRecords(exth);
-
-   drmCount = bswap_l(mobi->drmCount);
-
-   if (drmCount == 0) {
-      fprintf(stderr, "no PIDs found in this file, quitting\n");
-      free(record0);
-      fclose(prc);
-      exit(1);
-   }
-
-   if (GetUserName(name, &nlen) == 0) {
-      fprintf(stderr, "GetUserName failed, quitting\n");
-      fclose(prc);
-      exit(1);
-   }
-   fprintf(stderr, "Using UserName = \"%s\"\n", name);
-
-   d = getenv("SystemDrive");
-   if (d) {
-      strcpy(drive, d);
-      strcat(drive, "\\");
-   }
-   else {
-      strcpy(drive, "c:\\");
+   type = getFileType(in);
+   if (type == FileTypeTopaz) {
+      result = processTopaz(in, outfile, explode, pids);
    }
-   fprintf(stderr, "Using SystemDrive = \"%s\"\n", drive);
-   if (GetVolumeInformation(drive, volumeName, sizeof(volumeName), &volumeSerialNumber,
-                        NULL, NULL, fileSystemNameBuffer, sizeof(fileSystemNameBuffer))) {
-      sprintf(volumeID, "%u", volumeSerialNumber);
+   else if (type == FileTypeMobi) {
+      result = processMobi(in, outfile, pids);
    }
    else {
-      strcpy(volumeID, "9999999999");
-   }
-   fprintf(stderr, "Using VolumeSerialNumber = \"%s\"\n", volumeID);
-   MD5(volumeID, strlen(volumeID), md5sum);
-   vsn = mazamaEncode(md5sum, MD5_DIGEST_LENGTH, 0x20);
-
-   MD5(name, strlen(name), md5sum);
-   username = mazamaEncode(md5sum, MD5_DIGEST_LENGTH, 0x20);
-
-   MD5("MazamaRandomNumber", 18, md5sum);
-   mrn_key = mazamaEncode(md5sum, MD5_DIGEST_LENGTH, 0x40);
-
-   MD5("kindle.account.tokens", 21, md5sum);
-   kat_key = mazamaEncode(md5sum, MD5_DIGEST_LENGTH, 0x40);
-
-   SHA1_Init(&sha1_ctx);
-
-   ml = findNode(mrn_key);
-   if (ml) {
-      DATA_BLOB DataIn;
-      DATA_BLOB DataOut;
-      DataIn.pbData = mazamaDecode(ml->node->value, (int*)&DataIn.cbData);
-      if (CryptUnprotectData(&DataIn, NULL, NULL, NULL, NULL, 1, &DataOut)) {
-         char *devId = (char*)malloc(DataOut.cbData + 2 * MD5_DIGEST_LENGTH + 1);
-         char *finalDevId;
-         unsigned char pidbuf[10];
-         
-//         fprintf(stderr, "CryptUnprotectData success\n");
-//         fwrite(DataOut.pbData, DataOut.cbData, 1, stderr);
-//         fprintf(stderr, "\n");
-
-         memcpy(devId, DataOut.pbData, DataOut.cbData);
-         strcpy(devId + DataOut.cbData, vsn);
-         strcat(devId + DataOut.cbData, username);
-
-//         fprintf(stderr, "Computing sha1 over %d bytes\n", DataOut.cbData + 4 * MD5_DIGEST_LENGTH);
-         sha1(devId, DataOut.cbData + 4 * MD5_DIGEST_LENGTH, sha1sum);
-         finalDevId = mazamaEncode(sha1sum, SHA_DIGEST_LENGTH, 0x20);
-//         fprintf(stderr, "finalDevId: %s\n", finalDevId);
-
-         SHA1_Update(&sha1_ctx, finalDevId, strlen(finalDevId));
-
-         pidbuf[8] = 0;
-         doPngDecode(finalDevId, 4, (unsigned char*)pidbuf);
-//         fprintf(stderr, "Device PID: %s\n", pidbuf);
-
-         LocalFree(DataOut.pbData);
-         free(devId);
-         free(finalDevId);
-      }
-      else {
-         fprintf(stderr, "CryptUnprotectData failed, quitting\n");
-         free(record0);
-         fclose(prc);
-         exit(1);
-      }
-
-      free(DataIn.pbData);
-   }
-
-   ml = findNode(kat_key);
-   if (ml) {
-      DATA_BLOB DataIn;
-      DATA_BLOB DataOut;
-      DataIn.pbData = mazamaDecode(ml->node->value, (int*)&DataIn.cbData);
-      if (CryptUnprotectData(&DataIn, NULL, NULL, NULL, NULL, 1, &DataOut)) {
-//         fprintf(stderr, "CryptUnprotectData success\n");
-//         fwrite(DataOut.pbData, DataOut.cbData, 1, stderr);
-//         fprintf(stderr, "\n");
-
-         SHA1_Update(&sha1_ctx, DataOut.pbData, DataOut.cbData);
-
-         LocalFree(DataOut.pbData);
-      }
-      else {
-         fprintf(stderr, "CryptUnprotectData failed, quitting\n");
-         fclose(prc);
-         free(record0);
-         exit(1);
-      }
-
-      free(DataIn.pbData);
-   }
-
-   d209 = getExthData(exth, 209, &len209);
-
-   if (d209 != NULL) {
-      unsigned char *rec;
-      unsigned int idx;
-      SHA1_Update(&sha1_ctx, d209, len209);
-      for (idx = 0; idx < len209; idx += 5) {
-         unsigned int dlen;
-         unsigned int rtype = bswap_l(*(unsigned int*)(d209 + idx + 1));
-         rec = getExthData(exth, rtype, &dlen);
-         if (rec != NULL) {
-//            fprintf(stderr, "exth %d: %s\n", rtype, rec);
-            SHA1_Update(&sha1_ctx, rec, dlen);
-         }
-      }
-   }
-
-   SHA1_Final(sha1sum, &sha1_ctx);
-
-   outl = base64(sha1sum, SHA_DIGEST_LENGTH, pid_base64);
-
-   pid_base64[8] = 0;
-   fprintf(stderr, "PID for %s is: %s\n", header.name, pid_base64);
-
-/*
-   unique pid is computed as:
-   base64(sha1(idArray . kindleToken . 209_data . 209_tokens))
-*/
-
-   free(mrn_key);
-   free(kat_key);
-   free(vsn);
-   free(username);
-
-   drmOffset = bswap_l(mobi->drmOffset);
-
-   drm_len = bswap_l(mobi->drmSize);
-   drm = record0 + drmOffset;
-
-   found_key = parseDRM(drm, drmCount, pid_base64, 8);
-   if (found_key) {
-      fprintf(stderr, "Found a DRM key!\n");
-   }
-   else {
-      fprintf(stderr, "Failed to find DRM key\n");
-      free(record0);
-      fclose(prc);
+      fprintf(stderr, "%s file type unknown, quitting\n", infile);
+      fclose(in);
+      freeMap(kindleMap);
       exit(1);
    }
 
-   // kill the drm keys
-   memset(record0 + drmOffset, 0, drm_len);
-   // kill the drm pointers
-   mobi->drmOffset = 0xffffffff;
-   mobi->drmCount = mobi->drmSize = mobi->drmFlags = 0;
-   // clear the crypto type
-   pdh->encryptionType = 0;
-
-   out = fopen(argv[2], "wb");
-   if (out == NULL) {
-      fprintf(stderr, "Failed to open output file, quitting\n");
-      free(record0);
-      fclose(prc);
-      exit(1);
+   fclose(in);
+   if (result) {
+      fprintf(stderr, "Success! Enjoy!\n");
    }
-
-   fwrite(&header, sizeof(header), 1, out);
-   fwrite(hr, sizeof(HeaderRec), recs, out);
-   fwrite("\x00\x00", 1, 2, out);
-   fwrite(record0, record0_size, 1, out);
-
-   //need to zero out exth 209 data
-   for (i = 1; i < recs; i++) {
-      unsigned int offset = bswap_l(hr[i].offset);
-      unsigned int len, extra_size;
-      unsigned char *rec;
-      if (i == (recs - 1)) {  //last record extends to end of file
-         len = statbuf.st_size - offset;
-      }
-      else {
-         len = bswap_l(hr[i + 1].offset) - offset;
-      }
-      //make sure we are at proper offset
-      while (ftell(out) < offset) {
-         fwrite("\x00", 1, 1, out);
-      }
-      rec = (unsigned char *)malloc(len);
-      if (fseek(prc, offset, SEEK_SET) != 0) {
-         fprintf(stderr, "Failed record seek on input, quitting\n");
-         free(record0);
-         free(rec);
-         fclose(prc);
-         fclose(out);
-         _unlink(argv[2]);
-         exit(1);
-      }
-      if (fread(rec, len, 1, prc) != 1) {
-         fprintf(stderr, "Failed record read on input, quitting\n");
-         free(record0);
-         free(rec);
-         fclose(prc);
-         fclose(out);
-         _unlink(argv[2]);
-         exit(1);
-      }
-
-      extra_size = getSizeOfTrailingDataEntries(rec, len, extra_data_flags);
-      PC1(found_key, 16, rec, rec, len - extra_size, 1);
-      fwrite(rec, len, 1, out);
-      free(rec);
-   }
-   fprintf(stderr, "Done! Enjoy!\n");
-
-/*
-   //The following loop dumps the contents of your kindle.info file
-   for (ml = kindleMap; ml; ml = ml->next) {
-      DATA_BLOB DataIn;
-      DATA_BLOB DataOut;
-      DataIn.pbData = mazamaDecode(ml->node->value, (int*)&DataIn.cbData);
-      if (CryptUnprotectData(&DataIn, NULL, NULL, NULL, NULL, 1, &DataOut)) {
-         fprintf(stderr, "CryptUnprotectData success for key: %s\n", ml->node->key);
-         fwrite(DataOut.pbData, DataOut.cbData, 1, stderr);
-         fprintf(stderr, "\n");
-         LocalFree(DataOut.pbData);
-      }
-      else {
-         fprintf(stderr, "CryptUnprotectData failed\n");
-      }
-      free(DataIn.pbData);
+   else {
+      fprintf(stderr, "An error occurred, unable to process input file!\n");
    }
-*/
-
-   fclose(prc);
-   fclose(out);
-   free(record0);
+   
+   freeMap(kindleMap);
    return 0;
 }
index 3e47d1b26f138b51b519b51ad978b0ab3b3bb11f..3dc187f56c83686db493630c4e2180a5b7a4a926 100644 (file)
Binary files a/skindle/skindle.exe and b/skindle/skindle.exe differ
diff --git a/skindle/skinutils.c b/skindle/skinutils.c
new file mode 100644 (file)
index 0000000..add3336
--- /dev/null
@@ -0,0 +1,539 @@
+/*
+   Copyright 2010 BartSimpson aka skindle
+   
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+#include <windows.h>
+#include <Wincrypt.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+#include "skinutils.h"
+
+/* The kindle.info file created when you install KindleForPC is a set
+ * of key:value pairs delimited by '{'.  The keys and values are encoded
+ * in a variety of ways.  Keys are the mazama64 encoded md5 hash of the
+ * key name, while values are the mazama64 encoding of the blob returned
+ * by the Windows CryptProtectData function.  The use of CryptProtectData
+ * is what locks things to a particular user/machine
+
+ * kindle.info layout
+
+ * Key:AbaZZ6z4a7ZxzLzkZcaqauZMZjZ_Ztz6   ("kindle.account.tokens")
+ * Value: mazama64Encode(CryptProtectData(some sha1 hash))
+
+ * Key:AsAWa4ZJAQaCZ7A3zrZSaZavZMarZFAw  ("kindle.cookie.item")
+ * Value: mazama64Encode(CryptProtectData(base64(144 bytes of data)))
+
+ * Key:ZHatAla4a-zTzWA-AvaeAvZQzKZ-agAz   ("eulaVersionAccepted")
+ * Value: mazama64Encode(CryptProtectData(kindle version?))
+
+ * Key:ZiajZga7Z9zjZRz7AfZ-zRzUANZNZJzP   ("login_date")
+ * Value: mazama64Encode(CryptProtectData(registration date))
+
+ * Key:ZkzeAUA-Z2ZYA2Z_ayA_ahZEATaEAOaG  ("kindle.token.item")
+ * Value: mazama64Encode(CryptProtectData(multi-field crypto data))
+ * {enc:xxx}{iv:xxx}{key:xxx}{name:xxx}{serial:xxx}
+ * enc:base64(binary blob)
+ * iv:base64(16 bytes)
+ * key:base64(256 bytes)
+ * name:base64("ADPTokenEncryptionKey")
+ * serial:base64("1")
+
+ * Key:aVzrzRAFZ7aIzmASZOzVzIAGAKawzwaU    ("login")
+ * Value: mazama64Encode(CryptProtectData(your amazon email))
+
+ * Key:avalzbzkAcAPAQA5ApZgaOZPzQZzaiaO   mazama64Encode(md5("MazamaRandomNumber"))
+ * Value: mazama64Encode(CryptProtectData(mazama32Encode(32 bytes random data)))
+
+ * Key:zgACzqAjZ2zzAmAJa6ZFaZALaYAlZrz-   ("kindle.key.item")
+ * Value: mazama64Encode(CryptProtectData(RSA private key)) no password
+
+ * Key:zga-aIANZPzbzfZ1zHZWZcA4afZMZcA_   ("kindle.name.info")
+ * Value: mazama64Encode(CryptProtectData(your name))
+
+ * Key:zlZ9afz1AfAVZjacaqa-ZHa1aIa_ajz7   ("kindle.device.info");
+ * Value: mazama64Encode(CryptProtectData(the name of your kindle))
+*/
+
+char *kindleKeys[] = {
+   "AbaZZ6z4a7ZxzLzkZcaqauZMZjZ_Ztz6", "kindle.account.tokens",
+   "AsAWa4ZJAQaCZ7A3zrZSaZavZMarZFAw", "kindle.cookie.item",
+   "ZHatAla4a-zTzWA-AvaeAvZQzKZ-agAz", "eulaVersionAccepted",
+   "ZiajZga7Z9zjZRz7AfZ-zRzUANZNZJzP", "login_date",
+   "ZkzeAUA-Z2ZYA2Z_ayA_ahZEATaEAOaG", "kindle.token.item",
+   "aVzrzRAFZ7aIzmASZOzVzIAGAKawzwaU", "login",
+   "avalzbzkAcAPAQA5ApZgaOZPzQZzaiaO", "MazamaRandomNumber",
+   "zgACzqAjZ2zzAmAJa6ZFaZALaYAlZrz-", "kindle.key.item",
+   "zga-aIANZPzbzfZ1zHZWZcA4afZMZcA_", "kindle.name.info",
+   "zlZ9afz1AfAVZjacaqa-ZHa1aIa_ajz7", "kindle.device.info"
+};
+
+MapList *kindleMap;
+
+unsigned short bswap_s(unsigned short s) {
+   return (s >> 8) | (s << 8);
+}
+
+unsigned int bswap_l(unsigned int s) {
+   unsigned int u = bswap_s(s);
+   unsigned int l = bswap_s(s >> 16);
+   return (u << 16) | l;
+}
+
+char *translateKindleKey(char *key) {
+   int n = sizeof(kindleKeys) / sizeof(char*);
+   int i;
+   for (i = 0; i < n; i += 2) {
+      if (strcmp(key, kindleKeys[i]) == 0) {
+         return kindleKeys[i + 1];
+      }
+   }
+   return NULL;
+}
+
+MapList *findNode(MapList *map, char *key) {
+   MapList *l;
+   for (l = map; l; l = l->next) {
+      if (strcmp(key, l->key) == 0) {
+         return l;
+      }
+   }
+   return NULL;
+}
+
+MapList *findKindleNode(char *key) {
+   return findNode(kindleMap, key);
+}
+
+char *getNodeValue(MapList *map, char *key) {
+   MapList *l;
+   for (l = map; l; l = l->next) {
+      if (strcmp(key, l->key) == 0) {
+         return l->value;
+      }
+   }
+   return NULL;
+}
+
+char *getKindleValue(char *key) {
+   return getNodeValue(kindleMap, key);
+}
+
+MapList *addMapNode(MapList *map, char *key, char *value) {
+   MapList *ml;
+   ml = findNode(map, key);
+   if (ml) {
+      free(ml->value);
+      ml->value = value;
+      return map;
+   }
+   else {
+      ml = (MapList*)malloc(sizeof(MapList));
+      ml->key = key;
+      ml->value = value;
+      ml->next = map;
+      return ml;
+   }
+}
+
+void dumpMap(MapList *m) {
+   MapList *l;
+   for (l = m; l; l = l->next) {
+      fprintf(stderr, "%s:%s\n", l->key, l->value);
+   }
+}
+
+void freeMap(MapList *m) {
+   MapList *n;
+   while (m) {
+      n = m;
+      m = m->next;
+      free(n->key);
+      free(n->value);
+      free(n);
+   }
+}
+
+void parseLine(char *line) {
+   char *colon = strchr(line, ':');
+   if (colon) {
+      char *key, *value;
+      int len = colon - line;
+      key = (char*)malloc(len + 1);
+      *colon++ = 0;
+      strcpy(key, line);
+      len = strlen(colon);
+      value = (char*)malloc(len + 1);
+      strcpy(value, colon);
+      value[len] = 0;
+      kindleMap = addMapNode(kindleMap, key, value);
+   }
+}
+
+void dumpKindleMap() {
+   dumpMap(kindleMap);
+}
+
+int buildKindleMap(char *infoFile) {
+   int result = 0;
+   struct stat statbuf;
+   char ki[512];
+   DWORD len = sizeof(ki);
+   if (infoFile == NULL) {
+      HKEY regkey;
+      fprintf(stderr, "Attempting to locate kindle.info\n");
+      if (RegOpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\\", &regkey) != ERROR_SUCCESS) {
+         fprintf(stderr, "Unable to locate kindle.info, please specify path on command line\n");
+         return result;      
+      }
+      
+//      if (RegGetValue(regkey, "Local AppData", NULL, NULL, ki, &len) != ERROR_SUCCESS) {
+      if (RegQueryValueEx(regkey, "Local AppData", NULL, NULL, ki, &len) != ERROR_SUCCESS) {
+         RegCloseKey(regkey);
+         fprintf(stderr, "Unable to locate kindle.info, please specify path on command line\n");
+         return result;      
+      }
+      ki[len] = 0;
+      strncat(ki, "\\Amazon\\Kindle For PC\\{AMAwzsaPaaZAzmZzZQzgZCAkZ3AjA_AY}\\kindle.info", sizeof(ki) - 1 - strlen(ki));
+      infoFile = ki;      
+      fprintf(stderr, "Found kindle.info location\n");
+   }
+   if (stat(infoFile, &statbuf) == 0) {
+      FILE *fd = fopen(infoFile, "rb");
+      char *infoBuf = (char*)malloc(statbuf.st_size + 1);
+      infoBuf[statbuf.st_size] = 0;
+      if (fread(infoBuf, statbuf.st_size, 1, fd) == 1) {
+         char *end = infoBuf + statbuf.st_size;
+         char *b = infoBuf, *e;
+         while (e = strchr(b, '{')) {
+            *e = 0;
+            if ((e - b) > 2) {
+               parseLine(b);
+            }
+            e++;
+            b = e;
+         }
+         if (b < end) {
+            parseLine(b);
+         }
+      }
+      else {
+         fprintf(stderr, "short read on info file\n");
+      }
+      free(infoBuf);
+      fclose(fd);
+      return 1;
+   }
+   return 0;
+}
+
+static unsigned int crc_table[256];
+
+void png_crc_table_init() {
+   unsigned int i;
+   if (crc_table[255]) return;
+   for (i = 0; i < 256; i++) {
+      unsigned int n = i;
+      unsigned int j;
+      for (j = 0; j < 8; j++) {
+         if (n & 1) {
+            n = 0xEDB88320 ^ (n >> 1);
+         }
+         else {
+            n >>= 1;
+         }
+      }
+      crc_table[i] = n;
+   }
+}
+
+unsigned int do_crc(unsigned char *input, unsigned int len) {
+   unsigned int crc = 0;
+   unsigned int i;
+   png_crc_table_init();
+   for (i = 0; i < len; i++) {
+      unsigned int v = (input[i] ^ crc) & 0xff;
+      crc = crc_table[v] ^ (crc >> 8);
+   }
+   return crc;
+}
+
+char *decodeString = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
+
+void doPngDecode(unsigned char *input, unsigned int len, unsigned char *output) {
+//   unsigned int crc_table[256];
+   unsigned int crc, i, x = 0;
+   unsigned int *out = (unsigned int*)output;
+   crc = bswap_l(do_crc(input, len));
+   memset(output, 0, 8);
+   for (i = 0; i < len; i++) {
+      output[x++] ^= input[i];
+      if (x == 8) x = 0;
+   }
+   out[0] ^= crc;
+   out[1] ^= crc;
+   for (i = 0; i < 8; i++) {
+      unsigned char v = output[i];
+      output[i] = decodeString[((((v >> 5) & 3) ^ v) & 0x1F) + (v >> 7)];
+   }
+}
+
+static char *string_32 = "n5Pr6St7Uv8Wx9YzAb0Cd1Ef2Gh3Jk4M";
+static char *string_64 = "AaZzB0bYyCc1XxDdW2wEeVv3FfUuG4g-TtHh5SsIiR6rJjQq7KkPpL8lOoMm9Nn_";
+
+char *mazamaEncode32(unsigned char *input, unsigned int len) {
+   return mazamaEncode(input, len, 32);
+}
+
+char *mazamaEncode64(unsigned char *input, unsigned int len) {
+   return mazamaEncode(input, len, 64);
+}
+
+char *mazamaEncode(unsigned char *input, unsigned int len, unsigned char choice) {
+   unsigned int i;
+   char *enc, *out;
+   if (choice == 0x20) enc = string_32;
+   else if (choice == 0x40) enc = string_64;
+   else return NULL;
+   out = (char*)malloc(len * 2 + 1);
+   out[len * 2] = 0;
+   for (i = 0; i < len; i++) {
+      unsigned char v = input[i] + 128;
+      unsigned char q = v / choice;
+      unsigned char m = v % choice;
+      out[i * 2] = enc[q];
+      out[i * 2 + 1] = enc[m];
+   }
+   return out;
+}
+
+unsigned char *mazamaDecode(char *input, int *outlen) {
+   unsigned char *out;
+   int len = strlen(input);
+   char *dec = NULL;
+   int i, choice = 0x20;
+   *outlen = 0;
+   for (i = 0; i < 8 && i < len; i++) {
+      if (*input == string_32[i]) {
+         dec = string_32;
+         break;
+      }
+   }
+   if (dec == NULL) {
+      for (i = 0; i < 4 && i < len; i++) {
+         if (*input == string_64[i]) {
+            dec = string_64;
+            choice = 0x40;
+            break;
+         }
+      }
+   }
+   if (dec == NULL) {
+      return NULL;
+   }
+   out = (unsigned char*)malloc(len / 2 + 1);
+   out[len / 2] = 0;
+   for (i = 0; i < len; i += 2) {
+      int q, m, v;
+      char *p = strchr(dec, input[i]);
+      if (p == NULL) break;
+      q = p - dec;
+      p = strchr(dec, input[i + 1]);
+      if (p == NULL) break;
+      m = p - dec;
+      v = (choice * q + m) - 128;
+      out[(*outlen)++] = (unsigned char)v;
+   }
+   return out;
+}
+
+#ifndef HEADER_MD5_H
+
+void md5(unsigned char *in, int len, unsigned char *md) {
+   MD5_CTX s;
+   MD5_Init(&s);
+       MD5_Update(&s, in, len);
+       MD5_Final(md, &s);
+}
+#endif
+
+#ifndef HEADER_SHA_H
+
+void sha1(unsigned char *in, int len, unsigned char *md) {
+   SHA_CTX s;
+   SHA1_Init(&s);
+       SHA1_Update(&s, in, len);
+       SHA1_Final(md, &s);
+}
+#endif
+
+char *getBookPid(unsigned char *keys, unsigned int klen, unsigned char *keysValue, unsigned int kvlen) {
+   unsigned char *vsn, *username, *mrn_key, *kat_key, *pid;
+   char drive[256];
+   char name[256];
+   DWORD nlen = sizeof(name);
+   char *d;
+   char volumeName[256];
+   DWORD volumeSerialNumber;
+   char fileSystemNameBuffer[256];
+   char volumeID[32];
+   unsigned char md5sum[MD5_DIGEST_LENGTH];
+   unsigned char sha1sum[SHA_DIGEST_LENGTH];
+   SHA_CTX sha1_ctx;
+   char *mv;
+
+   if (GetUserName(name, &nlen) == 0) {
+      fprintf(stderr, "GetUserName failed\n");
+      return NULL;
+   }
+   fprintf(stderr, "Using UserName = \"%s\"\n", name);
+
+   d = getenv("SystemDrive");
+   if (d) {
+      strcpy(drive, d);
+      strcat(drive, "\\");
+   }
+   else {
+      strcpy(drive, "c:\\");
+   }
+   fprintf(stderr, "Using SystemDrive = \"%s\"\n", drive);
+   if (GetVolumeInformation(drive, volumeName, sizeof(volumeName), &volumeSerialNumber,
+                        NULL, NULL, fileSystemNameBuffer, sizeof(fileSystemNameBuffer))) {
+      sprintf(volumeID, "%u", volumeSerialNumber);
+   }
+   else {
+      strcpy(volumeID, "9999999999");
+   }
+   fprintf(stderr, "Using VolumeSerialNumber = \"%s\"\n", volumeID);
+   MD5(volumeID, strlen(volumeID), md5sum);
+   vsn = mazamaEncode(md5sum, MD5_DIGEST_LENGTH, 0x20);
+
+   MD5(name, strlen(name), md5sum);
+   username = mazamaEncode(md5sum, MD5_DIGEST_LENGTH, 0x20);
+
+   MD5("MazamaRandomNumber", 18, md5sum);
+   mrn_key = mazamaEncode(md5sum, MD5_DIGEST_LENGTH, 0x40);
+
+   MD5("kindle.account.tokens", 21, md5sum);
+   kat_key = mazamaEncode(md5sum, MD5_DIGEST_LENGTH, 0x40);
+
+   SHA1_Init(&sha1_ctx);
+
+   mv = getKindleValue(mrn_key);
+   if (mv) {
+      DATA_BLOB DataIn;
+      DATA_BLOB DataOut;
+      DataIn.pbData = mazamaDecode(mv, (int*)&DataIn.cbData);
+      if (CryptUnprotectData(&DataIn, NULL, NULL, NULL, NULL, 1, &DataOut)) {
+         char *devId = (char*)malloc(DataOut.cbData + 4 * MD5_DIGEST_LENGTH + 1);
+         char *finalDevId;
+         unsigned char pidbuf[10];
+         
+//         fprintf(stderr, "CryptUnprotectData success\n");
+//         fwrite(DataOut.pbData, DataOut.cbData, 1, stderr);
+//         fprintf(stderr, "\n");
+
+         memcpy(devId, DataOut.pbData, DataOut.cbData);
+         strcpy(devId + DataOut.cbData, vsn);
+         strcat(devId + DataOut.cbData, username);
+
+//         fprintf(stderr, "Computing sha1 over %d bytes\n", DataOut.cbData + 4 * MD5_DIGEST_LENGTH);
+         sha1(devId, DataOut.cbData + 4 * MD5_DIGEST_LENGTH, sha1sum);
+         finalDevId = mazamaEncode(sha1sum, SHA_DIGEST_LENGTH, 0x20);
+//         fprintf(stderr, "finalDevId: %s\n", finalDevId);
+
+         SHA1_Update(&sha1_ctx, finalDevId, strlen(finalDevId));
+
+         pidbuf[8] = 0;
+         doPngDecode(finalDevId, 4, (unsigned char*)pidbuf);
+         fprintf(stderr, "Device PID: %s\n", pidbuf);
+
+         LocalFree(DataOut.pbData);
+         free(devId);
+         free(finalDevId);
+      }
+      else {
+         fprintf(stderr, "CryptUnprotectData failed, quitting\n");
+         free(kat_key);
+         free(mrn_key);
+         return NULL;
+      }
+
+      free(DataIn.pbData);
+   }
+   else {
+      fprintf(stderr, "Failed to find map node: %s\n", mrn_key);
+   }
+
+   mv = getKindleValue(kat_key);
+   if (mv) {
+      DATA_BLOB DataIn;
+      DATA_BLOB DataOut;
+      DataIn.pbData = mazamaDecode(mv, (int*)&DataIn.cbData);
+      if (CryptUnprotectData(&DataIn, NULL, NULL, NULL, NULL, 1, &DataOut)) {
+//         fprintf(stderr, "CryptUnprotectData success\n");
+//         fwrite(DataOut.pbData, DataOut.cbData, 1, stderr);
+//         fprintf(stderr, "\n");
+
+         SHA1_Update(&sha1_ctx, DataOut.pbData, DataOut.cbData);
+
+         LocalFree(DataOut.pbData);
+      }
+      else {
+         fprintf(stderr, "CryptUnprotectData failed, quitting\n");
+         free(kat_key);
+         free(mrn_key);
+         return NULL;
+      }
+
+      free(DataIn.pbData);
+   }
+   else {
+      fprintf(stderr, "Failed to find map node: %s\n", kat_key);
+   }
+
+   SHA1_Update(&sha1_ctx, keys, klen);
+   SHA1_Update(&sha1_ctx, keysValue, kvlen);
+   SHA1_Final(sha1sum, &sha1_ctx);
+
+   pid = (char*)malloc(SHA_DIGEST_LENGTH * 2);
+   base64(sha1sum, SHA_DIGEST_LENGTH, pid);
+
+   pid[8] = 0;
+
+   free(mrn_key);
+   free(kat_key);
+   free(vsn);
+   free(username);
+
+   return pid;
+}
+
+static char *letters = "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
+
+int verifyPidChecksum(char *pid) {
+   int l = strlen(letters);
+   unsigned int crc = ~do_crc(pid, 8);
+   unsigned char b;
+   crc = crc ^ (crc >> 16);
+   b = crc & 0xff;
+   if (pid[8] != letters[((b / l) ^ (b % l)) % l]) return 0;
+   crc >>= 8;
+   b = crc & 0xff;
+   if (pid[9] != letters[((b / l) ^ (b % l)) % l]) return 0;
+   return 1;
+}
diff --git a/skindle/skinutils.h b/skindle/skinutils.h
new file mode 100644 (file)
index 0000000..4b88c6d
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+   Copyright 2010 BartSimpson aka skindle
+   
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+#ifndef __SKINUTILS_H
+#define __SKINUTILS_H
+
+typedef struct _PidList {
+   unsigned int numPids;
+   char *pidList[1];  //extra pids to try from command line
+} PidList;
+
+typedef struct _MapList {
+   char *key;
+   char *value;
+   struct _MapList *next;
+} MapList;
+
+extern MapList *kindleMap;
+
+unsigned int base64(unsigned char *inbuf, unsigned int len, unsigned char *outbuf);
+
+unsigned short bswap_s(unsigned short s);
+unsigned int bswap_l(unsigned int s);
+
+char *translateKindleKey(char *key);
+MapList *findNode(MapList *map, char *key);
+MapList *findKindleNode(char *key);
+
+//don't free the result of getNodeValue;
+char *getNodeValue(MapList *map, char *key);
+char *getKindleValue(char *key);
+
+MapList *addMapNode(MapList *map, char *key, char *value);
+void dumpMap(MapList *m);
+
+void freeMap(MapList *m);
+
+int buildKindleMap(char *infoFile);
+void dumpKindleMap();
+
+//void png_crc_table_init(unsigned int *crc_table);
+unsigned int do_crc(unsigned char *input, unsigned int len);
+void doPngDecode(unsigned char *input, unsigned int len, unsigned char *output);
+
+char *mazamaEncode(unsigned char *input, unsigned int len, unsigned char choice);
+char *mazamaEncode32(unsigned char *input, unsigned int len);
+char *mazamaEncode64(unsigned char *input, unsigned int len);
+
+unsigned char *mazamaDecode(char *input, int *outlen);
+
+int verifyPidChecksum(char *pid);
+
+//If you prefer to use openssl uncomment the following
+//#include <openssl/sha.h>
+//#include <openssl/md5.h>
+
+#ifndef HEADER_MD5_H
+#include "md5.h"
+
+#define MD5_DIGEST_LENGTH 16
+
+#define MD5_CTX md5_state_t
+#define MD5_Init md5_init
+#define MD5_Update md5_append
+#define MD5_Final(x, y) md5_finish(y, x)
+#define MD5 md5
+
+void md5(unsigned char *in, int len, unsigned char *md);
+#endif
+
+#ifndef HEADER_SHA_H
+
+#include "sha1.h"
+
+#define SHA_DIGEST_LENGTH 20
+#define SHA_CTX sha1_state_s
+#define SHA1_Init sha1_init
+#define SHA1_Update sha1_update
+#define SHA1_Final(x, y) sha1_finish(y, x)
+#define SHA1 sha1
+
+void sha1(unsigned char *in, int len, unsigned char *md);
+#endif
+
+char *getBookPid(unsigned char *keys, unsigned int klen, unsigned char *keysValue, unsigned int kvlen);
+
+#endif
diff --git a/skindle/tpz.c b/skindle/tpz.c
new file mode 100644 (file)
index 0000000..564f31a
--- /dev/null
@@ -0,0 +1,504 @@
+/*
+   Copyright 2010 BartSimpson aka skindle
+   
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "skinutils.h"
+#include "cbuf.h"
+#include "tpz.h"
+#include "zlib.h"
+
+//
+// Context initialisation for the Topaz Crypto
+//
+void topazCryptoInit(TpzCtx *ctx, unsigned char *key, int klen) {
+   int i = 0; 
+   ctx->v[0] = 0x0CAFFE19E;
+    
+   for (i = 0; i < klen; i++) {
+      ctx->v[1] = ctx->v[0]; 
+      ctx->v[0] = ((ctx->v[0] >> 2) * (ctx->v[0] >> 7)) ^  
+                  (key[i] * key[i] * 0x0F902007);
+   }
+}
+
+//
+// decrypt data with the context prepared by topazCryptoInit()
+//
+    
+void topazCryptoDecrypt(TpzCtx *ctx, unsigned char *in, unsigned char *out, int len) {
+   unsigned int ctx1 = ctx->v[0];
+   unsigned int ctx2 = ctx->v[1];
+   int i;
+   for (i = 0; i < len; i++) {
+      unsigned char m = in[i] ^ (ctx1 >> 3) ^ (ctx2 << 3);
+      ctx2 = ctx1;
+      ctx1 = ((ctx1 >> 2) * (ctx1 >> 7)) ^ (m * m * 0x0F902007);
+      out[i] = m;
+   }
+}
+
+int bookReadEncodedNumber(FILE *f) {
+   int flag = 0;
+   int data = fgetc(f);
+   if (data == 0xFF) {  //negative number flag
+      flag = 1;
+      data = fgetc(f);
+   }
+   if (data >= 0x80) {
+      int datax = data & 0x7F;
+      while (data >= 0x80) {
+         data = fgetc(f);
+         datax = (datax << 7) + (data & 0x7F);
+      }
+      data = datax;
+   }
+   
+   if (flag) {
+      data = -data;
+   }
+   return data;
+}
+
+//
+// Encode a number in 7 bit format
+//
+
+int encodeNumber(int number, unsigned char *out) {
+   unsigned char *b = out;
+   unsigned char flag = 0;
+   int len;
+   int neg = number < 0;
+   
+   if (neg) {
+      number = -number + 1;
+   }
+   
+   do {
+      *b++ = (number & 0x7F) | flag;
+      number >>= 7;
+      flag = 0x80;
+   } while (number);  
+
+   if (neg) {
+      *b++ = 0xFF;
+   }
+   len = b - out;
+   b--;
+   while (out < b) {
+      unsigned char v = *out;
+      *out++ = *b;
+      *b-- = v;
+   } 
+   return len;
+}
+
+//
+// Get a length prefixed string from the file 
+//
+
+char *bookReadString(FILE *f) {
+   int len = bookReadEncodedNumber(f);
+   char *s = (char*)malloc(len + 1);
+   s[len] = 0;
+   if (fread(s, 1, len, f) != len) {
+      fprintf(stderr, "String read failed at filepos %x\n", ftell(f));
+      free(s);
+      s = NULL;
+   }
+   return s;
+}
+    
+//
+// Read and return the data of one header record at the current book file position [[offset,compressedLength,decompressedLength],...]
+//
+
+Record *bookReadHeaderRecordData(FILE *f) {
+   int nbValues = bookReadEncodedNumber(f);
+   Record *result = NULL;
+   Record *tail = NULL;
+   unsigned int i;
+   if (nbValues == -1) {
+      fprintf(stderr, "Parse Error : EOF encountered\n");
+      return NULL;
+   }
+   for (i = 0; i < nbValues; i++) {
+      Record *r = (Record*)malloc(sizeof(Record));
+      r->offset = bookReadEncodedNumber(f);
+      r->length = bookReadEncodedNumber(f);
+      r->compressed = bookReadEncodedNumber(f);
+      r->next = NULL;
+      if (result == NULL) {
+         result = r;
+      }
+      else {
+         tail->next = r;
+      }
+      tail = r;
+   }
+   return result;
+}
+
+//
+// Read and parse one header record at the current book file position and return the associated data [[offset,compressedLength,decompressedLength],...]
+//
+
+void freeRecordList(Record *r) {
+   Record *n;
+   while (r) {
+      n = r;
+      r = r->next;
+      free(n);
+   }
+}
+
+void freeHeaderList(HeaderRecord *r) {
+   HeaderRecord *n;
+   while (r) {
+      free(r->tag);
+      freeRecordList(r->rec);
+      n = r;
+      r = r->next;
+      free(n);
+   }
+}
+
+void freeTopazFile(TopazFile *t) {
+   freeHeaderList(t->hdrs);
+   freeMap(t->metadata);
+   free(t);
+}
+
+HeaderRecord *parseTopazHeaderRecord(FILE *f) {
+   char *tag;
+   Record *record;
+   if (fgetc(f) != 0x63) {
+      fprintf(stderr, "Parse Error : Invalid Header at 0x%x\n", ftell(f) - 1);
+      return NULL;
+   }
+    
+   tag = bookReadString(f);
+   record = bookReadHeaderRecordData(f);
+   if (tag && record) {
+      HeaderRecord *r = (HeaderRecord*)malloc(sizeof(Record));
+      r->tag = tag;
+      r->rec = record;
+      r->next = NULL;
+      return r;
+   }
+   return NULL;
+}
+
+//
+// Parse the header of a Topaz file, get all the header records and the offset for the payload
+//
+HeaderRecord *addRecord(HeaderRecord *head, HeaderRecord *r) {
+   HeaderRecord *i;
+   for (i = head; i; i = i->next) {
+      if (i->next == NULL) {
+         i->next = r;
+         return head;
+      }
+   }
+   return r;
+}
+
+TopazFile *parseTopazHeader(FILE *f) {
+   unsigned int numRecs, i, magic;
+   TopazFile *tpz;
+   if (fread(&magic, sizeof(magic), 1, f) != 1) {
+      fprintf(stderr, "Failed to read file magic\n");
+      return NULL;
+   }
+   
+   if (magic != 0x305a5054) {
+      fprintf(stderr, "Parse Error : Invalid Header, not a Topaz file");
+      return NULL;
+   }
+   
+   numRecs = fgetc(f);
+
+   tpz = (TopazFile*)calloc(sizeof(TopazFile), 1);
+   tpz->f = f;
+
+   for (i = 0; i < numRecs; i++) {
+      HeaderRecord *result = parseTopazHeaderRecord(f);
+      if (result == NULL) {
+         break;
+      }
+      tpz->hdrs = addRecord(tpz->hdrs, result);
+   }
+   
+   if (fgetc(f) != 0x64) {
+      fprintf(stderr, "Parse Error : Invalid Header end at pos 0x%x\n", ftell(f) - 1);
+      //empty list
+      freeTopazFile(tpz);
+      return NULL;
+   }
+   
+   tpz->bodyOffset = ftell(f);
+   return tpz;
+}
+
+HeaderRecord *findHeader(TopazFile *tpz, char *tag) {
+   HeaderRecord *hr;
+   for (hr = tpz->hdrs; hr; hr = hr->next) {
+      if (strcmp(hr->tag, tag) == 0) {
+         break;
+      }
+   }
+   return hr;
+}
+
+void freePayload(Payload *p) {
+   free(p->blob);
+   free(p);
+}
+
+//
+//Get a record in the book payload, given its name and index. If necessary the record is decrypted. The record is not decompressed
+//
+Payload *getBookPayloadRecord(TopazFile *t, char *name, int index, int explode) {   
+   int encrypted = 0;
+   int recordOffset, i, recordIndex;
+   Record *r;
+   int fileSize;
+   char *tag;
+   Payload *p;
+   off_t fileOffset;
+   HeaderRecord *hr = findHeader(t, name);
+
+   if (hr == NULL) {
+      fprintf(stderr, "Parse Error : Invalid Record, record %s not found\n", name);
+      return NULL;
+   }
+   r = hr->rec;
+   for (i = 0; r && i < index; i++) {
+      r = r->next;
+   }
+   if (r == NULL) {
+      fprintf(stderr, "Parse Error : Invalid Record, record %s:%d not found\n", name, index);
+      return NULL;
+   }
+   recordOffset = r->offset;
+    
+   if (fseek(t->f, t->bodyOffset + recordOffset, SEEK_SET) == -1) {
+      fprintf(stderr, "Parse Error : Invalid Record offset, record %s:%d\n", name, index);
+      return NULL;
+   }
+    
+   tag = bookReadString(t->f);
+   if (strcmp(tag, name)) {
+      fprintf(stderr, "Parse Error : Invalid Record offset, record %s:%d name doesn't match\n", name, index);
+      return NULL;
+   }    
+   recordIndex = bookReadEncodedNumber(t->f);
+    
+   if (recordIndex < 0) {
+      encrypted = 1;
+      recordIndex = -recordIndex - 1;
+   }
+    
+   if (recordIndex != index) {
+      fprintf(stderr, "Parse Error : Invalid Record offset, record %s:%d index doesn't match\n", name, index);
+      return NULL;
+   }
+   
+   fileSize = r->compressed ? r->compressed : r->length;
+   p = (Payload*)malloc(sizeof(Payload));
+   p->blob = (unsigned char*)malloc(fileSize);
+   p->len = fileSize;
+   p->name = name;
+   p->index = index;
+   fileOffset = ftell(t->f);
+   if (fread(p->blob, fileSize, 1, t->f) != 1) {
+      freePayload(p);
+      fprintf(stderr, "Parse Error : Failed payload read of record %s:%d offset 0x%x:0x%x\n", name, index, fileOffset, fileSize);
+      return NULL;
+   }     
+   
+   if (encrypted) {
+      TpzCtx ctx;
+      topazCryptoInit(&ctx, t->bookKey, 8);
+      topazCryptoDecrypt(&ctx, p->blob, p->blob, p->len);
+   }
+   
+   if (r->compressed && explode) {
+      unsigned char *db = (unsigned char *)malloc(r->length);
+      uLongf dl = r->length;
+      switch (uncompress(db, &dl, p->blob, p->len)) {
+         case Z_OK:
+            free(p->blob);
+            p->blob = db;
+            p->len = dl;
+            break;
+         case Z_MEM_ERROR:
+            free(db);
+            fprintf(stderr, "out of memory\n");
+            break;         
+         case Z_BUF_ERROR:
+            free(db);
+            fprintf(stderr, "output buffer wasn't large enough!\n");
+            break;
+      }
+   }
+   
+   return p;
+}
+
+//
+// Parse the metadata record from the book payload and return a list of [key,values]
+//
+
+char *getMetadata(TopazFile *t, char *key) {
+   return getNodeValue(t->metadata, key);
+}
+
+void parseMetadata(TopazFile *t) {
+   char *tag;
+   int flags, nbRecords, i;
+   HeaderRecord *hr = findHeader(t, "metadata");
+    
+   fseek(t->f, t->bodyOffset + hr->rec->offset, SEEK_SET);
+   tag = bookReadString(t->f);
+   if (strcmp(tag, "metadata")) {
+      //raise CMBDTCFatal("Parse Error : Record Names Don't Match")
+      return;
+   }
+    
+   flags = fgetc(t->f);
+   nbRecords = bookReadEncodedNumber(t->f);
+    
+   for (i = 0; i < nbRecords; i++) {
+      char *key = bookReadString(t->f);
+      char *value = bookReadString(t->f);
+      t->metadata = addMapNode(t->metadata, key, value);
+   }
+}
+
+//
+// Decrypt a payload record with the PID
+//
+
+void decryptRecord(unsigned char *in, int len, unsigned char *out, unsigned char *PID) {
+   TpzCtx ctx;
+   topazCryptoInit(&ctx, PID, 8);  //is this length correct
+   topazCryptoDecrypt(&ctx, in, out, len);
+}
+
+//
+// Try to decrypt a dkey record (contains the book PID)
+//
+unsigned char *decryptDkeyRecord(unsigned char *data, int len, unsigned char *PID) {
+   decryptRecord(data, len, data, PID);
+   //fields = unpack("3sB8sB8s3s",record);
+    
+   if (strncmp(data, "PID", 3) || strncmp(data + 21, "pid", 3)) {
+      fprintf(stderr, "Didn't find PID magic numbers in record\n");
+      return NULL;
+   }
+   else if (data[3] != 8 || data[12] != 8) {
+      fprintf(stderr, "Record didn't contain correct length fields\n");
+      return NULL;
+   }
+   else if (strncmp(data + 4, PID, 8)) {
+      fprintf(stderr, "Record didn't contain PID\n");
+      return NULL;
+   }
+   return data + 13;
+}
+    
+//
+// Decrypt all the book's dkey records (contain the book PID)
+//
+unsigned char *decryptDkeyRecords(Payload *data, unsigned char *PID) {
+   int nbKeyRecords = data->blob[0];  //is this encoded number?
+   int i, idx;
+   idx = 1;
+   unsigned char *key = NULL;
+//   records = []
+   for (i = 0; i < nbKeyRecords && idx < data->len; i++) {
+      int length = data->blob[idx++];
+      key = decryptDkeyRecord(data->blob + idx, length, PID);
+      if (key) break;    //???
+      idx += length;
+   }
+   return key;
+}
+
+void bufEncodeInt(cbuf *b, int i) {
+   unsigned char encoded[16];
+   int len = encodeNumber(i, encoded);
+   b_add_buf(b, encoded, len);
+}
+
+void bufEncodeString(cbuf *b, char *s) {
+   bufEncodeInt(b, strlen(s));
+   b_add_str(b, s);
+}
+
+void writeTopazOutputFile(TopazFile *t, FILE *out, cbuf *tpzHeaders, 
+                          cbuf *tpzBody, int explode) {
+   int i, numHdrs = 0;
+   HeaderRecord *h;
+   b_add_str(tpzHeaders, "TPZ0");
+   for (h = t->hdrs; h; h = h->next) {
+      if (strcmp(h->tag, "dkey")) {
+         numHdrs++;
+      }
+   }
+   bufEncodeInt(tpzHeaders, numHdrs);
+
+   b_add_byte(tpzBody, 0x40);
+
+   for (h = t->hdrs; h; h = h->next) {
+      Record *r;
+      int nr = 0, idx = 0;
+      if (strcmp(h->tag, "dkey") == 0) continue;
+      b_add_byte(tpzHeaders, 0x63);
+      bufEncodeString(tpzHeaders, h->tag);
+      for (r = h->rec; r; r = r->next) nr++;
+      bufEncodeInt(tpzHeaders, nr);
+      for (r = h->rec; r; r = r->next) {
+         Payload *p;
+         int b, e;
+         bufEncodeInt(tpzHeaders, tpzBody->idx);         
+         bufEncodeString(tpzBody, h->tag);
+         bufEncodeInt(tpzBody, idx);
+         b = tpzBody->idx;
+         p = getBookPayloadRecord(t, h->tag, idx++, explode);
+         b_add_buf(tpzBody, p->blob, p->len);
+         e = tpzBody->idx;
+
+         bufEncodeInt(tpzHeaders, r->length);   //this is length of blob portion after decompression
+         if (explode) {
+            bufEncodeInt(tpzHeaders, 0); //this is the length in the file if compressed
+         }
+         else {
+            bufEncodeInt(tpzHeaders, r->compressed); //this is the length in the file if compressed
+         }
+         
+         freePayload(p);
+      }
+   }
+   
+   b_add_byte(tpzHeaders, 0x64);
+}
+
diff --git a/skindle/tpz.h b/skindle/tpz.h
new file mode 100644 (file)
index 0000000..4138171
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+   Copyright 2010 BartSimpson aka skindle
+   
+   Licensed under the Apache License, Version 2.0 (the "License");
+   you may not use this file except in compliance with the License.
+   You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+*/
+
+#ifndef __TPZ_H
+#define __TPZ_H
+
+#include <stdio.h>
+#include "skinutils.h"
+
+typedef struct _TpzCtx {
+   unsigned int v[2];
+} TpzCtx;
+
+void topazCryptoInit(TpzCtx *ctx, unsigned char *key, int klen);
+void topazCryptoDecrypt(TpzCtx *ctx, unsigned char *in, unsigned char *out, int len);
+int bookReadEncodedNumber(FILE *f);
+int encodeNumber(int number, unsigned char *out);
+char *bookReadString(FILE *f);
+
+typedef struct _Payload {
+   unsigned char *blob;
+   unsigned int len;
+   char *name;
+   int index;
+} Payload;
+
+typedef struct _Record {
+   int offset;
+   int length;
+   int compressed;
+   struct _Record *next;
+} Record;
+
+typedef struct _HeaderRecord {
+   char *tag;
+   Record *rec;
+   struct _HeaderRecord *next;
+} HeaderRecord;
+
+typedef struct _TopazFile {
+   FILE *f;
+   HeaderRecord *hdrs;
+   unsigned char *bookKey;
+   unsigned int bodyOffset;
+   MapList *metadata;
+   PidList *pids;    //extra pids to try from command line
+} TopazFile;
+
+Record *bookReadHeaderRecordData(FILE *f);
+void freeRecordList(Record *r);
+void freeHeaderList(HeaderRecord *r);
+void freeTopazFile(TopazFile *t);
+HeaderRecord *parseTopazHeaderRecord(FILE *f);
+HeaderRecord *addRecord(HeaderRecord *head, HeaderRecord *r);
+TopazFile *parseTopazHeader(FILE *f);
+void freeTopazFile(TopazFile *tpz);
+HeaderRecord *findHeader(TopazFile *tpz, char *tag);
+void freePayload(Payload *p);
+Payload *getBookPayloadRecord(TopazFile *t, char *name, int index, int explode);
+char *getMetadata(TopazFile *t, char *key);
+void parseMetadata(TopazFile *t);
+void decryptRecord(unsigned char *in, int len, unsigned char *out, unsigned char *PID);
+unsigned char *decryptDkeyRecord(unsigned char *data, int len, unsigned char *PID);
+unsigned char *decryptDkeyRecords(Payload *data, unsigned char *PID);
+void writeTopazOutputFile(TopazFile *t, FILE *out, cbuf *tpzHeaders, 
+                          cbuf *tpzBody, int explode);
+
+
+#endif
diff --git a/skindle/zconf.h b/skindle/zconf.h
new file mode 100644 (file)
index 0000000..03a9431
--- /dev/null
@@ -0,0 +1,332 @@
+/* zconf.h -- configuration of the zlib compression library
+ * Copyright (C) 1995-2005 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* @(#) $Id$ */
+
+#ifndef ZCONF_H
+#define ZCONF_H
+
+/*
+ * If you *really* need a unique prefix for all types and library functions,
+ * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
+ */
+#ifdef Z_PREFIX
+#  define deflateInit_          z_deflateInit_
+#  define deflate               z_deflate
+#  define deflateEnd            z_deflateEnd
+#  define inflateInit_          z_inflateInit_
+#  define inflate               z_inflate
+#  define inflateEnd            z_inflateEnd
+#  define deflateInit2_         z_deflateInit2_
+#  define deflateSetDictionary  z_deflateSetDictionary
+#  define deflateCopy           z_deflateCopy
+#  define deflateReset          z_deflateReset
+#  define deflateParams         z_deflateParams
+#  define deflateBound          z_deflateBound
+#  define deflatePrime          z_deflatePrime
+#  define inflateInit2_         z_inflateInit2_
+#  define inflateSetDictionary  z_inflateSetDictionary
+#  define inflateSync           z_inflateSync
+#  define inflateSyncPoint      z_inflateSyncPoint
+#  define inflateCopy           z_inflateCopy
+#  define inflateReset          z_inflateReset
+#  define inflateBack           z_inflateBack
+#  define inflateBackEnd        z_inflateBackEnd
+#  define compress              z_compress
+#  define compress2             z_compress2
+#  define compressBound         z_compressBound
+#  define uncompress            z_uncompress
+#  define adler32               z_adler32
+#  define crc32                 z_crc32
+#  define get_crc_table         z_get_crc_table
+#  define zError                z_zError
+
+#  define alloc_func            z_alloc_func
+#  define free_func             z_free_func
+#  define in_func               z_in_func
+#  define out_func              z_out_func
+#  define Byte                  z_Byte
+#  define uInt                  z_uInt
+#  define uLong                 z_uLong
+#  define Bytef                 z_Bytef
+#  define charf                 z_charf
+#  define intf                  z_intf
+#  define uIntf                 z_uIntf
+#  define uLongf                z_uLongf
+#  define voidpf                z_voidpf
+#  define voidp                 z_voidp
+#endif
+
+#if defined(__MSDOS__) && !defined(MSDOS)
+#  define MSDOS
+#endif
+#if (defined(OS_2) || defined(__OS2__)) && !defined(OS2)
+#  define OS2
+#endif
+#if defined(_WINDOWS) && !defined(WINDOWS)
+#  define WINDOWS
+#endif
+#if defined(_WIN32) || defined(_WIN32_WCE) || defined(__WIN32__)
+#  ifndef WIN32
+#    define WIN32
+#  endif
+#endif
+#if (defined(MSDOS) || defined(OS2) || defined(WINDOWS)) && !defined(WIN32)
+#  if !defined(__GNUC__) && !defined(__FLAT__) && !defined(__386__)
+#    ifndef SYS16BIT
+#      define SYS16BIT
+#    endif
+#  endif
+#endif
+
+/*
+ * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
+ * than 64k bytes at a time (needed on systems with 16-bit int).
+ */
+#ifdef SYS16BIT
+#  define MAXSEG_64K
+#endif
+#ifdef MSDOS
+#  define UNALIGNED_OK
+#endif
+
+#ifdef __STDC_VERSION__
+#  ifndef STDC
+#    define STDC
+#  endif
+#  if __STDC_VERSION__ >= 199901L
+#    ifndef STDC99
+#      define STDC99
+#    endif
+#  endif
+#endif
+#if !defined(STDC) && (defined(__STDC__) || defined(__cplusplus))
+#  define STDC
+#endif
+#if !defined(STDC) && (defined(__GNUC__) || defined(__BORLANDC__))
+#  define STDC
+#endif
+#if !defined(STDC) && (defined(MSDOS) || defined(WINDOWS) || defined(WIN32))
+#  define STDC
+#endif
+#if !defined(STDC) && (defined(OS2) || defined(__HOS_AIX__))
+#  define STDC
+#endif
+
+#if defined(__OS400__) && !defined(STDC)    /* iSeries (formerly AS/400). */
+#  define STDC
+#endif
+
+#ifndef STDC
+#  ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
+#    define const       /* note: need a more gentle solution here */
+#  endif
+#endif
+
+/* Some Mac compilers merge all .h files incorrectly: */
+#if defined(__MWERKS__)||defined(applec)||defined(THINK_C)||defined(__SC__)
+#  define NO_DUMMY_DECL
+#endif
+
+/* Maximum value for memLevel in deflateInit2 */
+#ifndef MAX_MEM_LEVEL
+#  ifdef MAXSEG_64K
+#    define MAX_MEM_LEVEL 8
+#  else
+#    define MAX_MEM_LEVEL 9
+#  endif
+#endif
+
+/* Maximum value for windowBits in deflateInit2 and inflateInit2.
+ * WARNING: reducing MAX_WBITS makes minigzip unable to extract .gz files
+ * created by gzip. (Files created by minigzip can still be extracted by
+ * gzip.)
+ */
+#ifndef MAX_WBITS
+#  define MAX_WBITS   15 /* 32K LZ77 window */
+#endif
+
+/* The memory requirements for deflate are (in bytes):
+            (1 << (windowBits+2)) +  (1 << (memLevel+9))
+ that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)
+ plus a few kilobytes for small objects. For example, if you want to reduce
+ the default memory requirements from 256K to 128K, compile with
+     make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
+ Of course this will generally degrade compression (there's no free lunch).
+
+   The memory requirements for inflate are (in bytes) 1 << windowBits
+ that is, 32K for windowBits=15 (default value) plus a few kilobytes
+ for small objects.
+*/
+
+                        /* Type declarations */
+
+#ifndef OF /* function prototypes */
+#  ifdef STDC
+#    define OF(args)  args
+#  else
+#    define OF(args)  ()
+#  endif
+#endif
+
+/* The following definitions for FAR are needed only for MSDOS mixed
+ * model programming (small or medium model with some far allocations).
+ * This was tested only with MSC; for other MSDOS compilers you may have
+ * to define NO_MEMCPY in zutil.h.  If you don't need the mixed model,
+ * just define FAR to be empty.
+ */
+#ifdef SYS16BIT
+#  if defined(M_I86SM) || defined(M_I86MM)
+     /* MSC small or medium model */
+#    define SMALL_MEDIUM
+#    ifdef _MSC_VER
+#      define FAR _far
+#    else
+#      define FAR far
+#    endif
+#  endif
+#  if (defined(__SMALL__) || defined(__MEDIUM__))
+     /* Turbo C small or medium model */
+#    define SMALL_MEDIUM
+#    ifdef __BORLANDC__
+#      define FAR _far
+#    else
+#      define FAR far
+#    endif
+#  endif
+#endif
+
+#if defined(WINDOWS) || defined(WIN32)
+   /* If building or using zlib as a DLL, define ZLIB_DLL.
+    * This is not mandatory, but it offers a little performance increase.
+    */
+#  ifdef ZLIB_DLL
+#    if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
+#      ifdef ZLIB_INTERNAL
+#        define ZEXTERN extern __declspec(dllexport)
+#      else
+#        define ZEXTERN extern __declspec(dllimport)
+#      endif
+#    endif
+#  endif  /* ZLIB_DLL */
+   /* If building or using zlib with the WINAPI/WINAPIV calling convention,
+    * define ZLIB_WINAPI.
+    * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
+    */
+#  ifdef ZLIB_WINAPI
+#    ifdef FAR
+#      undef FAR
+#    endif
+#    include <windows.h>
+     /* No need for _export, use ZLIB.DEF instead. */
+     /* For complete Windows compatibility, use WINAPI, not __stdcall. */
+#    define ZEXPORT WINAPI
+#    ifdef WIN32
+#      define ZEXPORTVA WINAPIV
+#    else
+#      define ZEXPORTVA FAR CDECL
+#    endif
+#  endif
+#endif
+
+#if defined (__BEOS__)
+#  ifdef ZLIB_DLL
+#    ifdef ZLIB_INTERNAL
+#      define ZEXPORT   __declspec(dllexport)
+#      define ZEXPORTVA __declspec(dllexport)
+#    else
+#      define ZEXPORT   __declspec(dllimport)
+#      define ZEXPORTVA __declspec(dllimport)
+#    endif
+#  endif
+#endif
+
+#ifndef ZEXTERN
+#  define ZEXTERN extern
+#endif
+#ifndef ZEXPORT
+#  define ZEXPORT
+#endif
+#ifndef ZEXPORTVA
+#  define ZEXPORTVA
+#endif
+
+#ifndef FAR
+#  define FAR
+#endif
+
+#if !defined(__MACTYPES__)
+typedef unsigned char  Byte;  /* 8 bits */
+#endif
+typedef unsigned int   uInt;  /* 16 bits or more */
+typedef unsigned long  uLong; /* 32 bits or more */
+
+#ifdef SMALL_MEDIUM
+   /* Borland C/C++ and some old MSC versions ignore FAR inside typedef */
+#  define Bytef Byte FAR
+#else
+   typedef Byte  FAR Bytef;
+#endif
+typedef char  FAR charf;
+typedef int   FAR intf;
+typedef uInt  FAR uIntf;
+typedef uLong FAR uLongf;
+
+#ifdef STDC
+   typedef void const *voidpc;
+   typedef void FAR   *voidpf;
+   typedef void       *voidp;
+#else
+   typedef Byte const *voidpc;
+   typedef Byte FAR   *voidpf;
+   typedef Byte       *voidp;
+#endif
+
+#if 0           /* HAVE_UNISTD_H -- this line is updated by ./configure */
+#  include <sys/types.h> /* for off_t */
+#  include <unistd.h>    /* for SEEK_* and off_t */
+#  ifdef VMS
+#    include <unixio.h>   /* for off_t */
+#  endif
+#  define z_off_t off_t
+#endif
+#ifndef SEEK_SET
+#  define SEEK_SET        0       /* Seek from beginning of file.  */
+#  define SEEK_CUR        1       /* Seek from current position.  */
+#  define SEEK_END        2       /* Set file pointer to EOF plus "offset" */
+#endif
+#ifndef z_off_t
+#  define z_off_t long
+#endif
+
+#if defined(__OS400__)
+#  define NO_vsnprintf
+#endif
+
+#if defined(__MVS__)
+#  define NO_vsnprintf
+#  ifdef FAR
+#    undef FAR
+#  endif
+#endif
+
+/* MVS linker does not support external names larger than 8 bytes */
+#if defined(__MVS__)
+#   pragma map(deflateInit_,"DEIN")
+#   pragma map(deflateInit2_,"DEIN2")
+#   pragma map(deflateEnd,"DEEND")
+#   pragma map(deflateBound,"DEBND")
+#   pragma map(inflateInit_,"ININ")
+#   pragma map(inflateInit2_,"ININ2")
+#   pragma map(inflateEnd,"INEND")
+#   pragma map(inflateSync,"INSY")
+#   pragma map(inflateSetDictionary,"INSEDI")
+#   pragma map(compressBound,"CMBND")
+#   pragma map(inflate_table,"INTABL")
+#   pragma map(inflate_fast,"INFA")
+#   pragma map(inflate_copyright,"INCOPY")
+#endif
+
+#endif /* ZCONF_H */
diff --git a/skindle/zlib.h b/skindle/zlib.h
new file mode 100644 (file)
index 0000000..0228179
--- /dev/null
@@ -0,0 +1,1357 @@
+/* zlib.h -- interface of the 'zlib' general purpose compression library
+  version 1.2.3, July 18th, 2005
+
+  Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+
+  Jean-loup Gailly        Mark Adler
+  jloup@gzip.org          madler@alumni.caltech.edu
+
+
+  The data format used by the zlib library is described by RFCs (Request for
+  Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
+  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
+*/
+
+#ifndef ZLIB_H
+#define ZLIB_H
+
+#include "zconf.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ZLIB_VERSION "1.2.3"
+#define ZLIB_VERNUM 0x1230
+
+/*
+     The 'zlib' compression library provides in-memory compression and
+  decompression functions, including integrity checks of the uncompressed
+  data.  This version of the library supports only one compression method
+  (deflation) but other algorithms will be added later and will have the same
+  stream interface.
+
+     Compression can be done in a single step if the buffers are large
+  enough (for example if an input file is mmap'ed), or can be done by
+  repeated calls of the compression function.  In the latter case, the
+  application must provide more input and/or consume the output
+  (providing more output space) before each call.
+
+     The compressed data format used by default by the in-memory functions is
+  the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
+  around a deflate stream, which is itself documented in RFC 1951.
+
+     The library also supports reading and writing files in gzip (.gz) format
+  with an interface similar to that of stdio using the functions that start
+  with "gz".  The gzip format is different from the zlib format.  gzip is a
+  gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
+
+     This library can optionally read and write gzip streams in memory as well.
+
+     The zlib format was designed to be compact and fast for use in memory
+  and on communications channels.  The gzip format was designed for single-
+  file compression on file systems, has a larger header than zlib to maintain
+  directory information, and uses a different, slower check method than zlib.
+
+     The library does not install any signal handler. The decoder checks
+  the consistency of the compressed data, so the library should never
+  crash even in case of corrupted input.
+*/
+
+typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
+typedef void   (*free_func)  OF((voidpf opaque, voidpf address));
+
+struct internal_state;
+
+typedef struct z_stream_s {
+    Bytef    *next_in;  /* next input byte */
+    uInt     avail_in;  /* number of bytes available at next_in */
+    uLong    total_in;  /* total nb of input bytes read so far */
+
+    Bytef    *next_out; /* next output byte should be put there */
+    uInt     avail_out; /* remaining free space at next_out */
+    uLong    total_out; /* total nb of bytes output so far */
+
+    char     *msg;      /* last error message, NULL if no error */
+    struct internal_state FAR *state; /* not visible by applications */
+
+    alloc_func zalloc;  /* used to allocate the internal state */
+    free_func  zfree;   /* used to free the internal state */
+    voidpf     opaque;  /* private data object passed to zalloc and zfree */
+
+    int     data_type;  /* best guess about the data type: binary or text */
+    uLong   adler;      /* adler32 value of the uncompressed data */
+    uLong   reserved;   /* reserved for future use */
+} z_stream;
+
+typedef z_stream FAR *z_streamp;
+
+/*
+     gzip header information passed to and from zlib routines.  See RFC 1952
+  for more details on the meanings of these fields.
+*/
+typedef struct gz_header_s {
+    int     text;       /* true if compressed data believed to be text */
+    uLong   time;       /* modification time */
+    int     xflags;     /* extra flags (not used when writing a gzip file) */
+    int     os;         /* operating system */
+    Bytef   *extra;     /* pointer to extra field or Z_NULL if none */
+    uInt    extra_len;  /* extra field length (valid if extra != Z_NULL) */
+    uInt    extra_max;  /* space at extra (only when reading header) */
+    Bytef   *name;      /* pointer to zero-terminated file name or Z_NULL */
+    uInt    name_max;   /* space at name (only when reading header) */
+    Bytef   *comment;   /* pointer to zero-terminated comment or Z_NULL */
+    uInt    comm_max;   /* space at comment (only when reading header) */
+    int     hcrc;       /* true if there was or will be a header crc */
+    int     done;       /* true when done reading gzip header (not used
+                           when writing a gzip file) */
+} gz_header;
+
+typedef gz_header FAR *gz_headerp;
+
+/*
+   The application must update next_in and avail_in when avail_in has
+   dropped to zero. It must update next_out and avail_out when avail_out
+   has dropped to zero. The application must initialize zalloc, zfree and
+   opaque before calling the init function. All other fields are set by the
+   compression library and must not be updated by the application.
+
+   The opaque value provided by the application will be passed as the first
+   parameter for calls of zalloc and zfree. This can be useful for custom
+   memory management. The compression library attaches no meaning to the
+   opaque value.
+
+   zalloc must return Z_NULL if there is not enough memory for the object.
+   If zlib is used in a multi-threaded application, zalloc and zfree must be
+   thread safe.
+
+   On 16-bit systems, the functions zalloc and zfree must be able to allocate
+   exactly 65536 bytes, but will not be required to allocate more than this
+   if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
+   pointers returned by zalloc for objects of exactly 65536 bytes *must*
+   have their offset normalized to zero. The default allocation function
+   provided by this library ensures this (see zutil.c). To reduce memory
+   requirements and avoid any allocation of 64K objects, at the expense of
+   compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
+
+   The fields total_in and total_out can be used for statistics or
+   progress reports. After compression, total_in holds the total size of
+   the uncompressed data and may be saved for use in the decompressor
+   (particularly if the decompressor wants to decompress everything in
+   a single step).
+*/
+
+                        /* constants */
+
+#define Z_NO_FLUSH      0
+#define Z_PARTIAL_FLUSH 1 /* will be removed, use Z_SYNC_FLUSH instead */
+#define Z_SYNC_FLUSH    2
+#define Z_FULL_FLUSH    3
+#define Z_FINISH        4
+#define Z_BLOCK         5
+/* Allowed flush values; see deflate() and inflate() below for details */
+
+#define Z_OK            0
+#define Z_STREAM_END    1
+#define Z_NEED_DICT     2
+#define Z_ERRNO        (-1)
+#define Z_STREAM_ERROR (-2)
+#define Z_DATA_ERROR   (-3)
+#define Z_MEM_ERROR    (-4)
+#define Z_BUF_ERROR    (-5)
+#define Z_VERSION_ERROR (-6)
+/* Return codes for the compression/decompression functions. Negative
+ * values are errors, positive values are used for special but normal events.
+ */
+
+#define Z_NO_COMPRESSION         0
+#define Z_BEST_SPEED             1
+#define Z_BEST_COMPRESSION       9
+#define Z_DEFAULT_COMPRESSION  (-1)
+/* compression levels */
+
+#define Z_FILTERED            1
+#define Z_HUFFMAN_ONLY        2
+#define Z_RLE                 3
+#define Z_FIXED               4
+#define Z_DEFAULT_STRATEGY    0
+/* compression strategy; see deflateInit2() below for details */
+
+#define Z_BINARY   0
+#define Z_TEXT     1
+#define Z_ASCII    Z_TEXT   /* for compatibility with 1.2.2 and earlier */
+#define Z_UNKNOWN  2
+/* Possible values of the data_type field (though see inflate()) */
+
+#define Z_DEFLATED   8
+/* The deflate compression method (the only one supported in this version) */
+
+#define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
+
+#define zlib_version zlibVersion()
+/* for compatibility with versions < 1.0.2 */
+
+                        /* basic functions */
+
+ZEXTERN const char * ZEXPORT zlibVersion OF((void));
+/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
+   If the first character differs, the library code actually used is
+   not compatible with the zlib.h header file used by the application.
+   This check is automatically made by deflateInit and inflateInit.
+ */
+
+/*
+ZEXTERN int ZEXPORT deflateInit OF((z_streamp strm, int level));
+
+     Initializes the internal stream state for compression. The fields
+   zalloc, zfree and opaque must be initialized before by the caller.
+   If zalloc and zfree are set to Z_NULL, deflateInit updates them to
+   use default allocation functions.
+
+     The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
+   1 gives best speed, 9 gives best compression, 0 gives no compression at
+   all (the input data is simply copied a block at a time).
+   Z_DEFAULT_COMPRESSION requests a default compromise between speed and
+   compression (currently equivalent to level 6).
+
+     deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_STREAM_ERROR if level is not a valid compression level,
+   Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
+   with the version assumed by the caller (ZLIB_VERSION).
+   msg is set to null if there is no error message.  deflateInit does not
+   perform any compression: this will be done by deflate().
+*/
+
+
+ZEXTERN int ZEXPORT deflate OF((z_streamp strm, int flush));
+/*
+    deflate compresses as much data as possible, and stops when the input
+  buffer becomes empty or the output buffer becomes full. It may introduce some
+  output latency (reading input without producing any output) except when
+  forced to flush.
+
+    The detailed semantics are as follows. deflate performs one or both of the
+  following actions:
+
+  - Compress more input starting at next_in and update next_in and avail_in
+    accordingly. If not all input can be processed (because there is not
+    enough room in the output buffer), next_in and avail_in are updated and
+    processing will resume at this point for the next call of deflate().
+
+  - Provide more output starting at next_out and update next_out and avail_out
+    accordingly. This action is forced if the parameter flush is non zero.
+    Forcing flush frequently degrades the compression ratio, so this parameter
+    should be set only when necessary (in interactive applications).
+    Some output may be provided even if flush is not set.
+
+  Before the call of deflate(), the application should ensure that at least
+  one of the actions is possible, by providing more input and/or consuming
+  more output, and updating avail_in or avail_out accordingly; avail_out
+  should never be zero before the call. The application can consume the
+  compressed output when it wants, for example when the output buffer is full
+  (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
+  and with zero avail_out, it must be called again after making room in the
+  output buffer because there might be more output pending.
+
+    Normally the parameter flush is set to Z_NO_FLUSH, which allows deflate to
+  decide how much data to accumualte before producing output, in order to
+  maximize compression.
+
+    If the parameter flush is set to Z_SYNC_FLUSH, all pending output is
+  flushed to the output buffer and the output is aligned on a byte boundary, so
+  that the decompressor can get all input data available so far. (In particular
+  avail_in is zero after the call if enough output space has been provided
+  before the call.)  Flushing may degrade compression for some compression
+  algorithms and so it should be used only when necessary.
+
+    If flush is set to Z_FULL_FLUSH, all output is flushed as with
+  Z_SYNC_FLUSH, and the compression state is reset so that decompression can
+  restart from this point if previous compressed data has been damaged or if
+  random access is desired. Using Z_FULL_FLUSH too often can seriously degrade
+  compression.
+
+    If deflate returns with avail_out == 0, this function must be called again
+  with the same value of the flush parameter and more output space (updated
+  avail_out), until the flush is complete (deflate returns with non-zero
+  avail_out). In the case of a Z_FULL_FLUSH or Z_SYNC_FLUSH, make sure that
+  avail_out is greater than six to avoid repeated flush markers due to
+  avail_out == 0 on return.
+
+    If the parameter flush is set to Z_FINISH, pending input is processed,
+  pending output is flushed and deflate returns with Z_STREAM_END if there
+  was enough output space; if deflate returns with Z_OK, this function must be
+  called again with Z_FINISH and more output space (updated avail_out) but no
+  more input data, until it returns with Z_STREAM_END or an error. After
+  deflate has returned Z_STREAM_END, the only possible operations on the
+  stream are deflateReset or deflateEnd.
+
+    Z_FINISH can be used immediately after deflateInit if all the compression
+  is to be done in a single step. In this case, avail_out must be at least
+  the value returned by deflateBound (see below). If deflate does not return
+  Z_STREAM_END, then it must be called again as described above.
+
+    deflate() sets strm->adler to the adler32 checksum of all input read
+  so far (that is, total_in bytes).
+
+    deflate() may update strm->data_type if it can make a good guess about
+  the input data type (Z_BINARY or Z_TEXT). In doubt, the data is considered
+  binary. This field is only for information purposes and does not affect
+  the compression algorithm in any manner.
+
+    deflate() returns Z_OK if some progress has been made (more input
+  processed or more output produced), Z_STREAM_END if all input has been
+  consumed and all output has been produced (only when flush is set to
+  Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
+  if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible
+  (for example avail_in or avail_out was zero). Note that Z_BUF_ERROR is not
+  fatal, and deflate() can be called again with more input and more output
+  space to continue compressing.
+*/
+
+
+ZEXTERN int ZEXPORT deflateEnd OF((z_streamp strm));
+/*
+     All dynamically allocated data structures for this stream are freed.
+   This function discards any unprocessed input and does not flush any
+   pending output.
+
+     deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
+   stream state was inconsistent, Z_DATA_ERROR if the stream was freed
+   prematurely (some input or output was discarded). In the error case,
+   msg may be set but then points to a static string (which must not be
+   deallocated).
+*/
+
+
+/*
+ZEXTERN int ZEXPORT inflateInit OF((z_streamp strm));
+
+     Initializes the internal stream state for decompression. The fields
+   next_in, avail_in, zalloc, zfree and opaque must be initialized before by
+   the caller. If next_in is not Z_NULL and avail_in is large enough (the exact
+   value depends on the compression method), inflateInit determines the
+   compression method from the zlib header and allocates all data structures
+   accordingly; otherwise the allocation will be deferred to the first call of
+   inflate.  If zalloc and zfree are set to Z_NULL, inflateInit updates them to
+   use default allocation functions.
+
+     inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not enough
+   memory, Z_VERSION_ERROR if the zlib library version is incompatible with the
+   version assumed by the caller.  msg is set to null if there is no error
+   message. inflateInit does not perform any decompression apart from reading
+   the zlib header if present: this will be done by inflate().  (So next_in and
+   avail_in may be modified, but next_out and avail_out are unchanged.)
+*/
+
+
+ZEXTERN int ZEXPORT inflate OF((z_streamp strm, int flush));
+/*
+    inflate decompresses as much data as possible, and stops when the input
+  buffer becomes empty or the output buffer becomes full. It may introduce
+  some output latency (reading input without producing any output) except when
+  forced to flush.
+
+  The detailed semantics are as follows. inflate performs one or both of the
+  following actions:
+
+  - Decompress more input starting at next_in and update next_in and avail_in
+    accordingly. If not all input can be processed (because there is not
+    enough room in the output buffer), next_in is updated and processing
+    will resume at this point for the next call of inflate().
+
+  - Provide more output starting at next_out and update next_out and avail_out
+    accordingly.  inflate() provides as much output as possible, until there
+    is no more input data or no more space in the output buffer (see below
+    about the flush parameter).
+
+  Before the call of inflate(), the application should ensure that at least
+  one of the actions is possible, by providing more input and/or consuming
+  more output, and updating the next_* and avail_* values accordingly.
+  The application can consume the uncompressed output when it wants, for
+  example when the output buffer is full (avail_out == 0), or after each
+  call of inflate(). If inflate returns Z_OK and with zero avail_out, it
+  must be called again after making room in the output buffer because there
+  might be more output pending.
+
+    The flush parameter of inflate() can be Z_NO_FLUSH, Z_SYNC_FLUSH,
+  Z_FINISH, or Z_BLOCK. Z_SYNC_FLUSH requests that inflate() flush as much
+  output as possible to the output buffer. Z_BLOCK requests that inflate() stop
+  if and when it gets to the next deflate block boundary. When decoding the
+  zlib or gzip format, this will cause inflate() to return immediately after
+  the header and before the first block. When doing a raw inflate, inflate()
+  will go ahead and process the first block, and will return when it gets to
+  the end of that block, or when it runs out of data.
+
+    The Z_BLOCK option assists in appending to or combining deflate streams.
+  Also to assist in this, on return inflate() will set strm->data_type to the
+  number of unused bits in the last byte taken from strm->next_in, plus 64
+  if inflate() is currently decoding the last block in the deflate stream,
+  plus 128 if inflate() returned immediately after decoding an end-of-block
+  code or decoding the complete header up to just before the first byte of the
+  deflate stream. The end-of-block will not be indicated until all of the
+  uncompressed data from that block has been written to strm->next_out.  The
+  number of unused bits may in general be greater than seven, except when
+  bit 7 of data_type is set, in which case the number of unused bits will be
+  less than eight.
+
+    inflate() should normally be called until it returns Z_STREAM_END or an
+  error. However if all decompression is to be performed in a single step
+  (a single call of inflate), the parameter flush should be set to
+  Z_FINISH. In this case all pending input is processed and all pending
+  output is flushed; avail_out must be large enough to hold all the
+  uncompressed data. (The size of the uncompressed data may have been saved
+  by the compressor for this purpose.) The next operation on this stream must
+  be inflateEnd to deallocate the decompression state. The use of Z_FINISH
+  is never required, but can be used to inform inflate that a faster approach
+  may be used for the single inflate() call.
+
+     In this implementation, inflate() always flushes as much output as
+  possible to the output buffer, and always uses the faster approach on the
+  first call. So the only effect of the flush parameter in this implementation
+  is on the return value of inflate(), as noted below, or when it returns early
+  because Z_BLOCK is used.
+
+     If a preset dictionary is needed after this call (see inflateSetDictionary
+  below), inflate sets strm->adler to the adler32 checksum of the dictionary
+  chosen by the compressor and returns Z_NEED_DICT; otherwise it sets
+  strm->adler to the adler32 checksum of all output produced so far (that is,
+  total_out bytes) and returns Z_OK, Z_STREAM_END or an error code as described
+  below. At the end of the stream, inflate() checks that its computed adler32
+  checksum is equal to that saved by the compressor and returns Z_STREAM_END
+  only if the checksum is correct.
+
+    inflate() will decompress and check either zlib-wrapped or gzip-wrapped
+  deflate data.  The header type is detected automatically.  Any information
+  contained in the gzip header is not retained, so applications that need that
+  information should instead use raw inflate, see inflateInit2() below, or
+  inflateBack() and perform their own processing of the gzip header and
+  trailer.
+
+    inflate() returns Z_OK if some progress has been made (more input processed
+  or more output produced), Z_STREAM_END if the end of the compressed data has
+  been reached and all uncompressed output has been produced, Z_NEED_DICT if a
+  preset dictionary is needed at this point, Z_DATA_ERROR if the input data was
+  corrupted (input stream not conforming to the zlib format or incorrect check
+  value), Z_STREAM_ERROR if the stream structure was inconsistent (for example
+  if next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
+  Z_BUF_ERROR if no progress is possible or if there was not enough room in the
+  output buffer when Z_FINISH is used. Note that Z_BUF_ERROR is not fatal, and
+  inflate() can be called again with more input and more output space to
+  continue decompressing. If Z_DATA_ERROR is returned, the application may then
+  call inflateSync() to look for a good compression block if a partial recovery
+  of the data is desired.
+*/
+
+
+ZEXTERN int ZEXPORT inflateEnd OF((z_streamp strm));
+/*
+     All dynamically allocated data structures for this stream are freed.
+   This function discards any unprocessed input and does not flush any
+   pending output.
+
+     inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
+   was inconsistent. In the error case, msg may be set but then points to a
+   static string (which must not be deallocated).
+*/
+
+                        /* Advanced functions */
+
+/*
+    The following functions are needed only in some special applications.
+*/
+
+/*
+ZEXTERN int ZEXPORT deflateInit2 OF((z_streamp strm,
+                                     int  level,
+                                     int  method,
+                                     int  windowBits,
+                                     int  memLevel,
+                                     int  strategy));
+
+     This is another version of deflateInit with more compression options. The
+   fields next_in, zalloc, zfree and opaque must be initialized before by
+   the caller.
+
+     The method parameter is the compression method. It must be Z_DEFLATED in
+   this version of the library.
+
+     The windowBits parameter is the base two logarithm of the window size
+   (the size of the history buffer). It should be in the range 8..15 for this
+   version of the library. Larger values of this parameter result in better
+   compression at the expense of memory usage. The default value is 15 if
+   deflateInit is used instead.
+
+     windowBits can also be -8..-15 for raw deflate. In this case, -windowBits
+   determines the window size. deflate() will then generate raw deflate data
+   with no zlib header or trailer, and will not compute an adler32 check value.
+
+     windowBits can also be greater than 15 for optional gzip encoding. Add
+   16 to windowBits to write a simple gzip header and trailer around the
+   compressed data instead of a zlib wrapper. The gzip header will have no
+   file name, no extra data, no comment, no modification time (set to zero),
+   no header crc, and the operating system will be set to 255 (unknown).  If a
+   gzip stream is being written, strm->adler is a crc32 instead of an adler32.
+
+     The memLevel parameter specifies how much memory should be allocated
+   for the internal compression state. memLevel=1 uses minimum memory but
+   is slow and reduces compression ratio; memLevel=9 uses maximum memory
+   for optimal speed. The default value is 8. See zconf.h for total memory
+   usage as a function of windowBits and memLevel.
+
+     The strategy parameter is used to tune the compression algorithm. Use the
+   value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
+   filter (or predictor), Z_HUFFMAN_ONLY to force Huffman encoding only (no
+   string match), or Z_RLE to limit match distances to one (run-length
+   encoding). Filtered data consists mostly of small values with a somewhat
+   random distribution. In this case, the compression algorithm is tuned to
+   compress them better. The effect of Z_FILTERED is to force more Huffman
+   coding and less string matching; it is somewhat intermediate between
+   Z_DEFAULT and Z_HUFFMAN_ONLY. Z_RLE is designed to be almost as fast as
+   Z_HUFFMAN_ONLY, but give better compression for PNG image data. The strategy
+   parameter only affects the compression ratio but not the correctness of the
+   compressed output even if it is not set appropriately.  Z_FIXED prevents the
+   use of dynamic Huffman codes, allowing for a simpler decoder for special
+   applications.
+
+      deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+   memory, Z_STREAM_ERROR if a parameter is invalid (such as an invalid
+   method). msg is set to null if there is no error message.  deflateInit2 does
+   not perform any compression: this will be done by deflate().
+*/
+
+ZEXTERN int ZEXPORT deflateSetDictionary OF((z_streamp strm,
+                                             const Bytef *dictionary,
+                                             uInt  dictLength));
+/*
+     Initializes the compression dictionary from the given byte sequence
+   without producing any compressed output. This function must be called
+   immediately after deflateInit, deflateInit2 or deflateReset, before any
+   call of deflate. The compressor and decompressor must use exactly the same
+   dictionary (see inflateSetDictionary).
+
+     The dictionary should consist of strings (byte sequences) that are likely
+   to be encountered later in the data to be compressed, with the most commonly
+   used strings preferably put towards the end of the dictionary. Using a
+   dictionary is most useful when the data to be compressed is short and can be
+   predicted with good accuracy; the data can then be compressed better than
+   with the default empty dictionary.
+
+     Depending on the size of the compression data structures selected by
+   deflateInit or deflateInit2, a part of the dictionary may in effect be
+   discarded, for example if the dictionary is larger than the window size in
+   deflate or deflate2. Thus the strings most likely to be useful should be
+   put at the end of the dictionary, not at the front. In addition, the
+   current implementation of deflate will use at most the window size minus
+   262 bytes of the provided dictionary.
+
+     Upon return of this function, strm->adler is set to the adler32 value
+   of the dictionary; the decompressor may later use this value to determine
+   which dictionary has been used by the compressor. (The adler32 value
+   applies to the whole dictionary even if only a subset of the dictionary is
+   actually used by the compressor.) If a raw deflate was requested, then the
+   adler32 value is not computed and strm->adler is not set.
+
+     deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
+   parameter is invalid (such as NULL dictionary) or the stream state is
+   inconsistent (for example if deflate has already been called for this stream
+   or if the compression method is bsort). deflateSetDictionary does not
+   perform any compression: this will be done by deflate().
+*/
+
+ZEXTERN int ZEXPORT deflateCopy OF((z_streamp dest,
+                                    z_streamp source));
+/*
+     Sets the destination stream as a complete copy of the source stream.
+
+     This function can be useful when several compression strategies will be
+   tried, for example when there are several ways of pre-processing the input
+   data with a filter. The streams that will be discarded should then be freed
+   by calling deflateEnd.  Note that deflateCopy duplicates the internal
+   compression state which can be quite large, so this strategy is slow and
+   can consume lots of memory.
+
+     deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
+   (such as zalloc being NULL). msg is left unchanged in both source and
+   destination.
+*/
+
+ZEXTERN int ZEXPORT deflateReset OF((z_streamp strm));
+/*
+     This function is equivalent to deflateEnd followed by deflateInit,
+   but does not free and reallocate all the internal compression state.
+   The stream will keep the same compression level and any other attributes
+   that may have been set by deflateInit2.
+
+      deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being NULL).
+*/
+
+ZEXTERN int ZEXPORT deflateParams OF((z_streamp strm,
+                                      int level,
+                                      int strategy));
+/*
+     Dynamically update the compression level and compression strategy.  The
+   interpretation of level and strategy is as in deflateInit2.  This can be
+   used to switch between compression and straight copy of the input data, or
+   to switch to a different kind of input data requiring a different
+   strategy. If the compression level is changed, the input available so far
+   is compressed with the old level (and may be flushed); the new level will
+   take effect only at the next call of deflate().
+
+     Before the call of deflateParams, the stream state must be set as for
+   a call of deflate(), since the currently available input may have to
+   be compressed and flushed. In particular, strm->avail_out must be non-zero.
+
+     deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
+   stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
+   if strm->avail_out was zero.
+*/
+
+ZEXTERN int ZEXPORT deflateTune OF((z_streamp strm,
+                                    int good_length,
+                                    int max_lazy,
+                                    int nice_length,
+                                    int max_chain));
+/*
+     Fine tune deflate's internal compression parameters.  This should only be
+   used by someone who understands the algorithm used by zlib's deflate for
+   searching for the best matching string, and even then only by the most
+   fanatic optimizer trying to squeeze out the last compressed bit for their
+   specific input data.  Read the deflate.c source code for the meaning of the
+   max_lazy, good_length, nice_length, and max_chain parameters.
+
+     deflateTune() can be called after deflateInit() or deflateInit2(), and
+   returns Z_OK on success, or Z_STREAM_ERROR for an invalid deflate stream.
+ */
+
+ZEXTERN uLong ZEXPORT deflateBound OF((z_streamp strm,
+                                       uLong sourceLen));
+/*
+     deflateBound() returns an upper bound on the compressed size after
+   deflation of sourceLen bytes.  It must be called after deflateInit()
+   or deflateInit2().  This would be used to allocate an output buffer
+   for deflation in a single pass, and so would be called before deflate().
+*/
+
+ZEXTERN int ZEXPORT deflatePrime OF((z_streamp strm,
+                                     int bits,
+                                     int value));
+/*
+     deflatePrime() inserts bits in the deflate output stream.  The intent
+  is that this function is used to start off the deflate output with the
+  bits leftover from a previous deflate stream when appending to it.  As such,
+  this function can only be used for raw deflate, and must be used before the
+  first deflate() call after a deflateInit2() or deflateReset().  bits must be
+  less than or equal to 16, and that many of the least significant bits of
+  value will be inserted in the output.
+
+      deflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent.
+*/
+
+ZEXTERN int ZEXPORT deflateSetHeader OF((z_streamp strm,
+                                         gz_headerp head));
+/*
+      deflateSetHeader() provides gzip header information for when a gzip
+   stream is requested by deflateInit2().  deflateSetHeader() may be called
+   after deflateInit2() or deflateReset() and before the first call of
+   deflate().  The text, time, os, extra field, name, and comment information
+   in the provided gz_header structure are written to the gzip header (xflag is
+   ignored -- the extra flags are set according to the compression level).  The
+   caller must assure that, if not Z_NULL, name and comment are terminated with
+   a zero byte, and that if extra is not Z_NULL, that extra_len bytes are
+   available there.  If hcrc is true, a gzip header crc is included.  Note that
+   the current versions of the command-line version of gzip (up through version
+   1.3.x) do not support header crc's, and will report that it is a "multi-part
+   gzip file" and give up.
+
+      If deflateSetHeader is not used, the default gzip header has text false,
+   the time set to zero, and os set to 255, with no extra, name, or comment
+   fields.  The gzip header is returned to the default state by deflateReset().
+
+      deflateSetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent.
+*/
+
+/*
+ZEXTERN int ZEXPORT inflateInit2 OF((z_streamp strm,
+                                     int  windowBits));
+
+     This is another version of inflateInit with an extra parameter. The
+   fields next_in, avail_in, zalloc, zfree and opaque must be initialized
+   before by the caller.
+
+     The windowBits parameter is the base two logarithm of the maximum window
+   size (the size of the history buffer).  It should be in the range 8..15 for
+   this version of the library. The default value is 15 if inflateInit is used
+   instead. windowBits must be greater than or equal to the windowBits value
+   provided to deflateInit2() while compressing, or it must be equal to 15 if
+   deflateInit2() was not used. If a compressed stream with a larger window
+   size is given as input, inflate() will return with the error code
+   Z_DATA_ERROR instead of trying to allocate a larger window.
+
+     windowBits can also be -8..-15 for raw inflate. In this case, -windowBits
+   determines the window size. inflate() will then process raw deflate data,
+   not looking for a zlib or gzip header, not generating a check value, and not
+   looking for any check values for comparison at the end of the stream. This
+   is for use with other formats that use the deflate compressed data format
+   such as zip.  Those formats provide their own check values. If a custom
+   format is developed using the raw deflate format for compressed data, it is
+   recommended that a check value such as an adler32 or a crc32 be applied to
+   the uncompressed data as is done in the zlib, gzip, and zip formats.  For
+   most applications, the zlib format should be used as is. Note that comments
+   above on the use in deflateInit2() applies to the magnitude of windowBits.
+
+     windowBits can also be greater than 15 for optional gzip decoding. Add
+   32 to windowBits to enable zlib and gzip decoding with automatic header
+   detection, or add 16 to decode only the gzip format (the zlib format will
+   return a Z_DATA_ERROR).  If a gzip stream is being decoded, strm->adler is
+   a crc32 instead of an adler32.
+
+     inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+   memory, Z_STREAM_ERROR if a parameter is invalid (such as a null strm). msg
+   is set to null if there is no error message.  inflateInit2 does not perform
+   any decompression apart from reading the zlib header if present: this will
+   be done by inflate(). (So next_in and avail_in may be modified, but next_out
+   and avail_out are unchanged.)
+*/
+
+ZEXTERN int ZEXPORT inflateSetDictionary OF((z_streamp strm,
+                                             const Bytef *dictionary,
+                                             uInt  dictLength));
+/*
+     Initializes the decompression dictionary from the given uncompressed byte
+   sequence. This function must be called immediately after a call of inflate,
+   if that call returned Z_NEED_DICT. The dictionary chosen by the compressor
+   can be determined from the adler32 value returned by that call of inflate.
+   The compressor and decompressor must use exactly the same dictionary (see
+   deflateSetDictionary).  For raw inflate, this function can be called
+   immediately after inflateInit2() or inflateReset() and before any call of
+   inflate() to set the dictionary.  The application must insure that the
+   dictionary that was used for compression is provided.
+
+     inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
+   parameter is invalid (such as NULL dictionary) or the stream state is
+   inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
+   expected one (incorrect adler32 value). inflateSetDictionary does not
+   perform any decompression: this will be done by subsequent calls of
+   inflate().
+*/
+
+ZEXTERN int ZEXPORT inflateSync OF((z_streamp strm));
+/*
+    Skips invalid compressed data until a full flush point (see above the
+  description of deflate with Z_FULL_FLUSH) can be found, or until all
+  available input is skipped. No output is provided.
+
+    inflateSync returns Z_OK if a full flush point has been found, Z_BUF_ERROR
+  if no more input was provided, Z_DATA_ERROR if no flush point has been found,
+  or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
+  case, the application may save the current current value of total_in which
+  indicates where valid compressed data was found. In the error case, the
+  application may repeatedly call inflateSync, providing more input each time,
+  until success or end of the input data.
+*/
+
+ZEXTERN int ZEXPORT inflateCopy OF((z_streamp dest,
+                                    z_streamp source));
+/*
+     Sets the destination stream as a complete copy of the source stream.
+
+     This function can be useful when randomly accessing a large stream.  The
+   first pass through the stream can periodically record the inflate state,
+   allowing restarting inflate at those points when randomly accessing the
+   stream.
+
+     inflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
+   (such as zalloc being NULL). msg is left unchanged in both source and
+   destination.
+*/
+
+ZEXTERN int ZEXPORT inflateReset OF((z_streamp strm));
+/*
+     This function is equivalent to inflateEnd followed by inflateInit,
+   but does not free and reallocate all the internal decompression state.
+   The stream will keep attributes that may have been set by inflateInit2.
+
+      inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being NULL).
+*/
+
+ZEXTERN int ZEXPORT inflatePrime OF((z_streamp strm,
+                                     int bits,
+                                     int value));
+/*
+     This function inserts bits in the inflate input stream.  The intent is
+  that this function is used to start inflating at a bit position in the
+  middle of a byte.  The provided bits will be used before any bytes are used
+  from next_in.  This function should only be used with raw inflate, and
+  should be used before the first inflate() call after inflateInit2() or
+  inflateReset().  bits must be less than or equal to 16, and that many of the
+  least significant bits of value will be inserted in the input.
+
+      inflatePrime returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent.
+*/
+
+ZEXTERN int ZEXPORT inflateGetHeader OF((z_streamp strm,
+                                         gz_headerp head));
+/*
+      inflateGetHeader() requests that gzip header information be stored in the
+   provided gz_header structure.  inflateGetHeader() may be called after
+   inflateInit2() or inflateReset(), and before the first call of inflate().
+   As inflate() processes the gzip stream, head->done is zero until the header
+   is completed, at which time head->done is set to one.  If a zlib stream is
+   being decoded, then head->done is set to -1 to indicate that there will be
+   no gzip header information forthcoming.  Note that Z_BLOCK can be used to
+   force inflate() to return immediately after header processing is complete
+   and before any actual data is decompressed.
+
+      The text, time, xflags, and os fields are filled in with the gzip header
+   contents.  hcrc is set to true if there is a header CRC.  (The header CRC
+   was valid if done is set to one.)  If extra is not Z_NULL, then extra_max
+   contains the maximum number of bytes to write to extra.  Once done is true,
+   extra_len contains the actual extra field length, and extra contains the
+   extra field, or that field truncated if extra_max is less than extra_len.
+   If name is not Z_NULL, then up to name_max characters are written there,
+   terminated with a zero unless the length is greater than name_max.  If
+   comment is not Z_NULL, then up to comm_max characters are written there,
+   terminated with a zero unless the length is greater than comm_max.  When
+   any of extra, name, or comment are not Z_NULL and the respective field is
+   not present in the header, then that field is set to Z_NULL to signal its
+   absence.  This allows the use of deflateSetHeader() with the returned
+   structure to duplicate the header.  However if those fields are set to
+   allocated memory, then the application will need to save those pointers
+   elsewhere so that they can be eventually freed.
+
+      If inflateGetHeader is not used, then the header information is simply
+   discarded.  The header is always checked for validity, including the header
+   CRC if present.  inflateReset() will reset the process to discard the header
+   information.  The application would need to call inflateGetHeader() again to
+   retrieve the header from the next gzip stream.
+
+      inflateGetHeader returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent.
+*/
+
+/*
+ZEXTERN int ZEXPORT inflateBackInit OF((z_streamp strm, int windowBits,
+                                        unsigned char FAR *window));
+
+     Initialize the internal stream state for decompression using inflateBack()
+   calls.  The fields zalloc, zfree and opaque in strm must be initialized
+   before the call.  If zalloc and zfree are Z_NULL, then the default library-
+   derived memory allocation routines are used.  windowBits is the base two
+   logarithm of the window size, in the range 8..15.  window is a caller
+   supplied buffer of that size.  Except for special applications where it is
+   assured that deflate was used with small window sizes, windowBits must be 15
+   and a 32K byte window must be supplied to be able to decompress general
+   deflate streams.
+
+     See inflateBack() for the usage of these routines.
+
+     inflateBackInit will return Z_OK on success, Z_STREAM_ERROR if any of
+   the paramaters are invalid, Z_MEM_ERROR if the internal state could not
+   be allocated, or Z_VERSION_ERROR if the version of the library does not
+   match the version of the header file.
+*/
+
+typedef unsigned (*in_func) OF((void FAR *, unsigned char FAR * FAR *));
+typedef int (*out_func) OF((void FAR *, unsigned char FAR *, unsigned));
+
+ZEXTERN int ZEXPORT inflateBack OF((z_streamp strm,
+                                    in_func in, void FAR *in_desc,
+                                    out_func out, void FAR *out_desc));
+/*
+     inflateBack() does a raw inflate with a single call using a call-back
+   interface for input and output.  This is more efficient than inflate() for
+   file i/o applications in that it avoids copying between the output and the
+   sliding window by simply making the window itself the output buffer.  This
+   function trusts the application to not change the output buffer passed by
+   the output function, at least until inflateBack() returns.
+
+     inflateBackInit() must be called first to allocate the internal state
+   and to initialize the state with the user-provided window buffer.
+   inflateBack() may then be used multiple times to inflate a complete, raw
+   deflate stream with each call.  inflateBackEnd() is then called to free
+   the allocated state.
+
+     A raw deflate stream is one with no zlib or gzip header or trailer.
+   This routine would normally be used in a utility that reads zip or gzip
+   files and writes out uncompressed files.  The utility would decode the
+   header and process the trailer on its own, hence this routine expects
+   only the raw deflate stream to decompress.  This is different from the
+   normal behavior of inflate(), which expects either a zlib or gzip header and
+   trailer around the deflate stream.
+
+     inflateBack() uses two subroutines supplied by the caller that are then
+   called by inflateBack() for input and output.  inflateBack() calls those
+   routines until it reads a complete deflate stream and writes out all of the
+   uncompressed data, or until it encounters an error.  The function's
+   parameters and return types are defined above in the in_func and out_func
+   typedefs.  inflateBack() will call in(in_desc, &buf) which should return the
+   number of bytes of provided input, and a pointer to that input in buf.  If
+   there is no input available, in() must return zero--buf is ignored in that
+   case--and inflateBack() will return a buffer error.  inflateBack() will call
+   out(out_desc, buf, len) to write the uncompressed data buf[0..len-1].  out()
+   should return zero on success, or non-zero on failure.  If out() returns
+   non-zero, inflateBack() will return with an error.  Neither in() nor out()
+   are permitted to change the contents of the window provided to
+   inflateBackInit(), which is also the buffer that out() uses to write from.
+   The length written by out() will be at most the window size.  Any non-zero
+   amount of input may be provided by in().
+
+     For convenience, inflateBack() can be provided input on the first call by
+   setting strm->next_in and strm->avail_in.  If that input is exhausted, then
+   in() will be called.  Therefore strm->next_in must be initialized before
+   calling inflateBack().  If strm->next_in is Z_NULL, then in() will be called
+   immediately for input.  If strm->next_in is not Z_NULL, then strm->avail_in
+   must also be initialized, and then if strm->avail_in is not zero, input will
+   initially be taken from strm->next_in[0 .. strm->avail_in - 1].
+
+     The in_desc and out_desc parameters of inflateBack() is passed as the
+   first parameter of in() and out() respectively when they are called.  These
+   descriptors can be optionally used to pass any information that the caller-
+   supplied in() and out() functions need to do their job.
+
+     On return, inflateBack() will set strm->next_in and strm->avail_in to
+   pass back any unused input that was provided by the last in() call.  The
+   return values of inflateBack() can be Z_STREAM_END on success, Z_BUF_ERROR
+   if in() or out() returned an error, Z_DATA_ERROR if there was a format
+   error in the deflate stream (in which case strm->msg is set to indicate the
+   nature of the error), or Z_STREAM_ERROR if the stream was not properly
+   initialized.  In the case of Z_BUF_ERROR, an input or output error can be
+   distinguished using strm->next_in which will be Z_NULL only if in() returned
+   an error.  If strm->next is not Z_NULL, then the Z_BUF_ERROR was due to
+   out() returning non-zero.  (in() will always be called before out(), so
+   strm->next_in is assured to be defined if out() returns non-zero.)  Note
+   that inflateBack() cannot return Z_OK.
+*/
+
+ZEXTERN int ZEXPORT inflateBackEnd OF((z_streamp strm));
+/*
+     All memory allocated by inflateBackInit() is freed.
+
+     inflateBackEnd() returns Z_OK on success, or Z_STREAM_ERROR if the stream
+   state was inconsistent.
+*/
+
+ZEXTERN uLong ZEXPORT zlibCompileFlags OF((void));
+/* Return flags indicating compile-time options.
+
+    Type sizes, two bits each, 00 = 16 bits, 01 = 32, 10 = 64, 11 = other:
+     1.0: size of uInt
+     3.2: size of uLong
+     5.4: size of voidpf (pointer)
+     7.6: size of z_off_t
+
+    Compiler, assembler, and debug options:
+     8: DEBUG
+     9: ASMV or ASMINF -- use ASM code
+     10: ZLIB_WINAPI -- exported functions use the WINAPI calling convention
+     11: 0 (reserved)
+
+    One-time table building (smaller code, but not thread-safe if true):
+     12: BUILDFIXED -- build static block decoding tables when needed
+     13: DYNAMIC_CRC_TABLE -- build CRC calculation tables when needed
+     14,15: 0 (reserved)
+
+    Library content (indicates missing functionality):
+     16: NO_GZCOMPRESS -- gz* functions cannot compress (to avoid linking
+                          deflate code when not needed)
+     17: NO_GZIP -- deflate can't write gzip streams, and inflate can't detect
+                    and decode gzip streams (to avoid linking crc code)
+     18-19: 0 (reserved)
+
+    Operation variations (changes in library functionality):
+     20: PKZIP_BUG_WORKAROUND -- slightly more permissive inflate
+     21: FASTEST -- deflate algorithm with only one, lowest compression level
+     22,23: 0 (reserved)
+
+    The sprintf variant used by gzprintf (zero is best):
+     24: 0 = vs*, 1 = s* -- 1 means limited to 20 arguments after the format
+     25: 0 = *nprintf, 1 = *printf -- 1 means gzprintf() not secure!
+     26: 0 = returns value, 1 = void -- 1 means inferred string length returned
+
+    Remainder:
+     27-31: 0 (reserved)
+ */
+
+
+                        /* utility functions */
+
+/*
+     The following utility functions are implemented on top of the
+   basic stream-oriented functions. To simplify the interface, some
+   default options are assumed (compression level and memory usage,
+   standard memory allocation functions). The source code of these
+   utility functions can easily be modified if you need special options.
+*/
+
+ZEXTERN int ZEXPORT compress OF((Bytef *dest,   uLongf *destLen,
+                                 const Bytef *source, uLong sourceLen));
+/*
+     Compresses the source buffer into the destination buffer.  sourceLen is
+   the byte length of the source buffer. Upon entry, destLen is the total
+   size of the destination buffer, which must be at least the value returned
+   by compressBound(sourceLen). Upon exit, destLen is the actual size of the
+   compressed buffer.
+     This function can be used to compress a whole file at once if the
+   input file is mmap'ed.
+     compress returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_BUF_ERROR if there was not enough room in the output
+   buffer.
+*/
+
+ZEXTERN int ZEXPORT compress2 OF((Bytef *dest,   uLongf *destLen,
+                                  const Bytef *source, uLong sourceLen,
+                                  int level));
+/*
+     Compresses the source buffer into the destination buffer. The level
+   parameter has the same meaning as in deflateInit.  sourceLen is the byte
+   length of the source buffer. Upon entry, destLen is the total size of the
+   destination buffer, which must be at least the value returned by
+   compressBound(sourceLen). Upon exit, destLen is the actual size of the
+   compressed buffer.
+
+     compress2 returns Z_OK if success, Z_MEM_ERROR if there was not enough
+   memory, Z_BUF_ERROR if there was not enough room in the output buffer,
+   Z_STREAM_ERROR if the level parameter is invalid.
+*/
+
+ZEXTERN uLong ZEXPORT compressBound OF((uLong sourceLen));
+/*
+     compressBound() returns an upper bound on the compressed size after
+   compress() or compress2() on sourceLen bytes.  It would be used before
+   a compress() or compress2() call to allocate the destination buffer.
+*/
+
+ZEXTERN int ZEXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
+                                   const Bytef *source, uLong sourceLen));
+/*
+     Decompresses the source buffer into the destination buffer.  sourceLen is
+   the byte length of the source buffer. Upon entry, destLen is the total
+   size of the destination buffer, which must be large enough to hold the
+   entire uncompressed data. (The size of the uncompressed data must have
+   been saved previously by the compressor and transmitted to the decompressor
+   by some mechanism outside the scope of this compression library.)
+   Upon exit, destLen is the actual size of the compressed buffer.
+     This function can be used to decompress a whole file at once if the
+   input file is mmap'ed.
+
+     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_BUF_ERROR if there was not enough room in the output
+   buffer, or Z_DATA_ERROR if the input data was corrupted or incomplete.
+*/
+
+
+typedef voidp gzFile;
+
+ZEXTERN gzFile ZEXPORT gzopen  OF((const char *path, const char *mode));
+/*
+     Opens a gzip (.gz) file for reading or writing. The mode parameter
+   is as in fopen ("rb" or "wb") but can also include a compression level
+   ("wb9") or a strategy: 'f' for filtered data as in "wb6f", 'h' for
+   Huffman only compression as in "wb1h", or 'R' for run-length encoding
+   as in "wb1R". (See the description of deflateInit2 for more information
+   about the strategy parameter.)
+
+     gzopen can be used to read a file which is not in gzip format; in this
+   case gzread will directly read from the file without decompression.
+
+     gzopen returns NULL if the file could not be opened or if there was
+   insufficient memory to allocate the (de)compression state; errno
+   can be checked to distinguish the two cases (if errno is zero, the
+   zlib error is Z_MEM_ERROR).  */
+
+ZEXTERN gzFile ZEXPORT gzdopen  OF((int fd, const char *mode));
+/*
+     gzdopen() associates a gzFile with the file descriptor fd.  File
+   descriptors are obtained from calls like open, dup, creat, pipe or
+   fileno (in the file has been previously opened with fopen).
+   The mode parameter is as in gzopen.
+     The next call of gzclose on the returned gzFile will also close the
+   file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
+   descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
+     gzdopen returns NULL if there was insufficient memory to allocate
+   the (de)compression state.
+*/
+
+ZEXTERN int ZEXPORT gzsetparams OF((gzFile file, int level, int strategy));
+/*
+     Dynamically update the compression level or strategy. See the description
+   of deflateInit2 for the meaning of these parameters.
+     gzsetparams returns Z_OK if success, or Z_STREAM_ERROR if the file was not
+   opened for writing.
+*/
+
+ZEXTERN int ZEXPORT    gzread  OF((gzFile file, voidp buf, unsigned len));
+/*
+     Reads the given number of uncompressed bytes from the compressed file.
+   If the input file was not in gzip format, gzread copies the given number
+   of bytes into the buffer.
+     gzread returns the number of uncompressed bytes actually read (0 for
+   end of file, -1 for error). */
+
+ZEXTERN int ZEXPORT    gzwrite OF((gzFile file,
+                                   voidpc buf, unsigned len));
+/*
+     Writes the given number of uncompressed bytes into the compressed file.
+   gzwrite returns the number of uncompressed bytes actually written
+   (0 in case of error).
+*/
+
+ZEXTERN int ZEXPORTVA   gzprintf OF((gzFile file, const char *format, ...));
+/*
+     Converts, formats, and writes the args to the compressed file under
+   control of the format string, as in fprintf. gzprintf returns the number of
+   uncompressed bytes actually written (0 in case of error).  The number of
+   uncompressed bytes written is limited to 4095. The caller should assure that
+   this limit is not exceeded. If it is exceeded, then gzprintf() will return
+   return an error (0) with nothing written. In this case, there may also be a
+   buffer overflow with unpredictable consequences, which is possible only if
+   zlib was compiled with the insecure functions sprintf() or vsprintf()
+   because the secure snprintf() or vsnprintf() functions were not available.
+*/
+
+ZEXTERN int ZEXPORT gzputs OF((gzFile file, const char *s));
+/*
+      Writes the given null-terminated string to the compressed file, excluding
+   the terminating null character.
+      gzputs returns the number of characters written, or -1 in case of error.
+*/
+
+ZEXTERN char * ZEXPORT gzgets OF((gzFile file, char *buf, int len));
+/*
+      Reads bytes from the compressed file until len-1 characters are read, or
+   a newline character is read and transferred to buf, or an end-of-file
+   condition is encountered.  The string is then terminated with a null
+   character.
+      gzgets returns buf, or Z_NULL in case of error.
+*/
+
+ZEXTERN int ZEXPORT    gzputc OF((gzFile file, int c));
+/*
+      Writes c, converted to an unsigned char, into the compressed file.
+   gzputc returns the value that was written, or -1 in case of error.
+*/
+
+ZEXTERN int ZEXPORT    gzgetc OF((gzFile file));
+/*
+      Reads one byte from the compressed file. gzgetc returns this byte
+   or -1 in case of end of file or error.
+*/
+
+ZEXTERN int ZEXPORT    gzungetc OF((int c, gzFile file));
+/*
+      Push one character back onto the stream to be read again later.
+   Only one character of push-back is allowed.  gzungetc() returns the
+   character pushed, or -1 on failure.  gzungetc() will fail if a
+   character has been pushed but not read yet, or if c is -1. The pushed
+   character will be discarded if the stream is repositioned with gzseek()
+   or gzrewind().
+*/
+
+ZEXTERN int ZEXPORT    gzflush OF((gzFile file, int flush));
+/*
+     Flushes all pending output into the compressed file. The parameter
+   flush is as in the deflate() function. The return value is the zlib
+   error number (see function gzerror below). gzflush returns Z_OK if
+   the flush parameter is Z_FINISH and all output could be flushed.
+     gzflush should be called only when strictly necessary because it can
+   degrade compression.
+*/
+
+ZEXTERN z_off_t ZEXPORT    gzseek OF((gzFile file,
+                                      z_off_t offset, int whence));
+/*
+      Sets the starting position for the next gzread or gzwrite on the
+   given compressed file. The offset represents a number of bytes in the
+   uncompressed data stream. The whence parameter is defined as in lseek(2);
+   the value SEEK_END is not supported.
+     If the file is opened for reading, this function is emulated but can be
+   extremely slow. If the file is opened for writing, only forward seeks are
+   supported; gzseek then compresses a sequence of zeroes up to the new
+   starting position.
+
+      gzseek returns the resulting offset location as measured in bytes from
+   the beginning of the uncompressed stream, or -1 in case of error, in
+   particular if the file is opened for writing and the new starting position
+   would be before the current position.
+*/
+
+ZEXTERN int ZEXPORT    gzrewind OF((gzFile file));
+/*
+     Rewinds the given file. This function is supported only for reading.
+
+   gzrewind(file) is equivalent to (int)gzseek(file, 0L, SEEK_SET)
+*/
+
+ZEXTERN z_off_t ZEXPORT    gztell OF((gzFile file));
+/*
+     Returns the starting position for the next gzread or gzwrite on the
+   given compressed file. This position represents a number of bytes in the
+   uncompressed data stream.
+
+   gztell(file) is equivalent to gzseek(file, 0L, SEEK_CUR)
+*/
+
+ZEXTERN int ZEXPORT gzeof OF((gzFile file));
+/*
+     Returns 1 when EOF has previously been detected reading the given
+   input stream, otherwise zero.
+*/
+
+ZEXTERN int ZEXPORT gzdirect OF((gzFile file));
+/*
+     Returns 1 if file is being read directly without decompression, otherwise
+   zero.
+*/
+
+ZEXTERN int ZEXPORT    gzclose OF((gzFile file));
+/*
+     Flushes all pending output if necessary, closes the compressed file
+   and deallocates all the (de)compression state. The return value is the zlib
+   error number (see function gzerror below).
+*/
+
+ZEXTERN const char * ZEXPORT gzerror OF((gzFile file, int *errnum));
+/*
+     Returns the error message for the last error which occurred on the
+   given compressed file. errnum is set to zlib error number. If an
+   error occurred in the file system and not in the compression library,
+   errnum is set to Z_ERRNO and the application may consult errno
+   to get the exact error code.
+*/
+
+ZEXTERN void ZEXPORT gzclearerr OF((gzFile file));
+/*
+     Clears the error and end-of-file flags for file. This is analogous to the
+   clearerr() function in stdio. This is useful for continuing to read a gzip
+   file that is being written concurrently.
+*/
+
+                        /* checksum functions */
+
+/*
+     These functions are not related to compression but are exported
+   anyway because they might be useful in applications using the
+   compression library.
+*/
+
+ZEXTERN uLong ZEXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
+/*
+     Update a running Adler-32 checksum with the bytes buf[0..len-1] and
+   return the updated checksum. If buf is NULL, this function returns
+   the required initial value for the checksum.
+   An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
+   much faster. Usage example:
+
+     uLong adler = adler32(0L, Z_NULL, 0);
+
+     while (read_buffer(buffer, length) != EOF) {
+       adler = adler32(adler, buffer, length);
+     }
+     if (adler != original_adler) error();
+*/
+
+ZEXTERN uLong ZEXPORT adler32_combine OF((uLong adler1, uLong adler2,
+                                          z_off_t len2));
+/*
+     Combine two Adler-32 checksums into one.  For two sequences of bytes, seq1
+   and seq2 with lengths len1 and len2, Adler-32 checksums were calculated for
+   each, adler1 and adler2.  adler32_combine() returns the Adler-32 checksum of
+   seq1 and seq2 concatenated, requiring only adler1, adler2, and len2.
+*/
+
+ZEXTERN uLong ZEXPORT crc32   OF((uLong crc, const Bytef *buf, uInt len));
+/*
+     Update a running CRC-32 with the bytes buf[0..len-1] and return the
+   updated CRC-32. If buf is NULL, this function returns the required initial
+   value for the for the crc. Pre- and post-conditioning (one's complement) is
+   performed within this function so it shouldn't be done by the application.
+   Usage example:
+
+     uLong crc = crc32(0L, Z_NULL, 0);
+
+     while (read_buffer(buffer, length) != EOF) {
+       crc = crc32(crc, buffer, length);
+     }
+     if (crc != original_crc) error();
+*/
+
+ZEXTERN uLong ZEXPORT crc32_combine OF((uLong crc1, uLong crc2, z_off_t len2));
+
+/*
+     Combine two CRC-32 check values into one.  For two sequences of bytes,
+   seq1 and seq2 with lengths len1 and len2, CRC-32 check values were
+   calculated for each, crc1 and crc2.  crc32_combine() returns the CRC-32
+   check value of seq1 and seq2 concatenated, requiring only crc1, crc2, and
+   len2.
+*/
+
+
+                        /* various hacks, don't look :) */
+
+/* deflateInit and inflateInit are macros to allow checking the zlib version
+ * and the compiler's view of z_stream:
+ */
+ZEXTERN int ZEXPORT deflateInit_ OF((z_streamp strm, int level,
+                                     const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateInit_ OF((z_streamp strm,
+                                     const char *version, int stream_size));
+ZEXTERN int ZEXPORT deflateInit2_ OF((z_streamp strm, int  level, int  method,
+                                      int windowBits, int memLevel,
+                                      int strategy, const char *version,
+                                      int stream_size));
+ZEXTERN int ZEXPORT inflateInit2_ OF((z_streamp strm, int  windowBits,
+                                      const char *version, int stream_size));
+ZEXTERN int ZEXPORT inflateBackInit_ OF((z_streamp strm, int windowBits,
+                                         unsigned char FAR *window,
+                                         const char *version,
+                                         int stream_size));
+#define deflateInit(strm, level) \
+        deflateInit_((strm), (level),       ZLIB_VERSION, sizeof(z_stream))
+#define inflateInit(strm) \
+        inflateInit_((strm),                ZLIB_VERSION, sizeof(z_stream))
+#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
+        deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
+                      (strategy),           ZLIB_VERSION, sizeof(z_stream))
+#define inflateInit2(strm, windowBits) \
+        inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
+#define inflateBackInit(strm, windowBits, window) \
+        inflateBackInit_((strm), (windowBits), (window), \
+        ZLIB_VERSION, sizeof(z_stream))
+
+
+#if !defined(ZUTIL_H) && !defined(NO_DUMMY_DECL)
+    struct internal_state {int dummy;}; /* hack for buggy compilers */
+#endif
+
+ZEXTERN const char   * ZEXPORT zError           OF((int));
+ZEXTERN int            ZEXPORT inflateSyncPoint OF((z_streamp z));
+ZEXTERN const uLongf * ZEXPORT get_crc_table    OF((void));
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* ZLIB_H */