forked from gdbinit/MachOView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadWrite.mm
368 lines (325 loc) · 12.7 KB
/
ReadWrite.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*
* ReadWrite.mm
* MachOView
*
* Created by psaghelyi on 20/07/2010.
*
*/
#include <string>
#include <vector>
#include <set>
#include <map>
#import "Common.h"
#import "ReadWrite.h"
#import "DataController.h"
//============================================================================
@implementation MVDataController (ReadWrite)
//-----------------------------------------------------------------------------
- (uint8_t)read_uint8:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
uint8_t buffer;
range = NSMakeRange(NSMaxRange(range),sizeof(uint8_t));
[fileData getBytes:&buffer range:range];
if (lastReadHex) *lastReadHex = [NSString stringWithFormat:@"%.2X",(0xFF & buffer)];
[realData getBytes:&buffer range:range];
return buffer;
}
//-----------------------------------------------------------------------------
- (uint16_t)read_uint16:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
uint16_t buffer;
range = NSMakeRange(NSMaxRange(range),sizeof(uint16_t));
[fileData getBytes:&buffer range:range];
if (lastReadHex) *lastReadHex = [NSString stringWithFormat:@"%.4X",(0xFFFF & buffer)];
[realData getBytes:&buffer range:range];
return buffer;
}
//-----------------------------------------------------------------------------
- (uint32_t)read_uint32:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
uint32_t buffer;
range = NSMakeRange(NSMaxRange(range),sizeof(uint32_t));
[fileData getBytes:&buffer range:range];
if (lastReadHex) *lastReadHex = [NSString stringWithFormat:@"%.8X",buffer];
[realData getBytes:&buffer range:range];
return buffer;
}
//-----------------------------------------------------------------------------
- (uint64_t)read_uint64:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
uint64_t buffer;
range = NSMakeRange(NSMaxRange(range),sizeof(uint64_t));
[fileData getBytes:&buffer range:range];
if (lastReadHex) *lastReadHex = [NSString stringWithFormat:@"%.16qX",buffer];
[realData getBytes:&buffer range:range];
return buffer;
}
//-----------------------------------------------------------------------------
- (int8_t)read_int8:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
int8_t buffer;
range = NSMakeRange(NSMaxRange(range),sizeof(int8_t));
[fileData getBytes:&buffer range:range];
if (lastReadHex) *lastReadHex = [NSString stringWithFormat:@"%.2X",(0xFF & buffer)];
[realData getBytes:&buffer range:range];
return buffer;
}
//-----------------------------------------------------------------------------
- (int16_t)read_int16:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
int16_t buffer;
range = NSMakeRange(NSMaxRange(range),sizeof(int16_t));
[fileData getBytes:&buffer range:range];
if (lastReadHex) *lastReadHex = [NSString stringWithFormat:@"%.4X",(0xFFFF & buffer)];
[realData getBytes:&buffer range:range];
return buffer;
}
//-----------------------------------------------------------------------------
- (int32_t)read_int32:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
int32_t buffer;
range = NSMakeRange(NSMaxRange(range),sizeof(int32_t));
[fileData getBytes:&buffer range:range];
if (lastReadHex) *lastReadHex = [NSString stringWithFormat:@"%.8X",buffer];
[realData getBytes:&buffer range:range];
return buffer;
}
//-----------------------------------------------------------------------------
- (int64_t)read_int64:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
int64_t buffer;
range = NSMakeRange(NSMaxRange(range),sizeof(int64_t));
[fileData getBytes:&buffer range:range];
if (lastReadHex) *lastReadHex = [NSString stringWithFormat:@"%.16qX",buffer];
[realData getBytes:&buffer range:range];
return buffer;
}
//-----------------------------------------------------------------------------
- (NSString *)getHexStr:(NSRange &)range
{
NSMutableString * lastReadHex = [NSMutableString stringWithCapacity:2*range.length];
for (NSUInteger i = 0; i < range.length; ++i)
{
int value = *((uint8_t *)[fileData bytes] + range.location + i);
[lastReadHex appendFormat:@"%.2X",value];
}
return lastReadHex;
}
//-----------------------------------------------------------------------------
- (NSString *) replaceEscapeCharsInString: (NSString *)orig
{
NSUInteger len = [orig length];
NSMutableString * str = [[NSMutableString alloc] init];
SEL sel = @selector(characterAtIndex:);
unichar (*charAtIdx)(id, SEL, NSUInteger) = (typeof(charAtIdx)) [orig methodForSelector:sel];
for (NSUInteger i = 0; i < len; i++)
{
unichar c = charAtIdx(orig, sel, i);
switch (c)
{
default: [str appendFormat:@"%C",c]; break;
case L'\f': [str appendString:@"\\f"]; break; // form feed - new page (byte 0x0c)
case L'\n': [str appendString:@"\\n"]; break; // line feed - new line (byte 0x0a)
case L'\r': [str appendString:@"\\r"]; break; // carriage return (byte 0x0d)
case L'\t': [str appendString:@"\\t"]; break; // horizontal tab (byte 0x09)
case L'\v': [str appendString:@"\\v"]; break; // vertical tab (byte 0x0b)
}
}
return str;
}
//-----------------------------------------------------------------------------
- (NSString *)read_string:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
range.location = NSMaxRange(range);
NSString * str = NSSTRING((uint8_t *)[fileData bytes] + range.location);
range.length = [str length] + 1;
if (lastReadHex) *lastReadHex = [self getHexStr:range];
return [self replaceEscapeCharsInString:str];
}
//-----------------------------------------------------------------------------
- (NSString *)read_string:(NSRange &)range fixlen:(NSUInteger)len lastReadHex:(NSString **)lastReadHex
{
range = NSMakeRange(NSMaxRange(range),len);
uint8_t * buffer = (uint8_t *)malloc(len + 1); buffer[len] = '\0';
[fileData getBytes:buffer range:range];
if (lastReadHex) *lastReadHex = [self getHexStr:range];
NSString * str = NSSTRING(buffer);
free (buffer);
return [self replaceEscapeCharsInString:str];
}
//-----------------------------------------------------------------------------
- (NSData *)read_bytes:(NSRange &)range length:(NSUInteger)length lastReadHex:(NSString **)lastReadHex
{
range = NSMakeRange(NSMaxRange(range),length);
uint8_t * buffer = (uint8_t *)malloc(length);
[fileData getBytes:buffer range:range];
if (lastReadHex) *lastReadHex = [self getHexStr:range];
NSData * ret = [NSData dataWithBytes:buffer length:length];
free (buffer);
return ret;
}
//-----------------------------------------------------------------------------
- (int64_t)read_sleb128:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
range.location = NSMaxRange(range);
uint8_t * p = (uint8_t *)[fileData bytes] + range.location, *start = p;
int64_t result = 0;
int bit = 0;
uint8_t byte;
do {
byte = *p++;
result |= ((byte & 0x7f) << bit);
bit += 7;
} while (byte & 0x80);
// sign extend negative numbers
if ( (byte & 0x40) != 0 )
{
result |= (-1LL) << bit;
}
range.length = (p - start);
if (lastReadHex) *lastReadHex = [self getHexStr:range];
return result;
}
// ----------------------------------------------------------------------------
- (uint64_t)read_uleb128:(NSRange &)range lastReadHex:(NSString **)lastReadHex
{
range.location = NSMaxRange(range);
uint8_t * p = (uint8_t *)[fileData bytes] + range.location, *start = p;
uint64_t result = 0;
int bit = 0;
do {
uint64_t slice = *p & 0x7f;
if (bit >= 64 || slice << bit >> bit != slice)
[NSException raise:@"uleb128 error" format:@"uleb128 too big"];
else {
result |= (slice << bit);
bit += 7;
}
}
while (*p++ & 0x80);
range.length = (p - start);
if (lastReadHex) *lastReadHex = [self getHexStr:range];
return result;
}
// ----------------------------------------------------------------------------
- (void) write_uint8:(NSUInteger)location data:(uint8_t)data
{
[fileData replaceBytesInRange:NSMakeRange(location,sizeof(uint8_t))
withBytes:&data];
}
// ----------------------------------------------------------------------------
- (void) write_uint16:(NSUInteger)location data:(uint16_t)data
{
[fileData replaceBytesInRange:NSMakeRange(location,sizeof(uint16_t))
withBytes:&data];
}
// ----------------------------------------------------------------------------
- (void) write_uint32:(NSUInteger)location data:(uint32_t)data
{
[fileData replaceBytesInRange:NSMakeRange(location,sizeof(uint32_t))
withBytes:&data];
}
// ----------------------------------------------------------------------------
- (void) write_uint64:(NSUInteger)location data:(uint64_t)data
{
[fileData replaceBytesInRange:NSMakeRange(location,sizeof(uint64_t))
withBytes:&data];
}
// ----------------------------------------------------------------------------
- (void) write_int8:(NSUInteger)location data:(int8_t)data
{
[fileData replaceBytesInRange:NSMakeRange(location,sizeof(int8_t))
withBytes:&data];
}
// ----------------------------------------------------------------------------
- (void) write_int16:(NSUInteger)location data:(int16_t)data
{
[fileData replaceBytesInRange:NSMakeRange(location,sizeof(int16_t))
withBytes:&data];
}
// ----------------------------------------------------------------------------
- (void) write_int32:(NSUInteger)location data:(int32_t)data
{
[fileData replaceBytesInRange:NSMakeRange(location,sizeof(int32_t))
withBytes:&data];
}
// ----------------------------------------------------------------------------
- (void) write_int64:(NSUInteger)location data:(int64_t)data
{
[fileData replaceBytesInRange:NSMakeRange(location,sizeof(int64_t))
withBytes:&data];
}
// ----------------------------------------------------------------------------
- (void) write_string:(NSUInteger)location data:(NSString *)data
{
assert(false);
}
// ----------------------------------------------------------------------------
- (void) write_bytes:(NSUInteger)location data:(NSData *)data
{
assert(false);
}
// ----------------------------------------------------------------------------
- (void) write_sleb128:(NSUInteger)location data:(int64_t)data
{
assert(false);
/*
uint8_t *
writeSLEB128(uint8_t *p, int64_t value)
{
bool isNeg = ( value < 0 );
uint8_t byte;
bool more;
do {
byte = value & 0x7F;
value = value >> 7;
if ( isNeg )
more = ( (value != -1) || ((byte & 0x40) == 0) );
else
more = ( (value != 0) || ((byte & 0x40) != 0) );
if ( more )
byte |= 0x80;
*(p++) = byte;
}
while( more );
return p;
}
*/
}
// ----------------------------------------------------------------------------
- (void) write_uleb128:(NSUInteger)location data:(uint64_t)data
{
assert(false);
/*
uint8_t *
writeULEB128 (uint8_t *p, uint64_t value)
{
uint8_t byte;
do {
byte = value & 0x7F;
value &= ~0x7F;
if ( value != 0 )
byte |= 0x80;
*(p++) = byte;
value = value >> 7;
} while( byte >= 0x80 );
return p;
}
*/
}
// ----------------------------------------------------------------------------
- (uint8_t)read_uint8:(NSRange &)range { return [self read_uint8:range lastReadHex:NULL]; }
- (uint16_t)read_uint16:(NSRange &)range { return [self read_uint16:range lastReadHex:NULL]; }
- (uint32_t)read_uint32:(NSRange &)range { return [self read_uint32:range lastReadHex:NULL]; }
- (uint64_t)read_uint64:(NSRange &)range { return [self read_uint64:range lastReadHex:NULL]; }
- (int8_t)read_int8:(NSRange &)range { return [self read_int8:range lastReadHex:NULL]; }
- (int16_t)read_int16:(NSRange &)range { return [self read_int16:range lastReadHex:NULL]; }
- (int32_t)read_int32:(NSRange &)range { return [self read_int32:range lastReadHex:NULL]; }
- (int64_t)read_int64:(NSRange &)range { return [self read_int64:range lastReadHex:NULL]; }
// ----------------------------------------------------------------------------
- (NSString *) read_string:(NSRange &)range { return [self read_string:range lastReadHex:NULL]; }
- (NSString *) read_string:(NSRange &)range fixlen:(NSUInteger)len { return [self read_string:range fixlen:len lastReadHex:NULL]; }
- (NSData *) read_bytes:(NSRange &)range length:(NSUInteger)length { return [self read_bytes:range length:length lastReadHex:NULL]; }
- (int64_t) read_sleb128:(NSRange &)range { return [self read_sleb128:range lastReadHex:NULL]; }
- (uint64_t) read_uleb128:(NSRange &)range { return [self read_uleb128:range lastReadHex:NULL]; }
@end