This source file includes following definitions.
- DoAppendStringOfType
- DoAppendInvalidNarrowString
- DoOverrideComponent
- PrepareUTF16OverrideComponent
- AppendStringOfType
- AppendStringOfType
- AppendInvalidNarrowString
- AppendInvalidNarrowString
- ConvertUTF16ToUTF8
- ConvertUTF8ToUTF16
- SetupOverrideComponents
- SetupUTF16OverrideComponents
- _itoa_s
- _itow_s
#include <errno.h>
#include <stdlib.h>
#include <cstdio>
#include <string>
#include "url/url_canon_internal.h"
namespace url_canon {
namespace {
template<typename CHAR, typename UCHAR>
void DoAppendStringOfType(const CHAR* source, int length,
SharedCharTypes type,
CanonOutput* output) {
for (int i = 0; i < length; i++) {
if (static_cast<UCHAR>(source[i]) >= 0x80) {
unsigned code_point;
ReadUTFChar(source, &i, length, &code_point);
AppendUTF8EscapedValue(code_point, output);
} else {
unsigned char uch = static_cast<unsigned char>(source[i]);
if (!IsCharOfType(uch, type))
AppendEscapedChar(uch, output);
else
output->push_back(uch);
}
}
}
template<typename CHAR, typename UCHAR>
void DoAppendInvalidNarrowString(const CHAR* spec, int begin, int end,
CanonOutput* output) {
for (int i = begin; i < end; i++) {
UCHAR uch = static_cast<UCHAR>(spec[i]);
if (uch >= 0x80) {
AppendUTF8EscapedChar(spec, &i, end, output);
} else if (uch <= ' ' || uch == 0x7f) {
AppendEscapedChar(static_cast<unsigned char>(uch), output);
} else {
output->push_back(static_cast<char>(uch));
}
}
}
void DoOverrideComponent(const char* override_source,
const url_parse::Component& override_component,
const char** dest,
url_parse::Component* dest_component) {
if (override_source) {
*dest = override_source;
*dest_component = override_component;
}
}
bool PrepareUTF16OverrideComponent(
const base::char16* override_source,
const url_parse::Component& override_component,
CanonOutput* utf8_buffer,
url_parse::Component* dest_component) {
bool success = true;
if (override_source) {
if (!override_component.is_valid()) {
*dest_component = url_parse::Component();
} else {
dest_component->begin = utf8_buffer->length();
success = ConvertUTF16ToUTF8(&override_source[override_component.begin],
override_component.len, utf8_buffer);
dest_component->len = utf8_buffer->length() - dest_component->begin;
}
}
return success;
}
}
const unsigned char kSharedCharTypeTable[0x100] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
0,
0,
CHAR_QUERY | CHAR_USERINFO,
CHAR_QUERY | CHAR_USERINFO,
CHAR_QUERY | CHAR_USERINFO,
0,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO,
CHAR_QUERY | CHAR_USERINFO,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_COMPONENT,
CHAR_QUERY,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_OCT | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_DEC | CHAR_COMPONENT,
CHAR_QUERY,
CHAR_QUERY,
0,
CHAR_QUERY,
0,
CHAR_QUERY,
CHAR_QUERY,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY,
CHAR_QUERY,
CHAR_QUERY,
CHAR_QUERY,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_HEX | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_IPV4 | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
CHAR_QUERY,
CHAR_QUERY,
CHAR_QUERY,
CHAR_QUERY | CHAR_USERINFO | CHAR_COMPONENT,
0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
const char kHexCharLookup[0x10] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
};
const char kCharToHexLookup[8] = {
0,
'0',
'A' - 10,
'a' - 10,
0,
0,
0,
0,
};
const base::char16 kUnicodeReplacementCharacter = 0xfffd;
void AppendStringOfType(const char* source, int length,
SharedCharTypes type,
CanonOutput* output) {
DoAppendStringOfType<char, unsigned char>(source, length, type, output);
}
void AppendStringOfType(const base::char16* source, int length,
SharedCharTypes type,
CanonOutput* output) {
DoAppendStringOfType<base::char16, base::char16>(
source, length, type, output);
}
void AppendInvalidNarrowString(const char* spec, int begin, int end,
CanonOutput* output) {
DoAppendInvalidNarrowString<char, unsigned char>(spec, begin, end, output);
}
void AppendInvalidNarrowString(const base::char16* spec, int begin, int end,
CanonOutput* output) {
DoAppendInvalidNarrowString<base::char16, base::char16>(
spec, begin, end, output);
}
bool ConvertUTF16ToUTF8(const base::char16* input, int input_len,
CanonOutput* output) {
bool success = true;
for (int i = 0; i < input_len; i++) {
unsigned code_point;
success &= ReadUTFChar(input, &i, input_len, &code_point);
AppendUTF8Value(code_point, output);
}
return success;
}
bool ConvertUTF8ToUTF16(const char* input, int input_len,
CanonOutputT<base::char16>* output) {
bool success = true;
for (int i = 0; i < input_len; i++) {
unsigned code_point;
success &= ReadUTFChar(input, &i, input_len, &code_point);
AppendUTF16Value(code_point, output);
}
return success;
}
void SetupOverrideComponents(const char* base,
const Replacements<char>& repl,
URLComponentSource<char>* source,
url_parse::Parsed* parsed) {
const URLComponentSource<char>& repl_source = repl.sources();
const url_parse::Parsed& repl_parsed = repl.components();
DoOverrideComponent(repl_source.scheme, repl_parsed.scheme,
&source->scheme, &parsed->scheme);
DoOverrideComponent(repl_source.username, repl_parsed.username,
&source->username, &parsed->username);
DoOverrideComponent(repl_source.password, repl_parsed.password,
&source->password, &parsed->password);
DoOverrideComponent(repl_source.host, repl_parsed.host,
&source->host, &parsed->host);
if (parsed->host.len == -1)
parsed->host.len = 0;
DoOverrideComponent(repl_source.port, repl_parsed.port,
&source->port, &parsed->port);
DoOverrideComponent(repl_source.path, repl_parsed.path,
&source->path, &parsed->path);
DoOverrideComponent(repl_source.query, repl_parsed.query,
&source->query, &parsed->query);
DoOverrideComponent(repl_source.ref, repl_parsed.ref,
&source->ref, &parsed->ref);
}
bool SetupUTF16OverrideComponents(const char* base,
const Replacements<base::char16>& repl,
CanonOutput* utf8_buffer,
URLComponentSource<char>* source,
url_parse::Parsed* parsed) {
bool success = true;
const URLComponentSource<base::char16>& repl_source = repl.sources();
const url_parse::Parsed& repl_parsed = repl.components();
success &= PrepareUTF16OverrideComponent(
repl_source.scheme, repl_parsed.scheme,
utf8_buffer, &parsed->scheme);
success &= PrepareUTF16OverrideComponent(
repl_source.username, repl_parsed.username,
utf8_buffer, &parsed->username);
success &= PrepareUTF16OverrideComponent(
repl_source.password, repl_parsed.password,
utf8_buffer, &parsed->password);
success &= PrepareUTF16OverrideComponent(
repl_source.host, repl_parsed.host,
utf8_buffer, &parsed->host);
success &= PrepareUTF16OverrideComponent(
repl_source.port, repl_parsed.port,
utf8_buffer, &parsed->port);
success &= PrepareUTF16OverrideComponent(
repl_source.path, repl_parsed.path,
utf8_buffer, &parsed->path);
success &= PrepareUTF16OverrideComponent(
repl_source.query, repl_parsed.query,
utf8_buffer, &parsed->query);
success &= PrepareUTF16OverrideComponent(
repl_source.ref, repl_parsed.ref,
utf8_buffer, &parsed->ref);
if (repl_source.scheme) source->scheme = utf8_buffer->data();
if (repl_source.username) source->username = utf8_buffer->data();
if (repl_source.password) source->password = utf8_buffer->data();
if (repl_source.host) source->host = utf8_buffer->data();
if (repl_source.port) source->port = utf8_buffer->data();
if (repl_source.path) source->path = utf8_buffer->data();
if (repl_source.query) source->query = utf8_buffer->data();
if (repl_source.ref) source->ref = utf8_buffer->data();
return success;
}
#ifndef WIN32
int _itoa_s(int value, char* buffer, size_t size_in_chars, int radix) {
const char* format_str;
if (radix == 10)
format_str = "%d";
else if (radix == 16)
format_str = "%x";
else
return EINVAL;
int written = snprintf(buffer, size_in_chars, format_str, value);
if (static_cast<size_t>(written) >= size_in_chars) {
return EINVAL;
}
return 0;
}
int _itow_s(int value, base::char16* buffer, size_t size_in_chars, int radix) {
if (radix != 10)
return EINVAL;
char temp[13];
int written = snprintf(temp, sizeof(temp), "%d", value);
if (static_cast<size_t>(written) >= size_in_chars) {
return EINVAL;
}
for (int i = 0; i < written; ++i) {
buffer[i] = static_cast<base::char16>(temp[i]);
}
buffer[written] = '\0';
return 0;
}
#endif
}