]> wimlib.net Git - wimlib/blobdiff - src/lzx_common.c
Introduce ilog2_ceil()
[wimlib] / src / lzx_common.c
index 1baeebe7469ed2fb585d53e7c8dd3e761bb1faa3..7925f1f197b6803ce31bca7fed7564302f6220e1 100644 (file)
@@ -3,7 +3,7 @@
  */
 
 /*
- * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers
+ * Copyright (C) 2012-2016 Eric Biggers
  *
  * This file 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
@@ -76,21 +76,16 @@ const u8 lzx_extra_offset_bits[LZX_MAX_OFFSET_SLOTS] = {
 unsigned
 lzx_get_window_order(size_t max_bufsize)
 {
-       unsigned order;
-
        if (max_bufsize == 0 || max_bufsize > LZX_MAX_WINDOW_SIZE)
                return 0;
 
-       order = fls32(max_bufsize);
-
-       if (((u32)1 << order) != max_bufsize)
-               order++;
-
-       return max(order, LZX_MIN_WINDOW_ORDER);
+       return max(ilog2_ceil(max_bufsize), LZX_MIN_WINDOW_ORDER);
 }
 
+/* Given a valid LZX window order, return the number of symbols that will exist
+ * in the main Huffman code.  */
 unsigned
-lzx_get_num_offset_slots(unsigned window_order)
+lzx_get_num_main_syms(unsigned window_order)
 {
        /* Note: one would expect that the maximum match offset would be
         * 'window_size - LZX_MIN_MATCH_LEN', which would occur if the first two
@@ -98,17 +93,13 @@ lzx_get_num_offset_slots(unsigned window_order)
         * disallows this case.  This reduces the number of needed offset slots
         * by 1.  */
        u32 window_size = (u32)1 << window_order;
-       u32 max_offset = window_size - LZX_MIN_MATCH_LEN - 1;
-       return 1 + lzx_get_offset_slot(max_offset);
-}
+       u32 max_adjusted_offset = (window_size - LZX_MIN_MATCH_LEN - 1) +
+                                 LZX_OFFSET_ADJUSTMENT;
+       unsigned num_offset_slots = 30;
+       while (max_adjusted_offset >= lzx_offset_slot_base[num_offset_slots])
+               num_offset_slots++;
 
-/* Given a valid LZX window order, return the number of symbols that will exist
- * in the main Huffman code.  */
-unsigned
-lzx_get_num_main_syms(unsigned window_order)
-{
-       return LZX_NUM_CHARS + (lzx_get_num_offset_slots(window_order) *
-                               LZX_NUM_LEN_HEADERS);
+       return LZX_NUM_CHARS + (num_offset_slots * LZX_NUM_LEN_HEADERS);
 }
 
 static void
@@ -116,7 +107,7 @@ do_translate_target(void *target, s32 input_pos)
 {
        s32 abs_offset, rel_offset;
 
-       rel_offset = get_unaligned_u32_le(target);
+       rel_offset = get_unaligned_le32(target);
        if (rel_offset >= -input_pos && rel_offset < LZX_WIM_MAGIC_FILESIZE) {
                if (rel_offset < LZX_WIM_MAGIC_FILESIZE - input_pos) {
                        /* "good translation" */
@@ -125,7 +116,7 @@ do_translate_target(void *target, s32 input_pos)
                        /* "compensating translation" */
                        abs_offset = rel_offset - LZX_WIM_MAGIC_FILESIZE;
                }
-               put_unaligned_u32_le(abs_offset, target);
+               put_unaligned_le32(abs_offset, target);
        }
 }
 
@@ -134,18 +125,18 @@ undo_translate_target(void *target, s32 input_pos)
 {
        s32 abs_offset, rel_offset;
 
-       abs_offset = get_unaligned_u32_le(target);
+       abs_offset = get_unaligned_le32(target);
        if (abs_offset >= 0) {
                if (abs_offset < LZX_WIM_MAGIC_FILESIZE) {
                        /* "good translation" */
                        rel_offset = abs_offset - input_pos;
-                       put_unaligned_u32_le(rel_offset, target);
+                       put_unaligned_le32(rel_offset, target);
                }
        } else {
                if (abs_offset >= -input_pos) {
                        /* "compensating translation" */
                        rel_offset = abs_offset + LZX_WIM_MAGIC_FILESIZE;
-                       put_unaligned_u32_le(rel_offset, target);
+                       put_unaligned_le32(rel_offset, target);
                }
        }
 }
@@ -325,13 +316,13 @@ lzx_e8_filter(u8 *data, u32 size, void (*process_target)(void *, s32))
 }
 
 void
-lzx_do_e8_preprocessing(u8 *data, u32 size)
+lzx_preprocess(u8 *data, u32 size)
 {
        lzx_e8_filter(data, size, do_translate_target);
 }
 
 void
-lzx_undo_e8_preprocessing(u8 *data, u32 size)
+lzx_postprocess(u8 *data, u32 size)
 {
        lzx_e8_filter(data, size, undo_translate_target);
 }