1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // ICU-based character set converter.
11 #include "base/check.h"
12 #include "base/memory/raw_ptr.h"
13 #include "base/memory/raw_ptr_exclusion.h"
14 #include "third_party/icu/source/common/unicode/ucnv.h"
15 #include "third_party/icu/source/common/unicode/ucnv_cb.h"
16 #include "third_party/icu/source/common/unicode/utypes.h"
17 #include "url/url_canon_icu.h"
18 #include "url/url_canon_internal.h" // for _itoa_s
24 // Called when converting a character that can not be represented, this will
25 // append an escaped version of the numerical character reference for that code
26 // point. It is of the form "Ӓ" and we will escape the non-digits to
27 // "%26%231234%3B". Why? This is what Netscape did back in the olden days.
28 void appendURLEscapedChar(const void* context,
29 UConverterFromUnicodeArgs* from_args,
30 const UChar* code_units,
33 UConverterCallbackReason reason,
35 if (reason == UCNV_UNASSIGNED) {
38 const static int prefix_len = 6;
39 const static char prefix[prefix_len + 1] = "%26%23"; // "&#" percent-escaped
40 ucnv_cbFromUWriteBytes(from_args, prefix, prefix_len, 0, err);
42 DCHECK(code_point < 0x110000);
43 char number[8]; // Max Unicode code point is 7 digits.
44 _itoa_s(code_point, number, 10);
45 int number_len = static_cast<int>(strlen(number));
46 ucnv_cbFromUWriteBytes(from_args, number, number_len, 0, err);
48 const static int postfix_len = 3;
49 const static char postfix[postfix_len + 1] = "%3B"; // ";" percent-escaped
50 ucnv_cbFromUWriteBytes(from_args, postfix, postfix_len, 0, err);
54 // A class for scoping the installation of the invalid character callback.
55 class AppendHandlerInstaller {
57 // The owner of this object must ensure that the converter is alive for the
58 // duration of this object's lifetime.
59 AppendHandlerInstaller(UConverter* converter) : converter_(converter) {
60 UErrorCode err = U_ZERO_ERROR;
61 ucnv_setFromUCallBack(converter_, appendURLEscapedChar, 0,
62 &old_callback_, &old_context_, &err);
65 ~AppendHandlerInstaller() {
66 UErrorCode err = U_ZERO_ERROR;
67 ucnv_setFromUCallBack(converter_, old_callback_, old_context_, 0, 0, &err);
71 raw_ptr<UConverter> converter_;
73 UConverterFromUCallback old_callback_;
74 // This field is not a raw_ptr<> because it was filtered by the rewriter for:
76 RAW_PTR_EXCLUSION const void* old_context_;
81 ICUCharsetConverter::ICUCharsetConverter(UConverter* converter)
82 : converter_(converter) {
85 ICUCharsetConverter::~ICUCharsetConverter() = default;
87 void ICUCharsetConverter::ConvertFromUTF16(const char16_t* input,
89 CanonOutput* output) {
90 // Install our error handler. It will be called for character that can not
91 // be represented in the destination character set.
92 AppendHandlerInstaller handler(converter_);
94 int begin_offset = output->length();
95 int dest_capacity = output->capacity() - begin_offset;
96 output->set_length(output->length());
99 UErrorCode err = U_ZERO_ERROR;
100 char* dest = &output->data()[begin_offset];
101 int required_capacity = ucnv_fromUChars(converter_, dest, dest_capacity,
102 input, input_len, &err);
103 if (err != U_BUFFER_OVERFLOW_ERROR) {
104 output->set_length(begin_offset + required_capacity);
108 // Output didn't fit, expand
109 dest_capacity = required_capacity;
110 output->Resize(begin_offset + dest_capacity);