]> wimlib.net Git - wimlib/blobdiff - include/wimlib/decompress_common.h
Get rid of input_idx_t
[wimlib] / include / wimlib / decompress_common.h
index 9efd0ef3b55b019cb34cd811d9e3708c8ca1a027..fab3c3e009413c4ef9ed4b9f0a4a6a3a672cc1a9 100644 (file)
@@ -2,6 +2,9 @@
  * decompress_common.h
  *
  * Header for decompression code shared by multiple compression formats.
+ *
+ * The author dedicates this file to the public domain.
+ * You can do whatever you want with this file.
  */
 
 #ifndef _WIMLIB_DECOMPRESS_COMMON_H
@@ -9,15 +12,9 @@
 
 #include "wimlib/assert.h"
 #include "wimlib/compiler.h"
-#include "wimlib/error.h"
 #include "wimlib/endianness.h"
 #include "wimlib/types.h"
 
-#ifndef INPUT_IDX_T_DEFINED
-#define INPUT_IDX_T_DEFINED
-typedef u32 input_idx_t;
-#endif
-
 /* Structure to encapsulate a block of in-memory data that is being interpreted
  * as a stream of bits.
  *
@@ -37,13 +34,13 @@ struct input_bitstream {
        const u8 *data;
 
        /* Number of bytes of data that are left.  */
-       input_idx_t data_bytes_left;
+       u32 data_bytes_left;
 };
 
 /* Initializes a bitstream to receive its input from @data. */
 static inline void
 init_input_bitstream(struct input_bitstream *istream,
-                    const void *data, input_idx_t num_data_bytes)
+                    const void *data, u32 num_data_bytes)
 {
        istream->bitbuf          = 0;
        istream->bitsleft        = 0;
@@ -200,4 +197,56 @@ make_huffman_decode_table(u16 decode_table[], unsigned num_syms,
                          unsigned num_bits, const u8 lens[],
                          unsigned max_codeword_len);
 
-#endif /* _WIMLIB_DECOMPRESS_H */
+
+/*
+ * Copy a LZ77 match at (dst - offset) to dst.
+ *
+ * The length and offset must be already validated --- that is, (dst - offset)
+ * can't underrun the output buffer, and (dst + length) can't overrun the output
+ * buffer.  Also, the length cannot be 0.
+ *
+ * @winend points to the byte past the end of the output buffer.
+ * This function won't write any data beyond this position.
+ */
+static inline void
+lz_copy(u8 *dst, unsigned length, unsigned offset, const u8 *winend)
+{
+       const u8 *src = dst - offset;
+#if defined(__x86_64__) || defined(__i386__)
+       /* Copy one 'unsigned long' at a time.  On i386 and x86_64 this is
+        * faster than copying one byte at a time, unless the data is
+        * near-random and all the matches have very short lengths.  Note that
+        * since this requires unaligned memory accesses, it won't necessarily
+        * be faster on every architecture.
+        *
+        * Also note that we might copy more than the length of the match.  For
+        * example, if an 'unsigned long' is 8 bytes and the match is of length
+        * 5, then we'll simply copy 8 bytes.  This is okay as long as we don't
+        * write beyond the end of the output buffer, hence the check for
+        * (winend - (dst + length) >= sizeof(unsigned long) - 1).  */
+       if (offset >= sizeof(unsigned long) &&
+                       winend - (dst + length) >= sizeof(unsigned long) - 1)
+       {
+               /* Access memory through a packed struct.  This tricks the
+                * compiler into allowing unaligned memory accesses.  */
+               struct ulong_wrapper {
+                       unsigned long v;
+               } _packed_attribute;
+
+               const u8 *end = dst + length;
+               do {
+                       unsigned long v = ((struct ulong_wrapper *)src)->v;
+                       ((struct ulong_wrapper *)dst)->v = v;
+                       dst += sizeof(unsigned long);
+                       src += sizeof(unsigned long);
+               } while (dst < end);
+
+               return;
+       }
+#endif
+       do {
+               *dst++ = *src++;
+       } while (--length);
+}
+
+#endif /* _WIMLIB_DECOMPRESS_COMMON_H */