* bin_to_cso_mp3\r
* originally written by Exophase as "bin_to_iso_ogg"\r
* updated for cso/mp3 by notaz\r
+ * v2\r
*/\r
#include <stdio.h>\r
#include <stdlib.h>\r
#include <unistd.h>\r
#include <sys/stat.h>\r
#include <string.h>\r
+#include <ctype.h>\r
\r
#ifndef MAX_PATH\r
#define MAX_PATH 1024\r
\r
char *skip_whitespace(char *str)\r
{\r
- while(*str == ' ')\r
+ while (isspace(*str))\r
str++;\r
\r
return str;\r
}\r
\r
+char *skip_whitespace_rev(char *str)\r
+{\r
+ while (isspace(*str))\r
+ str--;\r
+\r
+ return str;\r
+}\r
+\r
+char *skip_nonspace_rev(char *str)\r
+{\r
+ while (!isspace(*str))\r
+ str--;\r
+\r
+ return str;\r
+}\r
+\r
s32 load_bin_cue(char *cue_file_name)\r
{\r
FILE *cue_file = fopen(cue_file_name, "rb");\r
{\r
char line_buffer[256];\r
char *line_buffer_ptr;\r
+ char *tmp;\r
\r
char bin_file_name[MAX_PATH];\r
char *separator_pos;\r
u32 i;\r
\r
// First, get filename. Only support binary right now.\r
- fgets(line_buffer, 255, cue_file);\r
-\r
- strcpy(bin_file_name, strchr(line_buffer, '"') + 1);\r
-\r
- *(strrchr(bin_file_name, '"')) = 0;\r
+ tmp = fgets(line_buffer, 255, cue_file);\r
+ if (tmp == NULL) goto invalid;\r
+ separator_pos = line_buffer + strlen(line_buffer) - 1;\r
+ separator_pos = skip_whitespace_rev(separator_pos);\r
+ if (separator_pos <= line_buffer) goto invalid;\r
+ separator_pos = skip_nonspace_rev(separator_pos);\r
+ if (separator_pos <= line_buffer) goto invalid;\r
+ separator_pos = skip_whitespace_rev(separator_pos);\r
+ if (separator_pos <= line_buffer) goto invalid;\r
+ // see if what's there is a quote.\r
+ if(*separator_pos == '"')\r
+ {\r
+ separator_pos[0] = 0;\r
+ separator_pos = strrchr(line_buffer, '"');\r
+ if (separator_pos == NULL) goto invalid;\r
+ strcpy(bin_file_name, separator_pos + 1);\r
+ }\r
+ else\r
+ {\r
+ // Otherwise go to the next space.\r
+ separator_pos[1] = 0;\r
+ separator_pos = strrchr(line_buffer, ' ');\r
+ if (separator_pos == NULL) goto invalid;\r
+ strcpy(bin_file_name, separator_pos + 1);\r
+ }\r
\r
// Might have to change directory first.\r
separator_pos = strrchr(cue_file_name, DIR_SEPARATOR_CHAR);\r
cd_bin.bin_file = fopen(bin_file_name, "rb");\r
#endif\r
\r
- printf("loaded bin file %s (%p)\n", bin_file_name, cd_bin.bin_file);\r
-\r
*separator_pos = DIR_SEPARATOR_CHAR;\r
chdir(current_dir);\r
}\r
#endif\r
}\r
\r
+ if (cd_bin.bin_file == NULL)\r
+ {\r
+ printf("can't open bin file: \"%s\"\n", bin_file_name);\r
+ return -1;\r
+ }\r
+ else\r
+ {\r
+ printf("found bin file: %s\n", bin_file_name);\r
+ }\r
+\r
for(i = 0; i < 100; i++)\r
{\r
cd_bin.logical_tracks[i] = NULL;\r
return 0;\r
}\r
\r
+ return -1;\r
+invalid:\r
+ printf("error: invalid/unsupported .cue file\n");\r
return -1;\r
}\r
\r
#ifdef _WIN32\r
static void update_path(void)\r
{\r
- char buff1[MAX_PATH], buff2[MAX_PATH];\r
+ char buff1[MAX_PATH*4], *buff2;\r
char *path;\r
- int i;\r
+ int size, i;\r
\r
path = getenv("PATH");\r
GetModuleFileNameA(NULL, buff1, sizeof(buff1));\r
if (buff1[i] == '\\') break;\r
buff1[i] = 0;\r
\r
- snprintf(buff2, sizeof(buff2), "%s;%s", path, buff1);\r
+ size = strlen(path) + strlen(buff1) + 3;\r
+ buff2 = malloc(size);\r
+ if (buff2 == NULL) return;\r
+\r
+ snprintf(buff2, size, "%s;%s", path, buff1);\r
SetEnvironmentVariableA("PATH", buff2);\r
+ free(buff2);\r
}\r
#endif\r
\r