Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / 43 / 50fad60a0a901177efed019a4548cb80e7528d
1 Return-Path: <bremner@pivot.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 537F2429E27\r
6         for <notmuch@notmuchmail.org>; Tue, 13 Dec 2011 12:28:21 -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 O1bhvtjl1p7n for <notmuch@notmuchmail.org>;\r
16         Tue, 13 Dec 2011 12:28:20 -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 2DFA3429E28\r
21         for <notmuch@notmuchmail.org>; Tue, 13 Dec 2011 12:28:18 -0800 (PST)\r
22 Received: from convex-new.cs.unb.ca ([131.202.13.154])\r
23         by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pBDKSATR002924;\r
24         Tue, 13 Dec 2011 16:28:10 -0400\r
25 Received: from bremner by convex-new.cs.unb.ca with local (Exim 4.72)\r
26         (envelope-from <bremner@pivot.cs.unb.ca>)\r
27         id 1RaYxO-0001xH-Dq; Tue, 13 Dec 2011 16:28:10 -0400\r
28 From: David Bremner <david@tethera.net>\r
29 To: notmuch@notmuchmail.org\r
30 Subject: [Alpha PATCH 1/6] util/hex-escape.[ch]: encoding/decoding strings\r
31         into restricted character set\r
32 Date: Tue, 13 Dec 2011 16:27:50 -0400\r
33 Message-Id: <1323808075-7417-2-git-send-email-david@tethera.net>\r
34 X-Mailer: git-send-email 1.7.5.4\r
35 In-Reply-To: <1323808075-7417-1-git-send-email-david@tethera.net>\r
36 References: <1323808075-7417-1-git-send-email-david@tethera.net>\r
37 Cc: David Bremner <bremner@debian.org>, pere@hungry.com\r
38 X-BeenThere: notmuch@notmuchmail.org\r
39 X-Mailman-Version: 2.1.13\r
40 Precedence: list\r
41 List-Id: "Use and development of the notmuch mail system."\r
42         <notmuch.notmuchmail.org>\r
43 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
44         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
45 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
46 List-Post: <mailto:notmuch@notmuchmail.org>\r
47 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
48 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
50 X-List-Received-Date: Tue, 13 Dec 2011 20:28:21 -0000\r
51 \r
52 From: David Bremner <bremner@debian.org>\r
53 \r
54 The character set is chosen to be suitable for pathnames, and the same as that\r
55 used by contrib/nmbug\r
56 ---\r
57  util/Makefile.local |    2 +-\r
58  util/hex-escape.c   |  150 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
59  util/hex-escape.h   |   15 +++++\r
60  3 files changed, 166 insertions(+), 1 deletions(-)\r
61  create mode 100644 util/hex-escape.c\r
62  create mode 100644 util/hex-escape.h\r
63 \r
64 diff --git a/util/Makefile.local b/util/Makefile.local\r
65 index 26e4c3f..2e63932 100644\r
66 --- a/util/Makefile.local\r
67 +++ b/util/Makefile.local\r
68 @@ -3,7 +3,7 @@\r
69  dir := util\r
70  extra_cflags += -I$(srcdir)/$(dir)\r
71  \r
72 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c\r
73 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c\r
74  \r
75  libutil_modules := $(libutil_c_srcs:.c=.o)\r
76  \r
77 diff --git a/util/hex-escape.c b/util/hex-escape.c\r
78 new file mode 100644\r
79 index 0000000..3e08465\r
80 --- /dev/null\r
81 +++ b/util/hex-escape.c\r
82 @@ -0,0 +1,150 @@\r
83 +/* pathname.c -  Manage encoding and decoding of byte strings into path names\r
84 + *\r
85 + * Copyright (c) 2011 David Bremner\r
86 + *\r
87 + * This program is free software: you can redistribute it and/or modify\r
88 + * it under the terms of the GNU General Public License as published by\r
89 + * the Free Software Foundation, either version 3 of the License, or\r
90 + * (at your option) any later version.\r
91 + *\r
92 + * This program is distributed in the hope that it will be useful,\r
93 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
94 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
95 + * GNU General Public License for more details.\r
96 + *\r
97 + * You should have received a copy of the GNU General Public License\r
98 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
99 + *\r
100 + * Author: David Bremner <david@tethera.net>\r
101 + */\r
102 +\r
103 +#include <string.h>\r
104 +#include <talloc.h>\r
105 +#include "error_util.h"\r
106 +#include "hex-escape.h"\r
107 +\r
108 +static const size_t default_buf_size=1024;\r
109 +\r
110 +static const char* output_charset=\r
111 +    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.:,";\r
112 +\r
113 +static const char escape_char='%';\r
114 +\r
115 +static int\r
116 +is_output (char c) {\r
117 +    return (strchr (output_charset, c) != NULL);\r
118 +}\r
119 +\r
120 +\r
121 +static int\r
122 +maybe_realloc(void *ctx, size_t needed, char **out, size_t *out_size)\r
123 +{\r
124 +    if (*out_size < needed) {\r
125 +\r
126 +       if (*out == NULL)\r
127 +           *out = talloc_size(ctx,needed);\r
128 +       else\r
129 +           *out = talloc_realloc(ctx,*out,char,needed);\r
130 +\r
131 +       if (*out == NULL)\r
132 +           return 0;\r
133 +\r
134 +       *out_size = needed;\r
135 +    }\r
136 +    return 1;\r
137 +}\r
138 +\r
139 +\r
140 +hex_status_t\r
141 +hex_encode (void *ctx, const char *in, char **out, size_t *out_size)\r
142 +{\r
143 +\r
144 +    const char *p;\r
145 +    char *q;\r
146 +\r
147 +    int escape_count=0;\r
148 +    size_t needed;\r
149 +\r
150 +    for  (p = in; *p; p++)\r
151 +       escape_count += (! is_output (*p));\r
152 +\r
153 +    needed = strlen (in) + 2*escape_count + 1;\r
154 +\r
155 +    if (*out == NULL)\r
156 +       *out_size=0;\r
157 +\r
158 +    if (!maybe_realloc (ctx, needed, out, out_size))\r
159 +       return HEX_OUT_OF_MEMORY;\r
160 +\r
161 +    q = *out;\r
162 +    p = in;\r
163 +\r
164 +    while (*p) {\r
165 +       if (is_output (*p)) {\r
166 +           *q++ = *p++;\r
167 +       } else {\r
168 +           sprintf (q, "%%%02x", *p++);\r
169 +           q += 3;\r
170 +       }\r
171 +    }\r
172 +\r
173 +    *q = '\0';\r
174 +    return HEX_SUCCESS;\r
175 +}\r
176 +\r
177 +\r
178 +hex_status_t\r
179 +hex_decode (void *ctx, const char *in, char **out, size_t *out_size) {\r
180 +\r
181 +    char buf[3];\r
182 +\r
183 +    const char *p;\r
184 +    char *q;\r
185 +\r
186 +    size_t escape_count = 0;\r
187 +    size_t needed = 0;\r
188 +\r
189 +    size_t len = strlen (in);\r
190 +\r
191 +    for (p = in; *p; p++)\r
192 +       escape_count += (*p == escape_char);\r
193 +\r
194 +    needed = len - escape_count*2 +1;\r
195 +\r
196 +    if (!maybe_realloc(ctx, needed, out, out_size))\r
197 +       return HEX_OUT_OF_MEMORY;\r
198 +\r
199 +    p = in;\r
200 +    q = *out;\r
201 +    buf[2]=0;\r
202 +\r
203 +\r
204 +    while (*p) {\r
205 +\r
206 +       if (*p == escape_char) {\r
207 +\r
208 +           char *endp;\r
209 +\r
210 +           if (len < 3)\r
211 +               return HEX_SYNTAX_ERROR;\r
212 +\r
213 +           buf[0]=p[1];\r
214 +           buf[1]=p[2];\r
215 +\r
216 +           *q = strtol(buf, &endp, 16);\r
217 +\r
218 +           if (endp != buf+2)\r
219 +               return HEX_SYNTAX_ERROR;\r
220 +\r
221 +           len -= 3;\r
222 +           p += 3;\r
223 +           q++;\r
224 +       } else {\r
225 +           *q++ = *p++;\r
226 +       }\r
227 +    }\r
228 +\r
229 +    *q = '\0';\r
230 +\r
231 +    return HEX_SUCCESS;\r
232 +}\r
233 diff --git a/util/hex-escape.h b/util/hex-escape.h\r
234 new file mode 100644\r
235 index 0000000..98ecbe0\r
236 --- /dev/null\r
237 +++ b/util/hex-escape.h\r
238 @@ -0,0 +1,15 @@\r
239 +#ifndef _HEX_ESCAPE_H\r
240 +#define _HEX_ESCAPE_H\r
241 +\r
242 +typedef enum hex_status {\r
243 +    HEX_SUCCESS = 0,\r
244 +    HEX_SYNTAX_ERROR,\r
245 +    HEX_OUT_OF_MEMORY\r
246 +} hex_status_t;\r
247 +\r
248 +hex_status_t\r
249 +hex_encode (void *talloc_ctx, const char *in, char **out, size_t *out_size);\r
250 +\r
251 +hex_status_t\r
252 +hex_decode (void *talloc_ctx, const char *in, char **out, size_t *out_size);\r
253 +#endif\r
254 -- \r
255 1.7.5.4\r
256 \r