~samiam/MaraDNS

ref: 3.5.0021 MaraDNS/libs/MaraHash.c -rw-r--r-- 20.7 KiB
ca00f282 — Sam Trenholme MaraDNS release 3.5.0021 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* Copyright (c) 2006,2011 Sam Trenholme
 *
 * TERMS
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * This software is provided 'as is' with no guarantees of correctness or
 * fitness for purpose.
 */

/* Note: This file is encoded using utf-8 encoding */
/* Note also: This is the revamped linked-list based hash which was changed
   for MaraDNS 0.8.30 */
/* MaraHash: A series of functions to make assosciative arrays using
   js_string objects as keys and values.

   This is done with a hash that grows as needed for the data in question

   This source file also contains the tools for creating and manipulating
   (as such) the mara_tuple objects (similiar to Python tuples--immutable
   lists) */

#ifndef JS_STRING_INCLUDED
#include "JsStr.h"
#endif
#include "MaraHash.h"
#include <stdio.h>

/* Masks to limit the size of the hash */
/* These are powers of two, minus one */
mhash_offset mhash_mask[31] = { 1, 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,
                          2047, 4095, 8191, 16383, 32767, 65535, 131071,
                          262143, 524287, 1048575, 2097151, 4194303, 8388607,
                          16777215, 33554431, 67108863, 134217727,
                          268435455, 536870911, 1073741823 };

mhash_offset mhash_secret_add_constant = 7;

/* Create a new, blank mhash object
   input: none
   output: pointer to the object in quesiton on success, NULL (0)
           otherwise
*/

mhash *mhash_create(int hash_bits) {

    mhash *new;
    int counter;

    if((new = js_alloc(1,sizeof(mhash))) == 0)
        return 0;

    new->hash_bits = hash_bits;

    /* Create that hash table */
    /* mhash_mask[hash_bits] + 1 has a value of 2 to the power of hash_bits */
    if((new->hash_table =
       js_alloc(mhash_mask[hash_bits] + 1,sizeof(mhash_spot *))) == 0) {
        js_dealloc(new);
        return 0;
        }

    /* Zero out the hash table */
    for(counter=0;counter<=mhash_mask[hash_bits];counter++)
        new->hash_table[counter] = 0;

    /* Zero elements in the assosciative array */
    new->spots = 0;

    /* Exit */
    return new;

    }

/* Determine a hash for a given js_string object
   input: A pointer to the js_string object in question,
          the number of bits for the hash in the hash table
   output: A table offset to try (0 indicates error)
*/

mhash_offset mhash_js(js_string *tohash, int hash_bits) {

    mhash_offset ret = 0;
    unsigned char *point, *max;

    int shift = 0; /* number of bits shifted */

    if(js_has_sanity(tohash) == JS_ERROR)
        return 0;
    point = tohash->string;
    max = point + (tohash->unit_count * tohash->unit_size);
    if(max > point + js_octets(tohash)) /* Overflow protection */
        return 0;

    /* Simple enough hash */
    while(point < max) {
        ret += (mhash_offset)(*point << shift);
        ret += mhash_secret_add_constant;
        ret *= 45737;
        shift += 7;
        shift %= hash_bits;
        point++;
        }

    /* Do something useful with those extra seven bits */
    ret ^= ret >> 7;

    /* Make the hash fit in the table in question */
    ret = ret & mhash_mask[hash_bits];

    /* A return of 0 always indicates an error */
    if(ret == 0)
        ret++;

    return ret;

    }

/* Add an element to the hash
   input: js_string key of dictionaty lookup
          anonymous pointer to the data for the dictionary key
   output: JS_ERROR on error, JS_SUCCESS on success
*/

int mhash_put(mhash *hash, js_string *key, void *value, int datatype) {

    mhash_offset first_found;
    js_string *new_key; /* We copy the key values because the key value is,
                           by necessity, static.  */
    mhash_spot *point, *new;

    /* Sanity check */
    if(js_has_sanity(key) == JS_ERROR)
        return JS_ERROR;

    /* Find the element in the hash table we will mutilate */
    first_found = mhash_js(key,hash->hash_bits);
    if(first_found == 0)
        return JS_ERROR;

    /* Copy the key to the new_key, which needs to be immutable */
    /* Since the key is "immutable", we make it as compact as possible */
    if((new_key=js_create(key->unit_count + 1,key->unit_size)) == 0) {
        js_dealloc(hash->hash_table[first_found]);
        return JS_ERROR;
        }
    if(js_copy(key,new_key) == JS_ERROR) {
        js_dealloc(hash->hash_table[first_found]);
        js_destroy(new_key);
        return JS_ERROR;
        }

    /* Create a key/value pair */
    if((new = js_alloc(1,sizeof(mhash_spot))) == 0) {
        return JS_ERROR;
        }
    /* Put values in the newly created mhash_spot structure */
    new->key = new_key;
    new->value = value;
    new->datatype = datatype;
    new->next = 0;

    /* Find where to put the data */
    point = hash->hash_table[first_found];

    if(point == 0) {
        hash->hash_table[first_found] = new;
        }
    /* If needed, add this to the end of a linked list */
    else {
        /* We do not allow the same key to be in the hash twice */
        if(js_issame(point->key,new_key))
            goto cleanup;
        while(point->next != 0) {
             /* We do not allow the same key to be in the hash twice */
             if(js_issame(point->key,new_key))
                 goto cleanup;
             point = point->next;
             }
        /* We do not allow the same key to be in the hash twice */
        if(js_issame(point->key,new_key))
            goto cleanup;
        point->next = new;
        }

    /* Increment the number of elements in the hash. */
    hash->spots++;

    return JS_SUCCESS;

    cleanup:
        js_destroy(new_key);
        js_dealloc(new);
        return JS_ERROR;
    }

/* Get an element from the hash
   input: js_string key of dictionaty lookup
          anonymous pointer to the data for the dictionary key
   output: A mhash_e struct with the data in question.  "Value"
           points to 0 if there was a problem
*/

mhash_e mhash_get(mhash *hash, js_string *key) {

    mhash_offset first_found;
    mhash_spot *point;
    mhash_e ret;

    ret.point = 0;
    ret.value = 0;
    ret.datatype = -1;

    /* Sanity check */
    if(js_has_sanity(key) == JS_ERROR)
        return ret;

    /* Find the element in the hash table we will get data from */
    first_found = mhash_js(key,hash->hash_bits);

    /* Handle the case of looking up a non-existant element */
    if(hash->hash_table[first_found] == 0) {
        ret.value = 0;
        ret.datatype = 0;
        ret.point = 0;
        return ret;
        }

    point = hash->hash_table[first_found];

    /* Traverse the linked list, as needed */
    while(!js_issame(key,point->key)) {
        /* If the element is not in the table, return error */
        if(point->next == 0) {
            ret.value = 0;
            ret.datatype = 0;
            ret.point = 0;
            return ret;
            }
        point = point->next;
        }

    /* Now that we know what element in the hash table to view, view it */
    ret.point = &(point->value);
    ret.value = point->value;
    ret.datatype = point->datatype;

    return ret;

    }

/* Get a direct pointer to the "immutable" key from the hash
   input: pointer to hash table, js_string key of dictionary lookup
   output: A pointer to the instance of that key in the hash table,
           0 if it isn't there/an error occured
*/

js_string *mhash_get_immutable_key(mhash *hash, js_string *key) {

    mhash_offset first_found;
    mhash_spot *point;

    /* Sanity check */
    if(js_has_sanity(key) == JS_ERROR)
        return 0;

    /* Find the element in the hash table we will get data from */
    first_found = mhash_js(key,hash->hash_bits);

    /* Handle the case of looking up a non-existant element */
    if(hash->hash_table[first_found] == 0)
        return 0;

    point = hash->hash_table[first_found];

    /* Look for the key in the hash table */
    while(!js_issame(key,point->key)) {
        /* If the element is not in the table, return error */
        if(point->next == 0)
            return 0;
        point = point->next;
        }

    /* Now that we know what key in the hash table to view, return a
       pointer to it */
    return point->key;

    }

/* Remove an element from the assosciative array (hash)
   input: Hash to change, element to remove
   output: Pointer to value of array element to remove (which
           you will probably want to deallocate), 0 on error
*/
void *mhash_undef(mhash *hash, js_string *key) {
    mhash_offset first_found;
    mhash_spot *point, *last;
    void *ret;

    /* Find the element in the hash table we will remove */
    first_found = mhash_js(key,hash->hash_bits);
    if(first_found == 0) {
        return 0;
        }

    /* Handle the case of looking up a non-existant element */
    if(hash->hash_table[first_found] == 0) {
        return 0;
        }

    point = hash->hash_table[first_found];
    last = 0;

    /* Find the element in the hash */
    while(point != 0) {
        /* If we find the element we want to delete, break out of loop */
        if(js_issame(key,point->key))
            break;
        last = point;
        point = point->next;
        }

    /* If not found, return 0 */
    if(point == 0)
        return 0;

    /* Remove a pointer to the element in the hash */
    if(last == 0)
        hash->hash_table[first_found] = point->next;
    else
        last->next = point->next;

    /* Decrement the number of elements in the hash */
    hash->spots--;

    /* Deallocate the memory set aside for the hash key */
    if(js_destroy(point->key) == JS_ERROR) {
        return 0;
        }

    /* Remember the pointer to the hash value */
    ret = point->value;

    /* Deallocate the memory set aside for the mhash_spot */
    if(js_dealloc(point) == JS_ERROR) {
        return 0;
        }

    /* Return a pointer to the hash value, which the code calling
       this function will probably deallocate in short order */
    return ret;
    }

/* Function to add a js_string to a mhash structure
   input: Hash to change, key, value
   output: JS_ERROR: bad JS_SUCCESS: good
*/

int mhash_put_js(mhash *hash, js_string *key,js_string *value) {
    js_string *new;
    int ret;

    /* While the anonymous pointer is nice for flexibility, the
       developer probably expects to create a new string when adding
       a value to the table */
    /* We make the value as compact as possible, since it
       won't change */
    if((new=js_create(value->unit_count + 1,value->unit_size)) == 0)
        return JS_ERROR;
    if(js_copy(value,new) == JS_ERROR) {
        js_destroy(new);
        return JS_ERROR;
        }

    ret = mhash_put(hash,key,new,MARA_JS);

    if(ret == JS_ERROR)
        js_destroy(new);

    return ret;
    }

/* Function to get a js_string object from a mhash structure
   input: Hash to view, key
   output: pointer to js_string on success, 0 on error */

js_string *mhash_get_js(mhash *hash, js_string *key) {
   mhash_e info;
   info = mhash_get(hash,key);
   if(info.value == 0)
       return 0; /* Error condition */
   if(info.datatype != MARA_JS)
       return 0; /* Another error condition--we are looking for a js_string */
    if(js_has_sanity(info.value) == JS_ERROR)
        return 0; /* Make sure the string is kosher */
   return info.value;
   }

/* Function to remove a js_string object from a mhash structure
   input: Hash to fondle, key
   output: JS_SUCCESS or JS_ERROR, depending on success/failure */

int mhash_undef_js(mhash *hash, js_string *key) {
    js_string *tonuke;
    tonuke = mhash_undef(hash,key);
    if(tonuke == 0) {
        js_destroy(tonuke);
        return JS_ERROR;
        }
    return js_destroy(tonuke);
    }

/* Function to resize a hash table.
   input: pointer to mhash object (assosciative array), desired size of
          new hash table
   output: JS_SUCCESS on success, JS_ERROR on error
*/

int mhash_resize(mhash *hash,int new_bits) {
    mhash_offset counter;
    mhash_offset old_tablesize;
    mhash_spot **new_hash_table,**old_hash_table;
    mhash_spot *point, *save;

    /* If the value is out of range, return error */
    if(new_bits < 1 || new_bits > 29)
        return JS_ERROR;

    /* Allocate the new hash table -- mhash_mask[new_bits] + 1 is 2^new_bits */
    if((new_hash_table =
        js_alloc(mhash_mask[new_bits] + 1,sizeof(mhash_spot *))) == 0)
        return JS_ERROR;

    /* Zero out the new hash table */
    for(counter=0;counter<=mhash_mask[new_bits];counter++)
        new_hash_table[counter] = 0;

    /* Keep track of where the old hash table is */
    old_hash_table = hash->hash_table;

    /* Set the value of old_tablesize to size of the old hash table
       minus one (to make code more efficient) */
    old_tablesize = mhash_mask[hash->hash_bits];

    /* Have the hash (assosciative array) object point to the
       new hash table */
    hash->hash_table = new_hash_table;
    hash->hash_bits = new_bits;

    /* Copy over data from the old hash table to the new, shiny hash table */
    for(counter = 1;counter <= old_tablesize; counter++) {

        /* Copy found elements over */
        if(old_hash_table[counter] != 0) {
            point = old_hash_table[counter];

            /* Go down every "branch" of linked list elements */
            while(point != 0) {
                /* Sanity checks */
                if(point->key == 0)
                    continue;
                if(js_has_sanity(point->key) == JS_ERROR)
                    continue;

                /* Add the element to the new hash */
                mhash_put(hash,point->key,point->value,point->datatype);

                /* Go to the next element, destorying this element in the
                   process */
                save = point->next;
                js_dealloc(point);
                point = save;
                }
            }
        }

    /* Destroy the old hash table */
    js_dealloc(old_hash_table);

    return JS_SUCCESS;
    }

/* Function to, if needed, automatically grow a hash table
   Input: pointer hash table
   Output: JS_ERROR if something bad happened, 1 if the table did not grow,
           2 if the table grew.
*/

int mhash_autogrow(mhash *hash) {

    int bits;

    bits = hash->hash_bits;
    if(bits < 1 || bits > 29)
        return JS_ERROR;

    /* If is is really small, always grow it */
    if(bits < 4) {
        if(mhash_resize(hash,bits + 1) == JS_ERROR)
            return JS_ERROR;
        return 2;
        }

    /* Integer arithmetic, ugh.  If the hash is more than 50% full,
       we grow it.  Make this mhash_mask[bits - 1] to grow at 25% size and
       mhash_mask[bits - 2] + mhash_mask[bits - 3] to grow at 37% size */

    if(hash->spots >= mhash_mask[bits - 1]) {
        if(mhash_resize(hash,bits + 1) == JS_ERROR)
            return JS_ERROR;
        return 2;
        }

    /* Normally, we do not grow */
    return 1;
    }

/* This is part of the mhash method of getting keys from the hash,
   e.g. "for a in dict.keys()" equavalent.  What this routine does
   is find the first key in the hash, and overwrite the supplied
   key argument with the value of the hash's first key.
   input: pointer to hash, pointer to js_string object where we will
          put the first hash key
   output: JS_ERROR on fatal error, JS_SUCCESS we we found a key,
           0 if the hash is empty
*/
int mhash_firstkey(mhash *hash, js_string *key) {
    mhash_offset offset = 0;
    mhash_spot *point;

    while(offset < mhash_mask[hash->hash_bits] + 1) {
        /* If we found the first element in the hash, copy
           it over to the key, and return (usually) success */
        if(hash->hash_table[offset] != 0) {
            point = hash->hash_table[offset];
            return js_copy(point->key,key);
            }
        else {
            offset++;
            }
        }

    /* We are at the end of the hash */
    return 0;
    }

/* This is the mhash method of getting keys from the hash, e.g.
   "for a in dict.keys()" equavalent.  The way we do this is by having
   two arguments: The pointer to the hash, and a malleable js_string
   object which contins the current pointer that we need to rplace
   with the next pointer in the hash.
   input: pointer to hash, pointer to js_string object with current key
   output: JS_ERROR on fatal error, JS_SUCCESS if we incremented the
           hash pointer, 0 if we are at the last element in the hash
*/

int mhash_nextkey(mhash *hash, js_string *key) {
    mhash_spot *point;
    mhash_offset offset;

    /* Determine where we currently are in the hash */
    offset = mhash_js(key,hash->hash_bits);

    /* Sanity check */
    if(offset > mhash_mask[hash->hash_bits] || offset < 0)
        return JS_ERROR;

    /* Determine where we are in this branch, to look for the element
       in question */
    point = hash->hash_table[offset];

    /* If we are pointing at nothing, return error */
    if(point == 0)
        return JS_ERROR;

    /* Find the element we are looking for */
    while(!js_issame(key,point->key)) {
        /* If the key is not in the hash, return error */
        if(point->next == 0) {
            return JS_ERROR;
            }
        point = point->next;
        }

    /* OK, now, find out what the next element is. */

    /* If there is a subsequent element in this "branch" of the
       hash, then we simply copy over the pointer there */

    if(point->next != 0) {
        return js_copy(point->next->key,key);
        }

    /* Otherwise, we need to find the next element in the hash that has
       an element */
    offset++;

    while(offset < mhash_mask[hash->hash_bits] + 1) {
        /* If we found the next element in the hash, copy
           it over to the key, and return (usually) success */
        if(hash->hash_table[offset] != 0) {
            point = hash->hash_table[offset];
            return js_copy(point->key,key);
            }
        else {
            offset++;
            }
        }

    /* We are at the end of the hash */
    return 0;
    }

/* Create a new mara_tuple object
   input: Number of elements in mara_tuple table
   output: pointer to object in question, 0 on error */

mara_tuple *mtuple_new(int elements) {
    mara_tuple *ret;
    int counter;

    if((ret = js_alloc(1,sizeof(mara_tuple))) == 0)
        return 0;

    ret->elements = elements;
    if((ret->tuple_list = js_alloc(elements,sizeof(js_string *))) == 0) {
        js_dealloc(ret);
        return 0;
        }

    /* Zero out the tuple list table */
    for(counter=0;counter<elements;counter++)
        ret->tuple_list[counter] = 0;

    return ret;
    }

/* Copy a js_string object over to an element in a mara_tuple table.
   Since tuples are "immutable", this funciton will only change empty
   elements.
   input: Pointer to mara_tuple object, pointer to js_string to copy,
          element to copy in to
   output: JS_ERROR on error, JS_SUCCESS on success */

int mtuple_put(mara_tuple *tuple, js_string *js, int element) {
    js_string *copy;

    /* Make sure the "element" value makes sense: It should fit in
       the mara_tuple, and the element in the mara_tuple needs to
       be blank */
    if(element >= tuple->elements || element < 0)
        return JS_ERROR;
    if(tuple->tuple_list[element] != 0)
        return JS_ERROR;

    /* Begin copying over to copy */
    /* We make the copy as compact as possible, since it will not
       change */
    if((copy=js_create(js->unit_count + 1,js->unit_size)) == 0)
        return JS_ERROR;
    if(js_copy(js,copy) == JS_ERROR)
        return JS_ERROR;

    tuple->tuple_list[element] = copy;

    return JS_SUCCESS;
    }

/* Get the pointer to a js_string object in a mara_tuple table.
   input: Pointer to mara_tuple object, element we wish to look at
   output: pointer to appropriate js_string object on success,
           0 on error
*/

js_string *mtuple_get(mara_tuple *tuple, int element) {

    /* Make sure we are given sane numbers */
    if(element >= tuple->elements || element < 0)
        return 0;

    return tuple->tuple_list[element];
    }

/* Read four bytes from a filename and use that as a secret add constant */
int mhash_set_add_constant(char *filename) {
        FILE *read = 0;

        read = fopen(filename,"rb");
        if(read == NULL) {
                return -1;
        }

        mhash_secret_add_constant ^= getc(read);
        mhash_secret_add_constant <<= 8;
        mhash_secret_add_constant ^= getc(read);
        mhash_secret_add_constant <<= 8;
        mhash_secret_add_constant ^= getc(read);
        mhash_secret_add_constant <<= 7;
        mhash_secret_add_constant ^= getc(read);
        fclose(read);
        return 1;
}