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
226227error_strings :
227228 free_string_array (strings , count );
228229error :
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