From: Jonathan McDowell Date: Mon, 31 May 2004 23:47:07 +0000 (+0000) Subject: cscvs to tla changeset 27 X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/commitdiff_plain/62078c1601192c2594b954a122ac44a0c319c9bd?hp=340e776784a8902d43e6c4658c45254202e2779e cscvs to tla changeset 27 Author: noodles Date: 2002/11/11 14:15:54 Pull out some common fetch/putchar functions into charfuncs.c --- diff --git a/Makefile b/Makefile index 4eb0ad7..2d6cfee 100644 --- a/Makefile +++ b/Makefile @@ -11,10 +11,10 @@ LIBS = -L/usr/local/lib -ldb3 PROGS = add lookup gpgwww onak OBJS = armor.o parsekey.o keydb_$(DBTYPE).o merge.o keyid.o md5.o sha.o \ - getcgi.o keyindex.o mem.o stats.o ll.o hash.o onak-conf.o + getcgi.o keyindex.o mem.o stats.o ll.o hash.o onak-conf.o charfuncs.o SRCS = armor.c parsekey.c merge.c keyid.c md5.c sha.c main.c getcgi.c stats.c \ keyindex.c mem.c lookup.c add.c keydb_$(DBTYPE).c ll.c hash.c \ - gpgwww.c onak-conf.c + gpgwww.c onak-conf.c charfuncs.c all: $(PROGS) testparse maxpath @@ -28,22 +28,22 @@ gpgwww: gpgwww.o $(OBJS) $(CC) -o gpgwww gpgwww.o $(OBJS) $(LIBS) lookup: lookup.o getcgi.o keyindex.o keydb_$(DBTYPE).o keyid.o sha.o \ - parsekey.o mem.o armor.o ll.o hash.o onak-conf.o + parsekey.o mem.o armor.o ll.o hash.o onak-conf.o charfuncs.o $(CC) -o lookup lookup.o getcgi.o keyindex.o keydb_$(DBTYPE).o keyid.o \ - sha.o parsekey.o mem.o armor.o ll.o hash.o onak-conf.o $(LIBS) + sha.o parsekey.o mem.o armor.o ll.o hash.o onak-conf.o \ + charfuncs.o $(LIBS) add: add.o getcgi.o armor.o parsekey.o keydb_$(DBTYPE).o keyid.o sha.o mem.o \ - keyindex.o ll.o hash.o merge.o onak-conf.o + keyindex.o ll.o hash.o merge.o onak-conf.o charfuncs.o $(CC) -o add add.o getcgi.o armor.o parsekey.o keydb_$(DBTYPE).o \ keyid.o sha.o mem.o keyindex.o ll.o hash.o merge.o onak-conf.o \ - $(LIBS) + charfuncs.o $(LIBS) -onak: onak.o merge.o keyid.o sha.o armor.o parsekey.o ll.o \ +onak: onak.o merge.o keyid.o sha.o armor.o parsekey.o ll.o charfuncs.o \ keydb_$(DBTYPE).o mem.o keyindex.o hash.o getcgi.o onak-conf.o - $(CC) $(LDFLAGS) -o onak onak.o merge.o keyid.o sha.o armor.o parsekey.o \ - keydb_$(DBTYPE).o mem.o keyindex.o ll.o hash.o getcgi.o \ - onak-conf.o $(LIBS) - + $(CC) $(LDFLAGS) -o onak onak.o merge.o keyid.o sha.o armor.o \ + parsekey.o keydb_$(DBTYPE).o mem.o keyindex.o ll.o hash.o \ + getcgi.o charfuncs.o onak-conf.o $(LIBS) clean: rm -f $(PROGS) $(OBJS) Makefile.bak testparse maxpath *.core core \ diff --git a/add.c b/add.c index 95f2189..80afadd 100644 --- a/add.c +++ b/add.c @@ -12,37 +12,19 @@ #include #include "armor.h" +#include "charfuncs.h" #include "getcgi.h" #include "keydb.h" #include "keystructs.h" #include "parsekey.h" #include "merge.h" -struct cgi_get_ctx { - char *buffer; - int offset; -}; - - -int cgi_getchar(void *ctx, size_t count, unsigned char *c) -{ - struct cgi_get_ctx *buf = NULL; - - buf = (struct cgi_get_ctx *) ctx; - - while (count-- > 0 && *c != 0) { - *c = buf->buffer[buf->offset++]; - } - - return (*c == 0); -} - int main(int argc, char *argv[]) { struct openpgp_packet_list *packets = NULL; struct openpgp_publickey *keys = NULL; char **params = NULL; - struct cgi_get_ctx ctx; + struct buffer_ctx ctx; int i; memset(&ctx, 0, sizeof(ctx)); @@ -51,6 +33,7 @@ int main(int argc, char *argv[]) for (i = 0; params != NULL && params[i] != NULL; i += 2) { if (!strcmp(params[i], "keytext")) { ctx.buffer = params[i+1]; + ctx.size = strlen(ctx.buffer); } else { free(params[i+1]); } @@ -67,7 +50,7 @@ int main(int argc, char *argv[]) if (ctx.buffer == NULL) { puts("Error: No keytext to add supplied."); } else { - dearmor_openpgp_stream(cgi_getchar, + dearmor_openpgp_stream(buffer_fetchchar, &ctx, &packets); if (packets != NULL) { diff --git a/charfuncs.c b/charfuncs.c new file mode 100644 index 0000000..de68b66 --- /dev/null +++ b/charfuncs.c @@ -0,0 +1,81 @@ +/* + * charfuncs.c - Routines for dealing with character streams. + * + * Jonathan McDowell + * + * Copyright 2002 Project Purple + */ + +#include +#include +#include + +#include "charfuncs.h" + +/** + * buffer_fetchchar - Fetches a char from a buffer. + * @ctx: Our buffer context structure. + * @count: The number of characters to get from the buffer. + * @c: Where to put the characters retrieved. + */ +int buffer_fetchchar(void *ctx, size_t count, unsigned char *c) +{ + struct buffer_ctx *buf = NULL; + int i; + + buf = (struct buffer_ctx *) ctx; + for (i = 0; i < count; i++) { + c[i] = buf->buffer[buf->offset++]; + } + + return (((buf->offset) == (buf->size)) ? 1 : 0); +} + +/** + * buffer_putchar - Puts a char to a buffer. + * @ctx: Our buffer context structure. + * @count: The number of characters to put into the buffer. + * @c: The characters to add to the buffer. + * + * Adds characters to the buffer references by the buffer context. If we + * fill it then we double the size of the current buffer and then add the + * rest. + */ +int buffer_putchar(void *ctx, size_t count, unsigned char *c) +{ + struct buffer_ctx *buf = NULL; + size_t newsize = 0; + int i; + + buf = (struct buffer_ctx *) ctx; + + for (newsize = buf->size; newsize < (buf->offset + count); + newsize *= 2) ; + + if (newsize != buf->size) { + buf->buffer = realloc(buf->buffer, newsize); + buf->size = newsize; + } + + for (i = 0; i < count; i++) { + buf->buffer[buf->offset++] = c[i]; + } + + return 1; +} + +/** + * file_fetchchar - Fetches a char from a file. + */ +int file_fetchchar(void *fd, size_t count, unsigned char *c) +{ + return !(read( *(int *) fd, c, count)); +} + +/** + * file_putchar - Puts a char to a file. + */ +int file_putchar(void *fd, size_t count, unsigned char *c) +{ + return !(write( *(int *) fd, c, count)); +} diff --git a/charfuncs.h b/charfuncs.h new file mode 100644 index 0000000..f394e15 --- /dev/null +++ b/charfuncs.h @@ -0,0 +1,57 @@ +/* + * charfuncs.h - Routines for dealing with character streams. + * + * Jonathan McDowell + * + * Copyright 2002 Project Purple + */ + +#ifndef __CHARFUNCS_H__ +#define __CHARFUNCS_H__ + +#include + +/** + * buffer_ctx - Shared with CGI buffer stuff... + * @buffer: The data buffer. + * @offset: Our current position in the buffer. + * @size: The size of the data buffer. + */ +struct buffer_ctx { + char *buffer; + int offset; + int size; +}; + +/** + * buffer_fetchchar - Fetches a char from a buffer. + * @ctx: Our buffer context structure. + * @count: The number of characters to get from the buffer. + * @c: Where to put the characters retrieved. + */ +int buffer_fetchchar(void *ctx, size_t count, unsigned char *c); + +/** + * buffer_putchar - Puts a char to a buffer. + * @ctx: Our buffer context structure. + * @count: The number of characters to put into the buffer. + * @c: The characters to add to the buffer. + * + * Adds characters to the buffer references by the buffer context. If we + * fill it then we double the size of the current buffer and then add the + * rest. + */ +int buffer_putchar(void *ctx, size_t count, unsigned char *c); + +/** + * file_fetchchar - Fetches a char from a file. + */ +int file_fetchchar(void *fd, size_t count, unsigned char *c); + +/** + * file_putchar - Puts a char to a file. + */ +int file_putchar(void *fd, size_t count, unsigned char *c); + + +#endif /* __CHARFUNCS_H__ */ diff --git a/keydb_db2.c b/keydb_db2.c index 3d66c5a..a30b62c 100644 --- a/keydb_db2.c +++ b/keydb_db2.c @@ -16,6 +16,7 @@ #include #include +#include "charfuncs.h" #include "keydb.h" #include "keyid.h" #include "keyindex.h" @@ -41,40 +42,6 @@ static DB **db2_keydbfiles = NULL; */ static DB_ENV db2_env; -/* - * Shared with CGI buffer stuff... - */ -struct db2_get_ctx { - char *buffer; - int offset; - int size; -}; - -/** - * keydb_fetchchar - Fetches a char from a buffer. - */ -int keydb_fetchchar(void *ctx, int count, unsigned char *c) -{ - struct db2_get_ctx *buf = NULL; - int i; - - buf = (struct db2_get_ctx *) ctx; - for (i = 0; i < count; i++) { - c[i] = buf->buffer[buf->offset++]; - } - - return (((buf->offset) == (buf->size)) ? 1 : 0); -} - -/** - * keydb_putchar - Puts a char to a file. - */ -static int keydb_putchar(void *fd, unsigned char c) -{ -// return !(lo_write(dbconn, *(int *) fd, &c, sizeof(c))); - return 1; -} - DB *keydb(DBT *key) { /* @@ -196,7 +163,7 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, int ret; DBT key, data; char id[KEYDB_KEYID_BYTES]; - struct db2_get_ctx fetchbuf; + struct buffer_ctx fetchbuf; memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); @@ -214,7 +181,7 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, fetchbuf.buffer = data.data; fetchbuf.offset = 0; fetchbuf.size = data.size; - read_openpgp_stream(keydb_fetchchar, &fetchbuf, &packets); + read_openpgp_stream(buffer_fetchchar, &fetchbuf, &packets); parse_keys(packets, publickey); } diff --git a/keydb_db3.c b/keydb_db3.c index 263397a..5224b52 100644 --- a/keydb_db3.c +++ b/keydb_db3.c @@ -18,6 +18,7 @@ #include +#include "charfuncs.h" #include "keydb.h" #include "keyid.h" #include "keyindex.h" @@ -36,57 +37,6 @@ static DB *dbconn = NULL; */ static DB *worddb = NULL; -/** - * db3_get_ctx - Shared with CGI buffer stuff... - */ -struct db3_get_ctx { - char *buffer; - int offset; - int size; -}; - -/** - * keydb_fetchchar - Fetches a char from a file. - */ -static int keydb_fetchchar(void *ctx, size_t count, unsigned char *c) -{ - struct db3_get_ctx *buf = NULL; - int i; - - buf = (struct db3_get_ctx *) ctx; - for (i = 0; i < count; i++) { - c[i] = buf->buffer[buf->offset++]; - } - - return (((buf->offset) == (buf->size)) ? 1 : 0); -} - -/** - * keydb_putchar - Puts a char to a file. - */ -static int keydb_putchar(void *ctx, size_t count, unsigned char *c) -{ - struct db3_get_ctx *buf = NULL; - size_t newsize = 0; - int i; - - buf = (struct db3_get_ctx *) ctx; - - for (newsize = buf->size; newsize < (buf->offset + count); - newsize *= 2) ; - - if (newsize != buf->size) { - buf->buffer = realloc(buf->buffer, newsize); - buf->size = newsize; - } - - for (i = 0; i < count; i++) { - buf->buffer[buf->offset++] = c[i]; - } - - return 1; -} - /** * makewordlist - Takes a string and splits it into a set of unique words. * @wordlist: The current word list. @@ -242,7 +192,7 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, DBT key, data; int ret = 0; int numkeys = 0; - struct db3_get_ctx fetchbuf; + struct buffer_ctx fetchbuf; memset(&key, 0, sizeof(key)); memset(&data, 0, sizeof(data)); @@ -264,7 +214,7 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, fetchbuf.buffer = data.data; fetchbuf.offset = 0; fetchbuf.size = data.size; - read_openpgp_stream(keydb_fetchchar, &fetchbuf, + read_openpgp_stream(buffer_fetchchar, &fetchbuf, &packets); parse_keys(packets, publickey); numkeys++; @@ -347,7 +297,7 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update) struct openpgp_publickey *next = NULL; int ret = 0; int i = 0; - struct db3_get_ctx storebuf; + struct buffer_ctx storebuf; DBT key; DBT data; uint64_t keyid = 0; @@ -383,7 +333,7 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update) storebuf.size = 8192; storebuf.buffer = malloc(8192); - write_openpgp_stream(keydb_putchar, &storebuf, packets); + write_openpgp_stream(buffer_putchar, &storebuf, packets); /* * Now we have the key data store it in the DB; the keyid is the key. diff --git a/keydb_file.c b/keydb_file.c index 243e59e..ba8dc2e 100644 --- a/keydb_file.c +++ b/keydb_file.c @@ -15,6 +15,7 @@ #include #include +#include "charfuncs.h" #include "keydb.h" #include "keyid.h" #include "keystructs.h" @@ -23,22 +24,6 @@ #include "onak-conf.h" #include "parsekey.h" -/** - * keydb_fetchchar - Fetches a char from a file. - */ -static int keydb_fetchchar(void *fd, size_t count, unsigned char *c) -{ - return !(read( *(int *) fd, c, count)); -} - -/** - * keydb_putchar - Puts a char to a file. - */ -static int keydb_putchar(void *fd, size_t count, unsigned char *c) -{ - return !(write( *(int *) fd, c, count)); -} - /** * initdb - Initialize the key database. * @@ -101,7 +86,7 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, fd = open(keyfile, O_RDONLY); // | O_SHLOCK); if (fd > -1) { - read_openpgp_stream(keydb_fetchchar, &fd, &packets); + read_openpgp_stream(file_fetchchar, &fd, &packets); parse_keys(packets, publickey); close(fd); } @@ -138,7 +123,7 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update) flatten_publickey(publickey, &packets, &list_end); publickey -> next = next; - write_openpgp_stream(keydb_putchar, &fd, packets); + write_openpgp_stream(file_putchar, &fd, packets); close(fd); }