Skip to content

Commit 93ab8be

Browse files
committed
fix boolean logic
1 parent 83f72ba commit 93ab8be

3 files changed

Lines changed: 79 additions & 90 deletions

File tree

include/utils.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef UTILS_H
1010
#define UTILS_H
1111

12+
#include <stdbool.h>
1213
#include <stddef.h>
1314
#include <stdint.h>
1415
#include <stdio.h>
@@ -43,18 +44,18 @@ uint8_t *read_file(const char *path, size_t *outSize);
4344
/*
4445
* Write bytes to a file, creating or truncating it as needed.
4546
*/
46-
int write_file(const char *path, const uint8_t *data, size_t size);
47+
bool write_file(const char *path, const uint8_t *data, size_t size);
4748

4849
/*
4950
* Create a directory if it does not already exist.
5051
*/
51-
int mkdir_dir(const char *path);
52+
bool mkdir_dir(const char *path);
5253

5354
/*
5455
* Check whether a file extension matches one of the known Nintendo container
5556
* magic strings whose bytes appear reversed in the file header, or not.
5657
*/
57-
int is_invertible(const char *ext);
58+
bool is_invertible(const char *ext);
5859

5960
/*
6061
* Reverse a null-terminated string in place.
@@ -82,9 +83,9 @@ char *unescape_json_string(const char *start, size_t len);
8283
* Parse/write a flat JSON object, mapping the literals true, false, and null to
8384
* the integers 1, 0, and -1 respectively.
8485
*/
85-
int read_json_file_states(
86+
bool read_json_file_states(
8687
const char *path, char ***outNames, int **outStates, uint32_t *outCount);
87-
int write_json_file_states(
88+
bool write_json_file_states(
8889
const char *path, char *const *names, const int *states, uint32_t count);
8990

9091
/*

src/main.c

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "utils.h"
1111

1212
#include <ctype.h>
13+
#include <stdbool.h>
1314
#include <stdint.h>
1415
#include <stdio.h>
1516
#include <stdlib.h>
@@ -37,11 +38,11 @@ typedef struct {
3738

3839
typedef struct {
3940
uint32_t relativeOffset; // offset relative to data_start; if 0xFFFFFFFF,
40-
// skip this entry
41+
// skip this entry
4142
uint32_t outputSize; // size of the output (decompressed) data; pad the
42-
// output with zeros to reach this size
43+
// output with zeros to reach this size
4344
uint32_t inputSize; // size of the compressed input data if LZ10-compressed;
44-
// 0 if not compressed
45+
// 0 if not compressed
4546
} FATEntry;
4647

4748
/*
@@ -113,18 +114,18 @@ static void join_path(
113114
/*
114115
* Store a heap copy of a name at metaNames[index].
115116
*/
116-
static int set_meta_name(
117+
static bool set_meta_name(
117118
char **metaNames, uint32_t count, uint32_t index, const char *name)
118119
{
119120
(void)count; // unused; kept for a consistent signature
120121
metaNames[index] = xstrdup(name);
121-
return metaNames[index] ? EXIT_SUCCESS : EXIT_FAILURE;
122+
return metaNames[index] != NULL;
122123
}
123124

124125
/*
125126
* Store a fallback "NNNN.bin" name.
126127
*/
127-
static int set_meta_bin_name(char **metaNames, uint32_t count, uint32_t index)
128+
static bool set_meta_bin_name(char **metaNames, uint32_t count, uint32_t index)
128129
{
129130
char name[32];
130131
make_index_name(name, sizeof(name), index, "bin");
@@ -195,23 +196,23 @@ static const char *path_basename(const char *path)
195196
* Extract all files from an ACF archive into a sibling directory with the same
196197
* name minus the extension.
197198
*/
198-
static int extract_acf(const char *path)
199+
static bool extract_acf(const char *path)
199200
{
200201
if (!path) {
201-
return EXIT_FAILURE;
202+
return false;
202203
}
203204

204205
size_t fileSize = 0;
205206
uint8_t *fileData = read_file(path, &fileSize);
206207
if (!fileData) {
207208
fprintf(stderr, "extract_acf: cannot read '%s'\n", path);
208-
return EXIT_FAILURE;
209+
return false;
209210
}
210211

211212
if (fileSize < sizeof(ACFHeader)) {
212213
fprintf(stderr, "extract_acf: '%s' is too small to be an ACF\n", path);
213214
free(fileData);
214-
return EXIT_FAILURE;
215+
return false;
215216
}
216217

217218
ACFHeader hdr;
@@ -222,7 +223,7 @@ static int extract_acf(const char *path)
222223
"extract_acf: ''%s' does not have an 'acf\\0' header\n",
223224
path);
224225
free(fileData);
225-
return EXIT_FAILURE;
226+
return false;
226227
}
227228

228229
size_t fatOffset = hdr.headerSize;
@@ -231,7 +232,7 @@ static int extract_acf(const char *path)
231232
fprintf(
232233
stderr, "extract_acf: FAT table in '%s' exceeds file size\n", path);
233234
free(fileData);
234-
return EXIT_FAILURE;
235+
return false;
235236
}
236237

237238
const FATEntry *entries = (const FATEntry *)(fileData + fatOffset);
@@ -250,7 +251,7 @@ static int extract_acf(const char *path)
250251
if (!metaNames || !metaStates) {
251252
fprintf(stderr, "extract_acf: memory allocation failed\n");
252253
cleanup_extract(fileData, metaNames, metaStates, hdr.numFiles);
253-
return EXIT_FAILURE;
254+
return false;
254255
}
255256

256257
char extBuf[16];
@@ -260,10 +261,10 @@ static int extract_acf(const char *path)
260261

261262
// sentinel value marks an absent entry
262263
if (e.relativeOffset == 0xFFFFFFFFu) {
263-
if (set_meta_bin_name(metaNames, hdr.numFiles, i) != EXIT_SUCCESS) {
264+
if (!set_meta_bin_name(metaNames, hdr.numFiles, i)) {
264265
fprintf(stderr, "extract_acf: memory allocation failed\n");
265266
cleanup_extract(fileData, metaNames, metaStates, hdr.numFiles);
266-
return EXIT_FAILURE;
267+
return false;
267268
}
268269
metaStates[i] = -1;
269270
continue;
@@ -272,10 +273,10 @@ static int extract_acf(const char *path)
272273
size_t dataOffset = (size_t)hdr.dataStart + (size_t)e.relativeOffset;
273274
if (dataOffset >= fileSize) {
274275
fprintf(stderr, "extract_acf: entry %u: offset out of range\n", i);
275-
if (set_meta_bin_name(metaNames, hdr.numFiles, i) != EXIT_SUCCESS) {
276+
if (!set_meta_bin_name(metaNames, hdr.numFiles, i)) {
276277
fprintf(stderr, "extract_acf: memory allocation failed\n");
277278
cleanup_extract(fileData, metaNames, metaStates, hdr.numFiles);
278-
return EXIT_FAILURE;
279+
return false;
279280
}
280281
metaStates[i] = -1;
281282
continue;
@@ -284,20 +285,19 @@ static int extract_acf(const char *path)
284285
const uint8_t *src = fileData + dataOffset;
285286
uint8_t *outBuf = NULL;
286287
size_t outSize = 0;
287-
int compressed = 0;
288+
bool compressed = false;
288289

289290
if (e.inputSize > 0) { // the entry is compressed
290291
if (dataOffset + (size_t)e.inputSize > fileSize) {
291292
fprintf(stderr,
292293
"extract_acf: entry %u: compressed data exceeds file "
293294
"size\n",
294295
i);
295-
if (set_meta_bin_name(metaNames, hdr.numFiles, i)
296-
!= EXIT_SUCCESS) {
296+
if (!set_meta_bin_name(metaNames, hdr.numFiles, i)) {
297297
fprintf(stderr, "extract_acf: memory allocation failed\n");
298298
cleanup_extract(
299299
fileData, metaNames, metaStates, hdr.numFiles);
300-
return EXIT_FAILURE;
300+
return false;
301301
}
302302
metaStates[i] = -1;
303303
continue;
@@ -306,7 +306,7 @@ static int extract_acf(const char *path)
306306
if (src[0] == 0x10) { // LZ10 compression type byte
307307
outBuf = lz10_decompress(src, (size_t)e.inputSize, &outSize);
308308
if (outBuf) {
309-
compressed = 1;
309+
compressed = true;
310310
} else {
311311
fprintf(stderr,
312312
"extract_acf: decompression failed for entry %u, "
@@ -319,7 +319,7 @@ static int extract_acf(const char *path)
319319
stderr, "extract_acf: memory allocation failed\n");
320320
cleanup_extract(
321321
fileData, metaNames, metaStates, hdr.numFiles);
322-
return EXIT_FAILURE;
322+
return false;
323323
}
324324
memcpy(outBuf, src, outSize);
325325
}
@@ -330,7 +330,7 @@ static int extract_acf(const char *path)
330330
fprintf(stderr, "extract_acf: memory allocation failed\n");
331331
cleanup_extract(
332332
fileData, metaNames, metaStates, hdr.numFiles);
333-
return EXIT_FAILURE;
333+
return false;
334334
}
335335
memcpy(outBuf, src, outSize);
336336
}
@@ -339,12 +339,11 @@ static int extract_acf(const char *path)
339339
fprintf(stderr,
340340
"extract_acf: entry %u: raw data exceeds file size\n",
341341
i);
342-
if (set_meta_bin_name(metaNames, hdr.numFiles, i)
343-
!= EXIT_SUCCESS) {
342+
if (!set_meta_bin_name(metaNames, hdr.numFiles, i)) {
344343
fprintf(stderr, "extract_acf: memory allocation failed\n");
345344
cleanup_extract(
346345
fileData, metaNames, metaStates, hdr.numFiles);
347-
return EXIT_FAILURE;
346+
return false;
348347
}
349348
metaStates[i] = -1;
350349
continue;
@@ -355,7 +354,7 @@ static int extract_acf(const char *path)
355354
if (!outBuf) {
356355
fprintf(stderr, "extract_acf: memory allocation failed\n");
357356
cleanup_extract(fileData, metaNames, metaStates, hdr.numFiles);
358-
return EXIT_FAILURE;
357+
return false;
359358
}
360359
memcpy(outBuf, src, outSize);
361360
}
@@ -369,16 +368,15 @@ static int extract_acf(const char *path)
369368
char outname[768];
370369
join_path(outname, sizeof(outname), outdir, relname);
371370

372-
if (write_file(outname, outBuf, outSize) != 0) {
371+
if (!write_file(outname, outBuf, outSize)) {
373372
fprintf(stderr, "extract_acf: failed writing %s\n", outname);
374373
}
375374

376-
if (set_meta_name(metaNames, hdr.numFiles, i, relname)
377-
!= EXIT_SUCCESS) {
375+
if (!set_meta_name(metaNames, hdr.numFiles, i, relname)) {
378376
fprintf(stderr, "extract_acf: memory allocation failed\n");
379377
free(outBuf);
380378
cleanup_extract(fileData, metaNames, metaStates, hdr.numFiles);
381-
return EXIT_FAILURE;
379+
return false;
382380
}
383381

384382
metaStates[i] = compressed ? 1 : 0;
@@ -399,17 +397,17 @@ static int extract_acf(const char *path)
399397
char metafile[768];
400398
join_path(metafile, sizeof(metafile), outdir, "filelist.json");
401399

402-
if (write_json_file_states(metafile, metaNames, metaStates, hdr.numFiles)
403-
!= 0) {
400+
if (!write_json_file_states(
401+
metafile, metaNames, metaStates, hdr.numFiles)) {
404402
fprintf(stderr,
405403
"extract_acf: cannot create metadata file '%s'\n",
406404
metafile);
407405
cleanup_extract(fileData, metaNames, metaStates, hdr.numFiles);
408-
return EXIT_FAILURE;
406+
return false;
409407
}
410408

411409
cleanup_extract(fileData, metaNames, metaStates, hdr.numFiles);
412-
return EXIT_SUCCESS;
410+
return true;
413411
}
414412

415413
/*
@@ -465,10 +463,10 @@ static void process_directory(const char *directory)
465463
* Pack the contents of a directory into a new ACF archive named, guided by the
466464
* filelist.json file found inside the directory.
467465
*/
468-
static int build_acf(const char *directory)
466+
static bool build_acf(const char *directory)
469467
{
470468
if (!directory) {
471-
return EXIT_FAILURE;
469+
return false;
472470
}
473471

474472
char metafile[512];
@@ -478,19 +476,18 @@ static int build_acf(const char *directory)
478476
int *jsonStates = NULL; // -1 = null; 0 = false; 1 = true
479477
uint32_t jsonCount = 0;
480478

481-
if (read_json_file_states(metafile, &jsonNames, &jsonStates, &jsonCount)
482-
!= 0) {
479+
if (!read_json_file_states(metafile, &jsonNames, &jsonStates, &jsonCount)) {
483480
fprintf(stderr,
484481
"build_acf: metadata file not found or invalid: %s\n",
485482
metafile);
486-
return EXIT_FAILURE;
483+
return false;
487484
}
488485

489486
if (jsonCount == 0) {
490487
fprintf(stderr, "build_acf: no files to pack\n");
491488
free_string_array(jsonNames, jsonCount);
492489
free(jsonStates);
493-
return EXIT_FAILURE;
490+
return false;
494491
}
495492

496493
uint32_t numFiles = jsonCount;
@@ -509,7 +506,7 @@ static int build_acf(const char *directory)
509506
jsonNames,
510507
jsonStates,
511508
jsonCount);
512-
return EXIT_FAILURE;
509+
return false;
513510
}
514511

515512
for (uint32_t i = 0; i < numFiles; ++i) {
@@ -652,8 +649,7 @@ static int build_acf(const char *directory)
652649

653650
int doCompress = compressFlags[i];
654651
if (i == 0) {
655-
doCompress
656-
= 0; // first entry is always stored raw regardless of metadata
652+
doCompress = 0; // first entry is always stored raw
657653
}
658654

659655
fat[i].relativeOffset = (uint32_t)offset;
@@ -761,7 +757,7 @@ static int build_acf(const char *directory)
761757
jsonNames,
762758
jsonStates,
763759
jsonCount);
764-
return EXIT_SUCCESS;
760+
return true;
765761

766762
error:
767763
cleanup_build(out,
@@ -772,7 +768,7 @@ static int build_acf(const char *directory)
772768
jsonNames,
773769
jsonStates,
774770
jsonCount);
775-
return EXIT_FAILURE;
771+
return false;
776772
}
777773

778774
/*
@@ -815,7 +811,7 @@ int main(int argc, char **argv)
815811
printf("Extracting all ACFs in directory: '%s'\n", path);
816812
process_directory(path);
817813
} else {
818-
return extract_acf(path);
814+
return extract_acf(path) ? EXIT_SUCCESS : EXIT_FAILURE;
819815
}
820816

821817
} else if (!strcmp(mode, "-b") || !strcmp(mode, "--build")) {
@@ -826,7 +822,7 @@ int main(int argc, char **argv)
826822
}
827823

828824
printf("Building ACF from directory: '%s'\n", path);
829-
return build_acf(path);
825+
return build_acf(path) ? EXIT_SUCCESS : EXIT_FAILURE;
830826

831827
} else {
832828
fprintf(stderr, "Unknown option: '%s'\n", mode);

0 commit comments

Comments
 (0)