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 #include "../daemon/netsite.h"
53 #include "plist.h"
54 #include "plist_pvt.h"
55
56 int plistHashSizes[] =
PLSTSIZES;
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 NSAPI_PUBLIC int
83 PListAssignValue(
PList_t plist,
const char *pname,
84 const void *pvalue,
PList_t ptype)
85 {
86 PListStruct_t *pl = (
PListStruct_t *)plist;
87 PLValueStruct_t *pv;
88 int pindex;
89 int i;
90
91 if (!plist)
return ERRPLUNDEF;
92
93
94 if (pl->pl_symtab) {
95
96
97 i = PListHashName(pl->pl_symtab, pname);
98
99
100 for (pv = pl->pl_symtab->pt_hash[i]; pv; pv = pv->pv_next) {
101
102 if (!strcmp(pname, pv->pv_name)) {
103
104
105 pindex = pv->pv_pi;
106
107
108 pv->pv_value = (
char *)pvalue;
109
110
111 if (ptype) pv->pv_type = (
PListStruct_t *)ptype;
112
113
114 return pindex;
115 }
116 }
117 }
118
119
120 return ERRPLUNDEF;
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 NSAPI_PUBLIC PList_t
149 PListCreate(
pool_handle_t *mempool,
int resvprop,
int maxprop,
int flags)
150 {
151 PListStruct_t *plist;
152 int i;
153
154 plist = (
PListStruct_t *)pool_malloc(mempool,
sizeof(
PListStruct_t));
155 if (plist) {
156
157
158 if (maxprop <
0) maxprop =
0;
159
160
161 if (resvprop >
0) {
162 if (maxprop && (resvprop > maxprop)) resvprop = maxprop;
163 }
164 else resvprop =
0;
165
166
167 plist->pl_mempool = mempool;
168 plist->pl_symtab =
NULL;
169 plist->pl_maxprop = maxprop;
170 plist->pl_resvpi = resvprop;
171 plist->pl_initpi = resvprop;
172 plist->pl_lastpi = resvprop;
173
174
175 plist->pl_cursize = (resvprop) ? resvprop :
PLIST_DEFSIZE;
176
177
178 plist->pl_ppval = (pb_entry **)pool_malloc(mempool,
179 (plist->pl_cursize *
180 sizeof(
PLValueStruct_t *)));
181 if (!plist->pl_ppval) {
182
183
184 pool_free(mempool, (
void *)plist);
185 plist =
NULL;
186 }
187 else {
188
189 for (i =
0; i < plist->pl_lastpi; ++i) {
190 plist->pl_ppval[i] =
0;
191 }
192 }
193 }
194
195 return (
PList_t)plist;
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 NSAPI_PUBLIC int
225 PListDefProp(
PList_t plist,
int pindex,
const char *pname,
const int flags)
226 {
227 PListStruct_t *pl = (
PListStruct_t *)plist;
228 PLValueStruct_t *pv;
229
230 if (!plist)
return ERRPLUNDEF;
231
232
233 if (pindex >
0) {
234
235
236 if (flags !=
PLFLG_IGN_RES && pindex > pl->pl_resvpi) {
237
238 return ERRPLINVPI;
239 }
240
241 PLValueStruct_t **ppval = (
PLValueStruct_t **)(pl->pl_ppval);
242 if (ppval[pindex -
1]) {
243
244 return ERRPLEXIST;
245 }
246 }
247 else {
248
249
250 pindex = PListGetFreeIndex(pl);
251 if (pindex <
1) {
252
253 return pindex;
254 }
255 }
256
257
258 pv = (
PLValueStruct_t *)pool_calloc(pl->pl_mempool,
259 1,
sizeof(
PLValueStruct_t));
260 if (!pv) {
261
262
263 return ERRPLNOMEM;
264 }
265
266 PLValueStruct_t **ppval = (
PLValueStruct_t **)(pl->pl_ppval);
267 pv->pv_pbentry.param = &pv->pv_pbparam;
268 pv->pv_pi = pindex;
269 pv->pv_mempool = pl->pl_mempool;
270 ppval[pindex -
1] = pv;
271
272
273 if (pname) {
274
275
276 return PListNameProp(plist, pindex, pname);
277 }
278
279
280 return pindex;
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 NSAPI_PUBLIC const void *
304 PListDeleteProp(
PList_t plist,
int pindex,
const char *pname_in)
305 {
306 PListStruct_t *pl = (
PListStruct_t *)plist;
307 PLValueStruct_t **ppval;
308 PLValueStruct_t **pvp;
309 PLValueStruct_t *pv =
NULL;
310 int i;
311 const void *pvalue =
NULL;
312 char *pname = (
char *)pname_in;
313
314 if (!plist)
return NULL;
315
316 ppval = (
PLValueStruct_t **)(pl->pl_ppval);
317
318
319 if ((pindex >
0) && (pindex <= pl->pl_initpi)) {
320
321
322 pv = ppval[pindex -
1];
323 pname =
0;
324 if (pv) {
325 pname = pv->pv_name;
326 }
327 }
328
329 if (pname && pl->pl_symtab) {
330
331
332 i = PListHashName(pl->pl_symtab, pname);
333
334
335 for (pvp = &pl->pl_symtab->pt_hash[i]; *pvp; pvp = &(*pvp)->pv_next) {
336
337 pv = *pvp;
338 if (!strcmp(pname, pv->pv_name)) {
339
340
341 pindex = pv->pv_pi;
342 *pvp = pv->pv_next;
343 pl->pl_symtab->pt_nsyms--;
344 break;
345 }
346 pv =
NULL;
347 }
348 }
349
350
351 if (pv) {
352
353
354 ppval[pindex -
1] =
NULL;
355
356
357 if (pv->pv_name) {
358 pool_free(pv->pv_mempool, (
void *)(pv->pv_name));
359 }
360 pvalue = pv->pv_value;
361
362
363 pool_free(pv->pv_mempool, (
void *)pv);
364 }
365 return(pvalue);
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 NSAPI_PUBLIC int
397 PListFindValue(
PList_t plist,
const char *pname,
void **pvalue,
PList_t *ptype)
398 {
399 PListStruct_t *pl = (
PListStruct_t *)plist;
400 PLValueStruct_t *pv;
401 int pindex;
402 int i;
403
404 if (!plist)
return ERRPLUNDEF;
405
406
407 if (pl->pl_symtab) {
408
409
410 i = PListHashName(pl->pl_symtab, pname);
411
412
413 for (pv = pl->pl_symtab->pt_hash[i]; pv; pv = pv->pv_next) {
414
415 if (!strcmp(pname, pv->pv_name)) {
416
417
418 pindex = pv->pv_pi;
419
420
421 if (pvalue) *pvalue = (
void *)(pv->pv_value);
422
423
424 if (ptype) *ptype = (
PList_t)(pv->pv_type);
425
426
427 return pindex;
428 }
429 }
430 }
431
432
433 return ERRPLUNDEF;
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 NSAPI_PUBLIC int
461 PListInitProp(
PList_t plist,
int pindex,
const char *pname,
462 const void *pvalue,
PList_t ptype)
463 {
464 int rv;
465
466 if (!plist)
return ERRPLUNDEF;
467
468
469 rv = PListDefProp(plist, pindex, pname,
PLFLG_USE_RES);
470 if (rv >
0) {
471
472
473 rv = PListSetValue(plist, rv, pvalue, ptype);
474 }
475
476 return rv;
477 }
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500 NSAPI_PUBLIC PList_t
501 PListNew(
pool_handle_t *mempool)
502 {
503
504 return PListCreate(mempool,
0,
0,
0);
505 }
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523 void
524 PListDestroy(
PList_t plist)
525 {
526 PListStruct_t *pl = (
PListStruct_t *)plist;
527 PLValueStruct_t **ppval;
528 PLValueStruct_t *pv;
529 int i;
530
531 if (!plist)
return;
532
533
534 if (pl->pl_symtab) {
535 pool_free(pl->pl_mempool, (
void *)(pl->pl_symtab));
536 }
537
538 ppval = (
PLValueStruct_t **)(pl->pl_ppval);
539
540
541 for (i =
0; i < pl->pl_initpi; ++i) {
542
543
544 pv = ppval[i];
545 if (pv) {
546
547
548 if (pv->pv_name) {
549 pool_free(pv->pv_mempool, (
void *)(pv->pv_name));
550 }
551
552
553 pool_free(pv->pv_mempool, (
void *)pv);
554 }
555 }
556
557
558 pool_free(pl->pl_mempool, (
void *)ppval);
559
560
561 pool_free(pl->pl_mempool, (
void *)pl);
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 NSAPI_PUBLIC int
595 PListGetValue(
PList_t plist,
int pindex,
void **pvalue,
PList_t *ptype)
596 {
597 PListStruct_t *pl = (
PListStruct_t *)plist;
598 PLValueStruct_t **ppval;
599 PLValueStruct_t *pv;
600
601 if (!plist)
return ERRPLUNDEF;
602
603 ppval = (
PLValueStruct_t **)(pl->pl_ppval);
604
605
606 if ((pindex >
0) && (pindex <= pl->pl_initpi)) {
607
608
609 pv = ppval[pindex -
1];
610 if (pv) {
611
612
613 if (pvalue) *pvalue = (
void *)(pv->pv_value);
614
615
616 if (ptype) *ptype = (
PList_t)(pv->pv_type);
617
618
619 return pindex;
620 }
621 }
622
623
624 return ERRPLINVPI;
625 }
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643 unsigned int
644 PListHash(
const char *string)
645 {
646 unsigned int hashval =
0;
647
648 while (*string) {
649 hashval = (hashval<<
5) ^ (*string++ & 0x7f);
650 }
651
652 return hashval;
653 }
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674 int
675 PListHashName(
PLSymbolTable_t *symtab,
const char *pname)
676 {
677 return PListHash(pname) %
PLSIZENDX(symtab->pt_sizendx);
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 NSAPI_PUBLIC int
703 PListNameProp(
PList_t plist,
int pindex,
const char *pname)
704 {
705 PListStruct_t *pl = (
PListStruct_t *)plist;
706 PLValueStruct_t *pv;
707 PLSymbolTable_t *pt;
708 int i;
709
710 if (!plist)
return ERRPLUNDEF;
711
712 pt = pl->pl_symtab;
713
714
715 if ((pindex >
0) && (pindex <= pl->pl_initpi)) {
716
717
718 pv = ((
PLValueStruct_t **)(pl->pl_ppval))[pindex -
1];
719 if (pv) {
720
721
722 if (pv->pv_name) {
723 PLValueStruct_t **pvp;
724
725
726 i = PListHashName(pt, pv->pv_name);
727
728
729 for (pvp = &pt->pt_hash[i];
730 *pvp; pvp = &(*pvp)->pv_next) {
731
732 if (*pvp == pv) {
733
734
735 *pvp = pv->pv_next;
736 pt->pt_nsyms--;
737 break;
738 }
739 }
740
741
742 pool_free(pv->pv_mempool, (
void *)(pv->pv_name));
743 }
744
745
746 if (pname) {
747
748
749 pt = PListSymbolTable(pl);
750 if (!pt) {
751 return ERRPLNOMEM;
752 }
753
754
755 pv->pv_name = pool_strdup(pv->pv_mempool, (
char *)pname);
756
757
758 i = PListHashName(pt, pname);
759 pv->pv_next = pt->pt_hash[i];
760 pt->pt_hash[i] = pv;
761 pt->pt_nsyms++;
762 }
763
764
765 return pindex;
766 }
767 }
768
769
770 return ERRPLINVPI;
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 NSAPI_PUBLIC int
798 PListSetType(
PList_t plist,
int pindex,
PList_t ptype)
799 {
800 PListStruct_t *pl = (
PListStruct_t *)plist;
801 PLValueStruct_t **ppval;
802 PLValueStruct_t *pv;
803
804 if (!plist)
return ERRPLUNDEF;
805
806 ppval = (
PLValueStruct_t **)(pl->pl_ppval);
807
808
809 if ((pindex >
0) && (pindex <= pl->pl_initpi)) {
810
811
812 pv = ppval[pindex -
1];
813 if (pv) {
814
815
816 pv->pv_type = ptype;
817
818
819 return pindex;
820 }
821 }
822
823
824 return ERRPLINVPI;
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 NSAPI_PUBLIC int
855 PListSetValue(
PList_t plist,
int pindex,
const void *pvalue,
PList_t ptype)
856 {
857 PListStruct_t *pl = (
PListStruct_t *)plist;
858 PLValueStruct_t **ppval;
859 PLValueStruct_t *pv;
860
861 if (!plist)
return ERRPLUNDEF;
862
863 ppval = (
PLValueStruct_t **)(pl->pl_ppval);
864
865
866 if ((pindex >
0) && (pindex <= pl->pl_initpi)) {
867
868
869 pv = ppval[pindex -
1];
870 if (pv) {
871
872
873 pv->pv_value = (
char *)pvalue;
874
875
876 if (ptype) pv->pv_type = (
PListStruct_t *)ptype;
877
878
879 return pindex;
880 }
881 }
882
883
884 return ERRPLINVPI;
885 }
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902 NSAPI_PUBLIC void
903 PListEnumerate(
PList_t plist,
PListFunc_t *user_func,
void *user_data)
904 {
905 PListStruct_t *pl = (
PListStruct_t *)plist;
906 PLValueStruct_t **ppval;
907 PLValueStruct_t *pv;
908 int i;
909
910 if (!plist)
return;
911
912 ppval = (
PLValueStruct_t **)(pl->pl_ppval);
913
914
915 for (i =
0; i < pl->pl_initpi; ++i) {
916
917
918 pv = ppval[i];
919 if (pv) {
920 (*user_func)(pv->pv_name, pv->pv_value, user_data);
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 static PList_t
951 PListCreateDuplicate(
PList_t src_plist,
pool_handle_t *new_mempool,
int flags)
952 {
953 PListStruct_t *plist;
954 int i;
955 pool_handle_t *mempool;
956
957 mempool = (flags ==
PLFLG_NEW_MPOOL) ? new_mempool : src_plist->pl_mempool;
958
959 plist = (
PListStruct_t *)pool_malloc(mempool,
sizeof(
PListStruct_t));
960 if (plist) {
961
962
963 plist->pl_mempool = mempool;
964 plist->pl_symtab =
NULL;
965 plist->pl_maxprop = src_plist->pl_maxprop;
966 plist->pl_resvpi = src_plist->pl_resvpi;
967 plist->pl_initpi = src_plist->pl_initpi;
968 plist->pl_lastpi = src_plist->pl_lastpi;
969
970
971 plist->pl_cursize = src_plist->pl_cursize;
972
973
974 plist->pl_ppval = (pb_entry **)pool_malloc(mempool,
975 (plist->pl_cursize *
976 sizeof(
PLValueStruct_t *)));
977 if (!plist->pl_ppval) {
978
979
980 pool_free(mempool, (
void *)plist);
981 plist =
NULL;
982 }
983 else {
984
985 for (i =
0; i < plist->pl_lastpi; ++i) {
986 plist->pl_ppval[i] =
0;
987 }
988 }
989 }
990
991 return (
PList_t)plist;
992 }
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018 NSAPI_PUBLIC PList_t
1019 PListDuplicate(
PList_t plist,
pool_handle_t *new_mempool,
int flags)
1020 {
1021 PListStruct_t *pl = (
PListStruct_t *)plist;
1022 PLValueStruct_t **ppval;
1023 PLValueStruct_t *pv;
1024 int i;
1025 int rv =
0;
1026 PList_t new_plist;
1027
1028 if (!plist)
return NULL;
1029
1030 new_plist = PListCreateDuplicate(plist, new_mempool, flags);
1031 if (new_plist ==
NULL) {
1032 return(
NULL);
1033 }
1034
1035 ppval = (
PLValueStruct_t **)(pl->pl_ppval);
1036
1037
1038 for (i =
0; i < pl->pl_initpi; ++i) {
1039
1040
1041 pv = ppval[i];
1042 if (pv) {
1043
1044 rv = PListDefProp(new_plist, i +
1, pv->pv_name,
PLFLG_IGN_RES);
1045 if (rv >
0) {
1046
1047
1048 rv = PListSetValue(new_plist, rv, pv->pv_value, pv->pv_type);
1049 }
1050
1051 if ( rv <=
0 ) {
1052 PListDestroy(new_plist);
1053 return(
NULL);
1054 }
1055 }
1056
1057 }
1058
1059 return(new_plist);
1060 }
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078 NSAPI_PUBLIC pool_handle_t *
1079 PListGetPool(
PList_t plist)
1080 {
1081 if (!plist)
return NULL;
1082
1083 return(plist->pl_mempool);
1084 }
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104 int
1105 PListGetFreeIndex(
PListStruct_t *pl)
1106 {
1107 PLValueStruct_t **ppval = (
PLValueStruct_t **)(pl->pl_ppval);
1108 int wrapped;
1109 int i;
1110
1111
1112
1113
1114
1115 for (wrapped =
0, i = pl->pl_lastpi; ;) {
1116
1117
1118 if (i < pl->pl_initpi) {
1119
1120
1121 if (ppval[i] ==
0)
break;
1122
1123
1124 ++i;
1125 }
1126 else {
1127
1128
1129 if (i < pl->pl_cursize) {
1130
1131
1132
1133
1134
1135
1136 ppval[i] =
0;
1137 }
1138 else {
1139
1140
1141
1142
1143
1144
1145 if (!wrapped) {
1146
1147 i = pl->pl_resvpi;
1148 wrapped =
1;
1149 continue;
1150 }
1151
1152
1153
1154
1155
1156 i = pl->pl_cursize;
1157 if (pl->pl_maxprop && (i > pl->pl_maxprop)) {
1158
1159
1160 return ERRPLFULL;
1161 }
1162
1163
1164 int cursize = i +
PLIST_DEFGROW;
1165
1166
1167 ppval = (
PLValueStruct_t **)pool_realloc(pl->pl_mempool,
1168 (
void *)ppval,
1169 (cursize *
sizeof(
PLValueStruct_t *)));
1170 if (!ppval) {
1171
1172
1173 return ERRPLNOMEM;
1174 }
1175
1176
1177 ppval[i] =
NULL;
1178 pl->pl_ppval = (pb_entry **)ppval;
1179 pl->pl_cursize = cursize;
1180 }
1181
1182
1183 pl->pl_initpi = i +
1;
1184 break;
1185 }
1186 }
1187
1188
1189 pl->pl_lastpi = i +
1;
1190
1191 return i +
1;
1192 }
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 PLSymbolTable_t *
1213 PListSymbolTable(
PListStruct_t *pl)
1214 {
1215 PLSymbolTable_t *pt;
1216 int i;
1217
1218 pt = pl->pl_symtab;
1219
1220
1221 if (!pl->pl_symtab) {
1222
1223
1224 pt = (
PLSymbolTable_t *)pool_calloc(pl->pl_mempool,
1,
PLHASHSIZE(
0));
1225
1226 pl->pl_symtab = pt;
1227 }
1228 else {
1229
1230
1231 i =
PLSIZENDX(pt->pt_sizendx);
1232 if ((pt->pt_sizendx <
PLMAXSIZENDX) && pt->pt_nsyms >= (i + i)) {
1233
1234 PLSymbolTable_t *npt;
1235
1236
1237 npt = (
PLSymbolTable_t *)pool_calloc(pl->pl_mempool,
1,
1238 PLHASHSIZE(pt->pt_sizendx+
1));
1239 if (npt) {
1240 npt->pt_sizendx = pt->pt_sizendx +
1;
1241 npt->pt_nsyms = pt->pt_nsyms;
1242
1243
1244 for (i =
0; i <
PLSIZENDX(pt->pt_sizendx); ++i) {
1245
1246 while (pt->pt_hash[i]) {
1247 PLValueStruct_t **pvp;
1248 int j;
1249
1250
1251 for (pvp = &pt->pt_hash[i]; (*pvp)->pv_next; pvp = &(*pvp)->pv_next);
1252
1253
1254 j = PListHashName(npt, (*pvp)->pv_name);
1255 (*pvp)->pv_next = npt->pt_hash[j];
1256 npt->pt_hash[j] = (*pvp);
1257
1258
1259 *pvp =
NULL;
1260 }
1261 }
1262
1263 pl->pl_symtab = npt;
1264
1265
1266 pool_free(pl->pl_mempool, (
void *)pt);
1267 pt = npt;
1268 }
1269 }
1270 }
1271
1272 return pl->pl_symtab;
1273 }
1274