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 9D01E431FBF
\r
6 for <notmuch@notmuchmail.org>; Mon, 20 Aug 2012 00:18:40 -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 Xy3gQrXicApC for <notmuch@notmuchmail.org>;
\r
16 Mon, 20 Aug 2012 00:18:39 -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 5A562431FBD
\r
21 for <notmuch@notmuchmail.org>; Mon, 20 Aug 2012 00:18:38 -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 1T3MFx-0002G4-Lc; Mon, 20 Aug 2012 04:18:37 -0300
\r
25 Received: (nullmailer pid 7809 invoked by uid 1000);
\r
26 Sun, 19 Aug 2012 13:19:07 -0000
\r
27 From: david@tethera.net
\r
28 To: notmuch@notmuchmail.org
\r
29 Subject: [Patch v3 1/6] hex-escape: (en|de)code strings to/from restricted
\r
31 Date: Sun, 19 Aug 2012 15:18:29 +0200
\r
32 Message-Id: <1345382314-5330-2-git-send-email-david@tethera.net>
\r
33 X-Mailer: git-send-email 1.7.10.4
\r
34 In-Reply-To: <1345382314-5330-1-git-send-email-david@tethera.net>
\r
35 References: <1345382314-5330-1-git-send-email-david@tethera.net>
\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: Mon, 20 Aug 2012 07:18:40 -0000
\r
51 From: David Bremner <bremner@debian.org>
\r
53 The character set is chosen to be suitable for pathnames, and the same
\r
54 as that used by contrib/nmbug
\r
56 [With additions by Jani Nikula]
\r
58 util/Makefile.local | 2 +-
\r
59 util/hex-escape.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++
\r
60 util/hex-escape.h | 41 +++++++++++++
\r
61 3 files changed, 210 insertions(+), 1 deletion(-)
\r
62 create mode 100644 util/hex-escape.c
\r
63 create mode 100644 util/hex-escape.h
\r
65 diff --git a/util/Makefile.local b/util/Makefile.local
\r
66 index c7cae61..3ca623e 100644
\r
67 --- a/util/Makefile.local
\r
68 +++ b/util/Makefile.local
\r
71 extra_cflags += -I$(srcdir)/$(dir)
\r
73 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c
\r
74 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c
\r
76 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
78 diff --git a/util/hex-escape.c b/util/hex-escape.c
\r
79 new file mode 100644
\r
80 index 0000000..d8905d0
\r
82 +++ b/util/hex-escape.c
\r
84 +/* hex-escape.c - Manage encoding and decoding of byte strings into path names
\r
86 + * Copyright (c) 2011 David Bremner
\r
88 + * This program is free software: you can redistribute it and/or modify
\r
89 + * it under the terms of the GNU General Public License as published by
\r
90 + * the Free Software Foundation, either version 3 of the License, or
\r
91 + * (at your option) any later version.
\r
93 + * This program is distributed in the hope that it will be useful,
\r
94 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
95 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
96 + * GNU General Public License for more details.
\r
98 + * You should have received a copy of the GNU General Public License
\r
99 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
101 + * Author: David Bremner <david@tethera.net>
\r
104 +#include <assert.h>
\r
105 +#include <string.h>
\r
106 +#include <talloc.h>
\r
107 +#include <ctype.h>
\r
108 +#include "error_util.h"
\r
109 +#include "hex-escape.h"
\r
111 +static const size_t default_buf_size = 1024;
\r
113 +static const char *output_charset =
\r
114 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";
\r
116 +static const char escape_char = '%';
\r
119 +is_output (char c)
\r
121 + return (strchr (output_charset, c) != NULL);
\r
125 +maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)
\r
127 + if (*out_size < needed) {
\r
129 + if (*out == NULL)
\r
130 + *out = talloc_size (ctx, needed);
\r
132 + *out = talloc_realloc (ctx, *out, char, needed);
\r
134 + if (*out == NULL)
\r
137 + *out_size = needed;
\r
143 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)
\r
146 + const unsigned char *p;
\r
149 + size_t escape_count = 0;
\r
153 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
155 + for (p = (unsigned char *) in; *p; p++) {
\r
156 + escape_count += (!is_output (*p));
\r
160 + needed = len + escape_count * 2 + 1;
\r
162 + if (*out == NULL)
\r
165 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
166 + return HEX_OUT_OF_MEMORY;
\r
169 + p = (unsigned char *) in;
\r
172 + if (is_output (*p)) {
\r
175 + sprintf (q, "%%%02x", *p++);
\r
181 + return HEX_SUCCESS;
\r
184 +/* Hex decode 'in' to 'out'.
\r
186 + * This must succeed for in == out to support hex_decode_inplace().
\r
188 +static hex_status_t
\r
189 +hex_decode_internal (const char *in, unsigned char *out)
\r
194 + if (*in == escape_char) {
\r
197 + /* This also handles unexpected end-of-string. */
\r
198 + if (!isxdigit ((unsigned char) in[1]) ||
\r
199 + !isxdigit ((unsigned char) in[2]))
\r
200 + return HEX_SYNTAX_ERROR;
\r
206 + *out = strtoul (buf, &endp, 16);
\r
208 + if (endp != buf + 2)
\r
209 + return HEX_SYNTAX_ERROR;
\r
220 + return HEX_SUCCESS;
\r
224 +hex_decode_inplace (char *s)
\r
226 + /* A decoded string is never longer than the encoded one, so it is
\r
227 + * safe to decode a string onto itself. */
\r
228 + return hex_decode_internal (s, (unsigned char *) s);
\r
232 +hex_decode (void *ctx, const char *in, char **out, size_t * out_size)
\r
235 + size_t escape_count = 0;
\r
236 + size_t needed = 0;
\r
238 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
240 + size_t len = strlen (in);
\r
242 + for (p = in; *p; p++)
\r
243 + escape_count += (*p == escape_char);
\r
245 + needed = len - escape_count * 2 + 1;
\r
247 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
248 + return HEX_OUT_OF_MEMORY;
\r
250 + return hex_decode_internal (in, (unsigned char *) *out);
\r
252 diff --git a/util/hex-escape.h b/util/hex-escape.h
\r
253 new file mode 100644
\r
254 index 0000000..5182042
\r
256 +++ b/util/hex-escape.h
\r
258 +#ifndef _HEX_ESCAPE_H
\r
259 +#define _HEX_ESCAPE_H
\r
261 +typedef enum hex_status {
\r
263 + HEX_SYNTAX_ERROR,
\r
264 + HEX_OUT_OF_MEMORY
\r
268 + * The API for hex_encode() and hex_decode() is modelled on that for
\r
271 + * If 'out' points to a NULL pointer a char array of the appropriate
\r
272 + * size is allocated using talloc, and out_size is updated.
\r
274 + * If 'out' points to a non-NULL pointer, it assumed to describe an
\r
275 + * existing char array, with the size given in *out_size. This array
\r
276 + * may be resized by talloc_realloc if needed; in this case *out_size
\r
277 + * will also be updated.
\r
279 + * Note that it is an error to pass a NULL pointer for any parameter
\r
280 + * of these routines.
\r
284 +hex_encode (void *talloc_ctx, const char *in, char **out,
\r
285 + size_t *out_size);
\r
288 +hex_decode (void *talloc_ctx, const char *in, char **out,
\r
289 + size_t *out_size);
\r
292 + * Non-allocating hex decode to decode 's' in-place. The length of the
\r
293 + * result is always equal to or shorter than the length of the
\r
297 +hex_decode_inplace (char *s);
\r