]> wimlib.net Git - wimlib/blobdiff - src/lzx.h
bitstream_read_bits(): Remove bogus assertion
[wimlib] / src / lzx.h
index 2e89ec986660a3cfa31ed16317d87068ce155eb2..8a3968920f78d93c52c62c679ef4dca60a84bad5 100644 (file)
--- a/src/lzx.h
+++ b/src/lzx.h
@@ -1,24 +1,3 @@
-/*
- * lzx.h
- *
- * Copyright (C) 2012 Eric Biggers
- *
- * wimlib - Library for working with WIM files 
- *
- * This library is free software; you can redistribute it and/or modify it under
- * the terms of the GNU Lesser General Public License as published by the Free
- * Software Foundation; either version 2.1 of the License, or (at your option) any
- * later version.
- *
- * This library is distributed in the hope that it will be useful, but WITHOUT ANY
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License along
- * with this library; if not, write to the Free Software Foundation, Inc., 59
- * Temple Place, Suite 330, Boston, MA 02111-1307 USA 
- */
-
 #ifndef _WIMLIB_LZX_H
 #define _WIMLIB_LZX_H
 
@@ -31,8 +10,7 @@
 #      define LZX_DEBUG(format, ...)
 #endif
 
-
-/* Constants, some defined by the LZX specification: */
+/* Constants, most of which are defined by the LZX specification: */
 
 /* The smallest and largest allowed match lengths. */
 #define LZX_MIN_MATCH                2
 /* Number of values an uncompressed literal byte can represent. */
 #define LZX_NUM_CHARS                256
 
-/* Each LZX block begins with 3 bits that determines the block type: */
+/* Each LZX block begins with 3 bits that determines the block type.  Below are
+ * the valid block types.  Values 0, and 4 through 7, are invalid. */
 #define LZX_BLOCKTYPE_VERBATIM       1
 #define LZX_BLOCKTYPE_ALIGNED        2
 #define LZX_BLOCKTYPE_UNCOMPRESSED   3
-/* values 0, and 4 through 7, are invalid. */
-
 
 #define LZX_NUM_PRIMARY_LENS         7 /* this one missing from spec! */
 
-/* Only valid for 32768 block size! */
+/* NOTE: There are really 51 position slots in the LZX format as a whole, but
+ * only 30 are needed to allow for the window to be up to 32768 bytes long,
+ * which is the maximum in the WIM format. */
 #define LZX_NUM_POSITION_SLOTS       30
 
 /* Read the LZX specification for information about the Huffman trees used in
@@ -61,7 +40,7 @@
  * read_code_lens() function and built using the make_decode_table() function.
  * The decode table is not a real tree but rather a table that we can index by
  * some number of bits (*_TABLEBITS) of the input to quickly look up the symbol
- * corresponding to a Huffman code. 
+ * corresponding to a Huffman code.
  *
  * The ALIGNED tree is only present on ALIGNED blocks.
  *
 
 #define LZX_MAINTREE_NUM_SYMBOLS        (LZX_NUM_CHARS + \
                                        (LZX_NUM_POSITION_SLOTS << 3))
-#define LZX_MAINTREE_TABLEBITS         12
+#define LZX_MAINTREE_TABLEBITS         11
 
 #define LZX_LENTREE_NUM_SYMBOLS                249
-#define LZX_LENTREE_TABLEBITS          12
+#define LZX_LENTREE_TABLEBITS          10
 
 #define LZX_PRETREE_NUM_SYMBOLS                20
 #define LZX_PRETREE_TABLEBITS          6
 #define LZX_PRETREE_ELEMENT_SIZE       4
 
-
 #define LZX_ALIGNEDTREE_NUM_SYMBOLS    8
 #define LZX_ALIGNEDTREE_TABLEBITS      7
 #define LZX_ALIGNEDTREE_ELEMENT_SIZE   3
  * though the blocks themselves are not this size, and the size of the actual
  * file resource in the WIM file is very likely to be something entirely
  * different as well.  */
-#define LZX_MAGIC_FILESIZE           12000000
+#define LZX_WIM_MAGIC_FILESIZE         12000000
+
+#define USE_LZX_EXTRA_BITS_ARRAY
+
+#ifdef USE_LZX_EXTRA_BITS_ARRAY
+extern const u8 lzx_extra_bits[LZX_NUM_POSITION_SLOTS];
+#endif
+
+/* Given the number of a LZX position slot, return the number of extra bits that
+ * are needed to encode the match offset. */
+static inline unsigned lzx_get_num_extra_bits(unsigned position_slot)
+{
+#ifdef USE_LZX_EXTRA_BITS_ARRAY
+       /* Use a table */
+       return lzx_extra_bits[position_slot];
+#else
+       /* Calculate directly using a shift and subtraction. */
+       wimlib_assert(position_slot >= 2 && position_slot <= 37);
+       return (position_slot >> 1) - 1;
+#endif
+}
 
-extern const u8 lzx_extra_bits[51];
-extern const u32 lzx_position_base[51];
+extern const u32 lzx_position_base[LZX_NUM_POSITION_SLOTS];
 
 /* Least-recently used queue for match offsets. */
 struct lru_queue {
-       int R0;
-       int R1;
-       int R2;
+       u32 R0;
+       u32 R1;
+       u32 R2;
 };
 
-extern int lzx_decompress(const void *compressed_data, uint compressed_len, 
-                         void *uncompressed_data, uint uncompressed_len);
+extern int lzx_decompress(const void *compressed_data, unsigned compressed_len,
+                         void *uncompressed_data, unsigned uncompressed_len);
 
-extern int lzx_compress(const void *uncompressed_data, uint uncompressed_len,
-                       void *compressed_data, uint *compressed_len_ret);
+extern int lzx_compress(const void *uncompressed_data, unsigned uncompressed_len,
+                       void *compressed_data, unsigned *compressed_len_ret);
 
 #endif /* _WIMLIB_LZX_H */