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