[PATCH 04/10] Test against man pages in our source dir, not installed ones.
[notmuch-archives.git] / 81 / 015723b889c9f18bdda2ae300c748cf4a55f37
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 8BC4C429E2F\r
6         for <notmuch@notmuchmail.org>; Sun, 18 Dec 2011 05:15:53 -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: -2.3\r
10 X-Spam-Level: \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 th5QwngXGGH9 for <notmuch@notmuchmail.org>;\r
16         Sun, 18 Dec 2011 05:15:51 -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 A8238429E35\r
21         for <notmuch@notmuchmail.org>; Sun, 18 Dec 2011 05:15:43 -0800 (PST)\r
22 Received: from zancas.localnet\r
23         (fctnnbsc36w-156034079193.pppoe-dynamic.High-Speed.nb.bellaliant.net\r
24         [156.34.79.193]) (authenticated bits=0)\r
25         by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pBIDFc80017876\r
26         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO);\r
27         Sun, 18 Dec 2011 09:15:39 -0400\r
28 Received: from bremner by zancas.localnet with local (Exim 4.77)\r
29         (envelope-from <bremner@tethera.net>)\r
30         id 1RcGaY-0008RD-99; Sun, 18 Dec 2011 09:15:38 -0400\r
31 From: David Bremner <david@tethera.net>\r
32 To: notmuch@notmuchmail.org\r
33 Subject: [PATCH Draft 2 1/9] hex-escape: (en|de)code strings to/from\r
34         restricted character set\r
35 Date: Sun, 18 Dec 2011 09:15:03 -0400\r
36 Message-Id: <1324214111-32079-2-git-send-email-david@tethera.net>\r
37 X-Mailer: git-send-email 1.7.7.3\r
38 In-Reply-To: <1324214111-32079-1-git-send-email-david@tethera.net>\r
39 References: <id:87sjkmuck7.fsf@gmail.com>\r
40         <1324214111-32079-1-git-send-email-david@tethera.net>\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 Dec 2011 13:15:53 -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  util/Makefile.local |    2 +-\r
62  util/hex-escape.c   |  156 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
63  util/hex-escape.h   |   32 +++++++++++\r
64  3 files changed, 189 insertions(+), 1 deletions(-)\r
65  create mode 100644 util/hex-escape.c\r
66  create mode 100644 util/hex-escape.h\r
67 \r
68 diff --git a/util/Makefile.local b/util/Makefile.local\r
69 index 26e4c3f..2e63932 100644\r
70 --- a/util/Makefile.local\r
71 +++ b/util/Makefile.local\r
72 @@ -3,7 +3,7 @@\r
73  dir := util\r
74  extra_cflags += -I$(srcdir)/$(dir)\r
75  \r
76 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c\r
77 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c\r
78  \r
79  libutil_modules := $(libutil_c_srcs:.c=.o)\r
80  \r
81 diff --git a/util/hex-escape.c b/util/hex-escape.c\r
82 new file mode 100644\r
83 index 0000000..f58b9a2\r
84 --- /dev/null\r
85 +++ b/util/hex-escape.c\r
86 @@ -0,0 +1,156 @@\r
87 +/* pathname.c -  Manage encoding and decoding of byte strings into path names\r
88 + *\r
89 + * Copyright (c) 2011 David Bremner\r
90 + *\r
91 + * This program is free software: you can redistribute it and/or modify\r
92 + * it under the terms of the GNU General Public License as published by\r
93 + * the Free Software Foundation, either version 3 of the License, or\r
94 + * (at your option) any later version.\r
95 + *\r
96 + * This program is distributed in the hope that it will be useful,\r
97 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
98 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
99 + * GNU General Public License for more details.\r
100 + *\r
101 + * You should have received a copy of the GNU General Public License\r
102 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
103 + *\r
104 + * Author: David Bremner <david@tethera.net>\r
105 + */\r
106 +\r
107 +#include <assert.h>\r
108 +#include <string.h>\r
109 +#include <talloc.h>\r
110 +#include "error_util.h"\r
111 +#include "hex-escape.h"\r
112 +\r
113 +static const size_t default_buf_size = 1024;\r
114 +\r
115 +static const char *output_charset =\r
116 +    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";\r
117 +\r
118 +static const int escape_char = '%';\r
119 +\r
120 +static int\r
121 +is_output (char c)\r
122 +{\r
123 +    return (strchr (output_charset, c) != NULL);\r
124 +}\r
125 +\r
126 +static int\r
127 +maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)\r
128 +{\r
129 +    if (*out_size < needed) {\r
130 +\r
131 +       if (*out == NULL)\r
132 +           *out = talloc_size (ctx, needed);\r
133 +       else\r
134 +           *out = talloc_realloc (ctx, *out, char, needed);\r
135 +\r
136 +       if (*out == NULL)\r
137 +           return 0;\r
138 +\r
139 +       *out_size = needed;\r
140 +    }\r
141 +    return 1;\r
142 +}\r
143 +\r
144 +hex_status_t\r
145 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)\r
146 +{\r
147 +\r
148 +    const unsigned char *p;\r
149 +    char *q;\r
150 +\r
151 +    size_t escape_count = 0;\r
152 +    size_t len = 0;\r
153 +    size_t needed;\r
154 +\r
155 +    assert (ctx); assert (in); assert (out); assert (out_size);\r
156 +\r
157 +    for (p = (unsigned char *) in; *p; p++) {\r
158 +       escape_count += (!is_output (*p));\r
159 +       len++;\r
160 +    }\r
161 +\r
162 +    needed = len + escape_count * 2 + 1;\r
163 +\r
164 +    if (*out == NULL)\r
165 +       *out_size = 0;\r
166 +\r
167 +    if (!maybe_realloc (ctx, needed, out, out_size))\r
168 +       return HEX_OUT_OF_MEMORY;\r
169 +\r
170 +    q = *out;\r
171 +    p = (unsigned char *) in;\r
172 +\r
173 +    while (*p) {\r
174 +       if (is_output (*p)) {\r
175 +           *q++ = *p++;\r
176 +       } else {\r
177 +           sprintf (q, "%%%02x", *p++);\r
178 +           q += 3;\r
179 +       }\r
180 +    }\r
181 +\r
182 +    *q = '\0';\r
183 +    return HEX_SUCCESS;\r
184 +}\r
185 +\r
186 +hex_status_t\r
187 +hex_decode (void *ctx, const char *in, char **out, size_t * out_size)\r
188 +{\r
189 +\r
190 +    char buf[3];\r
191 +\r
192 +    const char *p;\r
193 +    unsigned char *q;\r
194 +\r
195 +    size_t escape_count = 0;\r
196 +    size_t needed = 0;\r
197 +\r
198 +    assert (ctx); assert (in); assert (out); assert (out_size);\r
199 +\r
200 +    size_t len = strlen (in);\r
201 +\r
202 +    for (p = in; *p; p++)\r
203 +       escape_count += (*p == escape_char);\r
204 +\r
205 +    needed = len - escape_count * 2 + 1;\r
206 +\r
207 +    if (!maybe_realloc (ctx, needed, out, out_size))\r
208 +       return HEX_OUT_OF_MEMORY;\r
209 +\r
210 +    p = in;\r
211 +    q = (unsigned char *) *out;\r
212 +    buf[2] = 0;\r
213 +\r
214 +    while (*p) {\r
215 +\r
216 +       if (*p == escape_char) {\r
217 +\r
218 +           char *endp;\r
219 +\r
220 +           if (len < 3)\r
221 +               return HEX_SYNTAX_ERROR;\r
222 +\r
223 +           buf[0] = p[1];\r
224 +           buf[1] = p[2];\r
225 +\r
226 +           *q = strtol (buf, &endp, 16);\r
227 +\r
228 +           if (endp != buf + 2)\r
229 +               return HEX_SYNTAX_ERROR;\r
230 +\r
231 +           len -= 3;\r
232 +           p += 3;\r
233 +           q++;\r
234 +       } else {\r
235 +           *q++ = *p++;\r
236 +       }\r
237 +    }\r
238 +\r
239 +    *q = '\0';\r
240 +\r
241 +    return HEX_SUCCESS;\r
242 +}\r
243 diff --git a/util/hex-escape.h b/util/hex-escape.h\r
244 new file mode 100644\r
245 index 0000000..e409626\r
246 --- /dev/null\r
247 +++ b/util/hex-escape.h\r
248 @@ -0,0 +1,32 @@\r
249 +#ifndef _HEX_ESCAPE_H\r
250 +#define _HEX_ESCAPE_H\r
251 +\r
252 +typedef enum hex_status {\r
253 +    HEX_SUCCESS = 0,\r
254 +    HEX_SYNTAX_ERROR,\r
255 +    HEX_OUT_OF_MEMORY\r
256 +} hex_status_t;\r
257 +\r
258 +/*\r
259 + * The API is modelled on that for getline.\r
260 + *\r
261 + * If 'out' points to a NULL pointer a char array of the appropriate\r
262 + * size is allocated using talloc, and out_size is updated.\r
263 + *\r
264 + * If 'out' points to a non-NULL pointer, it assumed to describe an\r
265 + * existing char array, with the size given in *out_size.  This array\r
266 + * may be resized by talloc_realloc if needed; in this case *out_size\r
267 + * will also be updated.\r
268 + *\r
269 + * Note that it is an error to pass a NULL pointer for any parameter\r
270 + * of these routines.\r
271 + */\r
272 +\r
273 +hex_status_t\r
274 +hex_encode (void *talloc_ctx, const char *in, char **out,\r
275 +            size_t *out_size);\r
276 +\r
277 +hex_status_t\r
278 +hex_decode (void *talloc_ctx, const char *in, char **out,\r
279 +            size_t *out_size);\r
280 +#endif\r
281 -- \r
282 1.7.7.3\r
283 \r