1 Return-Path: <jani@nikula.org>
\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 090D7431FD0
\r
6 for <notmuch@notmuchmail.org>; Sat, 14 Apr 2012 05:15:46 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"
\r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5
\r
13 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id agiprwdvByI6 for <notmuch@notmuchmail.org>;
\r
17 Sat, 14 Apr 2012 05:15:43 -0700 (PDT)
\r
18 Received: from mail-lpp01m010-f53.google.com (mail-lpp01m010-f53.google.com
\r
19 [209.85.215.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id A481D431FAF
\r
22 for <notmuch@notmuchmail.org>; Sat, 14 Apr 2012 05:15:41 -0700 (PDT)
\r
23 Received: by lahc1 with SMTP id c1so3043779lah.26
\r
24 for <notmuch@notmuchmail.org>; Sat, 14 Apr 2012 05:15:40 -0700 (PDT)
\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
\r
26 d=google.com; s=20120113;
\r
27 h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references
\r
28 :in-reply-to:references:x-gm-message-state;
\r
29 bh=xzBJ3u3Hut25+Jh3QsGfiqLOJrn/ACcxy3Y1cvJo4hc=;
\r
30 b=ULLhSmVZUmdMKKu68kSyJIGCB5HHUSIXCsiTKEm4uZeLp9NJx+ZTY/F9SPlwmk+Fga
\r
31 ghP33I42a1LchMqY55FBJg4ZS34Lq/Y/4Ri+ptr7nmw00+AzBfwKl3v1arADfkOJA229
\r
32 zHo+heqPZmwD3OqxN7hBXFJ1g3Lqiy2wUCaVLLxUF8LLM8a0tTUzvFFY2QjV/xPk6By1
\r
33 hRlkghjAoxtkd5DYEJMRY5r3/SzwasnWbKVZNlmM8oqaD1ft2YERgSEiEdb7aPwufcnW
\r
34 4ppzBZl3uqbfoferVlCZzGhA9uZTNMS4kEfYHy4BG0DHfGIM2nSdHLH8h6uVUObAhjTU
\r
36 Received: by 10.112.44.42 with SMTP id b10mr2322788lbm.31.1334405740093;
\r
37 Sat, 14 Apr 2012 05:15:40 -0700 (PDT)
\r
38 Received: from localhost (dsl-hkibrasgw4-fe50dc00-68.dhcp.inet.fi.
\r
40 by mx.google.com with ESMTPS id n6sm16004259lbn.11.2012.04.14.05.15.37
\r
41 (version=SSLv3 cipher=OTHER); Sat, 14 Apr 2012 05:15:38 -0700 (PDT)
\r
42 From: Jani Nikula <jani@nikula.org>
\r
43 To: notmuch@notmuchmail.org
\r
44 Subject: [PATCH v2 1/6] hex-escape: (en|de)code strings to/from restricted
\r
46 Date: Sat, 14 Apr 2012 15:15:26 +0300
\r
48 <921e481740d8c546c5f40a4f455a9f245cae005f.1334404979.git.jani@nikula.org>
\r
49 X-Mailer: git-send-email 1.7.5.4
\r
50 In-Reply-To: <cover.1334404979.git.jani@nikula.org>
\r
51 References: <cover.1334404979.git.jani@nikula.org>
\r
52 In-Reply-To: <cover.1334404979.git.jani@nikula.org>
\r
53 References: <cover.1334404979.git.jani@nikula.org>
\r
55 ALoCoQkX02IEqzJVDbGnLG4oNiU/vFZAEep5L5sOWTfw2GyQGhQz49j+V4w6ANEICne+/ddJx/eV
\r
56 X-BeenThere: notmuch@notmuchmail.org
\r
57 X-Mailman-Version: 2.1.13
\r
59 List-Id: "Use and development of the notmuch mail system."
\r
60 <notmuch.notmuchmail.org>
\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
62 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
64 List-Post: <mailto:notmuch@notmuchmail.org>
\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
67 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
68 X-List-Received-Date: Sat, 14 Apr 2012 12:15:46 -0000
\r
70 From: David Bremner <bremner@debian.org>
\r
72 The character set is chosen to be suitable for pathnames, and the same
\r
73 as that used by contrib/nmbug
\r
75 [With additions by Jani Nikula]
\r
77 util/Makefile.local | 2 +-
\r
78 util/hex-escape.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++
\r
79 util/hex-escape.h | 41 ++++++++++++
\r
80 3 files changed, 210 insertions(+), 1 deletions(-)
\r
81 create mode 100644 util/hex-escape.c
\r
82 create mode 100644 util/hex-escape.h
\r
84 diff --git a/util/Makefile.local b/util/Makefile.local
\r
85 index c7cae61..3ca623e 100644
\r
86 --- a/util/Makefile.local
\r
87 +++ b/util/Makefile.local
\r
90 extra_cflags += -I$(srcdir)/$(dir)
\r
92 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c
\r
93 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c
\r
95 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
97 diff --git a/util/hex-escape.c b/util/hex-escape.c
\r
98 new file mode 100644
\r
99 index 0000000..d8905d0
\r
101 +++ b/util/hex-escape.c
\r
103 +/* hex-escape.c - Manage encoding and decoding of byte strings into path names
\r
105 + * Copyright (c) 2011 David Bremner
\r
107 + * This program is free software: you can redistribute it and/or modify
\r
108 + * it under the terms of the GNU General Public License as published by
\r
109 + * the Free Software Foundation, either version 3 of the License, or
\r
110 + * (at your option) any later version.
\r
112 + * This program is distributed in the hope that it will be useful,
\r
113 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
114 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
115 + * GNU General Public License for more details.
\r
117 + * You should have received a copy of the GNU General Public License
\r
118 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
120 + * Author: David Bremner <david@tethera.net>
\r
123 +#include <assert.h>
\r
124 +#include <string.h>
\r
125 +#include <talloc.h>
\r
126 +#include <ctype.h>
\r
127 +#include "error_util.h"
\r
128 +#include "hex-escape.h"
\r
130 +static const size_t default_buf_size = 1024;
\r
132 +static const char *output_charset =
\r
133 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";
\r
135 +static const char escape_char = '%';
\r
138 +is_output (char c)
\r
140 + return (strchr (output_charset, c) != NULL);
\r
144 +maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)
\r
146 + if (*out_size < needed) {
\r
148 + if (*out == NULL)
\r
149 + *out = talloc_size (ctx, needed);
\r
151 + *out = talloc_realloc (ctx, *out, char, needed);
\r
153 + if (*out == NULL)
\r
156 + *out_size = needed;
\r
162 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)
\r
165 + const unsigned char *p;
\r
168 + size_t escape_count = 0;
\r
172 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
174 + for (p = (unsigned char *) in; *p; p++) {
\r
175 + escape_count += (!is_output (*p));
\r
179 + needed = len + escape_count * 2 + 1;
\r
181 + if (*out == NULL)
\r
184 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
185 + return HEX_OUT_OF_MEMORY;
\r
188 + p = (unsigned char *) in;
\r
191 + if (is_output (*p)) {
\r
194 + sprintf (q, "%%%02x", *p++);
\r
200 + return HEX_SUCCESS;
\r
203 +/* Hex decode 'in' to 'out'.
\r
205 + * This must succeed for in == out to support hex_decode_inplace().
\r
207 +static hex_status_t
\r
208 +hex_decode_internal (const char *in, unsigned char *out)
\r
213 + if (*in == escape_char) {
\r
216 + /* This also handles unexpected end-of-string. */
\r
217 + if (!isxdigit ((unsigned char) in[1]) ||
\r
218 + !isxdigit ((unsigned char) in[2]))
\r
219 + return HEX_SYNTAX_ERROR;
\r
225 + *out = strtoul (buf, &endp, 16);
\r
227 + if (endp != buf + 2)
\r
228 + return HEX_SYNTAX_ERROR;
\r
239 + return HEX_SUCCESS;
\r
243 +hex_decode_inplace (char *s)
\r
245 + /* A decoded string is never longer than the encoded one, so it is
\r
246 + * safe to decode a string onto itself. */
\r
247 + return hex_decode_internal (s, (unsigned char *) s);
\r
251 +hex_decode (void *ctx, const char *in, char **out, size_t * out_size)
\r
254 + size_t escape_count = 0;
\r
255 + size_t needed = 0;
\r
257 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
259 + size_t len = strlen (in);
\r
261 + for (p = in; *p; p++)
\r
262 + escape_count += (*p == escape_char);
\r
264 + needed = len - escape_count * 2 + 1;
\r
266 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
267 + return HEX_OUT_OF_MEMORY;
\r
269 + return hex_decode_internal (in, (unsigned char *) *out);
\r
271 diff --git a/util/hex-escape.h b/util/hex-escape.h
\r
272 new file mode 100644
\r
273 index 0000000..5182042
\r
275 +++ b/util/hex-escape.h
\r
277 +#ifndef _HEX_ESCAPE_H
\r
278 +#define _HEX_ESCAPE_H
\r
280 +typedef enum hex_status {
\r
282 + HEX_SYNTAX_ERROR,
\r
283 + HEX_OUT_OF_MEMORY
\r
287 + * The API for hex_encode() and hex_decode() is modelled on that for
\r
290 + * If 'out' points to a NULL pointer a char array of the appropriate
\r
291 + * size is allocated using talloc, and out_size is updated.
\r
293 + * If 'out' points to a non-NULL pointer, it assumed to describe an
\r
294 + * existing char array, with the size given in *out_size. This array
\r
295 + * may be resized by talloc_realloc if needed; in this case *out_size
\r
296 + * will also be updated.
\r
298 + * Note that it is an error to pass a NULL pointer for any parameter
\r
299 + * of these routines.
\r
303 +hex_encode (void *talloc_ctx, const char *in, char **out,
\r
304 + size_t *out_size);
\r
307 +hex_decode (void *talloc_ctx, const char *in, char **out,
\r
308 + size_t *out_size);
\r
311 + * Non-allocating hex decode to decode 's' in-place. The length of the
\r
312 + * result is always equal to or shorter than the length of the
\r
316 +hex_decode_inplace (char *s);
\r