Re: [PATCH v4 01/16] add util/search-path.{c, h} to test for executables in $PATH
[notmuch-archives.git] / 09 / d9ae2c347fc19c5ee8a741262d7c97f9fd92e3
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 497CC431FB6\r
6         for <notmuch@notmuchmail.org>; Tue, 14 Aug 2012 02:02:38 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 0\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none]\r
12         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 AhA5PorKchVX for <notmuch@notmuchmail.org>;\r
16         Tue, 14 Aug 2012 02:02:37 -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 46CD1431FAE\r
21         for <notmuch@notmuchmail.org>; Tue, 14 Aug 2012 02:02:37 -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 1T1D1G-00061U-9w; Tue, 14 Aug 2012 06:02:34 -0300\r
25 Received: (nullmailer pid 5271 invoked by uid 1000);\r
26         Mon, 13 Aug 2012 20:14:06 -0000\r
27 From: David Bremner <bremner@debian.org>\r
28 To: notmuch@notmuchmail.org\r
29 Subject: [Patch v2 1/6] hex-escape: (en|de)code strings to/from restricted\r
30         character set\r
31 Date: Mon, 13 Aug 2012 22:13:46 +0200\r
32 Message-Id: <1344888831-4301-2-git-send-email-bremner@debian.org>\r
33 X-Mailer: git-send-email 1.7.10.4\r
34 In-Reply-To: <1344888831-4301-1-git-send-email-bremner@debian.org>\r
35 References: <1344888831-4301-1-git-send-email-bremner@debian.org>\r
36 Cc: David Bremner <bremner@debian.org>\r
37 X-BeenThere: notmuch@notmuchmail.org\r
38 X-Mailman-Version: 2.1.13\r
39 Precedence: list\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: Tue, 14 Aug 2012 09:02:38 -0000\r
50 \r
51 The character set is chosen to be suitable for pathnames, and the same\r
52 as that used by contrib/nmbug\r
53 \r
54 [With additions by Jani Nikula]\r
55 ---\r
56  util/Makefile.local |    2 +-\r
57  util/hex-escape.c   |  168 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
58  util/hex-escape.h   |   41 +++++++++++++\r
59  3 files changed, 210 insertions(+), 1 deletion(-)\r
60  create mode 100644 util/hex-escape.c\r
61  create mode 100644 util/hex-escape.h\r
62 \r
63 diff --git a/util/Makefile.local b/util/Makefile.local\r
64 index c7cae61..3ca623e 100644\r
65 --- a/util/Makefile.local\r
66 +++ b/util/Makefile.local\r
67 @@ -3,7 +3,7 @@\r
68  dir := util\r
69  extra_cflags += -I$(srcdir)/$(dir)\r
70  \r
71 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c\r
72 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c\r
73  \r
74  libutil_modules := $(libutil_c_srcs:.c=.o)\r
75  \r
76 diff --git a/util/hex-escape.c b/util/hex-escape.c\r
77 new file mode 100644\r
78 index 0000000..d8905d0\r
79 --- /dev/null\r
80 +++ b/util/hex-escape.c\r
81 @@ -0,0 +1,168 @@\r
82 +/* hex-escape.c -  Manage encoding and decoding of byte strings into path names\r
83 + *\r
84 + * Copyright (c) 2011 David Bremner\r
85 + *\r
86 + * This program is free software: you can redistribute it and/or modify\r
87 + * it under the terms of the GNU General Public License as published by\r
88 + * the Free Software Foundation, either version 3 of the License, or\r
89 + * (at your option) any later version.\r
90 + *\r
91 + * This program is distributed in the hope that it will be useful,\r
92 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
93 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
94 + * GNU General Public License for more details.\r
95 + *\r
96 + * You should have received a copy of the GNU General Public License\r
97 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
98 + *\r
99 + * Author: David Bremner <david@tethera.net>\r
100 + */\r
101 +\r
102 +#include <assert.h>\r
103 +#include <string.h>\r
104 +#include <talloc.h>\r
105 +#include <ctype.h>\r
106 +#include "error_util.h"\r
107 +#include "hex-escape.h"\r
108 +\r
109 +static const size_t default_buf_size = 1024;\r
110 +\r
111 +static const char *output_charset =\r
112 +    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";\r
113 +\r
114 +static const char escape_char = '%';\r
115 +\r
116 +static int\r
117 +is_output (char c)\r
118 +{\r
119 +    return (strchr (output_charset, c) != NULL);\r
120 +}\r
121 +\r
122 +static int\r
123 +maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)\r
124 +{\r
125 +    if (*out_size < needed) {\r
126 +\r
127 +       if (*out == NULL)\r
128 +           *out = talloc_size (ctx, needed);\r
129 +       else\r
130 +           *out = talloc_realloc (ctx, *out, char, needed);\r
131 +\r
132 +       if (*out == NULL)\r
133 +           return 0;\r
134 +\r
135 +       *out_size = needed;\r
136 +    }\r
137 +    return 1;\r
138 +}\r
139 +\r
140 +hex_status_t\r
141 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)\r
142 +{\r
143 +\r
144 +    const unsigned char *p;\r
145 +    char *q;\r
146 +\r
147 +    size_t escape_count = 0;\r
148 +    size_t len = 0;\r
149 +    size_t needed;\r
150 +\r
151 +    assert (ctx); assert (in); assert (out); assert (out_size);\r
152 +\r
153 +    for (p = (unsigned char *) in; *p; p++) {\r
154 +       escape_count += (!is_output (*p));\r
155 +       len++;\r
156 +    }\r
157 +\r
158 +    needed = len + escape_count * 2 + 1;\r
159 +\r
160 +    if (*out == NULL)\r
161 +       *out_size = 0;\r
162 +\r
163 +    if (!maybe_realloc (ctx, needed, out, out_size))\r
164 +       return HEX_OUT_OF_MEMORY;\r
165 +\r
166 +    q = *out;\r
167 +    p = (unsigned char *) in;\r
168 +\r
169 +    while (*p) {\r
170 +       if (is_output (*p)) {\r
171 +           *q++ = *p++;\r
172 +       } else {\r
173 +           sprintf (q, "%%%02x", *p++);\r
174 +           q += 3;\r
175 +       }\r
176 +    }\r
177 +\r
178 +    *q = '\0';\r
179 +    return HEX_SUCCESS;\r
180 +}\r
181 +\r
182 +/* Hex decode 'in' to 'out'.\r
183 + *\r
184 + * This must succeed for in == out to support hex_decode_inplace().\r
185 + */\r
186 +static hex_status_t\r
187 +hex_decode_internal (const char *in, unsigned char *out)\r
188 +{\r
189 +    char buf[3];\r
190 +\r
191 +    while (*in) {\r
192 +       if (*in == escape_char) {\r
193 +           char *endp;\r
194 +\r
195 +           /* This also handles unexpected end-of-string. */\r
196 +           if (!isxdigit ((unsigned char) in[1]) ||\r
197 +               !isxdigit ((unsigned char) in[2]))\r
198 +               return HEX_SYNTAX_ERROR;\r
199 +\r
200 +           buf[0] = in[1];\r
201 +           buf[1] = in[2];\r
202 +           buf[2] = '\0';\r
203 +\r
204 +           *out = strtoul (buf, &endp, 16);\r
205 +\r
206 +           if (endp != buf + 2)\r
207 +               return HEX_SYNTAX_ERROR;\r
208 +\r
209 +           in += 3;\r
210 +           out++;\r
211 +       } else {\r
212 +           *out++ = *in++;\r
213 +       }\r
214 +    }\r
215 +\r
216 +    *out = '\0';\r
217 +\r
218 +    return HEX_SUCCESS;\r
219 +}\r
220 +\r
221 +hex_status_t\r
222 +hex_decode_inplace (char *s)\r
223 +{\r
224 +    /* A decoded string is never longer than the encoded one, so it is\r
225 +     * safe to decode a string onto itself. */\r
226 +    return hex_decode_internal (s, (unsigned char *) s);\r
227 +}\r
228 +\r
229 +hex_status_t\r
230 +hex_decode (void *ctx, const char *in, char **out, size_t * out_size)\r
231 +{\r
232 +    const char *p;\r
233 +    size_t escape_count = 0;\r
234 +    size_t needed = 0;\r
235 +\r
236 +    assert (ctx); assert (in); assert (out); assert (out_size);\r
237 +\r
238 +    size_t len = strlen (in);\r
239 +\r
240 +    for (p = in; *p; p++)\r
241 +       escape_count += (*p == escape_char);\r
242 +\r
243 +    needed = len - escape_count * 2 + 1;\r
244 +\r
245 +    if (!maybe_realloc (ctx, needed, out, out_size))\r
246 +       return HEX_OUT_OF_MEMORY;\r
247 +\r
248 +    return hex_decode_internal (in, (unsigned char *) *out);\r
249 +}\r
250 diff --git a/util/hex-escape.h b/util/hex-escape.h\r
251 new file mode 100644\r
252 index 0000000..5182042\r
253 --- /dev/null\r
254 +++ b/util/hex-escape.h\r
255 @@ -0,0 +1,41 @@\r
256 +#ifndef _HEX_ESCAPE_H\r
257 +#define _HEX_ESCAPE_H\r
258 +\r
259 +typedef enum hex_status {\r
260 +    HEX_SUCCESS = 0,\r
261 +    HEX_SYNTAX_ERROR,\r
262 +    HEX_OUT_OF_MEMORY\r
263 +} hex_status_t;\r
264 +\r
265 +/*\r
266 + * The API for hex_encode() and hex_decode() is modelled on that for\r
267 + * getline.\r
268 + *\r
269 + * If 'out' points to a NULL pointer a char array of the appropriate\r
270 + * size is allocated using talloc, and out_size is updated.\r
271 + *\r
272 + * If 'out' points to a non-NULL pointer, it assumed to describe an\r
273 + * existing char array, with the size given in *out_size.  This array\r
274 + * may be resized by talloc_realloc if needed; in this case *out_size\r
275 + * will also be updated.\r
276 + *\r
277 + * Note that it is an error to pass a NULL pointer for any parameter\r
278 + * of these routines.\r
279 + */\r
280 +\r
281 +hex_status_t\r
282 +hex_encode (void *talloc_ctx, const char *in, char **out,\r
283 +            size_t *out_size);\r
284 +\r
285 +hex_status_t\r
286 +hex_decode (void *talloc_ctx, const char *in, char **out,\r
287 +            size_t *out_size);\r
288 +\r
289 +/*\r
290 + * Non-allocating hex decode to decode 's' in-place. The length of the\r
291 + * result is always equal to or shorter than the length of the\r
292 + * original.\r
293 + */\r
294 +hex_status_t\r
295 +hex_decode_inplace (char *s);\r
296 +#endif\r
297 -- \r
298 1.7.10.4\r
299 \r