| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "ppapi/shared_impl/char_set_impl.h" | |
| 6 | |
| 7 #include "base/i18n/icu_string_conversions.h" | |
| 8 #include "ppapi/c/dev/ppb_memory_dev.h" | |
| 9 #include "ppapi/thunk/thunk.h" | |
| 10 #include "unicode/ucnv.h" | |
| 11 #include "unicode/ucnv_cb.h" | |
| 12 #include "unicode/ucnv_err.h" | |
| 13 #include "unicode/ustring.h" | |
| 14 | |
| 15 namespace ppapi { | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 // Converts the given PP error handling behavior to the version in base, | |
| 20 // placing the result in |*result| and returning true on success. Returns false | |
| 21 // if the enum is invalid. | |
| 22 bool PPToBaseConversionError(PP_CharSet_ConversionError on_error, | |
| 23 base::OnStringConversionError::Type* result) { | |
| 24 switch (on_error) { | |
| 25 case PP_CHARSET_CONVERSIONERROR_FAIL: | |
| 26 *result = base::OnStringConversionError::FAIL; | |
| 27 return true; | |
| 28 case PP_CHARSET_CONVERSIONERROR_SKIP: | |
| 29 *result = base::OnStringConversionError::SKIP; | |
| 30 return true; | |
| 31 case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE: | |
| 32 *result = base::OnStringConversionError::SUBSTITUTE; | |
| 33 return true; | |
| 34 default: | |
| 35 return false; | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 // static | |
| 42 // The "substitution" behavior of this function does not match the | |
| 43 // implementation in base, so we partially duplicate the code from | |
| 44 // icu_string_conversions.cc with the correct error handling setup required | |
| 45 // by the PPAPI interface. | |
| 46 char* CharSetImpl::UTF16ToCharSet(const uint16_t* utf16, | |
| 47 uint32_t utf16_len, | |
| 48 const char* output_char_set, | |
| 49 PP_CharSet_ConversionError on_error, | |
| 50 uint32_t* output_length) { | |
| 51 if (!utf16 || !output_char_set || !output_length) | |
| 52 return NULL; | |
| 53 | |
| 54 *output_length = 0; | |
| 55 | |
| 56 UErrorCode status = U_ZERO_ERROR; | |
| 57 UConverter* converter = ucnv_open(output_char_set, &status); | |
| 58 if (!U_SUCCESS(status)) | |
| 59 return NULL; | |
| 60 | |
| 61 int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(utf16_len, | |
| 62 ucnv_getMaxCharSize(converter)); | |
| 63 | |
| 64 // Setup our error handler. | |
| 65 switch (on_error) { | |
| 66 case PP_CHARSET_CONVERSIONERROR_FAIL: | |
| 67 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_STOP, 0, | |
| 68 NULL, NULL, &status); | |
| 69 break; | |
| 70 case PP_CHARSET_CONVERSIONERROR_SKIP: | |
| 71 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SKIP, 0, | |
| 72 NULL, NULL, &status); | |
| 73 break; | |
| 74 case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE: { | |
| 75 // ICU sets the substitution char for some character sets (like latin1) | |
| 76 // to be the ASCII "substitution character" (26). We want to use '?' | |
| 77 // instead for backwards-compat with Windows behavior. | |
| 78 char subst_chars[32]; | |
| 79 int8_t subst_chars_len = 32; | |
| 80 ucnv_getSubstChars(converter, subst_chars, &subst_chars_len, &status); | |
| 81 if (subst_chars_len == 1 && subst_chars[0] == 26) { | |
| 82 // Override to the question mark character if possible. When using | |
| 83 // setSubstString, the input is a Unicode character. The function will | |
| 84 // try to convert it to the destination character set and fail if that | |
| 85 // can not be converted to the destination character set. | |
| 86 // | |
| 87 // We just ignore any failure. If the dest char set has no | |
| 88 // representation for '?', then we'll just stick to the ICU default | |
| 89 // substitution character. | |
| 90 UErrorCode subst_status = U_ZERO_ERROR; | |
| 91 UChar question_mark = '?'; | |
| 92 ucnv_setSubstString(converter, &question_mark, 1, &subst_status); | |
| 93 } | |
| 94 | |
| 95 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0, | |
| 96 NULL, NULL, &status); | |
| 97 break; | |
| 98 } | |
| 99 default: | |
| 100 return NULL; | |
| 101 } | |
| 102 | |
| 103 // ucnv_fromUChars returns size not including terminating null. | |
| 104 char* encoded = static_cast<char*>( | |
| 105 thunk::GetPPB_Memory_Dev_Thunk()->MemAlloc(encoded_max_length + 1)); | |
| 106 int actual_size = ucnv_fromUChars(converter, encoded, | |
| 107 encoded_max_length, reinterpret_cast<const UChar*>(utf16), utf16_len, | |
| 108 &status); | |
| 109 ucnv_close(converter); | |
| 110 if (!U_SUCCESS(status)) { | |
| 111 thunk::GetPPB_Memory_Dev_Thunk()->MemFree(encoded); | |
| 112 return NULL; | |
| 113 } | |
| 114 encoded[actual_size] = 0; | |
| 115 *output_length = actual_size; | |
| 116 return encoded; | |
| 117 } | |
| 118 | |
| 119 // static | |
| 120 uint16_t* CharSetImpl::CharSetToUTF16(const char* input, | |
| 121 uint32_t input_len, | |
| 122 const char* input_char_set, | |
| 123 PP_CharSet_ConversionError on_error, | |
| 124 uint32_t* output_length) { | |
| 125 if (!input || !input_char_set || !output_length) | |
| 126 return NULL; | |
| 127 | |
| 128 *output_length = 0; | |
| 129 | |
| 130 base::OnStringConversionError::Type base_on_error; | |
| 131 if (!PPToBaseConversionError(on_error, &base_on_error)) | |
| 132 return NULL; // Invalid enum value. | |
| 133 | |
| 134 // We can convert this call to the implementation in base to avoid code | |
| 135 // duplication, although this does introduce an extra copy of the data. | |
| 136 string16 output; | |
| 137 if (!base::CodepageToUTF16(std::string(input, input_len), input_char_set, | |
| 138 base_on_error, &output)) | |
| 139 return NULL; | |
| 140 | |
| 141 uint16_t* ret_buf = static_cast<uint16_t*>( | |
| 142 thunk::GetPPB_Memory_Dev_Thunk()->MemAlloc( | |
| 143 (output.size() + 1) * sizeof(uint16_t))); | |
| 144 if (!ret_buf) | |
| 145 return NULL; | |
| 146 | |
| 147 *output_length = static_cast<uint32_t>(output.size()); | |
| 148 memcpy(ret_buf, output.c_str(), (output.size() + 1) * sizeof(uint16_t)); | |
| 149 return ret_buf; | |
| 150 } | |
| 151 | |
| 152 } // namespace ppapi | |
| OLD | NEW |