3
* Copyright 2008, Google Inc.
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
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.
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.
28
#include "talk/base/urlencode.h"
30
#include "talk/base/common.h"
31
#include "talk/base/stringutils.h"
33
static int HexPairValue(const char * code) {
35
const char * pch = code;
38
if (digit >= '0' && digit <= '9') {
41
else if (digit >= 'A' && digit <= 'F') {
42
value += digit - 'A' + 10;
44
else if (digit >= 'a' && digit <= 'f') {
45
value += digit - 'a' + 10;
56
int InternalUrlDecode(const char *source, char *dest,
57
bool encode_space_as_plus) {
63
if (encode_space_as_plus) {
70
if (source[1] && source[2]) {
71
int value = HexPairValue(source + 1);
91
return static_cast<int>(dest - start);
94
int UrlDecode(const char *source, char *dest) {
95
return InternalUrlDecode(source, dest, true);
98
int UrlDecodeWithoutEncodingSpaceAsPlus(const char *source, char *dest) {
99
return InternalUrlDecode(source, dest, false);
102
bool IsValidUrlChar(char ch, bool unsafe_only) {
104
return !(ch <= ' ' || strchr("\\\"^&`<>[]{}", ch));
106
return isalnum(ch) || strchr("-_.!~*'()", ch);
110
int InternalUrlEncode(const char *source, char *dest, unsigned int max,
111
bool encode_space_as_plus, bool unsafe_only) {
112
static const char *digits = "0123456789ABCDEF";
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) {
122
} else if (IsValidUrlChar(ch, unsafe_only)) {
125
if (static_cast<unsigned>(dest - start) + 4 > max) {
129
*dest++ = digits[(ch >> 4) & 0x0F];
130
*dest++ = digits[ ch & 0x0F];
134
ASSERT(static_cast<unsigned int>(dest - start) < max);
137
return static_cast<int>(dest - start);
140
int UrlEncode(const char *source, char *dest, unsigned max) {
141
return InternalUrlEncode(source, dest, max, true, false);
144
int UrlEncodeWithoutEncodingSpaceAsPlus(const char *source, char *dest,
146
return InternalUrlEncode(source, dest, max, false, false);
149
int UrlEncodeOnlyUnsafeChars(const char *source, char *dest, unsigned max) {
150
return InternalUrlEncode(source, dest, max, false, true);
154
InternalUrlDecodeString(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);
163
UrlDecodeString(const std::string & encoded) {
164
return InternalUrlDecodeString(encoded, true);
168
UrlDecodeStringWithoutEncodingSpaceAsPlus(const std::string & encoded) {
169
return InternalUrlDecodeString(encoded, false);
173
InternalUrlEncodeString(const std::string & decoded,
174
bool encode_space_as_plus,
176
int needed_length = static_cast<int>(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);
184
UrlEncodeString(const std::string & decoded) {
185
return InternalUrlEncodeString(decoded, true, false);
189
UrlEncodeStringWithoutEncodingSpaceAsPlus(const std::string & decoded) {
190
return InternalUrlEncodeString(decoded, false, false);
194
UrlEncodeStringForOnlyUnsafeChars(const std::string & decoded) {
195
return InternalUrlEncodeString(decoded, false, true);