blob: 390f183c5c6e9e80308031fc8b0636be9f3d26a7 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +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
erikchene606a172016-10-10 11:19:15 -070011#include <cstring>
jbauch555604a2016-04-26 03:13:22 -070012#include <memory>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
erikchene606a172016-10-10 11:19:15 -070014#include <sys/utsname.h>
kthelgasond5472242016-09-09 03:19:48 -070015
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/checks.h"
17#include "rtc_base/logging.h"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/mac_utils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019
20namespace rtc {
21
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022bool ToUtf8(const CFStringRef str16, std::string* str8) {
deadbeef37f5ecf2017-02-27 14:06:41 -080023 if ((nullptr == str16) || (nullptr == str8)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024 return false;
25 }
26 size_t maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str16),
Yves Gerey665174f2018-06-19 15:03:05 +020027 kCFStringEncodingUTF8) +
28 1;
jbauch555604a2016-04-26 03:13:22 -070029 std::unique_ptr<char[]> buffer(new char[maxlen]);
Yves Gerey665174f2018-06-19 15:03:05 +020030 if (!buffer ||
31 !CFStringGetCString(str16, buffer.get(), maxlen, kCFStringEncodingUTF8)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000032 return false;
33 }
34 str8->assign(buffer.get());
35 return true;
36}
37
38bool ToUtf16(const std::string& str8, CFStringRef* str16) {
deadbeef37f5ecf2017-02-27 14:06:41 -080039 if (nullptr == str16) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000040 return false;
41 }
42 *str16 = CFStringCreateWithBytes(kCFAllocatorDefault,
43 reinterpret_cast<const UInt8*>(str8.data()),
Yves Gerey665174f2018-06-19 15:03:05 +020044 str8.length(), kCFStringEncodingUTF8, false);
deadbeef37f5ecf2017-02-27 14:06:41 -080045 return nullptr != *str16;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000046}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000047} // namespace rtc