-
Notifications
You must be signed in to change notification settings - Fork 2
/
myconf.c
1113 lines (889 loc) · 26.8 KB
/
myconf.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
/*************************************************************************************************
* Emulation of system calls
* Copyright (C) 2000-2007 Mikio Hirabayashi
* This file is part of QDBM, Quick Database Manager.
* QDBM is free software; you can redistribute it and/or modify it under the terms of the GNU
* Lesser General Public License as published by the Free Software Foundation; either version
* 2.1 of the License or any later version. QDBM 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 Lesser General Public License for more
* details.
* You should have received a copy of the GNU Lesser General Public License along with QDBM; if
* not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA.
*************************************************************************************************/
#include "myconf.h"
/*************************************************************************************************
* for dosish filesystems
*************************************************************************************************/
#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_) || defined(_SYS_CYGWIN_)
#define DOSPATHBUFSIZ 8192
int _qdbm_win32_lstat(const char *pathname, struct stat *buf){
char pbuf[DOSPATHBUFSIZ], *p;
int inode;
if(stat(pathname, buf) == -1) return -1;
if(GetFullPathName(pathname, DOSPATHBUFSIZ, pbuf, &p) != 0){
inode = 11003;
for(p = pbuf; *p != '\0'; p++){
inode = inode * 31 + *(unsigned char *)p;
}
buf->st_ino = (inode * 911) & 0x7FFF;
}
return 0;
}
#endif
/*************************************************************************************************
* for POSIX thread
*************************************************************************************************/
#if defined(MYPTHREAD)
#include <pthread.h>
#define PTKEYMAX 8
struct { void *ptr; pthread_key_t key; } _qdbm_ptkeys[PTKEYMAX];
int _qdbm_ptknum = 0;
static void *_qdbm_gettsd(void *ptr, int size, const void *initval);
void *_qdbm_settsd(void *ptr, int size, const void *initval){
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
char *val;
if((val = _qdbm_gettsd(ptr, size, initval)) != NULL) return val;
if(pthread_mutex_lock(&mutex) != 0) return NULL;
if((val = _qdbm_gettsd(ptr, size, initval)) != NULL){
pthread_mutex_unlock(&mutex);
return val;
}
if(_qdbm_ptknum >= PTKEYMAX){
pthread_mutex_unlock(&mutex);
return NULL;
}
_qdbm_ptkeys[_qdbm_ptknum].ptr = ptr;
if(pthread_key_create(&(_qdbm_ptkeys[_qdbm_ptknum].key), free) != 0){
pthread_mutex_unlock(&mutex);
return NULL;
}
if(!(val = malloc(size))){
pthread_key_delete(_qdbm_ptkeys[_qdbm_ptknum].key);
pthread_mutex_unlock(&mutex);
return NULL;
}
memcpy(val, initval, size);
if(pthread_setspecific(_qdbm_ptkeys[_qdbm_ptknum].key, val) != 0){
free(val);
pthread_key_delete(_qdbm_ptkeys[_qdbm_ptknum].key);
pthread_mutex_unlock(&mutex);
return NULL;
}
_qdbm_ptknum++;
pthread_mutex_unlock(&mutex);
return val;
}
static void *_qdbm_gettsd(void *ptr, int size, const void *initval){
char *val;
int i;
for(i = 0; i < _qdbm_ptknum; i++){
if(_qdbm_ptkeys[i].ptr == ptr){
if(!(val = pthread_getspecific(_qdbm_ptkeys[i].key))){
if(!(val = malloc(size))) return NULL;
memcpy(val, initval, size);
if(pthread_setspecific(_qdbm_ptkeys[i].key, val) != 0){
free(val);
return NULL;
}
}
return val;
}
}
return NULL;
}
#endif
/*************************************************************************************************
* for systems without mmap
*************************************************************************************************/
#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
#define MMFDESCMAX 2048
struct { void *start; HANDLE handle; } mmhandles[MMFDESCMAX];
int mmhnum = 0;
CRITICAL_SECTION mmcsec;
static void _qdbm_delete_mmap_env(void);
void *_qdbm_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset){
static volatile long first = TRUE;
static volatile long ready = FALSE;
HANDLE handle;
int i;
if(InterlockedExchange((void *)&first, FALSE)){
InitializeCriticalSection(&mmcsec);
atexit(_qdbm_delete_mmap_env);
InterlockedExchange((void *)&ready, TRUE);
}
while(!InterlockedCompareExchange((void *)&ready, TRUE, TRUE)){
Sleep(1);
}
if(fd < 0 || flags & MAP_FIXED) return MAP_FAILED;
if(!(handle = CreateFileMapping((HANDLE)_get_osfhandle(fd), NULL,
(prot & PROT_WRITE) ? PAGE_READWRITE : PAGE_READONLY,
0, length, NULL))) return MAP_FAILED;
if(!(start = MapViewOfFile(handle, (prot & PROT_WRITE) ? FILE_MAP_WRITE : FILE_MAP_READ,
0, 0, length))){
CloseHandle(handle);
return MAP_FAILED;
}
EnterCriticalSection(&mmcsec);
if(mmhnum >= MMFDESCMAX - 1){
UnmapViewOfFile(start);
CloseHandle(handle);
LeaveCriticalSection(&mmcsec);
return MAP_FAILED;
}
for(i = 0; i < MMFDESCMAX; i++){
if(!mmhandles[i].start){
mmhandles[i].start = start;
mmhandles[i].handle = handle;
break;
}
}
mmhnum++;
LeaveCriticalSection(&mmcsec);
return start;
}
int _qdbm_munmap(void *start, size_t length){
HANDLE handle;
int i;
EnterCriticalSection(&mmcsec);
handle = NULL;
for(i = 0; i < MMFDESCMAX; i++){
if(mmhandles[i].start == start){
handle = mmhandles[i].handle;
mmhandles[i].start = NULL;
mmhandles[i].handle = NULL;
break;
}
}
if(!handle){
LeaveCriticalSection(&mmcsec);
return -1;
}
mmhnum--;
LeaveCriticalSection(&mmcsec);
if(!UnmapViewOfFile(start)){
CloseHandle(handle);
return -1;
}
if(!CloseHandle(handle)) return -1;
return 0;
}
int _qdbm_msync(const void *start, size_t length, int flags){
if(!FlushViewOfFile(start, length)) return -1;
return 0;
}
static void _qdbm_delete_mmap_env(void){
DeleteCriticalSection(&mmcsec);
}
#elif defined(_SYS_FREEBSD_) || defined(_SYS_NETBSD_) || defined(_SYS_OPENBSD_) || \
defined(_SYS_AIX_) || defined(_SYS_RISCOS_) || defined(MYNOMMAP)
void *_qdbm_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset){
char *buf, *wp;
int rv, rlen;
if(flags & MAP_FIXED) return MAP_FAILED;
if(lseek(fd, SEEK_SET, offset) == -1) return MAP_FAILED;
if(!(buf = malloc(sizeof(int) * 3 + length))) return MAP_FAILED;
wp = buf;
*(int *)wp = fd;
wp += sizeof(int);
*(int *)wp = offset;
wp += sizeof(int);
*(int *)wp = prot;
wp += sizeof(int);
rlen = 0;
while((rv = read(fd, wp + rlen, length - rlen)) > 0){
rlen += rv;
}
if(rv == -1 || rlen != length){
free(buf);
return MAP_FAILED;
}
return wp;
}
int _qdbm_munmap(void *start, size_t length){
char *buf, *rp;
int fd, offset, prot, rv, wlen;
buf = (char *)start - sizeof(int) * 3;
rp = buf;
fd = *(int *)rp;
rp += sizeof(int);
offset = *(int *)rp;
rp += sizeof(int);
prot = *(int *)rp;
rp += sizeof(int);
if(prot & PROT_WRITE){
if(lseek(fd, offset, SEEK_SET) == -1){
free(buf);
return -1;
}
wlen = 0;
while(wlen < (int)length){
rv = write(fd, rp + wlen, length - wlen);
if(rv == -1){
if(errno == EINTR) continue;
free(buf);
return -1;
}
wlen += rv;
}
}
free(buf);
return 0;
}
int _qdbm_msync(const void *start, size_t length, int flags){
char *buf, *rp;
int fd, offset, prot, rv, wlen;
buf = (char *)start - sizeof(int) * 3;
rp = buf;
fd = *(int *)rp;
rp += sizeof(int);
offset = *(int *)rp;
rp += sizeof(int);
prot = *(int *)rp;
rp += sizeof(int);
if(prot & PROT_WRITE){
if(lseek(fd, offset, SEEK_SET) == -1) return -1;
wlen = 0;
while(wlen < (int)length){
rv = write(fd, rp + wlen, length - wlen);
if(rv == -1){
if(errno == EINTR) continue;
return -1;
}
wlen += rv;
}
}
return 0;
}
#endif
/*************************************************************************************************
* for reentrant time routines
*************************************************************************************************/
#if defined(_SYS_LINUX_) || defined(_SYS_FREEBSD_) || defined(_SYS_OPENBSD_) || \
defined(_SYS_NETBSD_) || defined(_SYS_SUNOS_) || defined(_SYS_HPUX_) || \
defined(_SYS_MACOSX_) || defined(_SYS_CYGWIN_)
struct tm *_qdbm_gmtime(const time_t *timep, struct tm *result){
return gmtime_r(timep, result);
}
struct tm *_qdbm_localtime(const time_t *timep, struct tm *result){
return localtime_r(timep, result);
}
# else
struct tm *_qdbm_gmtime(const time_t *timep, struct tm *result){
return gmtime(timep);
}
struct tm *_qdbm_localtime(const time_t *timep, struct tm *result){
return localtime(timep);
}
# endif
/*************************************************************************************************
* for systems without times
*************************************************************************************************/
#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
clock_t _qdbm_times(struct tms *buf){
buf->tms_utime = clock();
buf->tms_stime = 0;
buf->tms_cutime = 0;
buf->tms_cstime = 0;
return 0;
}
#endif
/*************************************************************************************************
* for Win32
*************************************************************************************************/
#if defined(_SYS_MSVC_) || defined(_SYS_MINGW_)
#define WINLOCKWAIT 100
int _qdbm_win32_fcntl(int fd, int cmd, struct flock *lock){
HANDLE fh;
DWORD opt;
OVERLAPPED ol;
fh = (HANDLE)_get_osfhandle(fd);
opt = (cmd == F_SETLK) ? LOCKFILE_FAIL_IMMEDIATELY : 0;
if(lock->l_type == F_WRLCK) opt |= LOCKFILE_EXCLUSIVE_LOCK;
memset(&ol, 0, sizeof(OVERLAPPED));
ol.Offset = INT_MAX;
ol.OffsetHigh = 0;
ol.hEvent = 0;
if(!LockFileEx(fh, opt, 0, 1, 0, &ol)){
if(GetLastError() == ERROR_CALL_NOT_IMPLEMENTED){
while(TRUE){
if(LockFile(fh, 0, 0, 1, 0)) return 0;
Sleep(WINLOCKWAIT);
}
}
return -1;
}
return 0;
}
#endif
#if defined(_SYS_MSVC_)
DIR *_qdbm_win32_opendir(const char *name){
char expr[8192];
int len;
DIR *dir;
HANDLE fh;
WIN32_FIND_DATA data;
len = strlen(name);
if(len > 0 && name[len-1] == MYPATHCHR){
sprintf(expr, "%s*", name);
} else {
sprintf(expr, "%s%c*", name, MYPATHCHR);
}
if((fh = FindFirstFile(expr, &data)) == INVALID_HANDLE_VALUE) return NULL;
if(!(dir = malloc(sizeof(DIR)))){
FindClose(fh);
return NULL;
}
dir->fh = fh;
dir->data = data;
dir->first = TRUE;
return dir;
}
int _qdbm_win32_closedir(DIR *dir){
if(!FindClose(dir->fh)){
free(dir);
return -1;
}
free(dir);
return 0;
}
struct dirent *_qdbm_win32_readdir(DIR *dir){
if(dir->first){
sprintf(dir->de.d_name, "%s", dir->data.cFileName);
dir->first = FALSE;
return &(dir->de);
}
if(!FindNextFile(dir->fh, &(dir->data))) return NULL;
sprintf(dir->de.d_name, "%s", dir->data.cFileName);
return &(dir->de);
}
#endif
/*************************************************************************************************
* for checking information of the system
*************************************************************************************************/
#if defined(_SYS_LINUX_)
int _qdbm_vmemavail(size_t size){
char buf[4096], *rp;
int fd, rv, bsiz;
double avail;
if((fd = open("/proc/meminfo", O_RDONLY, 00644)) == -1) return TRUE;
rv = TRUE;
if((bsiz = read(fd, buf, sizeof(buf) - 1)) > 0){
buf[bsiz] = '\0';
avail = -1;
if((rp = strstr(buf, "MemFree:")) != NULL){
rp = strchr(rp, ':') + 1;
avail = strtod(rp, NULL) * 1024.0;
if((rp = strstr(buf, "SwapFree:")) != NULL){
rp = strchr(rp, ':') + 1;
avail += strtod(rp, NULL) * 1024.0;
}
if(size >= avail) rv = FALSE;
}
}
close(fd);
return rv;
}
#elif defined(_SYS_MSVC_) || defined(_SYS_MINGW_) || defined(_SYS_CYGWIN_)
int _qdbm_vmemavail(size_t size){
MEMORYSTATUS sbuf;
sbuf.dwLength = sizeof(MEMORYSTATUS);
GlobalMemoryStatus(&sbuf);
return size < sbuf.dwAvailVirtual;
}
#else
int _qdbm_vmemavail(size_t size){
return TRUE;
}
#endif
/*************************************************************************************************
* for ZLIB
*************************************************************************************************/
#if defined(MYZLIB)
#include <zlib.h>
#define ZLIBBUFSIZ 8192
static char *_qdbm_deflate_impl(const char *ptr, int size, int *sp, int mode);
static char *_qdbm_inflate_impl(const char *ptr, int size, int *sp, int mode);
static unsigned int _qdbm_getcrc_impl(const char *ptr, int size);
char *(*_qdbm_deflate)(const char *, int, int *, int) = _qdbm_deflate_impl;
char *(*_qdbm_inflate)(const char *, int, int *, int) = _qdbm_inflate_impl;
unsigned int (*_qdbm_getcrc)(const char *, int) = _qdbm_getcrc_impl;
static char *_qdbm_deflate_impl(const char *ptr, int size, int *sp, int mode){
z_stream zs;
char *buf, *swap;
unsigned char obuf[ZLIBBUFSIZ];
int rv, asiz, bsiz, osiz;
if(size < 0) size = strlen(ptr);
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
switch(mode){
case _QDBM_ZMRAW:
if(deflateInit2(&zs, 5, Z_DEFLATED, -15, 7, Z_DEFAULT_STRATEGY) != Z_OK)
return NULL;
break;
case _QDBM_ZMGZIP:
if(deflateInit2(&zs, 6, Z_DEFLATED, 15 + 16, 9, Z_DEFAULT_STRATEGY) != Z_OK)
return NULL;
break;
default:
if(deflateInit2(&zs, 6, Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY) != Z_OK)
return NULL;
break;
}
asiz = size + 16;
if(asiz < ZLIBBUFSIZ) asiz = ZLIBBUFSIZ;
if(!(buf = malloc(asiz))){
deflateEnd(&zs);
return NULL;
}
bsiz = 0;
zs.next_in = (unsigned char *)ptr;
zs.avail_in = size;
zs.next_out = obuf;
zs.avail_out = ZLIBBUFSIZ;
while((rv = deflate(&zs, Z_FINISH)) == Z_OK){
osiz = ZLIBBUFSIZ - zs.avail_out;
if(bsiz + osiz > asiz){
asiz = asiz * 2 + osiz;
if(!(swap = realloc(buf, asiz))){
free(buf);
deflateEnd(&zs);
return NULL;
}
buf = swap;
}
memcpy(buf + bsiz, obuf, osiz);
bsiz += osiz;
zs.next_out = obuf;
zs.avail_out = ZLIBBUFSIZ;
}
if(rv != Z_STREAM_END){
free(buf);
deflateEnd(&zs);
return NULL;
}
osiz = ZLIBBUFSIZ - zs.avail_out;
if(bsiz + osiz + 1 > asiz){
asiz = asiz * 2 + osiz;
if(!(swap = realloc(buf, asiz))){
free(buf);
deflateEnd(&zs);
return NULL;
}
buf = swap;
}
memcpy(buf + bsiz, obuf, osiz);
bsiz += osiz;
buf[bsiz] = '\0';
if(mode == _QDBM_ZMRAW) bsiz++;
*sp = bsiz;
deflateEnd(&zs);
return buf;
}
static char *_qdbm_inflate_impl(const char *ptr, int size, int *sp, int mode){
z_stream zs;
char *buf, *swap;
unsigned char obuf[ZLIBBUFSIZ];
int rv, asiz, bsiz, osiz;
zs.zalloc = Z_NULL;
zs.zfree = Z_NULL;
zs.opaque = Z_NULL;
switch(mode){
case _QDBM_ZMRAW:
if(inflateInit2(&zs, -15) != Z_OK) return NULL;
break;
case _QDBM_ZMGZIP:
if(inflateInit2(&zs, 15 + 16) != Z_OK) return NULL;
break;
default:
if(inflateInit2(&zs, 15) != Z_OK) return NULL;
break;
}
asiz = size * 2 + 16;
if(asiz < ZLIBBUFSIZ) asiz = ZLIBBUFSIZ;
if(!(buf = malloc(asiz))){
inflateEnd(&zs);
return NULL;
}
bsiz = 0;
zs.next_in = (unsigned char *)ptr;
zs.avail_in = size;
zs.next_out = obuf;
zs.avail_out = ZLIBBUFSIZ;
while((rv = inflate(&zs, Z_NO_FLUSH)) == Z_OK){
osiz = ZLIBBUFSIZ - zs.avail_out;
if(bsiz + osiz >= asiz){
asiz = asiz * 2 + osiz;
if(!(swap = realloc(buf, asiz))){
free(buf);
inflateEnd(&zs);
return NULL;
}
buf = swap;
}
memcpy(buf + bsiz, obuf, osiz);
bsiz += osiz;
zs.next_out = obuf;
zs.avail_out = ZLIBBUFSIZ;
}
if(rv != Z_STREAM_END){
free(buf);
inflateEnd(&zs);
return NULL;
}
osiz = ZLIBBUFSIZ - zs.avail_out;
if(bsiz + osiz >= asiz){
asiz = asiz * 2 + osiz;
if(!(swap = realloc(buf, asiz))){
free(buf);
inflateEnd(&zs);
return NULL;
}
buf = swap;
}
memcpy(buf + bsiz, obuf, osiz);
bsiz += osiz;
buf[bsiz] = '\0';
if(sp) *sp = bsiz;
inflateEnd(&zs);
return buf;
}
static unsigned int _qdbm_getcrc_impl(const char *ptr, int size){
int crc;
if(size < 0) size = strlen(ptr);
crc = crc32(0, Z_NULL, 0);
return crc32(crc, (unsigned char *)ptr, size);
}
#else
char *(*_qdbm_deflate)(const char *, int, int *, int) = NULL;
char *(*_qdbm_inflate)(const char *, int, int *, int) = NULL;
unsigned int (*_qdbm_getcrc)(const char *, int) = NULL;
#endif
/*************************************************************************************************
* for LZO
*************************************************************************************************/
#if defined(MYLZO)
#include <lzo/lzo1x.h>
static char *_qdbm_lzoencode_impl(const char *ptr, int size, int *sp);
static char *_qdbm_lzodecode_impl(const char *ptr, int size, int *sp);
int _qdbm_lzo_init = FALSE;
char *(*_qdbm_lzoencode)(const char *, int, int *) = _qdbm_lzoencode_impl;
char *(*_qdbm_lzodecode)(const char *, int, int *) = _qdbm_lzodecode_impl;
static char *_qdbm_lzoencode_impl(const char *ptr, int size, int *sp){
char wrkmem[LZO1X_1_MEM_COMPRESS];
lzo_bytep buf;
lzo_uint bsiz;
if(!_qdbm_lzo_init){
if(lzo_init() != LZO_E_OK) return NULL;
_qdbm_lzo_init = TRUE;
}
if(size < 0) size = strlen(ptr);
if(!(buf = malloc(size + size / 16 + 80))) return NULL;
if(lzo1x_1_compress((lzo_bytep)ptr, size, buf, &bsiz, wrkmem) != LZO_E_OK){
free(buf);
return NULL;
}
buf[bsiz] = '\0';
*sp = bsiz;
return (char *)buf;
}
static char *_qdbm_lzodecode_impl(const char *ptr, int size, int *sp){
lzo_bytep buf;
lzo_uint bsiz;
int rat, rv;
if(!_qdbm_lzo_init){
if(lzo_init() != LZO_E_OK) return NULL;
_qdbm_lzo_init = TRUE;
}
rat = 6;
while(TRUE){
bsiz = (size + 256) * rat + 3;
if(!(buf = malloc(bsiz + 1))) return NULL;
rv = lzo1x_decompress_safe((lzo_bytep)(ptr), size, buf, &bsiz, NULL);
if(rv == LZO_E_OK){
break;
} else if(rv == LZO_E_OUTPUT_OVERRUN){
free(buf);
rat *= 2;
} else {
free(buf);
return NULL;
}
}
buf[bsiz] = '\0';
if(sp) *sp = bsiz;
return (char *)buf;
}
#else
char *(*_qdbm_lzoencode)(const char *, int, int *) = NULL;
char *(*_qdbm_lzodecode)(const char *, int, int *) = NULL;
#endif
/*************************************************************************************************
* for BZIP2
*************************************************************************************************/
#if defined(MYBZIP)
#include <bzlib.h>
#define BZIPBUFSIZ 8192
static char *_qdbm_bzencode_impl(const char *ptr, int size, int *sp);
static char *_qdbm_bzdecode_impl(const char *ptr, int size, int *sp);
char *(*_qdbm_bzencode)(const char *, int, int *) = _qdbm_bzencode_impl;
char *(*_qdbm_bzdecode)(const char *, int, int *) = _qdbm_bzdecode_impl;
static char *_qdbm_bzencode_impl(const char *ptr, int size, int *sp){
bz_stream zs;
char *buf, *swap, obuf[BZIPBUFSIZ];
int rv, asiz, bsiz, osiz;
if(size < 0) size = strlen(ptr);
zs.bzalloc = NULL;
zs.bzfree = NULL;
zs.opaque = NULL;
if(BZ2_bzCompressInit(&zs, 9, 0, 30) != BZ_OK) return NULL;
asiz = size + 16;
if(asiz < BZIPBUFSIZ) asiz = BZIPBUFSIZ;
if(!(buf = malloc(asiz))){
BZ2_bzCompressEnd(&zs);
return NULL;
}
bsiz = 0;
zs.next_in = (char *)ptr;
zs.avail_in = size;
zs.next_out = obuf;
zs.avail_out = BZIPBUFSIZ;
while((rv = BZ2_bzCompress(&zs, BZ_FINISH)) == BZ_FINISH_OK){
osiz = BZIPBUFSIZ - zs.avail_out;
if(bsiz + osiz > asiz){
asiz = asiz * 2 + osiz;
if(!(swap = realloc(buf, asiz))){
free(buf);
BZ2_bzCompressEnd(&zs);
return NULL;
}
buf = swap;
}
memcpy(buf + bsiz, obuf, osiz);
bsiz += osiz;
zs.next_out = obuf;
zs.avail_out = BZIPBUFSIZ;
}
if(rv != BZ_STREAM_END){
free(buf);
BZ2_bzCompressEnd(&zs);
return NULL;
}
osiz = BZIPBUFSIZ - zs.avail_out;
if(bsiz + osiz + 1 > asiz){
asiz = asiz * 2 + osiz;
if(!(swap = realloc(buf, asiz))){
free(buf);
BZ2_bzCompressEnd(&zs);
return NULL;
}
buf = swap;
}
memcpy(buf + bsiz, obuf, osiz);
bsiz += osiz;
buf[bsiz] = '\0';
*sp = bsiz;
BZ2_bzCompressEnd(&zs);
return buf;
}
static char *_qdbm_bzdecode_impl(const char *ptr, int size, int *sp){
bz_stream zs;
char *buf, *swap, obuf[BZIPBUFSIZ];
int rv, asiz, bsiz, osiz;
zs.bzalloc = NULL;
zs.bzfree = NULL;
zs.opaque = NULL;
if(BZ2_bzDecompressInit(&zs, 0, 0) != BZ_OK) return NULL;
asiz = size * 2 + 16;
if(asiz < BZIPBUFSIZ) asiz = BZIPBUFSIZ;
if(!(buf = malloc(asiz))){
BZ2_bzDecompressEnd(&zs);
return NULL;
}
bsiz = 0;
zs.next_in = (char *)ptr;
zs.avail_in = size;
zs.next_out = obuf;
zs.avail_out = BZIPBUFSIZ;
while((rv = BZ2_bzDecompress(&zs)) == BZ_OK){
osiz = BZIPBUFSIZ - zs.avail_out;
if(bsiz + osiz >= asiz){
asiz = asiz * 2 + osiz;
if(!(swap = realloc(buf, asiz))){
free(buf);
BZ2_bzDecompressEnd(&zs);
return NULL;
}
buf = swap;
}
memcpy(buf + bsiz, obuf, osiz);
bsiz += osiz;
zs.next_out = obuf;
zs.avail_out = BZIPBUFSIZ;
}
if(rv != BZ_STREAM_END){
free(buf);
BZ2_bzDecompressEnd(&zs);
return NULL;
}
osiz = BZIPBUFSIZ - zs.avail_out;
if(bsiz + osiz >= asiz){
asiz = asiz * 2 + osiz;
if(!(swap = realloc(buf, asiz))){
free(buf);
BZ2_bzDecompressEnd(&zs);
return NULL;
}
buf = swap;
}
memcpy(buf + bsiz, obuf, osiz);
bsiz += osiz;
buf[bsiz] = '\0';
if(sp) *sp = bsiz;
BZ2_bzDecompressEnd(&zs);
return buf;
}
#else
char *(*_qdbm_bzencode)(const char *, int, int *) = NULL;
char *(*_qdbm_bzdecode)(const char *, int, int *) = NULL;
#endif
/*************************************************************************************************
* for ICONV
*************************************************************************************************/
#if defined(MYICONV)
#include <iconv.h>
#define ICONVCHECKSIZ 32768
#define ICONVMISSMAX 256
#define ICONVALLWRAT 0.001
static char *_qdbm_iconv_impl(const char *ptr, int size,
const char *icode, const char *ocode, int *sp, int *mp);
static const char *_qdbm_encname_impl(const char *ptr, int size);
static int _qdbm_encmiss(const char *ptr, int size, const char *icode, const char *ocode);
char *(*_qdbm_iconv)(const char *, int, const char *, const char *,
int *, int *) = _qdbm_iconv_impl;
const char *(*_qdbm_encname)(const char *, int) = _qdbm_encname_impl;
static char *_qdbm_iconv_impl(const char *ptr, int size,
const char *icode, const char *ocode, int *sp, int *mp){
iconv_t ic;
char *obuf, *wp, *rp;
size_t isiz, osiz;
int miss;
if(size < 0) size = strlen(ptr);
isiz = size;
if((ic = iconv_open(ocode, icode)) == (iconv_t)-1) return NULL;
osiz = isiz * 5;
if(!(obuf = malloc(osiz + 1))){
iconv_close(ic);
return NULL;
}
wp = obuf;
rp = (char *)ptr;
miss = 0;
while(isiz > 0){
if(iconv(ic, (void *)&rp, &isiz, &wp, &osiz) == -1){
if(errno == EILSEQ && (*rp == 0x5c || *rp == 0x7e)){
*wp = *rp;
wp++;
rp++;
isiz--;
} else if(errno == EILSEQ || errno == EINVAL){
rp++;
isiz--;
miss++;