henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +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 | |
erikchen | e606a17 | 2016-10-10 11:19:15 -0700 | [diff] [blame] | 11 | #include <cstring> |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 12 | #include <memory> |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 13 | |
erikchen | e606a17 | 2016-10-10 11:19:15 -0700 | [diff] [blame] | 14 | #include <sys/utsname.h> |
kthelgason | d547224 | 2016-09-09 03:19:48 -0700 | [diff] [blame] | 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "rtc_base/checks.h" |
| 17 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 18 | #include "rtc_base/mac_utils.h" |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 19 | |
| 20 | namespace rtc { |
| 21 | |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 22 | bool ToUtf8(const CFStringRef str16, std::string* str8) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 23 | if ((nullptr == str16) || (nullptr == str8)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 24 | return false; |
| 25 | } |
| 26 | size_t maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str16), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 27 | kCFStringEncodingUTF8) + |
| 28 | 1; |
jbauch | 555604a | 2016-04-26 03:13:22 -0700 | [diff] [blame] | 29 | std::unique_ptr<char[]> buffer(new char[maxlen]); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 30 | if (!buffer || |
| 31 | !CFStringGetCString(str16, buffer.get(), maxlen, kCFStringEncodingUTF8)) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 32 | return false; |
| 33 | } |
| 34 | str8->assign(buffer.get()); |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | bool ToUtf16(const std::string& str8, CFStringRef* str16) { |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 39 | if (nullptr == str16) { |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 40 | return false; |
| 41 | } |
| 42 | *str16 = CFStringCreateWithBytes(kCFAllocatorDefault, |
| 43 | reinterpret_cast<const UInt8*>(str8.data()), |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 44 | str8.length(), kCFStringEncodingUTF8, false); |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 45 | return nullptr != *str16; |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 46 | } |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 47 | } // namespace rtc |