Debian Codegraph @

mednafen/cdrom/CDAccess_CCD.cpp

raw symbols SCIP
1 /* Mednafen - Multi-system Emulator
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 */
17
18 #include "../mednafen.h"
19 #include "../general.h"
20 #include "../msvc_compat.h"
21 #include "CDAccess_CCD.h"
22
23 #include <limits>
24 #include <limits.h>
25 #include <map>
26
27 using namespace CDUtility;
28
29 static void MDFN_strtoupper(std::string &str)
30 {
31 const size_t len = str.length();
32
33 for(size_t x = 0; x < len; x++)
34 {
35 if(str[x] >= 'a' && str[x] <= 'z')
36 {
37 str[x] = str[x] - 'a' + 'A';
38 }
39 }
40 }
41
42 typedef std::map<std::string, std::string> CCD_Section;
43
44 template<typename T>
45 static T CCD_ReadInt(CCD_Section &s, const std::string &propname, const bool have_defval = false, const int defval = 0)
46 {
47 CCD_Section::iterator zit = s.find(propname);
48
49 if(zit == s.end())
50 {
51 if(have_defval)
52 return defval;
53 else
54 throw MDFN_Error(0, _("Missing property: %s"), propname.c_str());
55 }
56
57 const std::string &v = zit->second;
58 int scan_base = 10;
59 size_t scan_offset = 0;
60 long ret = 0;
61
62 if(v.length() >= 3 && v[0] == '0' && v[1] == 'x')
63 {
64 scan_base = 16;
65 scan_offset = 2;
66 }
67
68 const char *vp = v.c_str() + scan_offset;
69 char *ep = NULL;
70
71 if(std::numeric_limits<T>::is_signed)
72 ret = strtol(vp, &ep, scan_base);
73 else
74 ret = strtoul(vp, &ep, scan_base);
75
76 if(!vp[0] || ep[0])
77 {
78 throw MDFN_Error(0, _("Property %s: Malformed integer: %s"), propname.c_str(), v.c_str());
79 }
80
81 //if(ret < minv || ret > maxv)
82 //{
83 // throw MDFN_Error(0, _("Property %s: Integer %ld out of range(accepted: %d through %d)."), propname.c_str(), ret, minv, maxv);
84 //}
85
86 return ret;
87 }
88
89
90 CDAccess_CCD::CDAccess_CCD(const char *path, bool image_memcache) : img_stream(NULL), sub_stream(NULL), img_numsectors(0)
91 {
92 try
93 {
94 Load(path, image_memcache);
95 }
96 catch(...)
97 {
98 Cleanup();
99 throw;
100 }
101 }
102
103 void CDAccess_CCD::Load(const char *path, bool image_memcache)
104 {
105 FileStream cf(path, MODE_READ);
106 std::map<std::string, CCD_Section> Sections;
107 std::string linebuf;
108 std::string cur_section_name;
109 std::string dir_path, file_base, file_ext;
110 char img_extsd[4] = { 'i', 'm', 'g', 0 };
111 char sub_extsd[4] = { 's', 'u', 'b', 0 };
112
113 MDFN_GetFilePathComponents(path, &dir_path, &file_base, &file_ext);
114
115 if(file_ext.length() == 4 && file_ext[0] == '.')
116 {
117 signed char extupt[3] = { -1, -1, -1 };
118
119 for(int i = 1; i < 4; i++)
120 {
121 if(file_ext[i] >= 'A' && file_ext[i] <= 'Z')
122 extupt[i - 1] = 'A' - 'a';
123 else if(file_ext[i] >= 'a' && file_ext[i] <= 'z')
124 extupt[i - 1] = 0;
125 }
126
127 signed char av = -1;
128 for(int i = 0; i < 3; i++)
129 {
130 if(extupt[i] != -1)
131 av = extupt[i];
132 else
133 extupt[i] = av;
134 }
135
136 if(av == -1)
137 av = 0;
138
139 for(int i = 0; i < 3; i++)
140 {
141 if(extupt[i] == -1)
142 extupt[i] = av;
143 }
144
145 for(int i = 0; i < 3; i++)
146 {
147 img_extsd[i] += extupt[i];
148 sub_extsd[i] += extupt[i];
149 }
150 }
151
152 //printf("%s %d %d %d\n", file_ext.c_str(), extupt[0], extupt[1], extupt[2]);
153
154 linebuf.reserve(256);
155
156 while(cf.get_line(linebuf) >= 0)
157 {
158 MDFN_trim(linebuf);
159
160 if(linebuf.length() == 0) // Skip blank lines.
161 continue;
162
163 if(linebuf[0] == '[')
164 {
165 if(linebuf.length() < 3 || linebuf[linebuf.length() - 1] != ']')
166 throw MDFN_Error(0, _("Malformed section specifier: %s"), linebuf.c_str());
167
168 cur_section_name = linebuf.substr(1, linebuf.length() - 2);
169 MDFN_strtoupper(cur_section_name);
170 }
171 else
172 {
173 const size_t feqpos = linebuf.find('=');
174 const size_t leqpos = linebuf.rfind('=');
175 std::string k, v;
176
177 if(feqpos == std::string::npos || feqpos != leqpos)
178 throw MDFN_Error(0, _("Malformed value pair specifier: %s"), linebuf.c_str());
179
180 k = linebuf.substr(0, feqpos);
181 v = linebuf.substr(feqpos + 1);
182
183 MDFN_trim(k);
184 MDFN_trim(v);
185
186 MDFN_strtoupper(k);
187
188 Sections[cur_section_name][k] = v;
189 }
190 }
191
192 {
193 CCD_Section& ds = Sections["DISC"];
194 unsigned toc_entries = CCD_ReadInt<unsigned>(ds, "TOCENTRIES");
195 unsigned num_sessions = CCD_ReadInt<unsigned>(ds, "SESSIONS");
196 bool data_tracks_scrambled = CCD_ReadInt<unsigned>(ds, "DATATRACKSSCRAMBLED");
197
198 if(num_sessions != 1)
199 throw MDFN_Error(0, _("Unsupported number of sessions: %u"), num_sessions);
200
201 if(data_tracks_scrambled)
202 throw MDFN_Error(0, _("Scrambled CCD data tracks currently not supported."));
203
204 //printf("MOO: %d\n", toc_entries);
205
206 for(unsigned te = 0; te < toc_entries; te++)
207 {
208 char tmpbuf[64];
209 snprintf(tmpbuf, sizeof(tmpbuf), "ENTRY %u", te);
210 CCD_Section& ts = Sections[std::string(tmpbuf)];
211 unsigned session = CCD_ReadInt<unsigned>(ts, "SESSION");
212 uint8 point = CCD_ReadInt<uint8>(ts, "POINT");
213 uint8 adr = CCD_ReadInt<uint8>(ts, "ADR");
214 uint8 control = CCD_ReadInt<uint8>(ts, "CONTROL");
215 uint8 pmin = CCD_ReadInt<uint8>(ts, "PMIN");
216 uint8 psec = CCD_ReadInt<uint8>(ts, "PSEC");
217 uint8 pframe = CCD_ReadInt<uint8>(ts, "PFRAME");
218 signed plba = CCD_ReadInt<signed>(ts, "PLBA");
219
220 if(session != 1)
221 throw MDFN_Error(0, "Unsupported TOC entry Session value: %u", session);
222
223 // Reference: ECMA-394, page 5-14
224 switch(point)
225 {
226 default:
227 throw MDFN_Error(0, "Unsupported TOC entry Point value: %u", point);
228 break;
229
230 case 0xA0:
231 tocd.first_track = pmin;
232 tocd.disc_type = psec;
233 break;
234
235 case 0xA1:
236 tocd.last_track = pmin;
237 break;
238
239 case 0xA2:
240 tocd.tracks[100].adr = adr;
241 tocd.tracks[100].control = control;
242 tocd.tracks[100].lba = plba;
243 break;
244
245 case 99:
246 case 98:
247 case 97:
248 case 96:
249 case 95:
250 case 94:
251 case 93:
252 case 92:
253 case 91:
254 case 90:
255 case 89:
256 case 88:
257 case 87:
258 case 86:
259 case 85:
260 case 84:
261 case 83:
262 case 82:
263 case 81:
264 case 80:
265 case 79:
266 case 78:
267 case 77:
268 case 76:
269 case 75:
270 case 74:
271 case 73:
272 case 72:
273 case 71:
274 case 70:
275 case 69:
276 case 68:
277 case 67:
278 case 66:
279 case 65:
280 case 64:
281 case 63:
282 case 62:
283 case 61:
284 case 60:
285 case 59:
286 case 58:
287 case 57:
288 case 56:
289 case 55:
290 case 54:
291 case 53:
292 case 52:
293 case 51:
294 case 50:
295 case 49:
296 case 48:
297 case 47:
298 case 46:
299 case 45:
300 case 44:
301 case 43:
302 case 42:
303 case 41:
304 case 40:
305 case 39:
306 case 38:
307 case 37:
308 case 36:
309 case 35:
310 case 34:
311 case 33:
312 case 32:
313 case 31:
314 case 30:
315 case 29:
316 case 28:
317 case 27:
318 case 26:
319 case 25:
320 case 24:
321 case 23:
322 case 22:
323 case 21:
324 case 20:
325 case 19:
326 case 18:
327 case 17:
328 case 16:
329 case 15:
330 case 14:
331 case 13:
332 case 12:
333 case 11:
334 case 10:
335 case 9:
336 case 8:
337 case 7:
338 case 6:
339 case 5:
340 case 4:
341 case 3:
342 case 2:
343 case 1:
344 tocd.tracks[point].adr = adr;
345 tocd.tracks[point].control = control;
346 tocd.tracks[point].lba = plba;
347 break;
348 }
349 }
350 }
351
352 // Convenience leadout track duplication.
353 if(tocd.last_track < 99)
354 tocd.tracks[tocd.last_track + 1] = tocd.tracks[100];
355
356 //
357 // Open image stream.
358 {
359 std::string image_path = MDFN_EvalFIP(dir_path, file_base + std::string(".") + std::string(img_extsd), true);
360
361 if(image_memcache)
362 {
363 img_stream = new MemoryStream(new FileStream(image_path.c_str(), MODE_READ));
364 }
365 else
366 {
367 img_stream = new FileStream(image_path.c_str(), MODE_READ);
368 }
369
370 int64 ss = img_stream->size();
371
372 if(ss % 2352)
373 throw MDFN_Error(0, _("CCD image size is not evenly divisible by 2352."));
374
375 img_numsectors = ss / 2352;
376 }
377
378 //
379 // Open subchannel stream
380 {
381 std::string sub_path = MDFN_EvalFIP(dir_path, file_base + std::string(".") + std::string(sub_extsd), true);
382
383 if(image_memcache)
384 sub_stream = new MemoryStream(new FileStream(sub_path.c_str(), MODE_READ));
385 else
386 sub_stream = new FileStream(sub_path.c_str(), MODE_READ);
387
388 if(sub_stream->size() != (int64)img_numsectors * 96)
389 throw MDFN_Error(0, _("CCD SUB file size mismatch."));
390 }
391
392 CheckSubQSanity();
393 }
394
395 //
396 // Checks for Q subchannel mode 1(current time) data that has a correct checksum, but the data is nonsensical or corrupted nonetheless; this is the
397 // case for some bad rips floating around on the Internet. Allowing these bad rips to be used will cause all sorts of problems during emulation, so we
398 // error out here if a bad rip is detected.
399 //
400 // This check is not as aggressive or exhaustive as it could be, and will not detect all potential Q subchannel rip errors; as such, it should definitely NOT be
401 // used in an effort to "repair" a broken rip.
402 //
403 void CDAccess_CCD::CheckSubQSanity(void)
404 {
405 size_t checksum_pass_counter = 0;
406 int prev_lba = INT_MAX;
407 uint8 prev_track = 0;
408
409 for(size_t s = 0; s < img_numsectors; s++)
410 {
411 union
412 {
413 uint8 full[96];
414 struct
415 {
416 uint8 pbuf[12];
417 uint8 qbuf[12];
418 };
419 } buf;
420
421 sub_stream->seek(s * 96, SEEK_SET);
422 sub_stream->read(buf.full, 96);
423
424 if(subq_check_checksum(buf.qbuf))
425 {
426 uint8 adr = buf.qbuf[0] & 0xF;
427
428 if(adr == 0x01)
429 {
430 uint8 track_bcd = buf.qbuf[1];
431 uint8 index_bcd = buf.qbuf[2];
432 uint8 rm_bcd = buf.qbuf[3];
433 uint8 rs_bcd = buf.qbuf[4];
434 uint8 rf_bcd = buf.qbuf[5];
435 uint8 am_bcd = buf.qbuf[7];
436 uint8 as_bcd = buf.qbuf[8];
437 uint8 af_bcd = buf.qbuf[9];
438
439 //printf("%2x %2x %2x\n", am_bcd, as_bcd, af_bcd);
440
441 if(!BCD_is_valid(track_bcd) || !BCD_is_valid(index_bcd) || !BCD_is_valid(rm_bcd) || !BCD_is_valid(rs_bcd) || !BCD_is_valid(rf_bcd) ||
442 !BCD_is_valid(am_bcd) || !BCD_is_valid(as_bcd) || !BCD_is_valid(af_bcd) ||
443 rs_bcd > 0x59 || rf_bcd > 0x74 || as_bcd > 0x59 || af_bcd > 0x74)
444 {
445 throw MDFN_Error(0, _("Garbage subchannel Q data detected(bad BCD/out of range): %02x:%02x:%02x %02x:%02x:%02x"), rm_bcd, rs_bcd, rf_bcd, am_bcd, as_bcd, af_bcd);
446 }
447 else
448 {
449 int lba = ((BCD_to_U8(am_bcd) * 60 + BCD_to_U8(as_bcd)) * 75 + BCD_to_U8(af_bcd)) - 150;
450 uint8 track = BCD_to_U8(track_bcd);
451
452 prev_lba = lba;
453
454 if(track < prev_track)
455 throw MDFN_Error(0, _("Garbage subchannel Q data detected(bad track number)"));
456 //else if(prev_track && track - pre
457
458 prev_track = track;
459 }
460 checksum_pass_counter++;
461 }
462 }
463 }
464
465 //printf("%u/%u\n", checksum_pass_counter, img_numsectors);
466 }
467
468 void CDAccess_CCD::Cleanup(void)
469 {
470 if(img_stream)
471 {
472 delete img_stream;
473 img_stream = NULL;
474 }
475
476 if(sub_stream)
477 {
478 delete sub_stream;
479 sub_stream = NULL;
480 }
481 }
482
483 CDAccess_CCD::~CDAccess_CCD()
484 {
485 Cleanup();
486 }
487
488 void CDAccess_CCD::Read_Raw_Sector(uint8 *buf, int32 lba)
489 {
490 if(lba < 0 || (size_t)lba >= img_numsectors)
491 throw(MDFN_Error(0, _("LBA out of range.")));
492
493 uint8 sub_buf[96];
494
495 img_stream->seek(lba * 2352, SEEK_SET);
496 img_stream->read(buf, 2352);
497
498 sub_stream->seek(lba * 96, SEEK_SET);
499 sub_stream->read(sub_buf, 96);
500
501 subpw_interleave(sub_buf, buf + 2352);
502 }
503
504
505 void CDAccess_CCD::Read_TOC(CDUtility::TOC *toc)
506 {
507 *toc = tocd;
508 }
509
510 bool CDAccess_CCD::Is_Physical(void) throw()
511 {
512 return false;
513 }
514
515 void CDAccess_CCD::Eject(bool eject_status)
516 {
517
518 }
519