1 Return-Path: <bremner@tethera.net>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id 82890431FBF
\r
6 for <notmuch@notmuchmail.org>; Sun, 18 Nov 2012 11:05:28 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id mHgs6bZyj5W8 for <notmuch@notmuchmail.org>;
\r
16 Sun, 18 Nov 2012 11:05:24 -0800 (PST)
\r
17 Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238])
\r
18 (using TLSv1 with cipher AES256-SHA (256/256 bits))
\r
19 (No client certificate requested)
\r
20 by olra.theworths.org (Postfix) with ESMTPS id 3352D431FC3
\r
21 for <notmuch@notmuchmail.org>; Sun, 18 Nov 2012 11:05:18 -0800 (PST)
\r
22 Received: from fctnnbsc30w-156034089108.dhcp-dynamic.fibreop.nb.bellaliant.net
\r
23 ([156.34.89.108] helo=zancas.localnet)
\r
24 by tesseract.cs.unb.ca with esmtpsa
\r
25 (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72)
\r
26 (envelope-from <bremner@tethera.net>)
\r
27 id 1TaABA-000280-Ls; Sun, 18 Nov 2012 15:05:17 -0400
\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)
\r
29 (envelope-from <bremner@tethera.net>)
\r
30 id 1TaAB5-0001IC-49; Sun, 18 Nov 2012 15:05:11 -0400
\r
31 From: david@tethera.net
\r
32 To: notmuch@notmuchmail.org
\r
33 Subject: [PATCH 01/16] hex-escape: (en|de)code strings to/from restricted
\r
35 Date: Sun, 18 Nov 2012 15:04:43 -0400
\r
36 Message-Id: <1353265498-3839-2-git-send-email-david@tethera.net>
\r
37 X-Mailer: git-send-email 1.7.10.4
\r
38 In-Reply-To: <1353265498-3839-1-git-send-email-david@tethera.net>
\r
39 References: <1353265498-3839-1-git-send-email-david@tethera.net>
\r
41 Cc: David Bremner <bremner@debian.org>
\r
42 X-BeenThere: notmuch@notmuchmail.org
\r
43 X-Mailman-Version: 2.1.13
\r
45 List-Id: "Use and development of the notmuch mail system."
\r
46 <notmuch.notmuchmail.org>
\r
47 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
48 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
49 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
50 List-Post: <mailto:notmuch@notmuchmail.org>
\r
51 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
52 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
53 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
54 X-List-Received-Date: Sun, 18 Nov 2012 19:05:29 -0000
\r
56 From: David Bremner <bremner@debian.org>
\r
58 The character set is chosen to be suitable for pathnames, and the same
\r
59 as that used by contrib/nmbug
\r
61 [With additions by Jani Nikula]
\r
63 util/Makefile.local | 2 +-
\r
64 util/hex-escape.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++
\r
65 util/hex-escape.h | 41 +++++++++++++
\r
66 3 files changed, 210 insertions(+), 1 deletion(-)
\r
67 create mode 100644 util/hex-escape.c
\r
68 create mode 100644 util/hex-escape.h
\r
70 diff --git a/util/Makefile.local b/util/Makefile.local
\r
71 index c7cae61..3ca623e 100644
\r
72 --- a/util/Makefile.local
\r
73 +++ b/util/Makefile.local
\r
76 extra_cflags += -I$(srcdir)/$(dir)
\r
78 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c
\r
79 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c
\r
81 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
83 diff --git a/util/hex-escape.c b/util/hex-escape.c
\r
84 new file mode 100644
\r
85 index 0000000..d8905d0
\r
87 +++ b/util/hex-escape.c
\r
89 +/* hex-escape.c - Manage encoding and decoding of byte strings into path names
\r
91 + * Copyright (c) 2011 David Bremner
\r
93 + * This program is free software: you can redistribute it and/or modify
\r
94 + * it under the terms of the GNU General Public License as published by
\r
95 + * the Free Software Foundation, either version 3 of the License, or
\r
96 + * (at your option) any later version.
\r
98 + * This program is distributed in the hope that it will be useful,
\r
99 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
100 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
101 + * GNU General Public License for more details.
\r
103 + * You should have received a copy of the GNU General Public License
\r
104 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
106 + * Author: David Bremner <david@tethera.net>
\r
109 +#include <assert.h>
\r
110 +#include <string.h>
\r
111 +#include <talloc.h>
\r
112 +#include <ctype.h>
\r
113 +#include "error_util.h"
\r
114 +#include "hex-escape.h"
\r
116 +static const size_t default_buf_size = 1024;
\r
118 +static const char *output_charset =
\r
119 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";
\r
121 +static const char escape_char = '%';
\r
124 +is_output (char c)
\r
126 + return (strchr (output_charset, c) != NULL);
\r
130 +maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)
\r
132 + if (*out_size < needed) {
\r
134 + if (*out == NULL)
\r
135 + *out = talloc_size (ctx, needed);
\r
137 + *out = talloc_realloc (ctx, *out, char, needed);
\r
139 + if (*out == NULL)
\r
142 + *out_size = needed;
\r
148 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)
\r
151 + const unsigned char *p;
\r
154 + size_t escape_count = 0;
\r
158 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
160 + for (p = (unsigned char *) in; *p; p++) {
\r
161 + escape_count += (!is_output (*p));
\r
165 + needed = len + escape_count * 2 + 1;
\r
167 + if (*out == NULL)
\r
170 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
171 + return HEX_OUT_OF_MEMORY;
\r
174 + p = (unsigned char *) in;
\r
177 + if (is_output (*p)) {
\r
180 + sprintf (q, "%%%02x", *p++);
\r
186 + return HEX_SUCCESS;
\r
189 +/* Hex decode 'in' to 'out'.
\r
191 + * This must succeed for in == out to support hex_decode_inplace().
\r
193 +static hex_status_t
\r
194 +hex_decode_internal (const char *in, unsigned char *out)
\r
199 + if (*in == escape_char) {
\r
202 + /* This also handles unexpected end-of-string. */
\r
203 + if (!isxdigit ((unsigned char) in[1]) ||
\r
204 + !isxdigit ((unsigned char) in[2]))
\r
205 + return HEX_SYNTAX_ERROR;
\r
211 + *out = strtoul (buf, &endp, 16);
\r
213 + if (endp != buf + 2)
\r
214 + return HEX_SYNTAX_ERROR;
\r
225 + return HEX_SUCCESS;
\r
229 +hex_decode_inplace (char *s)
\r
231 + /* A decoded string is never longer than the encoded one, so it is
\r
232 + * safe to decode a string onto itself. */
\r
233 + return hex_decode_internal (s, (unsigned char *) s);
\r
237 +hex_decode (void *ctx, const char *in, char **out, size_t * out_size)
\r
240 + size_t escape_count = 0;
\r
241 + size_t needed = 0;
\r
243 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
245 + size_t len = strlen (in);
\r
247 + for (p = in; *p; p++)
\r
248 + escape_count += (*p == escape_char);
\r
250 + needed = len - escape_count * 2 + 1;
\r
252 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
253 + return HEX_OUT_OF_MEMORY;
\r
255 + return hex_decode_internal (in, (unsigned char *) *out);
\r
257 diff --git a/util/hex-escape.h b/util/hex-escape.h
\r
258 new file mode 100644
\r
259 index 0000000..5182042
\r
261 +++ b/util/hex-escape.h
\r
263 +#ifndef _HEX_ESCAPE_H
\r
264 +#define _HEX_ESCAPE_H
\r
266 +typedef enum hex_status {
\r
268 + HEX_SYNTAX_ERROR,
\r
269 + HEX_OUT_OF_MEMORY
\r
273 + * The API for hex_encode() and hex_decode() is modelled on that for
\r
276 + * If 'out' points to a NULL pointer a char array of the appropriate
\r
277 + * size is allocated using talloc, and out_size is updated.
\r
279 + * If 'out' points to a non-NULL pointer, it assumed to describe an
\r
280 + * existing char array, with the size given in *out_size. This array
\r
281 + * may be resized by talloc_realloc if needed; in this case *out_size
\r
282 + * will also be updated.
\r
284 + * Note that it is an error to pass a NULL pointer for any parameter
\r
285 + * of these routines.
\r
289 +hex_encode (void *talloc_ctx, const char *in, char **out,
\r
290 + size_t *out_size);
\r
293 +hex_decode (void *talloc_ctx, const char *in, char **out,
\r
294 + size_t *out_size);
\r
297 + * Non-allocating hex decode to decode 's' in-place. The length of the
\r
298 + * result is always equal to or shorter than the length of the
\r
302 +hex_decode_inplace (char *s);
\r