Add copy of .ttf font with .eot extension for testing
[wine-gecko.git] / xpcom / base / nsMacUtilsImpl.cpp
blob8eead37b9c67fe018afef7fabeab0d1cdc1a09b9
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is XPCOM utility functions for Mac OS X
17 * The Initial Developer of the Original Code is Google Inc.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
22 * Mark Mentovai <mark@moxienet.com> (Original Author)
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsMacUtilsImpl.h"
40 #include <CoreFoundation/CoreFoundation.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <sys/sysctl.h>
44 #include <mach-o/fat.h>
46 NS_IMPL_ISUPPORTS1(nsMacUtilsImpl, nsIMacUtils)
48 /* readonly attribute boolean isUniversalBinary; */
49 // True when the main executable is a fat file supporting at least
50 // ppc and x86 (universal binary).
51 NS_IMETHODIMP nsMacUtilsImpl::GetIsUniversalBinary(PRBool *aIsUniversalBinary)
53 static PRBool sInitialized = PR_FALSE,
54 sIsUniversalBinary = PR_FALSE;
56 if (sInitialized) {
57 *aIsUniversalBinary = sIsUniversalBinary;
58 return NS_OK;
61 PRBool foundPPC = PR_FALSE,
62 foundX86 = PR_FALSE;
63 CFURLRef executableURL = nsnull;
64 int fd = -1;
66 CFBundleRef mainBundle;
67 if (!(mainBundle = ::CFBundleGetMainBundle()))
68 goto done;
70 if (!(executableURL = ::CFBundleCopyExecutableURL(mainBundle)))
71 goto done;
73 char executablePath[PATH_MAX];
74 if (!::CFURLGetFileSystemRepresentation(executableURL, PR_TRUE,
75 (UInt8*) executablePath,
76 sizeof(executablePath)))
77 goto done;
79 if ((fd = open(executablePath, O_RDONLY)) == -1)
80 goto done;
82 struct fat_header fatHeader;
83 if (read(fd, &fatHeader, sizeof(fatHeader)) != sizeof(fatHeader))
84 goto done;
86 // The fat header is always stored on disk as big-endian.
87 fatHeader.magic = CFSwapInt32BigToHost(fatHeader.magic);
88 fatHeader.nfat_arch = CFSwapInt32BigToHost(fatHeader.nfat_arch);
90 // Main executable is thin.
91 if (fatHeader.magic != FAT_MAGIC)
92 goto done;
94 // Loop over each architecture in the file. We're presently only
95 // interested in 32-bit PPC and x86.
96 for (PRUint32 i = 0 ; i < fatHeader.nfat_arch ; i++) {
97 struct fat_arch fatArch;
98 if (read(fd, &fatArch, sizeof(fatArch)) != sizeof(fatArch))
99 goto done;
101 // This is still part of the fat header, so byte-swap as needed.
102 fatArch.cputype = CFSwapInt32BigToHost(fatArch.cputype);
104 // Don't mask out the ABI bits. This allows identification of ppc64
105 // as distinct from ppc. CPU_TYPE_X86 is preferred to CPU_TYPE_I386
106 // but does not exist prior to the 10.4 headers.
107 if (fatArch.cputype == CPU_TYPE_POWERPC)
108 foundPPC = PR_TRUE;
109 else if (fatArch.cputype == CPU_TYPE_I386)
110 foundX86 = PR_TRUE;
113 if (foundPPC && foundX86)
114 sIsUniversalBinary = PR_TRUE;
116 done:
117 if (fd != -1)
118 close(fd);
119 if (executableURL)
120 ::CFRelease(executableURL);
122 *aIsUniversalBinary = sIsUniversalBinary;
123 sInitialized = PR_TRUE;
125 return NS_OK;
128 /* readonly attribute boolean isTranslated; */
129 // True when running under binary translation (Rosetta).
130 NS_IMETHODIMP nsMacUtilsImpl::GetIsTranslated(PRBool *aIsTranslated)
132 #ifdef __ppc__
133 static PRBool sInitialized = PR_FALSE;
135 // Initialize sIsNative to 1. If the sysctl fails because it doesn't
136 // exist, then translation is not possible, so the process must not be
137 // running translated.
138 static PRInt32 sIsNative = 1;
140 if (!sInitialized) {
141 size_t sz = sizeof(sIsNative);
142 sysctlbyname("sysctl.proc_native", &sIsNative, &sz, NULL, 0);
143 sInitialized = PR_TRUE;
146 *aIsTranslated = !sIsNative;
147 #else
148 // Translation only exists for ppc code. Other architectures aren't
149 // translated.
150 *aIsTranslated = PR_FALSE;
151 #endif
153 return NS_OK;