@@ -33,17 +33,17 @@ static void bytes_to_hex(const uint8_t *bytes, char *hex_out, size_t dst_size) {
3333}
3434
3535/* Decode a 64-character hex string into a 32-byte SHA-256 digest.
36- * Returns 1 on success, 0 if any byte fails to parse . */
36+ * Returns 0 on success, non-zero on failure . */
3737static int sha256_hex_to_bytes (const char * hex_str , sha256_hash_t * out ) {
3838 for (int i = 0 ; i < 32 ; i ++ ) {
3939 unsigned int byte ;
4040 if (sscanf (hex_str + i * 2 , "%2x" , & byte ) != 1 ) {
4141 printf ("Warning: Invalid hex byte at offset %d in SHA256: %s\n" , i * 2 , hex_str );
42- return 0 ;
42+ return 1 ;
4343 }
4444 out -> bytes [i ] = (uint8_t )byte ;
4545 }
46- return 1 ;
46+ return 0 ;
4747}
4848
4949/* Trim leading and trailing whitespace from str in-place. */
@@ -55,14 +55,15 @@ static void trim_whitespace(char *str) {
5555 while (len > 0 && isspace ((unsigned char )str [len - 1 ])) str [-- len ] = '\0' ;
5656}
5757
58- /* Create an empty profiles.ini with a comment header. Returns 1 on success. */
58+ /* Create an empty profiles.ini with a comment header.
59+ * Returns 0 on success, non-zero on failure. */
5960static int create_profiles_ini (const char * path ) {
6061 FILE * file = fopen (path , "w" );
61- if (!file ) return 0 ;
62+ if (!file ) return 1 ;
6263 fprintf (file , "# ROM Profiles Database\n" );
6364 fprintf (file , "# Format: [SHA256] followed by quirk settings\n\n" );
6465 fclose (file );
65- return 1 ;
66+ return 0 ;
6667}
6768
6869/* Flush a completed profile entry into the hashmap. No-op if has_current is 0. */
@@ -76,12 +77,12 @@ static void commit_pending_profile(int has_current, const sha256_hash_t *sha256,
7677
7778/* Parse a section-header line of the form [SHA256] or [0xSHA256] (case-insensitive).
7879 * Writes the decoded 32-byte hash into *sha256_out.
79- * Returns 1 on success, 0 on any parse or validation error. */
80+ * Returns 0 on success, non-zero on any parse or validation error. */
8081static int parse_section_header (const char * line , sha256_hash_t * sha256_out ) {
8182 char inner [128 ];
8283 if (sscanf (line , "[%127[^]]]" , inner ) != 1 ) {
8384 printf ("Warning: Malformed section header: %s\n" , line );
84- return 0 ;
85+ return 1 ;
8586 }
8687
8788 /* Skip optional "0x" / "0X" prefix */
@@ -92,7 +93,7 @@ static int parse_section_header(const char *line, sha256_hash_t *sha256_out) {
9293 if (strlen (hex_start ) != 64 ) {
9394 printf ("Warning: SHA256 must be 64 hex characters (got %zu): %s\n" ,
9495 strlen (hex_start ), hex_start );
95- return 0 ;
96+ return 1 ;
9697 }
9798
9899 return sha256_hex_to_bytes (hex_start , sha256_out );
@@ -145,7 +146,7 @@ static void parse_profiles_ini(void) {
145146 commit_pending_profile (has_current , & current_sha256 , & current_profile );
146147
147148 sha256_hash_t parsed_sha256 ;
148- if (! parse_section_header (line , & parsed_sha256 )) {
149+ if (parse_section_header (line , & parsed_sha256 ) != 0 ) {
149150 has_current = 0 ;
150151 continue ;
151152 }
@@ -184,24 +185,25 @@ static void build_default_search_paths(char paths[2][LINE_MAX]) {
184185}
185186
186187/* Search candidate paths for an existing readable profiles.ini.
187- * On success, copies the found path into loaded_profiles_path and returns 1. */
188+ * On success, copies the found path into loaded_profiles_path and returns 0.
189+ * Returns non-zero if no candidate exists. */
188190static int find_existing_profiles_file (char candidates [2 ][LINE_MAX ]) {
189191 for (int i = 0 ; i < 2 ; i ++ ) {
190192 FILE * file = fopen (candidates [i ], "r" );
191193 if (file ) {
192194 fclose (file );
193195 set_path (loaded_profiles_path , sizeof (loaded_profiles_path ), candidates [i ]);
194- return 1 ;
196+ return 0 ;
195197 }
196198 }
197- return 0 ;
199+ return 1 ;
198200}
199201
200202/* Locate or create profiles.ini, populating loaded_profiles_path.
201203 * If a custom path was set by profiles_init(), honour it exclusively.
202204 * Otherwise, search next to the executable and in macOS bundle Resources,
203205 * creating a new file in the first candidate location if none is found.
204- * Returns 1 if an existing file is ready to be parsed, 0 otherwise . */
206+ * Returns 0 on success, non-zero on failure . */
205207static int resolve_profiles_path (void ) {
206208 char msg [TOAST_MSG_MAX ];
207209
@@ -213,42 +215,46 @@ static int resolve_profiles_path(void) {
213215 snprintf (msg , sizeof (msg ), "ROM profiles: %s" , loaded_profiles_path );
214216 toast_show (TOAST_INFO , msg );
215217 printf ("%s\n" , msg );
216- return 1 ;
218+ return 0 ;
217219 }
218220 /* File does not exist yet — create it */
219- if (create_profiles_ini (loaded_profiles_path )) {
221+ if (create_profiles_ini (loaded_profiles_path ) == 0 ) {
220222 snprintf (msg , sizeof (msg ), "ROM profiles created: %s" , loaded_profiles_path );
221223 toast_show (TOAST_SUCCESS , msg );
224+ printf ("%s\n" , msg );
225+ return 0 ;
222226 } else {
223227 snprintf (msg , sizeof (msg ), "Failed to create: %s" , loaded_profiles_path );
224228 toast_show (TOAST_ERROR , msg );
229+ printf ("%s\n" , msg );
230+ return 1 ;
225231 }
226- printf ("%s\n" , msg );
227- return 0 ;
228232 }
229233
230234 /* --- Default search paths --- */
231235 char search_paths [2 ][LINE_MAX ];
232236 build_default_search_paths (search_paths );
233237
234- if (find_existing_profiles_file (search_paths )) {
238+ if (find_existing_profiles_file (search_paths ) == 0 ) {
235239 snprintf (msg , sizeof (msg ), "ROM profiles: %s" , loaded_profiles_path );
236240 toast_show (TOAST_INFO , msg );
237241 printf ("%s\n" , msg );
238- return 1 ;
242+ return 0 ;
239243 }
240244
241245 /* --- Not found: create at the first candidate path --- */
242- if (create_profiles_ini (search_paths [0 ])) {
246+ if (create_profiles_ini (search_paths [0 ]) == 0 ) {
243247 set_path (loaded_profiles_path , sizeof (loaded_profiles_path ), search_paths [0 ]);
244248 snprintf (msg , sizeof (msg ), "ROM profiles created: %s" , search_paths [0 ]);
245249 toast_show (TOAST_INFO , msg );
250+ printf ("%s\n" , msg );
251+ return 0 ;
246252 } else {
247253 snprintf (msg , sizeof (msg ), "Failed to create: %s" , search_paths [0 ]);
248254 toast_show (TOAST_ERROR , msg );
255+ printf ("%s\n" , msg );
256+ return 1 ;
249257 }
250- printf ("%s\n" , msg );
251- return 0 ;
252258}
253259
254260void profiles_init (const char * custom_path ) {
@@ -257,7 +263,7 @@ void profiles_init(const char *custom_path) {
257263 if (custom_path && custom_path [0 ] != '\0' ) {
258264 set_path (loaded_profiles_path , sizeof (loaded_profiles_path ), custom_path );
259265 }
260- if (resolve_profiles_path ()) {
266+ if (resolve_profiles_path () == 0 ) {
261267 parse_profiles_ini ();
262268 }
263269}
@@ -281,16 +287,16 @@ const struct profile* profile_lookup(const sha256_hash_t *sha256) {
281287/* Rewrite the entire profiles.ini from the in-memory hashmap.
282288 * The file is fully replaced on each save rather than appended,
283289 * which keeps the format clean and avoids duplicate entries. */
284- static void profiles_write_to_file (void ) {
290+ static int profiles_write_to_file (void ) {
285291 if (loaded_profiles_path [0 ] == '\0' ) {
286292 printf ("Error: profiles.ini path not initialized\n" );
287- return ;
293+ return 1 ;
288294 }
289295
290296 FILE * file = fopen (loaded_profiles_path , "w" );
291297 if (!file ) {
292298 printf ("Error: Unable to write to %s\n" , loaded_profiles_path );
293- return ;
299+ return 1 ;
294300 }
295301
296302 fprintf (file , "# ROM Profiles Database\n" );
@@ -310,17 +316,18 @@ static void profiles_write_to_file(void) {
310316 }
311317
312318 fclose (file );
319+ return 0 ;
313320}
314321
315- void profiles_save (const uint8_t * rom , size_t rom_size , const char * rom_name ,
316- const struct quirks * quirks ) {
322+ int profiles_save (const uint8_t * rom , size_t rom_size , const char * rom_name ,
323+ const struct quirks * quirks ) {
317324 if (!rom || rom_size == 0 || !rom_name || !quirks ) {
318325 toast_show (TOAST_ERROR , "No ROM loaded. Cannot save profile." );
319- return ;
326+ return 1 ;
320327 }
321328 if (loaded_profiles_path [0 ] == '\0' ) {
322329 toast_show (TOAST_ERROR , "Could not locate profiles.ini" );
323- return ;
330+ return 1 ;
324331 }
325332
326333 sha256_hash_t sha256 ;
@@ -332,12 +339,16 @@ void profiles_save(const uint8_t *rom, size_t rom_size, const char *rom_name,
332339 p .quirks = * quirks ;
333340
334341 hmput (profile_map , sha256 , p );
335- profiles_write_to_file ();
342+ if (profiles_write_to_file () != 0 ) {
343+ toast_show (TOAST_ERROR , "Failed to write profiles.ini" );
344+ return 1 ;
345+ }
336346
337347 char hash_hex [65 ];
338348 bytes_to_hex (sha256 .bytes , hash_hex , sizeof (hash_hex ));
339349 printf ("Saved ROM profile for: %s (SHA256: %s)\n" , rom_name , hash_hex );
340350 char msg [TOAST_MSG_MAX ];
341351 snprintf (msg , sizeof (msg ), "Profile saved: %s" , rom_name );
342352 toast_show (TOAST_SUCCESS , msg );
353+ return 0 ;
343354}
0 commit comments