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