]> git.notmuchmail.org Git - notmuch/blob - util/hex-escape.c
Merge tag '0.29.2'
[notmuch] / util / hex-escape.c
1 /* hex-escape.c -  Manage encoding and decoding of byte strings into path names
2  *
3  * Copyright (c) 2011 David Bremner
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: David Bremner <david@tethera.net>
19  */
20
21 #include <assert.h>
22 #include <string.h>
23 #include <talloc.h>
24 #include <ctype.h>
25 #include "error_util.h"
26 #include "hex-escape.h"
27
28 static const char *output_charset =
29     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-_@=.,";
30
31 static const char escape_char = '%';
32
33 static int
34 is_output (char c)
35 {
36     return (strchr (output_charset, c) != NULL);
37 }
38
39 static int
40 maybe_realloc (void *ctx, size_t needed, char **out, size_t *out_size)
41 {
42     if (*out_size < needed) {
43
44         if (*out == NULL)
45             *out = talloc_size (ctx, needed);
46         else
47             *out = talloc_realloc (ctx, *out, char, needed);
48
49         if (*out == NULL)
50             return 0;
51
52         *out_size = needed;
53     }
54     return 1;
55 }
56
57 hex_status_t
58 hex_encode (void *ctx, const char *in, char **out, size_t *out_size)
59 {
60
61     const char *p;
62     char *q;
63
64     size_t needed = 1;  /* for the NUL */
65
66     assert (ctx); assert (in); assert (out); assert (out_size);
67
68     for (p = in; *p; p++) {
69         needed += is_output (*p) ? 1 : 3;
70     }
71
72     if (*out == NULL)
73         *out_size = 0;
74
75     if (! maybe_realloc (ctx, needed, out, out_size))
76         return HEX_OUT_OF_MEMORY;
77
78     q = *out;
79     p = in;
80
81     while (*p) {
82         if (is_output (*p)) {
83             *q++ = *p++;
84         } else {
85             sprintf (q, "%%%02x", (unsigned char) *p++);
86             q += 3;
87         }
88     }
89
90     *q = '\0';
91     return HEX_SUCCESS;
92 }
93
94 /* Hex decode 'in' to 'out'.
95  *
96  * This must succeed for in == out to support hex_decode_inplace().
97  */
98 static hex_status_t
99 hex_decode_internal (const char *in, unsigned char *out)
100 {
101     char buf[3];
102
103     while (*in) {
104         if (*in == escape_char) {
105             char *endp;
106
107             /* This also handles unexpected end-of-string. */
108             if (! isxdigit ((unsigned char) in[1]) ||
109                 ! isxdigit ((unsigned char) in[2]))
110                 return HEX_SYNTAX_ERROR;
111
112             buf[0] = in[1];
113             buf[1] = in[2];
114             buf[2] = '\0';
115
116             *out = strtoul (buf, &endp, 16);
117
118             if (endp != buf + 2)
119                 return HEX_SYNTAX_ERROR;
120
121             in += 3;
122             out++;
123         } else {
124             *out++ = *in++;
125         }
126     }
127
128     *out = '\0';
129
130     return HEX_SUCCESS;
131 }
132
133 hex_status_t
134 hex_decode_inplace (char *s)
135 {
136     /* A decoded string is never longer than the encoded one, so it is
137      * safe to decode a string onto itself. */
138     return hex_decode_internal (s, (unsigned char *) s);
139 }
140
141 hex_status_t
142 hex_decode (void *ctx, const char *in, char **out, size_t *out_size)
143 {
144     const char *p;
145     size_t needed = 1;  /* for the NUL */
146
147     assert (ctx); assert (in); assert (out); assert (out_size);
148
149     for (p = in; *p; p++)
150         if ((p[0] == escape_char) && isxdigit (p[1]) && isxdigit (p[2]))
151             needed -= 1;
152         else
153             needed += 1;
154
155     if (! maybe_realloc (ctx, needed, out, out_size))
156         return HEX_OUT_OF_MEMORY;
157
158     return hex_decode_internal (in, (unsigned char *) *out);
159 }