Skip to content

Commit 492e076

Browse files
author
Wessel van der Veen
committed
feat(label): add ZGLabel and ZGDocumentLabelManager implementations
1 parent 7828332 commit 492e076

17 files changed

+476
-12
lines changed

Bit Slicer.xcodeproj/project.pbxproj

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
4E1220AD297C747000E3166F /* ZGDocumentLabelManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E1220AC297C747000E3166F /* ZGDocumentLabelManager.m */; };
11+
4E1220AF297D432D00E3166F /* ZGLabel.m in Sources */ = {isa = PBXBuildFile; fileRef = 4E1220AE297D432D00E3166F /* ZGLabel.m */; };
1012
72030ADC1B00191400A5621C /* ZGScriptingInterpreter.m in Sources */ = {isa = PBXBuildFile; fileRef = 72030ADB1B00191400A5621C /* ZGScriptingInterpreter.m */; };
1113
720844FA24E0715C00F3D449 /* DataInspector.m in Sources */ = {isa = PBXBuildFile; fileRef = 720844F924E0715C00F3D449 /* DataInspector.m */; };
1214
720844FD24E072DB00F3D449 /* DataInspectorScrollView.m in Sources */ = {isa = PBXBuildFile; fileRef = 720844FC24E072DB00F3D449 /* DataInspectorScrollView.m */; };
@@ -301,6 +303,10 @@
301303
/* End PBXCopyFilesBuildPhase section */
302304

303305
/* Begin PBXFileReference section */
306+
4E1220AB297C745900E3166F /* ZGDocumentLabelManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ZGDocumentLabelManager.h; path = "Bit Slicer/ZGDocumentLabelManager.h"; sourceTree = "<group>"; };
307+
4E1220AC297C747000E3166F /* ZGDocumentLabelManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = ZGDocumentLabelManager.m; path = "Bit Slicer/ZGDocumentLabelManager.m"; sourceTree = "<group>"; };
308+
4E1220AE297D432D00E3166F /* ZGLabel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = ZGLabel.m; path = "Bit Slicer/ZGLabel.m"; sourceTree = "<group>"; };
309+
4E1220B0297D433700E3166F /* ZGLabel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = ZGLabel.h; path = "Bit Slicer/ZGLabel.h"; sourceTree = "<group>"; };
304310
72030ADA1B00191400A5621C /* ZGScriptingInterpreter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ZGScriptingInterpreter.h; path = "Bit Slicer/ZGScriptingInterpreter.h"; sourceTree = "<group>"; };
305311
72030ADB1B00191400A5621C /* ZGScriptingInterpreter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ZGScriptingInterpreter.m; path = "Bit Slicer/ZGScriptingInterpreter.m"; sourceTree = "<group>"; };
306312
720844F824E0715C00F3D449 /* DataInspector.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = DataInspector.h; path = deps/HexFiend/DataInspector.h; sourceTree = "<group>"; };
@@ -1324,6 +1330,8 @@
13241330
77FFC104186BDA3600122357 /* ZGWatchVariableWindowController.h */,
13251331
77FFC105186BDA3600122357 /* ZGWatchVariableWindowController.m */,
13261332
7739728017D26A1600B958EE /* Scripting */,
1333+
4E1220AB297C745900E3166F /* ZGDocumentLabelManager.h */,
1334+
4E1220AC297C747000E3166F /* ZGDocumentLabelManager.m */,
13271335
);
13281336
name = Documents;
13291337
sourceTree = "<group>";
@@ -1461,6 +1469,8 @@
14611469
77F1162B17D2EF50009E002A /* ZGVariable.m */,
14621470
7231440619C607C700FBE342 /* ZGWatchVariable.h */,
14631471
7231440719C607C700FBE342 /* ZGWatchVariable.m */,
1472+
4E1220AE297D432D00E3166F /* ZGLabel.m */,
1473+
4E1220B0297D433700E3166F /* ZGLabel.h */,
14641474
);
14651475
name = Models;
14661476
sourceTree = "<group>";
@@ -1701,6 +1711,7 @@
17011711
770F284218832132001B828C /* ZGBreakPointCondition.m in Sources */,
17021712
77F115D617D2ED8E009E002A /* ZGLineCountingRepresenter.m in Sources */,
17031713
772D1268190E0D8600800B1B /* ZGHotKeyPreferencesViewController.m in Sources */,
1714+
4E1220AD297C747000E3166F /* ZGDocumentLabelManager.m in Sources */,
17041715
771A931D18791A8D0098511D /* ZGTableView.m in Sources */,
17051716
77F115D917D2ED9D009E002A /* ZGLineCountingView.m in Sources */,
17061717
77607AB1185CD50100AC532B /* AGScopeBar.m in Sources */,
@@ -1791,6 +1802,7 @@
17911802
77F1165917D2F001009E002A /* ZGRegister.m in Sources */,
17921803
774B5EE61915361400D7AA69 /* ZGPyKeyCodeModule.m in Sources */,
17931804
7231440819C607C700FBE342 /* ZGWatchVariable.m in Sources */,
1805+
4E1220AF297D432D00E3166F /* ZGLabel.m in Sources */,
17941806
77179F6B18CD34F100BCB6D0 /* ZGAppUpdaterController.m in Sources */,
17951807
77F1165C17D2F00A009E002A /* ZGCodeInjectionWindowController.m in Sources */,
17961808
77CE28B717D538E300998F28 /* ZGLoggerWindowController.m in Sources */,

Bit Slicer/ZGCalculator.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#import <Foundation/Foundation.h>
3434
#import "ZGVariable.h"
35+
#import "ZGDocumentLabelManager.h"
3536

3637
@class ZGProcess;
3738

@@ -41,6 +42,7 @@ NS_ASSUME_NONNULL_BEGIN
4142

4243
@interface ZGVariable (ZGCalculatorAdditions)
4344

45+
@property (nonatomic, readonly) BOOL usesDynamicLabelAddress;
4446
@property (nonatomic, readonly) BOOL usesDynamicPointerAddress;
4547
@property (nonatomic, readonly) BOOL usesDynamicBaseAddress;
4648

@@ -54,7 +56,7 @@ NS_ASSUME_NONNULL_BEGIN
5456

5557
// Can evaluate [address] + [address2] + offset, [address + [address2 - [address3]]] + offset, etc...
5658
// And also has a base() function that takes in a string, and returns the first address to a region such that the passed string partially matches the end of the corresponding region's mapped path
57-
+ (nullable NSString *)evaluateExpression:(NSString *)expression process:(ZGProcess *)process failedImages:(nullable NSMutableArray<NSString *> *)failedImages error:(NSError **)error;
59+
+ (nullable NSString *)evaluateExpression:(NSString *)expression documentLabelManager:(ZGDocumentLabelManager *)documentLabelManager process:(ZGProcess *)process failedImages:(nullable NSMutableArray<NSString *> *)failedImages error:(NSError **)error;
5860
+ (nullable NSString *)evaluateAndSymbolicateExpression:(NSString *)expression process:(ZGProcess *)process currentAddress:(ZGMemoryAddress)currentAddress didSymbolicate:(nullable BOOL *)didSymbolicate error:(NSError **)error;
5961

6062
@end

Bit Slicer/ZGCalculator.m

+76-5
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,12 @@
3232

3333
#import "ZGCalculator.h"
3434
#import "NSStringAdditions.h"
35+
#import "ZGDocumentLabelManager.h"
3536
#import "ZGVirtualMemory.h"
3637
#import "ZGMachBinary.h"
3738
#import "ZGMachBinaryInfo.h"
3839
#import "ZGRegion.h"
40+
#import "ZGLabel.h"
3941
#import "ZGProcess.h"
4042
#pragma clang diagnostic push
4143
#pragma clang diagnostic ignored "-Wincomplete-umbrella"
@@ -49,17 +51,24 @@
4951

5052
#define ZGCalculatePointerFunction @"ZGCalculatePointerFunction"
5153
#define ZGFindSymbolFunction @"symbol"
54+
#define ZGFindLabelFunction @"label"
5255
#define ZGProcessVariable @"ZGProcessVariable"
56+
#define ZGDocumentLabelManagerVariable @"ZGDocumentLabelManagerVariable"
5357
#define ZGFailedImagesVariable @"ZGFailedImagesVariable"
5458
#define ZGSymbolicatesVariable @"ZGSymbolicatesVariable"
5559
#define ZGDidFindSymbol @"ZGDidFindSymbol"
5660
#define ZGLastSearchInfoVariable @"ZGLastSearchInfoVariable"
5761

5862
@implementation ZGVariable (ZGCalculatorAdditions)
5963

64+
- (BOOL)usesDynamicLabelAddress
65+
{
66+
return _addressFormula != nil && [_addressFormula rangeOfString:ZGFindLabelFunction].location != NSNotFound;
67+
}
68+
6069
- (BOOL)usesDynamicPointerAddress
6170
{
62-
return _addressFormula != nil && [_addressFormula rangeOfString:@"["].location != NSNotFound && [_addressFormula rangeOfString:@"]"].location != NSNotFound;
71+
return _addressFormula != nil && ([_addressFormula rangeOfString:@"["].location != NSNotFound && [_addressFormula rangeOfString:@"]"].location != NSNotFound);
6372
}
6473

6574
- (BOOL)usesDynamicBaseAddress
@@ -236,6 +245,62 @@ + (DDMathFunction)registerFindSymbolFunctionWithEvaluator:(DDMathEvaluator *)eva
236245
return findSymbolFunction;
237246
}
238247

248+
+ (DDMathFunction)registerFindLabelFunctionWithEvaluator:(DDMathEvaluator *)evaluator
249+
{
250+
DDMathFunction findLabelFunction = ^DDExpression *(NSArray<DDExpression *> *args, NSDictionary<NSString *, id> *vars, DDMathEvaluator * __unused eval, NSError *__autoreleasing *error) {
251+
ZGDocumentLabelManager *documentLabelManager = [vars objectForKey:ZGDocumentLabelManagerVariable];
252+
253+
__block NSNumber *labelAddressNumber = @(0);
254+
255+
if (args.count == 0 || args.count > 1)
256+
{
257+
if (error != NULL)
258+
{
259+
*error = [NSError errorWithDomain:DDMathParserErrorDomain code:DDErrorCodeInvalidNumberOfArguments userInfo:@{NSLocalizedDescriptionKey:ZGFindLabelFunction @" expects 1 argument"}];
260+
}
261+
}
262+
else if (documentLabelManager == nil || [documentLabelManager.labels count] == 0)
263+
{
264+
if (error != NULL)
265+
{
266+
*error = [NSError errorWithDomain:DDMathParserErrorDomain code:DDErrorCodeUnresolvedVariable userInfo:@{NSLocalizedDescriptionKey:ZGFindLabelFunction @" expects a labels variable"}];
267+
}
268+
}
269+
else
270+
{
271+
DDExpression *labelExpression = [args objectAtIndex:0];
272+
273+
if (labelExpression.expressionType != DDExpressionTypeVariable)
274+
{
275+
if (error != NULL)
276+
{
277+
*error = [NSError errorWithDomain:DDMathParserErrorDomain code:DDErrorCodeUnresolvedVariable userInfo:@{NSLocalizedDescriptionKey:ZGFindLabelFunction @" expects a string variable"}];
278+
}
279+
}
280+
else
281+
{
282+
NSString *labelString = labelExpression.variable;
283+
ZGLabel *label = [documentLabelManager.labels objectForKey:labelString];
284+
285+
labelAddressNumber = @(label.address);
286+
if (labelAddressNumber == nil)
287+
{
288+
if (error != NULL)
289+
{
290+
*error = [NSError errorWithDomain:DDMathParserErrorDomain code:DDErrorCodeInvalidArgument userInfo:@{NSLocalizedDescriptionKey:ZGFindLabelFunction @" could not find requested label"}];
291+
}
292+
}
293+
}
294+
}
295+
296+
return [DDExpression numberExpressionWithNumber:labelAddressNumber];
297+
};
298+
299+
[evaluator registerFunction:findLabelFunction forName:ZGFindLabelFunction];
300+
301+
return findLabelFunction;
302+
}
303+
239304
+ (void)registerFunctionResolverWithEvaluator:(DDMathEvaluator *)evaluator findSymbolFunction:(DDMathFunction)findSymbolFunction
240305
{
241306
evaluator.functionResolver = (DDFunctionResolver)^(NSString *name) {
@@ -262,6 +327,7 @@ + (void)initialize
262327
DDMathEvaluator *evaluator = [DDMathEvaluator defaultMathEvaluator];
263328
[self registerCalculatePointerFunctionWithEvaluator:evaluator];
264329
[self registerBaseAddressFunctionWithEvaluator:evaluator];
330+
[self registerFindLabelFunctionWithEvaluator:evaluator];
265331
DDMathFunction findSymbolFunction = [self registerFindSymbolFunctionWithEvaluator:evaluator];
266332
[self registerFunctionResolverWithEvaluator:evaluator findSymbolFunction:findSymbolFunction];
267333
});
@@ -479,12 +545,17 @@ + (NSString *)expressionBySubstitutingCalculatePointerFunctionInExpression:(NSSt
479545
return [[NSString alloc] initWithData:newData encoding:NSUTF8StringEncoding];
480546
}
481547

482-
+ (NSString *)evaluateExpression:(NSString *)expression process:(ZGProcess * __unsafe_unretained)process failedImages:(NSMutableArray<NSString *> * __unsafe_unretained)failedImages symbolicates:(BOOL)symbolicates foundSymbol:(BOOL *)foundSymbol currentAddress:(ZGMemoryAddress)currentAddress error:(NSError * __autoreleasing *)error
548+
+ (NSString *)evaluateExpression:(NSString *)expression documentLabelManager:(ZGDocumentLabelManager * __unsafe_unretained)documentLabelManager process:(ZGProcess * __unsafe_unretained)process failedImages:(NSMutableArray<NSString *> * __unsafe_unretained)failedImages symbolicates:(BOOL)symbolicates foundSymbol:(BOOL *)foundSymbol currentAddress:(ZGMemoryAddress)currentAddress error:(NSError * __autoreleasing *)error
483549
{
484550
NSString *newExpression = [self expressionBySubstitutingCalculatePointerFunctionInExpression:expression];
485551

486552
NSMutableDictionary<NSString *, id> *substitutions = [NSMutableDictionary dictionaryWithDictionary:@{ZGProcessVariable : process, ZGSymbolicatesVariable : @(symbolicates), ZGLastSearchInfoVariable : @(currentAddress), ZGDidFindSymbol : @(NO)}];
487553

554+
if (documentLabelManager != nil)
555+
{
556+
[substitutions setObject:documentLabelManager forKey:ZGDocumentLabelManagerVariable];
557+
}
558+
488559
if (failedImages != nil)
489560
{
490561
[substitutions setObject:failedImages forKey:ZGFailedImagesVariable];
@@ -501,12 +572,12 @@ + (NSString *)evaluateExpression:(NSString *)expression process:(ZGProcess * __u
501572

502573
+ (NSString *)evaluateAndSymbolicateExpression:(NSString *)expression process:(ZGProcess * __unsafe_unretained)process currentAddress:(ZGMemoryAddress)currentAddress didSymbolicate:(BOOL *)didSymbolicate error:(NSError * __autoreleasing *)error
503574
{
504-
return [self evaluateExpression:expression process:process failedImages:nil symbolicates:YES foundSymbol:didSymbolicate currentAddress:currentAddress error:error];
575+
return [self evaluateExpression:expression documentLabelManager:NULL process:process failedImages:nil symbolicates:YES foundSymbol:didSymbolicate currentAddress:currentAddress error:error];
505576
}
506577

507-
+ (NSString *)evaluateExpression:(NSString *)expression process:(ZGProcess * __unsafe_unretained)process failedImages:(NSMutableArray<NSString *> * __unsafe_unretained)failedImages error:(NSError * __autoreleasing *)error
578+
+ (NSString *)evaluateExpression:(NSString *)expression documentLabelManager:(ZGDocumentLabelManager * __unsafe_unretained)documentLabelManager process:(ZGProcess * __unsafe_unretained)process failedImages:(NSMutableArray<NSString *> * __unsafe_unretained)failedImages error:(NSError * __autoreleasing *)error
508579
{
509-
return [self evaluateExpression:expression process:process failedImages:failedImages symbolicates:NO foundSymbol:NULL currentAddress:0x0 error:error];
580+
return [self evaluateExpression:expression documentLabelManager:documentLabelManager process:process failedImages:failedImages symbolicates:NO foundSymbol:NULL currentAddress:0x0 error:error];
510581
}
511582

512583
@end

Bit Slicer/ZGDocument.m

+17
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#import "ZGDocumentData.h"
3636
#import "ZGSearchData.h"
3737
#import "ZGVariable.h"
38+
#import "ZGLabel.h"
3839
#import "ZGScriptManager.h"
3940

4041
@implementation ZGDocument
@@ -90,13 +91,19 @@ - (NSFileWrapper *)fileWrapperOfType:(NSString *)__unused typeName error:(NSErro
9091
NSKeyedArchiver *keyedArchiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:writeData];
9192

9293
NSArray<ZGVariable *> *watchVariablesArrayToSave = nil;
94+
NSArray<ZGLabel *> *labelsArrayToSave = nil;
9395

9496
watchVariablesArrayToSave = _data.variables;
97+
labelsArrayToSave = _data.labels;
9598

9699
[keyedArchiver
97100
encodeObject:watchVariablesArrayToSave
98101
forKey:ZGWatchVariablesArrayKey];
99102

103+
[keyedArchiver
104+
encodeObject:labelsArrayToSave
105+
forKey:ZGLabelsArrayKey];
106+
100107
[keyedArchiver
101108
encodeObject:_data.desiredProcessInternalName != nil ? _data.desiredProcessInternalName : [NSNull null]
102109
forKey:ZGProcessInternalNameKey];
@@ -186,6 +193,16 @@ - (BOOL)readFromFileWrapper:(NSFileWrapper *)fileWrapper ofType:(NSString *)__un
186193
_data.variables = [NSArray array];
187194
}
188195

196+
NSArray<ZGLabel *> *newLabels = [keyedUnarchiver decodeObjectOfClass:[NSArray class] forKey:ZGLabelsArrayKey];
197+
if (newLabels != nil)
198+
{
199+
_data.labels = newLabels;
200+
}
201+
else
202+
{
203+
_data.labels = [NSArray array];
204+
}
205+
189206
id desiredProcessInternalName = [keyedUnarchiver decodeObjectOfClass:[NSObject class] forKey:ZGProcessInternalNameKey];
190207
if (desiredProcessInternalName == [NSNull null] || ![(id<NSObject>)desiredProcessInternalName isKindOfClass:[NSString class]])
191208
{

Bit Slicer/ZGDocumentData.h

+3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#import <Foundation/Foundation.h>
3434

3535
#define ZGWatchVariablesArrayKey @"ZGWatchVariablesArrayKey"
36+
#define ZGLabelsArrayKey @"ZGLabelsArrayKey"
3637
#define ZGProcessInternalNameKey @"ZGProcessNameKey"
3738

3839
#define ZGSelectedDataTypeTag @"ZGSelectedDataTypeTag"
@@ -55,6 +56,7 @@
5556
NS_ASSUME_NONNULL_BEGIN
5657

5758
@class ZGVariable;
59+
@class ZGLabel;
5860

5961
@interface ZGDocumentData : NSObject
6062

@@ -67,6 +69,7 @@ NS_ASSUME_NONNULL_BEGIN
6769
@property (copy, nonatomic) NSString *endingAddressStringValue;
6870
@property (copy, nonatomic) NSString *searchValue;
6971
@property (nonatomic) NSArray<ZGVariable *> *variables;
72+
@property (nonatomic) NSArray<ZGLabel *> *labels;
7073
@property (copy, nonatomic, nullable) NSString *desiredProcessInternalName;
7174
@property (copy, nonatomic) NSString *lastEpsilonValue;
7275
@property (copy, nonatomic, nullable) NSString *lastAboveRangeValue;

Bit Slicer/ZGDocumentData.m

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#import "ZGSearchData.h"
3636
#import "ZGSearchFunctions.h"
3737
#import "ZGVariable.h"
38+
#import "ZGLabel.h"
3839

3940
@implementation ZGDocumentData
4041

@@ -44,6 +45,7 @@ - (id)init
4445
if (self != nil)
4546
{
4647
_variables = [[NSArray alloc] init];
48+
_labels = [[NSArray alloc] init];
4749
_beginningAddressStringValue = @"";
4850
_endingAddressStringValue = @"";
4951
_searchValue = @"";

Bit Slicer/ZGDocumentLabelManager.h

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2013 Mayur Pawashe
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions
7+
* are met:
8+
*
9+
* Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
*
12+
* Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* Neither the name of the project's author nor the names of its
17+
* contributors may be used to endorse or promote products derived from
18+
* this software without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26+
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
#import <Foundation/Foundation.h>
34+
35+
@class ZGLabel;
36+
@class ZGDocumentData;
37+
38+
@interface ZGDocumentLabelManager : NSObject
39+
40+
NS_ASSUME_NONNULL_BEGIN
41+
42+
@property (nonatomic, readonly) NSMutableDictionary<NSString *, ZGLabel *> *labels;
43+
44+
- (id)initWithDocumentData:(ZGDocumentData *)documentData;
45+
46+
- (void)addLabel:(ZGLabel *)label;
47+
- (void)removeLabel:(NSString *)name;
48+
49+
@end
50+
51+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)