Niels Moller | e21f3f5 | 2019-08-20 09:58:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "rtc_base/mac_utils.h" |
| 12 | |
| 13 | #include <sys/utsname.h> |
| 14 | |
| 15 | #include <cstring> |
| 16 | #include <memory> |
| 17 | |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/logging.h" |
| 20 | |
| 21 | namespace rtc { |
| 22 | |
| 23 | bool ToUtf8(const CFStringRef str16, std::string* str8) { |
| 24 | if ((nullptr == str16) || (nullptr == str8)) { |
| 25 | return false; |
| 26 | } |
| 27 | size_t maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str16), |
| 28 | kCFStringEncodingUTF8) + |
| 29 | 1; |
| 30 | std::unique_ptr<char[]> buffer(new char[maxlen]); |
| 31 | if (!buffer || |
| 32 | !CFStringGetCString(str16, buffer.get(), maxlen, kCFStringEncodingUTF8)) { |
| 33 | return false; |
| 34 | } |
| 35 | str8->assign(buffer.get()); |
| 36 | return true; |
| 37 | } |
| 38 | |
| 39 | bool ToUtf16(const std::string& str8, CFStringRef* str16) { |
| 40 | if (nullptr == str16) { |
| 41 | return false; |
| 42 | } |
| 43 | *str16 = CFStringCreateWithBytes(kCFAllocatorDefault, |
| 44 | reinterpret_cast<const UInt8*>(str8.data()), |
| 45 | str8.length(), kCFStringEncodingUTF8, false); |
| 46 | return nullptr != *str16; |
| 47 | } |
| 48 | } // namespace rtc |