Skip to content

Commit 83ab208

Browse files
committed
Cleaned up the Mach-O archive implementation
1 parent 54bc1b6 commit 83ab208

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

src/parser/file/macho/archive.c

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
11
/*
2-
* Callstack Library - Library creating human-readable call stacks.
2+
* CallstackLibrary - Library creating human-readable call stacks.
33
*
4-
* Copyright (C) 2024 mhahnFr
4+
* Copyright (C) 2024 - 2025 mhahnFr
55
*
6-
* This file is part of the CallstackLibrary. This library is free software:
7-
* you can redistribute it and/or modify it under the terms of the
8-
* GNU General Public License as published by the Free Software Foundation,
9-
* either version 3 of the License, or (at your option) any later version.
6+
* This file is part of the CallstackLibrary.
107
*
11-
* This library is distributed in the hope that it will be useful,
8+
* The CallstackLibrary is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* The CallstackLibrary is distributed in the hope that it will be useful,
1214
* but WITHOUT ANY WARRANTY; without even the implied warranty of
1315
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1416
* GNU General Public License for more details.
1517
*
16-
* You should have received a copy of the GNU General Public License along with
17-
* this library, see the file LICENSE. If not, see <https://www.gnu.org/licenses/>.
18+
* You should have received a copy of the GNU General Public License along with the
19+
* CallstackLibrary, see the file LICENSE. If not, see <https://www.gnu.org/licenses/>.
1820
*/
1921

22+
#include "archive.h"
23+
2024
#include <ar.h>
2125
#include <stdlib.h>
22-
#include <stdio.h>
23-
#include <string.h>
2426
#include <sys/stat.h>
2527

26-
#include "archive.h"
2728
#include "objectFile.h"
28-
2929
#include "../loader.h"
3030

3131
/**
3232
* Creates name for the given file name indicating the archive it came from.
3333
*
3434
* @param fileName the name of the file out of the archive
3535
* @param archiveName the name of the archive the file came from
36-
* @return the allocated name or `NULL` if unable to allocate
36+
* @return the allocated name or @c NULL if unable to allocate
3737
*/
3838
static inline char* macho_archive_constructName(const char* fileName, const char* archiveName) {
3939
if (fileName == NULL || archiveName == NULL) return NULL;
4040

4141
const size_t size = strlen(archiveName) + strlen(fileName) + 3;
42-
// Why +3: 1 byte for NUL termination and 2 bytes for the parentheses. - mhahnFr
42+
// Why +3: 1 byte for NULL termination and 2 bytes for the parentheses. - mhahnFr
4343

4444
char* toReturn = malloc(size);
4545
if (toReturn == NULL) {
@@ -54,7 +54,8 @@ static inline char* macho_archive_constructName(const char* fileName, const char
5454
}
5555

5656
/**
57-
* Parses and returns a number in the given base from the given string with the given length.
57+
* Parses and returns a number in the given base from the given string with the
58+
* given length.
5859
*
5960
* @param string the string
6061
* @param length the length of the string
@@ -71,15 +72,16 @@ static inline size_t macho_archive_parseNumber(const char* string, const size_t
7172
}
7273

7374
/**
74-
* Calculates and returns the length of the given string without the padding spaces.
75+
* Calculates and returns the length of the given string without the padding
76+
* spaces.
7577
*
7678
* @param string the string whose length to be determined
7779
* @param maximumLength the maximum length of the string
7880
* @return the actual length of the string
7981
*/
8082
static inline size_t macho_archive_stringLength(const char* string, const size_t maximumLength) {
81-
long i;
82-
for (i = maximumLength - 1; i >= 0 && string[i] == ' '; --i);
83+
ssize_t i;
84+
for (i = (ssize_t) maximumLength - 1; i >= 0 && string[i] == ' '; --i);
8385
return i + 1;
8486
}
8587

@@ -92,7 +94,7 @@ static inline size_t macho_archive_stringLength(const char* string, const size_t
9294
* @param cb the callback to be called once an object file has been extracted
9395
* @return whether the archive was parsed successfully
9496
*/
95-
static inline bool macho_archive_parseImpl(void* buffer, const char* fileName, const size_t totalSize, macho_archive_callback cb) {
97+
static inline bool macho_archive_parseImpl(void* buffer, const char* fileName, const size_t totalSize, const macho_archive_callback cb) {
9698
size_t counter = 0;
9799
const char* magic = buffer;
98100
if (strncmp(magic, ARMAG, SARMAG) != 0) {
@@ -103,14 +105,14 @@ static inline bool macho_archive_parseImpl(void* buffer, const char* fileName, c
103105
const size_t exSize = strlen(AR_EFMT1),
104106
endSize = strlen(ARFMAG);
105107
while (counter < totalSize) {
106-
struct ar_hdr* fileHeader = buffer + counter;
108+
const struct ar_hdr* fileHeader = buffer + counter;
107109
counter += sizeof(struct ar_hdr);
108110
if (strncmp(fileHeader->ar_fmag, ARFMAG, endSize) != 0) return false;
109111

110112
char* name;
111113
size_t nameLength = 0;
112114
if (strncmp(fileHeader->ar_name, AR_EFMT1, exSize) == 0) {
113-
const size_t size = macho_archive_parseNumber(fileHeader->ar_name + exSize, (sizeof fileHeader->ar_name / sizeof(char)) - exSize, 10);
115+
const size_t size = macho_archive_parseNumber(fileHeader->ar_name + exSize, sizeof fileHeader->ar_name / sizeof(char) - exSize, 10);
114116
name = malloc(size + 1);
115117
if (name == NULL) {
116118
return false;
@@ -131,20 +133,20 @@ static inline bool macho_archive_parseImpl(void* buffer, const char* fileName, c
131133

132134
void* objectFile = buffer + counter;
133135
struct objectFile* file = objectFile_new();
134-
file->lastModified = macho_archive_parseNumber(fileHeader->ar_date, sizeof fileHeader->ar_date / sizeof(char), 10);
136+
file->lastModified = (time_t) macho_archive_parseNumber(fileHeader->ar_date, sizeof fileHeader->ar_date / sizeof(char), 10);
135137
file->name = macho_archive_constructName(name, fileName);
136138
free(name);
137139

138140
file->parsed = objectFile_parseBuffer(file, objectFile);
139141
cb(file);
140142

141143
counter += macho_archive_parseNumber(fileHeader->ar_size, sizeof fileHeader->ar_size / sizeof(char), 10) - nameLength;
142-
for (; counter < totalSize && *((char*) (buffer + counter)) == '\n'; ++counter);
144+
for (; counter < totalSize && *(char*) (buffer + counter) == '\n'; ++counter);
143145
}
144146
return true;
145147
}
146148

147-
bool macho_archive_parse(const char* fileName, macho_archive_callback cb) {
149+
bool macho_archive_parse(const char* fileName, const macho_archive_callback cb) {
148150
return loader_loadFileAndExecute(fileName, (union loader_parserFunction) {
149151
.parseFuncExtended = (loader_parserExtended) macho_archive_parseImpl
150152
}, true, cb);

0 commit comments

Comments
 (0)