Repository navigation
Expand file tree
/
Copy pathcdbmss.c
More file actions
94 lines (87 loc) · 2.43 KB
/
Copy pathcdbmss.c
File metadata and controls
94 lines (87 loc) · 2.43 KB
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
/*
* $Log: cdbmss.c,v $
* Revision 1.8 2025-01-21 22:19:11+05:30 Cprogrammer
* fixes for gcc14
*
* Revision 1.7 2024-05-09 23:46:19+05:30 mbhangui
* fix discarded-qualifier compiler warnings
*
* Revision 1.6 2022-10-18 20:00:50+05:30 Cprogrammer
* converted proto to ansic
*
* Revision 1.5 2005-08-23 17:14:46+05:30 Cprogrammer
* gcc 4 compliance
*
* Revision 1.4 2004-10-22 20:23:24+05:30 Cprogrammer
* added RCS id
*
* Revision 1.3 2004-10-22 15:34:27+05:30 Cprogrammer
* replaced readwrite.h with unistd.h
*
* Revision 1.2 2004-07-17 21:17:28+05:30 Cprogrammer
* added RCS log
*
*/
#include <unistd.h>
#include "seek.h"
#include "alloc.h"
#include "cdbmss.h"
int
cdbmss_start(struct cdbmss *c, int fd)
{
cdbmake_init(&c->cdbm);
c->fd = fd;
c->pos = sizeof(c->cdbm.final);
substdio_fdbuf(&c->ss, (ssize_t (*)(int, char *, size_t)) write, fd, c->ssbuf, sizeof(c->ssbuf));
return seek_set(fd, (seek_pos) c->pos);
}
int
cdbmss_add(struct cdbmss *c, unsigned char *key, unsigned int keylen, unsigned char *data, unsigned int datalen)
{
uint32 h;
int i;
cdbmake_pack((unsigned char *) c->packbuf, (uint32) keylen);
cdbmake_pack((unsigned char *) c->packbuf + 4, (uint32) datalen);
if (substdio_put(&c->ss, c->packbuf, 8) == -1)
return -1;
if (substdio_put(&c->ss, (char *) key, (int) keylen) == -1)
return -1;
if (substdio_put(&c->ss, (char *) data, (int) datalen) == -1)
return -1;
h = CDBMAKE_HASHSTART;
for (i = 0; i < keylen; ++i)
h = cdbmake_hashadd(h, (unsigned int) key[i]);
if (!cdbmake_add(&c->cdbm, h, c->pos, alloc))
return -1;
c->pos += 8 + keylen + datalen; /*- XXX: overflow? */
return 0;
}
int
cdbmss_finish(struct cdbmss *c)
{
int i;
uint32 len;
uint32 u;
if (!cdbmake_split(&c->cdbm, alloc))
return -1;
for (i = 0; i < 256; ++i) {
len = cdbmake_throw(&c->cdbm, c->pos, i);
for (u = 0; u < len; ++u) {
cdbmake_pack((unsigned char *) c->packbuf, c->cdbm.hash[u].h);
cdbmake_pack((unsigned char *) c->packbuf + 4, c->cdbm.hash[u].p);
if (substdio_put(&c->ss, c->packbuf, 8) == -1)
return -1;
c->pos += 8; /*- XXX: overflow? */
}
}
if (substdio_flush(&c->ss) == -1 ||
seek_begin(c->fd) == -1)
return -1;
return substdio_putflush(&c->ss, c->cdbm.final, sizeof(c->cdbm.final));
}
void
getversion_cdbmss_c()
{
const char *x = "$Id: cdbmss.c,v 1.8 2025-01-21 22:19:11+05:30 Cprogrammer Exp mbhangui $";
x++;
}