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.
4
5 // ICU-based character set converter.
6
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <string.h>
10
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
19
20 namespace url {
21
22 namespace {
23
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.
appendURLEscapedChar(const void * context,UConverterFromUnicodeArgs * from_args,const UChar * code_units,int32_t length,UChar32 code_point,UConverterCallbackReason reason,UErrorCode * err)28 void appendURLEscapedChar(const void* context,
29 UConverterFromUnicodeArgs* from_args,
30 const UChar* code_units,
31 int32_t length,
32 UChar32 code_point,
33 UConverterCallbackReason reason,
34 UErrorCode* err) {
35 if (reason == UCNV_UNASSIGNED) {
36 *err = U_ZERO_ERROR;
37
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);
41
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);
47
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);
51 }
52 }
53
54 // A class for scoping the installation of the invalid character callback.
55 class AppendHandlerInstaller {
56 public:
57 // The owner of this object must ensure that the converter is alive for the
58 // duration of this object's lifetime.
AppendHandlerInstaller(UConverter * converter)59 AppendHandlerInstaller(UConverter* converter) : converter_(converter) {
60 UErrorCode err = U_ZERO_ERROR;
61 ucnv_setFromUCallBack(converter_, appendURLEscapedChar, 0,
62 &old_callback_, &old_context_, &err);
63 }
64
~AppendHandlerInstaller()65 ~AppendHandlerInstaller() {
66 UErrorCode err = U_ZERO_ERROR;
67 ucnv_setFromUCallBack(converter_, old_callback_, old_context_, 0, 0, &err);
68 }
69
70 private:
71 raw_ptr<UConverter> converter_;
72
73 UConverterFromUCallback old_callback_;
74 // This field is not a raw_ptr<> because it was filtered by the rewriter for:
75 // #addr-of
76 RAW_PTR_EXCLUSION const void* old_context_;
77 };
78
79 } // namespace
80
ICUCharsetConverter(UConverter * converter)81 ICUCharsetConverter::ICUCharsetConverter(UConverter* converter)
82 : converter_(converter) {
83 }
84
85 ICUCharsetConverter::~ICUCharsetConverter() = default;
86
ConvertFromUTF16(const char16_t * input,int input_len,CanonOutput * output)87 void ICUCharsetConverter::ConvertFromUTF16(const char16_t* input,
88 int input_len,
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_);
93
94 int begin_offset = output->length();
95 int dest_capacity = output->capacity() - begin_offset;
96 output->set_length(output->length());
97
98 do {
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);
105 return;
106 }
107
108 // Output didn't fit, expand
109 dest_capacity = required_capacity;
110 output->Resize(begin_offset + dest_capacity);
111 } while (true);
112 }
113
114 } // namespace url
115