Skip to content

Commit 276d059

Browse files
committed
fix boolean logic
1 parent 2b9157a commit 276d059

6 files changed

Lines changed: 70 additions & 63 deletions

File tree

include/lz10.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@
99
#ifndef LZ10_H
1010
#define LZ10_H
1111

12+
#include <stdbool.h>
1213
#include <stdint.h>
1314
#include <stdlib.h>
1415

1516
/*
1617
* Perform an heuristic check to determine if a buffer resembles
1718
* LZ10-compressed data. This is not a full validation.
1819
*/
19-
int looks_like_lz10(const uint8_t *buf, size_t size);
20+
bool looks_like_lz10(const uint8_t *buf, size_t size);
2021

2122
/*
2223
* Decompress an LZ10 buffer.

include/utils.h

Lines changed: 3 additions & 2 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 <stdint.h>
1314
#include <stdio.h>
1415

@@ -39,7 +40,7 @@ char *xstrdup(const char *s);
3940
/*
4041
* Check whether a file exists at the given path.
4142
*/
42-
int file_exists(const char *path);
43+
bool file_exists(const char *path);
4344

4445
/*
4546
* Read an entire file into memory.
@@ -64,7 +65,7 @@ char **read_json_strings(const char *path, uint32_t *out_count);
6465
/*
6566
* Write an array of strings into a flat JSON object
6667
*/
67-
int write_json_strings(
68+
bool write_json_strings(
6869
const char *output, char *const *strings, uint32_t count);
6970

7071
/*

src/common/utils.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "utils.h"
1010

1111
#include <ctype.h>
12+
#include <stdbool.h>
1213
#include <stdint.h>
1314
#include <stdio.h>
1415
#include <stdlib.h>
@@ -91,7 +92,7 @@ char *xstrdup(const char *s)
9192
/*
9293
* Check whether a file exists at the given path.
9394
*/
94-
int file_exists(const char *path)
95+
bool file_exists(const char *path)
9596
{
9697
struct stat st;
9798
return stat(path, &st) == 0;
@@ -231,12 +232,12 @@ char **json_parse_strings(const char *json, uint32_t *out_count)
231232

232233
// skip key
233234
p++;
234-
int escaped = 0;
235+
bool escaped = false;
235236
while (*p) {
236237
if (escaped) {
237-
escaped = 0;
238+
escaped = false;
238239
} else if (*p == '\\') {
239-
escaped = 1;
240+
escaped = true;
240241
} else if (*p == '"') {
241242
break;
242243
}
@@ -269,12 +270,12 @@ char **json_parse_strings(const char *json, uint32_t *out_count)
269270
const char *start = p;
270271

271272
// read value string
272-
escaped = 0;
273+
escaped = false;
273274
while (*p) {
274275
if (escaped) {
275-
escaped = 0;
276+
escaped = false;
276277
} else if (*p == '\\') {
277-
escaped = 1;
278+
escaped = true;
278279
} else if (*p == '"') {
279280
break;
280281
}
@@ -354,12 +355,13 @@ char **read_json_strings(const char *path, uint32_t *out_count)
354355
/*
355356
* Write an array of strings into a flat JSON object.
356357
*/
357-
int write_json_strings(const char *output, char *const *strings, uint32_t count)
358+
bool write_json_strings(
359+
const char *output, char *const *strings, uint32_t count)
358360
{
359361
FILE *f = xfopen(output, "wb");
360362
if (!f) {
361363
fprintf(stderr, "write_json_strings: cannot open '%s'\n", output);
362-
return EXIT_FAILURE;
364+
return false;
363365
}
364366

365367
fputs("{\n", f);
@@ -386,7 +388,7 @@ int write_json_strings(const char *output, char *const *strings, uint32_t count)
386388
fprintf(
387389
stderr, "write_json_strings: escape failed for string %u\n", i);
388390
fclose(f);
389-
return EXIT_FAILURE;
391+
return false;
390392
}
391393

392394
fprintf(f,
@@ -401,7 +403,7 @@ int write_json_strings(const char *output, char *const *strings, uint32_t count)
401403

402404
fputs("}\n", f);
403405
fclose(f);
404-
return EXIT_SUCCESS;
406+
return true;
405407
}
406408

407409
/*

src/ra2mes/lz10.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "lz10.h"
1010

11+
#include <stdbool.h>
1112
#include <stddef.h>
1213
#include <stdint.h>
1314
#include <stdio.h>
@@ -17,13 +18,13 @@
1718
* Perform an heuristic check to determine if a buffer resembles
1819
* LZ10-compressed data. This is not a full validation.
1920
*/
20-
int looks_like_lz10(const uint8_t *buf, size_t size)
21+
bool looks_like_lz10(const uint8_t *buf, size_t size)
2122
{
2223
if (!buf || size < 4) {
23-
return 0;
24+
return false;
2425
}
2526
if (buf[0] != 0x10) {
26-
return 0;
27+
return false;
2728
}
2829

2930
// 24-bit little-endian decompressed size

src/ra2mes/main.c

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* SPDX-License-Identifier: MIT
77
*/
88

9+
#include <stdbool.h>
910
#include <stdint.h>
1011
#include <stdio.h>
1112
#include <stdlib.h>
@@ -117,33 +118,33 @@ static unsigned char *build_mes_buffer(const char *input, size_t *outSize)
117118
* Validate whether a buffer matches the expected Pokémon Ranger 2 MES file
118119
* structure.
119120
*/
120-
static int is_valid_mes(const uint8_t *buf, size_t size)
121+
static bool is_valid_mes(const uint8_t *buf, size_t size)
121122
{
122123
if (size < 8) { // minimum header size
123-
return 0;
124+
return false;
124125
}
125126

126127
uint32_t total = read_u32_le(buf); // total file size
127128
uint32_t count = read_u32_le(buf + 4); // number of strings
128129

129130
// stored total size must match actual buffer size
130131
if (total != size) {
131-
return 0;
132+
return false;
132133
}
133134

134135
uint32_t off = 8;
135136

136137
// iterate over all string blocks and read them
137138
for (uint32_t i = 0; i < count; i++) {
138139
if ((uint64_t)off + 4 > size) {
139-
return 0;
140+
return false;
140141
}
141142

142143
uint32_t blk = read_u32_le(buf + off);
143144
off += 4;
144145

145146
if (blk == 0 || (uint64_t)off + blk > size) {
146-
return 0;
147+
return false;
147148
}
148149

149150
off += blk;
@@ -156,7 +157,7 @@ static int is_valid_mes(const uint8_t *buf, size_t size)
156157
/*
157158
* Convert a (raw or LZ10-compressed) Pokémon Ranger 2 MES file into JSON.
158159
*/
159-
static int mes_to_json(const char *input, const char *output)
160+
static bool mes_to_json(const char *input, const char *output)
160161
{
161162
size_t size;
162163
unsigned char *buf = NULL;
@@ -217,62 +218,62 @@ static int mes_to_json(const char *input, const char *output)
217218
off += blk;
218219
}
219220

220-
int res = write_json_strings(output, strings, count);
221+
bool ok = write_json_strings(output, strings, count);
221222

222223
free_string_array(strings, count);
223224
free(work);
224-
return res;
225+
return ok;
225226

226227
error_strings:
227228
free_string_array(strings, count);
228229
error:
229230
free(work);
230-
return EXIT_FAILURE;
231+
return false;
231232
}
232233

233234
/*
234235
* Convert a JSON file into Pokémon Ranger 2 MES format.
235236
*/
236-
static int json_to_mes(const char *input, const char *output)
237+
static bool json_to_mes(const char *input, const char *output)
237238
{
238239
size_t size;
239240
unsigned char *buf = build_mes_buffer(input, &size);
240241
if (!buf) {
241242
fprintf(stderr, "json_to_mes: failed to build MES from '%s'\n", input);
242-
return EXIT_FAILURE;
243+
return false;
243244
}
244245

245246
FILE *f = xfopen(output, "wb");
246247
if (!f) {
247248
fprintf(stderr, "json_to_mes: cannot open '%s'\n", output);
248249
free(buf);
249-
return EXIT_FAILURE;
250+
return false;
250251
}
251252

252253
if (fwrite(buf, 1, size, f) != size) {
253254
fprintf(stderr, "json_to_mes: write failed\n");
254255
fclose(f);
255256
free(buf);
256-
return EXIT_FAILURE;
257+
return false;
257258
}
258259

259260
fclose(f);
260261
free(buf);
261-
return EXIT_SUCCESS;
262+
return true;
262263
}
263264

264265
/*
265266
* Convert a JSON file into Pokémon Ranger 2 MES format and compresses it using
266267
* LZ10.
267268
*/
268-
static int json_to_meslz(const char *input, const char *output)
269+
static bool json_to_meslz(const char *input, const char *output)
269270
{
270271
size_t rawSize;
271272
unsigned char *raw = build_mes_buffer(input, &rawSize);
272273
if (!raw) {
273274
fprintf(
274275
stderr, "json_to_meslz: failed to build MESLZ from '%s'\n", input);
275-
return EXIT_FAILURE;
276+
return false;
276277
}
277278

278279
size_t cmpSize;
@@ -281,26 +282,26 @@ static int json_to_meslz(const char *input, const char *output)
281282

282283
if (!cmp) {
283284
fprintf(stderr, "json_to_meslz: compression failed\n");
284-
return EXIT_FAILURE;
285+
return false;
285286
}
286287

287288
FILE *f = xfopen(output, "wb");
288289
if (!f) {
289290
fprintf(stderr, "json_to_meslz: cannot open '%s'\n", output);
290291
free(cmp);
291-
return EXIT_FAILURE;
292+
return false;
292293
}
293294

294295
if (fwrite(cmp, 1, cmpSize, f) != cmpSize) {
295296
fprintf(stderr, "json_to_meslz: write failed\n");
296297
fclose(f);
297298
free(cmp);
298-
return EXIT_FAILURE;
299+
return false;
299300
}
300301

301302
fclose(f);
302303
free(cmp);
303-
return EXIT_SUCCESS;
304+
return true;
304305
}
305306

306307
/*
@@ -336,7 +337,7 @@ int main(int argc, char **argv)
336337
}
337338

338339
char *output = NULL;
339-
int result = EXIT_FAILURE;
340+
bool ok = false;
340341

341342
const char *input = argv[2];
342343
const char *outarg = (argc == 4) ? argv[3] : NULL;
@@ -346,27 +347,27 @@ int main(int argc, char **argv)
346347
if (!output) {
347348
return EXIT_FAILURE;
348349
}
349-
result = mes_to_json(input, output);
350+
ok = mes_to_json(input, output);
350351

351352
} else if (strcmp(argv[1], "--to-mes") == 0) {
352353
output = outarg ? xstrdup(outarg) : make_output_path(input, ".mes");
353354
if (!output) {
354355
return EXIT_FAILURE;
355356
}
356-
result = json_to_mes(input, output);
357+
ok = json_to_mes(input, output);
357358

358359
} else if (strcmp(argv[1], "--to-meslz") == 0) {
359360
output = outarg ? xstrdup(outarg) : make_output_path(input, ".meslz");
360361
if (!output) {
361362
return EXIT_FAILURE;
362363
}
363-
result = json_to_meslz(input, output);
364+
ok = json_to_meslz(input, output);
364365

365366
} else {
366367
fprintf(stderr, "Unknown option: '%s'\n", argv[1]);
367368
fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
368369
}
369370

370371
free(output);
371-
return result;
372+
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
372373
}

0 commit comments

Comments
 (0)