]> wimlib.net Git - wimlib/blob - src/lzx-common.c
win32_wglob(): Improve setting of errno
[wimlib] / src / lzx-common.c
1 /*
2  * lzx-common.c - Common data for LZX compression and decompression.
3  */
4
5 /*
6  * Copyright (C) 2012, 2013 Eric Biggers
7  *
8  * This file is part of wimlib, a library for working with WIM files.
9  *
10  * wimlib is free software; you can redistribute it and/or modify it under the
11  * terms of the GNU General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option)
13  * any later version.
14  *
15  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
16  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
17  * A PARTICULAR PURPOSE. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with wimlib; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "wimlib/lzx.h"
29 #include "wimlib/util.h"
30
31 /* LZX uses what it calls 'position slots' to represent match offsets.
32  * What this means is that a small 'position slot' number and a small
33  * offset from that slot are encoded instead of one large offset for
34  * every match.
35  * - lzx_position_base is an index to the position slot bases
36  * - lzx_extra_bits states how many bits of offset-from-base data is needed.
37  */
38
39 const u32 lzx_position_base[LZX_MAX_POSITION_SLOTS] = {
40         0      , 1      , 2      , 3      , 4      ,    /* 0  --- 4  */
41         6      , 8      , 12     , 16     , 24     ,    /* 5  --- 9  */
42         32     , 48     , 64     , 96     , 128    ,    /* 10 --- 14 */
43         192    , 256    , 384    , 512    , 768    ,    /* 15 --- 19 */
44         1024   , 1536   , 2048   , 3072   , 4096   ,    /* 20 --- 24 */
45         6144   , 8192   , 12288  , 16384  , 24576  ,    /* 25 --- 29 */
46         32768  , 49152  , 65536  , 98304  , 131072 ,    /* 30 --- 34 */
47         196608 , 262144 , 393216 , 524288 , 655360 ,    /* 35 --- 39 */
48         786432 , 917504 , 1048576, 1179648, 1310720,    /* 40 --- 44 */
49         1441792, 1572864, 1703936, 1835008, 1966080,    /* 45 --- 49 */
50         2097152                                         /* 50        */
51 };
52
53 #ifdef USE_LZX_EXTRA_BITS_ARRAY
54 const u8 lzx_extra_bits[LZX_MAX_POSITION_SLOTS] = {
55         0 , 0 , 0 , 0 , 1 ,
56         1 , 2 , 2 , 3 , 3 ,
57         4 , 4 , 5 , 5 , 6 ,
58         6 , 7 , 7 , 8 , 8 ,
59         9 , 9 , 10, 10, 11,
60         11, 12, 12, 13, 13,
61         14, 14, 15, 15, 16,
62         16, 17, 17, 17, 17,
63         17, 17, 17, 17, 17,
64         17, 17, 17, 17, 17,
65         17
66 };
67 #endif
68
69 /* LZX window size must be a power of 2 between 2^15 and 2^21, inclusively.  */
70 bool
71 lzx_window_size_valid(size_t window_size)
72 {
73         if (window_size == 0 || (u32)window_size != window_size)
74                 return false;
75         u32 order = bsr32(window_size);
76         if (window_size != 1U << order)
77                 return false;
78         return (order >= LZX_MIN_WINDOW_ORDER && order <= LZX_MAX_WINDOW_ORDER);
79 }
80
81 /* Given the specified valid window size, return the number of LZX main symbols
82  * that will be needed.  (This depends on the number of position slots, which
83  * itself depends on the window size.)  */
84 unsigned
85 lzx_get_num_main_syms(u32 window_size)
86 {
87         unsigned num_position_slots = lzx_get_position_slot_raw(window_size);
88         return LZX_NUM_CHARS + (num_position_slots << 3);
89 }