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 DC6FF42116A
\r
6 for <notmuch@notmuchmail.org>; Sat, 31 Mar 2012 15:17:45 -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 pKSerVo0XCdY for <notmuch@notmuchmail.org>;
\r
17 Sat, 31 Mar 2012 15:17:43 -0700 (PDT)
\r
18 Received: from mail-bk0-f53.google.com (mail-bk0-f53.google.com
\r
19 [209.85.214.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 468C2429E54
\r
22 for <notmuch@notmuchmail.org>; Sat, 31 Mar 2012 15:17:39 -0700 (PDT)
\r
23 Received: by mail-bk0-f53.google.com with SMTP id j4so1547751bkw.26
\r
24 for <notmuch@notmuchmail.org>; Sat, 31 Mar 2012 15:17:39 -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=Hz2+KZxj5Kpv8YFLNT74TEGUMY0ki3yMWdw2jyYy7aU=;
\r
30 b=KzcnRddeJBeD62HBrsoLh9zZixtJAmB1Uk5GBxtGJ7jCeMUYgmqnc4y7hH6qAS2Gzj
\r
31 Lxny33RgWS4uMGZBN+7ZZ9qqz33YtFgcQM2CSitkzbNxfYsNqzoXFXDqiBNLlt7yrNqm
\r
32 lzI6v6FChp/6gXv1UudfiHJ5rgImMaSl49TQ7JsmsOaD6FYxes/sQVrO669whrW2jTst
\r
33 ZVcEBlDmfceWn6Vlrjvhw5o2zD2RUMGabF7BXHXe7OML8GPlHbCRlE972PXsbFVUSqQY
\r
34 gGW1WbiSDQ7tdUzrxVgciSZq98DzS9IMmFSyVwjwyNbvtS5mxisKypgElurBw3B+Qdgr
\r
36 Received: by 10.204.153.203 with SMTP id l11mr1400223bkw.31.1333232258791;
\r
37 Sat, 31 Mar 2012 15:17:38 -0700 (PDT)
\r
38 Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.
\r
40 by mx.google.com with ESMTPS id f5sm28556643bke.9.2012.03.31.15.17.34
\r
41 (version=SSLv3 cipher=OTHER); Sat, 31 Mar 2012 15:17:37 -0700 (PDT)
\r
42 From: Jani Nikula <jani@nikula.org>
\r
43 To: notmuch@notmuchmail.org
\r
44 Subject: [PATCH 1/8] hex-escape: (en|de)code strings to/from restricted
\r
46 Date: Sun, 1 Apr 2012 01:17:21 +0300
\r
48 <3431e1b916320f0ab19b412ad877955f446de443.1333231401.git.jani@nikula.org>
\r
49 X-Mailer: git-send-email 1.7.5.4
\r
50 In-Reply-To: <cover.1333231401.git.jani@nikula.org>
\r
51 References: <cover.1333231401.git.jani@nikula.org>
\r
52 In-Reply-To: <cover.1333231401.git.jani@nikula.org>
\r
53 References: <cover.1333231401.git.jani@nikula.org>
\r
55 ALoCoQnbqd8opq8R24U6UJWITmt05o206ubzmI8cfcwPxP3i70dnH8Nra6FC1uoMdrKiaG+Zc97Q
\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, 31 Mar 2012 22:17: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 util/Makefile.local | 2 +-
\r
76 util/hex-escape.c | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++
\r
77 util/hex-escape.h | 32 +++++++++++
\r
78 3 files changed, 189 insertions(+), 1 deletions(-)
\r
79 create mode 100644 util/hex-escape.c
\r
80 create mode 100644 util/hex-escape.h
\r
82 diff --git a/util/Makefile.local b/util/Makefile.local
\r
83 index c7cae61..3ca623e 100644
\r
84 --- a/util/Makefile.local
\r
85 +++ b/util/Makefile.local
\r
88 extra_cflags += -I$(srcdir)/$(dir)
\r
90 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c
\r
91 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c
\r
93 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
95 diff --git a/util/hex-escape.c b/util/hex-escape.c
\r
96 new file mode 100644
\r
97 index 0000000..6c1260b
\r
99 +++ b/util/hex-escape.c
\r
101 +/* hex-escape.c - Manage encoding and decoding of byte strings into path names
\r
103 + * Copyright (c) 2011 David Bremner
\r
105 + * This program is free software: you can redistribute it and/or modify
\r
106 + * it under the terms of the GNU General Public License as published by
\r
107 + * the Free Software Foundation, either version 3 of the License, or
\r
108 + * (at your option) any later version.
\r
110 + * This program is distributed in the hope that it will be useful,
\r
111 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
112 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
113 + * GNU General Public License for more details.
\r
115 + * You should have received a copy of the GNU General Public License
\r
116 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
118 + * Author: David Bremner <david@tethera.net>
\r
121 +#include <assert.h>
\r
122 +#include <string.h>
\r
123 +#include <talloc.h>
\r
124 +#include "error_util.h"
\r
125 +#include "hex-escape.h"
\r
127 +static const size_t default_buf_size = 1024;
\r
129 +static const char *output_charset =
\r
130 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";
\r
132 +static const int escape_char = '%';
\r
135 +is_output (char c)
\r
137 + return (strchr (output_charset, c) != NULL);
\r
141 +maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)
\r
143 + if (*out_size < needed) {
\r
145 + if (*out == NULL)
\r
146 + *out = talloc_size (ctx, needed);
\r
148 + *out = talloc_realloc (ctx, *out, char, needed);
\r
150 + if (*out == NULL)
\r
153 + *out_size = needed;
\r
159 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)
\r
162 + const unsigned char *p;
\r
165 + size_t escape_count = 0;
\r
169 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
171 + for (p = (unsigned char *) in; *p; p++) {
\r
172 + escape_count += (!is_output (*p));
\r
176 + needed = len + escape_count * 2 + 1;
\r
178 + if (*out == NULL)
\r
181 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
182 + return HEX_OUT_OF_MEMORY;
\r
185 + p = (unsigned char *) in;
\r
188 + if (is_output (*p)) {
\r
191 + sprintf (q, "%%%02x", *p++);
\r
197 + return HEX_SUCCESS;
\r
201 +hex_decode (void *ctx, const char *in, char **out, size_t * out_size)
\r
207 + unsigned char *q;
\r
209 + size_t escape_count = 0;
\r
210 + size_t needed = 0;
\r
212 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
214 + size_t len = strlen (in);
\r
216 + for (p = in; *p; p++)
\r
217 + escape_count += (*p == escape_char);
\r
219 + needed = len - escape_count * 2 + 1;
\r
221 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
222 + return HEX_OUT_OF_MEMORY;
\r
225 + q = (unsigned char *) *out;
\r
230 + if (*p == escape_char) {
\r
235 + return HEX_SYNTAX_ERROR;
\r
240 + *q = strtol (buf, &endp, 16);
\r
242 + if (endp != buf + 2)
\r
243 + return HEX_SYNTAX_ERROR;
\r
255 + return HEX_SUCCESS;
\r
257 diff --git a/util/hex-escape.h b/util/hex-escape.h
\r
258 new file mode 100644
\r
259 index 0000000..e409626
\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 is modelled on that for getline.
\r
275 + * If 'out' points to a NULL pointer a char array of the appropriate
\r
276 + * size is allocated using talloc, and out_size is updated.
\r
278 + * If 'out' points to a non-NULL pointer, it assumed to describe an
\r
279 + * existing char array, with the size given in *out_size. This array
\r
280 + * may be resized by talloc_realloc if needed; in this case *out_size
\r
281 + * will also be updated.
\r
283 + * Note that it is an error to pass a NULL pointer for any parameter
\r
284 + * of these routines.
\r
288 +hex_encode (void *talloc_ctx, const char *in, char **out,
\r
289 + size_t *out_size);
\r
292 +hex_decode (void *talloc_ctx, const char *in, char **out,
\r
293 + size_t *out_size);
\r