mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 17:41:41 +01:00
Chore: run clang-format
```bash find . -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.hpp" \) ! -path "src/3rdparty/*" -print0 | xargs -0 clang-format -i ```
This commit is contained in:
@@ -17,8 +17,7 @@
|
||||
/**
|
||||
* Helper function to read data from a file at a specific offset
|
||||
*/
|
||||
static inline bool readData(FILE *objFile, void *buf, size_t size, off_t offset)
|
||||
{
|
||||
static inline bool readData(FILE* objFile, void* buf, size_t size, off_t offset) {
|
||||
fseek(objFile, offset, SEEK_SET);
|
||||
return fread(buf, 1, size, objFile) == size;
|
||||
}
|
||||
@@ -36,23 +35,29 @@ static inline bool readData(FILE *objFile, void *buf, size_t size, off_t offset)
|
||||
*
|
||||
* @return true to continue processing, false to stop
|
||||
*/
|
||||
static bool handleMachSection(FILE *objFile, const char *name, off_t offset, size_t size, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata, uint32_t minLength)
|
||||
{
|
||||
if (!ffStrEquals(name, "__cstring")) return true;
|
||||
static bool handleMachSection(FILE* objFile, const char* name, off_t offset, size_t size, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata, uint32_t minLength) {
|
||||
if (!ffStrEquals(name, "__cstring")) {
|
||||
return true;
|
||||
}
|
||||
|
||||
FF_AUTO_FREE char* data = (char*) malloc(size);
|
||||
if (!readData(objFile, data, size, offset))
|
||||
if (!readData(objFile, data, size, offset)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (size_t off = 0; off < size; ++off)
|
||||
{
|
||||
for (size_t off = 0; off < size; ++off) {
|
||||
const char* p = (const char*) data + off;
|
||||
if (*p == '\0') continue;
|
||||
if (*p == '\0') {
|
||||
continue;
|
||||
}
|
||||
uint32_t len = (uint32_t) strlen(p);
|
||||
if (len < minLength) continue;
|
||||
if (*p >= ' ' && *p <= '~') // Ignore control characters
|
||||
{
|
||||
if (!cb(p, len, userdata)) return false;
|
||||
if (len < minLength) {
|
||||
continue;
|
||||
}
|
||||
if (*p >= ' ' && *p <= '~') { // Ignore control characters
|
||||
if (!cb(p, len, userdata)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
off += len;
|
||||
}
|
||||
@@ -75,25 +80,23 @@ static bool handleMachSection(FILE *objFile, const char *name, off_t offset, siz
|
||||
*
|
||||
* @return NULL on success, error message on failure
|
||||
*/
|
||||
static const char* dumpMachHeader(FILE *objFile, off_t offset, bool is_64, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata, uint32_t minLength)
|
||||
{
|
||||
static const char* dumpMachHeader(FILE* objFile, off_t offset, bool is_64, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata, uint32_t minLength) {
|
||||
uint32_t ncmds;
|
||||
off_t loadCommandsOffset = offset;
|
||||
|
||||
if (is_64)
|
||||
{
|
||||
if (is_64) {
|
||||
struct mach_header_64 header;
|
||||
if (!readData(objFile, &header, sizeof(header), offset))
|
||||
if (!readData(objFile, &header, sizeof(header), offset)) {
|
||||
return "read mach header failed";
|
||||
}
|
||||
|
||||
ncmds = header.ncmds;
|
||||
loadCommandsOffset += sizeof(header);
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
struct mach_header header;
|
||||
if (!readData(objFile, &header, sizeof(header), offset))
|
||||
if (!readData(objFile, &header, sizeof(header), offset)) {
|
||||
return "read mach header failed";
|
||||
}
|
||||
|
||||
ncmds = header.ncmds;
|
||||
loadCommandsOffset += sizeof(header);
|
||||
@@ -101,45 +104,50 @@ static const char* dumpMachHeader(FILE *objFile, off_t offset, bool is_64, bool
|
||||
|
||||
off_t commandOffset = loadCommandsOffset;
|
||||
struct load_command cmd = {};
|
||||
for (uint32_t i = 0U; i < ncmds; i++, commandOffset += cmd.cmdsize)
|
||||
{
|
||||
if (!readData(objFile, &cmd, sizeof(cmd), commandOffset))
|
||||
for (uint32_t i = 0U; i < ncmds; i++, commandOffset += cmd.cmdsize) {
|
||||
if (!readData(objFile, &cmd, sizeof(cmd), commandOffset)) {
|
||||
continue;
|
||||
|
||||
if (cmd.cmd == LC_SEGMENT_64)
|
||||
{
|
||||
struct segment_command_64 segment;
|
||||
if (!readData(objFile, &segment, sizeof(segment), commandOffset))
|
||||
continue;
|
||||
|
||||
if (!ffStrEquals(segment.segname, "__TEXT")) continue;
|
||||
|
||||
for (uint32_t j = 0U; j < segment.nsects; j++)
|
||||
{
|
||||
struct section_64 section;
|
||||
if (!readData(objFile, §ion, sizeof(section), (off_t) ((size_t) commandOffset + sizeof(segment) + j * sizeof(section))))
|
||||
continue;
|
||||
|
||||
if (!handleMachSection(objFile, section.sectname, section.offset, section.size, cb, userdata, minLength))
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
else if (cmd.cmd == LC_SEGMENT)
|
||||
{
|
||||
struct segment_command segment;
|
||||
if (!readData(objFile, &segment, sizeof(segment), commandOffset))
|
||||
|
||||
if (cmd.cmd == LC_SEGMENT_64) {
|
||||
struct segment_command_64 segment;
|
||||
if (!readData(objFile, &segment, sizeof(segment), commandOffset)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ffStrEquals(segment.segname, "__TEXT")) continue;
|
||||
if (!ffStrEquals(segment.segname, "__TEXT")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (uint32_t j = 0; j < segment.nsects; j++)
|
||||
{
|
||||
struct section section;
|
||||
if (!readData(objFile, §ion, sizeof(section), (off_t) ((size_t) commandOffset + sizeof(segment) + j * sizeof(section))))
|
||||
for (uint32_t j = 0U; j < segment.nsects; j++) {
|
||||
struct section_64 section;
|
||||
if (!readData(objFile, §ion, sizeof(section), (off_t) ((size_t) commandOffset + sizeof(segment) + j * sizeof(section)))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!handleMachSection(objFile, section.sectname, section.offset, section.size, cb, userdata, minLength))
|
||||
if (!handleMachSection(objFile, section.sectname, section.offset, section.size, cb, userdata, minLength)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
} else if (cmd.cmd == LC_SEGMENT) {
|
||||
struct segment_command segment;
|
||||
if (!readData(objFile, &segment, sizeof(segment), commandOffset)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!ffStrEquals(segment.segname, "__TEXT")) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (uint32_t j = 0; j < segment.nsects; j++) {
|
||||
struct section section;
|
||||
if (!readData(objFile, §ion, sizeof(section), (off_t) ((size_t) commandOffset + sizeof(segment) + j * sizeof(section)))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!handleMachSection(objFile, section.sectname, section.offset, section.size, cb, userdata, minLength)) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,47 +171,49 @@ static const char* dumpMachHeader(FILE *objFile, off_t offset, bool is_64, bool
|
||||
*
|
||||
* @return NULL on success, error message on failure
|
||||
*/
|
||||
static const char* dumpFatHeader(FILE *objFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata, uint32_t minLength)
|
||||
{
|
||||
static const char* dumpFatHeader(FILE* objFile, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata, uint32_t minLength) {
|
||||
struct fat_header header;
|
||||
if (!readData(objFile, &header, sizeof(header), 0))
|
||||
if (!readData(objFile, &header, sizeof(header), 0)) {
|
||||
return "read fat header failed";
|
||||
}
|
||||
|
||||
bool needSwap = header.magic == FAT_CIGAM || header.magic == FAT_CIGAM_64;
|
||||
|
||||
if (needSwap) swap_fat_header(&header, NX_UnknownByteOrder);
|
||||
if (needSwap) {
|
||||
swap_fat_header(&header, NX_UnknownByteOrder);
|
||||
}
|
||||
|
||||
for (uint32_t i = 0U; i < header.nfat_arch; i++)
|
||||
{
|
||||
for (uint32_t i = 0U; i < header.nfat_arch; i++) {
|
||||
off_t machHeaderOffset = 0;
|
||||
if (header.magic == FAT_MAGIC)
|
||||
{
|
||||
if (header.magic == FAT_MAGIC) {
|
||||
struct fat_arch arch;
|
||||
if (!readData(objFile, &arch, sizeof(arch), (off_t) (sizeof(header) + i * sizeof(arch))))
|
||||
if (!readData(objFile, &arch, sizeof(arch), (off_t) (sizeof(header) + i * sizeof(arch)))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (needSwap)
|
||||
if (needSwap) {
|
||||
swap_fat_arch(&arch, 1, NX_UnknownByteOrder);
|
||||
machHeaderOffset = (off_t)arch.offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
}
|
||||
machHeaderOffset = (off_t) arch.offset;
|
||||
} else {
|
||||
struct fat_arch_64 arch;
|
||||
if (!readData(objFile, &arch, sizeof(arch), (off_t) (sizeof(header) + i * sizeof(arch))))
|
||||
if (!readData(objFile, &arch, sizeof(arch), (off_t) (sizeof(header) + i * sizeof(arch)))) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (needSwap)
|
||||
if (needSwap) {
|
||||
swap_fat_arch_64(&arch, 1, NX_UnknownByteOrder);
|
||||
}
|
||||
|
||||
machHeaderOffset = (off_t)arch.offset;
|
||||
machHeaderOffset = (off_t) arch.offset;
|
||||
}
|
||||
|
||||
uint32_t magic;
|
||||
if (!readData(objFile, &magic, sizeof(magic), machHeaderOffset))
|
||||
if (!readData(objFile, &magic, sizeof(magic), machHeaderOffset)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (magic == MH_MAGIC_64 || magic == MH_MAGIC)
|
||||
{
|
||||
if (magic == MH_MAGIC_64 || magic == MH_MAGIC) {
|
||||
dumpMachHeader(objFile, machHeaderOffset, magic == MH_MAGIC_64, cb, userdata, minLength);
|
||||
return NULL;
|
||||
}
|
||||
@@ -219,26 +229,29 @@ static const char* dumpFatHeader(FILE *objFile, bool (*cb)(const char *str, uint
|
||||
* It locates the __cstring section in the __TEXT segment which contains
|
||||
* the string literals used in the program.
|
||||
*/
|
||||
const char *ffBinaryExtractStrings(const char *machoFile, bool (*cb)(const char *str, uint32_t len, void *userdata), void *userdata, uint32_t minLength)
|
||||
{
|
||||
FF_AUTO_CLOSE_FILE FILE *objFile = fopen(machoFile, "rb");
|
||||
if (objFile == NULL)
|
||||
const char* ffBinaryExtractStrings(const char* machoFile, bool (*cb)(const char* str, uint32_t len, void* userdata), void* userdata, uint32_t minLength) {
|
||||
FF_AUTO_CLOSE_FILE FILE* objFile = fopen(machoFile, "rb");
|
||||
if (objFile == NULL) {
|
||||
return "File could not be opened";
|
||||
}
|
||||
|
||||
// Read the magic number to determine the type of binary
|
||||
uint32_t magic;
|
||||
if (!readData(objFile, &magic, sizeof(magic), 0))
|
||||
if (!readData(objFile, &magic, sizeof(magic), 0)) {
|
||||
return "read magic number failed";
|
||||
}
|
||||
|
||||
// Check for supported formats
|
||||
// MH_CIGAM and MH_CIGAM_64 seem to be no longer used, as `swap_mach_header` is marked as deprecated.
|
||||
// However FAT_CIGAM and FAT_CIGAM_64 are still used (/usr/bin/vim).
|
||||
if (magic != MH_MAGIC && magic != MH_MAGIC_64 && magic != FAT_CIGAM && magic != FAT_CIGAM_64 && magic != FAT_MAGIC && magic != FAT_MAGIC_64)
|
||||
if (magic != MH_MAGIC && magic != MH_MAGIC_64 && magic != FAT_CIGAM && magic != FAT_CIGAM_64 && magic != FAT_MAGIC && magic != FAT_MAGIC_64) {
|
||||
return "Unsupported format or big endian mach-o file";
|
||||
}
|
||||
|
||||
// Process either a fat binary or a regular Mach-O binary
|
||||
if (magic == FAT_MAGIC || magic == FAT_MAGIC_64 || magic == FAT_CIGAM || magic == FAT_CIGAM_64)
|
||||
if (magic == FAT_MAGIC || magic == FAT_MAGIC_64 || magic == FAT_CIGAM || magic == FAT_CIGAM_64) {
|
||||
return dumpFatHeader(objFile, cb, userdata, minLength);
|
||||
else
|
||||
} else {
|
||||
return dumpMachHeader(objFile, 0, magic == MH_MAGIC_64, cb, userdata, minLength);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user