--- /dev/null
+Return-Path: <tomi.ollila@iki.fi>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 7DD3E431FBC\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Apr 2014 03:21:45 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 1.775\r
+X-Spam-Level: *\r
+X-Spam-Status: No, score=1.775 tagged_above=-999 required=5\r
+ tests=[URIBL_BLACK=1.775] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id 7mVhW2YiBtCZ for <notmuch@notmuchmail.org>;\r
+ Sun, 6 Apr 2014 03:21:36 -0700 (PDT)\r
+Received: from guru.guru-group.fi (guru.guru-group.fi [46.183.73.34])\r
+ by olra.theworths.org (Postfix) with ESMTP id 77B8A431FB6\r
+ for <notmuch@notmuchmail.org>; Sun, 6 Apr 2014 03:21:36 -0700 (PDT)\r
+Received: from guru.guru-group.fi (localhost [IPv6:::1])\r
+ by guru.guru-group.fi (Postfix) with ESMTP id E0EB9100086;\r
+ Sun, 6 Apr 2014 13:21:29 +0300 (EEST)\r
+From: Tomi Ollila <tomi.ollila@iki.fi>\r
+To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org\r
+Subject: Re: [Patch v7 2/6] util: add gz_readline\r
+In-Reply-To: <1396712636-10640-3-git-send-email-david@tethera.net>\r
+References: <1396712636-10640-1-git-send-email-david@tethera.net>\r
+ <1396712636-10640-3-git-send-email-david@tethera.net>\r
+User-Agent: Notmuch/0.17+174~gef82849 (http://notmuchmail.org) Emacs/24.3.1\r
+ (x86_64-unknown-linux-gnu)\r
+X-Face: HhBM'cA~<r"^Xv\KRN0P{vn'Y"Kd;zg_y3S[4)KSN~s?O\"QPoL\r
+ $[Xv_BD:i/F$WiEWax}R(MPS`^UaptOGD`*/=@\1lKoVa9tnrg0TW?"r7aRtgk[F\r
+ !)g;OY^,BjTbr)Np:%c_o'jj,Z\r
+Date: Sun, 06 Apr 2014 13:21:29 +0300\r
+Message-ID: <m2y4ziahcm.fsf@guru.guru-group.fi>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sun, 06 Apr 2014 10:21:45 -0000\r
+\r
+On Sat, Apr 05 2014, David Bremner <david@tethera.net> wrote:\r
+\r
+> The idea is to provide a more or less drop in replacement for readline\r
+> to read from zlib/gzip streams. Take the opportunity to replace\r
+> malloc with talloc.\r
+> ---\r
+> util/Makefile.local | 3 +-\r
+> util/util.c | 24 +++++++++++++++\r
+> util/util.h | 29 ++++++++++++++++++\r
+> util/zlib-extra.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++\r
+> util/zlib-extra.h | 25 ++++++++++++++++\r
+> 5 files changed, 165 insertions(+), 1 deletion(-)\r
+> create mode 100644 util/util.c\r
+> create mode 100644 util/util.h\r
+> create mode 100644 util/zlib-extra.c\r
+> create mode 100644 util/zlib-extra.h\r
+>\r
+> diff --git a/util/Makefile.local b/util/Makefile.local\r
+> index 29c0ce6..905f237 100644\r
+> --- a/util/Makefile.local\r
+> +++ b/util/Makefile.local\r
+> @@ -4,7 +4,8 @@ dir := util\r
+> extra_cflags += -I$(srcdir)/$(dir)\r
+> \r
+> libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c \\r
+> - $(dir)/string-util.c $(dir)/talloc-extra.c\r
+> + $(dir)/string-util.c $(dir)/talloc-extra.c $(dir)/zlib-extra.c \\r
+> + $(dir)/util.c\r
+> \r
+> libutil_modules := $(libutil_c_srcs:.c=.o)\r
+> \r
+> diff --git a/util/util.c b/util/util.c\r
+> new file mode 100644\r
+> index 0000000..3bd305d\r
+> --- /dev/null\r
+> +++ b/util/util.c\r
+> @@ -0,0 +1,24 @@\r
+> +#include "util.h"\r
+> +#include "error_util.h"\r
+> +#include <string.h>\r
+> +#include <errno.h>\r
+> +\r
+> +const char *\r
+> +util_error_string (util_status_t errnum)\r
+> +{\r
+> + switch (errnum) {\r
+> + case UTIL_SUCCESS:\r
+> + return "none";\r
+\r
+The series gets OK from me. This "none" could be amended to "success" to be\r
+'consistent' with libc strerror(0) -- which returns 'Success'.\r
+\r
+\r
+Tomi\r
+\r
+\r
+> + case UTIL_OUT_OF_MEMORY:\r
+> + return "out of memory";\r
+> + case UTIL_EOF:\r
+> + return "end of file";\r
+> + case UTIL_ERRNO:\r
+> + return strerror (errno);\r
+> + case UTIL_GZERROR:\r
+> + /* we lack context to be more informative here */\r
+> + return "zlib error";\r
+> + default:\r
+> + INTERNAL_ERROR("unexpected error status %d", errnum);\r
+> + }\r
+> +}\r
+> diff --git a/util/util.h b/util/util.h\r
+> new file mode 100644\r
+> index 0000000..d12fadb\r
+> --- /dev/null\r
+> +++ b/util/util.h\r
+> @@ -0,0 +1,29 @@\r
+> +#ifndef _UTIL_H\r
+> +#define _UTIL_H\r
+> +\r
+> +typedef enum util_status {\r
+> + /**\r
+> + * No error occurred.\r
+> + */\r
+> + UTIL_SUCCESS = 0,\r
+> + /**\r
+> + * Out of memory.\r
+> + */\r
+> + UTIL_OUT_OF_MEMORY,\r
+> + /**\r
+> + * End of stream reached while attempting to read.\r
+> + */\r
+> + UTIL_EOF,\r
+> + /**\r
+> + * Low level error occured, consult errno.\r
+> + */\r
+> + UTIL_ERRNO,\r
+> + /**\r
+> + * Zlib error occured, call gzerror for details.\r
+> + */\r
+> + UTIL_GZERROR\r
+> +} util_status_t;\r
+> +\r
+> +const char *\r
+> +util_error_string (util_status_t status);\r
+> +#endif\r
+> diff --git a/util/zlib-extra.c b/util/zlib-extra.c\r
+> new file mode 100644\r
+> index 0000000..cb34845\r
+> --- /dev/null\r
+> +++ b/util/zlib-extra.c\r
+> @@ -0,0 +1,85 @@\r
+> +/* zlib-extra.c - Extra or enhanced routines for compressed I/O.\r
+> + *\r
+> + * Copyright (c) 2014 David Bremner\r
+> + *\r
+> + * This program is free software: you can redistribute it and/or modify\r
+> + * it under the terms of the GNU General Public License as published by\r
+> + * the Free Software Foundation, either version 3 of the License, or\r
+> + * (at your option) any later version.\r
+> + *\r
+> + * This program is distributed in the hope that it will be useful,\r
+> + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+> + * GNU General Public License for more details.\r
+> + *\r
+> + * You should have received a copy of the GNU General Public License\r
+> + * along with this program. If not, see http://www.gnu.org/licenses/ .\r
+> + *\r
+> + * Author: David Bremner <david@tethera.net>\r
+> + */\r
+> +\r
+> +#include "zlib-extra.h"\r
+> +#include <talloc.h>\r
+> +#include <stdio.h>\r
+> +#include <string.h>\r
+> +\r
+> +/* mimic POSIX/glibc getline, but on a zlib gzFile stream, and using talloc */\r
+> +util_status_t\r
+> +gz_getline (void *talloc_ctx, char **bufptr, ssize_t *bytes_read, gzFile stream)\r
+> +{\r
+> + char *buf = *bufptr;\r
+> + unsigned int len;\r
+> + size_t offset = 0;\r
+> +\r
+> + if (buf) {\r
+> + len = talloc_array_length (buf);\r
+> + } else {\r
+> + /* same as getdelim from gnulib */\r
+> + len = 120;\r
+> + buf = talloc_array (talloc_ctx, char, len);\r
+> + if (buf == NULL)\r
+> + return UTIL_OUT_OF_MEMORY;\r
+> + }\r
+> +\r
+> + while (1) {\r
+> + if (! gzgets (stream, buf + offset, len - offset)) {\r
+> + /* Null indicates EOF or error */\r
+> + int zlib_status = 0;\r
+> + (void) gzerror (stream, &zlib_status);\r
+> + switch (zlib_status) {\r
+> + case Z_OK:\r
+> + /* no data read before EOF */\r
+> + if (offset == 0)\r
+> + return UTIL_EOF;\r
+> + else\r
+> + goto SUCCESS;\r
+> + case Z_ERRNO:\r
+> + return UTIL_ERRNO;\r
+> + default:\r
+> + return UTIL_GZERROR;\r
+> + }\r
+> + }\r
+> +\r
+> + offset += strlen (buf + offset);\r
+> +\r
+> + if (buf[offset - 1] == '\n')\r
+> + goto SUCCESS;\r
+> +\r
+> + len *= 2;\r
+> + buf = talloc_realloc (talloc_ctx, buf, char, len);\r
+> + if (buf == NULL)\r
+> + return UTIL_OUT_OF_MEMORY;\r
+> + }\r
+> + SUCCESS:\r
+> + *bufptr = buf;\r
+> + *bytes_read = offset;\r
+> + return UTIL_SUCCESS;\r
+> +}\r
+> +\r
+> +const char *gz_error_string (util_status_t status, gzFile file) \r
+> +{\r
+> + if (status == UTIL_GZERROR)\r
+> + return gzerror (file, NULL);\r
+> + else\r
+> + return util_error_string (status);\r
+> +}\r
+> diff --git a/util/zlib-extra.h b/util/zlib-extra.h\r
+> new file mode 100644\r
+> index 0000000..dbaa0b9\r
+> --- /dev/null\r
+> +++ b/util/zlib-extra.h\r
+> @@ -0,0 +1,25 @@\r
+> +#ifndef _ZLIB_EXTRA_H\r
+> +#define _ZLIB_EXTRA_H\r
+> +\r
+> +#include "util.h"\r
+> +#include <zlib.h>\r
+> +\r
+> +/* Like getline, but read from a gzFile. Allocation is with talloc.\r
+> + * Returns:\r
+> + *\r
+> + * UTIL_SUCCESS, UTIL_OUT_OF_MEMORY, UTIL_ERRNO, UTIL_GZERROR\r
+> + * Consult util.h for description\r
+> + *\r
+> + * UTIL_EOF End of file encountered before \r
+> + * any characters read\r
+> + */\r
+> +util_status_t\r
+> +gz_getline (void *ctx, char **lineptr, ssize_t *bytes_read, gzFile stream);\r
+> +\r
+> +/* return a suitable error string based on the return status\r
+> + * from gz_readline\r
+> + */\r
+> +\r
+> +const char *\r
+> +gz_error_string (util_status_t status, gzFile stream);\r
+> +#endif\r
+> -- \r
+> 1.9.0\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r