forked from kpwn/uikittools-ng
-
Notifications
You must be signed in to change notification settings - Fork 14
/
mgask.m
217 lines (186 loc) · 5.29 KB
/
mgask.m
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
212
213
214
215
216
217
#import <Foundation/Foundation.h>
#include <CommonCrypto/CommonDigest.h>
#include <err.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include "gssc.h"
#if NLS
# include <libintl.h>
# define _(a) gettext(a)
# define PACKAGE "uikittools-ng"
#else
# define _(a) a
#endif
#ifndef LOCALEDIR
# define LOCALEDIR "/usr/share/locale"
#endif
CFTypeRef MGCopyAnswer(CFStringRef);
const char *toJson(NSObject *object, bool prettyprint) {
NSError *error;
NSData *json;
json = [NSJSONSerialization
dataWithJSONObject:object
options:prettyprint ? NSJSONWritingPrettyPrinted : 0
error:&error];
if (error)
errx(1, _("JSON formating failed: %s"),
error.localizedDescription.UTF8String);
return [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]
.UTF8String;
}
NSString *obfuscateKey(const char *key)
{
char buffer[256] = { 0 };
snprintf(buffer, sizeof(buffer), "%s%s", "MGCopyAnswer", key);
unsigned char md5Hash[CC_MD5_DIGEST_LENGTH] = { 0 };
CC_MD5(buffer, (CC_LONG)strlen(buffer), md5Hash);
NSData *data = [NSData dataWithBytes:md5Hash length:CC_MD5_DIGEST_LENGTH];
NSString *obfuscatedKey = [data base64EncodedStringWithOptions:0];
return [obfuscatedKey substringToIndex:22];
}
int main(int argc, char **argv) {
#if NLS
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
#endif
bool json, plist, prettyprint, quiet, gssc, calculate;
json = plist = prettyprint = quiet = gssc = calculate = false;
int ch, index;
if (!strcmp(getprogname(), "gssc")) {
if (argc == 1) {
gssc = plist = quiet = true;
json = false;
}
fprintf(stderr, "The gssc utility is deprecated.\nPlease use mgask(1)\n");
}
enum options {
OPT_GSSC = CHAR_MAX + 1,
OPT_PRETTY
};
struct option longopts[] = {
{"json", no_argument, 0, 'j'},
{"plist", no_argument, 0, 'p'},
{"quiet", no_argument, 0, 'q'},
{"gssc", no_argument, NULL, OPT_GSSC},
{"pretty", no_argument, NULL, OPT_PRETTY},
{"calculate", no_argument, NULL, 'c'},
{NULL, 0, NULL, 0}};
while ((ch = getopt_long(argc, argv, "cjpq", longopts, &index)) != -1) {
switch (ch) {
case 'j':
json = true;
plist = false;
break;
case 'p':
plist = true;
json = false;
break;
case OPT_GSSC:
gssc = plist = quiet = true;
json = false;
break;
case OPT_PRETTY:
prettyprint = true;
break;
case 'q':
quiet = true;
break;
case 'c':
calculate = true;
break;
}
}
argv += optind;
argc -= optind;
if (!gssc && argc == 0) {
fprintf(stderr, _("Usage: %s [-cjpq] [--gssc] [--pretty] question ...\n"), getprogname());
return 1;
}
int i = 0, count;
char **array;
NSMutableDictionary *outDict = [[NSMutableDictionary alloc] init];
if (gssc) {
count = sizeof(gsscKeys) / sizeof(gsscKeys[0]);
array = gsscKeys;
} else {
count = argc;
array = argv;
}
if (calculate) {
for (i = 0; i < count; i++) {
if (!json && !plist) {
if (argc != 1)
printf("%s: ", array[i]);
printf("%s\n", [obfuscateKey(array[i]) UTF8String]);
} else {
[outDict setObject:obfuscateKey(array[i])
forKey:[NSString stringWithUTF8String:array[i]]];
}
}
} else {
for (i = 0; i < count; i++) {
const char *answer;
CFTypeRef mganswer;
mganswer = MGCopyAnswer(
(__bridge CFStringRef)[NSString stringWithUTF8String:array[i]]);
if (mganswer == NULL) {
if (!quiet)
fprintf(stderr, _("Cannot find key %s\n"), array[i]);
goto skipprint;
}
CFTypeID typeid = CFGetTypeID(mganswer);
if (typeid == CFStringGetTypeID()) {
if (!json && !plist)
answer = [(__bridge_transfer NSString *)mganswer UTF8String];
else
[outDict setObject:(__bridge_transfer NSString *)mganswer
forKey:[NSString stringWithUTF8String:array[i]]];
} else if (typeid == CFBooleanGetTypeID()) {
if (!json && !plist)
answer = CFBooleanGetValue(mganswer) ? _("true") : _("false");
else
[outDict setObject:CFBooleanGetValue(mganswer) ? @YES : @NO
forKey:[NSString stringWithUTF8String:array[i]]];
} else if (typeid == CFNumberGetTypeID()) {
if (!json && !plist)
answer = [(__bridge_transfer NSNumber *)mganswer stringValue]
.UTF8String;
else
[outDict setObject:(__bridge_transfer NSNumber *)mganswer
forKey:[NSString stringWithUTF8String:array[i]]];
} else if (typeid == CFDictionaryGetTypeID()) {
if (!json && !plist)
answer = toJson((__bridge NSObject *)mganswer, prettyprint);
else
[outDict setObject:(__bridge_transfer NSDictionary *)mganswer
forKey:[NSString stringWithUTF8String:array[i]]];
} else if (typeid == CFArrayGetTypeID()) {
if (!json && !plist)
answer = toJson((__bridge NSObject *)mganswer, prettyprint);
else
[outDict setObject:(__bridge_transfer NSArray *)mganswer
forKey:[NSString stringWithUTF8String:array[i]]];
} else {
if (!quiet)
fprintf(stderr, "%s has an unknown answer type\n", array[i]);
if (mganswer != NULL)
CFRelease(mganswer);
goto skipprint;
}
if (!json && !plist) {
if (argc != 1)
printf("%s: ", array[i]);
printf("%s\n", answer);
}
skipprint:
continue;
}
}
if (json)
printf("%s\n", toJson(outDict, prettyprint));
else if (plist)
printf("%s\n", [outDict descriptionWithLocale:nil].UTF8String);
return 0;
}