ReactOS 0.4.16-dev-295-g4aee028
lzx.c File Reference
#include "lzx.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "windef.h"
#include "winbase.h"
Include dependency graph for lzx.c:

Go to the source code of this file.

Classes

struct  LZXstate
 
struct  lzx_bits
 

Macros

#define LZX_MIN_MATCH   (2)
 
#define LZX_MAX_MATCH   (257)
 
#define LZX_NUM_CHARS   (256)
 
#define LZX_BLOCKTYPE_INVALID   (0) /* also blocktypes 4-7 invalid */
 
#define LZX_BLOCKTYPE_VERBATIM   (1)
 
#define LZX_BLOCKTYPE_ALIGNED   (2)
 
#define LZX_BLOCKTYPE_UNCOMPRESSED   (3)
 
#define LZX_PRETREE_NUM_ELEMENTS   (20)
 
#define LZX_ALIGNED_NUM_ELEMENTS   (8) /* aligned offset tree #elements */
 
#define LZX_NUM_PRIMARY_LENGTHS   (7) /* this one missing from spec! */
 
#define LZX_NUM_SECONDARY_LENGTHS   (249) /* length tree #elements */
 
#define LZX_PRETREE_MAXSYMBOLS   (LZX_PRETREE_NUM_ELEMENTS)
 
#define LZX_PRETREE_TABLEBITS   (6)
 
#define LZX_MAINTREE_MAXSYMBOLS   (LZX_NUM_CHARS + 50*8)
 
#define LZX_MAINTREE_TABLEBITS   (12)
 
#define LZX_LENGTH_MAXSYMBOLS   (LZX_NUM_SECONDARY_LENGTHS+1)
 
#define LZX_LENGTH_TABLEBITS   (12)
 
#define LZX_ALIGNED_MAXSYMBOLS   (LZX_ALIGNED_NUM_ELEMENTS)
 
#define LZX_ALIGNED_TABLEBITS   (7)
 
#define LZX_LENTABLE_SAFETY   (64) /* we allow length table decoding overruns */
 
#define LZX_DECLARE_TABLE(tbl)
 
#define ULONG_BITS   (sizeof(ULONG)<<3)
 
#define INIT_BITSTREAM   do { bitsleft = 0; bitbuf = 0; } while (0)
 
#define ENSURE_BITS(n)
 
#define PEEK_BITS(n)   (bitbuf >> (ULONG_BITS - (n)))
 
#define REMOVE_BITS(n)   ((bitbuf <<= (n)), (bitsleft -= (n)))
 
#define READ_BITS(v, n)
 
#define TABLEBITS(tbl)   (LZX_##tbl##_TABLEBITS)
 
#define MAXSYMBOLS(tbl)   (LZX_##tbl##_MAXSYMBOLS)
 
#define SYMTABLE(tbl)   (pState->tbl##_table)
 
#define LENTABLE(tbl)   (pState->tbl##_len)
 
#define BUILD_TABLE(tbl)
 
#define READ_HUFFSYM(tbl, var)
 
#define READ_LENGTHS(tbl, first, last)
 

Typedefs

typedef unsigned char UBYTE
 
typedef unsigned short UWORD
 

Functions

struct LZXstateLZXinit (int wndsize)
 
void LZXteardown (struct LZXstate *pState)
 
int LZXreset (struct LZXstate *pState)
 
static int make_decode_table (ULONG nsyms, ULONG nbits, UBYTE *length, UWORD *table)
 
static int lzx_read_lens (struct LZXstate *pState, UBYTE *lens, ULONG first, ULONG last, struct lzx_bits *lb)
 
int LZXdecompress (struct LZXstate *pState, unsigned char *inpos, unsigned char *outpos, int inlen, int outlen)
 

Variables

static const UBYTE extra_bits [51]
 
static const ULONG position_base [51]
 

Macro Definition Documentation

◆ BUILD_TABLE

#define BUILD_TABLE (   tbl)
Value:
MAXSYMBOLS(tbl), TABLEBITS(tbl), LENTABLE(tbl), SYMTABLE(tbl) \
)) { return DECR_ILLEGALDATA; }
#define DECR_ILLEGALDATA
Definition: mszip.h:81
#define LENTABLE(tbl)
Definition: lzx.c:284
#define SYMTABLE(tbl)
Definition: lzx.c:283
#define MAXSYMBOLS(tbl)
Definition: lzx.c:282
static int make_decode_table(ULONG nsyms, ULONG nbits, UBYTE *length, UWORD *table)
Definition: lzx.c:342
#define TABLEBITS(tbl)
Definition: lzx.c:281

Definition at line 291 of file lzx.c.

◆ ENSURE_BITS

#define ENSURE_BITS (   n)
Value:
while (bitsleft < (n)) { \
bitbuf |= ((inpos[1]<<8)|inpos[0]) << (ULONG_BITS-16 - bitsleft); \
bitsleft += 16; inpos+=2; \
}
GLdouble n
Definition: glext.h:7729
#define ULONG_BITS
Definition: lzx.c:259

Definition at line 263 of file lzx.c.

◆ INIT_BITSTREAM

#define INIT_BITSTREAM   do { bitsleft = 0; bitbuf = 0; } while (0)

Definition at line 261 of file lzx.c.

◆ LENTABLE

#define LENTABLE (   tbl)    (pState->tbl##_len)

Definition at line 284 of file lzx.c.

◆ LZX_ALIGNED_MAXSYMBOLS

#define LZX_ALIGNED_MAXSYMBOLS   (LZX_ALIGNED_NUM_ELEMENTS)

Definition at line 71 of file lzx.c.

◆ LZX_ALIGNED_NUM_ELEMENTS

#define LZX_ALIGNED_NUM_ELEMENTS   (8) /* aligned offset tree #elements */

Definition at line 60 of file lzx.c.

◆ LZX_ALIGNED_TABLEBITS

#define LZX_ALIGNED_TABLEBITS   (7)

Definition at line 72 of file lzx.c.

◆ LZX_BLOCKTYPE_ALIGNED

#define LZX_BLOCKTYPE_ALIGNED   (2)

Definition at line 57 of file lzx.c.

◆ LZX_BLOCKTYPE_INVALID

#define LZX_BLOCKTYPE_INVALID   (0) /* also blocktypes 4-7 invalid */

Definition at line 55 of file lzx.c.

◆ LZX_BLOCKTYPE_UNCOMPRESSED

#define LZX_BLOCKTYPE_UNCOMPRESSED   (3)

Definition at line 58 of file lzx.c.

◆ LZX_BLOCKTYPE_VERBATIM

#define LZX_BLOCKTYPE_VERBATIM   (1)

Definition at line 56 of file lzx.c.

◆ LZX_DECLARE_TABLE

#define LZX_DECLARE_TABLE (   tbl)
Value:
UWORD tbl##_table[(1<<LZX_##tbl##_TABLEBITS) + (LZX_##tbl##_MAXSYMBOLS<<1)];\
UBYTE tbl##_len [LZX_##tbl##_MAXSYMBOLS + LZX_LENTABLE_SAFETY]
unsigned char UBYTE
Definition: lzx.c:48
#define LZX_LENTABLE_SAFETY
Definition: lzx.c:74
unsigned short UWORD
Definition: lzx.c:49

Definition at line 76 of file lzx.c.

◆ LZX_LENGTH_MAXSYMBOLS

#define LZX_LENGTH_MAXSYMBOLS   (LZX_NUM_SECONDARY_LENGTHS+1)

Definition at line 69 of file lzx.c.

◆ LZX_LENGTH_TABLEBITS

#define LZX_LENGTH_TABLEBITS   (12)

Definition at line 70 of file lzx.c.

◆ LZX_LENTABLE_SAFETY

#define LZX_LENTABLE_SAFETY   (64) /* we allow length table decoding overruns */

Definition at line 74 of file lzx.c.

◆ LZX_MAINTREE_MAXSYMBOLS

#define LZX_MAINTREE_MAXSYMBOLS   (LZX_NUM_CHARS + 50*8)

Definition at line 67 of file lzx.c.

◆ LZX_MAINTREE_TABLEBITS

#define LZX_MAINTREE_TABLEBITS   (12)

Definition at line 68 of file lzx.c.

◆ LZX_MAX_MATCH

#define LZX_MAX_MATCH   (257)

Definition at line 53 of file lzx.c.

◆ LZX_MIN_MATCH

#define LZX_MIN_MATCH   (2)

Definition at line 52 of file lzx.c.

◆ LZX_NUM_CHARS

#define LZX_NUM_CHARS   (256)

Definition at line 54 of file lzx.c.

◆ LZX_NUM_PRIMARY_LENGTHS

#define LZX_NUM_PRIMARY_LENGTHS   (7) /* this one missing from spec! */

Definition at line 61 of file lzx.c.

◆ LZX_NUM_SECONDARY_LENGTHS

#define LZX_NUM_SECONDARY_LENGTHS   (249) /* length tree #elements */

Definition at line 62 of file lzx.c.

◆ LZX_PRETREE_MAXSYMBOLS

#define LZX_PRETREE_MAXSYMBOLS   (LZX_PRETREE_NUM_ELEMENTS)

Definition at line 65 of file lzx.c.

◆ LZX_PRETREE_NUM_ELEMENTS

#define LZX_PRETREE_NUM_ELEMENTS   (20)

Definition at line 59 of file lzx.c.

◆ LZX_PRETREE_TABLEBITS

#define LZX_PRETREE_TABLEBITS   (6)

Definition at line 66 of file lzx.c.

◆ MAXSYMBOLS

#define MAXSYMBOLS (   tbl)    (LZX_##tbl##_MAXSYMBOLS)

Definition at line 282 of file lzx.c.

◆ PEEK_BITS

#define PEEK_BITS (   n)    (bitbuf >> (ULONG_BITS - (n)))

Definition at line 269 of file lzx.c.

◆ READ_BITS

#define READ_BITS (   v,
  n 
)
Value:
do { \
ENSURE_BITS(n); \
(v) = PEEK_BITS(n); \
REMOVE_BITS(n); \
} while (0)
const GLdouble * v
Definition: gl.h:2040
#define PEEK_BITS(n)
Definition: lzx.c:269

Definition at line 272 of file lzx.c.

◆ READ_HUFFSYM

#define READ_HUFFSYM (   tbl,
  var 
)
Value:
do { \
ENSURE_BITS(16); \
hufftbl = SYMTABLE(tbl); \
if ((i = hufftbl[PEEK_BITS(TABLEBITS(tbl))]) >= MAXSYMBOLS(tbl)) { \
j = 1 << (ULONG_BITS - TABLEBITS(tbl)); \
do { \
j >>= 1; i <<= 1; i |= (bitbuf & j) ? 1 : 0; \
if (!j) { return DECR_ILLEGALDATA; } \
} while ((i = hufftbl[i]) >= MAXSYMBOLS(tbl)); \
} \
j = LENTABLE(tbl)[(var) = i]; \
REMOVE_BITS(j); \
} while (0)
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
const char * var
Definition: shader.c:5666

Definition at line 300 of file lzx.c.

◆ READ_LENGTHS

#define READ_LENGTHS (   tbl,
  first,
  last 
)
Value:
do { \
lb.bb = bitbuf; lb.bl = bitsleft; lb.ip = inpos; \
if (lzx_read_lens(pState, LENTABLE(tbl),(first),(last),&lb)) { \
return DECR_ILLEGALDATA; \
} \
bitbuf = lb.bb; bitsleft = lb.bl; inpos = lb.ip; \
} while (0)
const GLint * first
Definition: glext.h:5794
static int lzx_read_lens(struct LZXstate *pState, UBYTE *lens, ULONG first, ULONG last, struct lzx_bits *lb)
Definition: lzx.c:418
static UINT UINT last
Definition: font.c:45

Definition at line 319 of file lzx.c.

◆ REMOVE_BITS

#define REMOVE_BITS (   n)    ((bitbuf <<= (n)), (bitsleft -= (n)))

Definition at line 270 of file lzx.c.

◆ SYMTABLE

#define SYMTABLE (   tbl)    (pState->tbl##_table)

Definition at line 283 of file lzx.c.

◆ TABLEBITS

#define TABLEBITS (   tbl)    (LZX_##tbl##_TABLEBITS)

Definition at line 281 of file lzx.c.

◆ ULONG_BITS

#define ULONG_BITS   (sizeof(ULONG)<<3)

Definition at line 259 of file lzx.c.

Typedef Documentation

◆ UBYTE

typedef unsigned char UBYTE

Definition at line 48 of file lzx.c.

◆ UWORD

typedef unsigned short UWORD

Definition at line 49 of file lzx.c.

Function Documentation

◆ lzx_read_lens()

static int lzx_read_lens ( struct LZXstate pState,
UBYTE lens,
ULONG  first,
ULONG  last,
struct lzx_bits lb 
)
static

Definition at line 418 of file lzx.c.

418 {
419 ULONG i,j, x,y;
420 int z;
421
422 register ULONG bitbuf = lb->bb;
423 register int bitsleft = lb->bl;
424 UBYTE *inpos = lb->ip;
425 UWORD *hufftbl;
426
427 for (x = 0; x < 20; x++) {
428 READ_BITS(y, 4);
429 LENTABLE(PRETREE)[x] = y;
430 }
431 BUILD_TABLE(PRETREE);
432
433 for (x = first; x < last; ) {
434 READ_HUFFSYM(PRETREE, z);
435 if (z == 17) {
436 READ_BITS(y, 4); y += 4;
437 while (y--) lens[x++] = 0;
438 }
439 else if (z == 18) {
440 READ_BITS(y, 5); y += 20;
441 while (y--) lens[x++] = 0;
442 }
443 else if (z == 19) {
444 READ_BITS(y, 1); y += 4;
445 READ_HUFFSYM(PRETREE, z);
446 z = lens[x] - z; if (z < 0) z += 17;
447 while (y--) lens[x++] = z;
448 }
449 else {
450 z = lens[x] - z; if (z < 0) z += 17;
451 lens[x++] = z;
452 }
453 }
454
455 lb->bb = bitbuf;
456 lb->bl = bitsleft;
457 lb->ip = inpos;
458 return 0;
459}
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
GLint GLint GLint GLint GLint GLint y
Definition: gl.h:1548
GLdouble GLdouble z
Definition: glext.h:5874
#define READ_BITS(v, n)
Definition: lzx.c:272
#define READ_HUFFSYM(tbl, var)
Definition: lzx.c:300
#define BUILD_TABLE(tbl)
Definition: lzx.c:291
cab_ULONG bb
Definition: cabinet.h:235
cab_UBYTE * ip
Definition: cabinet.h:237
int bl
Definition: cabinet.h:236
uint32_t ULONG
Definition: typedefs.h:59

◆ LZXdecompress()

int LZXdecompress ( struct LZXstate pState,
unsigned char inpos,
unsigned char outpos,
int  inlen,
int  outlen 
)

Definition at line 461 of file lzx.c.

461 {
462 UBYTE *endinp = inpos + inlen;
463 UBYTE *window = pState->window;
464 UBYTE *runsrc, *rundest;
465 UWORD *hufftbl; /* used in READ_HUFFSYM macro as chosen decoding table */
466
467 ULONG window_posn = pState->window_posn;
468 ULONG window_size = pState->window_size;
469 ULONG R0 = pState->R0;
470 ULONG R1 = pState->R1;
471 ULONG R2 = pState->R2;
472
473 register ULONG bitbuf;
474 register int bitsleft;
475 ULONG match_offset, i,j,k; /* ijk used in READ_HUFFSYM macro */
476 struct lzx_bits lb; /* used in READ_LENGTHS macro */
477
478 int togo = outlen, this_run, main_element, aligned_bits;
479 int match_length, length_footer, extra, verbatim_bits;
480 int copy_length;
481
483
484 /* read header if necessary */
485 if (!pState->header_read) {
486 i = j = 0;
487 READ_BITS(k, 1); if (k) { READ_BITS(i,16); READ_BITS(j,16); }
488 pState->intel_filesize = (i << 16) | j; /* or 0 if not encoded */
489 pState->header_read = 1;
490 }
491
492 /* main decoding loop */
493 while (togo > 0) {
494 /* last block finished, new block expected */
495 if (pState->block_remaining == 0) {
496 if (pState->block_type == LZX_BLOCKTYPE_UNCOMPRESSED) {
497 if (pState->block_length & 1) inpos++; /* realign bitstream to word */
499 }
500
501 READ_BITS(pState->block_type, 3);
502 READ_BITS(i, 16);
503 READ_BITS(j, 8);
504 pState->block_remaining = pState->block_length = (i << 8) | j;
505
506 switch (pState->block_type) {
508 for (i = 0; i < 8; i++) { READ_BITS(j, 3); LENTABLE(ALIGNED)[i] = j; }
510 /* rest of aligned header is same as verbatim */
511
513 READ_LENGTHS(MAINTREE, 0, 256);
514 READ_LENGTHS(MAINTREE, 256, pState->main_elements);
515 BUILD_TABLE(MAINTREE);
516 if (LENTABLE(MAINTREE)[0xE8] != 0) pState->intel_started = 1;
517
520 break;
521
523 pState->intel_started = 1; /* because we can't assume otherwise */
524 ENSURE_BITS(16); /* get up to 16 pad bits into the buffer */
525 if (bitsleft > 16) inpos -= 2; /* and align the bitstream! */
526 R0 = inpos[0]|(inpos[1]<<8)|(inpos[2]<<16)|(inpos[3]<<24);inpos+=4;
527 R1 = inpos[0]|(inpos[1]<<8)|(inpos[2]<<16)|(inpos[3]<<24);inpos+=4;
528 R2 = inpos[0]|(inpos[1]<<8)|(inpos[2]<<16)|(inpos[3]<<24);inpos+=4;
529 break;
530
531 default:
532 return DECR_ILLEGALDATA;
533 }
534 }
535
536 /* buffer exhaustion check */
537 if (inpos > endinp) {
538 /* it's possible to have a file where the next run is less than
539 * 16 bits in size. In this case, the READ_HUFFSYM() macro used
540 * in building the tables will exhaust the buffer, so we should
541 * allow for this, but not allow those accidentally read bits to
542 * be used (so we check that there are at least 16 bits
543 * remaining - in this boundary case they aren't really part of
544 * the compressed data)
545 */
546 if (inpos > (endinp+2) || bitsleft < 16) return DECR_ILLEGALDATA;
547 }
548
549 while ((this_run = pState->block_remaining) > 0 && togo > 0) {
550 if (this_run > togo) this_run = togo;
551 togo -= this_run;
552 pState->block_remaining -= this_run;
553
554 /* apply 2^x-1 mask */
555 window_posn &= window_size - 1;
556 /* runs can't straddle the window wraparound */
557 if ((window_posn + this_run) > window_size)
558 return DECR_DATAFORMAT;
559
560 switch (pState->block_type) {
561
563 while (this_run > 0) {
564 READ_HUFFSYM(MAINTREE, main_element);
565
566 if (main_element < LZX_NUM_CHARS) {
567 /* literal: 0 to LZX_NUM_CHARS-1 */
568 window[window_posn++] = main_element;
569 this_run--;
570 }
571 else {
572 /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */
573 main_element -= LZX_NUM_CHARS;
574
575 match_length = main_element & LZX_NUM_PRIMARY_LENGTHS;
576 if (match_length == LZX_NUM_PRIMARY_LENGTHS) {
577 READ_HUFFSYM(LENGTH, length_footer);
578 match_length += length_footer;
579 }
580 match_length += LZX_MIN_MATCH;
581
582 match_offset = main_element >> 3;
583
584 if (match_offset > 2) {
585 /* not repeated offset */
586 if (match_offset != 3) {
587 extra = extra_bits[match_offset];
588 READ_BITS(verbatim_bits, extra);
589 match_offset = position_base[match_offset] - 2 + verbatim_bits;
590 }
591 else {
592 match_offset = 1;
593 }
594
595 /* update repeated offset LRU queue */
596 R2 = R1; R1 = R0; R0 = match_offset;
597 }
598 else if (match_offset == 0) {
599 match_offset = R0;
600 }
601 else if (match_offset == 1) {
602 match_offset = R1;
603 R1 = R0; R0 = match_offset;
604 }
605 else /* match_offset == 2 */ {
606 match_offset = R2;
607 R2 = R0; R0 = match_offset;
608 }
609
610 rundest = window + window_posn;
611 this_run -= match_length;
612
613 /* copy any wrapped around source data */
614 if (window_posn >= match_offset) {
615 /* no wrap */
616 runsrc = rundest - match_offset;
617 } else {
618 runsrc = rundest + (window_size - match_offset);
619 copy_length = match_offset - window_posn;
620 if (copy_length < match_length) {
621 match_length -= copy_length;
622 window_posn += copy_length;
623 while (copy_length-- > 0) *rundest++ = *runsrc++;
624 runsrc = window;
625 }
626 }
627 window_posn += match_length;
628
629 /* copy match data - no worries about destination wraps */
630 while (match_length-- > 0) *rundest++ = *runsrc++;
631
632 }
633 }
634 break;
635
637 while (this_run > 0) {
638 READ_HUFFSYM(MAINTREE, main_element);
639
640 if (main_element < LZX_NUM_CHARS) {
641 /* literal: 0 to LZX_NUM_CHARS-1 */
642 window[window_posn++] = main_element;
643 this_run--;
644 }
645 else {
646 /* match: LZX_NUM_CHARS + ((slot<<3) | length_header (3 bits)) */
647 main_element -= LZX_NUM_CHARS;
648
649 match_length = main_element & LZX_NUM_PRIMARY_LENGTHS;
650 if (match_length == LZX_NUM_PRIMARY_LENGTHS) {
651 READ_HUFFSYM(LENGTH, length_footer);
652 match_length += length_footer;
653 }
654 match_length += LZX_MIN_MATCH;
655
656 match_offset = main_element >> 3;
657
658 if (match_offset > 2) {
659 /* not repeated offset */
660 extra = extra_bits[match_offset];
661 match_offset = position_base[match_offset] - 2;
662 if (extra > 3) {
663 /* verbatim and aligned bits */
664 extra -= 3;
665 READ_BITS(verbatim_bits, extra);
666 match_offset += (verbatim_bits << 3);
667 READ_HUFFSYM(ALIGNED, aligned_bits);
668 match_offset += aligned_bits;
669 }
670 else if (extra == 3) {
671 /* aligned bits only */
672 READ_HUFFSYM(ALIGNED, aligned_bits);
673 match_offset += aligned_bits;
674 }
675 else if (extra > 0) { /* extra==1, extra==2 */
676 /* verbatim bits only */
677 READ_BITS(verbatim_bits, extra);
678 match_offset += verbatim_bits;
679 }
680 else /* extra == 0 */ {
681 /* ??? */
682 match_offset = 1;
683 }
684
685 /* update repeated offset LRU queue */
686 R2 = R1; R1 = R0; R0 = match_offset;
687 }
688 else if (match_offset == 0) {
689 match_offset = R0;
690 }
691 else if (match_offset == 1) {
692 match_offset = R1;
693 R1 = R0; R0 = match_offset;
694 }
695 else /* match_offset == 2 */ {
696 match_offset = R2;
697 R2 = R0; R0 = match_offset;
698 }
699
700 rundest = window + window_posn;
701 this_run -= match_length;
702
703 /* copy any wrapped around source data */
704 if (window_posn >= match_offset) {
705 /* no wrap */
706 runsrc = rundest - match_offset;
707 } else {
708 runsrc = rundest + (window_size - match_offset);
709 copy_length = match_offset - window_posn;
710 if (copy_length < match_length) {
711 match_length -= copy_length;
712 window_posn += copy_length;
713 while (copy_length-- > 0) *rundest++ = *runsrc++;
714 runsrc = window;
715 }
716 }
717 window_posn += match_length;
718
719 /* copy match data - no worries about destination wraps */
720 while (match_length-- > 0) *rundest++ = *runsrc++;
721
722 }
723 }
724 break;
725
727 if ((inpos + this_run) > endinp) return DECR_ILLEGALDATA;
728 memcpy(window + window_posn, inpos, (size_t) this_run);
729 inpos += this_run; window_posn += this_run;
730 break;
731
732 default:
733 return DECR_ILLEGALDATA; /* might as well */
734 }
735
736 }
737 }
738
739 if (togo != 0) return DECR_ILLEGALDATA;
740 memcpy(outpos, window + ((!window_posn) ? window_size : window_posn) - outlen, (size_t) outlen);
741
742 pState->window_posn = window_posn;
743 pState->R0 = R0;
744 pState->R1 = R1;
745 pState->R2 = R2;
746
747 /* intel E8 decoding */
748 if ((pState->frames_read++ < 32768) && pState->intel_filesize != 0) {
749 if (outlen <= 6 || !pState->intel_started) {
750 pState->intel_curpos += outlen;
751 }
752 else {
753 UBYTE *data = outpos;
754 UBYTE *dataend = data + outlen - 10;
755 LONG curpos = pState->intel_curpos;
756 LONG filesize = pState->intel_filesize;
757 LONG abs_off, rel_off;
758
759 pState->intel_curpos = curpos + outlen;
760
761 while (data < dataend) {
762 if (*data++ != 0xE8) { curpos++; continue; }
763 abs_off = data[0] | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);
764 if ((abs_off >= -curpos) && (abs_off < filesize)) {
765 rel_off = (abs_off >= 0) ? abs_off - curpos : abs_off + filesize;
766 data[0] = (UBYTE) rel_off;
767 data[1] = (UBYTE) (rel_off >> 8);
768 data[2] = (UBYTE) (rel_off >> 16);
769 data[3] = (UBYTE) (rel_off >> 24);
770 }
771 data += 4;
772 curpos += 5;
773 }
774 }
775 }
776 return DECR_OK;
777}
#define DECR_OK
Definition: mszip.h:79
#define DECR_DATAFORMAT
Definition: mszip.h:80
@ LENGTH
Definition: inflate.c:185
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
@ extra
Definition: id3.c:95
#define LZX_NUM_CHARS
Definition: lzx.c:54
#define READ_LENGTHS(tbl, first, last)
Definition: lzx.c:319
#define LZX_BLOCKTYPE_ALIGNED
Definition: lzx.c:57
#define LZX_NUM_SECONDARY_LENGTHS
Definition: lzx.c:62
#define LZX_BLOCKTYPE_VERBATIM
Definition: lzx.c:56
#define LZX_NUM_PRIMARY_LENGTHS
Definition: lzx.c:61
#define LZX_BLOCKTYPE_UNCOMPRESSED
Definition: lzx.c:58
static const ULONG position_base[51]
Definition: lzx.c:165
#define INIT_BITSTREAM
Definition: lzx.c:261
#define LZX_MIN_MATCH
Definition: lzx.c:52
#define ENSURE_BITS(n)
Definition: lzx.c:263
static const UBYTE extra_bits[51]
Definition: lzx.c:158
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static IHTMLWindow2 * window
Definition: events.c:77
int k
Definition: mpi.c:3369
#define ALIGNED(a)
Definition: optimize.h:190
long LONG
Definition: pedump.c:60
#define R1(v, w, x, y, z, i)
Definition: sha1.c:36
#define R2(v, w, x, y, z, i)
Definition: sha1.c:37
#define R0(v, w, x, y, z, i)
Definition: sha1.c:35
cab_ULONG block_remaining
Definition: cabinet.h:222
int header_read
Definition: cabinet.h:219
cab_UWORD main_elements
Definition: cabinet.h:218
cab_LONG intel_filesize
Definition: cabinet.h:224
cab_ULONG R0
Definition: cabinet.h:217
cab_LONG intel_curpos
Definition: cabinet.h:225
cab_UWORD block_type
Definition: cabinet.h:220
cab_ULONG R2
Definition: cabinet.h:217
cab_ULONG frames_read
Definition: cabinet.h:223
int intel_started
Definition: cabinet.h:226
cab_ULONG R1
Definition: cabinet.h:217
cab_UBYTE * window
Definition: cabinet.h:213
cab_ULONG window_size
Definition: cabinet.h:214
cab_ULONG block_length
Definition: cabinet.h:221
cab_ULONG window_posn
Definition: cabinet.h:216

Referenced by _chm_decompress_block(), and CreateNewFileFromPatch().

◆ LZXinit()

struct LZXstate * LZXinit ( int  wndsize)

Definition at line 172 of file lzx.c.

173{
174 struct LZXstate *pState=NULL;
175 int i, posn_slots;
176
177 /* allocate state and associated window */
178 pState = HeapAlloc(GetProcessHeap(), 0, sizeof(struct LZXstate));
179 if (!(pState->window = HeapAlloc(GetProcessHeap(), 0, wndsize)))
180 {
181 HeapFree(GetProcessHeap(), 0, pState);
182 return NULL;
183 }
184 pState->actual_size = wndsize;
185 pState->window_size = wndsize;
186
187 /* calculate required position slots */
188 posn_slots = i = 0;
189 while (i < wndsize) i += 1 << extra_bits[posn_slots++];
190
191 /* initialize other state */
192 pState->R0 = pState->R1 = pState->R2 = 1;
193 pState->main_elements = LZX_NUM_CHARS + (posn_slots << 3);
194 pState->header_read = 0;
195 pState->frames_read = 0;
196 pState->block_remaining = 0;
198 pState->intel_curpos = 0;
199 pState->intel_started = 0;
200 pState->window_posn = 0;
201
202 /* initialise tables to 0 (because deltas will be applied to them) */
203 for (i = 0; i < LZX_MAINTREE_MAXSYMBOLS; i++) pState->MAINTREE_len[i] = 0;
204 for (i = 0; i < LZX_LENGTH_MAXSYMBOLS; i++) pState->LENGTH_len[i] = 0;
205
206 return pState;
207}
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapFree(x, y, z)
Definition: compat.h:735
#define LZX_BLOCKTYPE_INVALID
Definition: lzx.c:55
#define LZX_MAINTREE_MAXSYMBOLS
Definition: lzx.c:67
#define LZX_LENGTH_MAXSYMBOLS
Definition: lzx.c:69
#define for
Definition: utility.h:88
cab_ULONG actual_size
Definition: cabinet.h:215

Referenced by _chm_decompress_region(), and CreateNewFileFromPatch().

◆ LZXreset()

int LZXreset ( struct LZXstate pState)

Definition at line 218 of file lzx.c.

219{
220 int i;
221
222 pState->R0 = pState->R1 = pState->R2 = 1;
223 pState->header_read = 0;
224 pState->frames_read = 0;
225 pState->block_remaining = 0;
227 pState->intel_curpos = 0;
228 pState->intel_started = 0;
229 pState->window_posn = 0;
230
231 for (i = 0; i < LZX_MAINTREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY; i++) pState->MAINTREE_len[i] = 0;
232 for (i = 0; i < LZX_LENGTH_MAXSYMBOLS + LZX_LENTABLE_SAFETY; i++) pState->LENGTH_len[i] = 0;
233
234 return DECR_OK;
235}

Referenced by _chm_decompress_block().

◆ LZXteardown()

void LZXteardown ( struct LZXstate pState)

Definition at line 209 of file lzx.c.

210{
211 if (pState)
212 {
213 HeapFree(GetProcessHeap(), 0, pState->window);
214 HeapFree(GetProcessHeap(), 0, pState);
215 }
216}

Referenced by chm_close(), and CreateNewFileFromPatch().

◆ make_decode_table()

static int make_decode_table ( ULONG  nsyms,
ULONG  nbits,
UBYTE length,
UWORD table 
)
static

Definition at line 342 of file lzx.c.

342 {
343 register UWORD sym;
344 register ULONG leaf;
345 register UBYTE bit_num = 1;
346 ULONG fill;
347 ULONG pos = 0; /* the current position in the decode table */
348 ULONG table_mask = 1 << nbits;
349 ULONG bit_mask = table_mask >> 1; /* don't do 0 length codes */
350 ULONG next_symbol = bit_mask; /* base of allocation for long codes */
351
352 /* fill entries for codes short enough for a direct mapping */
353 while (bit_num <= nbits) {
354 for (sym = 0; sym < nsyms; sym++) {
355 if (length[sym] == bit_num) {
356 leaf = pos;
357
358 if((pos += bit_mask) > table_mask) return 1; /* table overrun */
359
360 /* fill all possible lookups of this symbol with the symbol itself */
361 fill = bit_mask;
362 while (fill-- > 0) table[leaf++] = sym;
363 }
364 }
365 bit_mask >>= 1;
366 bit_num++;
367 }
368
369 /* if there are any codes longer than nbits */
370 if (pos != table_mask) {
371 /* clear the remainder of the table */
372 for (sym = pos; sym < table_mask; sym++) table[sym] = 0;
373
374 /* give ourselves room for codes to grow by up to 16 more bits */
375 pos <<= 16;
376 table_mask <<= 16;
377 bit_mask = 1 << 15;
378
379 while (bit_num <= 16) {
380 for (sym = 0; sym < nsyms; sym++) {
381 if (length[sym] == bit_num) {
382 leaf = pos >> 16;
383 for (fill = 0; fill < bit_num - nbits; fill++) {
384 /* if this path hasn't been taken yet, 'allocate' two entries */
385 if (table[leaf] == 0) {
386 table[(next_symbol << 1)] = 0;
387 table[(next_symbol << 1) + 1] = 0;
388 table[leaf] = next_symbol++;
389 }
390 /* follow the path and select either left or right for next bit */
391 leaf = table[leaf] << 1;
392 if ((pos >> (15-fill)) & 1) leaf++;
393 }
394 table[leaf] = sym;
395
396 if ((pos += bit_mask) > table_mask) return 1; /* table overflow */
397 }
398 }
399 bit_mask >>= 1;
400 bit_num++;
401 }
402 }
403
404 /* full table? */
405 if (pos == table_mask) return 0;
406
407 /* either erroneous table, or all elements are 0 - let's find out. */
408 for (sym = 0; sym < nsyms; sym++) if (length[sym]) return 1;
409 return 0;
410}
_STLP_MOVE_TO_STD_NAMESPACE void fill(_ForwardIter __first, _ForwardIter __last, const _Tp &__val)
Definition: _algobase.h:449
GLuint GLsizei GLsizei * length
Definition: glext.h:6040

Variable Documentation

◆ extra_bits

const UBYTE extra_bits[51]
static
Initial value:
= {
0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14,
15, 15, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17
}

Definition at line 158 of file lzx.c.

Referenced by LZXdecompress(), LZXfdi_decomp(), LZXfdi_init(), and LZXinit().

◆ position_base

const ULONG position_base[51]
static
Initial value:
= {
0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192,
256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152,
65536, 98304, 131072, 196608, 262144, 393216, 524288, 655360, 786432, 917504, 1048576, 1179648, 1310720, 1441792, 1572864, 1703936,
1835008, 1966080, 2097152
}

Definition at line 165 of file lzx.c.

Referenced by LZXdecompress().