fix use of freed mem
[picodrive.git] / pico / cd / cd_image.c
CommitLineData
274fcc35 1/*
2 * CD image handler
3 * (C) notaz, 2007,2013
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
8
9#include "../pico_int.h"
10#include "genplus_macros.h"
11#include "cdd.h"
12#include "cue.h"
13
14static int handle_mp3(const char *fname, int index)
15{
16 track_t *track = &cdd.toc.tracks[index];
17 FILE *tmp_file;
18 int kBps;
19 int fs, ret;
20
21 tmp_file = fopen(fname, "rb");
22 if (tmp_file == NULL)
23 return -1;
24
25 ret = fseek(tmp_file, 0, SEEK_END);
26 fs = ftell(tmp_file);
27 fseek(tmp_file, 0, SEEK_SET);
28
29#ifdef _PSP_FW_VERSION
30 // some systems (like PSP) can't have many open files at a time,
31 // so we work with their names instead.
32 fclose(tmp_file);
33 tmp_file = (void *) strdup(fname);
34#endif
35
36 kBps = mp3_get_bitrate(tmp_file, fs) / 8;
37 if (ret != 0 || kBps <= 0)
38 {
39 elprintf(EL_STATUS, "track %2i: mp3 bitrate %i", index+1, kBps);
40#ifdef _PSP_FW_VERSION
41 free(tmp_file);
42#else
43 fclose(tmp_file);
44#endif
45 return -1;
46 }
47
48 track->fd = tmp_file;
49 track->offset = 0;
50
51 fs *= 75;
52 fs /= kBps * 1000;
53 return fs;
54}
55
56static void to_upper(char *d, const char *s)
57{
58 for (; *s != 0; d++, s++) {
59 if ('a' <= *s && *s <= 'z')
60 *d = *s - 'a' + 'A';
61 else
62 *d = *s;
63 }
64}
65
66// cdd.c uses lba - 150
67static void sprintf_lba(char *buf, size_t size, int lba)
68{
69 lba += 150;
70 snprintf(buf, size, "%02d:%02d:%02d", lba / 60 / 75,
71 (lba / 75) % 60, lba % 75);
72}
73
74int load_cd_image(const char *cd_img_name, int *type)
75{
76 static const char *exts[] = {
77 "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3",
78 "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3",
79 };
80 int i, j, n, lba, index, length, ret;
81 int iso_name_len, missed, cd_img_sectors;
82 char tmp_name[256], tmp_ext[10], tmp_ext_u[10];
83 track_t *tracks = cdd.toc.tracks;
84 cue_data_t *cue_data = NULL;
85 pm_file *pmf;
86
87 if (PicoCDLoadProgressCB != NULL)
88 PicoCDLoadProgressCB(cd_img_name, 1);
89
90 Pico_mcd->cdda_type = CT_UNKNOWN;
91
92 /* is this a .cue? */
93 cue_data = cue_parse(cd_img_name);
94 if (cue_data != NULL) {
95 cd_img_name = cue_data->tracks[1].fname;
96 *type = cue_data->tracks[1].type;
97 }
98
99 pmf = pm_open(cd_img_name);
100 if (pmf == NULL)
101 {
102 if (cue_data != NULL)
103 cue_destroy(cue_data);
104 return -1;
105 }
106 tracks[0].fd = pmf;
107
108 if (*type == CT_ISO)
109 cd_img_sectors = pmf->size >>= 11; // size in sectors
110 else cd_img_sectors = pmf->size /= 2352;
111
112 // cdd.c operates with lba - 150
113 tracks[0].start = 0;
114 tracks[0].end = cd_img_sectors;
115 tracks[0].offset = 0;
116
117 sprintf_lba(tmp_ext, sizeof(tmp_ext), 0);
118 elprintf(EL_STATUS, "Track 1: %s %9i DATA %s",
119 tmp_ext, tracks[0].end, cd_img_name);
120
121 lba = cd_img_sectors;
122
123 if (cue_data != NULL)
124 {
125 if (cue_data->tracks[2].fname == NULL) {
126 // NULL fname means track2 is in same file as track1
127 lba = tracks[0].end = cue_data->tracks[2].sector_offset;
128 }
129 i = 100 / cue_data->track_count + 1; // progress display
130
131 for (n = 2; n <= cue_data->track_count; n++)
132 {
133 if (PicoCDLoadProgressCB != NULL)
134 PicoCDLoadProgressCB(cd_img_name, i * n);
135
136 index = n - 1;
137 lba += cue_data->tracks[n].pregap;
138 if (cue_data->tracks[n].type == CT_MP3) {
139 ret = handle_mp3(cue_data->tracks[n].fname, index);
140 if (ret < 0)
141 break;
142 length = ret;
143 }
144 else if (cue_data->tracks[n].fname != NULL)
145 {
146 pm_file *f = pm_open(cue_data->tracks[n].fname);
147 if (f != NULL)
148 {
149 // assume raw, ignore header for wav..
150 tracks[index].fd = f;
151 tracks[index].offset = cue_data->tracks[n].sector_offset;
152 length = f->size / 2352;
153 }
154 else
155 {
156 elprintf(EL_STATUS, "track %2i (%s): can't determine length",
157 n, cue_data->tracks[n].fname);
158 tracks[index].offset = 0;
159 length = 2*75;
160 }
161 }
162 else
163 {
164 if (n < cue_data->track_count)
165 length = cue_data->tracks[n+1].sector_offset -
166 cue_data->tracks[n].sector_offset;
167 else
168 length = cd_img_sectors - cue_data->tracks[n].sector_offset;
169 tracks[index].offset = cue_data->tracks[n].sector_offset;
170 }
171
172 if (cue_data->tracks[n].sector_xlength != 0)
173 // overriden by custom cue command
174 length = cue_data->tracks[n].sector_xlength;
175
176 Pico_mcd->cdda_type = cue_data->tracks[n].type;
177
178 tracks[index].start = lba;
179 lba += length;
180 tracks[index].end = lba;
181
182 sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
183 elprintf(EL_STATUS, "Track %2i: %s %9i AUDIO %s",
184 n, tmp_ext, length, cue_data->tracks[n].fname);
185 }
274fcc35 186 goto finish;
187 }
188
189 /* mp3 track autosearch, Gens-like */
190 iso_name_len = strlen(cd_img_name);
191 if (iso_name_len >= sizeof(tmp_name))
192 iso_name_len = sizeof(tmp_name) - 1;
193
194 for (n = 2, i = 0, missed = 0; i < 100 && missed < 4; i++)
195 {
196 if (PicoCDLoadProgressCB != NULL && i > 1)
197 PicoCDLoadProgressCB(cd_img_name, i + (100-i)*missed/4);
198
199 for (j = 0; j < sizeof(exts)/sizeof(char *); j++)
200 {
201 int ext_len;
202 char *p;
203
204 index = n - 1;
205
206 snprintf(tmp_ext, sizeof(tmp_ext), exts[j], i);
207 ext_len = strlen(tmp_ext);
208 to_upper(tmp_ext_u, tmp_ext);
209
210 memcpy(tmp_name, cd_img_name, iso_name_len + 1);
211 p = tmp_name + iso_name_len - 4;
212
213 strcpy(p, tmp_ext);
214 ret = handle_mp3(tmp_name, index);
215 if (ret <= 0) {
216 strcpy(p, tmp_ext_u);
217 ret = handle_mp3(tmp_name, index);
218 }
219
220 if (ret <= 0 && i > 1 && iso_name_len > ext_len) {
221 p = tmp_name + iso_name_len - ext_len;
222 strcpy(p, tmp_ext);
223 ret = handle_mp3(tmp_name, index);
224 if (ret <= 0) {
225 strcpy(p, tmp_ext_u);
226 ret = handle_mp3(tmp_name, index);
227 }
228 }
229
230 if (ret > 0)
231 {
232 length = ret;
233 tracks[index].start = lba;
234 lba += length;
235 tracks[index].end = lba;
236
237 Pico_mcd->cdda_type = CT_MP3;
238
239 sprintf_lba(tmp_ext, sizeof(tmp_ext), tracks[index].start);
240 elprintf(EL_STATUS, "Track %2i: %s %9i AUDIO - %s",
241 n, tmp_ext, length, tmp_name);
242
243 n++;
244 missed = 0;
245 break;
246 }
247 }
248 if (ret <= 0 && i > 1)
249 missed++;
250 }
251
252finish:
253 cdd.toc.last = n - 1;
254 cdd.toc.end = lba;
255
256 sprintf_lba(tmp_ext, sizeof(tmp_ext), cdd.toc.end);
257 elprintf(EL_STATUS, "End CD - %s\n", tmp_ext);
258
259 if (PicoCDLoadProgressCB != NULL)
260 PicoCDLoadProgressCB(cd_img_name, 100);
261
9993e0d6 262 if (cue_data != NULL)
263 cue_destroy(cue_data);
264
274fcc35 265 return 0;
266}
267
268// vim:shiftwidth=2:ts=2:expandtab