ReactOS 0.4.15-dev-7953-g1f49173
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 window)
 
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 300 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 272 of file lzx.c.

◆ INIT_BITSTREAM

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

Definition at line 270 of file lzx.c.

◆ LENTABLE

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

Definition at line 293 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 291 of file lzx.c.

◆ PEEK_BITS

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

Definition at line 278 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 281 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 309 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 328 of file lzx.c.

◆ REMOVE_BITS

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

Definition at line 279 of file lzx.c.

◆ SYMTABLE

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

Definition at line 292 of file lzx.c.

◆ TABLEBITS

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

Definition at line 290 of file lzx.c.

◆ ULONG_BITS

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

Definition at line 268 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 427 of file lzx.c.

427 {
428 ULONG i,j, x,y;
429 int z;
430
431 register ULONG bitbuf = lb->bb;
432 register int bitsleft = lb->bl;
433 UBYTE *inpos = lb->ip;
434 UWORD *hufftbl;
435
436 for (x = 0; x < 20; x++) {
437 READ_BITS(y, 4);
438 LENTABLE(PRETREE)[x] = y;
439 }
440 BUILD_TABLE(PRETREE);
441
442 for (x = first; x < last; ) {
443 READ_HUFFSYM(PRETREE, z);
444 if (z == 17) {
445 READ_BITS(y, 4); y += 4;
446 while (y--) lens[x++] = 0;
447 }
448 else if (z == 18) {
449 READ_BITS(y, 5); y += 20;
450 while (y--) lens[x++] = 0;
451 }
452 else if (z == 19) {
453 READ_BITS(y, 1); y += 4;
454 READ_HUFFSYM(PRETREE, z);
455 z = lens[x] - z; if (z < 0) z += 17;
456 while (y--) lens[x++] = z;
457 }
458 else {
459 z = lens[x] - z; if (z < 0) z += 17;
460 lens[x++] = z;
461 }
462 }
463
464 lb->bb = bitbuf;
465 lb->bl = bitsleft;
466 lb->ip = inpos;
467 return 0;
468}
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 470 of file lzx.c.

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

◆ LZXinit()

struct LZXstate * LZXinit ( int  window)

alternatively

Definition at line 172 of file lzx.c.

173{
174 struct LZXstate *pState=NULL;
175 ULONG wndsize = 1 << window;
176 int i, posn_slots;
177
178 /* LZX supports window sizes of 2^15 (32Kb) through 2^21 (2Mb) */
179 /* if a previously allocated window is big enough, keep it */
180 if (window < 15 || window > 21) return NULL;
181
182 /* allocate state and associated window */
183 pState = HeapAlloc(GetProcessHeap(), 0, sizeof(struct LZXstate));
184 if (!(pState->window = HeapAlloc(GetProcessHeap(), 0, wndsize)))
185 {
186 HeapFree(GetProcessHeap(), 0, pState);
187 return NULL;
188 }
189 pState->actual_size = wndsize;
190 pState->window_size = wndsize;
191
192 /* calculate required position slots */
193 if (window == 20) posn_slots = 42;
194 else if (window == 21) posn_slots = 50;
195 else posn_slots = window << 1;
196
198 /* posn_slots=i=0; while (i < wndsize) i += 1 << extra_bits[posn_slots++]; */
199
200 /* initialize other state */
201 pState->R0 = pState->R1 = pState->R2 = 1;
202 pState->main_elements = LZX_NUM_CHARS + (posn_slots << 3);
203 pState->header_read = 0;
204 pState->frames_read = 0;
205 pState->block_remaining = 0;
207 pState->intel_curpos = 0;
208 pState->intel_started = 0;
209 pState->window_posn = 0;
210
211 /* initialise tables to 0 (because deltas will be applied to them) */
212 for (i = 0; i < LZX_MAINTREE_MAXSYMBOLS; i++) pState->MAINTREE_len[i] = 0;
213 for (i = 0; i < LZX_LENGTH_MAXSYMBOLS; i++) pState->LENGTH_len[i] = 0;
214
215 return pState;
216}
#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

◆ LZXreset()

int LZXreset ( struct LZXstate pState)

Definition at line 227 of file lzx.c.

228{
229 int i;
230
231 pState->R0 = pState->R1 = pState->R2 = 1;
232 pState->header_read = 0;
233 pState->frames_read = 0;
234 pState->block_remaining = 0;
236 pState->intel_curpos = 0;
237 pState->intel_started = 0;
238 pState->window_posn = 0;
239
240 for (i = 0; i < LZX_MAINTREE_MAXSYMBOLS + LZX_LENTABLE_SAFETY; i++) pState->MAINTREE_len[i] = 0;
241 for (i = 0; i < LZX_LENGTH_MAXSYMBOLS + LZX_LENTABLE_SAFETY; i++) pState->LENGTH_len[i] = 0;
242
243 return DECR_OK;
244}

◆ LZXteardown()

void LZXteardown ( struct LZXstate pState)

Definition at line 218 of file lzx.c.

219{
220 if (pState)
221 {
222 HeapFree(GetProcessHeap(), 0, pState->window);
223 HeapFree(GetProcessHeap(), 0, pState);
224 }
225}

◆ make_decode_table()

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

Definition at line 351 of file lzx.c.

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

◆ 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.