1 Return-Path: <bremner@tesseract.cs.unb.ca>
\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 497CC431FB6
\r
6 for <notmuch@notmuchmail.org>; Tue, 14 Aug 2012 02:02:38 -0700 (PDT)
\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 AhA5PorKchVX for <notmuch@notmuchmail.org>;
\r
16 Tue, 14 Aug 2012 02:02:37 -0700 (PDT)
\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 46CD1431FAE
\r
21 for <notmuch@notmuchmail.org>; Tue, 14 Aug 2012 02:02:37 -0700 (PDT)
\r
22 Received: from remotemail by tesseract.cs.unb.ca with local (Exim 4.72)
\r
23 (envelope-from <bremner@tesseract.cs.unb.ca>)
\r
24 id 1T1D1G-00061U-9w; Tue, 14 Aug 2012 06:02:34 -0300
\r
25 Received: (nullmailer pid 5271 invoked by uid 1000);
\r
26 Mon, 13 Aug 2012 20:14:06 -0000
\r
27 From: David Bremner <bremner@debian.org>
\r
28 To: notmuch@notmuchmail.org
\r
29 Subject: [Patch v2 1/6] hex-escape: (en|de)code strings to/from restricted
\r
31 Date: Mon, 13 Aug 2012 22:13:46 +0200
\r
32 Message-Id: <1344888831-4301-2-git-send-email-bremner@debian.org>
\r
33 X-Mailer: git-send-email 1.7.10.4
\r
34 In-Reply-To: <1344888831-4301-1-git-send-email-bremner@debian.org>
\r
35 References: <1344888831-4301-1-git-send-email-bremner@debian.org>
\r
36 Cc: David Bremner <bremner@debian.org>
\r
37 X-BeenThere: notmuch@notmuchmail.org
\r
38 X-Mailman-Version: 2.1.13
\r
40 List-Id: "Use and development of the notmuch mail system."
\r
41 <notmuch.notmuchmail.org>
\r
42 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
43 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
44 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
45 List-Post: <mailto:notmuch@notmuchmail.org>
\r
46 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
47 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
48 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
49 X-List-Received-Date: Tue, 14 Aug 2012 09:02:38 -0000
\r
51 The character set is chosen to be suitable for pathnames, and the same
\r
52 as that used by contrib/nmbug
\r
54 [With additions by Jani Nikula]
\r
56 util/Makefile.local | 2 +-
\r
57 util/hex-escape.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++
\r
58 util/hex-escape.h | 41 +++++++++++++
\r
59 3 files changed, 210 insertions(+), 1 deletion(-)
\r
60 create mode 100644 util/hex-escape.c
\r
61 create mode 100644 util/hex-escape.h
\r
63 diff --git a/util/Makefile.local b/util/Makefile.local
\r
64 index c7cae61..3ca623e 100644
\r
65 --- a/util/Makefile.local
\r
66 +++ b/util/Makefile.local
\r
69 extra_cflags += -I$(srcdir)/$(dir)
\r
71 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c
\r
72 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c
\r
74 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
76 diff --git a/util/hex-escape.c b/util/hex-escape.c
\r
77 new file mode 100644
\r
78 index 0000000..d8905d0
\r
80 +++ b/util/hex-escape.c
\r
82 +/* hex-escape.c - Manage encoding and decoding of byte strings into path names
\r
84 + * Copyright (c) 2011 David Bremner
\r
86 + * This program is free software: you can redistribute it and/or modify
\r
87 + * it under the terms of the GNU General Public License as published by
\r
88 + * the Free Software Foundation, either version 3 of the License, or
\r
89 + * (at your option) any later version.
\r
91 + * This program is distributed in the hope that it will be useful,
\r
92 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
93 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
94 + * GNU General Public License for more details.
\r
96 + * You should have received a copy of the GNU General Public License
\r
97 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
99 + * Author: David Bremner <david@tethera.net>
\r
102 +#include <assert.h>
\r
103 +#include <string.h>
\r
104 +#include <talloc.h>
\r
105 +#include <ctype.h>
\r
106 +#include "error_util.h"
\r
107 +#include "hex-escape.h"
\r
109 +static const size_t default_buf_size = 1024;
\r
111 +static const char *output_charset =
\r
112 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";
\r
114 +static const char escape_char = '%';
\r
117 +is_output (char c)
\r
119 + return (strchr (output_charset, c) != NULL);
\r
123 +maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)
\r
125 + if (*out_size < needed) {
\r
127 + if (*out == NULL)
\r
128 + *out = talloc_size (ctx, needed);
\r
130 + *out = talloc_realloc (ctx, *out, char, needed);
\r
132 + if (*out == NULL)
\r
135 + *out_size = needed;
\r
141 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)
\r
144 + const unsigned char *p;
\r
147 + size_t escape_count = 0;
\r
151 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
153 + for (p = (unsigned char *) in; *p; p++) {
\r
154 + escape_count += (!is_output (*p));
\r
158 + needed = len + escape_count * 2 + 1;
\r
160 + if (*out == NULL)
\r
163 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
164 + return HEX_OUT_OF_MEMORY;
\r
167 + p = (unsigned char *) in;
\r
170 + if (is_output (*p)) {
\r
173 + sprintf (q, "%%%02x", *p++);
\r
179 + return HEX_SUCCESS;
\r
182 +/* Hex decode 'in' to 'out'.
\r
184 + * This must succeed for in == out to support hex_decode_inplace().
\r
186 +static hex_status_t
\r
187 +hex_decode_internal (const char *in, unsigned char *out)
\r
192 + if (*in == escape_char) {
\r
195 + /* This also handles unexpected end-of-string. */
\r
196 + if (!isxdigit ((unsigned char) in[1]) ||
\r
197 + !isxdigit ((unsigned char) in[2]))
\r
198 + return HEX_SYNTAX_ERROR;
\r
204 + *out = strtoul (buf, &endp, 16);
\r
206 + if (endp != buf + 2)
\r
207 + return HEX_SYNTAX_ERROR;
\r
218 + return HEX_SUCCESS;
\r
222 +hex_decode_inplace (char *s)
\r
224 + /* A decoded string is never longer than the encoded one, so it is
\r
225 + * safe to decode a string onto itself. */
\r
226 + return hex_decode_internal (s, (unsigned char *) s);
\r
230 +hex_decode (void *ctx, const char *in, char **out, size_t * out_size)
\r
233 + size_t escape_count = 0;
\r
234 + size_t needed = 0;
\r
236 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
238 + size_t len = strlen (in);
\r
240 + for (p = in; *p; p++)
\r
241 + escape_count += (*p == escape_char);
\r
243 + needed = len - escape_count * 2 + 1;
\r
245 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
246 + return HEX_OUT_OF_MEMORY;
\r
248 + return hex_decode_internal (in, (unsigned char *) *out);
\r
250 diff --git a/util/hex-escape.h b/util/hex-escape.h
\r
251 new file mode 100644
\r
252 index 0000000..5182042
\r
254 +++ b/util/hex-escape.h
\r
256 +#ifndef _HEX_ESCAPE_H
\r
257 +#define _HEX_ESCAPE_H
\r
259 +typedef enum hex_status {
\r
261 + HEX_SYNTAX_ERROR,
\r
262 + HEX_OUT_OF_MEMORY
\r
266 + * The API for hex_encode() and hex_decode() is modelled on that for
\r
269 + * If 'out' points to a NULL pointer a char array of the appropriate
\r
270 + * size is allocated using talloc, and out_size is updated.
\r
272 + * If 'out' points to a non-NULL pointer, it assumed to describe an
\r
273 + * existing char array, with the size given in *out_size. This array
\r
274 + * may be resized by talloc_realloc if needed; in this case *out_size
\r
275 + * will also be updated.
\r
277 + * Note that it is an error to pass a NULL pointer for any parameter
\r
278 + * of these routines.
\r
282 +hex_encode (void *talloc_ctx, const char *in, char **out,
\r
283 + size_t *out_size);
\r
286 +hex_decode (void *talloc_ctx, const char *in, char **out,
\r
287 + size_t *out_size);
\r
290 + * Non-allocating hex decode to decode 's' in-place. The length of the
\r
291 + * result is always equal to or shorter than the length of the
\r
295 +hex_decode_inplace (char *s);
\r