Welcome to mirror list, hosted at ThFree Co, Russian Federation.

load_bundle.c « mach_inject_bundle_stub - github.com/mumble-voip/mach_override.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 007b0dded5d1da8641b4a3a6a6dbfb1054b885c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*******************************************************************************
	load_bundle.c
		Copyright (c) 2003-2009 Jonathan 'Wolf' Rentzsch: <http://rentzsch.com>
		Some rights reserved: <http://opensource.org/licenses/mit-license.php>

	***************************************************************************/

#include "load_bundle.h"
#include <CoreServices/CoreServices.h>
#include <sys/syslimits.h> // for PATH_MAX.
#include <mach-o/dyld.h>
#include <dlfcn.h>

#include <mach/MACH_ERROR.h>
#define MACH_ERROR(msg, err) { if(err != err_none) mach_error(msg, err); }

	mach_error_t
load_bundle_package(
		const char *bundlePackageFileSystemRepresentation )
{
	fprintf(stderr, "mach_inject_bundle load_bundle_package: %s\n", bundlePackageFileSystemRepresentation);
	assert( bundlePackageFileSystemRepresentation );
	assert( strlen( bundlePackageFileSystemRepresentation ) );
	
	mach_error_t err = err_none;
	MACH_ERROR("mach error on bundle load", err);

	//	Morph the FSR into a URL.
	CFURLRef bundlePackageURL = NULL;
	if( !err ) {
		bundlePackageURL = CFURLCreateFromFileSystemRepresentation(
			kCFAllocatorDefault,
			(const UInt8*)bundlePackageFileSystemRepresentation,
			strlen(bundlePackageFileSystemRepresentation),
			true );
		if( bundlePackageURL == NULL )
			err = err_load_bundle_url_from_path;
	}
	MACH_ERROR("mach error on bundle load", err);

	//	Create bundle.
	CFBundleRef bundle = NULL;
	if( !err ) {
		bundle = CFBundleCreate( kCFAllocatorDefault, bundlePackageURL );
		if( bundle == NULL )
			err = err_load_bundle_create_bundle;
	}
	MACH_ERROR("mach error on bundle load", err);

	//	Discover the bundle's executable file.
	CFURLRef bundleExecutableURL = NULL;
	if( !err ) {
		assert( bundle );
		bundleExecutableURL = CFBundleCopyExecutableURL( bundle );
		if( bundleExecutableURL == NULL )
			err = err_load_bundle_package_executable_url;
	}
	MACH_ERROR("mach error on bundle load", err);

	//	Morph the executable's URL into an FSR.
	char bundleExecutableFileSystemRepresentation[PATH_MAX];
	if( !err ) {
		assert( bundleExecutableURL );
		if( !CFURLGetFileSystemRepresentation(
			bundleExecutableURL,
			true,
			(UInt8*)bundleExecutableFileSystemRepresentation,
			sizeof(bundleExecutableFileSystemRepresentation) ) )
		{
			err = err_load_bundle_path_from_url;
		}
	}
	MACH_ERROR("mach error on bundle load", err);

	//	Do the real work.
	if( !err ) {
		assert( strlen(bundleExecutableFileSystemRepresentation) );
		err = load_bundle_executable( bundleExecutableFileSystemRepresentation);
	}
	
	//	Clean up.
	if( bundleExecutableURL )
		CFRelease( bundleExecutableURL );
	/*if( bundle )
		CFRelease( bundle );*/
	if( bundlePackageURL )
		CFRelease( bundlePackageURL );
	
	MACH_ERROR("mach error on bundle load", err);
	return err;
}

	mach_error_t
load_bundle_executable(
		const char *bundleExecutableFileSystemRepresentation )
{
	assert( bundleExecutableFileSystemRepresentation );

	/*
	NSBundle* bundle = [NSBundle bundleWithPath:[NSString stringWithUTF8String:bundleExecutableFileSystemRepresentation]];
	
	if(![bundle load]) {
		fprintf(stderr, "mach_inject: failed to load %s\n", bundleExecutableFileSystemRepresentation);
		return err_load_bundle_NSObjectFileImageFailure;
	}
	else
		fprintf(stderr, "mach_inject: loaded succesfull: %s\n", bundleExecutableFileSystemRepresentation);
	*/
	
	//fprintf(stderr, "FS rep %s\n", bundleExecutableFileSystemRepresentation);
	void *image = dlopen(bundleExecutableFileSystemRepresentation, RTLD_NOW);
	//fprintf(stderr, "OH shit load? %p\n", image);
	if (!image) {
		dlerror();
		return err_load_bundle_NSObjectFileImageFailure;
	}

	return 0;
}