forked from nygard/class-dump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCDMachOFileDataCursor.m
113 lines (88 loc) · 2.38 KB
/
CDMachOFileDataCursor.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
// -*- 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 "CDMachOFileDataCursor.h"
#import "CDMachOFile.h"
#import "CDLCSegment.h"
#import "CDSection.h"
@implementation CDMachOFileDataCursor
{
__weak CDMachOFile *_machOFile;
NSUInteger _ptrSize;
CDByteOrder _byteOrder;
}
- (id)initWithFile:(CDMachOFile *)machOFile;
{
return [self initWithFile:machOFile offset:0];
}
- (id)initWithFile:(CDMachOFile *)machOFile offset:(NSUInteger)offset;
{
if ((self = [super initWithData:machOFile.data])) {
self.machOFile = machOFile;
[self setOffset:offset];
}
return self;
}
- (id)initWithFile:(CDMachOFile *)machOFile address:(NSUInteger)address;
{
if ((self = [super initWithData:machOFile.data])) {
self.machOFile = machOFile;
[self setAddress:address];
}
return self;
}
- (id)initWithSection:(CDSection *)section;
{
if ((self = [super initWithData:[section data]])) {
self.machOFile = section.segment.machOFile;
}
return self;
}
#pragma mark -
- (void)setMachOFile:(CDMachOFile *)machOFile;
{
_machOFile = machOFile;
_ptrSize = machOFile.ptrSize;
_byteOrder = machOFile.byteOrder;
}
- (void)setAddress:(NSUInteger)address;
{
NSUInteger dataOffset = [_machOFile dataOffsetForAddress:address];
[self setOffset:dataOffset];
}
#pragma mark - Read using the current byteOrder
- (uint16_t)readInt16;
{
if (_byteOrder == CDByteOrder_LittleEndian)
return [self readLittleInt16];
return [self readBigInt16];
}
- (uint32_t)readInt32;
{
if (_byteOrder == CDByteOrder_LittleEndian)
return [self readLittleInt32];
return [self readBigInt32];
}
- (uint64_t)readInt64;
{
if (_byteOrder == CDByteOrder_LittleEndian)
return [self readLittleInt64];
return [self readBigInt64];
}
- (uint32_t)peekInt32;
{
NSUInteger savedOffset = self.offset;
uint32_t val = [self readInt32];
self.offset = savedOffset;
return val;
}
- (uint64_t)readPtr;
{
switch (_ptrSize) {
case sizeof(uint32_t): return [self readInt32];
case sizeof(uint64_t): return [self readInt64];
}
[NSException raise:NSInternalInconsistencyException format:@"The ptrSize must be either 4 (32-bit) or 8 (64-bit)"];
return 0;
}
@end