| 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/ppb_char_set_shared.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* PPB_CharSet_Shared::UTF16ToCharSet( | |
| 47 const uint16_t* utf16, | |
| 48 uint32_t utf16_len, | |
| 49 const char* output_char_set, | |
| 50 PP_CharSet_ConversionError on_error, | |
| 51 uint32_t* output_length) { | |
| 52 if (!utf16 || !output_char_set || !output_length) | |
| 53 return NULL; | |
| 54 | |
| 55 *output_length = 0; | |
| 56 | |
| 57 UErrorCode status = U_ZERO_ERROR; | |
| 58 UConverter* converter = ucnv_open(output_char_set, &status); | |
| 59 if (!U_SUCCESS(status)) | |
| 60 return NULL; | |
| 61 | |
| 62 int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(utf16_len, | |
| 63 ucnv_getMaxCharSize(converter)); | |
| 64 | |
| 65 // Setup our error handler. | |
| 66 switch (on_error) { | |
| 67 case PP_CHARSET_CONVERSIONERROR_FAIL: | |
| 68 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_STOP, 0, | |
| 69 NULL, NULL, &status); | |
| 70 break; | |
| 71 case PP_CHARSET_CONVERSIONERROR_SKIP: | |
| 72 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SKIP, 0, | |
| 73 NULL, NULL, &status); | |
| 74 break; | |
| 75 case PP_CHARSET_CONVERSIONERROR_SUBSTITUTE: { | |
| 76 // ICU sets the substitution char for some character sets (like latin1) | |
| 77 // to be the ASCII "substitution character" (26). We want to use '?' | |
| 78 // instead for backwards-compat with Windows behavior. | |
| 79 char subst_chars[32]; | |
| 80 int8_t subst_chars_len = 32; | |
| 81 ucnv_getSubstChars(converter, subst_chars, &subst_chars_len, &status); | |
| 82 if (subst_chars_len == 1 && subst_chars[0] == 26) { | |
| 83 // Override to the question mark character if possible. When using | |
| 84 // setSubstString, the input is a Unicode character. The function will | |
| 85 // try to convert it to the destination character set and fail if that | |
| 86 // can not be converted to the destination character set. | |
| 87 // | |
| 88 // We just ignore any failure. If the dest char set has no | |
| 89 // representation for '?', then we'll just stick to the ICU default | |
| 90 // substitution character. | |
| 91 UErrorCode subst_status = U_ZERO_ERROR; | |
| 92 UChar question_mark = '?'; | |
| 93 ucnv_setSubstString(converter, &question_mark, 1, &subst_status); | |
| 94 } | |
| 95 | |
| 96 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0, | |
| 97 NULL, NULL, &status); | |
| 98 break; | |
| 99 } | |
| 100 default: | |
| 101 return NULL; | |
| 102 } | |
| 103 | |
| 104 // ucnv_fromUChars returns size not including terminating null. | |
| 105 char* encoded = static_cast<char*>( | |
| 106 thunk::GetPPB_Memory_Dev_Thunk()->MemAlloc(encoded_max_length + 1)); | |
| 107 int actual_size = ucnv_fromUChars(converter, encoded, | |
| 108 encoded_max_length, reinterpret_cast<const UChar*>(utf16), utf16_len, | |
| 109 &status); | |
| 110 ucnv_close(converter); | |
| 111 if (!U_SUCCESS(status)) { | |
| 112 thunk::GetPPB_Memory_Dev_Thunk()->MemFree(encoded); | |
| 113 return NULL; | |
| 114 } | |
| 115 encoded[actual_size] = 0; | |
| 116 *output_length = actual_size; | |
| 117 return encoded; | |
| 118 } | |
| 119 | |
| 120 // static | |
| 121 uint16_t* PPB_CharSet_Shared::CharSetToUTF16( | |
| 122 const char* input, | |
| 123 uint32_t input_len, | |
| 124 const char* input_char_set, | |
| 125 PP_CharSet_ConversionError on_error, | |
| 126 uint32_t* output_length) { | |
| 127 if (!input || !input_char_set || !output_length) | |
| 128 return NULL; | |
| 129 | |
| 130 *output_length = 0; | |
| 131 | |
| 132 base::OnStringConversionError::Type base_on_error; | |
| 133 if (!PPToBaseConversionError(on_error, &base_on_error)) | |
| 134 return NULL; // Invalid enum value. | |
| 135 | |
| 136 // We can convert this call to the implementation in base to avoid code | |
| 137 // duplication, although this does introduce an extra copy of the data. | |
| 138 string16 output; | |
| 139 if (!base::CodepageToUTF16(std::string(input, input_len), input_char_set, | |
| 140 base_on_error, &output)) | |
| 141 return NULL; | |
| 142 | |
| 143 uint16_t* ret_buf = static_cast<uint16_t*>( | |
| 144 thunk::GetPPB_Memory_Dev_Thunk()->MemAlloc( | |
| 145 (output.size() + 1) * sizeof(uint16_t))); | |
| 146 if (!ret_buf) | |
| 147 return NULL; | |
| 148 | |
| 149 *output_length = static_cast<uint32_t>(output.size()); | |
| 150 memcpy(ret_buf, output.c_str(), (output.size() + 1) * sizeof(uint16_t)); | |
| 151 return ret_buf; | |
| 152 } | |
| 153 | |
| 154 } // namespace ppapi | |
| OLD | NEW |