This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
shared.mm
222 lines (168 loc) · 5.58 KB
/
shared.mm
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* shared.mm
* AssitantExtensions
*
* Created by K3A on 13.3.11.
* Copyright 2011 __MyCompanyName__. All rights reserved.
*
*/
#include "shared.h"
#include <AppSupport/CPDistributedMessagingCenter.h>
#pragma mark - LOGGING -----------------------------------------------------------
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
void LogInfo(const char* function, const char* desc, ...)
{
static char buffer[1024];
va_list argList;
va_start(argList, desc);
vsprintf(buffer, desc, argList);
va_end(argList);
// get time
time_t tim;
time(&tim);
tm *timm = localtime(&tim);
int tda = timm->tm_mday;
int tmo = timm->tm_mon+1;
int tho = timm->tm_hour;
int tmi = timm->tm_min;
char buffer2[2048];
sprintf(buffer2, "ObjcDump[%d.%d@%d:%d]: %d: %s\r\n", tda, tmo, tho, tmi, getpid(), buffer);
//print
//printf("%s", buffer2);
NSLog(@"%s", buffer2);
}
void FlushLog()
{
static volatile int i=0;
i++;
//fflush(fp);
}
#pragma mark - APP IDENTIFIER -----------------------------------------------------------
#include <sys/sysctl.h>
NSString* getAppIdentifier()
{
NSString* appIdent = nil;
if (!appIdent)
{
NSString *returnString = nil;
int mib[4], maxarg = 0, numArgs = 0;
size_t size = 0;
char *args = NULL, *namePtr = NULL, *stringPtr = NULL;
mib[0] = CTL_KERN;
mib[1] = KERN_ARGMAX;
size = sizeof(maxarg);
if ( sysctl(mib, 2, &maxarg, &size, NULL, 0) == -1 ) {
return @"Unknown";
}
args = (char *)malloc( maxarg );
if ( args == NULL ) {
return @"Unknown";
}
mib[0] = CTL_KERN;
mib[1] = KERN_PROCARGS2;
mib[2] = getpid();
size = (size_t)maxarg;
if ( sysctl(mib, 3, args, &size, NULL, 0) == -1 ) {
free( args );
return @"Unknown";
}
memcpy( &numArgs, args, sizeof(numArgs) );
stringPtr = args + sizeof(numArgs);
if ( (namePtr = strrchr(stringPtr, '/')) != NULL ) {
namePtr++;
returnString = [[NSString alloc] initWithUTF8String:namePtr];
} else {
returnString = [[NSString alloc] initWithUTF8String:stringPtr];
}
return [returnString autorelease];
}
if (!appIdent) appIdent = [[NSBundle mainBundle] bundleIdentifier];
return appIdent;
}
#pragma mark - IS VALID POINTER? -----------------------------------------------------------
#import <malloc/malloc.h>
#import <objc/runtime.h>
static sigjmp_buf sigjmp_env;
inline void PointerReadFailedHandler(int signum)
{
siglongjmp (sigjmp_env, 1);
}
BOOL IsPointerAnObject(const void *testPointer)
{
BOOL allocatedLargeEnough = NO;
if (!testPointer) return NO;
// Set up SIGSEGV and SIGBUS handlers
struct sigaction new_segv_action, old_segv_action;
struct sigaction new_bus_action, old_bus_action;
new_segv_action.sa_handler = PointerReadFailedHandler;
new_bus_action.sa_handler = PointerReadFailedHandler;
sigemptyset(&new_segv_action.sa_mask);
sigemptyset(&new_bus_action.sa_mask);
new_segv_action.sa_flags = 0;
new_bus_action.sa_flags = 0;
sigaction (SIGSEGV, &new_segv_action, &old_segv_action);
sigaction (SIGBUS, &new_bus_action, &old_bus_action);
// The signal handler will return us to here if a signal is raised
if (sigsetjmp(sigjmp_env, 1))
{
sigaction (SIGSEGV, &old_segv_action, NULL);
sigaction (SIGBUS, &old_bus_action, NULL);
return NO;
}
Class testPointerClass = *((Class *)testPointer);
// Get the list of classes and look for testPointerClass
BOOL isClass = NO;
NSInteger numClasses = objc_getClassList(NULL, 0);
Class *classesList = (Class *)malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classesList, numClasses);
for (int i = 0; i < numClasses; i++)
{
if (classesList[i] == testPointerClass)
{
isClass = YES;
break;
}
}
free(classesList);
// We're done with the signal handlers (install the previous ones)
sigaction (SIGSEGV, &old_segv_action, NULL);
sigaction (SIGBUS, &old_bus_action, NULL);
// Pointer does not point to a valid isa pointer
if (!isClass)
{
return NO;
}
// Check the allocation size
size_t allocated_size = malloc_size(testPointer);
size_t instance_size = class_getInstanceSize(testPointerClass);
if (allocated_size > instance_size)
{
allocatedLargeEnough = YES;
}
return allocatedLargeEnough;
}
#pragma mark - OTHER HELPERS -----------------------------------------------------------
NSString* RandomUUID()
{
return [NSString stringWithFormat:@"1%07x-%04x-%04x-%04x-%06x%06x", rand()%0xFFFFFFF, rand()%0xFFFF, rand()%0xFFFF, rand()%0xFFFF, rand()%0xFFFFFF, rand()%0xFFFFFF];
}
unsigned GetTimestampMsec()
{
timeval time;
gettimeofday(&time, NULL);
unsigned elapsed_seconds = time.tv_sec;
unsigned elapsed_useconds = time.tv_usec;
return elapsed_seconds * 1000 + elapsed_useconds/1000;
}
void IPCCall(NSString* center, NSString* message, NSDictionary* object)
{
[[CPDistributedMessagingCenter centerNamed:center] sendMessageName:message userInfo:object];
}
NSDictionary* IPCCallResponse(NSString* center, NSString* message, NSDictionary* object)
{
return [[CPDistributedMessagingCenter centerNamed:center] sendMessageAndReceiveReplyName:message userInfo:object];
}