cdb-full 0.5a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/COPYING +6 -0
  2. data/FILES +88 -0
  3. data/README +163 -0
  4. data/VERSION +1 -0
  5. data/cdb-0.75/CHANGES +16 -0
  6. data/cdb-0.75/FILES +78 -0
  7. data/cdb-0.75/Makefile +320 -0
  8. data/cdb-0.75/README +7 -0
  9. data/cdb-0.75/SYSDEPS +3 -0
  10. data/cdb-0.75/TARGETS +66 -0
  11. data/cdb-0.75/TODO +1 -0
  12. data/cdb-0.75/VERSION +1 -0
  13. data/cdb-0.75/alloc.c +32 -0
  14. data/cdb-0.75/alloc.h +8 -0
  15. data/cdb-0.75/auto-str.c +44 -0
  16. data/cdb-0.75/auto_home.h +6 -0
  17. data/cdb-0.75/buffer.c +10 -0
  18. data/cdb-0.75/buffer.h +56 -0
  19. data/cdb-0.75/buffer_0.c +12 -0
  20. data/cdb-0.75/buffer_1.c +6 -0
  21. data/cdb-0.75/buffer_1s.c +6 -0
  22. data/cdb-0.75/buffer_2.c +6 -0
  23. data/cdb-0.75/buffer_copy.c +16 -0
  24. data/cdb-0.75/buffer_get.c +67 -0
  25. data/cdb-0.75/buffer_put.c +88 -0
  26. data/cdb-0.75/byte.h +13 -0
  27. data/cdb-0.75/byte_copy.c +14 -0
  28. data/cdb-0.75/byte_cr.c +16 -0
  29. data/cdb-0.75/byte_diff.c +16 -0
  30. data/cdb-0.75/cdb.c +136 -0
  31. data/cdb-0.75/cdb.h +37 -0
  32. data/cdb-0.75/cdb_hash.c +21 -0
  33. data/cdb-0.75/cdb_make.c +153 -0
  34. data/cdb-0.75/cdb_make.h +39 -0
  35. data/cdb-0.75/cdbdump.c +83 -0
  36. data/cdb-0.75/cdbget.c +67 -0
  37. data/cdb-0.75/cdbmake-12.sh +8 -0
  38. data/cdb-0.75/cdbmake-sv.sh +22 -0
  39. data/cdb-0.75/cdbmake.c +110 -0
  40. data/cdb-0.75/cdbstats.c +125 -0
  41. data/cdb-0.75/cdbtest.c +119 -0
  42. data/cdb-0.75/choose.sh +18 -0
  43. data/cdb-0.75/conf-cc +3 -0
  44. data/cdb-0.75/conf-home +3 -0
  45. data/cdb-0.75/conf-ld +3 -0
  46. data/cdb-0.75/error.c +109 -0
  47. data/cdb-0.75/error.h +25 -0
  48. data/cdb-0.75/error_str.c +271 -0
  49. data/cdb-0.75/exit.h +6 -0
  50. data/cdb-0.75/find-systype.sh +159 -0
  51. data/cdb-0.75/fmt.h +25 -0
  52. data/cdb-0.75/fmt_ulong.c +13 -0
  53. data/cdb-0.75/hier.c +15 -0
  54. data/cdb-0.75/install.c +149 -0
  55. data/cdb-0.75/instcheck.c +108 -0
  56. data/cdb-0.75/open.h +10 -0
  57. data/cdb-0.75/open_read.c +6 -0
  58. data/cdb-0.75/open_trunc.c +6 -0
  59. data/cdb-0.75/random.cdb +0 -0
  60. data/cdb-0.75/readwrite.h +7 -0
  61. data/cdb-0.75/rts.exp +251 -0
  62. data/cdb-0.75/rts.sh +1 -0
  63. data/cdb-0.75/rts.tests +210 -0
  64. data/cdb-0.75/scan.h +28 -0
  65. data/cdb-0.75/scan_ulong.c +11 -0
  66. data/cdb-0.75/seek.h +15 -0
  67. data/cdb-0.75/seek_cur.c +7 -0
  68. data/cdb-0.75/seek_set.c +7 -0
  69. data/cdb-0.75/str.h +14 -0
  70. data/cdb-0.75/str_len.c +14 -0
  71. data/cdb-0.75/strerr.h +78 -0
  72. data/cdb-0.75/strerr_die.c +31 -0
  73. data/cdb-0.75/strerr_sys.c +12 -0
  74. data/cdb-0.75/testzero.c +35 -0
  75. data/cdb-0.75/trycpp.c +7 -0
  76. data/cdb-0.75/tryulong32.c +11 -0
  77. data/cdb-0.75/uint32.h1 +11 -0
  78. data/cdb-0.75/uint32.h2 +11 -0
  79. data/cdb-0.75/uint32_pack.c +21 -0
  80. data/cdb-0.75/uint32_unpack.c +31 -0
  81. data/cdb-0.75/warn-auto.sh +2 -0
  82. data/cdb-0.75/x86cpuid.c +38 -0
  83. data/cdb.c +372 -0
  84. data/depend +1 -0
  85. data/extconf.rb +11 -0
  86. data/lib/cdb.rb +90 -0
  87. data/sample/sample.rb +35 -0
  88. metadata +152 -0
@@ -0,0 +1,13 @@
1
+ #ifndef BYTE_H
2
+ #define BYTE_H
3
+
4
+ extern unsigned int byte_chr();
5
+ extern unsigned int byte_rchr();
6
+ extern void byte_copy();
7
+ extern void byte_copyr();
8
+ extern int byte_diff();
9
+ extern void byte_zero();
10
+
11
+ #define byte_equal(s,n,t) (!byte_diff((s),(n),(t)))
12
+
13
+ #endif
@@ -0,0 +1,14 @@
1
+ #include "byte.h"
2
+
3
+ void byte_copy(to,n,from)
4
+ register char *to;
5
+ register unsigned int n;
6
+ register char *from;
7
+ {
8
+ for (;;) {
9
+ if (!n) return; *to++ = *from++; --n;
10
+ if (!n) return; *to++ = *from++; --n;
11
+ if (!n) return; *to++ = *from++; --n;
12
+ if (!n) return; *to++ = *from++; --n;
13
+ }
14
+ }
@@ -0,0 +1,16 @@
1
+ #include "byte.h"
2
+
3
+ void byte_copyr(to,n,from)
4
+ register char *to;
5
+ register unsigned int n;
6
+ register char *from;
7
+ {
8
+ to += n;
9
+ from += n;
10
+ for (;;) {
11
+ if (!n) return; *--to = *--from; --n;
12
+ if (!n) return; *--to = *--from; --n;
13
+ if (!n) return; *--to = *--from; --n;
14
+ if (!n) return; *--to = *--from; --n;
15
+ }
16
+ }
@@ -0,0 +1,16 @@
1
+ #include "byte.h"
2
+
3
+ int byte_diff(s,n,t)
4
+ register char *s;
5
+ register unsigned int n;
6
+ register char *t;
7
+ {
8
+ for (;;) {
9
+ if (!n) return 0; if (*s != *t) break; ++s; ++t; --n;
10
+ if (!n) return 0; if (*s != *t) break; ++s; ++t; --n;
11
+ if (!n) return 0; if (*s != *t) break; ++s; ++t; --n;
12
+ if (!n) return 0; if (*s != *t) break; ++s; ++t; --n;
13
+ }
14
+ return ((int)(unsigned int)(unsigned char) *s)
15
+ - ((int)(unsigned int)(unsigned char) *t);
16
+ }
@@ -0,0 +1,136 @@
1
+ /* Public domain. */
2
+
3
+ #include <sys/types.h>
4
+ #include <sys/stat.h>
5
+ #include <sys/mman.h>
6
+ #include "readwrite.h"
7
+ #include "error.h"
8
+ #include "seek.h"
9
+ #include "byte.h"
10
+ #include "cdb.h"
11
+
12
+ void cdb_free(struct cdb *c)
13
+ {
14
+ if (c->map) {
15
+ munmap(c->map,c->size);
16
+ c->map = 0;
17
+ }
18
+ }
19
+
20
+ void cdb_findstart(struct cdb *c)
21
+ {
22
+ c->loop = 0;
23
+ }
24
+
25
+ void cdb_init(struct cdb *c,int fd)
26
+ {
27
+ struct stat st;
28
+ char *x;
29
+
30
+ cdb_free(c);
31
+ cdb_findstart(c);
32
+ c->fd = fd;
33
+
34
+ if (fstat(fd,&st) == 0)
35
+ if (st.st_size <= 0xffffffff) {
36
+ x = mmap(0,st.st_size,PROT_READ,MAP_SHARED,fd,0);
37
+ if (x + 1) {
38
+ c->size = st.st_size;
39
+ c->map = x;
40
+ }
41
+ }
42
+ }
43
+
44
+ int cdb_read(struct cdb *c,char *buf,unsigned int len,uint32 pos)
45
+ {
46
+ if (c->map) {
47
+ if ((pos > c->size) || (c->size - pos < len)) goto FORMAT;
48
+ byte_copy(buf,len,c->map + pos);
49
+ }
50
+ else {
51
+ if (seek_set(c->fd,pos) == -1) return -1;
52
+ while (len > 0) {
53
+ int r;
54
+ do
55
+ r = read(c->fd,buf,len);
56
+ while ((r == -1) && (errno == error_intr));
57
+ if (r == -1) return -1;
58
+ if (r == 0) goto FORMAT;
59
+ buf += r;
60
+ len -= r;
61
+ }
62
+ }
63
+ return 0;
64
+
65
+ FORMAT:
66
+ errno = error_proto;
67
+ return -1;
68
+ }
69
+
70
+ static int match(struct cdb *c,char *key,unsigned int len,uint32 pos)
71
+ {
72
+ char buf[32];
73
+ int n;
74
+
75
+ while (len > 0) {
76
+ n = sizeof buf;
77
+ if (n > len) n = len;
78
+ if (cdb_read(c,buf,n,pos) == -1) return -1;
79
+ if (byte_diff(buf,n,key)) return 0;
80
+ pos += n;
81
+ key += n;
82
+ len -= n;
83
+ }
84
+ return 1;
85
+ }
86
+
87
+ int cdb_findnext(struct cdb *c,char *key,unsigned int len)
88
+ {
89
+ char buf[8];
90
+ uint32 pos;
91
+ uint32 u;
92
+
93
+ if (!c->loop) {
94
+ u = cdb_hash(key,len);
95
+ if (cdb_read(c,buf,8,(u << 3) & 2047) == -1) return -1;
96
+ uint32_unpack(buf + 4,&c->hslots);
97
+ if (!c->hslots) return 0;
98
+ uint32_unpack(buf,&c->hpos);
99
+ c->khash = u;
100
+ u >>= 8;
101
+ u %= c->hslots;
102
+ u <<= 3;
103
+ c->kpos = c->hpos + u;
104
+ }
105
+
106
+ while (c->loop < c->hslots) {
107
+ if (cdb_read(c,buf,8,c->kpos) == -1) return -1;
108
+ uint32_unpack(buf + 4,&pos);
109
+ if (!pos) return 0;
110
+ c->loop += 1;
111
+ c->kpos += 8;
112
+ if (c->kpos == c->hpos + (c->hslots << 3)) c->kpos = c->hpos;
113
+ uint32_unpack(buf,&u);
114
+ if (u == c->khash) {
115
+ if (cdb_read(c,buf,8,pos) == -1) return -1;
116
+ uint32_unpack(buf,&u);
117
+ if (u == len)
118
+ switch(match(c,key,len,pos + 8)) {
119
+ case -1:
120
+ return -1;
121
+ case 1:
122
+ uint32_unpack(buf + 4,&c->dlen);
123
+ c->dpos = pos + 8 + len;
124
+ return 1;
125
+ }
126
+ }
127
+ }
128
+
129
+ return 0;
130
+ }
131
+
132
+ int cdb_find(struct cdb *c,char *key,unsigned int len)
133
+ {
134
+ cdb_findstart(c);
135
+ return cdb_findnext(c,key,len);
136
+ }
@@ -0,0 +1,37 @@
1
+ /* Public domain. */
2
+
3
+ #ifndef CDB_H
4
+ #define CDB_H
5
+
6
+ #include "uint32.h"
7
+
8
+ #define CDB_HASHSTART 5381
9
+ extern uint32 cdb_hashadd(uint32,unsigned char);
10
+ extern uint32 cdb_hash(char *,unsigned int);
11
+
12
+ struct cdb {
13
+ char *map; /* 0 if no map is available */
14
+ int fd;
15
+ uint32 size; /* initialized if map is nonzero */
16
+ uint32 loop; /* number of hash slots searched under this key */
17
+ uint32 khash; /* initialized if loop is nonzero */
18
+ uint32 kpos; /* initialized if loop is nonzero */
19
+ uint32 hpos; /* initialized if loop is nonzero */
20
+ uint32 hslots; /* initialized if loop is nonzero */
21
+ uint32 dpos; /* initialized if cdb_findnext() returns 1 */
22
+ uint32 dlen; /* initialized if cdb_findnext() returns 1 */
23
+ } ;
24
+
25
+ extern void cdb_free(struct cdb *);
26
+ extern void cdb_init(struct cdb *,int fd);
27
+
28
+ extern int cdb_read(struct cdb *,char *,unsigned int,uint32);
29
+
30
+ extern void cdb_findstart(struct cdb *);
31
+ extern int cdb_findnext(struct cdb *,char *,unsigned int);
32
+ extern int cdb_find(struct cdb *,char *,unsigned int);
33
+
34
+ #define cdb_datapos(c) ((c)->dpos)
35
+ #define cdb_datalen(c) ((c)->dlen)
36
+
37
+ #endif
@@ -0,0 +1,21 @@
1
+ /* Public domain. */
2
+
3
+ #include "cdb.h"
4
+
5
+ uint32 cdb_hashadd(uint32 h,unsigned char c)
6
+ {
7
+ h += (h << 5);
8
+ return h ^ c;
9
+ }
10
+
11
+ uint32 cdb_hash(char *buf,unsigned int len)
12
+ {
13
+ uint32 h;
14
+
15
+ h = CDB_HASHSTART;
16
+ while (len) {
17
+ h = cdb_hashadd(h,*buf++);
18
+ --len;
19
+ }
20
+ return h;
21
+ }
@@ -0,0 +1,153 @@
1
+ /* Public domain. */
2
+
3
+ #include "readwrite.h"
4
+ #include "seek.h"
5
+ #include "error.h"
6
+ #include "alloc.h"
7
+ #include "cdb.h"
8
+ #include "cdb_make.h"
9
+
10
+ int cdb_make_start(struct cdb_make *c,int fd)
11
+ {
12
+ c->head = 0;
13
+ c->split = 0;
14
+ c->hash = 0;
15
+ c->numentries = 0;
16
+ c->fd = fd;
17
+ c->pos = sizeof c->final;
18
+ buffer_init(&c->b,write,fd,c->bspace,sizeof c->bspace);
19
+ return seek_set(fd,c->pos);
20
+ }
21
+
22
+ static int posplus(struct cdb_make *c,uint32 len)
23
+ {
24
+ uint32 newpos = c->pos + len;
25
+ if (newpos < len) { errno = error_nomem; return -1; }
26
+ c->pos = newpos;
27
+ return 0;
28
+ }
29
+
30
+ int cdb_make_addend(struct cdb_make *c,unsigned int keylen,unsigned int datalen,uint32 h)
31
+ {
32
+ struct cdb_hplist *head;
33
+
34
+ head = c->head;
35
+ if (!head || (head->num >= CDB_HPLIST)) {
36
+ head = (struct cdb_hplist *) alloc(sizeof(struct cdb_hplist));
37
+ if (!head) return -1;
38
+ head->num = 0;
39
+ head->next = c->head;
40
+ c->head = head;
41
+ }
42
+ head->hp[head->num].h = h;
43
+ head->hp[head->num].p = c->pos;
44
+ ++head->num;
45
+ ++c->numentries;
46
+ if (posplus(c,8) == -1) return -1;
47
+ if (posplus(c,keylen) == -1) return -1;
48
+ if (posplus(c,datalen) == -1) return -1;
49
+ return 0;
50
+ }
51
+
52
+ int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen)
53
+ {
54
+ char buf[8];
55
+
56
+ if (keylen > 0xffffffff) { errno = error_nomem; return -1; }
57
+ if (datalen > 0xffffffff) { errno = error_nomem; return -1; }
58
+
59
+ uint32_pack(buf,keylen);
60
+ uint32_pack(buf + 4,datalen);
61
+ if (buffer_putalign(&c->b,buf,8) == -1) return -1;
62
+ return 0;
63
+ }
64
+
65
+ int cdb_make_add(struct cdb_make *c,char *key,unsigned int keylen,char *data,unsigned int datalen)
66
+ {
67
+ if (cdb_make_addbegin(c,keylen,datalen) == -1) return -1;
68
+ if (buffer_putalign(&c->b,key,keylen) == -1) return -1;
69
+ if (buffer_putalign(&c->b,data,datalen) == -1) return -1;
70
+ return cdb_make_addend(c,keylen,datalen,cdb_hash(key,keylen));
71
+ }
72
+
73
+ int cdb_make_finish(struct cdb_make *c)
74
+ {
75
+ char buf[8];
76
+ int i;
77
+ uint32 len;
78
+ uint32 u;
79
+ uint32 memsize;
80
+ uint32 count;
81
+ uint32 where;
82
+ struct cdb_hplist *x;
83
+ struct cdb_hp *hp;
84
+
85
+ for (i = 0;i < 256;++i)
86
+ c->count[i] = 0;
87
+
88
+ for (x = c->head;x;x = x->next) {
89
+ i = x->num;
90
+ while (i--)
91
+ ++c->count[255 & x->hp[i].h];
92
+ }
93
+
94
+ memsize = 1;
95
+ for (i = 0;i < 256;++i) {
96
+ u = c->count[i] * 2;
97
+ if (u > memsize)
98
+ memsize = u;
99
+ }
100
+
101
+ memsize += c->numentries; /* no overflow possible up to now */
102
+ u = (uint32) 0 - (uint32) 1;
103
+ u /= sizeof(struct cdb_hp);
104
+ if (memsize > u) { errno = error_nomem; return -1; }
105
+
106
+ c->split = (struct cdb_hp *) alloc(memsize * sizeof(struct cdb_hp));
107
+ if (!c->split) return -1;
108
+
109
+ c->hash = c->split + c->numentries;
110
+
111
+ u = 0;
112
+ for (i = 0;i < 256;++i) {
113
+ u += c->count[i]; /* bounded by numentries, so no overflow */
114
+ c->start[i] = u;
115
+ }
116
+
117
+ for (x = c->head;x;x = x->next) {
118
+ i = x->num;
119
+ while (i--)
120
+ c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
121
+ }
122
+
123
+ for (i = 0;i < 256;++i) {
124
+ count = c->count[i];
125
+
126
+ len = count + count; /* no overflow possible */
127
+ uint32_pack(c->final + 8 * i,c->pos);
128
+ uint32_pack(c->final + 8 * i + 4,len);
129
+
130
+ for (u = 0;u < len;++u)
131
+ c->hash[u].h = c->hash[u].p = 0;
132
+
133
+ hp = c->split + c->start[i];
134
+ for (u = 0;u < count;++u) {
135
+ where = (hp->h >> 8) % len;
136
+ while (c->hash[where].p)
137
+ if (++where == len)
138
+ where = 0;
139
+ c->hash[where] = *hp++;
140
+ }
141
+
142
+ for (u = 0;u < len;++u) {
143
+ uint32_pack(buf,c->hash[u].h);
144
+ uint32_pack(buf + 4,c->hash[u].p);
145
+ if (buffer_putalign(&c->b,buf,8) == -1) return -1;
146
+ if (posplus(c,8) == -1) return -1;
147
+ }
148
+ }
149
+
150
+ if (buffer_flush(&c->b) == -1) return -1;
151
+ if (seek_begin(c->fd) == -1) return -1;
152
+ return buffer_putflush(&c->b,c->final,sizeof c->final);
153
+ }
@@ -0,0 +1,39 @@
1
+ /* Public domain. */
2
+
3
+ #ifndef CDB_MAKE_H
4
+ #define CDB_MAKE_H
5
+
6
+ #include "buffer.h"
7
+ #include "uint32.h"
8
+
9
+ #define CDB_HPLIST 1000
10
+
11
+ struct cdb_hp { uint32 h; uint32 p; } ;
12
+
13
+ struct cdb_hplist {
14
+ struct cdb_hp hp[CDB_HPLIST];
15
+ struct cdb_hplist *next;
16
+ int num;
17
+ } ;
18
+
19
+ struct cdb_make {
20
+ char bspace[8192];
21
+ char final[2048];
22
+ uint32 count[256];
23
+ uint32 start[256];
24
+ struct cdb_hplist *head;
25
+ struct cdb_hp *split; /* includes space for hash */
26
+ struct cdb_hp *hash;
27
+ uint32 numentries;
28
+ buffer b;
29
+ uint32 pos;
30
+ int fd;
31
+ } ;
32
+
33
+ extern int cdb_make_start(struct cdb_make *,int);
34
+ extern int cdb_make_addbegin(struct cdb_make *,unsigned int,unsigned int);
35
+ extern int cdb_make_addend(struct cdb_make *,unsigned int,unsigned int,uint32);
36
+ extern int cdb_make_add(struct cdb_make *,char *,unsigned int,char *,unsigned int);
37
+ extern int cdb_make_finish(struct cdb_make *);
38
+
39
+ #endif