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 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/350788890): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 // ICU-based character set converter.
11
12 #include "url/url_canon_icu.h"
13
14 #include <stdint.h>
15 #include <stdlib.h>
16 #include <string.h>
17
18 #include "base/check.h"
19 #include "base/memory/stack_allocated.h"
20 #include "base/numerics/safe_conversions.h"
21 #include "third_party/icu/source/common/unicode/ucnv.h"
22 #include "third_party/icu/source/common/unicode/ucnv_cb.h"
23 #include "third_party/icu/source/common/unicode/utypes.h"
24 #include "url/url_canon_internal.h" // for _itoa_s
25
26 namespace url {
27
28 namespace {
29
30 // Called when converting a character that can not be represented, this will
31 // append an escaped version of the numerical character reference for that code
32 // point. It is of the form "Ӓ" and we will escape the non-digits to
33 // "%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)34 void appendURLEscapedChar(const void* context,
35 UConverterFromUnicodeArgs* from_args,
36 const UChar* code_units,
37 int32_t length,
38 UChar32 code_point,
39 UConverterCallbackReason reason,
40 UErrorCode* err) {
41 if (reason == UCNV_UNASSIGNED) {
42 *err = U_ZERO_ERROR;
43
44 const static int prefix_len = 6;
45 const static char prefix[prefix_len + 1] = "%26%23"; // "&#" percent-escaped
46 ucnv_cbFromUWriteBytes(from_args, prefix, prefix_len, 0, err);
47
48 DCHECK(code_point < 0x110000);
49 char number[8]; // Max Unicode code point is 7 digits.
50 _itoa_s(code_point, number, 10);
51 int number_len = static_cast<int>(strlen(number));
52 ucnv_cbFromUWriteBytes(from_args, number, number_len, 0, err);
53
54 const static int postfix_len = 3;
55 const static char postfix[postfix_len + 1] = "%3B"; // ";" percent-escaped
56 ucnv_cbFromUWriteBytes(from_args, postfix, postfix_len, 0, err);
57 }
58 }
59
60 // A class for scoping the installation of the invalid character callback.
61 class AppendHandlerInstaller {
62 STACK_ALLOCATED();
63
64 public:
65 // The owner of this object must ensure that the converter is alive for the
66 // duration of this object's lifetime.
AppendHandlerInstaller(UConverter * converter)67 AppendHandlerInstaller(UConverter* converter) : converter_(converter) {
68 UErrorCode err = U_ZERO_ERROR;
69 ucnv_setFromUCallBack(converter_, appendURLEscapedChar, 0,
70 &old_callback_, &old_context_, &err);
71 }
72
~AppendHandlerInstaller()73 ~AppendHandlerInstaller() {
74 UErrorCode err = U_ZERO_ERROR;
75 ucnv_setFromUCallBack(converter_, old_callback_, old_context_, 0, 0, &err);
76 }
77
78 private:
79 UConverter* converter_;
80
81 UConverterFromUCallback old_callback_;
82 const void* old_context_;
83 };
84
85 } // namespace
86
ICUCharsetConverter(UConverter * converter)87 ICUCharsetConverter::ICUCharsetConverter(UConverter* converter)
88 : converter_(converter) {
89 }
90
91 ICUCharsetConverter::~ICUCharsetConverter() = default;
92
ConvertFromUTF16(std::u16string_view input,CanonOutput * output)93 void ICUCharsetConverter::ConvertFromUTF16(std::u16string_view input,
94 CanonOutput* output) {
95 // Install our error handler. It will be called for character that can not
96 // be represented in the destination character set.
97 AppendHandlerInstaller handler(converter_);
98
99 int begin_offset = output->length();
100 int dest_capacity = output->capacity() - begin_offset;
101 output->set_length(output->length());
102
103 do {
104 UErrorCode err = U_ZERO_ERROR;
105 char* dest = &output->data()[begin_offset];
106 int required_capacity =
107 ucnv_fromUChars(converter_, dest, dest_capacity, input.data(),
108 base::checked_cast<int32_t>(input.size()), &err);
109 if (err != U_BUFFER_OVERFLOW_ERROR) {
110 output->set_length(begin_offset + required_capacity);
111 return;
112 }
113
114 // Output didn't fit, expand
115 dest_capacity = required_capacity;
116 output->Resize(begin_offset + dest_capacity);
117 } while (true);
118 }
119
120 } // namespace url
121