| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/shared_impl/char_set_impl.h" | 5 #include "ppapi/shared_impl/char_set_impl.h" |
| 6 | 6 |
| 7 #include "base/i18n/icu_string_conversions.h" | 7 #include "base/i18n/icu_string_conversions.h" |
| 8 #include "ppapi/c/ppb_core.h" | 8 #include "ppapi/c/dev/ppb_memory_dev.h" |
| 9 #include "unicode/ucnv.h" | 9 #include "unicode/ucnv.h" |
| 10 #include "unicode/ucnv_cb.h" | 10 #include "unicode/ucnv_cb.h" |
| 11 #include "unicode/ucnv_err.h" | 11 #include "unicode/ucnv_err.h" |
| 12 #include "unicode/ustring.h" | 12 #include "unicode/ustring.h" |
| 13 | 13 |
| 14 namespace ppapi { | 14 namespace ppapi { |
| 15 | 15 |
| 16 namespace { | 16 namespace { |
| 17 | 17 |
| 18 // Converts the given PP error handling behavior to the version in base, | 18 // Converts the given PP error handling behavior to the version in base, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 35 } | 35 } |
| 36 } | 36 } |
| 37 | 37 |
| 38 } // namespace | 38 } // namespace |
| 39 | 39 |
| 40 // static | 40 // static |
| 41 // The "substitution" behavior of this function does not match the | 41 // The "substitution" behavior of this function does not match the |
| 42 // implementation in base, so we partially duplicate the code from | 42 // implementation in base, so we partially duplicate the code from |
| 43 // icu_string_conversions.cc with the correct error handling setup required | 43 // icu_string_conversions.cc with the correct error handling setup required |
| 44 // by the PPAPI interface. | 44 // by the PPAPI interface. |
| 45 char* CharSetImpl::UTF16ToCharSet(const PPB_Core* core, | 45 char* CharSetImpl::UTF16ToCharSet(const PPB_Memory_Dev* memory, |
| 46 const uint16_t* utf16, | 46 const uint16_t* utf16, |
| 47 uint32_t utf16_len, | 47 uint32_t utf16_len, |
| 48 const char* output_char_set, | 48 const char* output_char_set, |
| 49 PP_CharSet_ConversionError on_error, | 49 PP_CharSet_ConversionError on_error, |
| 50 uint32_t* output_length) { | 50 uint32_t* output_length) { |
| 51 if (!core || !utf16 || !output_char_set || !output_length) | 51 if (!memory || !utf16 || !output_char_set || !output_length) |
| 52 return NULL; | 52 return NULL; |
| 53 | 53 |
| 54 *output_length = 0; | 54 *output_length = 0; |
| 55 | 55 |
| 56 UErrorCode status = U_ZERO_ERROR; | 56 UErrorCode status = U_ZERO_ERROR; |
| 57 UConverter* converter = ucnv_open(output_char_set, &status); | 57 UConverter* converter = ucnv_open(output_char_set, &status); |
| 58 if (!U_SUCCESS(status)) | 58 if (!U_SUCCESS(status)) |
| 59 return NULL; | 59 return NULL; |
| 60 | 60 |
| 61 int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(utf16_len, | 61 int encoded_max_length = UCNV_GET_MAX_BYTES_FOR_STRING(utf16_len, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 | 94 |
| 95 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0, | 95 ucnv_setFromUCallBack(converter, UCNV_FROM_U_CALLBACK_SUBSTITUTE, 0, |
| 96 NULL, NULL, &status); | 96 NULL, NULL, &status); |
| 97 break; | 97 break; |
| 98 } | 98 } |
| 99 default: | 99 default: |
| 100 return NULL; | 100 return NULL; |
| 101 } | 101 } |
| 102 | 102 |
| 103 // ucnv_fromUChars returns size not including terminating null. | 103 // ucnv_fromUChars returns size not including terminating null. |
| 104 char* encoded = static_cast<char*>(core->MemAlloc(encoded_max_length + 1)); | 104 char* encoded = static_cast<char*>(memory->MemAlloc(encoded_max_length + 1)); |
| 105 int actual_size = ucnv_fromUChars(converter, encoded, | 105 int actual_size = ucnv_fromUChars(converter, encoded, |
| 106 encoded_max_length, reinterpret_cast<const UChar*>(utf16), utf16_len, | 106 encoded_max_length, reinterpret_cast<const UChar*>(utf16), utf16_len, |
| 107 &status); | 107 &status); |
| 108 ucnv_close(converter); | 108 ucnv_close(converter); |
| 109 if (!U_SUCCESS(status)) { | 109 if (!U_SUCCESS(status)) { |
| 110 core->MemFree(encoded); | 110 memory->MemFree(encoded); |
| 111 return NULL; | 111 return NULL; |
| 112 } | 112 } |
| 113 encoded[actual_size] = 0; | 113 encoded[actual_size] = 0; |
| 114 *output_length = actual_size; | 114 *output_length = actual_size; |
| 115 return encoded; | 115 return encoded; |
| 116 } | 116 } |
| 117 | 117 |
| 118 // static | 118 // static |
| 119 uint16_t* CharSetImpl::CharSetToUTF16(const PPB_Core* core, | 119 uint16_t* CharSetImpl::CharSetToUTF16(const PPB_Memory_Dev* memory, |
| 120 const char* input, | 120 const char* input, |
| 121 uint32_t input_len, | 121 uint32_t input_len, |
| 122 const char* input_char_set, | 122 const char* input_char_set, |
| 123 PP_CharSet_ConversionError on_error, | 123 PP_CharSet_ConversionError on_error, |
| 124 uint32_t* output_length) { | 124 uint32_t* output_length) { |
| 125 if (!core || !input || !input_char_set || !output_length) | 125 if (!memory || !input || !input_char_set || !output_length) |
| 126 return NULL; | 126 return NULL; |
| 127 | 127 |
| 128 *output_length = 0; | 128 *output_length = 0; |
| 129 | 129 |
| 130 base::OnStringConversionError::Type base_on_error; | 130 base::OnStringConversionError::Type base_on_error; |
| 131 if (!PPToBaseConversionError(on_error, &base_on_error)) | 131 if (!PPToBaseConversionError(on_error, &base_on_error)) |
| 132 return NULL; // Invalid enum value. | 132 return NULL; // Invalid enum value. |
| 133 | 133 |
| 134 // We can convert this call to the implementation in base to avoid code | 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. | 135 // duplication, although this does introduce an extra copy of the data. |
| 136 string16 output; | 136 string16 output; |
| 137 if (!base::CodepageToUTF16(std::string(input, input_len), input_char_set, | 137 if (!base::CodepageToUTF16(std::string(input, input_len), input_char_set, |
| 138 base_on_error, &output)) | 138 base_on_error, &output)) |
| 139 return NULL; | 139 return NULL; |
| 140 | 140 |
| 141 uint16_t* ret_buf = static_cast<uint16_t*>( | 141 uint16_t* ret_buf = static_cast<uint16_t*>( |
| 142 core->MemAlloc((output.size() + 1) * sizeof(uint16_t))); | 142 memory->MemAlloc((output.size() + 1) * sizeof(uint16_t))); |
| 143 if (!ret_buf) | 143 if (!ret_buf) |
| 144 return NULL; | 144 return NULL; |
| 145 | 145 |
| 146 *output_length = static_cast<uint32_t>(output.size()); | 146 *output_length = static_cast<uint32_t>(output.size()); |
| 147 memcpy(ret_buf, output.c_str(), (output.size() + 1) * sizeof(uint16_t)); | 147 memcpy(ret_buf, output.c_str(), (output.size() + 1) * sizeof(uint16_t)); |
| 148 return ret_buf; | 148 return ret_buf; |
| 149 } | 149 } |
| 150 | 150 |
| 151 } // namespace ppapi | 151 } // namespace ppapi |
| OLD | NEW |