Coverage Report

Created: 2026-06-30 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/json-c/linkhash.c
Line
Count
Source
1
/*
2
 * $Id: linkhash.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
3
 *
4
 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5
 * Michael Clark <michael@metaparadigm.com>
6
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
7
 *
8
 * This library is free software; you can redistribute it and/or modify
9
 * it under the terms of the MIT license. See COPYING for details.
10
 *
11
 */
12
13
#include "config.h"
14
15
#include <assert.h>
16
#include <limits.h>
17
#include <stdarg.h>
18
#include <stddef.h>
19
#include <stdio.h>
20
#include <stdlib.h>
21
#include <string.h>
22
23
#ifdef HAVE_ENDIAN_H
24
#include <endian.h> /* attempt to define endianness */
25
#endif
26
27
#if defined(_MSC_VER) || defined(__MINGW32__)
28
#define WIN32_LEAN_AND_MEAN
29
#include <windows.h> /* Get InterlockedCompareExchange */
30
#endif
31
32
#include "linkhash.h"
33
#include "random_seed.h"
34
35
/* hash functions */
36
static unsigned long lh_char_hash(const void *k);
37
static unsigned long lh_perllike_str_hash(const void *k);
38
static lh_hash_fn *char_hash_fn = lh_char_hash;
39
40
/* comparison functions */
41
int lh_char_equal(const void *k1, const void *k2);
42
int lh_ptr_equal(const void *k1, const void *k2);
43
44
int json_global_set_string_hash(const int h)
45
0
{
46
0
  switch (h)
47
0
  {
48
0
  case JSON_C_STR_HASH_DFLT: char_hash_fn = lh_char_hash; break;
49
0
  case JSON_C_STR_HASH_PERLLIKE: char_hash_fn = lh_perllike_str_hash; break;
50
0
  default: return -1;
51
0
  }
52
0
  return 0;
53
0
}
54
55
static unsigned long lh_ptr_hash(const void *k)
56
0
{
57
  /* CAW: refactored to be 64bit nice */
58
0
  return (unsigned long)((((ptrdiff_t)k * LH_PRIME) >> 4) & ULONG_MAX);
59
0
}
60
61
int lh_ptr_equal(const void *k1, const void *k2)
62
0
{
63
0
  return (k1 == k2);
64
0
}
65
66
/*
67
 * hashlittle from lookup3.c, by Bob Jenkins, May 2006, Public Domain.
68
 * https://burtleburtle.net/bob/c/lookup3.c
69
 * minor modifications to make functions static so no symbols are exported
70
 * minor modifications to compile with -Werror
71
 */
72
73
/*
74
-------------------------------------------------------------------------------
75
lookup3.c, by Bob Jenkins, May 2006, Public Domain.
76
77
These are functions for producing 32-bit hashes for hash table lookup.
78
hashword(), hashlittle(), hashlittle2(), hashbig(), mix(), and final()
79
are externally useful functions.  Routines to test the hash are included
80
if SELF_TEST is defined.  You can use this free for any purpose.  It's in
81
the public domain.  It has no warranty.
82
83
You probably want to use hashlittle().  hashlittle() and hashbig()
84
hash byte arrays.  hashlittle() is faster than hashbig() on
85
little-endian machines.  Intel and AMD are little-endian machines.
86
On second thought, you probably want hashlittle2(), which is identical to
87
hashlittle() except it returns two 32-bit hashes for the price of one.
88
You could implement hashbig2() if you wanted but I haven't bothered here.
89
90
If you want to find a hash of, say, exactly 7 integers, do
91
  a = i1;  b = i2;  c = i3;
92
  mix(a,b,c);
93
  a += i4; b += i5; c += i6;
94
  mix(a,b,c);
95
  a += i7;
96
  final(a,b,c);
97
then use c as the hash value.  If you have a variable length array of
98
4-byte integers to hash, use hashword().  If you have a byte array (like
99
a character string), use hashlittle().  If you have several byte arrays, or
100
a mix of things, see the comments above hashlittle().
101
102
Why is this so big?  I read 12 bytes at a time into 3 4-byte integers,
103
then mix those integers.  This is fast (you can do a lot more thorough
104
mixing with 12*3 instructions on 3 integers than you can with 3 instructions
105
on 1 byte), but shoehorning those bytes into integers efficiently is messy.
106
-------------------------------------------------------------------------------
107
*/
108
109
/*
110
 * My best guess at if you are big-endian or little-endian.  This may
111
 * need adjustment.
112
 */
113
#if (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN) || \
114
    (defined(i386) || defined(__i386__) || defined(__i486__) || defined(__i586__) ||          \
115
     defined(__i686__) || defined(vax) || defined(MIPSEL))
116
51.7k
#define HASH_LITTLE_ENDIAN 1
117
#define HASH_BIG_ENDIAN 0
118
#elif (defined(__BYTE_ORDER) && defined(__BIG_ENDIAN) && __BYTE_ORDER == __BIG_ENDIAN) || \
119
    (defined(sparc) || defined(POWERPC) || defined(mc68000) || defined(sel))
120
#define HASH_LITTLE_ENDIAN 0
121
#define HASH_BIG_ENDIAN 1
122
#else
123
#define HASH_LITTLE_ENDIAN 0
124
#define HASH_BIG_ENDIAN 0
125
#endif
126
127
#define hashsize(n) ((uint32_t)1 << (n))
128
#define hashmask(n) (hashsize(n) - 1)
129
216k
#define rot(x, k) (((x) << (k)) | ((x) >> (32 - (k))))
130
131
/*
132
-------------------------------------------------------------------------------
133
mix -- mix 3 32-bit values reversibly.
134
135
This is reversible, so any information in (a,b,c) before mix() is
136
still in (a,b,c) after mix().
137
138
If four pairs of (a,b,c) inputs are run through mix(), or through
139
mix() in reverse, there are at least 32 bits of the output that
140
are sometimes the same for one pair and different for another pair.
141
This was tested for:
142
* pairs that differed by one bit, by two bits, in any combination
143
  of top bits of (a,b,c), or in any combination of bottom bits of
144
  (a,b,c).
145
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
146
  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
147
  is commonly produced by subtraction) look like a single 1-bit
148
  difference.
149
* the base values were pseudorandom, all zero but one bit set, or
150
  all zero plus a counter that starts at zero.
151
152
Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
153
satisfy this are
154
    4  6  8 16 19  4
155
    9 15  3 18 27 15
156
   14  9  3  7 17  3
157
Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing
158
for "differ" defined as + with a one-bit base and a two-bit delta.  I
159
used https://burtleburtle.net/bob/hash/avalanche.html to choose
160
the operations, constants, and arrangements of the variables.
161
162
This does not achieve avalanche.  There are input bits of (a,b,c)
163
that fail to affect some output bits of (a,b,c), especially of a.  The
164
most thoroughly mixed value is c, but it doesn't really even achieve
165
avalanche in c.
166
167
This allows some parallelism.  Read-after-writes are good at doubling
168
the number of bits affected, so the goal of mixing pulls in the opposite
169
direction as the goal of parallelism.  I did what I could.  Rotates
170
seem to cost as much as shifts on every machine I could lay my hands
171
on, and rotates are much kinder to the top and bottom bits, so I used
172
rotates.
173
-------------------------------------------------------------------------------
174
*/
175
/* clang-format off */
176
8.89k
#define mix(a,b,c) \
177
8.89k
{ \
178
8.89k
  a -= c;  a ^= rot(c, 4);  c += b; \
179
8.89k
  b -= a;  b ^= rot(a, 6);  a += c; \
180
8.89k
  c -= b;  c ^= rot(b, 8);  b += a; \
181
8.89k
  a -= c;  a ^= rot(c,16);  c += b; \
182
8.89k
  b -= a;  b ^= rot(a,19);  a += c; \
183
8.89k
  c -= b;  c ^= rot(b, 4);  b += a; \
184
8.89k
}
185
/* clang-format on */
186
187
/*
188
-------------------------------------------------------------------------------
189
final -- final mixing of 3 32-bit values (a,b,c) into c
190
191
Pairs of (a,b,c) values differing in only a few bits will usually
192
produce values of c that look totally different.  This was tested for
193
* pairs that differed by one bit, by two bits, in any combination
194
  of top bits of (a,b,c), or in any combination of bottom bits of
195
  (a,b,c).
196
* "differ" is defined as +, -, ^, or ~^.  For + and -, I transformed
197
  the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
198
  is commonly produced by subtraction) look like a single 1-bit
199
  difference.
200
* the base values were pseudorandom, all zero but one bit set, or
201
  all zero plus a counter that starts at zero.
202
203
These constants passed:
204
 14 11 25 16 4 14 24
205
 12 14 25 16 4 14 24
206
and these came close:
207
  4  8 15 26 3 22 24
208
 10  8 15 26 3 22 24
209
 11  8 15 26 3 22 24
210
-------------------------------------------------------------------------------
211
*/
212
/* clang-format off */
213
23.2k
#define final(a,b,c) \
214
23.2k
{ \
215
23.2k
  c ^= b; c -= rot(b,14); \
216
23.2k
  a ^= c; a -= rot(c,11); \
217
23.2k
  b ^= a; b -= rot(a,25); \
218
23.2k
  c ^= b; c -= rot(b,16); \
219
23.2k
  a ^= c; a -= rot(c,4);  \
220
23.2k
  b ^= a; b -= rot(a,14); \
221
23.2k
  c ^= b; c -= rot(b,24); \
222
23.2k
}
223
/* clang-format on */
224
225
/*
226
-------------------------------------------------------------------------------
227
hashlittle() -- hash a variable-length key into a 32-bit value
228
  k       : the key (the unaligned variable-length array of bytes)
229
  length  : the length of the key, counting by bytes
230
  initval : can be any 4-byte value
231
Returns a 32-bit value.  Every bit of the key affects every bit of
232
the return value.  Two keys differing by one or two bits will have
233
totally different hash values.
234
235
The best hash table sizes are powers of 2.  There is no need to do
236
mod a prime (mod is sooo slow!).  If you need less than 32 bits,
237
use a bitmask.  For example, if you need only 10 bits, do
238
  h = (h & hashmask(10));
239
In which case, the hash table should have hashsize(10) elements.
240
241
If you are hashing n strings (uint8_t **)k, do it like this:
242
  for (i=0, h=0; i<n; ++i) h = hashlittle( k[i], len[i], h);
243
244
By Bob Jenkins, 2006.  bob_jenkins@burtleburtle.net.  You may use this
245
code any way you wish, private, educational, or commercial.  It's free.
246
247
Use for hash table lookup, or anything where one collision in 2^^32 is
248
acceptable.  Do NOT use for cryptographic purposes.
249
-------------------------------------------------------------------------------
250
*/
251
252
/* clang-format off */
253
static uint32_t hashlittle(const void *key, size_t length, uint32_t initval)
254
25.8k
{
255
25.8k
  uint32_t a,b,c; /* internal state */
256
25.8k
  union
257
25.8k
  {
258
25.8k
    const void *ptr;
259
25.8k
    size_t i;
260
25.8k
  } u; /* needed for Mac Powerbook G4 */
261
262
  /* Set up the internal state */
263
25.8k
  a = b = c = 0xdeadbeef + ((uint32_t)length) + initval;
264
265
25.8k
  u.ptr = key;
266
25.8k
  if (HASH_LITTLE_ENDIAN && ((u.i & 0x3) == 0)) {
267
25.8k
    const uint32_t *k = (const uint32_t *)key; /* read 32-bit chunks */
268
269
    /*------ all but last block: aligned reads and affect 32 bits of (a,b,c) */
270
34.7k
    while (length > 12)
271
8.89k
    {
272
8.89k
      a += k[0];
273
8.89k
      b += k[1];
274
8.89k
      c += k[2];
275
8.89k
      mix(a,b,c);
276
8.89k
      length -= 12;
277
8.89k
      k += 3;
278
8.89k
    }
279
280
    /*----------------------------- handle the last (probably partial) block */
281
    /*
282
     * "k[2]&0xffffff" actually reads beyond the end of the string, but
283
     * then masks off the part it's not allowed to read.  Because the
284
     * string is aligned, the masked-off tail is in the same word as the
285
     * rest of the string.  Every machine with memory protection I've seen
286
     * does it on word boundaries, so is OK with this.  But VALGRIND will
287
     * still catch it and complain.  The masking trick does make the hash
288
     * noticeably faster for short strings (like English words).
289
     * AddressSanitizer is similarly picky about overrunning
290
     * the buffer. (https://clang.llvm.org/docs/AddressSanitizer.html)
291
     */
292
#ifdef VALGRIND
293
#define PRECISE_MEMORY_ACCESS 1
294
#elif defined(__SANITIZE_ADDRESS__) /* GCC's ASAN */
295
#define PRECISE_MEMORY_ACCESS 1
296
#elif defined(__has_feature)
297
#if __has_feature(address_sanitizer) /* Clang's ASAN */
298
#define PRECISE_MEMORY_ACCESS 1
299
#endif
300
25.8k
#endif
301
25.8k
#ifndef PRECISE_MEMORY_ACCESS
302
303
25.8k
    switch(length)
304
25.8k
    {
305
679
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
306
893
    case 11: c+=k[2]&0xffffff; b+=k[1]; a+=k[0]; break;
307
931
    case 10: c+=k[2]&0xffff; b+=k[1]; a+=k[0]; break;
308
1.10k
    case 9 : c+=k[2]&0xff; b+=k[1]; a+=k[0]; break;
309
1.44k
    case 8 : b+=k[1]; a+=k[0]; break;
310
1.30k
    case 7 : b+=k[1]&0xffffff; a+=k[0]; break;
311
1.96k
    case 6 : b+=k[1]&0xffff; a+=k[0]; break;
312
2.23k
    case 5 : b+=k[1]&0xff; a+=k[0]; break;
313
2.26k
    case 4 : a+=k[0]; break;
314
2.09k
    case 3 : a+=k[0]&0xffffff; break;
315
3.73k
    case 2 : a+=k[0]&0xffff; break;
316
4.65k
    case 1 : a+=k[0]&0xff; break;
317
2.56k
    case 0 : return c; /* zero length strings require no mixing */
318
25.8k
    }
319
320
#else /* make valgrind happy */
321
322
    const uint8_t  *k8 = (const uint8_t *)k;
323
    switch(length)
324
    {
325
    case 12: c+=k[2]; b+=k[1]; a+=k[0]; break;
326
    case 11: c+=((uint32_t)k8[10])<<16;  /* fall through */
327
    case 10: c+=((uint32_t)k8[9])<<8;    /* fall through */
328
    case 9 : c+=k8[8];                   /* fall through */
329
    case 8 : b+=k[1]; a+=k[0]; break;
330
    case 7 : b+=((uint32_t)k8[6])<<16;   /* fall through */
331
    case 6 : b+=((uint32_t)k8[5])<<8;    /* fall through */
332
    case 5 : b+=k8[4];                   /* fall through */
333
    case 4 : a+=k[0]; break;
334
    case 3 : a+=((uint32_t)k8[2])<<16;   /* fall through */
335
    case 2 : a+=((uint32_t)k8[1])<<8;    /* fall through */
336
    case 1 : a+=k8[0]; break;
337
    case 0 : return c;
338
    }
339
340
#endif /* !valgrind */
341
342
25.8k
  }
343
0
  else if (HASH_LITTLE_ENDIAN && ((u.i & 0x1) == 0))
344
0
  {
345
0
    const uint16_t *k = (const uint16_t *)key; /* read 16-bit chunks */
346
0
    const uint8_t  *k8;
347
348
    /*--------------- all but last block: aligned reads and different mixing */
349
0
    while (length > 12)
350
0
    {
351
0
      a += k[0] + (((uint32_t)k[1])<<16);
352
0
      b += k[2] + (((uint32_t)k[3])<<16);
353
0
      c += k[4] + (((uint32_t)k[5])<<16);
354
0
      mix(a,b,c);
355
0
      length -= 12;
356
0
      k += 6;
357
0
    }
358
359
    /*----------------------------- handle the last (probably partial) block */
360
0
    k8 = (const uint8_t *)k;
361
0
    switch(length)
362
0
    {
363
0
    case 12: c+=k[4]+(((uint32_t)k[5])<<16);
364
0
       b+=k[2]+(((uint32_t)k[3])<<16);
365
0
       a+=k[0]+(((uint32_t)k[1])<<16);
366
0
       break;
367
0
    case 11: c+=((uint32_t)k8[10])<<16;     /* fall through */
368
0
    case 10: c+=k[4];
369
0
       b+=k[2]+(((uint32_t)k[3])<<16);
370
0
       a+=k[0]+(((uint32_t)k[1])<<16);
371
0
       break;
372
0
    case 9 : c+=k8[8];                      /* fall through */
373
0
    case 8 : b+=k[2]+(((uint32_t)k[3])<<16);
374
0
       a+=k[0]+(((uint32_t)k[1])<<16);
375
0
       break;
376
0
    case 7 : b+=((uint32_t)k8[6])<<16;      /* fall through */
377
0
    case 6 : b+=k[2];
378
0
       a+=k[0]+(((uint32_t)k[1])<<16);
379
0
       break;
380
0
    case 5 : b+=k8[4];                      /* fall through */
381
0
    case 4 : a+=k[0]+(((uint32_t)k[1])<<16);
382
0
       break;
383
0
    case 3 : a+=((uint32_t)k8[2])<<16;      /* fall through */
384
0
    case 2 : a+=k[0];
385
0
       break;
386
0
    case 1 : a+=k8[0];
387
0
       break;
388
0
    case 0 : return c;                     /* zero length requires no mixing */
389
0
    }
390
391
0
  }
392
0
  else
393
0
  {
394
    /* need to read the key one byte at a time */
395
0
    const uint8_t *k = (const uint8_t *)key;
396
397
    /*--------------- all but the last block: affect some 32 bits of (a,b,c) */
398
0
    while (length > 12)
399
0
    {
400
0
      a += k[0];
401
0
      a += ((uint32_t)k[1])<<8;
402
0
      a += ((uint32_t)k[2])<<16;
403
0
      a += ((uint32_t)k[3])<<24;
404
0
      b += k[4];
405
0
      b += ((uint32_t)k[5])<<8;
406
0
      b += ((uint32_t)k[6])<<16;
407
0
      b += ((uint32_t)k[7])<<24;
408
0
      c += k[8];
409
0
      c += ((uint32_t)k[9])<<8;
410
0
      c += ((uint32_t)k[10])<<16;
411
0
      c += ((uint32_t)k[11])<<24;
412
0
      mix(a,b,c);
413
0
      length -= 12;
414
0
      k += 12;
415
0
    }
416
417
    /*-------------------------------- last block: affect all 32 bits of (c) */
418
0
    switch(length) /* all the case statements fall through */
419
0
    {
420
0
    case 12: c+=((uint32_t)k[11])<<24; /* FALLTHRU */
421
0
    case 11: c+=((uint32_t)k[10])<<16; /* FALLTHRU */
422
0
    case 10: c+=((uint32_t)k[9])<<8; /* FALLTHRU */
423
0
    case 9 : c+=k[8]; /* FALLTHRU */
424
0
    case 8 : b+=((uint32_t)k[7])<<24; /* FALLTHRU */
425
0
    case 7 : b+=((uint32_t)k[6])<<16; /* FALLTHRU */
426
0
    case 6 : b+=((uint32_t)k[5])<<8; /* FALLTHRU */
427
0
    case 5 : b+=k[4]; /* FALLTHRU */
428
0
    case 4 : a+=((uint32_t)k[3])<<24; /* FALLTHRU */
429
0
    case 3 : a+=((uint32_t)k[2])<<16; /* FALLTHRU */
430
0
    case 2 : a+=((uint32_t)k[1])<<8; /* FALLTHRU */
431
0
    case 1 : a+=k[0];
432
0
       break;
433
0
    case 0 : return c;
434
0
    }
435
0
  }
436
437
23.2k
  final(a,b,c);
438
23.2k
  return c;
439
25.8k
}
440
/* clang-format on */
441
442
/* a simple hash function similar to what perl does for strings.
443
 * for good results, the string should not be excessively large.
444
 */
445
static unsigned long lh_perllike_str_hash(const void *k)
446
0
{
447
0
  const char *rkey = (const char *)k;
448
0
  unsigned hashval = 1;
449
450
0
  while (*rkey)
451
0
    hashval = hashval * 33 + *rkey++;
452
453
0
  return hashval;
454
0
}
455
456
static unsigned long lh_char_hash(const void *k)
457
25.8k
{
458
#if defined _MSC_VER || defined __MINGW32__
459
#define RANDOM_SEED_TYPE LONG
460
#else
461
25.8k
#define RANDOM_SEED_TYPE int
462
25.8k
#endif
463
25.8k
  static volatile RANDOM_SEED_TYPE random_seed = -1;
464
465
25.8k
  if (random_seed == -1)
466
1
  {
467
1
    RANDOM_SEED_TYPE seed;
468
    /* we can't use -1 as it is the uninitialized sentinel */
469
1
    while ((seed = json_c_get_random_seed()) == -1) {}
470
#if SIZEOF_INT == 8 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_8
471
#define USE_SYNC_COMPARE_AND_SWAP 1
472
#endif
473
1
#if SIZEOF_INT == 4 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
474
1
#define USE_SYNC_COMPARE_AND_SWAP 1
475
1
#endif
476
#if SIZEOF_INT == 2 && defined __GCC_HAVE_SYNC_COMPARE_AND_SWAP_2
477
#define USE_SYNC_COMPARE_AND_SWAP 1
478
#endif
479
1
#if defined USE_SYNC_COMPARE_AND_SWAP
480
1
    (void)__sync_val_compare_and_swap(&random_seed, -1, seed);
481
#elif defined _MSC_VER || defined __MINGW32__
482
    InterlockedCompareExchange(&random_seed, seed, -1);
483
#else
484
    //#warning "racy random seed initialization if used by multiple threads"
485
    random_seed = seed; /* potentially racy */
486
#endif
487
1
  }
488
489
25.8k
  return hashlittle((const char *)k, strlen((const char *)k), (uint32_t)random_seed);
490
25.8k
}
491
492
int lh_char_equal(const void *k1, const void *k2)
493
13.5k
{
494
13.5k
  return (strcmp((const char *)k1, (const char *)k2) == 0);
495
13.5k
}
496
497
struct lh_table *lh_table_new(int size, lh_entry_free_fn *free_fn, lh_hash_fn *hash_fn,
498
                              lh_equal_fn *equal_fn)
499
4.66k
{
500
4.66k
  int i;
501
4.66k
  struct lh_table *t;
502
503
  /* Allocate space for elements to avoid divisions by zero. */
504
4.66k
  assert(size > 0);
505
4.66k
  t = (struct lh_table *)calloc(1, sizeof(struct lh_table));
506
4.66k
  if (!t)
507
0
    return NULL;
508
509
4.66k
  t->count = 0;
510
4.66k
  t->size = size;
511
4.66k
  t->table = (struct lh_entry *)calloc(size, sizeof(struct lh_entry));
512
4.66k
  if (!t->table)
513
0
  {
514
0
    free(t);
515
0
    return NULL;
516
0
  }
517
4.66k
  t->free_fn = free_fn;
518
4.66k
  t->hash_fn = hash_fn;
519
4.66k
  t->equal_fn = equal_fn;
520
101k
  for (i = 0; i < size; i++)
521
96.8k
    t->table[i].k = LH_EMPTY;
522
4.66k
  return t;
523
4.66k
}
524
525
struct lh_table *lh_kchar_table_new(int size, lh_entry_free_fn *free_fn)
526
4.02k
{
527
4.02k
  return lh_table_new(size, free_fn, char_hash_fn, lh_char_equal);
528
4.02k
}
529
530
struct lh_table *lh_kptr_table_new(int size, lh_entry_free_fn *free_fn)
531
0
{
532
0
  return lh_table_new(size, free_fn, lh_ptr_hash, lh_ptr_equal);
533
0
}
534
535
int lh_table_resize(struct lh_table *t, int new_size)
536
638
{
537
638
  struct lh_table *new_t;
538
638
  struct lh_entry *ent;
539
540
638
  new_t = lh_table_new(new_size, NULL, t->hash_fn, t->equal_fn);
541
638
  if (new_t == NULL)
542
0
    return -1;
543
544
11.7k
  for (ent = t->head; ent != NULL; ent = ent->next)
545
11.1k
  {
546
11.1k
    unsigned long h = lh_get_hash(new_t, ent->k);
547
11.1k
    unsigned int opts = 0;
548
11.1k
    if (ent->k_is_constant)
549
0
      opts = JSON_C_OBJECT_ADD_CONSTANT_KEY;
550
11.1k
    if (lh_table_insert_w_hash(new_t, ent->k, ent->v, h, opts) != 0)
551
0
    {
552
0
      lh_table_free(new_t);
553
0
      return -1;
554
0
    }
555
11.1k
  }
556
638
  free(t->table);
557
638
  t->table = new_t->table;
558
638
  t->size = new_size;
559
638
  t->head = new_t->head;
560
638
  t->tail = new_t->tail;
561
638
  free(new_t);
562
563
638
  return 0;
564
638
}
565
566
void lh_table_free(struct lh_table *t)
567
4.02k
{
568
4.02k
  struct lh_entry *c;
569
4.02k
  if (t->free_fn)
570
4.02k
  {
571
15.6k
    for (c = t->head; c != NULL; c = c->next)
572
11.6k
      t->free_fn(c);
573
4.02k
  }
574
4.02k
  free(t->table);
575
4.02k
  free(t);
576
4.02k
}
577
578
int lh_table_insert_w_hash(struct lh_table *t, const void *k, const void *v, const unsigned long h,
579
                           const unsigned opts)
580
22.7k
{
581
22.7k
  unsigned long n;
582
583
22.7k
  if (t->count >= t->size * LH_LOAD_FACTOR)
584
638
  {
585
    /* Avoid signed integer overflow with large tables. */
586
638
    int new_size = (t->size > INT_MAX / 2) ? INT_MAX : (t->size * 2);
587
638
    if (t->size == INT_MAX || lh_table_resize(t, new_size) != 0)
588
0
      return -1;
589
638
  }
590
591
22.7k
  n = h % t->size;
592
593
34.0k
  while (1)
594
34.0k
  {
595
34.0k
    if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED)
596
22.7k
      break;
597
11.3k
    if ((int)++n == t->size)
598
668
      n = 0;
599
11.3k
  }
600
601
22.7k
  t->table[n].k = k;
602
22.7k
  t->table[n].k_is_constant = (opts & JSON_C_OBJECT_ADD_CONSTANT_KEY);
603
22.7k
  t->table[n].v = v;
604
22.7k
  t->count++;
605
606
22.7k
  if (t->head == NULL)
607
2.89k
  {
608
2.89k
    t->head = t->tail = &t->table[n];
609
2.89k
    t->table[n].next = t->table[n].prev = NULL;
610
2.89k
  }
611
19.8k
  else
612
19.8k
  {
613
19.8k
    t->tail->next = &t->table[n];
614
19.8k
    t->table[n].prev = t->tail;
615
19.8k
    t->table[n].next = NULL;
616
19.8k
    t->tail = &t->table[n];
617
19.8k
  }
618
619
22.7k
  return 0;
620
22.7k
}
621
int lh_table_insert(struct lh_table *t, const void *k, const void *v)
622
0
{
623
0
  return lh_table_insert_w_hash(t, k, v, lh_get_hash(t, k), 0);
624
0
}
625
626
struct lh_entry *lh_table_lookup_entry_w_hash(struct lh_table *t, const void *k,
627
                                              const unsigned long h)
628
14.7k
{
629
14.7k
  unsigned long n = h % t->size;
630
14.7k
  int count = 0;
631
632
25.1k
  while (count < t->size)
633
25.1k
  {
634
25.1k
    if (t->table[n].k == LH_EMPTY)
635
11.6k
      return NULL;
636
13.5k
    if (t->table[n].k != LH_FREED && t->equal_fn(t->table[n].k, k))
637
3.14k
      return &t->table[n];
638
10.3k
    if ((int)++n == t->size)
639
558
      n = 0;
640
10.3k
    count++;
641
10.3k
  }
642
0
  return NULL;
643
14.7k
}
644
645
struct lh_entry *lh_table_lookup_entry(struct lh_table *t, const void *k)
646
0
{
647
0
  return lh_table_lookup_entry_w_hash(t, k, lh_get_hash(t, k));
648
0
}
649
650
json_bool lh_table_lookup_ex(struct lh_table *t, const void *k, void **v)
651
0
{
652
0
  struct lh_entry *e = lh_table_lookup_entry(t, k);
653
0
  if (e != NULL)
654
0
  {
655
0
    if (v != NULL)
656
0
      *v = lh_entry_v(e);
657
0
    return 1; /* key found */
658
0
  }
659
0
  if (v != NULL)
660
0
    *v = NULL;
661
0
  return 0; /* key not found */
662
0
}
663
664
int lh_table_delete_entry(struct lh_table *t, struct lh_entry *e)
665
0
{
666
  /* CAW: fixed to be 64bit nice, still need the crazy negative case... */
667
0
  ptrdiff_t n = (ptrdiff_t)(e - t->table);
668
669
  /* CAW: this is bad, really bad, maybe stack goes other direction on this machine... */
670
0
  if (n < 0)
671
0
  {
672
0
    return -2;
673
0
  }
674
675
0
  if (t->table[n].k == LH_EMPTY || t->table[n].k == LH_FREED)
676
0
    return -1;
677
0
  t->count--;
678
0
  if (t->free_fn)
679
0
    t->free_fn(e);
680
0
  t->table[n].v = NULL;
681
0
  t->table[n].k = LH_FREED;
682
0
  if (t->tail == &t->table[n] && t->head == &t->table[n])
683
0
  {
684
0
    t->head = t->tail = NULL;
685
0
  }
686
0
  else if (t->head == &t->table[n])
687
0
  {
688
0
    t->head->next->prev = NULL;
689
0
    t->head = t->head->next;
690
0
  }
691
0
  else if (t->tail == &t->table[n])
692
0
  {
693
0
    t->tail->prev->next = NULL;
694
0
    t->tail = t->tail->prev;
695
0
  }
696
0
  else
697
0
  {
698
0
    t->table[n].prev->next = t->table[n].next;
699
0
    t->table[n].next->prev = t->table[n].prev;
700
0
  }
701
0
  t->table[n].next = t->table[n].prev = NULL;
702
0
  return 0;
703
0
}
704
705
int lh_table_delete(struct lh_table *t, const void *k)
706
0
{
707
0
  struct lh_entry *e = lh_table_lookup_entry(t, k);
708
0
  if (!e)
709
0
    return -1;
710
0
  return lh_table_delete_entry(t, e);
711
0
}
712
713
int lh_table_length(struct lh_table *t)
714
0
{
715
0
  return t->count;
716
0
}