forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDDataCursor.m
308 lines (243 loc) · 6.33 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
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
// -*- 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-2009 Steve Nygard.
#import "CDDataCursor.h"
@implementation CDDataCursor
- (id)initWithData:(NSData *)someData;
{
if ([super init] == nil)
return nil;
data = [someData retain];
offset = 0;
byteOrder = CDByteOrderLittleEndian;
return self;
}
- (void)dealloc;
{
[data release];
[super dealloc];
}
- (NSData *)data;
{
return data;
}
- (const void *)bytes;
{
return [data bytes];
}
- (NSUInteger)offset;
{
return offset;
}
// Return NO on failure.
- (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;
{
[self setOffset:offset + length];
}
- (NSUInteger)remaining;
{
return [data length] - offset;
}
- (uint8_t)readByte;
{
const uint8_t *ptr;
ptr = [data bytes] + offset;
offset += 1;
return *ptr;
}
- (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]) {
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 *)targetData;
{
if (offset + length <= [data length]) {
[targetData appendBytes:[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, [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];
}
- (CDByteOrder)byteOrder;
{
return byteOrder;
}
- (void)setByteOrder:(CDByteOrder)newByteOrder;
{
byteOrder = newByteOrder;
}
//
// Read using the current byteOrder
//
- (uint16_t)readInt16;
{
if (byteOrder == CDByteOrderLittleEndian)
return [self readLittleInt16];
return [self readBigInt16];
}
- (uint32_t)readInt32;
{
if (byteOrder == CDByteOrderLittleEndian)
return [self readLittleInt32];
return [self readBigInt32];
}
- (uint64_t)readInt64;
{
if (byteOrder == CDByteOrderLittleEndian)
return [self readLittleInt64];
return [self readBigInt64];
}
- (uint32_t)peekInt32;
{
NSUInteger savedOffset;
uint32_t val;
savedOffset = offset;
val = [self readInt32];
offset = savedOffset;
return val;
}
- (NSString *)readCString;
{
return [self readStringOfLength:strlen([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, [data bytes] + offset, length);
buf[length] = 0;
str = [[[NSString alloc] initWithBytes:buf length:strlen(buf) encoding:encoding] autorelease];
offset += length;
free(buf);
return str;
} else {
str = [[[NSString alloc] initWithBytes:[data bytes] + offset length:length encoding:encoding] autorelease];;
offset += length;
return str;
}
} else {
[NSException raise:NSRangeException format:@"Trying to read past end in %s", _cmd];
}
return nil;
}
@end