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 8BC4C429E2F
\r
6 for <notmuch@notmuchmail.org>; Sun, 18 Dec 2011 05:15:53 -0800 (PST)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5
\r
12 tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled
\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 th5QwngXGGH9 for <notmuch@notmuchmail.org>;
\r
16 Sun, 18 Dec 2011 05:15:51 -0800 (PST)
\r
17 Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21])
\r
18 (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
\r
19 (No client certificate requested)
\r
20 by olra.theworths.org (Postfix) with ESMTPS id A8238429E35
\r
21 for <notmuch@notmuchmail.org>; Sun, 18 Dec 2011 05:15:43 -0800 (PST)
\r
22 Received: from zancas.localnet
\r
23 (fctnnbsc36w-156034079193.pppoe-dynamic.High-Speed.nb.bellaliant.net
\r
24 [156.34.79.193]) (authenticated bits=0)
\r
25 by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pBIDFc80017876
\r
26 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO);
\r
27 Sun, 18 Dec 2011 09:15:39 -0400
\r
28 Received: from bremner by zancas.localnet with local (Exim 4.77)
\r
29 (envelope-from <bremner@tethera.net>)
\r
30 id 1RcGaY-0008RD-99; Sun, 18 Dec 2011 09:15:38 -0400
\r
31 From: David Bremner <david@tethera.net>
\r
32 To: notmuch@notmuchmail.org
\r
33 Subject: [PATCH Draft 2 1/9] hex-escape: (en|de)code strings to/from
\r
34 restricted character set
\r
35 Date: Sun, 18 Dec 2011 09:15:03 -0400
\r
36 Message-Id: <1324214111-32079-2-git-send-email-david@tethera.net>
\r
37 X-Mailer: git-send-email 1.7.7.3
\r
38 In-Reply-To: <1324214111-32079-1-git-send-email-david@tethera.net>
\r
39 References: <id:87sjkmuck7.fsf@gmail.com>
\r
40 <1324214111-32079-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 Dec 2011 13:15:53 -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 util/Makefile.local | 2 +-
\r
62 util/hex-escape.c | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++
\r
63 util/hex-escape.h | 32 +++++++++++
\r
64 3 files changed, 189 insertions(+), 1 deletions(-)
\r
65 create mode 100644 util/hex-escape.c
\r
66 create mode 100644 util/hex-escape.h
\r
68 diff --git a/util/Makefile.local b/util/Makefile.local
\r
69 index 26e4c3f..2e63932 100644
\r
70 --- a/util/Makefile.local
\r
71 +++ b/util/Makefile.local
\r
74 extra_cflags += -I$(srcdir)/$(dir)
\r
76 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c
\r
77 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c
\r
79 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
81 diff --git a/util/hex-escape.c b/util/hex-escape.c
\r
82 new file mode 100644
\r
83 index 0000000..f58b9a2
\r
85 +++ b/util/hex-escape.c
\r
87 +/* pathname.c - Manage encoding and decoding of byte strings into path names
\r
89 + * Copyright (c) 2011 David Bremner
\r
91 + * This program is free software: you can redistribute it and/or modify
\r
92 + * it under the terms of the GNU General Public License as published by
\r
93 + * the Free Software Foundation, either version 3 of the License, or
\r
94 + * (at your option) any later version.
\r
96 + * This program is distributed in the hope that it will be useful,
\r
97 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
98 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
99 + * GNU General Public License for more details.
\r
101 + * You should have received a copy of the GNU General Public License
\r
102 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
104 + * Author: David Bremner <david@tethera.net>
\r
107 +#include <assert.h>
\r
108 +#include <string.h>
\r
109 +#include <talloc.h>
\r
110 +#include "error_util.h"
\r
111 +#include "hex-escape.h"
\r
113 +static const size_t default_buf_size = 1024;
\r
115 +static const char *output_charset =
\r
116 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";
\r
118 +static const int escape_char = '%';
\r
121 +is_output (char c)
\r
123 + return (strchr (output_charset, c) != NULL);
\r
127 +maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)
\r
129 + if (*out_size < needed) {
\r
131 + if (*out == NULL)
\r
132 + *out = talloc_size (ctx, needed);
\r
134 + *out = talloc_realloc (ctx, *out, char, needed);
\r
136 + if (*out == NULL)
\r
139 + *out_size = needed;
\r
145 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)
\r
148 + const unsigned char *p;
\r
151 + size_t escape_count = 0;
\r
155 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
157 + for (p = (unsigned char *) in; *p; p++) {
\r
158 + escape_count += (!is_output (*p));
\r
162 + needed = len + escape_count * 2 + 1;
\r
164 + if (*out == NULL)
\r
167 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
168 + return HEX_OUT_OF_MEMORY;
\r
171 + p = (unsigned char *) in;
\r
174 + if (is_output (*p)) {
\r
177 + sprintf (q, "%%%02x", *p++);
\r
183 + return HEX_SUCCESS;
\r
187 +hex_decode (void *ctx, const char *in, char **out, size_t * out_size)
\r
193 + unsigned char *q;
\r
195 + size_t escape_count = 0;
\r
196 + size_t needed = 0;
\r
198 + assert (ctx); assert (in); assert (out); assert (out_size);
\r
200 + size_t len = strlen (in);
\r
202 + for (p = in; *p; p++)
\r
203 + escape_count += (*p == escape_char);
\r
205 + needed = len - escape_count * 2 + 1;
\r
207 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
208 + return HEX_OUT_OF_MEMORY;
\r
211 + q = (unsigned char *) *out;
\r
216 + if (*p == escape_char) {
\r
221 + return HEX_SYNTAX_ERROR;
\r
226 + *q = strtol (buf, &endp, 16);
\r
228 + if (endp != buf + 2)
\r
229 + return HEX_SYNTAX_ERROR;
\r
241 + return HEX_SUCCESS;
\r
243 diff --git a/util/hex-escape.h b/util/hex-escape.h
\r
244 new file mode 100644
\r
245 index 0000000..e409626
\r
247 +++ b/util/hex-escape.h
\r
249 +#ifndef _HEX_ESCAPE_H
\r
250 +#define _HEX_ESCAPE_H
\r
252 +typedef enum hex_status {
\r
254 + HEX_SYNTAX_ERROR,
\r
255 + HEX_OUT_OF_MEMORY
\r
259 + * The API is modelled on that for getline.
\r
261 + * If 'out' points to a NULL pointer a char array of the appropriate
\r
262 + * size is allocated using talloc, and out_size is updated.
\r
264 + * If 'out' points to a non-NULL pointer, it assumed to describe an
\r
265 + * existing char array, with the size given in *out_size. This array
\r
266 + * may be resized by talloc_realloc if needed; in this case *out_size
\r
267 + * will also be updated.
\r
269 + * Note that it is an error to pass a NULL pointer for any parameter
\r
270 + * of these routines.
\r
274 +hex_encode (void *talloc_ctx, const char *in, char **out,
\r
275 + size_t *out_size);
\r
278 +hex_decode (void *talloc_ctx, const char *in, char **out,
\r
279 + size_t *out_size);
\r