This source file includes following definitions.
- appendURLEscapedChar
- ConvertFromUTF16
- IDNToASCII
- ReadUTFChar
- ReadUTFChar
#include <stdlib.h>
#include <string.h>
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "third_party/icu/source/common/unicode/ucnv.h"
#include "third_party/icu/source/common/unicode/ucnv_cb.h"
#include "third_party/icu/source/common/unicode/uidna.h"
#include "url/url_canon_icu.h"
#include "url/url_canon_internal.h"
namespace url_canon {
namespace {
void appendURLEscapedChar(const void* context,
UConverterFromUnicodeArgs* from_args,
const UChar* code_units,
int32_t length,
UChar32 code_point,
UConverterCallbackReason reason,
UErrorCode* err) {
if (reason == UCNV_UNASSIGNED) {
*err = U_ZERO_ERROR;
const static int prefix_len = 6;
const static char prefix[prefix_len + 1] = "%26%23";
ucnv_cbFromUWriteBytes(from_args, prefix, prefix_len, 0, err);
DCHECK(code_point < 0x110000);
char number[8];
_itoa_s(code_point, number, 10);
int number_len = static_cast<int>(strlen(number));
ucnv_cbFromUWriteBytes(from_args, number, number_len, 0, err);
const static int postfix_len = 3;
const static char postfix[postfix_len + 1] = "%3B";
ucnv_cbFromUWriteBytes(from_args, postfix, postfix_len, 0, err);
}
}
class AppendHandlerInstaller {
public:
AppendHandlerInstaller(UConverter* converter) : converter_(converter) {
UErrorCode err = U_ZERO_ERROR;
ucnv_setFromUCallBack(converter_, appendURLEscapedChar, 0,
&old_callback_, &old_context_, &err);
}
~AppendHandlerInstaller() {
UErrorCode err = U_ZERO_ERROR;
ucnv_setFromUCallBack(converter_, old_callback_, old_context_, 0, 0, &err);
}
private:
UConverter* converter_;
UConverterFromUCallback old_callback_;
const void* old_context_;
};
struct UIDNAWrapper {
UIDNAWrapper() {
UErrorCode err = U_ZERO_ERROR;
value = uidna_openUTS46(UIDNA_CHECK_BIDI, &err);
if (U_FAILURE(err))
value = NULL;
}
UIDNA* value;
};
}
ICUCharsetConverter::ICUCharsetConverter(UConverter* converter)
: converter_(converter) {
}
ICUCharsetConverter::~ICUCharsetConverter() {
}
void ICUCharsetConverter::ConvertFromUTF16(const base::char16* input,
int input_len,
CanonOutput* output) {
AppendHandlerInstaller handler(converter_);
int begin_offset = output->length();
int dest_capacity = output->capacity() - begin_offset;
output->set_length(output->length());
do {
UErrorCode err = U_ZERO_ERROR;
char* dest = &output->data()[begin_offset];
int required_capacity = ucnv_fromUChars(converter_, dest, dest_capacity,
input, input_len, &err);
if (err != U_BUFFER_OVERFLOW_ERROR) {
output->set_length(begin_offset + required_capacity);
return;
}
dest_capacity = required_capacity;
output->Resize(begin_offset + dest_capacity);
} while (true);
}
static base::LazyInstance<UIDNAWrapper>::Leaky
g_uidna = LAZY_INSTANCE_INITIALIZER;
bool IDNToASCII(const base::char16* src, int src_len, CanonOutputW* output) {
DCHECK(output->length() == 0);
UIDNA* uidna = g_uidna.Get().value;
DCHECK(uidna != NULL);
while (true) {
UErrorCode err = U_ZERO_ERROR;
UIDNAInfo info = UIDNA_INFO_INITIALIZER;
int output_length = uidna_nameToASCII(uidna, src, src_len, output->data(),
output->capacity(), &info, &err);
if (U_SUCCESS(err) && info.errors == 0) {
output->set_length(output_length);
return true;
}
if (err != U_BUFFER_OVERFLOW_ERROR || info.errors != 0)
return false;
output->Resize(output_length);
}
}
bool ReadUTFChar(const char* str, int* begin, int length,
unsigned* code_point_out) {
int code_point;
U8_NEXT(str, *begin, length, code_point);
*code_point_out = static_cast<unsigned>(code_point);
(*begin)--;
if (U_IS_UNICODE_CHAR(code_point))
return true;
*code_point_out = kUnicodeReplacementCharacter;
return false;
}
bool ReadUTFChar(const base::char16* str, int* begin, int length,
unsigned* code_point) {
if (U16_IS_SURROGATE(str[*begin])) {
if (!U16_IS_SURROGATE_LEAD(str[*begin]) || *begin + 1 >= length ||
!U16_IS_TRAIL(str[*begin + 1])) {
*code_point = kUnicodeReplacementCharacter;
return false;
} else {
*code_point = U16_GET_SUPPLEMENTARY(str[*begin], str[*begin + 1]);
(*begin)++;
}
} else {
*code_point = str[*begin];
}
if (U_IS_UNICODE_CHAR(*code_point))
return true;
*code_point = kUnicodeReplacementCharacter;
return false;
}
}