mirror of
https://github.com/veracrypt/VeraCrypt.git
synced 2025-11-11 11:08:02 -06:00
Windows vulnerability fix : finally make bootloader decompressor more robust and secure by adding multiple checks and validation code. This solves the issue found by the Open Crypt Audit project. Note that we had to switch to the slow implementation of the function decode in order to keep the size of the decompressor code under 2K.
This commit is contained in:
@@ -13,7 +13,7 @@
|
|||||||
#include "BootDefs.h"
|
#include "BootDefs.h"
|
||||||
|
|
||||||
// The user will be advised to upgrade the rescue disk if upgrading from the following or any previous version
|
// The user will be advised to upgrade the rescue disk if upgrading from the following or any previous version
|
||||||
#define TC_RESCUE_DISK_UPGRADE_NOTICE_MAX_VERSION 0x010a
|
#define TC_RESCUE_DISK_UPGRADE_NOTICE_MAX_VERSION 0x010e
|
||||||
|
|
||||||
#define TC_BOOT_LOADER_AREA_SIZE (TC_BOOT_LOADER_AREA_SECTOR_COUNT * TC_SECTOR_SIZE_BIOS)
|
#define TC_BOOT_LOADER_AREA_SIZE (TC_BOOT_LOADER_AREA_SECTOR_COUNT * TC_SECTOR_SIZE_BIOS)
|
||||||
|
|
||||||
|
|||||||
@@ -134,6 +134,8 @@ checksum_ok:
|
|||||||
push dx
|
push dx
|
||||||
|
|
||||||
; Decompress boot loader
|
; Decompress boot loader
|
||||||
|
mov cx, word ptr [start + TC_BOOT_SECTOR_LOADER_LENGTH_OFFSET]
|
||||||
|
push cx ; Compressed data size
|
||||||
push TC_BOOT_LOADER_COMPRESSED_BUFFER_OFFSET + TC_GZIP_HEADER_SIZE ; Compressed data
|
push TC_BOOT_LOADER_COMPRESSED_BUFFER_OFFSET + TC_GZIP_HEADER_SIZE ; Compressed data
|
||||||
push TC_MAX_BOOT_LOADER_DECOMPRESSED_SIZE ; Output buffer size
|
push TC_MAX_BOOT_LOADER_DECOMPRESSED_SIZE ; Output buffer size
|
||||||
push TC_BOOT_LOADER_DECOMPRESSOR_MEMORY_SIZE + TC_COM_EXECUTABLE_OFFSET ; Output buffer
|
push TC_BOOT_LOADER_DECOMPRESSOR_MEMORY_SIZE + TC_COM_EXECUTABLE_OFFSET ; Output buffer
|
||||||
@@ -145,7 +147,7 @@ checksum_ok:
|
|||||||
retf
|
retf
|
||||||
decompressor_ret:
|
decompressor_ret:
|
||||||
|
|
||||||
add sp, 6
|
add sp, 8
|
||||||
pop dx
|
pop dx
|
||||||
|
|
||||||
; Restore boot sector segment
|
; Restore boot sector segment
|
||||||
|
|||||||
@@ -1,410 +1,432 @@
|
|||||||
/*
|
/*
|
||||||
puff.c
|
puff.c
|
||||||
Copyright (C) 2002-2004 Mark Adler, all rights reserved
|
Copyright (C) 2002-2004 Mark Adler, all rights reserved
|
||||||
version 1.8, 9 Jan 2004
|
version 1.8, 9 Jan 2004
|
||||||
|
|
||||||
This software is provided 'as-is', without any express or implied
|
This software is provided 'as-is', without any express or implied
|
||||||
warranty. In no event will the author be held liable for any damages
|
warranty. In no event will the author be held liable for any damages
|
||||||
arising from the use of this software.
|
arising from the use of this software.
|
||||||
|
|
||||||
Permission is granted to anyone to use this software for any purpose,
|
Permission is granted to anyone to use this software for any purpose,
|
||||||
including commercial applications, and to alter it and redistribute it
|
including commercial applications, and to alter it and redistribute it
|
||||||
freely, subject to the following restrictions:
|
freely, subject to the following restrictions:
|
||||||
|
|
||||||
1. The origin of this software must not be misrepresented; you must not
|
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
|
claim that you wrote the original software. If you use this software
|
||||||
in a product, an acknowledgment in the product documentation would be
|
in a product, an acknowledgment in the product documentation would be
|
||||||
appreciated but is not required.
|
appreciated but is not required.
|
||||||
2. Altered source versions must be plainly marked as such, and must not be
|
2. Altered source versions must be plainly marked as such, and must not be
|
||||||
misrepresented as being the original software.
|
misrepresented as being the original software.
|
||||||
3. This notice may not be removed or altered from any source distribution.
|
3. This notice may not be removed or altered from any source distribution.
|
||||||
|
|
||||||
Mark Adler madler@alumni.caltech.edu
|
Mark Adler madler@alumni.caltech.edu
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Adapted for TrueCrypt */
|
/* Adapted for TrueCrypt */
|
||||||
|
|
||||||
|
|
||||||
#define local static /* for local function definitions */
|
#define local static /* for local function definitions */
|
||||||
#define NIL ((unsigned char *)0) /* for no output option */
|
#define NIL ((unsigned char *)0) /* for no output option */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Maximums for allocations and loops. It is not useful to change these --
|
* Maximums for allocations and loops. It is not useful to change these --
|
||||||
* they are fixed by the deflate format.
|
* they are fixed by the deflate format.
|
||||||
*/
|
*/
|
||||||
#define MAXBITS 15 /* maximum bits in a code */
|
#define MAXBITS 15 /* maximum bits in a code */
|
||||||
#define MAXLCODES 286 /* maximum number of literal/length codes */
|
#define MAXLCODES 286 /* maximum number of literal/length codes */
|
||||||
#define MAXDCODES 30 /* maximum number of distance codes */
|
#define MAXDCODES 30 /* maximum number of distance codes */
|
||||||
#define MAXCODES (MAXLCODES+MAXDCODES) /* maximum codes lengths to read */
|
#define MAXCODES (MAXLCODES+MAXDCODES) /* maximum codes lengths to read */
|
||||||
#define FIXLCODES 288 /* number of fixed literal/length codes */
|
#define FIXLCODES 288 /* number of fixed literal/length codes */
|
||||||
|
|
||||||
/* input and output state */
|
/* input and output state */
|
||||||
struct state {
|
struct state {
|
||||||
/* output state */
|
/* output state */
|
||||||
unsigned char *out; /* output buffer */
|
unsigned char *out; /* output buffer */
|
||||||
unsigned int outlen; /* available space at out */
|
unsigned int outlen; /* available space at out */
|
||||||
unsigned int outcnt; /* bytes written to out so far */
|
unsigned int outcnt; /* bytes written to out so far */
|
||||||
|
|
||||||
/* input state */
|
/* input state */
|
||||||
unsigned char *in; /* input buffer */
|
unsigned char *in; /* input buffer */
|
||||||
unsigned int incnt; /* bytes read so far */
|
unsigned int inlen; /* available input at in */
|
||||||
int bitbuf; /* bit buffer */
|
unsigned int incnt; /* bytes read so far */
|
||||||
int bitcnt; /* number of bits in bit buffer */
|
int bitbuf; /* bit buffer */
|
||||||
};
|
int bitcnt; /* number of bits in bit buffer */
|
||||||
|
};
|
||||||
|
|
||||||
local int bits(struct state *s, int need)
|
|
||||||
{
|
local int bits(struct state *s, int need)
|
||||||
long val; /* bit accumulator (can use up to 20 bits) */
|
{
|
||||||
|
long val; /* bit accumulator (can use up to 20 bits) */
|
||||||
/* load at least need bits into val */
|
|
||||||
val = s->bitbuf;
|
/* load at least need bits into val */
|
||||||
while (s->bitcnt < need) {
|
val = s->bitbuf;
|
||||||
val |= (long)(s->in[s->incnt++]) << s->bitcnt; /* load eight bits */
|
while (s->bitcnt < need) {
|
||||||
s->bitcnt += 8;
|
val |= (long)(s->in[s->incnt++]) << s->bitcnt; /* load eight bits */
|
||||||
}
|
s->bitcnt += 8;
|
||||||
|
}
|
||||||
/* drop need bits and update buffer, always zero to seven bits left */
|
|
||||||
s->bitbuf = (int)(val >> need);
|
/* drop need bits and update buffer, always zero to seven bits left */
|
||||||
s->bitcnt -= need;
|
s->bitbuf = (int)(val >> need);
|
||||||
|
s->bitcnt -= need;
|
||||||
/* return need bits, zeroing the bits above that */
|
|
||||||
return (int)(val & ((1L << need) - 1));
|
/* return need bits, zeroing the bits above that */
|
||||||
}
|
return (int)(val & ((1L << need) - 1));
|
||||||
|
}
|
||||||
|
|
||||||
local int stored(struct state *s)
|
|
||||||
{
|
local int stored(struct state *s)
|
||||||
unsigned len; /* length of stored block */
|
{
|
||||||
|
unsigned len; /* length of stored block */
|
||||||
/* discard leftover bits from current byte (assumes s->bitcnt < 8) */
|
|
||||||
s->bitbuf = 0;
|
/* discard leftover bits from current byte (assumes s->bitcnt < 8) */
|
||||||
s->bitcnt = 0;
|
s->bitbuf = 0;
|
||||||
|
s->bitcnt = 0;
|
||||||
/* get length and check against its one's complement */
|
|
||||||
len = s->in[s->incnt++];
|
if (s->incnt + 4 > s->inlen)
|
||||||
len |= s->in[s->incnt++] << 8;
|
return 2; /* not enough input */
|
||||||
if (s->in[s->incnt++] != (~len & 0xff) ||
|
|
||||||
s->in[s->incnt++] != ((~len >> 8) & 0xff))
|
/* get length and check against its one's complement */
|
||||||
return -2; /* didn't match complement! */
|
len = s->in[s->incnt++];
|
||||||
|
len |= s->in[s->incnt++] << 8;
|
||||||
/* copy len bytes from in to out */
|
if (s->in[s->incnt++] != (~len & 0xff) ||
|
||||||
if (s->out != NIL) {
|
s->in[s->incnt++] != ((~len >> 8) & 0xff))
|
||||||
if (s->outcnt + len > s->outlen)
|
return -2; /* didn't match complement! */
|
||||||
return 1; /* not enough output space */
|
|
||||||
while (len--)
|
if (s->incnt + len > s->inlen)
|
||||||
s->out[s->outcnt++] = s->in[s->incnt++];
|
return 2; /* not enough input */
|
||||||
}
|
|
||||||
else { /* just scanning */
|
/* copy len bytes from in to out */
|
||||||
s->outcnt += len;
|
if (s->out != NIL) {
|
||||||
s->incnt += len;
|
if (s->outcnt + len > s->outlen)
|
||||||
}
|
return 1; /* not enough output space */
|
||||||
|
while (len--)
|
||||||
/* done with a valid stored block */
|
s->out[s->outcnt++] = s->in[s->incnt++];
|
||||||
return 0;
|
}
|
||||||
}
|
else { /* just scanning */
|
||||||
|
s->outcnt += len;
|
||||||
|
s->incnt += len;
|
||||||
struct huffman {
|
}
|
||||||
short *count; /* number of symbols of each length */
|
|
||||||
short *symbol; /* canonically ordered symbols */
|
/* done with a valid stored block */
|
||||||
};
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef SLOW
|
|
||||||
local int decode(struct state *s, struct huffman *h)
|
struct huffman {
|
||||||
{
|
short *count; /* number of symbols of each length */
|
||||||
int len; /* current number of bits in code */
|
short *symbol; /* canonically ordered symbols */
|
||||||
int code; /* len bits being decoded */
|
};
|
||||||
int first; /* first code of length len */
|
|
||||||
int count; /* number of codes of length len */
|
/* reduce code size by using slow version of the decompressor */
|
||||||
int index; /* index of first code of length len in symbol table */
|
#define SLOW
|
||||||
|
|
||||||
code = first = index = 0;
|
#ifdef SLOW
|
||||||
for (len = 1; len <= MAXBITS; len++) {
|
local int decode(struct state *s, struct huffman *h)
|
||||||
code |= bits(s, 1); /* get next bit */
|
{
|
||||||
count = h->count[len];
|
int len; /* current number of bits in code */
|
||||||
if (code < first + count) /* if length len, return symbol */
|
int code; /* len bits being decoded */
|
||||||
return h->symbol[index + (code - first)];
|
int first; /* first code of length len */
|
||||||
index += count; /* else update for next length */
|
int count; /* number of codes of length len */
|
||||||
first += count;
|
int index; /* index of first code of length len in symbol table */
|
||||||
first <<= 1;
|
|
||||||
code <<= 1;
|
code = first = index = 0;
|
||||||
}
|
for (len = 1; len <= MAXBITS; len++) {
|
||||||
return -9; /* ran out of codes */
|
code |= bits(s, 1); /* get next bit */
|
||||||
}
|
count = h->count[len];
|
||||||
|
if (code < first + count) /* if length len, return symbol */
|
||||||
/*
|
return h->symbol[index + (code - first)];
|
||||||
* A faster version of decode() for real applications of this code. It's not
|
index += count; /* else update for next length */
|
||||||
* as readable, but it makes puff() twice as fast. And it only makes the code
|
first += count;
|
||||||
* a few percent larger.
|
first <<= 1;
|
||||||
*/
|
code <<= 1;
|
||||||
#else /* !SLOW */
|
}
|
||||||
local int decode(struct state *s, struct huffman *h)
|
return -9; /* ran out of codes */
|
||||||
{
|
}
|
||||||
int len; /* current number of bits in code */
|
|
||||||
int code; /* len bits being decoded */
|
/*
|
||||||
int first; /* first code of length len */
|
* A faster version of decode() for real applications of this code. It's not
|
||||||
int count; /* number of codes of length len */
|
* as readable, but it makes puff() twice as fast. And it only makes the code
|
||||||
int index; /* index of first code of length len in symbol table */
|
* a few percent larger.
|
||||||
int bitbuf; /* bits from stream */
|
*/
|
||||||
int left; /* bits left in next or left to process */
|
#else /* !SLOW */
|
||||||
short *next; /* next number of codes */
|
local int decode(struct state *s, struct huffman *h)
|
||||||
|
{
|
||||||
bitbuf = s->bitbuf;
|
int len; /* current number of bits in code */
|
||||||
left = s->bitcnt;
|
int code; /* len bits being decoded */
|
||||||
code = first = index = 0;
|
int first; /* first code of length len */
|
||||||
len = 1;
|
int count; /* number of codes of length len */
|
||||||
next = h->count + 1;
|
int index; /* index of first code of length len in symbol table */
|
||||||
while (1) {
|
int bitbuf; /* bits from stream */
|
||||||
while (left--) {
|
int left; /* bits left in next or left to process */
|
||||||
code |= bitbuf & 1;
|
short *next; /* next number of codes */
|
||||||
bitbuf >>= 1;
|
|
||||||
count = *next++;
|
bitbuf = s->bitbuf;
|
||||||
if (code < first + count) { /* if length len, return symbol */
|
left = s->bitcnt;
|
||||||
s->bitbuf = bitbuf;
|
code = first = index = 0;
|
||||||
s->bitcnt = (s->bitcnt - len) & 7;
|
len = 1;
|
||||||
return h->symbol[index + (code - first)];
|
next = h->count + 1;
|
||||||
}
|
while (1) {
|
||||||
index += count; /* else update for next length */
|
while (left--) {
|
||||||
first += count;
|
code |= bitbuf & 1;
|
||||||
first <<= 1;
|
bitbuf >>= 1;
|
||||||
code <<= 1;
|
count = *next++;
|
||||||
len++;
|
if (code < first + count) { /* if length len, return symbol */
|
||||||
}
|
s->bitbuf = bitbuf;
|
||||||
left = (MAXBITS+1) - len;
|
s->bitcnt = (s->bitcnt - len) & 7;
|
||||||
if (left == 0) break;
|
return h->symbol[index + (code - first)];
|
||||||
bitbuf = s->in[s->incnt++];
|
}
|
||||||
if (left > 8) left = 8;
|
index += count; /* else update for next length */
|
||||||
}
|
first += count;
|
||||||
return -9; /* ran out of codes */
|
first <<= 1;
|
||||||
}
|
code <<= 1;
|
||||||
#endif /* SLOW */
|
len++;
|
||||||
|
}
|
||||||
|
left = (MAXBITS+1) - len;
|
||||||
local int construct(struct huffman *h, short *length, int n)
|
if (left == 0) break;
|
||||||
{
|
bitbuf = s->in[s->incnt++];
|
||||||
int symbol; /* current symbol when stepping through length[] */
|
if (left > 8) left = 8;
|
||||||
int len; /* current length when stepping through h->count[] */
|
}
|
||||||
int left; /* number of possible codes left of current length */
|
return -9; /* ran out of codes */
|
||||||
short offs[MAXBITS+1]; /* offsets in symbol table for each length */
|
}
|
||||||
|
#endif /* SLOW */
|
||||||
/* count number of codes of each length */
|
|
||||||
for (len = 0; len <= MAXBITS; len++)
|
|
||||||
h->count[len] = 0;
|
local int construct(struct huffman *h, short *length, int n)
|
||||||
for (symbol = 0; symbol < n; symbol++)
|
{
|
||||||
(h->count[length[symbol]])++; /* assumes lengths are within bounds */
|
int symbol; /* current symbol when stepping through length[] */
|
||||||
if (h->count[0] == n) /* no codes! */
|
int len; /* current length when stepping through h->count[] */
|
||||||
return 0; /* complete, but decode() will fail */
|
int left; /* number of possible codes left of current length */
|
||||||
|
short offs[MAXBITS+1]; /* offsets in symbol table for each length */
|
||||||
/* check for an over-subscribed or incomplete set of lengths */
|
|
||||||
left = 1; /* one possible code of zero length */
|
/* count number of codes of each length */
|
||||||
for (len = 1; len <= MAXBITS; len++) {
|
for (len = 0; len <= MAXBITS; len++)
|
||||||
left <<= 1; /* one more bit, double codes left */
|
h->count[len] = 0;
|
||||||
left -= h->count[len]; /* deduct count from possible codes */
|
for (symbol = 0; symbol < n; symbol++)
|
||||||
if (left < 0) return left; /* over-subscribed--return negative */
|
(h->count[length[symbol]])++; /* assumes lengths are within bounds */
|
||||||
} /* left > 0 means incomplete */
|
if (h->count[0] == n) /* no codes! */
|
||||||
|
return 0; /* complete, but decode() will fail */
|
||||||
/* generate offsets into symbol table for each length for sorting */
|
|
||||||
offs[1] = 0;
|
/* check for an over-subscribed or incomplete set of lengths */
|
||||||
for (len = 1; len < MAXBITS; len++)
|
left = 1; /* one possible code of zero length */
|
||||||
offs[len + 1] = offs[len] + h->count[len];
|
for (len = 1; len <= MAXBITS; len++) {
|
||||||
|
left <<= 1; /* one more bit, double codes left */
|
||||||
/*
|
left -= h->count[len]; /* deduct count from possible codes */
|
||||||
* put symbols in table sorted by length, by symbol order within each
|
if (left < 0) return left; /* over-subscribed--return negative */
|
||||||
* length
|
} /* left > 0 means incomplete */
|
||||||
*/
|
|
||||||
for (symbol = 0; symbol < n; symbol++)
|
/* generate offsets into symbol table for each length for sorting */
|
||||||
if (length[symbol] != 0)
|
offs[1] = 0;
|
||||||
h->symbol[offs[length[symbol]]++] = symbol;
|
for (len = 1; len < MAXBITS; len++)
|
||||||
|
offs[len + 1] = offs[len] + h->count[len];
|
||||||
/* return zero for complete set, positive for incomplete set */
|
|
||||||
return left;
|
/*
|
||||||
}
|
* put symbols in table sorted by length, by symbol order within each
|
||||||
|
* length
|
||||||
|
*/
|
||||||
local int codes(struct state *s,
|
for (symbol = 0; symbol < n; symbol++)
|
||||||
struct huffman *lencode,
|
if (length[symbol] != 0)
|
||||||
struct huffman *distcode)
|
h->symbol[offs[length[symbol]]++] = symbol;
|
||||||
{
|
|
||||||
int symbol; /* decoded symbol */
|
/* return zero for complete set, positive for incomplete set */
|
||||||
int len; /* length for copy */
|
return left;
|
||||||
unsigned dist; /* distance for copy */
|
}
|
||||||
static const short lens[29] = { /* Size base for length codes 257..285 */
|
|
||||||
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
|
|
||||||
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
|
local int codes(struct state *s,
|
||||||
static const short lext[29] = { /* Extra bits for length codes 257..285 */
|
struct huffman *lencode,
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
|
struct huffman *distcode)
|
||||||
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
|
{
|
||||||
static const short dists[30] = { /* Offset base for distance codes 0..29 */
|
int symbol; /* decoded symbol */
|
||||||
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
int len; /* length for copy */
|
||||||
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
|
unsigned dist; /* distance for copy */
|
||||||
8193, 12289, 16385, 24577};
|
static const short lens[29] = { /* Size base for length codes 257..285 */
|
||||||
static const short dext[30] = { /* Extra bits for distance codes 0..29 */
|
3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
|
||||||
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
|
35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258};
|
||||||
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
|
static const short lext[29] = { /* Extra bits for length codes 257..285 */
|
||||||
12, 12, 13, 13};
|
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
|
||||||
|
3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
|
||||||
/* decode literals and length/distance pairs */
|
static const short dists[30] = { /* Offset base for distance codes 0..29 */
|
||||||
do {
|
1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
||||||
symbol = decode(s, lencode);
|
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
|
||||||
if (symbol < 0) return symbol; /* invalid symbol */
|
8193, 12289, 16385, 24577};
|
||||||
if (symbol < 256) { /* literal: symbol is the byte */
|
static const short dext[30] = { /* Extra bits for distance codes 0..29 */
|
||||||
/* write out the literal */
|
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
|
||||||
if (s->out != NIL) {
|
7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
|
||||||
if (s->outcnt == s->outlen) return 1;
|
12, 12, 13, 13};
|
||||||
s->out[s->outcnt] = symbol;
|
|
||||||
}
|
/* decode literals and length/distance pairs */
|
||||||
s->outcnt++;
|
do {
|
||||||
}
|
symbol = decode(s, lencode);
|
||||||
else if (symbol > 256) { /* length */
|
if (symbol < 0) return symbol; /* invalid symbol */
|
||||||
/* get and compute length */
|
if (symbol < 256) { /* literal: symbol is the byte */
|
||||||
symbol -= 257;
|
/* write out the literal */
|
||||||
if (symbol >= 29) return -9; /* invalid fixed code */
|
if (s->out != NIL) {
|
||||||
len = lens[symbol] + bits(s, lext[symbol]);
|
if (s->outcnt == s->outlen) return 1;
|
||||||
|
s->out[s->outcnt] = symbol;
|
||||||
/* get and check distance */
|
}
|
||||||
symbol = decode(s, distcode);
|
s->outcnt++;
|
||||||
if (symbol < 0) return symbol; /* invalid symbol */
|
}
|
||||||
dist = dists[symbol] + bits(s, dext[symbol]);
|
else if (symbol > 256) { /* length */
|
||||||
if (dist > s->outcnt)
|
/* get and compute length */
|
||||||
return -10; /* distance too far back */
|
symbol -= 257;
|
||||||
|
if (symbol >= 29) return -9; /* invalid fixed code */
|
||||||
/* copy length bytes from distance bytes back */
|
len = lens[symbol] + bits(s, lext[symbol]);
|
||||||
if (s->out != NIL) {
|
|
||||||
if (s->outcnt + len > s->outlen) return 1;
|
/* get and check distance */
|
||||||
while (len--) {
|
symbol = decode(s, distcode);
|
||||||
s->out[s->outcnt] = s->out[s->outcnt - dist];
|
if (symbol < 0) return symbol; /* invalid symbol */
|
||||||
s->outcnt++;
|
dist = dists[symbol] + bits(s, dext[symbol]);
|
||||||
}
|
if (dist > s->outcnt)
|
||||||
}
|
return -10; /* distance too far back */
|
||||||
else
|
|
||||||
s->outcnt += len;
|
/* copy length bytes from distance bytes back */
|
||||||
}
|
if (s->out != NIL) {
|
||||||
} while (symbol != 256); /* end of block symbol */
|
if (s->outcnt + len > s->outlen) return 1;
|
||||||
|
while (len--) {
|
||||||
/* done with a valid fixed or dynamic block */
|
s->out[s->outcnt] = s->out[s->outcnt - dist];
|
||||||
return 0;
|
s->outcnt++;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
local int fixed(struct state *s)
|
s->outcnt += len;
|
||||||
{
|
}
|
||||||
static int virgin = 1;
|
} while (symbol != 256); /* end of block symbol */
|
||||||
static short lencnt[MAXBITS+1], lensym[FIXLCODES];
|
|
||||||
static short distcnt[MAXBITS+1], distsym[MAXDCODES];
|
/* done with a valid fixed or dynamic block */
|
||||||
static struct huffman lencode = {lencnt, lensym};
|
return 0;
|
||||||
static struct huffman distcode = {distcnt, distsym};
|
}
|
||||||
|
|
||||||
/* build fixed huffman tables if first call (may not be thread safe) */
|
|
||||||
if (virgin) {
|
local int fixed(struct state *s)
|
||||||
int symbol;
|
{
|
||||||
short lengths[FIXLCODES];
|
static int virgin = 1;
|
||||||
|
static short lencnt[MAXBITS+1], lensym[FIXLCODES];
|
||||||
/* literal/length table */
|
static short distcnt[MAXBITS+1], distsym[MAXDCODES];
|
||||||
for (symbol = 0; symbol < 144; symbol++)
|
static struct huffman lencode = {lencnt, lensym};
|
||||||
lengths[symbol] = 8;
|
static struct huffman distcode = {distcnt, distsym};
|
||||||
for (; symbol < 256; symbol++)
|
|
||||||
lengths[symbol] = 9;
|
/* build fixed huffman tables if first call (may not be thread safe) */
|
||||||
for (; symbol < 280; symbol++)
|
if (virgin) {
|
||||||
lengths[symbol] = 7;
|
int symbol;
|
||||||
for (; symbol < FIXLCODES; symbol++)
|
short lengths[FIXLCODES];
|
||||||
lengths[symbol] = 8;
|
|
||||||
construct(&lencode, lengths, FIXLCODES);
|
/* literal/length table */
|
||||||
|
for (symbol = 0; symbol < 144; symbol++)
|
||||||
/* distance table */
|
lengths[symbol] = 8;
|
||||||
for (symbol = 0; symbol < MAXDCODES; symbol++)
|
for (; symbol < 256; symbol++)
|
||||||
lengths[symbol] = 5;
|
lengths[symbol] = 9;
|
||||||
construct(&distcode, lengths, MAXDCODES);
|
for (; symbol < 280; symbol++)
|
||||||
|
lengths[symbol] = 7;
|
||||||
/* do this just once */
|
for (; symbol < FIXLCODES; symbol++)
|
||||||
virgin = 0;
|
lengths[symbol] = 8;
|
||||||
}
|
construct(&lencode, lengths, FIXLCODES);
|
||||||
|
|
||||||
/* decode data until end-of-block code */
|
/* distance table */
|
||||||
return codes(s, &lencode, &distcode);
|
for (symbol = 0; symbol < MAXDCODES; symbol++)
|
||||||
}
|
lengths[symbol] = 5;
|
||||||
|
construct(&distcode, lengths, MAXDCODES);
|
||||||
|
|
||||||
local int dynamic(struct state *s)
|
/* do this just once */
|
||||||
{
|
virgin = 0;
|
||||||
int nlen, ndist, ncode; /* number of lengths in descriptor */
|
}
|
||||||
int index; /* index of lengths[] */
|
|
||||||
int err; /* construct() return value */
|
/* decode data until end-of-block code */
|
||||||
short lengths[MAXCODES]; /* descriptor code lengths */
|
return codes(s, &lencode, &distcode);
|
||||||
short lencnt[MAXBITS+1], lensym[MAXLCODES]; /* lencode memory */
|
}
|
||||||
short distcnt[MAXBITS+1], distsym[MAXDCODES]; /* distcode memory */
|
|
||||||
struct huffman lencode = {lencnt, lensym}; /* length code */
|
|
||||||
struct huffman distcode = {distcnt, distsym}; /* distance code */
|
local int dynamic(struct state *s)
|
||||||
static const short order[19] = /* permutation of code length codes */
|
{
|
||||||
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
|
int nlen, ndist, ncode; /* number of lengths in descriptor */
|
||||||
|
int index; /* index of lengths[] */
|
||||||
/* get number of lengths in each table, check lengths */
|
int err; /* construct() return value */
|
||||||
nlen = bits(s, 5) + 257;
|
short lengths[MAXCODES]; /* descriptor code lengths */
|
||||||
ndist = bits(s, 5) + 1;
|
short lencnt[MAXBITS+1], lensym[MAXLCODES]; /* lencode memory */
|
||||||
ncode = bits(s, 4) + 4;
|
short distcnt[MAXBITS+1], distsym[MAXDCODES]; /* distcode memory */
|
||||||
if (nlen > MAXLCODES || ndist > MAXDCODES)
|
struct huffman lencode = {lencnt, lensym}; /* length code */
|
||||||
return -3; /* bad counts */
|
struct huffman distcode = {distcnt, distsym}; /* distance code */
|
||||||
|
static const short order[19] = /* permutation of code length codes */
|
||||||
/* read code length code lengths (really), missing lengths are zero */
|
{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
|
||||||
for (index = 0; index < ncode; index++)
|
|
||||||
lengths[order[index]] = bits(s, 3);
|
/* get number of lengths in each table, check lengths */
|
||||||
for (; index < 19; index++)
|
nlen = bits(s, 5) + 257;
|
||||||
lengths[order[index]] = 0;
|
ndist = bits(s, 5) + 1;
|
||||||
|
ncode = bits(s, 4) + 4;
|
||||||
/* build huffman table for code lengths codes (use lencode temporarily) */
|
if (nlen > MAXLCODES || ndist > MAXDCODES)
|
||||||
err = construct(&lencode, lengths, 19);
|
return -3; /* bad counts */
|
||||||
if (err != 0) return -4; /* require complete code set here */
|
|
||||||
|
/* read code length code lengths (really), missing lengths are zero */
|
||||||
/* read length/literal and distance code length tables */
|
for (index = 0; index < ncode; index++)
|
||||||
index = 0;
|
lengths[order[index]] = bits(s, 3);
|
||||||
while (index < nlen + ndist) {
|
for (; index < 19; index++)
|
||||||
int symbol; /* decoded value */
|
lengths[order[index]] = 0;
|
||||||
int len; /* last length to repeat */
|
|
||||||
|
/* build huffman table for code lengths codes (use lencode temporarily) */
|
||||||
symbol = decode(s, &lencode);
|
err = construct(&lencode, lengths, 19);
|
||||||
if (symbol < 16) /* length in 0..15 */
|
if (err != 0) return -4; /* require complete code set here */
|
||||||
lengths[index++] = symbol;
|
|
||||||
else { /* repeat instruction */
|
/* read length/literal and distance code length tables */
|
||||||
len = 0; /* assume repeating zeros */
|
index = 0;
|
||||||
if (symbol == 16) { /* repeat last length 3..6 times */
|
while (index < nlen + ndist) {
|
||||||
if (index == 0) return -5; /* no last length! */
|
int symbol; /* decoded value */
|
||||||
len = lengths[index - 1]; /* last length */
|
int len; /* last length to repeat */
|
||||||
symbol = 3 + bits(s, 2);
|
|
||||||
}
|
symbol = decode(s, &lencode);
|
||||||
else if (symbol == 17) /* repeat zero 3..10 times */
|
if (symbol < 0)
|
||||||
symbol = 3 + bits(s, 3);
|
return symbol; /* invalid symbol */
|
||||||
else /* == 18, repeat zero 11..138 times */
|
if (symbol < 16) /* length in 0..15 */
|
||||||
symbol = 11 + bits(s, 7);
|
lengths[index++] = symbol;
|
||||||
if (index + symbol > nlen + ndist)
|
else { /* repeat instruction */
|
||||||
return -6; /* too many lengths! */
|
len = 0; /* assume repeating zeros */
|
||||||
while (symbol--) /* repeat last or zero symbol times */
|
switch(symbol)
|
||||||
lengths[index++] = len;
|
{
|
||||||
}
|
case 16: { /* repeat last length 3..6 times */
|
||||||
}
|
if (index == 0) return -5; /* no last length! */
|
||||||
|
len = lengths[index - 1]; /* last length */
|
||||||
/* build huffman table for literal/length codes */
|
symbol = 3 + bits(s, 2);
|
||||||
err = construct(&lencode, lengths, nlen);
|
break;
|
||||||
if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1))
|
}
|
||||||
return -7; /* only allow incomplete codes if just one code */
|
case 17: /* repeat zero 3..10 times */
|
||||||
|
symbol = 3 + bits(s, 3);
|
||||||
/* build huffman table for distance codes */
|
break;
|
||||||
err = construct(&distcode, lengths + nlen, ndist);
|
default: /* == 18, repeat zero 11..138 times */
|
||||||
if (err < 0 || (err > 0 && ndist - distcode.count[0] != 1))
|
symbol = 11 + bits(s, 7);
|
||||||
return -8; /* only allow incomplete codes if just one code */
|
break;
|
||||||
|
}
|
||||||
/* decode data until end-of-block code */
|
if ((index + symbol > nlen + ndist))
|
||||||
return codes(s, &lencode, &distcode);
|
return -6; /* too many lengths! */
|
||||||
}
|
while (symbol--) /* repeat last or zero symbol times */
|
||||||
|
lengths[index++] = len;
|
||||||
|
}
|
||||||
void _acrtused () { }
|
}
|
||||||
|
|
||||||
// Decompress deflated data
|
/* check for end-of-block code -- there better be one! */
|
||||||
|
if (lengths[256] == 0)
|
||||||
|
return -9;
|
||||||
|
|
||||||
|
/* build huffman table for literal/length codes */
|
||||||
|
err = construct(&lencode, lengths, nlen);
|
||||||
|
if (err < 0 || (err > 0 && nlen - lencode.count[0] != 1))
|
||||||
|
return -7; /* only allow incomplete codes if just one code */
|
||||||
|
|
||||||
|
/* build huffman table for distance codes */
|
||||||
|
err = construct(&distcode, lengths + nlen, ndist);
|
||||||
|
if (err < 0 || (err > 0 && ndist - distcode.count[0] != 1))
|
||||||
|
return -8; /* only allow incomplete codes if just one code */
|
||||||
|
|
||||||
|
/* decode data until end-of-block code */
|
||||||
|
return codes(s, &lencode, &distcode);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void _acrtused () { }
|
||||||
|
|
||||||
|
// Decompress deflated data
|
||||||
int far main (
|
int far main (
|
||||||
unsigned char *dest, /* pointer to destination pointer */
|
unsigned char *dest, /* pointer to destination pointer */
|
||||||
unsigned int destlen, /* amount of output space */
|
unsigned int destlen, /* amount of output space */
|
||||||
unsigned char *source) /* pointer to source data pointer */
|
unsigned char *source, /* pointer to source data pointer */
|
||||||
|
unsigned int sourcelen)
|
||||||
{
|
{
|
||||||
struct state s; /* input/output state */
|
struct state s; /* input/output state */
|
||||||
int last, type; /* block information */
|
int last, type; /* block information */
|
||||||
@@ -417,6 +439,7 @@ int far main (
|
|||||||
|
|
||||||
/* initialize input state */
|
/* initialize input state */
|
||||||
s.in = source;
|
s.in = source;
|
||||||
|
s.inlen = sourcelen;
|
||||||
s.incnt = 0;
|
s.incnt = 0;
|
||||||
s.bitbuf = 0;
|
s.bitbuf = 0;
|
||||||
s.bitcnt = 0;
|
s.bitcnt = 0;
|
||||||
@@ -425,12 +448,24 @@ int far main (
|
|||||||
do {
|
do {
|
||||||
last = bits(&s, 1); /* one if last block */
|
last = bits(&s, 1); /* one if last block */
|
||||||
type = bits(&s, 2); /* block type 0..3 */
|
type = bits(&s, 2); /* block type 0..3 */
|
||||||
err = type == 0 ? stored(&s) :
|
switch(type)
|
||||||
(type == 1 ? fixed(&s) :
|
{
|
||||||
(type == 2 ? dynamic(&s) :
|
case 0:
|
||||||
-1)); /* type == 3, invalid */
|
err = stored(&s);
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
err = fixed(&s);
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
err = dynamic(&s);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
err = -1; /* type == 3, invalid */
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
if (err != 0) break; /* return with error */
|
if (err != 0) break; /* return with error */
|
||||||
} while (!last);
|
} while (!last);
|
||||||
|
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user