blob: 03f53d51f91a6b6cb13377c7c4e9bbc3cd7db745 [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#include <sstream>
14
erikchene606a172016-10-10 11:19:15 -070015#include <sys/utsname.h>
kthelgasond5472242016-09-09 03:19:48 -070016
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/checks.h"
18#include "rtc_base/logging.h"
19#include "rtc_base/macutils.h"
20#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000021
22namespace rtc {
23
24///////////////////////////////////////////////////////////////////////////////
25
26bool ToUtf8(const CFStringRef str16, std::string* str8) {
deadbeef37f5ecf2017-02-27 14:06:41 -080027 if ((nullptr == str16) || (nullptr == str8)) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000028 return false;
29 }
30 size_t maxlen = CFStringGetMaximumSizeForEncoding(CFStringGetLength(str16),
31 kCFStringEncodingUTF8) + 1;
jbauch555604a2016-04-26 03:13:22 -070032 std::unique_ptr<char[]> buffer(new char[maxlen]);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000033 if (!buffer || !CFStringGetCString(str16, buffer.get(), maxlen,
34 kCFStringEncodingUTF8)) {
35 return false;
36 }
37 str8->assign(buffer.get());
38 return true;
39}
40
41bool ToUtf16(const std::string& str8, CFStringRef* str16) {
deadbeef37f5ecf2017-02-27 14:06:41 -080042 if (nullptr == str16) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000043 return false;
44 }
45 *str16 = CFStringCreateWithBytes(kCFAllocatorDefault,
46 reinterpret_cast<const UInt8*>(str8.data()),
47 str8.length(), kCFStringEncodingUTF8,
48 false);
deadbeef37f5ecf2017-02-27 14:06:41 -080049 return nullptr != *str16;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050}
51
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000052void DecodeFourChar(UInt32 fc, std::string* out) {
53 std::stringstream ss;
54 ss << '\'';
55 bool printable = true;
56 for (int i = 3; i >= 0; --i) {
57 char ch = (fc >> (8 * i)) & 0xFF;
58 if (isprint(static_cast<unsigned char>(ch))) {
59 ss << ch;
60 } else {
61 printable = false;
62 break;
63 }
64 }
65 if (printable) {
66 ss << '\'';
67 } else {
68 ss.str("");
69 ss << "0x" << std::hex << fc;
70 }
71 out->append(ss.str());
72}
73
thakis0cf208a2016-07-06 10:46:41 -070074static bool GetOSVersion(int* major, int* minor, int* bugfix) {
kwibergee89e782017-08-09 17:22:01 -070075 RTC_DCHECK(major);
76 RTC_DCHECK(minor);
77 RTC_DCHECK(bugfix);
erikchene606a172016-10-10 11:19:15 -070078 struct utsname uname_info;
79 if (uname(&uname_info) != 0)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000080 return false;
erikchene606a172016-10-10 11:19:15 -070081
82 if (strcmp(uname_info.sysname, "Darwin") != 0)
83 return false;
84 *major = 10;
85
86 // The market version of macOS is always 4 lower than the internal version.
87 int minor_version = atoi(uname_info.release);
88 RTC_CHECK(minor_version >= 6);
89 *minor = minor_version - 4;
90
91 const char* dot = ::strchr(uname_info.release, '.');
92 if (!dot)
93 return false;
94 *bugfix = atoi(dot + 1);
95 return true;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096}
97
98MacOSVersionName GetOSVersionName() {
99 int major = 0, minor = 0, bugfix = 0;
100 if (!GetOSVersion(&major, &minor, &bugfix)) {
101 return kMacOSUnknown;
102 }
103 if (major > 10) {
104 return kMacOSNewer;
105 }
106 if ((major < 10) || (minor < 3)) {
107 return kMacOSOlder;
108 }
109 switch (minor) {
110 case 3:
111 return kMacOSPanther;
112 case 4:
113 return kMacOSTiger;
114 case 5:
115 return kMacOSLeopard;
116 case 6:
117 return kMacOSSnowLeopard;
118 case 7:
119 return kMacOSLion;
120 case 8:
121 return kMacOSMountainLion;
122 case 9:
123 return kMacOSMavericks;
124 }
125 return kMacOSNewer;
126}
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000127} // namespace rtc