Planeshift
xdelta3-hash.h
Go to the documentation of this file.
1 /* xdelta 3 - delta compression tools and library
2  * Copyright (C) 2001, 2003, 2004, 2005, 2006, 2007. Joshua P. MacDonald
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */
18 
19 #ifndef _XDELTA3_HASH_H_
20 #define _XDELTA3_HASH_H_
21 
22 #if XD3_DEBUG
23 #define SMALL_HASH_DEBUG1(s,inp) \
24  usize_t debug_state; \
25  usize_t debug_hval = xd3_checksum_hash (& (s)->small_hash, \
26  xd3_scksum (&debug_state, (inp), (s)->smatcher.small_look))
27 #define SMALL_HASH_DEBUG2(s,inp) \
28  XD3_ASSERT (debug_hval == xd3_checksum_hash (& (s)->small_hash, \
29  xd3_scksum (&debug_state, (inp), (s)->smatcher.small_look)))
30 #else
31 #define SMALL_HASH_DEBUG1(s,inp)
32 #define SMALL_HASH_DEBUG2(s,inp)
33 #endif /* XD3_DEBUG */
34 
35 /* This is a good hash multiplier for 32-bit LCGs: see "linear
36  * congruential generators of different sizes and good lattice
37  * structure" */
38 static const uint32_t hash_multiplier = 1597334677U;
39 
40 /***********************************************************************
41  Permute stuff
42  ***********************************************************************/
43 
44 #if HASH_PERMUTE == 0
45 #define PERMUTE(x) (x)
46 #else
47 #define PERMUTE(x) (__single_hash[(uint32_t)x])
48 
49 static const uint16_t __single_hash[256] =
50 {
51  /* Random numbers generated using SLIB's pseudo-random number generator.
52  * This hashes the input alphabet. */
53  0xbcd1, 0xbb65, 0x42c2, 0xdffe, 0x9666, 0x431b, 0x8504, 0xeb46,
54  0x6379, 0xd460, 0xcf14, 0x53cf, 0xdb51, 0xdb08, 0x12c8, 0xf602,
55  0xe766, 0x2394, 0x250d, 0xdcbb, 0xa678, 0x02af, 0xa5c6, 0x7ea6,
56  0xb645, 0xcb4d, 0xc44b, 0xe5dc, 0x9fe6, 0x5b5c, 0x35f5, 0x701a,
57  0x220f, 0x6c38, 0x1a56, 0x4ca3, 0xffc6, 0xb152, 0x8d61, 0x7a58,
58  0x9025, 0x8b3d, 0xbf0f, 0x95a3, 0xe5f4, 0xc127, 0x3bed, 0x320b,
59  0xb7f3, 0x6054, 0x333c, 0xd383, 0x8154, 0x5242, 0x4e0d, 0x0a94,
60  0x7028, 0x8689, 0x3a22, 0x0980, 0x1847, 0xb0f1, 0x9b5c, 0x4176,
61  0xb858, 0xd542, 0x1f6c, 0x2497, 0x6a5a, 0x9fa9, 0x8c5a, 0x7743,
62  0xa8a9, 0x9a02, 0x4918, 0x438c, 0xc388, 0x9e2b, 0x4cad, 0x01b6,
63  0xab19, 0xf777, 0x365f, 0x1eb2, 0x091e, 0x7bf8, 0x7a8e, 0x5227,
64  0xeab1, 0x2074, 0x4523, 0xe781, 0x01a3, 0x163d, 0x3b2e, 0x287d,
65  0x5e7f, 0xa063, 0xb134, 0x8fae, 0x5e8e, 0xb7b7, 0x4548, 0x1f5a,
66  0xfa56, 0x7a24, 0x900f, 0x42dc, 0xcc69, 0x02a0, 0x0b22, 0xdb31,
67  0x71fe, 0x0c7d, 0x1732, 0x1159, 0xcb09, 0xe1d2, 0x1351, 0x52e9,
68  0xf536, 0x5a4f, 0xc316, 0x6bf9, 0x8994, 0xb774, 0x5f3e, 0xf6d6,
69  0x3a61, 0xf82c, 0xcc22, 0x9d06, 0x299c, 0x09e5, 0x1eec, 0x514f,
70  0x8d53, 0xa650, 0x5c6e, 0xc577, 0x7958, 0x71ac, 0x8916, 0x9b4f,
71  0x2c09, 0x5211, 0xf6d8, 0xcaaa, 0xf7ef, 0x287f, 0x7a94, 0xab49,
72  0xfa2c, 0x7222, 0xe457, 0xd71a, 0x00c3, 0x1a76, 0xe98c, 0xc037,
73  0x8208, 0x5c2d, 0xdfda, 0xe5f5, 0x0b45, 0x15ce, 0x8a7e, 0xfcad,
74  0xaa2d, 0x4b5c, 0xd42e, 0xb251, 0x907e, 0x9a47, 0xc9a6, 0xd93f,
75  0x085e, 0x35ce, 0xa153, 0x7e7b, 0x9f0b, 0x25aa, 0x5d9f, 0xc04d,
76  0x8a0e, 0x2875, 0x4a1c, 0x295f, 0x1393, 0xf760, 0x9178, 0x0f5b,
77  0xfa7d, 0x83b4, 0x2082, 0x721d, 0x6462, 0x0368, 0x67e2, 0x8624,
78  0x194d, 0x22f6, 0x78fb, 0x6791, 0xb238, 0xb332, 0x7276, 0xf272,
79  0x47ec, 0x4504, 0xa961, 0x9fc8, 0x3fdc, 0xb413, 0x007a, 0x0806,
80  0x7458, 0x95c6, 0xccaa, 0x18d6, 0xe2ae, 0x1b06, 0xf3f6, 0x5050,
81  0xc8e8, 0xf4ac, 0xc04c, 0xf41c, 0x992f, 0xae44, 0x5f1b, 0x1113,
82  0x1738, 0xd9a8, 0x19ea, 0x2d33, 0x9698, 0x2fe9, 0x323f, 0xcde2,
83  0x6d71, 0xe37d, 0xb697, 0x2c4f, 0x4373, 0x9102, 0x075d, 0x8e25,
84  0x1672, 0xec28, 0x6acb, 0x86cc, 0x186e, 0x9414, 0xd674, 0xd1a5
85 };
86 #endif
87 
88 /* Update the checksum state. */
89 #if ADLER_LARGE_CKSUM
90 inline uint32_t
91 xd3_large_cksum_update (uint32_t cksum,
92  const uint8_t *base,
93  usize_t look) {
94  uint32_t old_c = PERMUTE(base[0]);
95  uint32_t new_c = PERMUTE(base[look]);
96  uint32_t low = ((cksum & 0xffff) - old_c + new_c) & 0xffff;
97  uint32_t high = ((cksum >> 16) - (old_c * look) + low) & 0xffff;
98  return (high << 16) | low;
99 }
100 #else
101 // TODO: revisit this topic
102 #endif
103 
104 /* Note: small cksum is hard-coded for 4 bytes */
105 #if UNALIGNED_OK
106 static inline uint32_t
107 xd3_scksum (uint32_t *state,
108  const uint8_t *base,
109  const usize_t look)
110 {
111  (*state) = *(uint32_t*)base;
112  return (*state) * hash_multiplier;
113 }
114 static inline uint32_t
115 xd3_small_cksum_update (uint32_t *state,
116  const uint8_t *base,
117  usize_t look)
118 {
119  (*state) = *(uint32_t*)(base+1);
120  return (*state) * hash_multiplier;
121 }
122 #else
123 static inline uint32_t
124 xd3_scksum (uint32_t *state,
125  const uint8_t *base,
126  const usize_t look)
127 {
128  (*state) = (base[0] << 24 |
129  base[1] << 16 |
130  base[2] << 8 |
131  base[3]);
132  return (*state) * hash_multiplier;
133 }
134 static inline uint32_t
135 xd3_small_cksum_update (uint32_t *state,
136  const uint8_t *base,
137  const usize_t look)
138 {
139  (*state) <<= 8;
140  (*state) |= base[4];
141  return (*state) * hash_multiplier;
142 }
143 #endif
144 
145 /***********************************************************************
146  Ctable stuff
147  ***********************************************************************/
148 
149 static inline usize_t
150 xd3_checksum_hash (const xd3_hash_cfg *cfg, const usize_t cksum)
151 {
152  return (cksum >> cfg->shift) ^ (cksum & cfg->mask);
153 }
154 
155 /***********************************************************************
156  Cksum function
157  ***********************************************************************/
158 
159 #if ADLER_LARGE_CKSUM
160 static inline uint32_t
161 xd3_lcksum (const uint8_t *seg, const usize_t ln)
162 {
163  usize_t i = 0;
164  uint32_t low = 0;
165  uint32_t high = 0;
166 
167  for (; i < ln; i += 1)
168  {
169  low += PERMUTE(*seg++);
170  high += low;
171  }
172 
173  return ((high & 0xffff) << 16) | (low & 0xffff);
174 }
175 #else
176 static inline uint32_t
177 xd3_lcksum (const uint8_t *seg, const usize_t ln)
178 {
179  usize_t i, j;
180  uint32_t h = 0;
181  for (i = 0, j = ln - 1; i < ln; ++i, --j) {
182  h += PERMUTE(seg[i]) * hash_multiplier_powers[j];
183  }
184  return h;
185 }
186 #endif
187 
188 #if XD3_ENCODER
189 static usize_t
190 xd3_size_log2 (usize_t slots)
191 {
192  int bits = 28; /* This should not be an unreasonable limit. */
193  int i;
194 
195  for (i = 3; i <= bits; i += 1)
196  {
197  if (slots < (1U << i))
198  {
199  /* TODO: this is compaction=1 in checksum_test.cc and maybe should
200  * not be fixed at -1. */
201  bits = i - 1;
202  break;
203  }
204  }
205 
206  return bits;
207 }
208 
209 static void
210 xd3_size_hashtable (xd3_stream *stream,
211  usize_t slots,
212  xd3_hash_cfg *cfg)
213 {
214  int bits = xd3_size_log2 (slots);
215 
216  /* TODO: there's a 32-bit assumption here */
217  cfg->size = (1 << bits);
218  cfg->mask = (cfg->size - 1);
219  cfg->shift = 32 - bits;
220 }
221 #endif
222 
223 #endif
static uint32_t xd3_lcksum(const uint8_t *seg, const usize_t ln)
Definition: xdelta3-hash.h:177
usize_t size
Definition: xdelta3.h:639
static usize_t xd3_checksum_hash(const xd3_hash_cfg *cfg, const usize_t cksum)
Definition: xdelta3-hash.h:150
#define PERMUTE(x)
Definition: xdelta3-hash.h:45
unsigned int usize_t
Definition: xdelta3.h:102
static uint32_t xd3_scksum(uint32_t *state, const uint8_t *base, const usize_t look)
Definition: xdelta3-hash.h:124
static const uint32_t hash_multiplier
Definition: xdelta3-hash.h:38
usize_t shift
Definition: xdelta3.h:640
static uint32_t xd3_small_cksum_update(uint32_t *state, const uint8_t *base, const usize_t look)
Definition: xdelta3-hash.h:135
usize_t mask
Definition: xdelta3.h:641