Skip to content

Commit 08b05e4

Browse files
committed
all: use uint8_t instead of void
CMakeLists: better setting defaults pak_defs: prefer fast ints in custom structs
1 parent 436db51 commit 08b05e4

11 files changed

+45
-46
lines changed

CMakeLists.txt

+14-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
cmake_minimum_required (VERSION 2.8)
2-
project (chrome-pak C)
1+
cmake_minimum_required(VERSION 2.8)
2+
project(chrome-pak C)
33
option(LGPL "Use custom startfiles on windows for smaller binary, requiring GNU Lesser General Public License 2.1+." ON)
4-
5-
set (CMAKE_BUILD_TYPE Release)
4+
if (NOT CMAKE_BUILD_TYPE)
5+
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE)
6+
endif (NOT CMAKE_BUILD_TYPE)
67
if ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
7-
set (CMAKE_C_FLAGS "-O3 -Os -s -pipe -Wall -Wextra -fmerge-all-constants -Wl,--gc-sections,--build-id=none")
8-
if (WIN32 AND LGPL)
9-
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostartfiles -D_LGPL")
10-
endif ()
11-
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS}")
8+
set(CMAKE_C_FLAGS "-O3 -Os -pipe -Wall -Wextra -fmerge-all-constants -Wl,--gc-sections,--build-id=none")
9+
set(CMAKE_C_FLAGS_DEBUG "-g")
10+
set(CMAKE_C_FLAGS_RELEASE "-s")
11+
if (WIN32 AND LGPL)
12+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -nostartfiles -D_LGPL")
13+
endif ()
14+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_C_FLAGS}")
1215
endif ()
13-
aux_source_directory (${CMAKE_CURRENT_SOURCE_DIR} DIR_SRCS)
14-
add_executable (pak ${DIR_SRCS})
16+
aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR} DIR_SRCS)
17+
add_executable(pak ${DIR_SRCS})

main.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int pakPackIndexFile(char *indexPath, char *outputFilePath) {
6464
index = strrchr(indexPath, '/');
6565
}
6666
if (index != NULL) {
67-
filesPath = calloc(index - indexPath + 2, sizeof(char));
67+
filesPath = calloc(index - indexPath + 2, sizeof(uint8_t));
6868
if (filesPath == NULL) {
6969
returnCode = 5;
7070
goto PAK_PACK_INDEX_END;
@@ -85,7 +85,7 @@ int pakPackIndexFile(char *indexPath, char *outputFilePath) {
8585
}
8686

8787
// workaround outputFilePath="" after pakPack()
88-
outputFilePath2 = calloc(strlen(outputFilePath) + 1, sizeof(char));
88+
outputFilePath2 = calloc(strlen(outputFilePath) + 1, sizeof(uint8_t));
8989
if (pakIndexFile.buffer == NULL) {
9090
returnCode = 7;
9191
goto PAK_PACK_INDEX_END;

pak_defs.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,16 @@ typedef struct PakAlias {
6363
#define PAK_ERROR_BROKEN_INDEX "Error: Probably broken pak index file."
6464

6565
typedef struct MyPakHeader {
66-
uint32_t version;
67-
uint32_t resource_count;
68-
uint16_t alias_count;
69-
uint8_t encoding;
70-
uint8_t size;
66+
uint_fast32_t version;
67+
uint_fast32_t resource_count;
68+
uint_fast16_t alias_count;
69+
uint_fast8_t encoding;
70+
uint_fast8_t size;
7171
} MyPakHeader;
7272

7373
typedef struct PakFile {
74-
uint16_t id;
75-
uint32_t size;
74+
uint_fast16_t id;
75+
uint_fast32_t size;
7676
void *buffer;
7777
} PakFile;
7878

pak_file.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#include "pak_file.h"
22
PakFile pakPackFiles(MyPakHeader *myHeader, PakFile *pakResFile,
33
PakAlias *pakAlias) {
4-
void *buffer = NULL;
4+
uint8_t *buffer = NULL;
55
uint32_t size = myHeader->size;
66
uint32_t entrySize = (myHeader->resource_count + 1) * PAK_ENTRY_SIZE;
77
uint32_t aliasSize = myHeader->alias_count * PAK_ALIAS_SIZE;
88
size += entrySize + aliasSize;
99
for (uint32_t i = 0; i < myHeader->resource_count; i++) {
1010
size += (pakResFile + i)->size;
1111
}
12-
buffer = calloc(size, sizeof(char));
12+
buffer = calloc(size, sizeof(uint8_t));
1313
if (buffer == NULL)
1414
return NULL_File;
1515
uint32_t offset = pakWriteHeader(myHeader, buffer);
@@ -19,7 +19,7 @@ PakFile pakPackFiles(MyPakHeader *myHeader, PakFile *pakResFile,
1919
}
2020

2121
PakEntry *enrtyPtr = (PakEntry *)(buffer + offset);
22-
void *filePtr = buffer + offset + entrySize + aliasSize;
22+
uint8_t *filePtr = buffer + offset + entrySize + aliasSize;
2323
for (uint32_t i = 0; i < myHeader->resource_count; i++) {
2424
memcpy(filePtr, pakResFile->buffer, pakResFile->size);
2525
enrtyPtr->resource_id = pakResFile->id;
@@ -39,7 +39,7 @@ PakFile pakPackFiles(MyPakHeader *myHeader, PakFile *pakResFile,
3939
return pakFile;
4040
}
4141

42-
PakFile pakGetFile(void *pakBuffer, uint16_t id) {
42+
PakFile pakGetFile(uint8_t *pakBuffer, uint16_t id) {
4343
PakFile pakFile = NULL_File;
4444
MyPakHeader myHeader;
4545
if (!pakParseHeader(pakBuffer, &myHeader)) {
@@ -70,7 +70,7 @@ PakFile pakGetFile(void *pakBuffer, uint16_t id) {
7070
return NULL_File;
7171
}
7272

73-
PakFile *pakGetFiles(void *buffer) {
73+
PakFile *pakGetFiles(uint8_t *buffer) {
7474
PakFile *pakResFile = NULL;
7575
MyPakHeader myHeader;
7676
if (!pakParseHeader(buffer, &myHeader)) {

pak_file.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ PakFile pakPackFiles(MyPakHeader *myHeader, PakFile *pakResFile,
2626
* @param uint16_t *id - target file id.
2727
* @return PakFile - the target file.
2828
*/
29-
PakFile pakGetFile(void *pakBuffer, uint16_t id);
29+
PakFile pakGetFile(uint8_t *pakBuffer, uint16_t id);
3030

3131
/**
3232
* Get all files from pak, returns NULL on failure.
3333
* Note: this would allocate memory.
3434
* @param void* buffer - pointer to pak.
3535
* @return PakFile* - pointer to all files in pak.
3636
*/
37-
PakFile *pakGetFiles(void *buffer);
37+
PakFile *pakGetFiles(uint8_t *buffer);
3838

3939
#endif // __PAK_FILE_H__

pak_file_io.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,32 @@ PakFile readFile(const char *fileName) {
1010

1111
/* Get the number of bytes */
1212
fseeko(filePtr, 0, SEEK_END);
13-
file.size = ftello(filePtr);
13+
file.size = (uint32_t) ftello(filePtr);
1414

1515
/* reset the file position indicator to
1616
the beginning of the file */
1717
fseeko(filePtr, 0, SEEK_SET);
1818

1919
/* grab sufficient memory for the
2020
buffer to hold the text */
21-
file.buffer = calloc(file.size + 1, sizeof(char));
21+
file.buffer = calloc(file.size + 1, sizeof(uint8_t));
2222

2323
/* memory error */
2424
if (file.buffer == NULL)
2525
return NULL_File;
2626

2727
/* copy all the text into the buffer */
28-
fread(file.buffer, sizeof(char), file.size, filePtr);
28+
fread(file.buffer, sizeof(uint8_t), file.size, filePtr);
2929
fclose(filePtr);
3030

31-
/* free the memory we used for the buffer */
32-
// free(buffer);
3331
return file;
3432
}
3533

3634
bool writeFile(const char *fileName, const PakFile file) {
3735
FILE *filePtr = fopen(fileName, "wb");
3836
if (filePtr == NULL)
3937
return false;
40-
const size_t result = fwrite(file.buffer, sizeof(char), file.size, filePtr);
38+
const size_t result = fwrite(file.buffer, sizeof(uint8_t), file.size, filePtr);
4139
fclose(filePtr);
4240
return (result == file.size);
4341
}

pak_get_file_type.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ char *pakGetFileType(PakFile file) {
1616
for (unsigned int i = 0; i < FILE_TYPE_COUNT; i++)
1717
if (file.size > FILE_TYPES[i].size &&
1818
memcmp(file.buffer, FILE_TYPES[i].identifer, FILE_TYPES[i].size) ==
19-
0)
19+
0)
2020
return FILE_TYPES[i].type;
2121
return "";
2222
}

pak_header.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ unsigned int pakWriteHeader(MyPakHeader *myHeader, void *buffer) {
2828
if (myHeader->version == 5) {
2929
PakHeaderV5 *header = (PakHeaderV5 *)buffer;
3030
header->version = myHeader->version;
31-
header->resource_count = myHeader->resource_count;
31+
header->resource_count = (uint16_t) myHeader->resource_count;
3232
header->encoding = myHeader->encoding;
3333
header->alias_count = myHeader->alias_count;
3434
} else if (myHeader->version == 4) {
@@ -43,7 +43,7 @@ unsigned int pakWriteHeader(MyPakHeader *myHeader, void *buffer) {
4343
return myHeader->size;
4444
}
4545

46-
bool pakCheckFormat(void *buffer, unsigned int size) {
46+
bool pakCheckFormat(uint8_t *buffer, unsigned int size) {
4747
MyPakHeader myHeader;
4848
if (!pakParseHeader(buffer, &myHeader)) {
4949
return false;

pak_header.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ unsigned int pakWriteHeader(MyPakHeader *myHeader, void *buffer);
2727
* @param unsigned int size - pak buffer size in bytes.
2828
* @return bool - succeed or not.
2929
*/
30-
bool pakCheckFormat(void *buffer, unsigned int size);
30+
bool pakCheckFormat(uint8_t *buffer, unsigned int size);
3131

3232
#endif // __PAK_HEADERS_H__

pak_pack.c

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "pak_pack.h"
2-
bool pakUnpack(void *buffer, char *outputPath) {
2+
bool pakUnpack(uint8_t *buffer, char *outputPath) {
33
MyPakHeader myHeader;
44
if (!pakParseHeader(buffer, &myHeader)) {
55
return false;
@@ -19,7 +19,7 @@ bool pakUnpack(void *buffer, char *outputPath) {
1919
#else
2020
mkdir(outputPath, 0777);
2121
#endif
22-
char *pakIndexStr = calloc(PAK_BUFFER_BLOCK_SIZE, sizeof(char));
22+
char *pakIndexStr = calloc(PAK_BUFFER_BLOCK_SIZE, sizeof(uint8_t));
2323
if (pakIndexStr == NULL) {
2424
free(files);
2525
return false;
@@ -87,8 +87,7 @@ PakFile pakPack(PakFile pakIndex, char *path) { // TODO
8787
PakFile pakFile = NULL_File;
8888
PakFile *resFiles = NULL;
8989
PakAlias *pakAlias = NULL;
90-
// if (!sscanf(pakIndexBuf, "version=%u", &myHeader.version))
91-
// return NULL_File;
90+
9291
uint32_t count = 0;
9392
uint32_t offset = sizeof(PAK_INDEX_GLOBAL_TAG) - 1;
9493
sscanf(pakIndexBuf + offset, " version=%u%n", &myHeader.version, &count);
@@ -122,7 +121,7 @@ PakFile pakPack(PakFile pakIndex, char *path) { // TODO
122121
} else {
123122
pakAliasIndex += sizeof(PAK_INDEX_ALIAS_TAG) - 1;
124123
myHeader.alias_count =
125-
countChar(pakAliasIndex, pakIndexEnd - pakAliasIndex, '=');
124+
(uint16_t) countChar(pakAliasIndex, pakIndexEnd - pakAliasIndex, '=');
126125
}
127126
myHeader.resource_count =
128127
countChar(pakEntryIndex, pakAliasIndex - pakEntryIndex, '=');
@@ -138,7 +137,7 @@ PakFile pakPack(PakFile pakIndex, char *path) { // TODO
138137
if (resFiles == NULL) {
139138
goto PAK_PACK_END;
140139
}
141-
// uint32_t totalFileSize = 0;
140+
142141
offset = 0;
143142
for (uint32_t i = 0; i < myHeader.resource_count; i++) {
144143
uint32_t id = 0;
@@ -155,8 +154,7 @@ PakFile pakPack(PakFile pakIndex, char *path) { // TODO
155154
myHeader.resource_count = i;
156155
goto PAK_PACK_END;
157156
}
158-
resFiles[i].id = id;
159-
// totalFileSize += resFiles[i].size;
157+
resFiles[i].id = (uint16_t) id;
160158
// printf("id=%u\tfile_name=%s\tpath=%s\tsize=%u\n",id,
161159
// fileNameBuf, pathBuf, resFiles[i].size);
162160
}

pak_pack.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* @param char *outputPath - target path.
2626
* @return bool - succeed or not.
2727
*/
28-
bool pakUnpack(void *buffer, char *outputPath);
28+
bool pakUnpack(uint8_t *buffer, char *outputPath);
2929

3030
/**
3131
* Pack a pak file from target index (ini) file,

0 commit comments

Comments
 (0)