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
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
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
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
54 From: David Bremner <bremner@debian.org>
\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
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
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
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
78 extra_cflags += -I$(srcdir)/$(dir)
\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
83 libutil_modules := $(libutil_c_srcs:.c=.o)
\r
85 $(dir)/libutil.a: $(libutil_modules)
\r
86 $(call quiet,AR) rcs $@ $^
\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
94 +++ b/util/hex-escape.c
\r
96 +/* hex-escape.c - Manage encoding and decoding of byte strings into
\r
97 + * a restricted character set.
\r
99 + * Copyright (c) 2011 David Bremner
\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
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
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
114 + * Author: David Bremner <david@tethera.net>
\r
117 +#include <string.h>
\r
118 +#include <talloc.h>
\r
119 +#include "error_util.h"
\r
120 +#include "hex-escape.h"
\r
123 +escapes_needed (const char *str){
\r
127 + if (index (HEX_NO_ESCAPE, *str) == NULL)
\r
136 +hex_encode (void *ctx, const char *str) {
\r
137 + char *newstr = talloc_size (ctx, strlen (str)+3*escapes_needed (str)+1);
\r
139 + char *out = newstr;
\r
142 + if (index (HEX_NO_ESCAPE, *str)) {
\r
145 + sprintf (out, "%%%02x", *str);
\r
156 + if ('0' <= c && c <= '9')
\r
159 + if ('A' <= c && c <= 'F')
\r
162 + if ('a' <= c && c <= 'f')
\r
165 + INTERNAL_ERROR ("Illegal hex digit %c", c);
\r
170 +char *hex_decode (void *ctx, const char *str) {
\r
172 + int len = strlen(str);
\r
179 + for (p = str; *p; p++)
\r
180 + escapes += (*p == HEX_ESCAPE_CHAR);
\r
182 + newstr = talloc_size (ctx, len - escapes*2 + 1);
\r
189 + if (*p == HEX_ESCAPE_CHAR) {
\r
191 + if (len < 3) INTERNAL_ERROR ("Syntax error decoding %s", str);
\r
193 + *q = _digit(p[1]) * 16;
\r
194 + *q += _digit(p[2]);
\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
210 +++ b/util/hex-escape.h
\r
212 +#ifndef _HEX_ESCAPE_H
\r
213 +#define _HEX_ESCAPE_H
\r
215 +#define HEX_ESCAPE_CHAR '%'
\r
216 +#define HEX_NO_ESCAPE "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" \
\r
217 + "0123456789+-_@=.:,"
\r
219 +char *hex_encode (void *talloc_ctx, const char *string);
\r
220 +char *hex_decode (void *talloc_ctx, const char *hex);
\r