Skip to content

Commit

Permalink
[MachO Parser] Format Size (qyang-nj#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
qyang-nj committed May 22, 2023
1 parent 070f216 commit 90c7908
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 24 deletions.
17 changes: 7 additions & 10 deletions macho_parser/sources/segment_64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "argument.h"
#include "util.h"
#include "utils/utils.h"
#include "load_command.h"
#include "symtab.h"

Expand All @@ -29,16 +30,13 @@ void printSegment(uint8_t *base, struct segment_command_64 *segCmd, int firstSec
return;
}

char formatted_filesize[16];
char formatted_vmsize[16];

format_size(segCmd->filesize, formatted_filesize);
format_size(segCmd->vmsize, formatted_vmsize);
auto formattedFileSize = formatSize(segCmd->filesize);
auto formattedVMSize = formatSize(segCmd->vmsize);

printf("%-20s cmdsize: %-6d segname: %-12.16s file: 0x%08llx-0x%08llx %-9s vm: 0x%09llx-0x%09llx %-9s prot: %d/%d\n",
"LC_SEGMENT_64", segCmd->cmdsize, segCmd->segname,
segCmd->fileoff, segCmd->fileoff + segCmd->filesize, formatted_filesize,
segCmd->vmaddr, segCmd->vmaddr + segCmd->vmsize, formatted_vmsize,
segCmd->fileoff, segCmd->fileoff + segCmd->filesize, formattedFileSize.c_str(),
segCmd->vmaddr, segCmd->vmaddr + segCmd->vmsize, formattedVMSize.c_str(),
segCmd->initprot, segCmd->maxprot);

if (args.verbosity < 1) {
Expand Down Expand Up @@ -69,16 +67,15 @@ static bool hasSectionToShow(struct segment_command_64 *segCmd, int firstSection

static void printSection(uint8_t *base, struct section_64 sect, int sectionIndex) {
char formattedSegSec[64];
char formattedSize[16];

const uint8_t type = sect.flags & SECTION_TYPE;

auto formattedType = formatSectionType(type);
snprintf(formattedSegSec, sizeof(formattedSegSec), "(%.16s,%.16s)", sect.segname, sect.sectname);
format_size(sect.size, formattedSize);
auto formattedSize = formatSize(sect.size);

printf(" %2d: 0x%09x-0x%09llx %-11s %-32s type: %s offset: %d",
sectionIndex, sect.offset, sect.offset + sect.size, formattedSize, formattedSegSec, formattedType.c_str(), sect.offset);
sectionIndex, sect.offset, sect.offset + sect.size, formattedSize.c_str(), formattedSegSec, formattedType.c_str(), sect.offset);

if (sect.reserved1 > 0) {
printf(" reserved1: %2d", sect.reserved1);
Expand Down
12 changes: 0 additions & 12 deletions macho_parser/sources/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,3 @@ void format_hex(void *buffer, size_t size, char *formatted) {
sprintf(formatted + i * 2, "%02x", *((uint8_t *)buffer + i));
}
}

void format_size(uint64_t size_in_byte, char *formatted) {
if (size_in_byte < 1024) {
sprintf(formatted, "%lluB", size_in_byte);
} else if (size_in_byte / 1024 < 1024) {
sprintf(formatted, "%.2fKB", (double)size_in_byte / 1024 );
} else if (size_in_byte / 1024 / 1204 < 1024) {
sprintf(formatted, "%.2fMB", (double)size_in_byte / 1024 / 1024);
} else {
sprintf(formatted, "%.2fGB", (double)size_in_byte / 1024 / 1024 / 1024);
}
}
2 changes: 0 additions & 2 deletions macho_parser/sources/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ void format_string(char *str, char *formatted);
// Hex dump a binary buffer.
void format_hex(void *buffer, size_t size, char *formatted);

void format_size(uint64_t size_in_byte, char *formatted);

#ifdef __cplusplus
}
#endif
Expand Down
21 changes: 21 additions & 0 deletions macho_parser/sources/utils/formatting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iomanip>
#include <sstream>

std::string formatSize(uint64_t sizeInByte) {
std::stringstream sstream;

if (sizeInByte < 1024) {
sstream << sizeInByte << "B";
} else {
sstream << std::fixed << std::setprecision(2);
if (sizeInByte / 1024 < 1024) {
sstream << (double)sizeInByte / 1024 << "KB";
} else if (sizeInByte / 1024 / 1024 < 1024) {
sstream << (double)sizeInByte / 1024 / 1024 << "MB";
} else {
sstream << (double)sizeInByte / 1024 / 1024 / 1024 << "GB";
}
}

return sstream.str();
}
9 changes: 9 additions & 0 deletions macho_parser/sources/utils/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,13 @@ void decompressZlibData(const uint8_t *inputData, size_t inputSize, uint8_t *out
#endif


#ifdef __cplusplus

#include <string>
// formatting
std::string formatSize(uint64_t sizeInByte);

#endif


#endif //MACHO_PARSER_UTILS_H
14 changes: 14 additions & 0 deletions macho_parser/tests/formatting.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <gtest/gtest.h>
#include "utils/utils.h"

TEST(FormatSize, ALL) {
EXPECT_EQ(formatSize(0), "0B");
EXPECT_EQ(formatSize(128), "128B");
EXPECT_EQ(formatSize(1023), "1023B");
EXPECT_EQ(formatSize(1024), "1.00KB");
EXPECT_EQ(formatSize(1024 + 102), "1.10KB");
EXPECT_EQ(formatSize(1024 * 1024), "1.00MB");
EXPECT_EQ(formatSize(1024 * 1024 + 10), "1.00MB");
EXPECT_EQ(formatSize(1024 * 1024 + 1024 * 10), "1.01MB");
EXPECT_EQ(formatSize(1024 * 1024 * 1024), "1.00GB");
}

0 comments on commit 90c7908

Please sign in to comment.