--- /dev/null
+Return-Path: <bremner@tethera.net>\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 776114143A2\r
+ for <notmuch@notmuchmail.org>; Sat, 14 Jan 2012 17:41:08 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -2.3\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
+ tests=[RCVD_IN_DNSWL_MED=-2.3] 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 5dqC1QzzGpQ3 for <notmuch@notmuchmail.org>;\r
+ Sat, 14 Jan 2012 17:41:05 -0800 (PST)\r
+Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id C6D714143A1\r
+ for <notmuch@notmuchmail.org>; Sat, 14 Jan 2012 17:40:52 -0800 (PST)\r
+Received: from zancas.localnet\r
+ (fctnnbsc36w-156034076032.pppoe-dynamic.High-Speed.nb.bellaliant.net\r
+ [156.34.76.32]) (authenticated bits=0)\r
+ by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id q0F1ef7l008567\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO);\r
+ Sat, 14 Jan 2012 21:40:44 -0400\r
+Received: from bremner by zancas.localnet with local (Exim 4.77)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1RmF5M-00042g-PD; Sat, 14 Jan 2012 21:40:40 -0400\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 01/10] hex-escape: (en|de)code strings to/from restricted\r
+ character set\r
+Date: Sat, 14 Jan 2012 21:40:15 -0400\r
+Message-Id: <1326591624-15493-2-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 1.7.7.3\r
+In-Reply-To: <1326591624-15493-1-git-send-email-david@tethera.net>\r
+References: <874nwxbkhr.fsf@zancas.localnet>\r
+ <1326591624-15493-1-git-send-email-david@tethera.net>\r
+Cc: David Bremner <bremner@debian.org>\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, 15 Jan 2012 01:41:09 -0000\r
+\r
+From: David Bremner <bremner@debian.org>\r
+\r
+The character set is chosen to be suitable for pathnames, and the same\r
+as that used by contrib/nmbug\r
+---\r
+ util/Makefile.local | 2 +-\r
+ util/hex-escape.c | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ util/hex-escape.h | 32 +++++++++++\r
+ 3 files changed, 189 insertions(+), 1 deletions(-)\r
+ create mode 100644 util/hex-escape.c\r
+ create mode 100644 util/hex-escape.h\r
+\r
+diff --git a/util/Makefile.local b/util/Makefile.local\r
+index 26e4c3f..2e63932 100644\r
+--- a/util/Makefile.local\r
++++ b/util/Makefile.local\r
+@@ -3,7 +3,7 @@\r
+ dir := util\r
+ extra_cflags += -I$(srcdir)/$(dir)\r
+ \r
+-libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c\r
++libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c\r
+ \r
+ libutil_modules := $(libutil_c_srcs:.c=.o)\r
+ \r
+diff --git a/util/hex-escape.c b/util/hex-escape.c\r
+new file mode 100644\r
+index 0000000..6c1260b\r
+--- /dev/null\r
++++ b/util/hex-escape.c\r
+@@ -0,0 +1,156 @@\r
++/* hex-escape.c - Manage encoding and decoding of byte strings into path names\r
++ *\r
++ * Copyright (c) 2011 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 <assert.h>\r
++#include <string.h>\r
++#include <talloc.h>\r
++#include "error_util.h"\r
++#include "hex-escape.h"\r
++\r
++static const size_t default_buf_size = 1024;\r
++\r
++static const char *output_charset =\r
++ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";\r
++\r
++static const int escape_char = '%';\r
++\r
++static int\r
++is_output (char c)\r
++{\r
++ return (strchr (output_charset, c) != NULL);\r
++}\r
++\r
++static int\r
++maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)\r
++{\r
++ if (*out_size < needed) {\r
++\r
++ if (*out == NULL)\r
++ *out = talloc_size (ctx, needed);\r
++ else\r
++ *out = talloc_realloc (ctx, *out, char, needed);\r
++\r
++ if (*out == NULL)\r
++ return 0;\r
++\r
++ *out_size = needed;\r
++ }\r
++ return 1;\r
++}\r
++\r
++hex_status_t\r
++hex_encode (void *ctx, const char *in, char **out, size_t *out_size)\r
++{\r
++\r
++ const unsigned char *p;\r
++ char *q;\r
++\r
++ size_t escape_count = 0;\r
++ size_t len = 0;\r
++ size_t needed;\r
++\r
++ assert (ctx); assert (in); assert (out); assert (out_size);\r
++\r
++ for (p = (unsigned char *) in; *p; p++) {\r
++ escape_count += (!is_output (*p));\r
++ len++;\r
++ }\r
++\r
++ needed = len + escape_count * 2 + 1;\r
++\r
++ if (*out == NULL)\r
++ *out_size = 0;\r
++\r
++ if (!maybe_realloc (ctx, needed, out, out_size))\r
++ return HEX_OUT_OF_MEMORY;\r
++\r
++ q = *out;\r
++ p = (unsigned char *) in;\r
++\r
++ while (*p) {\r
++ if (is_output (*p)) {\r
++ *q++ = *p++;\r
++ } else {\r
++ sprintf (q, "%%%02x", *p++);\r
++ q += 3;\r
++ }\r
++ }\r
++\r
++ *q = '\0';\r
++ return HEX_SUCCESS;\r
++}\r
++\r
++hex_status_t\r
++hex_decode (void *ctx, const char *in, char **out, size_t * out_size)\r
++{\r
++\r
++ char buf[3];\r
++\r
++ const char *p;\r
++ unsigned char *q;\r
++\r
++ size_t escape_count = 0;\r
++ size_t needed = 0;\r
++\r
++ assert (ctx); assert (in); assert (out); assert (out_size);\r
++\r
++ size_t len = strlen (in);\r
++\r
++ for (p = in; *p; p++)\r
++ escape_count += (*p == escape_char);\r
++\r
++ needed = len - escape_count * 2 + 1;\r
++\r
++ if (!maybe_realloc (ctx, needed, out, out_size))\r
++ return HEX_OUT_OF_MEMORY;\r
++\r
++ p = in;\r
++ q = (unsigned char *) *out;\r
++ buf[2] = 0;\r
++\r
++ while (*p) {\r
++\r
++ if (*p == escape_char) {\r
++\r
++ char *endp;\r
++\r
++ if (len < 3)\r
++ return HEX_SYNTAX_ERROR;\r
++\r
++ buf[0] = p[1];\r
++ buf[1] = p[2];\r
++\r
++ *q = strtol (buf, &endp, 16);\r
++\r
++ if (endp != buf + 2)\r
++ return HEX_SYNTAX_ERROR;\r
++\r
++ len -= 3;\r
++ p += 3;\r
++ q++;\r
++ } else {\r
++ *q++ = *p++;\r
++ }\r
++ }\r
++\r
++ *q = '\0';\r
++\r
++ return HEX_SUCCESS;\r
++}\r
+diff --git a/util/hex-escape.h b/util/hex-escape.h\r
+new file mode 100644\r
+index 0000000..e409626\r
+--- /dev/null\r
++++ b/util/hex-escape.h\r
+@@ -0,0 +1,32 @@\r
++#ifndef _HEX_ESCAPE_H\r
++#define _HEX_ESCAPE_H\r
++\r
++typedef enum hex_status {\r
++ HEX_SUCCESS = 0,\r
++ HEX_SYNTAX_ERROR,\r
++ HEX_OUT_OF_MEMORY\r
++} hex_status_t;\r
++\r
++/*\r
++ * The API is modelled on that for getline.\r
++ *\r
++ * If 'out' points to a NULL pointer a char array of the appropriate\r
++ * size is allocated using talloc, and out_size is updated.\r
++ *\r
++ * If 'out' points to a non-NULL pointer, it assumed to describe an\r
++ * existing char array, with the size given in *out_size. This array\r
++ * may be resized by talloc_realloc if needed; in this case *out_size\r
++ * will also be updated.\r
++ *\r
++ * Note that it is an error to pass a NULL pointer for any parameter\r
++ * of these routines.\r
++ */\r
++\r
++hex_status_t\r
++hex_encode (void *talloc_ctx, const char *in, char **out,\r
++ size_t *out_size);\r
++\r
++hex_status_t\r
++hex_decode (void *talloc_ctx, const char *in, char **out,\r
++ size_t *out_size);\r
++#endif\r
+-- \r
+1.7.7.3\r
+\r