forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDDataCursor.m
257 lines (207 loc) · 6.05 KB
/
CDDataCursor.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
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
// -*- mode: ObjC -*-
// This file is part of class-dump, a utility for examining the Objective-C segment of Mach-O files.
// Copyright (C) 1997-1998, 2000-2001, 2004-2015 Steve Nygard.
#import "CDDataCursor.h"
@implementation CDDataCursor
{
NSData *_data;
NSUInteger _offset;
}
- (id)initWithData:(NSData *)data;
{
if ((self = [super init])) {
_data = data;
_offset = 0;
}
return self;
}
#pragma mark -
- (const void *)bytes;
{
return [_data bytes];
}
- (void)setOffset:(NSUInteger)newOffset;
{
if (newOffset <= [_data length]) {
_offset = newOffset;
} else {
[NSException raise:NSRangeException format:@"Trying to seek past end of data."];
}
}
- (void)advanceByLength:(NSUInteger)length;
{
if (_offset + length <= [_data length]) {
_offset += length;
} else {
[NSException raise:NSRangeException format:@"Trying to advance past end of data."];
}
}
- (NSUInteger)remaining;
{
return [_data length] - _offset;
}
#pragma mark -
- (uint8_t)readByte;
{
uint8_t result;
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadLittleInt16([_data bytes], _offset) & 0xFF;
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}
return result;
}
- (uint16_t)readLittleInt16;
{
uint16_t result;
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadLittleInt16([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}
return result;
}
- (uint32_t)readLittleInt32;
{
uint32_t result;
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadLittleInt32([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}
return result;
}
- (uint64_t)readLittleInt64;
{
uint64_t result;
if (_offset + sizeof(result) <= [_data length]) {
// uint8_t *ptr = [_data bytes] + _offset;
// NSLog(@"%016llx: %02x %02x %02x %02x %02x %02x %02x %02x", _offset, ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7]);
result = OSReadLittleInt64([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}
return result;
}
- (uint16_t)readBigInt16;
{
uint16_t result;
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadBigInt16([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}
return result;
}
- (uint32_t)readBigInt32;
{
uint32_t result;
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadBigInt32([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}
return result;
}
- (uint64_t)readBigInt64;
{
uint64_t result;
if (_offset + sizeof(result) <= [_data length]) {
result = OSReadBigInt64([_data bytes], _offset);
_offset += sizeof(result);
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
result = 0;
}
return result;
}
- (float)readLittleFloat32;
{
uint32_t val;
val = [self readLittleInt32];
return *(float *)&val;
}
- (float)readBigFloat32;
{
uint32_t val;
val = [self readBigInt32];
return *(float *)&val;
}
- (double)readLittleFloat64;
{
uint32_t v1, v2, *ptr;
double dval;
v1 = [self readLittleInt32];
v2 = [self readLittleInt32];
ptr = (uint32_t *)&dval;
*ptr++ = v1;
*ptr = v2;
return dval;
}
- (void)appendBytesOfLength:(NSUInteger)length intoData:(NSMutableData *)data;
{
if (_offset + length <= [_data length]) {
[data appendBytes:(uint8_t *)[_data bytes] + _offset length:length];
_offset += length;
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
}
}
- (void)readBytesOfLength:(NSUInteger)length intoBuffer:(void *)buf;
{
if (_offset + length <= [_data length]) {
memcpy(buf, (uint8_t *)[_data bytes] + _offset, length);
_offset += length;
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
}
}
- (BOOL)isAtEnd;
{
return _offset >= [_data length];
}
- (NSString *)readCString;
{
return [self readStringOfLength:strlen((const char *)[_data bytes] + _offset) encoding:NSASCIIStringEncoding];
}
- (NSString *)readStringOfLength:(NSUInteger)length encoding:(NSStringEncoding)encoding;
{
if (_offset + length <= [_data length]) {
NSString *str;
if (encoding == NSASCIIStringEncoding) {
char *buf;
// Jump through some hoops if the length is padded with zero bytes, as in the case of 10.5's Property List Editor and iSync Plug-in Maker.
buf = malloc(length + 1);
if (buf == NULL) {
NSLog(@"Error: malloc() failed.");
return nil;
}
strncpy(buf, (const char *)[_data bytes] + _offset, length);
buf[length] = 0;
str = [[NSString alloc] initWithBytes:buf length:strlen(buf) encoding:encoding];
_offset += length;
free(buf);
return str;
} else {
str = [[NSString alloc] initWithBytes:(uint8_t *)[_data bytes] + _offset length:length encoding:encoding];
_offset += length;
return str;
}
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
}
return nil;
}
@end