Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 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/sys_string_conversions.h" |
michael@0 | 6 | |
michael@0 | 7 | #import <Foundation/Foundation.h> |
michael@0 | 8 | |
michael@0 | 9 | #include <vector> |
michael@0 | 10 | |
michael@0 | 11 | #include "base/scoped_cftyperef.h" |
michael@0 | 12 | #include "base/string_piece.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace base { |
michael@0 | 15 | |
michael@0 | 16 | namespace { |
michael@0 | 17 | |
michael@0 | 18 | // Convert the supplied CFString into the specified encoding, and return it as |
michael@0 | 19 | // an STL string of the template type. Returns an empty string on failure. |
michael@0 | 20 | // |
michael@0 | 21 | // Do not assert in this function since it is used by the asssertion code! |
michael@0 | 22 | template<typename StringType> |
michael@0 | 23 | static StringType CFStringToSTLStringWithEncodingT(CFStringRef cfstring, |
michael@0 | 24 | CFStringEncoding encoding) { |
michael@0 | 25 | CFIndex length = CFStringGetLength(cfstring); |
michael@0 | 26 | if (length == 0) |
michael@0 | 27 | return StringType(); |
michael@0 | 28 | |
michael@0 | 29 | CFRange whole_string = CFRangeMake(0, length); |
michael@0 | 30 | CFIndex out_size; |
michael@0 | 31 | CFIndex converted = CFStringGetBytes(cfstring, |
michael@0 | 32 | whole_string, |
michael@0 | 33 | encoding, |
michael@0 | 34 | 0, // lossByte |
michael@0 | 35 | false, // isExternalRepresentation |
michael@0 | 36 | NULL, // buffer |
michael@0 | 37 | 0, // maxBufLen |
michael@0 | 38 | &out_size); |
michael@0 | 39 | if (converted == 0 || out_size == 0) |
michael@0 | 40 | return StringType(); |
michael@0 | 41 | |
michael@0 | 42 | // out_size is the number of UInt8-sized units needed in the destination. |
michael@0 | 43 | // A buffer allocated as UInt8 units might not be properly aligned to |
michael@0 | 44 | // contain elements of StringType::value_type. Use a container for the |
michael@0 | 45 | // proper value_type, and convert out_size by figuring the number of |
michael@0 | 46 | // value_type elements per UInt8. Leave room for a NUL terminator. |
michael@0 | 47 | typename StringType::size_type elements = |
michael@0 | 48 | out_size * sizeof(UInt8) / sizeof(typename StringType::value_type) + 1; |
michael@0 | 49 | |
michael@0 | 50 | std::vector<typename StringType::value_type> out_buffer(elements); |
michael@0 | 51 | converted = CFStringGetBytes(cfstring, |
michael@0 | 52 | whole_string, |
michael@0 | 53 | encoding, |
michael@0 | 54 | 0, // lossByte |
michael@0 | 55 | false, // isExternalRepresentation |
michael@0 | 56 | reinterpret_cast<UInt8*>(&out_buffer[0]), |
michael@0 | 57 | out_size, |
michael@0 | 58 | NULL); // usedBufLen |
michael@0 | 59 | if (converted == 0) |
michael@0 | 60 | return StringType(); |
michael@0 | 61 | |
michael@0 | 62 | out_buffer[elements - 1] = '\0'; |
michael@0 | 63 | return StringType(&out_buffer[0], elements - 1); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | // Given an STL string |in| with an encoding specified by |in_encoding|, |
michael@0 | 67 | // convert it to |out_encoding| and return it as an STL string of the |
michael@0 | 68 | // |OutStringType| template type. Returns an empty string on failure. |
michael@0 | 69 | // |
michael@0 | 70 | // Do not assert in this function since it is used by the asssertion code! |
michael@0 | 71 | template<typename InStringType, typename OutStringType> |
michael@0 | 72 | static OutStringType STLStringToSTLStringWithEncodingsT( |
michael@0 | 73 | const InStringType& in, |
michael@0 | 74 | CFStringEncoding in_encoding, |
michael@0 | 75 | CFStringEncoding out_encoding) { |
michael@0 | 76 | typename InStringType::size_type in_length = in.length(); |
michael@0 | 77 | if (in_length == 0) |
michael@0 | 78 | return OutStringType(); |
michael@0 | 79 | |
michael@0 | 80 | scoped_cftyperef<CFStringRef> cfstring( |
michael@0 | 81 | CFStringCreateWithBytesNoCopy(NULL, |
michael@0 | 82 | reinterpret_cast<const UInt8*>(in.data()), |
michael@0 | 83 | in_length * |
michael@0 | 84 | sizeof(typename InStringType::value_type), |
michael@0 | 85 | in_encoding, |
michael@0 | 86 | false, |
michael@0 | 87 | kCFAllocatorNull)); |
michael@0 | 88 | if (!cfstring) |
michael@0 | 89 | return OutStringType(); |
michael@0 | 90 | |
michael@0 | 91 | return CFStringToSTLStringWithEncodingT<OutStringType>(cfstring, |
michael@0 | 92 | out_encoding); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | // Given an STL string |in| with an encoding specified by |in_encoding|, |
michael@0 | 96 | // return it as a CFStringRef. Returns NULL on failure. |
michael@0 | 97 | template<typename StringType> |
michael@0 | 98 | static CFStringRef STLStringToCFStringWithEncodingsT( |
michael@0 | 99 | const StringType& in, |
michael@0 | 100 | CFStringEncoding in_encoding) { |
michael@0 | 101 | typename StringType::size_type in_length = in.length(); |
michael@0 | 102 | if (in_length == 0) |
michael@0 | 103 | return CFSTR(""); |
michael@0 | 104 | |
michael@0 | 105 | return CFStringCreateWithBytes(kCFAllocatorDefault, |
michael@0 | 106 | reinterpret_cast<const UInt8*>(in.data()), |
michael@0 | 107 | in_length * |
michael@0 | 108 | sizeof(typename StringType::value_type), |
michael@0 | 109 | in_encoding, |
michael@0 | 110 | false); |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | // Specify the byte ordering explicitly, otherwise CFString will be confused |
michael@0 | 114 | // when strings don't carry BOMs, as they typically won't. |
michael@0 | 115 | static const CFStringEncoding kNarrowStringEncoding = kCFStringEncodingUTF8; |
michael@0 | 116 | |
michael@0 | 117 | #ifdef __BIG_ENDIAN__ |
michael@0 | 118 | static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32BE; |
michael@0 | 119 | #elif defined(__LITTLE_ENDIAN__) |
michael@0 | 120 | static const CFStringEncoding kWideStringEncoding = kCFStringEncodingUTF32LE; |
michael@0 | 121 | #endif // __LITTLE_ENDIAN__ |
michael@0 | 122 | |
michael@0 | 123 | } // namespace |
michael@0 | 124 | |
michael@0 | 125 | // Do not assert in this function since it is used by the asssertion code! |
michael@0 | 126 | std::string SysWideToUTF8(const std::wstring& wide) { |
michael@0 | 127 | return STLStringToSTLStringWithEncodingsT<std::wstring, std::string>( |
michael@0 | 128 | wide, kWideStringEncoding, kNarrowStringEncoding); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | // Do not assert in this function since it is used by the asssertion code! |
michael@0 | 132 | std::wstring SysUTF8ToWide(const StringPiece& utf8) { |
michael@0 | 133 | return STLStringToSTLStringWithEncodingsT<StringPiece, std::wstring>( |
michael@0 | 134 | utf8, kNarrowStringEncoding, kWideStringEncoding); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | std::string SysWideToNativeMB(const std::wstring& wide) { |
michael@0 | 138 | return SysWideToUTF8(wide); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | std::wstring SysNativeMBToWide(const StringPiece& native_mb) { |
michael@0 | 142 | return SysUTF8ToWide(native_mb); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | } // namespace base |