4
* This file imported from the Samba4 project which itself is imported
5
* from the Squid project. The licence below is reproduced intact,
6
* but refers to files in Squid's repository, not in Samba nor
7
* OpenChange. See COPYING for the GPLv3 notice (being the later
8
* version mentioned below).
10
* This file has also been modified, in particular to use talloc to
11
* allocate in rfc1738_escape()
13
* OpenChange version has been modified to include . as an escapable
14
* character for unix and offer the ability to extend the scope for
15
* other operating systems in the future.
17
* - Andrew Bartlett Oct-2009
18
* - Julien Kerihuel <j.kerihuel@openchange.org> March 2011
26
* AUTHOR: Harvest Derived
28
* SQUID Web Proxy Cache http://www.squid-cache.org/
29
* ----------------------------------------------------------
31
* Squid is the result of efforts by numerous individuals from
32
* the Internet community; see the CONTRIBUTORS file for full
33
* details. Many organizations have provided support for Squid's
34
* development; see the SPONSORS file for full details. Squid is
35
* Copyrighted (C) 2001 by the Regents of the University of
36
* California; see the COPYRIGHT file for full details. Squid
37
* incorporates software developed and/or copyrighted by other
38
* sources; see the CREDITS file for full details.
40
* This program is free software; you can redistribute it and/or modify
41
* it under the terms of the GNU General Public License as published by
42
* the Free Software Foundation; either version 2 of the License, or
43
* (at your option) any later version.
45
* This program is distributed in the hope that it will be useful,
46
* but WITHOUT ANY WARRANTY; without even the implied warranty of
47
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48
* GNU General Public License for more details.
50
* You should have received a copy of the GNU General Public License
51
* along with this program; if not, write to the Free Software
52
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
56
#define __STDC_FORMAT_MACROS 1
59
#include "mapistore_errors.h"
60
#include "mapistore.h"
61
#include "mapistore_private.h"
62
#include "mapistore_backend.h"
63
#include "mapistore_common.h"
66
* RFC 1738 defines that these characters should be escaped, as well
67
* any non-US-ASCII character or anything between 0x00 - 0x1F.
69
static char rfc1738_unsafe_chars[] = {
74
#if 0 /* done in code */
87
(char) 0x20 /* space */
90
static char rfc1738_reserved_chars[] = {
101
static char mapistore_reserved_unix_chars[] = {
106
enum mapistore_encoding {
107
MAPISTORE_ENCODING_RFC1738_UNSAFE_CHARS = (int) -1,
108
MAPISTORE_ENCODING_RFC1738_UNSAFE_CHARS_AND_PERCENT = (int) 0,
109
MAPISTORE_ENCODING_RFC1738_RESERVED_CHARS = (int) 1,
110
MAPISTORE_ENCODING_UNIX_RESERVED_CHARS = (int) 2,
111
MAPISTORE_ENCODING_WINDOWS_RESERVED_CHARS = (int) 3
115
* do_escape - Returns a static buffer contains the RFC 1738
116
* compliant or not (depending on encoding value), escaped version of
121
mapistore_util_do_escape(TALLOC_CTX *mem_ctx, const char *url, enum mapistore_encoding encoding)
127
unsigned int i, do_escape;
129
bufsize = strlen(url) * 3 + 1;
130
buf = talloc_array(mem_ctx, char, bufsize);
135
talloc_set_name_const(buf, buf);
138
for (p = url, q = buf; *p != '\0' && q < (buf + bufsize - 1); p++, q++) {
141
/* RFC 1738 defines these chars as unsafe */
142
for (i = 0; i < sizeof(rfc1738_unsafe_chars); i++) {
143
if (*p == rfc1738_unsafe_chars[i]) {
149
/* Handle % separately */
150
if (encoding >= 0 && *p == '%')
154
case MAPISTORE_ENCODING_RFC1738_RESERVED_CHARS:
155
/* RFC 1738 defines these chars as reserved */
156
for (i = 0; i < sizeof(rfc1738_reserved_chars); i++) {
157
if (*p == rfc1738_reserved_chars[i]) {
163
case MAPISTORE_ENCODING_UNIX_RESERVED_CHARS:
164
for (i = 0; i < sizeof(mapistore_reserved_unix_chars); i++) {
165
if (*p == mapistore_reserved_unix_chars[i]) {
175
/* RFC 1738 says any control chars (0x00-0x1F) are encoded */
176
if ((unsigned char) *p <= (unsigned char) 0x1F) {
179
/* RFC 1738 says 0x7f is encoded */
180
if (*p == (char) 0x7F) {
183
/* RFC 1738 says any non-US-ASCII are encoded */
184
if (((unsigned char) *p >= (unsigned char) 0x80)) {
187
/* Do the triplet encoding, or just copy the char */
188
/* note: while we do not need snprintf here as q is appropriately
189
* allocated, Samba does to avoid our macro banning it -- abartlet */
191
if (do_escape == 1) {
192
(void) snprintf(q, 4, "%%%02X", (unsigned char) *p);
193
q += sizeof(char) * 2;
203
* rfc1738_escape - Returns a buffer that contains the RFC
204
* 1738 compliant, escaped version of the given url. (escapes unsafe and % characters)
207
mapistore_util_rfc1738_escape(TALLOC_CTX *mem_ctx, const char *url)
209
return mapistore_util_do_escape(mem_ctx, url, MAPISTORE_ENCODING_RFC1738_UNSAFE_CHARS_AND_PERCENT);
213
* rfc1738_escape_unescaped - Returns a buffer that contains
214
* the RFC 1738 compliant, escaped version of the given url (escapes unsafe chars only)
217
mapistore_util_rfc1738_escape_unescaped(TALLOC_CTX *mem_ctx, const char *url)
219
return mapistore_util_do_escape(mem_ctx, url, MAPISTORE_ENCODING_RFC1738_UNSAFE_CHARS);
223
* rfc1738_escape_part - Returns a buffer that contains the RFC
224
* 1738 compliant, escaped version of the given url segment. (escapes
225
* unsafe, reserved and % chars) It would mangle the :// in http://,
226
* and mangle paths (because of /).
229
mapistore_util_rfc1738_escape_part(TALLOC_CTX *mem_ctx, const char *url)
231
return mapistore_util_do_escape(mem_ctx, url, MAPISTORE_ENCODING_RFC1738_RESERVED_CHARS);
235
mapistore_util_unix_name_escape(TALLOC_CTX *mem_ctx, const char *url)
237
return mapistore_util_do_escape(mem_ctx, url, MAPISTORE_ENCODING_UNIX_RESERVED_CHARS);
241
* rfc1738_unescape() - Converts escaped characters (%xy numbers) in
242
* given the string. %% is a %. %ab is the 8-bit hexadecimal number "ab"
245
mapistore_util_string_unescape(char *s)
248
int i, j; /* i is write, j is read */
250
for (i = j = 0; s[j]; i++, j++) {
254
if (s[j + 1] == '%') { /* %% case */
258
if (s[j + 1] && s[j + 2]) {
259
if (s[j + 1] == '0' && s[j + 2] == '0') { /* %00 case */
263
hexnum[0] = s[j + 1];
264
hexnum[1] = s[j + 2];
266
if (1 == sscanf(hexnum, "%x", &x)) {
267
s[i] = (char) (0x0ff & x);