Skip to content

Commit b34ce6f

Browse files
committed
wip
1 parent 58309a4 commit b34ce6f

File tree

10 files changed

+46
-19
lines changed

10 files changed

+46
-19
lines changed

objectivec/objc.cpp

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,12 +1509,22 @@ void ObjCProcessor::ProcessCFStrings(std::optional<std::string> imageName)
15091509
uint8_t* rawData = static_cast<uint8_t*>(data.GetData());
15101510
uint8_t* offsetAddress = rawData + bufferOff;
15111511
uint16_t c = *reinterpret_cast<uint16_t*>(offsetAddress);
1512-
if (c == 0x20)
1512+
if (c == 0x20) {
15131513
str.push_back('_');
1514-
else if (c < 0x80)
1514+
} else if (c == '\r') {
1515+
str.push_back('\\');
1516+
str.push_back('r');
1517+
} else if (c == '\n') {
1518+
str.push_back('\\');
1519+
str.push_back('n');
1520+
} else if (c == '\t') {
1521+
str.push_back('\\');
1522+
str.push_back('t');
1523+
} else if (c > 0x20 && c < 0x80) {
15151524
str.push_back(c);
1516-
else
1525+
} else {
15171526
str.push_back('?');
1527+
}
15181528
}
15191529
DefineObjCSymbol(
15201530
DataSymbol, Type::ArrayType(Type::WideCharType(2), size + 1), "ustr_" + str, strLoc, true);
@@ -1524,11 +1534,26 @@ void ObjCProcessor::ProcessCFStrings(std::optional<std::string> imageName)
15241534
else // UTF8 / ASCII
15251535
{
15261536
reader->Seek(strLoc);
1527-
str = reader->ReadCString();
1528-
for (auto& c : str)
1537+
std::string rawStr = reader->ReadCString(size + 1);
1538+
str = "";
1539+
for (signed char c : rawStr)
15291540
{
1530-
if (c == ' ')
1531-
c = '_';
1541+
if (c == 0x20) {
1542+
str.push_back('_');
1543+
} else if (c == '\r') {
1544+
str.push_back('\\');
1545+
str.push_back('r');
1546+
} else if (c == '\n') {
1547+
str.push_back('\\');
1548+
str.push_back('n');
1549+
} else if (c == '\t') {
1550+
str.push_back('\\');
1551+
str.push_back('t');
1552+
} else if (c > 0x20 || c < 0) {
1553+
str.push_back(c);
1554+
} else {
1555+
str.push_back('?');
1556+
}
15321557
}
15331558
DefineObjCSymbol(DataSymbol, Type::ArrayType(Type::IntegerType(1, true), str.size() + 1), "cstr_" + str,
15341559
strLoc, true);

objectivec/objc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ namespace BinaryNinja {
166166
\throws Exception
167167
\return the string
168168
*/
169-
virtual std::string ReadCString() = 0;
169+
virtual std::string ReadCString(size_t maxLength = -1) = 0;
170170

171171
/*! Read a uint8_t from the current cursor position and advance the cursor by 1 byte
172172

view/macho/objc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ void MachoObjCReader::Read(void* dest, size_t len)
1111
m_reader.Read(dest, len);
1212
}
1313

14-
std::string MachoObjCReader::ReadCString()
14+
std::string MachoObjCReader::ReadCString(size_t maxLength)
1515
{
16-
return m_reader.ReadCString();
16+
return m_reader.ReadCString(maxLength);
1717
}
1818

1919
uint8_t MachoObjCReader::Read8()

view/macho/objc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace BinaryNinja {
1010

1111
public:
1212
void Read(void* dest, size_t len) override;
13-
std::string ReadCString() override;
13+
std::string ReadCString(size_t maxLength = -1) override;
1414
uint8_t Read8() override;
1515
uint16_t Read16() override;
1616
uint32_t Read32() override;

view/sharedcache/core/MappedFileAccessor.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ void MappedFileAccessor::WritePointer(size_t address, size_t pointer)
1717
*reinterpret_cast<size_t *>(&m_file._mmap[address]) = pointer;
1818
}
1919

20-
std::string MappedFileAccessor::ReadNullTermString(size_t address) const
20+
std::string MappedFileAccessor::ReadNullTermString(size_t address, size_t maxLength) const
2121
{
2222
if (address > Length())
2323
return "";
2424
auto start = m_file._mmap + address;
25-
auto end = m_file._mmap + m_file.len;
25+
auto endLen = (maxLength > 0) ? maxLength : m_file.len;
26+
auto end = start + endLen;
2627
auto nul = std::find(start, end, 0);
2728
return {start, nul};
2829
}

view/sharedcache/core/MappedFileAccessor.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class MappedFileAccessor : public std::enable_shared_from_this<MappedFileAccesso
5151
*/
5252
void WritePointer(size_t address, size_t pointer);
5353

54-
std::string ReadNullTermString(size_t address) const;
54+
std::string ReadNullTermString(size_t address, size_t maxLength = -1) const;
5555

5656
uint8_t ReadUInt8(size_t address);
5757

view/sharedcache/core/ObjC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ void SharedCacheObjCReader::Read(void* dest, size_t len)
1414
m_reader.Read(dest, len);
1515
}
1616

17-
std::string SharedCacheObjCReader::ReadCString()
17+
std::string SharedCacheObjCReader::ReadCString(size_t maxLength)
1818
{
19-
return m_reader.ReadCString(m_reader.GetOffset());
19+
return m_reader.ReadCString(m_reader.GetOffset(), maxLength);
2020
}
2121

2222
uint8_t SharedCacheObjCReader::Read8()

view/sharedcache/core/ObjC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace DSCObjC {
2222

2323
public:
2424
void Read(void* dest, size_t len) override;
25-
std::string ReadCString() override;
25+
std::string ReadCString(size_t maxLength = -1) override;
2626
uint8_t Read8() override;
2727
uint16_t Read16() override;
2828
uint32_t Read32() override;

view/sharedcache/core/VirtualMemory.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ VirtualMemoryReader::VirtualMemoryReader(std::shared_ptr<VirtualMemory> memory,
151151
{
152152
m_memory = memory;
153153
m_addressSize = addressSize;
154+
m_cursor = 0;
154155
}
155156

156-
std::string VirtualMemoryReader::ReadCString(uint64_t address)
157+
std::string VirtualMemoryReader::ReadCString(uint64_t address, size_t maxLength)
157158
{
158159
uint64_t offset;
159160
auto region = m_memory->GetRegionAtAddress(address, offset);

view/sharedcache/core/VirtualMemory.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ class VirtualMemoryReader
8989

9090
size_t GetOffset() const { return m_cursor; }
9191

92-
std::string ReadCString(uint64_t address);
92+
std::string ReadCString(uint64_t address, size_t maxLength = -1);
9393

9494
uint64_t ReadULEB128(size_t cursorLimit);
9595

0 commit comments

Comments
 (0)