author | nemo |
Thu, 03 Mar 2016 15:41:53 -0500 | |
changeset 11588 | 7c8fd2f66e9b |
parent 10017 | de822cd3df3a |
child 12213 | bb5522e88ab2 |
permissions | -rw-r--r-- |
7768 | 1 |
/* |
2 |
* Mac OS X support routines for PhysicsFS. |
|
3 |
* |
|
4 |
* Please see the file LICENSE.txt in the source's root directory. |
|
5 |
* |
|
6 |
* This file written by Ryan C. Gordon. |
|
7 |
*/ |
|
8 |
||
9 |
#define __PHYSICSFS_INTERNAL__ |
|
10 |
#include "physfs_platforms.h" |
|
11 |
||
12 |
#ifdef PHYSFS_PLATFORM_MACOSX |
|
13 |
||
14 |
#include <CoreFoundation/CoreFoundation.h> |
|
15 |
||
16 |
#if !defined(PHYSFS_NO_CDROM_SUPPORT) |
|
17 |
#include <Carbon/Carbon.h> /* !!! FIXME */ |
|
18 |
#include <IOKit/storage/IOMedia.h> |
|
19 |
#include <IOKit/storage/IOCDMedia.h> |
|
20 |
#include <IOKit/storage/IODVDMedia.h> |
|
21 |
#include <sys/mount.h> |
|
22 |
#endif |
|
23 |
||
24 |
/* Seems to get defined in some system header... */ |
|
25 |
#ifdef Free |
|
26 |
#undef Free |
|
27 |
#endif |
|
28 |
||
29 |
#include "physfs_internal.h" |
|
30 |
||
31 |
||
8928
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
32 |
#if defined(__APPLE__) |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
33 |
#if defined(TARGET_OS_MAC) && MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4 |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
34 |
/* __eprintf shouldn't have been made visible from libstdc++, or anywhere, but |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
35 |
on Mac OS X 10.4 it was defined in libstdc++.6.0.3.dylib; so on that platform |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
36 |
we have to keep defining it to keep binary compatibility. |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
37 |
We can't just put the libgcc version in the export list, because that |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
38 |
doesn't work; once a symbol is marked as hidden, it stays that way. */ |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
39 |
|
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
40 |
void __eprintf (const char *string, const char *expression, |
10017 | 41 |
unsigned int line, const char *filename) |
8928
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
42 |
{ |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
43 |
fprintf(stderr, string, expression, line, filename); |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
44 |
fflush(stderr); |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
45 |
abort(); |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
46 |
} |
9183
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
47 |
|
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
48 |
|
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
49 |
/* apparently libssp is missing from 10.4 SDK |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
50 |
code from http://wiki.osdev.org/GCC_Stack_Smashing_Protector */ |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
51 |
void * __stack_chk_guard = NULL; |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
52 |
|
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
53 |
void __stack_chk_guard_setup() |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
54 |
{ |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
55 |
unsigned char * p; |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
56 |
p = (unsigned char *) &__stack_chk_guard; |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
57 |
|
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
58 |
/* If you have the ability to generate random numbers in your kernel then use them, |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
59 |
otherwise for 32-bit code: *p = 0x00000aff; */ |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
60 |
*p = random(); |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
61 |
} |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
62 |
|
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
63 |
void __attribute__((noreturn)) __stack_chk_fail() |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
64 |
{ |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
65 |
/* put your panic function or similar in here */ |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
66 |
unsigned char * vid = (unsigned char *)0xB8000; |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
67 |
vid[1] = 7; |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
68 |
for(;;) |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
69 |
vid[0]++; |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
70 |
} |
4cd7c7aafea7
libssp (required by stack-protector) is not available on 10.4
koda
parents:
8928
diff
changeset
|
71 |
|
8928
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
72 |
#endif |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
73 |
#endif /* __APPLE__ */ |
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
74 |
|
10a79d89ca31
expose a hidden function that prevented linking on osx 10.4 when compiling physfs statically
koda
parents:
8524
diff
changeset
|
75 |
|
7768 | 76 |
/* Wrap PHYSFS_Allocator in a CFAllocator... */ |
77 |
static CFAllocatorRef cfallocator = NULL; |
|
78 |
||
79 |
static CFStringRef cfallocDesc(const void *info) |
|
80 |
{ |
|
81 |
return CFStringCreateWithCString(cfallocator, "PhysicsFS", |
|
82 |
kCFStringEncodingASCII); |
|
83 |
} /* cfallocDesc */ |
|
84 |
||
85 |
||
86 |
static void *cfallocMalloc(CFIndex allocSize, CFOptionFlags hint, void *info) |
|
87 |
{ |
|
88 |
return allocator.Malloc(allocSize); |
|
89 |
} /* cfallocMalloc */ |
|
90 |
||
91 |
||
92 |
static void cfallocFree(void *ptr, void *info) |
|
93 |
{ |
|
94 |
allocator.Free(ptr); |
|
95 |
} /* cfallocFree */ |
|
96 |
||
97 |
||
98 |
static void *cfallocRealloc(void *ptr, CFIndex newsize, |
|
99 |
CFOptionFlags hint, void *info) |
|
100 |
{ |
|
101 |
if ((ptr == NULL) || (newsize <= 0)) |
|
102 |
return NULL; /* ADC docs say you should always return NULL here. */ |
|
103 |
return allocator.Realloc(ptr, newsize); |
|
104 |
} /* cfallocRealloc */ |
|
105 |
||
106 |
||
107 |
int __PHYSFS_platformInit(void) |
|
108 |
{ |
|
109 |
/* set up a CFAllocator, so Carbon can use the physfs allocator, too. */ |
|
110 |
CFAllocatorContext ctx; |
|
111 |
memset(&ctx, '\0', sizeof (ctx)); |
|
112 |
ctx.copyDescription = cfallocDesc; |
|
113 |
ctx.allocate = cfallocMalloc; |
|
114 |
ctx.reallocate = cfallocRealloc; |
|
115 |
ctx.deallocate = cfallocFree; |
|
116 |
cfallocator = CFAllocatorCreate(kCFAllocatorUseContext, &ctx); |
|
117 |
BAIL_IF_MACRO(!cfallocator, PHYSFS_ERR_OUT_OF_MEMORY, 0); |
|
118 |
return 1; /* success. */ |
|
119 |
} /* __PHYSFS_platformInit */ |
|
120 |
||
121 |
||
122 |
int __PHYSFS_platformDeinit(void) |
|
123 |
{ |
|
124 |
CFRelease(cfallocator); |
|
125 |
cfallocator = NULL; |
|
126 |
return 1; /* always succeed. */ |
|
127 |
} /* __PHYSFS_platformDeinit */ |
|
128 |
||
129 |
||
130 |
||
131 |
/* CD-ROM detection code... */ |
|
132 |
||
133 |
/* |
|
134 |
* Code based on sample from Apple Developer Connection: |
|
135 |
* http://developer.apple.com/samplecode/Sample_Code/Devices_and_Hardware/Disks/VolumeToBSDNode/VolumeToBSDNode.c.htm |
|
136 |
*/ |
|
137 |
||
138 |
#if !defined(PHYSFS_NO_CDROM_SUPPORT) |
|
139 |
||
140 |
static int darwinIsWholeMedia(io_service_t service) |
|
141 |
{ |
|
142 |
int retval = 0; |
|
143 |
CFTypeRef wholeMedia; |
|
144 |
||
145 |
if (!IOObjectConformsTo(service, kIOMediaClass)) |
|
146 |
return 0; |
|
10017 | 147 |
|
7768 | 148 |
wholeMedia = IORegistryEntryCreateCFProperty(service, |
149 |
CFSTR(kIOMediaWholeKey), |
|
150 |
cfallocator, 0); |
|
151 |
if (wholeMedia == NULL) |
|
152 |
return 0; |
|
153 |
||
154 |
retval = CFBooleanGetValue(wholeMedia); |
|
155 |
CFRelease(wholeMedia); |
|
156 |
||
157 |
return retval; |
|
158 |
} /* darwinIsWholeMedia */ |
|
159 |
||
160 |
||
161 |
static int darwinIsMountedDisc(char *bsdName, mach_port_t masterPort) |
|
162 |
{ |
|
163 |
int retval = 0; |
|
164 |
CFMutableDictionaryRef matchingDict; |
|
165 |
kern_return_t rc; |
|
166 |
io_iterator_t iter; |
|
167 |
io_service_t service; |
|
168 |
||
169 |
if ((matchingDict = IOBSDNameMatching(masterPort, 0, bsdName)) == NULL) |
|
170 |
return 0; |
|
171 |
||
172 |
rc = IOServiceGetMatchingServices(masterPort, matchingDict, &iter); |
|
173 |
if ((rc != KERN_SUCCESS) || (!iter)) |
|
174 |
return 0; |
|
175 |
||
176 |
service = IOIteratorNext(iter); |
|
177 |
IOObjectRelease(iter); |
|
178 |
if (!service) |
|
179 |
return 0; |
|
180 |
||
181 |
rc = IORegistryEntryCreateIterator(service, kIOServicePlane, |
|
182 |
kIORegistryIterateRecursively | kIORegistryIterateParents, &iter); |
|
10017 | 183 |
|
7768 | 184 |
if (!iter) |
185 |
return 0; |
|
186 |
||
187 |
if (rc != KERN_SUCCESS) |
|
188 |
{ |
|
189 |
IOObjectRelease(iter); |
|
190 |
return 0; |
|
191 |
} /* if */ |
|
192 |
||
193 |
IOObjectRetain(service); /* add an extra object reference... */ |
|
194 |
||
195 |
do |
|
196 |
{ |
|
197 |
if (darwinIsWholeMedia(service)) |
|
198 |
{ |
|
199 |
if ( (IOObjectConformsTo(service, kIOCDMediaClass)) || |
|
200 |
(IOObjectConformsTo(service, kIODVDMediaClass)) ) |
|
201 |
{ |
|
202 |
retval = 1; |
|
203 |
} /* if */ |
|
204 |
} /* if */ |
|
205 |
IOObjectRelease(service); |
|
206 |
} while ((service = IOIteratorNext(iter)) && (!retval)); |
|
10017 | 207 |
|
7768 | 208 |
IOObjectRelease(iter); |
209 |
IOObjectRelease(service); |
|
210 |
||
211 |
return retval; |
|
212 |
} /* darwinIsMountedDisc */ |
|
213 |
||
214 |
#endif /* !defined(PHYSFS_NO_CDROM_SUPPORT) */ |
|
215 |
||
216 |
||
217 |
void __PHYSFS_platformDetectAvailableCDs(PHYSFS_StringCallback cb, void *data) |
|
218 |
{ |
|
219 |
#if !defined(PHYSFS_NO_CDROM_SUPPORT) |
|
220 |
const char *devPrefix = "/dev/"; |
|
221 |
const int prefixLen = strlen(devPrefix); |
|
222 |
mach_port_t masterPort = 0; |
|
223 |
struct statfs *mntbufp; |
|
224 |
int i, mounts; |
|
225 |
||
226 |
if (IOMasterPort(MACH_PORT_NULL, &masterPort) != KERN_SUCCESS) |
|
227 |
BAIL_MACRO(PHYSFS_ERR_OS_ERROR, ) /*return void*/; |
|
228 |
||
229 |
mounts = getmntinfo(&mntbufp, MNT_WAIT); /* NOT THREAD SAFE! */ |
|
230 |
for (i = 0; i < mounts; i++) |
|
231 |
{ |
|
232 |
char *dev = mntbufp[i].f_mntfromname; |
|
233 |
char *mnt = mntbufp[i].f_mntonname; |
|
234 |
if (strncmp(dev, devPrefix, prefixLen) != 0) /* a virtual device? */ |
|
235 |
continue; |
|
236 |
||
237 |
dev += prefixLen; |
|
238 |
if (darwinIsMountedDisc(dev, masterPort)) |
|
239 |
cb(data, mnt); |
|
240 |
} /* for */ |
|
241 |
#endif /* !defined(PHYSFS_NO_CDROM_SUPPORT) */ |
|
242 |
} /* __PHYSFS_platformDetectAvailableCDs */ |
|
243 |
||
244 |
||
245 |
static char *convertCFString(CFStringRef cfstr) |
|
246 |
{ |
|
247 |
CFIndex len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstr), |
|
248 |
kCFStringEncodingUTF8) + 1; |
|
249 |
char *retval = (char *) allocator.Malloc(len); |
|
250 |
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
251 |
||
252 |
if (CFStringGetCString(cfstr, retval, len, kCFStringEncodingUTF8)) |
|
253 |
{ |
|
254 |
/* shrink overallocated buffer if possible... */ |
|
255 |
CFIndex newlen = strlen(retval) + 1; |
|
256 |
if (newlen < len) |
|
257 |
{ |
|
258 |
void *ptr = allocator.Realloc(retval, newlen); |
|
259 |
if (ptr != NULL) |
|
260 |
retval = (char *) ptr; |
|
261 |
} /* if */ |
|
262 |
} /* if */ |
|
263 |
||
264 |
else /* probably shouldn't fail, but just in case... */ |
|
265 |
{ |
|
266 |
allocator.Free(retval); |
|
267 |
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
268 |
} /* else */ |
|
269 |
||
270 |
return retval; |
|
271 |
} /* convertCFString */ |
|
272 |
||
273 |
||
274 |
char *__PHYSFS_platformCalcBaseDir(const char *argv0) |
|
275 |
{ |
|
276 |
CFURLRef cfurl = NULL; |
|
277 |
CFStringRef cfstr = NULL; |
|
278 |
CFMutableStringRef cfmutstr = NULL; |
|
279 |
char *retval = NULL; |
|
280 |
||
281 |
cfurl = CFBundleCopyBundleURL(CFBundleGetMainBundle()); |
|
282 |
BAIL_IF_MACRO(cfurl == NULL, PHYSFS_ERR_OS_ERROR, NULL); |
|
283 |
cfstr = CFURLCopyFileSystemPath(cfurl, kCFURLPOSIXPathStyle); |
|
284 |
CFRelease(cfurl); |
|
285 |
BAIL_IF_MACRO(!cfstr, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
286 |
cfmutstr = CFStringCreateMutableCopy(cfallocator, 0, cfstr); |
|
287 |
CFRelease(cfstr); |
|
288 |
BAIL_IF_MACRO(!cfmutstr, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
289 |
CFStringAppendCString(cfmutstr, "/", kCFStringEncodingUTF8); |
|
290 |
retval = convertCFString(cfmutstr); |
|
291 |
CFRelease(cfmutstr); |
|
292 |
||
293 |
return retval; /* whew. */ |
|
294 |
} /* __PHYSFS_platformCalcBaseDir */ |
|
295 |
||
296 |
||
297 |
char *__PHYSFS_platformCalcPrefDir(const char *org, const char *app) |
|
298 |
{ |
|
299 |
/* !!! FIXME: there's a real API to determine this */ |
|
300 |
const char *userdir = __PHYSFS_getUserDir(); |
|
301 |
const char *append = "Library/Application Support/"; |
|
302 |
const size_t len = strlen(userdir) + strlen(append) + strlen(app) + 2; |
|
303 |
char *retval = allocator.Malloc(len); |
|
304 |
BAIL_IF_MACRO(!retval, PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
305 |
snprintf(retval, len, "%s%s%s/", userdir, append, app); |
|
306 |
return retval; |
|
307 |
} /* __PHYSFS_platformCalcPrefDir */ |
|
308 |
||
309 |
||
310 |
/* Platform allocator uses default CFAllocator at PHYSFS_init() time. */ |
|
311 |
||
312 |
static CFAllocatorRef cfallocdef = NULL; |
|
313 |
||
314 |
static int macosxAllocatorInit(void) |
|
315 |
{ |
|
316 |
int retval = 0; |
|
317 |
cfallocdef = CFAllocatorGetDefault(); |
|
318 |
retval = (cfallocdef != NULL); |
|
319 |
if (retval) |
|
320 |
CFRetain(cfallocdef); |
|
321 |
return retval; |
|
322 |
} /* macosxAllocatorInit */ |
|
323 |
||
324 |
||
325 |
static void macosxAllocatorDeinit(void) |
|
326 |
{ |
|
327 |
if (cfallocdef != NULL) |
|
328 |
{ |
|
329 |
CFRelease(cfallocdef); |
|
330 |
cfallocdef = NULL; |
|
331 |
} /* if */ |
|
332 |
} /* macosxAllocatorDeinit */ |
|
333 |
||
334 |
||
335 |
static void *macosxAllocatorMalloc(PHYSFS_uint64 s) |
|
336 |
{ |
|
337 |
if (!__PHYSFS_ui64FitsAddressSpace(s)) |
|
338 |
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
339 |
return CFAllocatorAllocate(cfallocdef, (CFIndex) s, 0); |
|
340 |
} /* macosxAllocatorMalloc */ |
|
341 |
||
342 |
||
343 |
static void *macosxAllocatorRealloc(void *ptr, PHYSFS_uint64 s) |
|
344 |
{ |
|
345 |
if (!__PHYSFS_ui64FitsAddressSpace(s)) |
|
346 |
BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL); |
|
347 |
return CFAllocatorReallocate(cfallocdef, ptr, (CFIndex) s, 0); |
|
348 |
} /* macosxAllocatorRealloc */ |
|
349 |
||
350 |
||
351 |
static void macosxAllocatorFree(void *ptr) |
|
352 |
{ |
|
353 |
CFAllocatorDeallocate(cfallocdef, ptr); |
|
354 |
} /* macosxAllocatorFree */ |
|
355 |
||
356 |
||
357 |
int __PHYSFS_platformSetDefaultAllocator(PHYSFS_Allocator *a) |
|
358 |
{ |
|
359 |
allocator.Init = macosxAllocatorInit; |
|
360 |
allocator.Deinit = macosxAllocatorDeinit; |
|
361 |
allocator.Malloc = macosxAllocatorMalloc; |
|
362 |
allocator.Realloc = macosxAllocatorRealloc; |
|
363 |
allocator.Free = macosxAllocatorFree; |
|
364 |
return 1; /* return non-zero: we're supplying custom allocator. */ |
|
365 |
} /* __PHYSFS_platformSetDefaultAllocator */ |
|
366 |
||
367 |
#endif /* PHYSFS_PLATFORM_MACOSX */ |
|
368 |
||
369 |
/* end of macosx.c ... */ |
|
370 |