Skip to content

Commit

Permalink
Import hexdec from upstream to replace hex2int
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbinit committed May 12, 2023
1 parent 457dae4 commit 59ba4da
Showing 1 changed file with 34 additions and 40 deletions.
74 changes: 34 additions & 40 deletions MachOLayout.mm
Original file line number Diff line number Diff line change
Expand Up @@ -193,55 +193,49 @@ - (void)addRelocAtFileOffset:(uint64_t)offset withLength:(uint64_t)length andVal
}

// ----------------------------------------------------------------------------
static inline
uint32_t
_hex2int(char const * a, uint32_t len)

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winitializer-overrides"
static const long hextable[] =
{
uint32_t val = 0;

for(uint32_t i = 0; i < len; i++)
{
if(a[i] <= '9')
{
val += (a[i]-'0')*(1<<(4*(len-1-i)));
}
else
{
val += (a[i]-'7')*(1<<(4*(len-1-i)));
[0 ... 255] = -1, // bit aligned access into this table is considerably
['0'] = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, // faster for most modern processors,
['A'] = 10, 11, 12, 13, 14, 15, // for the space conscious, reduce to
['a'] = 10, 11, 12, 13, 14, 15 // signed char.
};
#pragma clang diagnostic pop

/**
* @brief convert a hexidecimal string to a signed long
* will not produce or process negative numbers except
* to signal error.
*
* @param hex without decoration, case insensitive.
*
* @return -1 on error, or result (max (sizeof(long)*8)-1 bits)
*/
static inline
long hexdec(const char *hex) {
long ret = 0;
while (*hex && ret >= 0) {
ret = (ret << 4) | hextable[*hex++];
}
}

return val;
return ret;
}

// ----------------------------------------------------------------------------
// RAW string to RVA string converter for data source
- (NSString *)convertToRVA: (NSString *)offsetStr
{
uint32_t fileOffset;

/*
BOOL scanResult = [[NSScanner scannerWithString:offsetStr] scanHexInt:&fileOffset];
// return empty string if it is out of bounds
if (scanResult == NO || segmentInfo.empty() ||
fileOffset < segmentInfo.begin()->first ||
fileOffset + 1 >= (--segmentInfo.end())->first + (--segmentInfo.end())->second.second)
{
return @"";
}
*/
// _hex2int is supposed to be must faster
// note: on problems use the traditional scanner!

fileOffset = _hex2int(CSTRING(offsetStr), [offsetStr length]);
uint64_t fileOffset = hexdec(CSTRING(offsetStr));
NSParameterAssert((long)fileOffset != -1);

if (segmentInfo.empty() ||
fileOffset < segmentInfo.begin()->first ||
fileOffset + 1 >= (--segmentInfo.end())->first + (--segmentInfo.end())->second.second)
{
return @"";
}
if (segmentInfo.empty() ||
fileOffset < segmentInfo.begin()->first ||
fileOffset + 1 >= (--segmentInfo.end())->first + (--segmentInfo.end())->second.second)
{
return @"";
}

return [NSString stringWithFormat:@"%.8qX",[self fileOffsetToRVA:fileOffset]];
}
Expand Down

0 comments on commit 59ba4da

Please sign in to comment.