Skip to content

Commit 606ad2c

Browse files
committed
Merge branch '0xced-optimizations'
2 parents e79d0ad + 8578147 commit 606ad2c

8 files changed

+200
-196
lines changed

Source/CDDataCursor.m

+48-39
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ - (id)initWithData:(NSData *)data;
2525

2626
- (const void *)bytes;
2727
{
28-
return [self.data bytes];
28+
return [_data bytes];
2929
}
3030

3131
- (void)setOffset:(NSUInteger)newOffset;
3232
{
33-
if (newOffset <= [self.data length]) {
33+
if (newOffset <= [_data length]) {
3434
_offset = newOffset;
3535
} else {
3636
[NSException raise:NSRangeException format:@"Trying to seek past end of data."];
@@ -39,33 +39,42 @@ - (void)setOffset:(NSUInteger)newOffset;
3939

4040
- (void)advanceByLength:(NSUInteger)length;
4141
{
42-
self.offset += length;
42+
if (_offset + length <= [_data length]) {
43+
_offset += length;
44+
} else {
45+
[NSException raise:NSRangeException format:@"Trying to advance past end of data."];
46+
}
4347
}
4448

4549
- (NSUInteger)remaining;
4650
{
47-
return [self.data length] - self.offset;
51+
return [_data length] - _offset;
4852
}
4953

5054
#pragma mark -
5155

5256
- (uint8_t)readByte;
5357
{
54-
const uint8_t *ptr;
58+
uint8_t result;
5559

56-
ptr = (uint8_t *)[self.data bytes] + self.offset;
57-
self.offset += 1;
60+
if (_offset + sizeof(result) <= [_data length]) {
61+
result = OSReadLittleInt16([_data bytes], _offset) & 0xFF;
62+
_offset += sizeof(result);
63+
} else {
64+
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
65+
result = 0;
66+
}
5867

59-
return *ptr;
68+
return result;
6069
}
6170

6271
- (uint16_t)readLittleInt16;
6372
{
6473
uint16_t result;
6574

66-
if (self.offset + sizeof(result) <= [self.data length]) {
67-
result = OSReadLittleInt16([self.data bytes], self.offset);
68-
self.offset += sizeof(result);
75+
if (_offset + sizeof(result) <= [_data length]) {
76+
result = OSReadLittleInt16([_data bytes], _offset);
77+
_offset += sizeof(result);
6978
} else {
7079
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
7180
result = 0;
@@ -78,9 +87,9 @@ - (uint32_t)readLittleInt32;
7887
{
7988
uint32_t result;
8089

81-
if (self.offset + sizeof(result) <= [self.data length]) {
82-
result = OSReadLittleInt32([self.data bytes], self.offset);
83-
self.offset += sizeof(result);
90+
if (_offset + sizeof(result) <= [_data length]) {
91+
result = OSReadLittleInt32([_data bytes], _offset);
92+
_offset += sizeof(result);
8493
} else {
8594
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
8695
result = 0;
@@ -93,9 +102,9 @@ - (uint64_t)readLittleInt64;
93102
{
94103
uint64_t result;
95104

96-
if (self.offset + sizeof(result) <= [self.data length]) {
97-
result = OSReadLittleInt64([self.data bytes], self.offset);
98-
self.offset += sizeof(result);
105+
if (_offset + sizeof(result) <= [_data length]) {
106+
result = OSReadLittleInt64([_data bytes], _offset);
107+
_offset += sizeof(result);
99108
} else {
100109
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
101110
result = 0;
@@ -108,9 +117,9 @@ - (uint16_t)readBigInt16;
108117
{
109118
uint16_t result;
110119

111-
if (self.offset + sizeof(result) <= [self.data length]) {
112-
result = OSReadBigInt16([self.data bytes], self.offset);
113-
self.offset += sizeof(result);
120+
if (_offset + sizeof(result) <= [_data length]) {
121+
result = OSReadBigInt16([_data bytes], _offset);
122+
_offset += sizeof(result);
114123
} else {
115124
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
116125
result = 0;
@@ -123,9 +132,9 @@ - (uint32_t)readBigInt32;
123132
{
124133
uint32_t result;
125134

126-
if (self.offset + sizeof(result) <= [self.data length]) {
127-
result = OSReadBigInt32([self.data bytes], self.offset);
128-
self.offset += sizeof(result);
135+
if (_offset + sizeof(result) <= [_data length]) {
136+
result = OSReadBigInt32([_data bytes], _offset);
137+
_offset += sizeof(result);
129138
} else {
130139
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
131140
result = 0;
@@ -138,9 +147,9 @@ - (uint64_t)readBigInt64;
138147
{
139148
uint64_t result;
140149

141-
if (self.offset + sizeof(result) <= [self.data length]) {
142-
result = OSReadBigInt64([self.data bytes], self.offset);
143-
self.offset += sizeof(result);
150+
if (_offset + sizeof(result) <= [_data length]) {
151+
result = OSReadBigInt64([_data bytes], _offset);
152+
_offset += sizeof(result);
144153
} else {
145154
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
146155
result = 0;
@@ -181,37 +190,37 @@ - (double)readLittleFloat64;
181190

182191
- (void)appendBytesOfLength:(NSUInteger)length intoData:(NSMutableData *)data;
183192
{
184-
if (self.offset + length <= [self.data length]) {
185-
[data appendBytes:(uint8_t *)[self.data bytes] + self.offset length:length];
186-
self.offset += length;
193+
if (_offset + length <= [_data length]) {
194+
[data appendBytes:(uint8_t *)[_data bytes] + _offset length:length];
195+
_offset += length;
187196
} else {
188197
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
189198
}
190199
}
191200

192201
- (void)readBytesOfLength:(NSUInteger)length intoBuffer:(void *)buf;
193202
{
194-
if (self.offset + length <= [self.data length]) {
195-
memcpy(buf, (uint8_t *)[self.data bytes] + self.offset, length);
196-
self.offset += length;
203+
if (_offset + length <= [_data length]) {
204+
memcpy(buf, (uint8_t *)[_data bytes] + _offset, length);
205+
_offset += length;
197206
} else {
198207
[NSException raise:NSRangeException format:@"Trying to read past end in %s", __cmd];
199208
}
200209
}
201210

202211
- (BOOL)isAtEnd;
203212
{
204-
return self.offset >= [self.data length];
213+
return _offset >= [_data length];
205214
}
206215

207216
- (NSString *)readCString;
208217
{
209-
return [self readStringOfLength:strlen((const char *)[self.data bytes] + self.offset) encoding:NSASCIIStringEncoding];
218+
return [self readStringOfLength:strlen((const char *)[_data bytes] + _offset) encoding:NSASCIIStringEncoding];
210219
}
211220

212221
- (NSString *)readStringOfLength:(NSUInteger)length encoding:(NSStringEncoding)encoding;
213222
{
214-
if (self.offset + length <= [self.data length]) {
223+
if (_offset + length <= [_data length]) {
215224
NSString *str;
216225

217226
if (encoding == NSASCIIStringEncoding) {
@@ -224,16 +233,16 @@ - (NSString *)readStringOfLength:(NSUInteger)length encoding:(NSStringEncoding)e
224233
return nil;
225234
}
226235

227-
strncpy(buf, (const char *)[self.data bytes] + self.offset, length);
236+
strncpy(buf, (const char *)[_data bytes] + _offset, length);
228237
buf[length] = 0;
229238

230239
str = [[NSString alloc] initWithBytes:buf length:strlen(buf) encoding:encoding];
231-
self.offset += length;
240+
_offset += length;
232241
free(buf);
233242
return str;
234243
} else {
235-
str = [[NSString alloc] initWithBytes:(uint8_t *)[self.data bytes] + self.offset length:length encoding:encoding];
236-
self.offset += length;
244+
str = [[NSString alloc] initWithBytes:(uint8_t *)[_data bytes] + _offset length:length encoding:encoding];
245+
_offset += length;
237246
return str;
238247
}
239248
} else {

Source/CDMachOFileDataCursor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
- (id)initWithSection:(CDSection *)section;
1717

18-
@property (weak, readonly) CDMachOFile *machOFile;
18+
@property (nonatomic, weak, readonly) CDMachOFile *machOFile;
1919

2020
- (void)setAddress:(NSUInteger)address;
2121

Source/CDMachOFileDataCursor.m

+17-8
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
@implementation CDMachOFileDataCursor
1212
{
1313
__weak CDMachOFile *_machOFile;
14+
NSUInteger _ptrSize;
15+
CDByteOrder _byteOrder;
1416
}
1517

1618
- (id)initWithFile:(CDMachOFile *)machOFile;
@@ -21,7 +23,7 @@ - (id)initWithFile:(CDMachOFile *)machOFile;
2123
- (id)initWithFile:(CDMachOFile *)machOFile offset:(NSUInteger)offset;
2224
{
2325
if ((self = [super initWithData:machOFile.data])) {
24-
_machOFile = machOFile;
26+
self.machOFile = machOFile;
2527
[self setOffset:offset];
2628
}
2729

@@ -31,7 +33,7 @@ - (id)initWithFile:(CDMachOFile *)machOFile offset:(NSUInteger)offset;
3133
- (id)initWithFile:(CDMachOFile *)machOFile address:(NSUInteger)address;
3234
{
3335
if ((self = [super initWithData:machOFile.data])) {
34-
_machOFile = machOFile;
36+
self.machOFile = machOFile;
3537
[self setAddress:address];
3638
}
3739

@@ -41,41 +43,48 @@ - (id)initWithFile:(CDMachOFile *)machOFile address:(NSUInteger)address;
4143
- (id)initWithSection:(CDSection *)section;
4244
{
4345
if ((self = [super initWithData:[section data]])) {
44-
_machOFile = [section machOFile];
46+
self.machOFile = [section machOFile];
4547
}
4648

4749
return self;
4850
}
4951

5052
#pragma mark -
5153

54+
- (void)setMachOFile:(CDMachOFile *)machOFile;
55+
{
56+
_machOFile = machOFile;
57+
_ptrSize = machOFile.ptrSize;
58+
_byteOrder = machOFile.byteOrder;
59+
}
60+
5261
- (void)setAddress:(NSUInteger)address;
5362
{
54-
NSUInteger dataOffset = [self.machOFile dataOffsetForAddress:address];
63+
NSUInteger dataOffset = [_machOFile dataOffsetForAddress:address];
5564
[self setOffset:dataOffset];
5665
}
5766

5867
#pragma mark - Read using the current byteOrder
5968

6069
- (uint16_t)readInt16;
6170
{
62-
if (self.machOFile.byteOrder == CDByteOrder_LittleEndian)
71+
if (_byteOrder == CDByteOrder_LittleEndian)
6372
return [self readLittleInt16];
6473

6574
return [self readBigInt16];
6675
}
6776

6877
- (uint32_t)readInt32;
6978
{
70-
if (self.machOFile.byteOrder == CDByteOrder_LittleEndian)
79+
if (_byteOrder == CDByteOrder_LittleEndian)
7180
return [self readLittleInt32];
7281

7382
return [self readBigInt32];
7483
}
7584

7685
- (uint64_t)readInt64;
7786
{
78-
if (self.machOFile.byteOrder == CDByteOrder_LittleEndian)
87+
if (_byteOrder == CDByteOrder_LittleEndian)
7988
return [self readLittleInt64];
8089

8190
return [self readBigInt64];
@@ -92,7 +101,7 @@ - (uint32_t)peekInt32;
92101

93102
- (uint64_t)readPtr;
94103
{
95-
switch ([self.machOFile ptrSize]) {
104+
switch (_ptrSize) {
96105
case sizeof(uint32_t): return [self readInt32];
97106
case sizeof(uint64_t): return [self readInt64];
98107
}

Source/CDObjectiveC2Processor.m

+4
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,10 @@ - (CDOCClass *)loadClassAtAddress:(uint64_t)address;
204204
if (address == 0)
205205
return nil;
206206

207+
CDOCClass *class = [self classWithAddress:address];
208+
if (class)
209+
return class;
210+
207211
//NSLog(@"%s, address=%016lx", __cmd, address);
208212

209213
CDMachOFileDataCursor *cursor = [[CDMachOFileDataCursor alloc] initWithFile:self.machOFile address:address];

Source/CDType.h

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
@interface CDType : NSObject <NSCopying>
99

10-
- (id)init;
1110
- (id)initSimpleType:(int)type;
1211
- (id)initIDType:(CDTypeName *)name;
1312
- (id)initIDType:(CDTypeName *)name withProtocols:(NSArray *)protocols;

0 commit comments

Comments
 (0)