[PATCH 6/8] CLI: refactor dumping of tags.
[notmuch-archives.git] / 31 / ea39d1ba25ff9795f386d177381539dc01fa33
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 65617431FBC\r
6         for <notmuch@notmuchmail.org>; Sun,  2 Dec 2012 05:33:56 -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 zFT9sp1drW3A for <notmuch@notmuchmail.org>;\r
16         Sun,  2 Dec 2012 05:33:51 -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 DC0A4431FBD\r
21         for <notmuch@notmuchmail.org>; Sun,  2 Dec 2012 05:33:49 -0800 (PST)\r
22 Received: from fctnnbsc30w-142167090129.dhcp-dynamic.fibreop.nb.bellaliant.net\r
23         ([142.167.90.129] 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 1Tf9g0-0005w0-BJ; Sun, 02 Dec 2012 09:33:49 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.80)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1Tf9fu-0001rJ-Sg; Sun, 02 Dec 2012 09:33:38 -0400\r
31 From: david@tethera.net\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [patch v3 1/6] hex-escape: (en|de)code strings to/from restricted\r
34         character set\r
35 Date: Sun,  2 Dec 2012 09:33:19 -0400\r
36 Message-Id: <1354455204-6908-2-git-send-email-david@tethera.net>\r
37 X-Mailer: git-send-email 1.7.10.4\r
38 In-Reply-To: <1354455204-6908-1-git-send-email-david@tethera.net>\r
39 References: <1354455204-6908-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, 02 Dec 2012 13:33:57 -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   |  161 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
65  util/hex-escape.h   |   41 +++++++++++++\r
66  3 files changed, 203 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..b7e2e07\r
86 --- /dev/null\r
87 +++ b/util/hex-escape.c\r
88 @@ -0,0 +1,161 @@\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 char *p;\r
152 +    char *q;\r
153 +\r
154 +    size_t needed = 1;  /* for the NUL */\r
155 +\r
156 +    assert (ctx); assert (in); assert (out); assert (out_size);\r
157 +\r
158 +    for (p = in; *p; p++) {\r
159 +       needed += is_output (*p) ? 1 : 3;\r
160 +    }\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 = 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", (unsigned char)*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 needed = 1; /* for the NUL */\r
236 +\r
237 +    assert (ctx); assert (in); assert (out); assert (out_size);\r
238 +\r
239 +    for (p = in; *p; p++)\r
240 +       if ((p[0] == escape_char) && isxdigit (p[1]) && isxdigit (p[2]))\r
241 +           needed -= 1;\r
242 +       else\r
243 +           needed += 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