1/*
2 * libjingle
3 * Copyright 2008, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 *  1. Redistributions of source code must retain the above copyright notice,
9 *     this list of conditions and the following disclaimer.
10 *  2. Redistributions in binary form must reproduce the above copyright notice,
11 *     this list of conditions and the following disclaimer in the documentation
12 *     and/or other materials provided with the distribution.
13 *  3. The name of the author may not be used to endorse or promote products
14 *     derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "talk/base/urlencode.h"
29
30#include "talk/base/common.h"
31#include "talk/base/stringutils.h"
32
33static int HexPairValue(const char * code) {
34  int value = 0;
35  const char * pch = code;
36  for (;;) {
37    int digit = *pch++;
38    if (digit >= '0' && digit <= '9') {
39      value += digit - '0';
40    }
41    else if (digit >= 'A' && digit <= 'F') {
42      value += digit - 'A' + 10;
43    }
44    else if (digit >= 'a' && digit <= 'f') {
45      value += digit - 'a' + 10;
46    }
47    else {
48      return -1;
49    }
50    if (pch == code + 2)
51      return value;
52    value <<= 4;
53  }
54}
55
56int InternalUrlDecode(const char *source, char *dest,
57                      bool encode_space_as_plus) {
58  char * start = dest;
59
60  while (*source) {
61    switch (*source) {
62    case '+':
63      if (encode_space_as_plus) {
64        *(dest++) = ' ';
65      } else {
66        *dest++ = *source;
67      }
68      break;
69    case '%':
70      if (source[1] && source[2]) {
71        int value = HexPairValue(source + 1);
72        if (value >= 0) {
73          *(dest++) = value;
74          source += 2;
75        }
76        else {
77          *dest++ = '?';
78        }
79      }
80      else {
81        *dest++ = '?';
82      }
83      break;
84    default:
85      *dest++ = *source;
86    }
87    source++;
88  }
89
90  *dest = 0;
91  return dest - start;
92}
93
94int UrlDecode(const char *source, char *dest) {
95  return InternalUrlDecode(source, dest, true);
96}
97
98int UrlDecodeWithoutEncodingSpaceAsPlus(const char *source, char *dest) {
99  return InternalUrlDecode(source, dest, false);
100}
101
102bool IsValidUrlChar(char ch, bool unsafe_only) {
103  if (unsafe_only) {
104    return !(ch <= ' ' || strchr("\\\"^&`<>[]{}", ch));
105  } else {
106    return isalnum(ch) || strchr("-_.!~*'()", ch);
107  }
108}
109
110int InternalUrlEncode(const char *source, char *dest, unsigned int max,
111                      bool encode_space_as_plus, bool unsafe_only) {
112  static const char *digits = "0123456789ABCDEF";
113  if (max == 0) {
114    return 0;
115  }
116
117  char *start = dest;
118  while (static_cast<unsigned>(dest - start) < max && *source) {
119    unsigned char ch = static_cast<unsigned char>(*source);
120    if (*source == ' ' && encode_space_as_plus && !unsafe_only) {
121      *dest++ = '+';
122    } else if (IsValidUrlChar(ch, unsafe_only)) {
123      *dest++ = *source;
124    } else {
125      if (static_cast<unsigned>(dest - start) + 4 > max) {
126        break;
127      }
128      *dest++ = '%';
129      *dest++ = digits[(ch >> 4) & 0x0F];
130      *dest++ = digits[       ch & 0x0F];
131    }
132    source++;
133  }
134  ASSERT(static_cast<unsigned int>(dest - start) < max);
135  *dest = 0;
136
137  return dest - start;
138}
139
140int UrlEncode(const char *source, char *dest, unsigned max) {
141  return InternalUrlEncode(source, dest, max, true, false);
142}
143
144int UrlEncodeWithoutEncodingSpaceAsPlus(const char *source, char *dest,
145                                        unsigned max) {
146  return InternalUrlEncode(source, dest, max, false, false);
147}
148
149int UrlEncodeOnlyUnsafeChars(const char *source, char *dest, unsigned max) {
150  return InternalUrlEncode(source, dest, max, false, true);
151}
152
153std::string
154InternalUrlDecodeString(const std::string & encoded,
155                        bool encode_space_as_plus) {
156  size_t needed_length = encoded.length() + 1;
157  char* buf = STACK_ARRAY(char, needed_length);
158  InternalUrlDecode(encoded.c_str(), buf, encode_space_as_plus);
159  return buf;
160}
161
162std::string
163UrlDecodeString(const std::string & encoded) {
164  return InternalUrlDecodeString(encoded, true);
165}
166
167std::string
168UrlDecodeStringWithoutEncodingSpaceAsPlus(const std::string & encoded) {
169  return InternalUrlDecodeString(encoded, false);
170}
171
172std::string
173InternalUrlEncodeString(const std::string & decoded,
174                        bool encode_space_as_plus,
175                        bool unsafe_only) {
176  size_t needed_length = decoded.length() * 3 + 1;
177  char* buf = STACK_ARRAY(char, needed_length);
178  InternalUrlEncode(decoded.c_str(), buf, needed_length,
179                    encode_space_as_plus, unsafe_only);
180  return buf;
181}
182
183std::string
184UrlEncodeString(const std::string & decoded) {
185  return InternalUrlEncodeString(decoded, true, false);
186}
187
188std::string
189UrlEncodeStringWithoutEncodingSpaceAsPlus(const std::string & decoded) {
190  return InternalUrlEncodeString(decoded, false, false);
191}
192
193std::string
194UrlEncodeStringForOnlyUnsafeChars(const std::string & decoded) {
195  return InternalUrlEncodeString(decoded, false, true);
196}
197