/
table.c
1276 lines (994 loc) · 28.3 KB
/
table.c
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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ProFTPD - FTP server daemon
* Copyright (c) 2004-2016 The ProFTPD Project team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, The ProFTPD Project team and other respective
* copyright holders give permission to link this program with OpenSSL, and
* distribute the resulting executable, without including the source code for
* OpenSSL in the source distribution.
*/
/* Table API implementation */
#include "conf.h"
#ifdef PR_USE_OPENSSL
#include <openssl/rand.h>
#endif /* PR_USE_OPENSSL */
#define PR_TABLE_DEFAULT_NCHAINS 256
#define PR_TABLE_DEFAULT_MAX_ENTS 8192
#define PR_TABLE_ENT_POOL_SIZE 64
struct table_rec {
pool *pool;
unsigned long flags;
/* These bytes are randomly generated at table creation time, and
* are used to seed the hashing function, so as to defend/migitate
* against attempts to feed carefully crafted keys which force the
* table into its worst-case performance scenario.
*
* For more information on attacks of this nature, see:
*
* http://www.cs.rice.edu/~scrosby/hash/CrosbyWallach_UsenixSec2003/
*/
unsigned int seed;
/* Maximum number of entries that can be stored in this table. The
* default maximum (PR_TABLE_DEFAULT_MAX_ENTS) is set fairly high.
* This limit is present in order to defend/mitigate against certain abuse
* scenarios.
*
* XXX Note that an additional protective measure can/might be placed on
* the maximum length of a given chain, to detect other types of attacks
* that force the table into the worse-case performance scenario (i.e.
* linear scanning of a long chain). If such is added, then a Table API
* function should be added for returning the length of the longest chain
* in the table. Such a function could be used by modules to determine
* if their tables are being abused (or in need of readjustment).
*/
unsigned int nmaxents;
pr_table_entry_t **chains;
unsigned int nchains;
unsigned int nents;
/* List of free structures. */
pr_table_entry_t *free_ents;
pr_table_key_t *free_keys;
/* For iterating over all the keys in the entire table. */
pr_table_entry_t *tab_iter_ent;
/* For iterating through all of the possible multiple values for a single
* key. Only used if the PR_TABLE_FL_MULTI_VALUE flag is set.
*/
pr_table_entry_t *val_iter_ent;
/* Cache of last looked-up entry. Usage of this field can be enabled
* by using the PR_TABLE_FL_USE_CACHE flag.
*/
pr_table_entry_t *cache_ent;
/* Table callbacks. */
int (*keycmp)(const void *, size_t, const void *, size_t);
unsigned int (*keyhash)(const void *, size_t);
void (*entinsert)(pr_table_entry_t **, pr_table_entry_t *);
void (*entremove)(pr_table_entry_t **, pr_table_entry_t *);
};
static int handling_signal = FALSE;
static const char *trace_channel = "table";
/* Default table callbacks
*/
static int key_cmp(const void *key1, size_t keysz1, const void *key2,
size_t keysz2) {
const char *k1, *k2;
if (keysz1 != keysz2) {
return keysz1 < keysz2 ? -1 : 1;
}
k1 = key1;
k2 = key2;
if (keysz1 >= 1) {
/* Basic check of the first character in each key, trying to reduce
* the chances of calling strncmp(3) if not needed.
*/
if (k1[0] != k2[0]) {
return k1[0] < k2[0] ? -1 : 1;
}
/* Special case (unlikely, but possible). */
if (keysz1 == 1) {
return 0;
}
}
return strncmp((const char *) key1, (const char *) key2, keysz1);
}
/* Use Perl's hashing algorithm by default.
*
* Here's a good article about this hashing algorithm, and about hashing
* functions in general:
*
* http://www.perl.com/pub/2002/10/01/hashes.html
*/
static unsigned int key_hash(const void *key, size_t keysz) {
unsigned int i = 0;
size_t sz = !keysz ? strlen((const char *) key) : keysz;
while (sz--) {
const char *k = key;
unsigned int c;
c = k[sz];
if (!handling_signal) {
/* Always handle signals in potentially long-running while loops. */
pr_signals_handle();
}
i = (i * 33) + c;
}
return i;
}
/* Default insertion is simply to add the given entry to the end of the
* chain.
*/
static void entry_insert(pr_table_entry_t **h, pr_table_entry_t *e) {
pr_table_entry_t *ei;
if (*h == NULL) {
return;
}
for (ei = *h; ei != NULL && ei->next; ei = ei->next);
/* Now, ei points to the last entry in the chain. */
ei->next = e;
e->prev = ei;
}
/* Default removal is simply to remove the entry from the chain. */
static void entry_remove(pr_table_entry_t **h, pr_table_entry_t *e) {
if (e->next) {
e->next->prev = e->prev;
}
if (e->prev) {
e->prev->next = e->next;
} else {
/* This entry is the head. */
*h = e->next;
}
e->prev = e->next = NULL;
return;
}
/* Table key management
*/
static pr_table_key_t *tab_key_alloc(pr_table_t *tab) {
pr_table_key_t *k;
/* Try to find a free key on the free list first... */
if (tab->free_keys) {
k = tab->free_keys;
tab->free_keys = k->next;
k->next = NULL;
return k;
}
/* ...otherwise, allocate a new key. */
k = pcalloc(tab->pool, sizeof(pr_table_key_t));
return k;
}
static void tab_key_free(pr_table_t *tab, pr_table_key_t *k) {
/* Clear everything from the given key. */
memset(k, 0, sizeof(pr_table_key_t));
/* Add this key to the table's free list. */
if (tab->free_keys) {
pr_table_key_t *i = tab->free_keys;
/* Scan to the end of the list. */
while (i->next != NULL) {
if (!handling_signal) {
pr_signals_handle();
}
i = i->next;
}
i->next = k;
} else {
tab->free_keys = k;
}
}
/* Table entry management
*/
static pr_table_entry_t *tab_entry_alloc(pr_table_t *tab) {
pr_table_entry_t *e;
/* Try to find a free entry on the free list first... */
if (tab->free_ents) {
e = tab->free_ents;
tab->free_ents = e->next;
e->next = NULL;
return e;
}
/* ...otherwise, allocate a new entry. */
e = pcalloc(tab->pool, sizeof(pr_table_entry_t));
return e;
}
static void tab_entry_free(pr_table_t *tab, pr_table_entry_t *e) {
/* Clear everything from the given entry. */
memset(e, 0, sizeof(pr_table_entry_t));
/* Add this entry to the table's free list. */
if (tab->free_ents) {
pr_table_entry_t *i = tab->free_ents;
/* Scan to the end of the list. */
while (i->next != NULL) {
if (!handling_signal) {
pr_signals_handle();
}
i = i->next;
}
i->next = e;
} else {
tab->free_ents = e;
}
}
static void tab_entry_insert(pr_table_t *tab, pr_table_entry_t *e) {
pr_table_entry_t *h = tab->chains[e->idx];
if (h &&
h != e) {
/* Only insert the entry if the head we found is different from the
* the given entry. There is an edge case when the entry being added
* is the head of a new chain.
*/
tab->entinsert(&h, e);
tab->chains[e->idx] = h;
}
e->key->nents++;
tab->nents++;
}
static pr_table_entry_t *tab_entry_next(pr_table_t *tab) {
pr_table_entry_t *ent = NULL;
if (tab->tab_iter_ent) {
ent = tab->tab_iter_ent->next;
if (!ent) {
register unsigned int i;
/* Reset ent to be NULL, so that if we don't find a populated chain,
* we properly return NULL to the caller.
*/
ent = NULL;
/* Skip to the next populated chain. */
for (i = tab->tab_iter_ent->idx + 1; i < tab->nchains; i++) {
if (tab->chains[i]) {
ent = tab->chains[i];
break;
}
}
}
} else {
register unsigned int i;
/* Find the first non-empty chain. */
for (i = 0; i < tab->nchains; i++) {
if (tab->chains[i]) {
ent = tab->chains[i];
break;
}
}
}
tab->tab_iter_ent = ent;
return ent;
}
static void tab_entry_remove(pr_table_t *tab, pr_table_entry_t *e) {
pr_table_entry_t *h;
h = tab->chains[e->idx];
tab->entremove(&h, e);
tab->chains[e->idx] = h;
e->key->nents--;
if (e->key->nents == 0) {
tab_key_free(tab, e->key);
e->key = NULL;
}
tab->nents--;
}
static unsigned int tab_get_seed(void) {
unsigned int seed = 0;
#ifndef PR_USE_OPENSSL
FILE *fp = NULL;
size_t nitems = 0;
#endif /* Not PR_USE_OPENSSL */
#ifdef PR_USE_OPENSSL
RAND_bytes((unsigned char *) &seed, sizeof(seed));
#else
/* Try reading from /dev/urandom, if present */
fp = fopen("/dev/urandom", "rb");
if (fp != NULL) {
nitems = fread(&seed, sizeof(seed), 1, fp);
(void) fclose(fp);
}
if (nitems == 0) {
time_t now = time(NULL);
/* No /dev/urandom present (e.g. we might be in a chroot) or not able
* to read the needed amount of data, but we still want a seed. Fallback
* to using rand(3), PID, and current time.
*/
seed = (unsigned int) (rand() ^ getpid() ^ now);
}
#endif /* PR_USE_OPENSSL */
return seed;
}
/* Public Table API
*/
int pr_table_kadd(pr_table_t *tab, const void *key_data, size_t key_datasz,
const void *value_data, size_t value_datasz) {
unsigned int h, idx;
pr_table_entry_t *e, *n;
if (tab == NULL ||
key_data == NULL ||
(value_datasz > 0 && value_data == NULL)) {
errno = EINVAL;
return -1;
}
if (tab->nents == tab->nmaxents) {
/* Table is full. */
errno = ENOSPC;
return -1;
}
/* Don't forget to add in the random seed data. */
h = tab->keyhash(key_data, key_datasz) + tab->seed;
/* The index of the chain to use is the hash value modulo the number
* of chains.
*/
idx = h % tab->nchains;
/* Allocate a new entry for the given values. */
n = tab_entry_alloc(tab);
n->value_data = value_data;
n->value_datasz = value_datasz;
n->idx = idx;
/* Find the current chain entry at this index. */
e = tab->chains[idx];
if (e != NULL) {
pr_table_entry_t *ei;
/* There is a chain at this index. Next step is to see if any entry
* on this chain has the exact same key. If so, increase the entry ref
* count on that key, otherwise, create a new key.
*/
for (ei = e; ei; ei = ei->next) {
if (ei->key->hash != h) {
continue;
}
/* Hash collision. Now check if the key data that was hashed
* is identical. If so, we have multiple values for the same key.
*/
if (tab->keycmp(ei->key->key_data, ei->key->key_datasz,
key_data, key_datasz) == 0) {
/* Check if this table allows multivalues. */
if (!(tab->flags & PR_TABLE_FL_MULTI_VALUE)) {
errno = EEXIST;
return -1;
}
n->key = ei->key;
}
}
} else {
/* This new entry becomes the head of this chain. */
tab->chains[idx] = n;
}
if (!n->key) {
pr_table_key_t *k;
/* Allocate a new key. */
k = tab_key_alloc(tab);
k->key_data = (void *) key_data;
k->key_datasz = key_datasz;
k->hash = h;
k->nents = 0;
n->key = k;
}
tab_entry_insert(tab, n);
return 0;
}
int pr_table_kexists(pr_table_t *tab, const void *key_data, size_t key_datasz) {
unsigned int h, idx;
pr_table_entry_t *head, *ent;
if (tab == NULL ||
key_data == NULL) {
errno = EINVAL;
return -1;
}
if (tab->nents == 0) {
errno = ENOENT;
return -1;
}
if (tab->flags & PR_TABLE_FL_USE_CACHE) {
/* Has the caller already wanted to lookup this same key previously?
* If so, reuse that lookup if we can. In this case, "same key" means
* the _exact same pointer_, not identical data.
*/
if (tab->cache_ent &&
tab->cache_ent->key->key_data == key_data)
return tab->cache_ent->key->nents;
}
/* Don't forget to add in the random seed data. */
h = tab->keyhash(key_data, key_datasz) + tab->seed;
idx = h % tab->nchains;
head = tab->chains[idx];
if (head == NULL) {
tab->cache_ent = NULL;
return 0;
}
for (ent = head; ent; ent = ent->next) {
if (ent->key == NULL ||
ent->key->hash != h) {
continue;
}
/* Matching hashes. Now to see if the keys themselves match. */
if (tab->keycmp(ent->key->key_data, ent->key->key_datasz,
key_data, key_datasz) == 0) {
if (tab->flags & PR_TABLE_FL_USE_CACHE) {
tab->cache_ent = ent;
}
return ent->key->nents;
}
}
tab->cache_ent = NULL;
errno = EINVAL;
return 0;
}
const void *pr_table_kget(pr_table_t *tab, const void *key_data,
size_t key_datasz, size_t *value_datasz) {
unsigned int h;
pr_table_entry_t *head, *ent;
if (tab == NULL) {
errno = EINVAL;
return NULL;
}
/* Use a NULL key as a way of rewinding the per-key lookup. */
if (key_data == NULL) {
tab->cache_ent = NULL;
tab->val_iter_ent = NULL;
errno = ENOENT;
return NULL;
}
if (tab->nents == 0) {
tab->cache_ent = NULL;
tab->val_iter_ent = NULL;
errno = ENOENT;
return NULL;
}
/* Don't forget to add in the random seed data. */
h = tab->keyhash(key_data, key_datasz) + tab->seed;
/* Has the caller already looked up this same key previously?
* If so, continue the lookup where we left off. In this case,
* "same key" means the _exact same pointer_, not identical data.
*/
if (tab->val_iter_ent &&
tab->val_iter_ent->key->key_data == key_data) {
head = tab->val_iter_ent->next;
} else if ((tab->flags & PR_TABLE_FL_USE_CACHE) &&
tab->cache_ent &&
tab->cache_ent->key->key_data == key_data) {
/* If the cached lookup entry matches, we'll use it. */
head = tab->cache_ent->next;
} else {
unsigned int idx = h % tab->nchains;
head = tab->chains[idx];
}
if (head == NULL) {
tab->cache_ent = NULL;
tab->val_iter_ent = NULL;
errno = ENOENT;
return NULL;
}
for (ent = head; ent; ent = ent->next) {
if (ent->key == NULL ||
ent->key->hash != h) {
continue;
}
/* Matching hashes. Now to see if the keys themselves match. */
if (tab->keycmp(ent->key->key_data, ent->key->key_datasz,
key_data, key_datasz) == 0) {
if (tab->flags & PR_TABLE_FL_USE_CACHE) {
tab->cache_ent = ent;
}
if (tab->flags & PR_TABLE_FL_MULTI_VALUE) {
tab->val_iter_ent = ent;
}
if (value_datasz) {
*value_datasz = ent->value_datasz;
}
return ent->value_data;
}
}
tab->cache_ent = NULL;
tab->val_iter_ent = NULL;
errno = ENOENT;
return NULL;
}
const void *pr_table_kremove(pr_table_t *tab, const void *key_data,
size_t key_datasz, size_t *value_datasz) {
unsigned int h, idx;
pr_table_entry_t *head, *ent;
if (tab == NULL ||
key_data == NULL) {
errno = EINVAL;
return NULL;
}
if (tab->nents == 0) {
errno = ENOENT;
return NULL;
}
/* Has the caller already wanted to lookup this same key previously?
* If so, reuse that lookup if we can. In this case, "same key" means
* the _exact same pointer_, not identical data.
*/
if ((tab->flags & PR_TABLE_FL_USE_CACHE) &&
tab->cache_ent &&
tab->cache_ent->key->key_data == key_data) {
const void *value_data;
value_data = tab->cache_ent->value_data;
if (value_datasz) {
*value_datasz = tab->cache_ent->value_datasz;
}
tab_entry_remove(tab, tab->cache_ent);
tab_entry_free(tab, tab->cache_ent);
tab->cache_ent = NULL;
return value_data;
}
/* Don't forget to add in the random seed data. */
h = tab->keyhash(key_data, key_datasz) + tab->seed;
idx = h % tab->nchains;
head = tab->chains[idx];
if (head == NULL) {
tab->cache_ent = NULL;
errno = ENOENT;
return NULL;
}
for (ent = head; ent; ent = ent->next) {
if (ent->key == NULL ||
ent->key->hash != h) {
continue;
}
/* Matching hashes. Now to see if the keys themselves match. */
if (tab->keycmp(ent->key->key_data, ent->key->key_datasz,
key_data, key_datasz) == 0) {
const void *value_data;
value_data = ent->value_data;
if (value_datasz) {
*value_datasz = ent->value_datasz;
}
tab_entry_remove(tab, ent);
tab_entry_free(tab, ent);
tab->cache_ent = NULL;
return value_data;
}
}
tab->cache_ent = NULL;
errno = EINVAL;
return NULL;
}
int pr_table_kset(pr_table_t *tab, const void *key_data, size_t key_datasz,
const void *value_data, size_t value_datasz) {
unsigned int h;
pr_table_entry_t *head, *ent;
/* XXX Should callers be allowed to set NULL values for keys? */
if (tab == NULL ||
key_data == NULL ||
(value_datasz > 0 && value_data == NULL)) {
errno = EINVAL;
return -1;
}
if (tab->nents == 0) {
errno = ENOENT;
return -1;
}
/* Don't forget to add in the random seed data. */
h = tab->keyhash(key_data, key_datasz) + tab->seed;
/* Has the caller already looked up this same key previously?
* If so, continue the lookup where we left off. In this case,
* "same key" means the _exact same pointer_, not identical data.
*/
if (tab->val_iter_ent &&
tab->val_iter_ent->key->key_data == key_data) {
head = tab->val_iter_ent->next;
} else if ((tab->flags & PR_TABLE_FL_USE_CACHE) &&
tab->cache_ent &&
tab->cache_ent->key->key_data == key_data) {
/* If the cached lookup entry matches, we'll use it. */
head = tab->cache_ent->next;
} else {
unsigned int idx = h % tab->nchains;
head = tab->chains[idx];
}
if (head == NULL) {
tab->cache_ent = NULL;
tab->val_iter_ent = NULL;
errno = ENOENT;
return -1;
}
for (ent = head; ent; ent = ent->next) {
if (ent->key == NULL ||
ent->key->hash != h) {
continue;
}
/* Matching hashes. Now to see if the keys themselves match. */
if (tab->keycmp(ent->key->key_data, ent->key->key_datasz,
key_data, key_datasz) == 0) {
if (ent->value_data == value_data) {
errno = EEXIST;
return -1;
}
ent->value_data = value_data;
ent->value_datasz = value_datasz;
if (tab->flags & PR_TABLE_FL_USE_CACHE) {
tab->cache_ent = ent;
}
if (tab->flags & PR_TABLE_FL_MULTI_VALUE) {
tab->val_iter_ent = ent;
}
return 0;
}
}
tab->cache_ent = NULL;
tab->val_iter_ent = NULL;
errno = EINVAL;
return -1;
}
int pr_table_add(pr_table_t *tab, const char *key_data, const void *value_data,
size_t value_datasz) {
if (tab == NULL ||
key_data == NULL) {
errno = EINVAL;
return -1;
}
if (value_data &&
value_datasz == 0) {
value_datasz = strlen((char *) value_data) + 1;
}
return pr_table_kadd(tab, key_data, strlen(key_data) + 1, value_data,
value_datasz);
}
int pr_table_add_dup(pr_table_t *tab, const char *key_data,
const void *value_data, size_t value_datasz) {
void *dup_data;
if (tab == NULL ||
key_data == NULL) {
errno = EINVAL;
return -1;
}
if (value_data == NULL &&
value_datasz != 0) {
errno = EINVAL;
return -1;
}
if (value_data != NULL &&
value_datasz == 0) {
value_datasz = strlen((const char *) value_data) + 1;
}
dup_data = pcalloc(tab->pool, value_datasz);
memcpy(dup_data, value_data, value_datasz);
return pr_table_add(tab, key_data, dup_data, value_datasz);
}
pr_table_t *pr_table_nalloc(pool *p, int flags, unsigned int nchains) {
pr_table_t *tab;
pool *tab_pool;
if (p == NULL ||
nchains == 0) {
errno = EINVAL;
return NULL;
}
tab_pool = make_sub_pool(p);
pr_pool_tag(tab_pool, "table pool");
tab = pcalloc(tab_pool, sizeof(pr_table_t));
tab->pool = tab_pool;
tab->flags = flags;
tab->nchains = nchains;
tab->chains = pcalloc(tab_pool,
sizeof(pr_table_entry_t *) * tab->nchains);
tab->keycmp = key_cmp;
tab->keyhash = key_hash;
tab->entinsert = entry_insert;
tab->entremove = entry_remove;
tab->seed = tab_get_seed();
tab->nmaxents = PR_TABLE_DEFAULT_MAX_ENTS;
return tab;
}
pr_table_t *pr_table_alloc(pool *p, int flags) {
return pr_table_nalloc(p, flags, PR_TABLE_DEFAULT_NCHAINS);
}
int pr_table_count(pr_table_t *tab) {
if (tab == NULL) {
errno = EINVAL;
return -1;
}
return tab->nents;
}
int pr_table_do(pr_table_t *tab, int (*cb)(const void *key_data,
size_t key_datasz, const void *value_data, size_t value_datasz,
void *user_data), void *user_data, int flags) {
register unsigned int i;
if (tab == NULL ||
cb == NULL) {
errno = EINVAL;
return -1;
}
if (tab->nents == 0) {
return 0;
}
for (i = 0; i < tab->nchains; i++) {
pr_table_entry_t *ent;
ent = tab->chains[i];
while (ent != NULL) {
pr_table_entry_t *next_ent;
int res;
next_ent = ent->next;
if (!handling_signal) {
pr_signals_handle();
}
res = cb(ent->key->key_data, ent->key->key_datasz, ent->value_data,
ent->value_datasz, user_data);
if (res < 0 &&
!(flags & PR_TABLE_DO_FL_ALL)) {
errno = EPERM;
return -1;
}
ent = next_ent;
}
}
return 0;
}
int pr_table_empty(pr_table_t *tab) {
register unsigned int i;
if (tab == NULL) {
errno = EINVAL;
return -1;
}
if (tab->nents == 0) {
return 0;
}
for (i = 0; i < tab->nchains; i++) {
pr_table_entry_t *e;
e = tab->chains[i];
while (e != NULL) {
if (!handling_signal) {
pr_signals_handle();
}
tab_entry_remove(tab, e);
tab_entry_free(tab, e);
e = tab->chains[i];
}
tab->chains[i] = NULL;
}
return 0;
}
int pr_table_exists(pr_table_t *tab, const char *key_data) {
if (tab == NULL ||
key_data == NULL) {
errno = EINVAL;
return -1;
}
return pr_table_kexists(tab, key_data, strlen(key_data) + 1);
}
int pr_table_free(pr_table_t *tab) {
if (tab == NULL) {
errno = EINVAL;
return -1;
}
if (tab->nents != 0) {
errno = EPERM;
return -1;
}
destroy_pool(tab->pool);
return 0;
}
const void *pr_table_get(pr_table_t *tab, const char *key_data,
size_t *value_datasz) {
size_t key_datasz = 0;
if (tab == NULL) {
errno = EINVAL;
return NULL;
}
if (key_data) {
key_datasz = strlen(key_data) + 1;
}
return pr_table_kget(tab, key_data, key_datasz, value_datasz);