Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / a2 / cf77736c898f9499c770c79c637a95b2ed400e
1 Return-Path: <jani@nikula.org>\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 DC6FF42116A\r
6         for <notmuch@notmuchmail.org>; Sat, 31 Mar 2012 15:17:45 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: -0.7\r
11 X-Spam-Level: \r
12 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
13         tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
14 Received: from olra.theworths.org ([127.0.0.1])\r
15         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
16         with ESMTP id pKSerVo0XCdY for <notmuch@notmuchmail.org>;\r
17         Sat, 31 Mar 2012 15:17:43 -0700 (PDT)\r
18 Received: from mail-bk0-f53.google.com (mail-bk0-f53.google.com\r
19         [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 468C2429E54\r
22         for <notmuch@notmuchmail.org>; Sat, 31 Mar 2012 15:17:39 -0700 (PDT)\r
23 Received: by mail-bk0-f53.google.com with SMTP id j4so1547751bkw.26\r
24         for <notmuch@notmuchmail.org>; Sat, 31 Mar 2012 15:17:39 -0700 (PDT)\r
25 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;\r
26         d=google.com; s=20120113;\r
27         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
28         :in-reply-to:references:x-gm-message-state;\r
29         bh=Hz2+KZxj5Kpv8YFLNT74TEGUMY0ki3yMWdw2jyYy7aU=;\r
30         b=KzcnRddeJBeD62HBrsoLh9zZixtJAmB1Uk5GBxtGJ7jCeMUYgmqnc4y7hH6qAS2Gzj\r
31         Lxny33RgWS4uMGZBN+7ZZ9qqz33YtFgcQM2CSitkzbNxfYsNqzoXFXDqiBNLlt7yrNqm\r
32         lzI6v6FChp/6gXv1UudfiHJ5rgImMaSl49TQ7JsmsOaD6FYxes/sQVrO669whrW2jTst\r
33         ZVcEBlDmfceWn6Vlrjvhw5o2zD2RUMGabF7BXHXe7OML8GPlHbCRlE972PXsbFVUSqQY\r
34         gGW1WbiSDQ7tdUzrxVgciSZq98DzS9IMmFSyVwjwyNbvtS5mxisKypgElurBw3B+Qdgr\r
35         z34A==\r
36 Received: by 10.204.153.203 with SMTP id l11mr1400223bkw.31.1333232258791;\r
37         Sat, 31 Mar 2012 15:17:38 -0700 (PDT)\r
38 Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi.\r
39         [84.248.80.253])\r
40         by mx.google.com with ESMTPS id f5sm28556643bke.9.2012.03.31.15.17.34\r
41         (version=SSLv3 cipher=OTHER); Sat, 31 Mar 2012 15:17:37 -0700 (PDT)\r
42 From: Jani Nikula <jani@nikula.org>\r
43 To: notmuch@notmuchmail.org\r
44 Subject: [PATCH 1/8] hex-escape: (en|de)code strings to/from restricted\r
45         character set\r
46 Date: Sun,  1 Apr 2012 01:17:21 +0300\r
47 Message-Id:\r
48  <3431e1b916320f0ab19b412ad877955f446de443.1333231401.git.jani@nikula.org>\r
49 X-Mailer: git-send-email 1.7.5.4\r
50 In-Reply-To: <cover.1333231401.git.jani@nikula.org>\r
51 References: <cover.1333231401.git.jani@nikula.org>\r
52 In-Reply-To: <cover.1333231401.git.jani@nikula.org>\r
53 References: <cover.1333231401.git.jani@nikula.org>\r
54 X-Gm-Message-State:\r
55  ALoCoQnbqd8opq8R24U6UJWITmt05o206ubzmI8cfcwPxP3i70dnH8Nra6FC1uoMdrKiaG+Zc97Q\r
56 X-BeenThere: notmuch@notmuchmail.org\r
57 X-Mailman-Version: 2.1.13\r
58 Precedence: list\r
59 List-Id: "Use and development of the notmuch mail system."\r
60         <notmuch.notmuchmail.org>\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
62         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
64 List-Post: <mailto:notmuch@notmuchmail.org>\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
67         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
68 X-List-Received-Date: Sat, 31 Mar 2012 22:17:46 -0000\r
69 \r
70 From: David Bremner <bremner@debian.org>\r
71 \r
72 The character set is chosen to be suitable for pathnames, and the same\r
73 as that used by contrib/nmbug\r
74 ---\r
75  util/Makefile.local |    2 +-\r
76  util/hex-escape.c   |  156 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
77  util/hex-escape.h   |   32 +++++++++++\r
78  3 files changed, 189 insertions(+), 1 deletions(-)\r
79  create mode 100644 util/hex-escape.c\r
80  create mode 100644 util/hex-escape.h\r
81 \r
82 diff --git a/util/Makefile.local b/util/Makefile.local\r
83 index c7cae61..3ca623e 100644\r
84 --- a/util/Makefile.local\r
85 +++ b/util/Makefile.local\r
86 @@ -3,7 +3,7 @@\r
87  dir := util\r
88  extra_cflags += -I$(srcdir)/$(dir)\r
89  \r
90 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c\r
91 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c\r
92  \r
93  libutil_modules := $(libutil_c_srcs:.c=.o)\r
94  \r
95 diff --git a/util/hex-escape.c b/util/hex-escape.c\r
96 new file mode 100644\r
97 index 0000000..6c1260b\r
98 --- /dev/null\r
99 +++ b/util/hex-escape.c\r
100 @@ -0,0 +1,156 @@\r
101 +/* hex-escape.c -  Manage encoding and decoding of byte strings into path names\r
102 + *\r
103 + * Copyright (c) 2011 David Bremner\r
104 + *\r
105 + * This program is free software: you can redistribute it and/or modify\r
106 + * it under the terms of the GNU General Public License as published by\r
107 + * the Free Software Foundation, either version 3 of the License, or\r
108 + * (at your option) any later version.\r
109 + *\r
110 + * This program is distributed in the hope that it will be useful,\r
111 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
112 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
113 + * GNU General Public License for more details.\r
114 + *\r
115 + * You should have received a copy of the GNU General Public License\r
116 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
117 + *\r
118 + * Author: David Bremner <david@tethera.net>\r
119 + */\r
120 +\r
121 +#include <assert.h>\r
122 +#include <string.h>\r
123 +#include <talloc.h>\r
124 +#include "error_util.h"\r
125 +#include "hex-escape.h"\r
126 +\r
127 +static const size_t default_buf_size = 1024;\r
128 +\r
129 +static const char *output_charset =\r
130 +    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";\r
131 +\r
132 +static const int escape_char = '%';\r
133 +\r
134 +static int\r
135 +is_output (char c)\r
136 +{\r
137 +    return (strchr (output_charset, c) != NULL);\r
138 +}\r
139 +\r
140 +static int\r
141 +maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)\r
142 +{\r
143 +    if (*out_size < needed) {\r
144 +\r
145 +       if (*out == NULL)\r
146 +           *out = talloc_size (ctx, needed);\r
147 +       else\r
148 +           *out = talloc_realloc (ctx, *out, char, needed);\r
149 +\r
150 +       if (*out == NULL)\r
151 +           return 0;\r
152 +\r
153 +       *out_size = needed;\r
154 +    }\r
155 +    return 1;\r
156 +}\r
157 +\r
158 +hex_status_t\r
159 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)\r
160 +{\r
161 +\r
162 +    const unsigned char *p;\r
163 +    char *q;\r
164 +\r
165 +    size_t escape_count = 0;\r
166 +    size_t len = 0;\r
167 +    size_t needed;\r
168 +\r
169 +    assert (ctx); assert (in); assert (out); assert (out_size);\r
170 +\r
171 +    for (p = (unsigned char *) in; *p; p++) {\r
172 +       escape_count += (!is_output (*p));\r
173 +       len++;\r
174 +    }\r
175 +\r
176 +    needed = len + escape_count * 2 + 1;\r
177 +\r
178 +    if (*out == NULL)\r
179 +       *out_size = 0;\r
180 +\r
181 +    if (!maybe_realloc (ctx, needed, out, out_size))\r
182 +       return HEX_OUT_OF_MEMORY;\r
183 +\r
184 +    q = *out;\r
185 +    p = (unsigned char *) in;\r
186 +\r
187 +    while (*p) {\r
188 +       if (is_output (*p)) {\r
189 +           *q++ = *p++;\r
190 +       } else {\r
191 +           sprintf (q, "%%%02x", *p++);\r
192 +           q += 3;\r
193 +       }\r
194 +    }\r
195 +\r
196 +    *q = '\0';\r
197 +    return HEX_SUCCESS;\r
198 +}\r
199 +\r
200 +hex_status_t\r
201 +hex_decode (void *ctx, const char *in, char **out, size_t * out_size)\r
202 +{\r
203 +\r
204 +    char buf[3];\r
205 +\r
206 +    const char *p;\r
207 +    unsigned char *q;\r
208 +\r
209 +    size_t escape_count = 0;\r
210 +    size_t needed = 0;\r
211 +\r
212 +    assert (ctx); assert (in); assert (out); assert (out_size);\r
213 +\r
214 +    size_t len = strlen (in);\r
215 +\r
216 +    for (p = in; *p; p++)\r
217 +       escape_count += (*p == escape_char);\r
218 +\r
219 +    needed = len - escape_count * 2 + 1;\r
220 +\r
221 +    if (!maybe_realloc (ctx, needed, out, out_size))\r
222 +       return HEX_OUT_OF_MEMORY;\r
223 +\r
224 +    p = in;\r
225 +    q = (unsigned char *) *out;\r
226 +    buf[2] = 0;\r
227 +\r
228 +    while (*p) {\r
229 +\r
230 +       if (*p == escape_char) {\r
231 +\r
232 +           char *endp;\r
233 +\r
234 +           if (len < 3)\r
235 +               return HEX_SYNTAX_ERROR;\r
236 +\r
237 +           buf[0] = p[1];\r
238 +           buf[1] = p[2];\r
239 +\r
240 +           *q = strtol (buf, &endp, 16);\r
241 +\r
242 +           if (endp != buf + 2)\r
243 +               return HEX_SYNTAX_ERROR;\r
244 +\r
245 +           len -= 3;\r
246 +           p += 3;\r
247 +           q++;\r
248 +       } else {\r
249 +           *q++ = *p++;\r
250 +       }\r
251 +    }\r
252 +\r
253 +    *q = '\0';\r
254 +\r
255 +    return HEX_SUCCESS;\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..e409626\r
260 --- /dev/null\r
261 +++ b/util/hex-escape.h\r
262 @@ -0,0 +1,32 @@\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 is modelled on that for getline.\r
274 + *\r
275 + * If 'out' points to a NULL pointer a char array of the appropriate\r
276 + * size is allocated using talloc, and out_size is updated.\r
277 + *\r
278 + * If 'out' points to a non-NULL pointer, it assumed to describe an\r
279 + * existing char array, with the size given in *out_size.  This array\r
280 + * may be resized by talloc_realloc if needed; in this case *out_size\r
281 + * will also be updated.\r
282 + *\r
283 + * Note that it is an error to pass a NULL pointer for any parameter\r
284 + * of these routines.\r
285 + */\r
286 +\r
287 +hex_status_t\r
288 +hex_encode (void *talloc_ctx, const char *in, char **out,\r
289 +            size_t *out_size);\r
290 +\r
291 +hex_status_t\r
292 +hex_decode (void *talloc_ctx, const char *in, char **out,\r
293 +            size_t *out_size);\r
294 +#endif\r
295 -- \r
296 1.7.5.4\r
297 \r