diff options
Diffstat (limited to 'util')
-rw-r--r-- | util/ChangeLog | 4 | ||||
-rw-r--r-- | util/Makefile.am | 2 | ||||
-rw-r--r-- | util/membuf.c | 83 |
3 files changed, 88 insertions, 1 deletions
diff --git a/util/ChangeLog b/util/ChangeLog index e16df58ee..f1b36ff49 100644 --- a/util/ChangeLog +++ b/util/ChangeLog @@ -1,3 +1,7 @@ +2005-05-19 Werner Koch <[email protected]> + + * membuf.c: New. Taken from gnupg 1.9. + 2005-05-05 David Shaw <[email protected]> * miscutil.c (add_days_to_timestamp): Remove as unused. diff --git a/util/Makefile.am b/util/Makefile.am index cb241da3f..d54aa7a97 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -39,7 +39,7 @@ endif libutil_a_SOURCES = logger.c fileutil.c miscutil.c strgutil.c \ ttyio.c argparse.c memory.c secmem.c errors.c iobuf.c \ dotlock.c http.c srv.h srv.c simple-gettext.c \ - w32reg.c $(assuan_source) + membuf.c w32reg.c $(assuan_source) libutil_a_DEPENDENCIES = @LIBOBJS@ @REGEX_O@ # LIBOBJS is for the replacement functions diff --git a/util/membuf.c b/util/membuf.c new file mode 100644 index 000000000..ea69ee953 --- /dev/null +++ b/util/membuf.c @@ -0,0 +1,83 @@ +/* membuf.c - A simple implementation of a dynamic buffer + * Copyright (C) 2001, 2003 Free Software Foundation, Inc. + * + * This file is part of GnuPG. + * + * GnuPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GnuPG 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include <config.h> +#include <stdlib.h> +#include <errno.h> +#include <string.h> + +#include "util.h" + + +/* A simple implementation of a dynamic buffer. Use init_membuf() to + create a buffer, put_membuf to append bytes and get_membuf to + release and return the buffer. Allocation errors are detected but + only returned at the final get_membuf(), this helps not to clutter + the code with out of core checks. */ + +void +init_membuf (membuf_t *mb, int initiallen) +{ + mb->len = 0; + mb->size = initiallen; + mb->out_of_core = 0; + mb->buf = xmalloc (initiallen); + if (!mb->buf) + mb->out_of_core = errno; +} + + +void +put_membuf (membuf_t *mb, const void *buf, size_t len) +{ + if (mb->out_of_core) + return; + + if (mb->len + len >= mb->size) + { + char *p; + + mb->size += len + 1024; + p = xrealloc (mb->buf, mb->size); + mb->buf = p; + } + memcpy (mb->buf + mb->len, buf, len); + mb->len += len; +} + + +void * +get_membuf (membuf_t *mb, size_t *len) +{ + char *p; + + if (mb->out_of_core) + { + xfree (mb->buf); + mb->buf = NULL; + return NULL; + } + + p = mb->buf; + *len = mb->len; + mb->buf = NULL; + mb->out_of_core = ENOMEM; /* hack to make sure it won't get reused. */ + return p; +} |