blob: d2428b4e0cf8f19598e5e8537454d5154f07528c [file] [log] [blame]
Niels Mollere21f3f52019-08-20 09:58:21 +00001/*
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
21namespace rtc {
22
23bool 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
39bool 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