Re: [PATCH] emacs: wash: make word-wrap bound message width
[notmuch-archives.git] / 01 / fe7ccd0b814352cc7c5c108a9ecb05a16e9a60
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 44C77429E25\r
6         for <notmuch@notmuchmail.org>; Sun, 11 Dec 2011 08:20:08 -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.299\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.299 tagged_above=-999 required=5\r
12         tests=[NORMAL_HTTP_TO_IP=0.001, RCVD_IN_DNSWL_MED=-2.3]\r
13         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 jmS3uDKQkHjG for <notmuch@notmuchmail.org>;\r
17         Sun, 11 Dec 2011 08:20:07 -0800 (PST)\r
18 Received: from tempo.its.unb.ca (tempo.its.unb.ca [131.202.1.21])\r
19         (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 48D0B431FB6\r
22         for <notmuch@notmuchmail.org>; Sun, 11 Dec 2011 08:20:07 -0800 (PST)\r
23 Received: from zancas.localnet\r
24         (fctnnbsc36w-156034079193.pppoe-dynamic.High-Speed.nb.bellaliant.net\r
25         [156.34.79.193]) (authenticated bits=0)\r
26         by tempo.its.unb.ca (8.13.8/8.13.8) with ESMTP id pBBGK30U020253\r
27         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO);\r
28         Sun, 11 Dec 2011 12:20:04 -0400\r
29 Received: from bremner by zancas.localnet with local (Exim 4.77)\r
30         (envelope-from <bremner@tethera.net>)\r
31         id 1RZm8A-0004BN-Vj; Sun, 11 Dec 2011 12:20:03 -0400\r
32 From: David Bremner <david@tethera.net>\r
33 To: notmuch@notmuchmail.org\r
34 Subject: [PATCH] util/hex-escape.[ch]: encoding/decoding strings into\r
35         restricted character set\r
36 Date: Sun, 11 Dec 2011 12:19:44 -0400\r
37 Message-Id: <1323620384-16043-1-git-send-email-david@tethera.net>\r
38 X-Mailer: git-send-email 1.7.7.3\r
39 Cc: David Bremner <bremner@debian.org>\r
40 X-BeenThere: notmuch@notmuchmail.org\r
41 X-Mailman-Version: 2.1.13\r
42 Precedence: list\r
43 List-Id: "Use and development of the notmuch mail system."\r
44         <notmuch.notmuchmail.org>\r
45 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
47 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
48 List-Post: <mailto:notmuch@notmuchmail.org>\r
49 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
50 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
51         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
52 X-List-Received-Date: Sun, 11 Dec 2011 16:20:08 -0000\r
53 \r
54 From: David Bremner <bremner@debian.org>\r
55 \r
56 The character set is chosen to be suitable for pathnames, and the same\r
57 as that used by contrib/nmbug. The new encoded/decoded strings are\r
58 allocated using talloc.\r
59 ---\r
60 This isn't urgent, but it is useful for a couple projects I have\r
61 brewing (nmbug compatible dump/restore and tag logging), so I thought\r
62 I would get some feedback on it.\r
63 \r
64 \r
65  util/Makefile.local |    4 +-\r
66  util/hex-escape.c   |  110 +++++++++++++++++++++++++++++++++++++++++++++++++++\r
67  util/hex-escape.h   |   10 +++++\r
68  3 files changed, 122 insertions(+), 2 deletions(-)\r
69  create mode 100644 util/hex-escape.c\r
70  create mode 100644 util/hex-escape.h\r
71 \r
72 diff --git a/util/Makefile.local b/util/Makefile.local\r
73 index 0340899..2e63932 100644\r
74 --- a/util/Makefile.local\r
75 +++ b/util/Makefile.local\r
76 @@ -3,11 +3,11 @@\r
77  dir := util\r
78  extra_cflags += -I$(srcdir)/$(dir)\r
79  \r
80 -libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c\r
81 +libutil_c_srcs := $(dir)/xutil.c $(dir)/error_util.c $(dir)/hex-escape.c\r
82  \r
83  libutil_modules := $(libutil_c_srcs:.c=.o)\r
84  \r
85  $(dir)/libutil.a: $(libutil_modules)\r
86         $(call quiet,AR) rcs $@ $^\r
87  \r
88 -CLEAN := $(CLEAN) $(dir)/xutil.o $(dir)/error_util.o $(dir)/libutil.a\r
89 +CLEAN := $(CLEAN) $(libutil_modules) $(dir)/libutil.a\r
90 diff --git a/util/hex-escape.c b/util/hex-escape.c\r
91 new file mode 100644\r
92 index 0000000..c294bb5\r
93 --- /dev/null\r
94 +++ b/util/hex-escape.c\r
95 @@ -0,0 +1,110 @@\r
96 +/* hex-escape.c -  Manage encoding and decoding of byte strings into\r
97 + *                a restricted character set.\r
98 + *\r
99 + * Copyright (c) 2011 David Bremner\r
100 + *\r
101 + * This program is free software: you can redistribute it and/or modify\r
102 + * it under the terms of the GNU General Public License as published by\r
103 + * the Free Software Foundation, either version 3 of the License, or\r
104 + * (at your option) any later version.\r
105 + *\r
106 + * This program is distributed in the hope that it will be useful,\r
107 + * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
108 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
109 + * GNU General Public License for more details.\r
110 + *\r
111 + * You should have received a copy of the GNU General Public License\r
112 + * along with this program.  If not, see http://www.gnu.org/licenses/ .\r
113 + *\r
114 + * Author: David Bremner <david@tethera.net>\r
115 + */\r
116 +\r
117 +#include <string.h>\r
118 +#include <talloc.h>\r
119 +#include "error_util.h"\r
120 +#include "hex-escape.h"\r
121 +\r
122 +static int\r
123 +escapes_needed (const char *str){\r
124 +    int escapes = 0;\r
125 +\r
126 +    while (*str) {\r
127 +       if (index (HEX_NO_ESCAPE, *str) == NULL)\r
128 +           escapes++;\r
129 +       str++;\r
130 +    }\r
131 +\r
132 +   return escapes;\r
133 +}\r
134 +\r
135 +char *\r
136 +hex_encode (void *ctx, const char *str) {\r
137 +    char *newstr = talloc_size (ctx, strlen (str)+3*escapes_needed (str)+1);\r
138 +\r
139 +    char *out = newstr;\r
140 +\r
141 +    while (*str) {\r
142 +       if (index (HEX_NO_ESCAPE, *str)) {\r
143 +           *out++ = *str++;\r
144 +       } else {\r
145 +           sprintf (out, "%%%02x", *str);\r
146 +           str++;\r
147 +           out += 3;\r
148 +       }\r
149 +    }\r
150 +    *out = 0;\r
151 +    return newstr;\r
152 +}\r
153 +\r
154 +inline static int\r
155 +_digit (char c) {\r
156 +    if ('0' <= c && c <= '9')\r
157 +       return c - '0';\r
158 +\r
159 +    if ('A' <= c && c <= 'F')\r
160 +       return c - 'A';\r
161 +\r
162 +    if ('a' <= c && c <= 'f')\r
163 +       return c - 'a';\r
164 +\r
165 +    INTERNAL_ERROR ("Illegal hex digit %c", c);\r
166 +    /*NOTREACHED*/\r
167 +    return 0;\r
168 +}\r
169 +\r
170 +char *hex_decode (void *ctx, const char *str) {\r
171 +\r
172 +    int len = strlen(str);\r
173 +\r
174 +    const char *p;\r
175 +    char *q;\r
176 +    char *newstr;\r
177 +    int escapes = 0;\r
178 +\r
179 +    for (p = str; *p; p++)\r
180 +       escapes += (*p == HEX_ESCAPE_CHAR);\r
181 +\r
182 +    newstr = talloc_size (ctx, len - escapes*2 + 1);\r
183 +\r
184 +    p = str;\r
185 +    q = newstr;\r
186 +\r
187 +    while (*p) {\r
188 +\r
189 +       if (*p == HEX_ESCAPE_CHAR) {\r
190 +\r
191 +           if (len < 3) INTERNAL_ERROR ("Syntax error decoding %s", str);\r
192 +\r
193 +           *q = _digit(p[1]) * 16;\r
194 +           *q += _digit(p[2]);\r
195 +\r
196 +           len -= 3;\r
197 +           p += 3;\r
198 +           q++;\r
199 +       } else {\r
200 +           *q++ = *p++;\r
201 +       }\r
202 +    }\r
203 +\r
204 +    return newstr;\r
205 +}\r
206 diff --git a/util/hex-escape.h b/util/hex-escape.h\r
207 new file mode 100644\r
208 index 0000000..7caff15\r
209 --- /dev/null\r
210 +++ b/util/hex-escape.h\r
211 @@ -0,0 +1,10 @@\r
212 +#ifndef _HEX_ESCAPE_H\r
213 +#define _HEX_ESCAPE_H\r
214 +\r
215 +#define HEX_ESCAPE_CHAR '%'\r
216 +#define HEX_NO_ESCAPE  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" \\r
217 +                       "0123456789+-_@=.:,"\r
218 +\r
219 +char *hex_encode (void *talloc_ctx, const char *string);\r
220 +char *hex_decode (void *talloc_ctx, const char *hex);\r
221 +#endif\r
222 -- \r
223 1.7.7.3\r
224 \r