1 Return-Path: <bremner@pivot.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 537F2429E27
\r
6 for <notmuch@notmuchmail.org>; Tue, 13 Dec 2011 12:28:21 -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 O1bhvtjl1p7n for <notmuch@notmuchmail.org>;
\r
16 Tue, 13 Dec 2011 12:28:20 -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 2DFA3429E28
\r
21 for <notmuch@notmuchmail.org>; Tue, 13 Dec 2011 12:28:18 -0800 (PST)
\r
22 Received: from convex-new.cs.unb.ca ([131.202.13.154])
\r
23 by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pBDKSATR002924;
\r
24 Tue, 13 Dec 2011 16:28:10 -0400
\r
25 Received: from bremner by convex-new.cs.unb.ca with local (Exim 4.72)
\r
26 (envelope-from <bremner@pivot.cs.unb.ca>)
\r
27 id 1RaYxO-0001xH-Dq; Tue, 13 Dec 2011 16:28:10 -0400
\r
28 From: David Bremner <david@tethera.net>
\r
29 To: notmuch@notmuchmail.org
\r
30 Subject: [Alpha PATCH 1/6] util/hex-escape.[ch]: encoding/decoding strings
\r
31 into restricted character set
\r
32 Date: Tue, 13 Dec 2011 16:27:50 -0400
\r
33 Message-Id: <1323808075-7417-2-git-send-email-david@tethera.net>
\r
34 X-Mailer: git-send-email 1.7.5.4
\r
35 In-Reply-To: <1323808075-7417-1-git-send-email-david@tethera.net>
\r
36 References: <1323808075-7417-1-git-send-email-david@tethera.net>
\r
37 Cc: David Bremner <bremner@debian.org>, pere@hungry.com
\r
38 X-BeenThere: notmuch@notmuchmail.org
\r
39 X-Mailman-Version: 2.1.13
\r
41 List-Id: "Use and development of the notmuch mail system."
\r
42 <notmuch.notmuchmail.org>
\r
43 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
44 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
46 List-Post: <mailto:notmuch@notmuchmail.org>
\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
48 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
49 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
50 X-List-Received-Date: Tue, 13 Dec 2011 20:28:21 -0000
\r
52 From: David Bremner <bremner@debian.org>
\r
54 The character set is chosen to be suitable for pathnames, and the same as that
\r
55 used by contrib/nmbug
\r
57 util/Makefile.local | 2 +-
\r
58 util/hex-escape.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++++
\r
59 util/hex-escape.h | 15 +++++
\r
60 3 files changed, 166 insertions(+), 1 deletions(-)
\r
61 create mode 100644 util/hex-escape.c
\r
62 create mode 100644 util/hex-escape.h
\r
64 diff --git a/util/Makefile.local b/util/Makefile.local
\r
65 index 26e4c3f..2e63932 100644
\r
66 --- a/util/Makefile.local
\r
67 +++ b/util/Makefile.local
\r
70 extra_cflags += -I$(srcdir)/$(dir)
\r
72 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c
\r
73 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c
\r
75 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
77 diff --git a/util/hex-escape.c b/util/hex-escape.c
\r
78 new file mode 100644
\r
79 index 0000000..3e08465
\r
81 +++ b/util/hex-escape.c
\r
83 +/* pathname.c - Manage encoding and decoding of byte strings into path names
\r
85 + * Copyright (c) 2011 David Bremner
\r
87 + * This program is free software: you can redistribute it and/or modify
\r
88 + * it under the terms of the GNU General Public License as published by
\r
89 + * the Free Software Foundation, either version 3 of the License, or
\r
90 + * (at your option) any later version.
\r
92 + * This program is distributed in the hope that it will be useful,
\r
93 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
94 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
95 + * GNU General Public License for more details.
\r
97 + * You should have received a copy of the GNU General Public License
\r
98 + * along with this program. If not, see http://www.gnu.org/licenses/ .
\r
100 + * Author: David Bremner <david@tethera.net>
\r
103 +#include <string.h>
\r
104 +#include <talloc.h>
\r
105 +#include "error_util.h"
\r
106 +#include "hex-escape.h"
\r
108 +static const size_t default_buf_size=1024;
\r
110 +static const char* output_charset=
\r
111 + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";
\r
113 +static const char escape_char='%';
\r
116 +is_output (char c) {
\r
117 + return (strchr (output_charset, c) != NULL);
\r
122 +maybe_realloc(void *ctx, size_t needed, char **out, size_t *out_size)
\r
124 + if (*out_size < needed) {
\r
126 + if (*out == NULL)
\r
127 + *out = talloc_size(ctx,needed);
\r
129 + *out = talloc_realloc(ctx,*out,char,needed);
\r
131 + if (*out == NULL)
\r
134 + *out_size = needed;
\r
141 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)
\r
147 + int escape_count=0;
\r
150 + for (p = in; *p; p++)
\r
151 + escape_count += (! is_output (*p));
\r
153 + needed = strlen (in) + 2*escape_count + 1;
\r
155 + if (*out == NULL)
\r
158 + if (!maybe_realloc (ctx, needed, out, out_size))
\r
159 + return HEX_OUT_OF_MEMORY;
\r
165 + if (is_output (*p)) {
\r
168 + sprintf (q, "%%%02x", *p++);
\r
174 + return HEX_SUCCESS;
\r
179 +hex_decode (void *ctx, const char *in, char **out, size_t *out_size) {
\r
186 + size_t escape_count = 0;
\r
187 + size_t needed = 0;
\r
189 + size_t len = strlen (in);
\r
191 + for (p = in; *p; p++)
\r
192 + escape_count += (*p == escape_char);
\r
194 + needed = len - escape_count*2 +1;
\r
196 + if (!maybe_realloc(ctx, needed, out, out_size))
\r
197 + return HEX_OUT_OF_MEMORY;
\r
206 + if (*p == escape_char) {
\r
211 + return HEX_SYNTAX_ERROR;
\r
216 + *q = strtol(buf, &endp, 16);
\r
218 + if (endp != buf+2)
\r
219 + return HEX_SYNTAX_ERROR;
\r
231 + return HEX_SUCCESS;
\r
233 diff --git a/util/hex-escape.h b/util/hex-escape.h
\r
234 new file mode 100644
\r
235 index 0000000..98ecbe0
\r
237 +++ b/util/hex-escape.h
\r
239 +#ifndef _HEX_ESCAPE_H
\r
240 +#define _HEX_ESCAPE_H
\r
242 +typedef enum hex_status {
\r
244 + HEX_SYNTAX_ERROR,
\r
245 + HEX_OUT_OF_MEMORY
\r
249 +hex_encode (void *talloc_ctx, const char *in, char **out, size_t *out_size);
\r
252 +hex_decode (void *talloc_ctx, const char *in, char **out, size_t *out_size);
\r