Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
michael@0 | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/strings/utf_string_conversion_utils.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/third_party/icu/icu_utf.h" |
michael@0 | 8 | |
michael@0 | 9 | namespace base { |
michael@0 | 10 | |
michael@0 | 11 | // ReadUnicodeCharacter -------------------------------------------------------- |
michael@0 | 12 | |
michael@0 | 13 | bool ReadUnicodeCharacter(const char* src, |
michael@0 | 14 | int32 src_len, |
michael@0 | 15 | int32* char_index, |
michael@0 | 16 | uint32* code_point_out) { |
michael@0 | 17 | // U8_NEXT expects to be able to use -1 to signal an error, so we must |
michael@0 | 18 | // use a signed type for code_point. But this function returns false |
michael@0 | 19 | // on error anyway, so code_point_out is unsigned. |
michael@0 | 20 | int32 code_point; |
michael@0 | 21 | CBU8_NEXT(src, *char_index, src_len, code_point); |
michael@0 | 22 | *code_point_out = static_cast<uint32>(code_point); |
michael@0 | 23 | |
michael@0 | 24 | // The ICU macro above moves to the next char, we want to point to the last |
michael@0 | 25 | // char consumed. |
michael@0 | 26 | (*char_index)--; |
michael@0 | 27 | |
michael@0 | 28 | // Validate the decoded value. |
michael@0 | 29 | return IsValidCodepoint(code_point); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | bool ReadUnicodeCharacter(const char16* src, |
michael@0 | 33 | int32 src_len, |
michael@0 | 34 | int32* char_index, |
michael@0 | 35 | uint32* code_point) { |
michael@0 | 36 | if (CBU16_IS_SURROGATE(src[*char_index])) { |
michael@0 | 37 | if (!CBU16_IS_SURROGATE_LEAD(src[*char_index]) || |
michael@0 | 38 | *char_index + 1 >= src_len || |
michael@0 | 39 | !CBU16_IS_TRAIL(src[*char_index + 1])) { |
michael@0 | 40 | // Invalid surrogate pair. |
michael@0 | 41 | return false; |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | // Valid surrogate pair. |
michael@0 | 45 | *code_point = CBU16_GET_SUPPLEMENTARY(src[*char_index], |
michael@0 | 46 | src[*char_index + 1]); |
michael@0 | 47 | (*char_index)++; |
michael@0 | 48 | } else { |
michael@0 | 49 | // Not a surrogate, just one 16-bit word. |
michael@0 | 50 | *code_point = src[*char_index]; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | return IsValidCodepoint(*code_point); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | #if defined(WCHAR_T_IS_UTF32) |
michael@0 | 57 | bool ReadUnicodeCharacter(const wchar_t* src, |
michael@0 | 58 | int32 src_len, |
michael@0 | 59 | int32* char_index, |
michael@0 | 60 | uint32* code_point) { |
michael@0 | 61 | // Conversion is easy since the source is 32-bit. |
michael@0 | 62 | *code_point = src[*char_index]; |
michael@0 | 63 | |
michael@0 | 64 | // Validate the value. |
michael@0 | 65 | return IsValidCodepoint(*code_point); |
michael@0 | 66 | } |
michael@0 | 67 | #endif // defined(WCHAR_T_IS_UTF32) |
michael@0 | 68 | |
michael@0 | 69 | // WriteUnicodeCharacter ------------------------------------------------------- |
michael@0 | 70 | |
michael@0 | 71 | size_t WriteUnicodeCharacter(uint32 code_point, std::string* output) { |
michael@0 | 72 | if (code_point <= 0x7f) { |
michael@0 | 73 | // Fast path the common case of one byte. |
michael@0 | 74 | output->push_back(code_point); |
michael@0 | 75 | return 1; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | |
michael@0 | 79 | // CBU8_APPEND_UNSAFE can append up to 4 bytes. |
michael@0 | 80 | size_t char_offset = output->length(); |
michael@0 | 81 | size_t original_char_offset = char_offset; |
michael@0 | 82 | output->resize(char_offset + CBU8_MAX_LENGTH); |
michael@0 | 83 | |
michael@0 | 84 | CBU8_APPEND_UNSAFE(&(*output)[0], char_offset, code_point); |
michael@0 | 85 | |
michael@0 | 86 | // CBU8_APPEND_UNSAFE will advance our pointer past the inserted character, so |
michael@0 | 87 | // it will represent the new length of the string. |
michael@0 | 88 | output->resize(char_offset); |
michael@0 | 89 | return char_offset - original_char_offset; |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | size_t WriteUnicodeCharacter(uint32 code_point, string16* output) { |
michael@0 | 93 | if (CBU16_LENGTH(code_point) == 1) { |
michael@0 | 94 | // Thie code point is in the Basic Multilingual Plane (BMP). |
michael@0 | 95 | output->push_back(static_cast<char16>(code_point)); |
michael@0 | 96 | return 1; |
michael@0 | 97 | } |
michael@0 | 98 | // Non-BMP characters use a double-character encoding. |
michael@0 | 99 | size_t char_offset = output->length(); |
michael@0 | 100 | output->resize(char_offset + CBU16_MAX_LENGTH); |
michael@0 | 101 | CBU16_APPEND_UNSAFE(&(*output)[0], char_offset, code_point); |
michael@0 | 102 | return CBU16_MAX_LENGTH; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | // Generalized Unicode converter ----------------------------------------------- |
michael@0 | 106 | |
michael@0 | 107 | template<typename CHAR> |
michael@0 | 108 | void PrepareForUTF8Output(const CHAR* src, |
michael@0 | 109 | size_t src_len, |
michael@0 | 110 | std::string* output) { |
michael@0 | 111 | output->clear(); |
michael@0 | 112 | if (src_len == 0) |
michael@0 | 113 | return; |
michael@0 | 114 | if (src[0] < 0x80) { |
michael@0 | 115 | // Assume that the entire input will be ASCII. |
michael@0 | 116 | output->reserve(src_len); |
michael@0 | 117 | } else { |
michael@0 | 118 | // Assume that the entire input is non-ASCII and will have 3 bytes per char. |
michael@0 | 119 | output->reserve(src_len * 3); |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | // Instantiate versions we know callers will need. |
michael@0 | 124 | template void PrepareForUTF8Output(const wchar_t*, size_t, std::string*); |
michael@0 | 125 | template void PrepareForUTF8Output(const char16*, size_t, std::string*); |
michael@0 | 126 | |
michael@0 | 127 | template<typename STRING> |
michael@0 | 128 | void PrepareForUTF16Or32Output(const char* src, |
michael@0 | 129 | size_t src_len, |
michael@0 | 130 | STRING* output) { |
michael@0 | 131 | output->clear(); |
michael@0 | 132 | if (src_len == 0) |
michael@0 | 133 | return; |
michael@0 | 134 | if (static_cast<unsigned char>(src[0]) < 0x80) { |
michael@0 | 135 | // Assume the input is all ASCII, which means 1:1 correspondence. |
michael@0 | 136 | output->reserve(src_len); |
michael@0 | 137 | } else { |
michael@0 | 138 | // Otherwise assume that the UTF-8 sequences will have 2 bytes for each |
michael@0 | 139 | // character. |
michael@0 | 140 | output->reserve(src_len / 2); |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | // Instantiate versions we know callers will need. |
michael@0 | 145 | template void PrepareForUTF16Or32Output(const char*, size_t, std::wstring*); |
michael@0 | 146 | template void PrepareForUTF16Or32Output(const char*, size_t, string16*); |
michael@0 | 147 | |
michael@0 | 148 | } // namespace base |