2 * (C) GraÅžvydas "notaz" Ignotas, 2010
4 * This work is licensed under the terms of any of these licenses
6 * - GNU GPL, version 2 or later.
7 * - GNU LGPL, version 2.1 or later.
8 * See the COPYING file in the top-level directory.
15 #if !defined(_WIN32) && !defined(NO_DYLIB)
24 #define PFX "cdrcimg: "
25 #define err(f, ...) fprintf(stderr, PFX f, ##__VA_ARGS__)
27 #define CD_FRAMESIZE_RAW 2352
35 static const char *cd_fname;
36 static unsigned int *cd_index_table;
37 static unsigned int cd_index_len;
38 static unsigned int cd_sectors_per_blk;
39 static int cd_compression;
42 static int (*pBZ2_bzBuffToBuffDecompress)(char *dest, unsigned int *destLen, char *source,
43 unsigned int sourceLen, int small, int verbosity);
46 unsigned char raw[16][CD_FRAMESIZE_RAW];
47 unsigned char compressed[CD_FRAMESIZE_RAW * 16 + 100];
49 static int current_block, current_sect_in_blk;
52 extern long CALLBACK CDR__getStatus(struct CdrStat *stat);
58 unsigned char Time[3]; // current playing time
62 enum {DATA, CDDA} type;
63 char start[3]; // MSF-format
64 char length[3]; // MSF-format
67 #define MAXTRACKS 100 /* How many tracks can a CD hold? */
69 static int numtracks = 0;
71 #define btoi(b) ((b) / 16 * 10 + (b) % 16) /* BCD to u_char */
72 #define MSF2SECT(m, s, f) (((m) * 60 + (s) - 2) * 75 + (f))
74 // return Starting and Ending Track
76 // byte 0 - start track
78 static long CDRgetTN(unsigned char *buffer)
81 buffer[1] = numtracks > 0 ? numtracks : 1;
91 static long CDRgetTD(unsigned char track, unsigned char *buffer)
100 static int uncompress2_pcsx(void *out, unsigned long *out_size, void *in, unsigned long in_size)
105 if (z.zalloc == NULL) {
106 // XXX: one-time leak here..
112 ret = inflateInit2(&z, -15);
115 ret = inflateReset(&z);
120 z.avail_in = in_size;
122 z.avail_out = *out_size;
124 ret = inflate(&z, Z_NO_FLUSH);
127 *out_size -= z.avail_out;
128 return ret == 1 ? 0 : ret;
132 // time: byte 0 - minute; byte 1 - second; byte 2 - frame
134 static long CDRreadTrack(unsigned char *time)
136 unsigned int start_byte, size;
137 unsigned long cdbuffer_size;
138 int ret, sector, block;
143 sector = MSF2SECT(btoi(time[0]), btoi(time[1]), btoi(time[2]));
145 // avoid division if possible
146 switch (cd_sectors_per_blk) {
149 current_sect_in_blk = 0;
153 current_sect_in_blk = sector % 10;
157 current_sect_in_blk = sector & 15;
160 err("unhandled cd_sectors_per_blk: %d\n", cd_sectors_per_blk);
164 if (block == current_block) {
165 // it's already there, nothing to do
166 //printf("hit sect %d\n", sector);
170 if (sector >= cd_index_len * cd_sectors_per_blk) {
171 err("sector %d is past track end\n", sector);
175 start_byte = cd_index_table[block];
176 if (fseek(cd_file, start_byte, SEEK_SET) != 0) {
177 err("seek error for block %d at %x: ",
183 size = cd_index_table[block + 1] - start_byte;
184 if (size > sizeof(cdbuffer->compressed)) {
185 err("block %d is too large: %u\n", block, size);
189 if (fread(cdbuffer->compressed, 1, size, cd_file) != size) {
190 err("read error for block %d at %x: ", block, start_byte);
195 cdbuffer_size = sizeof(cdbuffer->raw[0]) * cd_sectors_per_blk;
196 switch (cd_compression) {
198 ret = uncompress(cdbuffer->raw[0], &cdbuffer_size, cdbuffer->compressed, size);
201 ret = uncompress2_pcsx(cdbuffer->raw[0], &cdbuffer_size, cdbuffer->compressed, size);
204 ret = pBZ2_bzBuffToBuffDecompress((char *)cdbuffer->raw, (unsigned int *)&cdbuffer_size,
205 (char *)cdbuffer->compressed, size, 0, 0);
208 err("bad cd_compression: %d\n", cd_compression);
213 err("uncompress failed with %d for block %d, sector %d\n",
217 if (cdbuffer_size != sizeof(cdbuffer->raw[0]) * cd_sectors_per_blk)
218 err("cdbuffer_size: %lu != %d, sector %d\n", cdbuffer_size,
219 (int)sizeof(cdbuffer->raw[0]) * cd_sectors_per_blk, sector);
222 current_block = block;
227 static unsigned char *CDRgetBuffer(void)
229 return cdbuffer->raw[current_sect_in_blk] + 12;
233 // sector: byte 0 - minute; byte 1 - second; byte 2 - frame
234 // does NOT uses bcd format
235 static long CDRplay(unsigned char *time)
241 static long CDRstop(void)
246 // gets subchannel data
247 static unsigned char* CDRgetBufferSub(void)
252 static long CDRgetStatus(struct CdrStat *stat) {
253 CDR__getStatus(stat);
260 static long CDRclose(void)
262 if (cd_file != NULL) {
266 if (cd_index_table != NULL) {
267 free(cd_index_table);
268 cd_index_table = NULL;
273 static long CDRshutdown(void)
278 static long CDRinit(void)
280 if (cdbuffer == NULL) {
281 cdbuffer = malloc(sizeof(*cdbuffer));
282 if (cdbuffer == NULL) {
287 #if !defined(_WIN32) && !defined(NO_DYLIB)
288 if (pBZ2_bzBuffToBuffDecompress == NULL) {
289 void *h = dlopen("/usr/lib/libbz2.so.1", RTLD_LAZY);
291 h = dlopen("./lib/libbz2.so.1", RTLD_LAZY);
293 pBZ2_bzBuffToBuffDecompress = dlsym(h, "BZ2_bzBuffToBuffDecompress");
294 if (pBZ2_bzBuffToBuffDecompress == NULL) {
295 err("dlsym bz2: %s", dlerror());
304 static long handle_eboot(void)
308 unsigned int dontcare[8];
309 unsigned int psar_offs;
314 unsigned int dontcare[6];
317 unsigned int cdimg_base;
321 f = fopen(cd_fname, "rb");
323 err("missing file: %s: ", cd_fname);
328 ret = fread(&pbp_hdr, 1, sizeof(pbp_hdr), f);
329 if (ret != sizeof(pbp_hdr)) {
330 err("failed to read pbp\n");
334 ret = fseek(f, pbp_hdr.psar_offs, SEEK_SET);
336 err("failed to seek to %x\n", pbp_hdr.psar_offs);
340 ret = fread(psar_sig, 1, sizeof(psar_sig), f);
341 if (ret != sizeof(psar_sig)) {
342 err("failed to read psar_sig\n");
347 if (strcmp(psar_sig, "PSISOIMG") != 0) {
348 err("bad psar_sig: %s\n", psar_sig);
353 ret = fseek(f, 0x4000 - sizeof(psar_sig), SEEK_CUR);
355 err("failed to seek to ISO index\n");
359 cd_index_len = (0x100000 - 0x4000) / sizeof(index_entry);
360 cd_index_table = malloc((cd_index_len + 1) * sizeof(cd_index_table[0]));
361 if (cd_index_table == NULL)
364 cdimg_base = pbp_hdr.psar_offs + 0x100000;
365 for (i = 0; i < cd_index_len; i++) {
366 ret = fread(&index_entry, 1, sizeof(index_entry), f);
367 if (ret != sizeof(index_entry)) {
368 err("failed to read index_entry #%d\n", i);
372 if (index_entry.size == 0)
375 cd_index_table[i] = cdimg_base + index_entry.offset;
377 cd_index_table[i] = cdimg_base + index_entry.offset + index_entry.size;
379 cd_compression = CDRC_ZLIB2;
380 cd_sectors_per_blk = 16;
383 printf(PFX "Loaded EBOOT CD Image: %s.\n", cd_fname);
387 free(cd_index_table);
388 cd_index_table = NULL;
394 // This function is invoked by the front-end when opening an ISO
396 static long CDRopen(void)
402 } __attribute__((packed)) ztab_entry;
406 unsigned int dontcare;
407 } __attribute__((packed)) znxtab_entry;
408 unsigned int bztab_entry;
411 char table_fname[256];
418 return 0; // it's already open
422 current_sect_in_blk = 0;
424 if (cd_fname == NULL)
427 ext = strrchr(cd_fname, '.');
431 if (strcasecmp(ext, ".pbp") == 0) {
432 return handle_eboot();
436 else if (strcasecmp(ext, ".z") == 0) {
437 cd_compression = CDRC_ZLIB;
438 tabentry_size = sizeof(u.ztab_entry);
439 snprintf(table_fname, sizeof(table_fname), "%s.table", cd_fname);
441 else if (strcasecmp(ext, ".znx") == 0) {
442 cd_compression = CDRC_ZLIB;
443 tabentry_size = sizeof(u.znxtab_entry);
444 snprintf(table_fname, sizeof(table_fname), "%s.table", cd_fname);
446 else if (strcasecmp(ext, ".bz") == 0) {
447 if (pBZ2_bzBuffToBuffDecompress == NULL) {
448 err("libbz2 unavailable for .bz2 handling\n");
451 cd_compression = CDRC_BZ;
452 tabentry_size = sizeof(u.bztab_entry);
453 snprintf(table_fname, sizeof(table_fname), "%s.index", cd_fname);
456 err("unhandled extension: %s\n", ext);
460 f = fopen(table_fname, "rb");
462 err("missing file: %s: ", table_fname);
467 ret = fseek(f, 0, SEEK_END);
469 err("failed to seek\n");
472 table_size = ftell(f);
473 fseek(f, 0, SEEK_SET);
475 if (table_size > 4 * 1024 * 1024) {
476 err(".table too large\n");
480 cd_index_len = table_size / tabentry_size;
482 cd_index_table = malloc((cd_index_len + 1) * sizeof(cd_index_table[0]));
483 if (cd_index_table == NULL)
486 switch (cd_compression) {
488 // a Z.table file is binary where each element represents
489 // one compressed frame.
490 // 4 bytes: the offset of the frame in the .Z file
491 // 2 bytes: the length of the compressed frame
492 // .znx.table has 4 additional bytes (xa header??)
493 u.znxtab_entry.dontcare = 0;
494 for (i = 0; i < cd_index_len; i++) {
495 ret = fread(&u, 1, tabentry_size, f);
496 if (ret != tabentry_size) {
497 err(".table read failed on entry %d/%d\n", i, cd_index_len);
498 goto fail_table_io_read;
500 cd_index_table[i] = u.ztab_entry.offset;
501 //if (u.znxtab_entry.dontcare != 0)
502 // printf("znx %08x!\n", u.znxtab_entry.dontcare);
504 // fake entry, so that we know last compressed block size
505 cd_index_table[i] = u.ztab_entry.offset + u.ztab_entry.size;
506 cd_sectors_per_blk = 1;
509 // the .BZ.table file is arranged so that one entry represents
510 // 10 compressed frames. Each element is a 4 byte unsigned integer
511 // representing the offset in the .BZ file. Last entry is the size
512 // of the compressed file.
513 for (i = 0; i < cd_index_len; i++) {
514 ret = fread(&u.bztab_entry, 1, sizeof(u.bztab_entry), f);
515 if (ret != sizeof(u.bztab_entry)) {
516 err(".table read failed on entry %d/%d\n", i, cd_index_len);
517 goto fail_table_io_read;
519 cd_index_table[i] = u.bztab_entry;
521 cd_sectors_per_blk = 10;
525 cd_file = fopen(cd_fname, "rb");
526 if (cd_file == NULL) {
527 err("failed to open: %s: ", table_fname);
533 printf(PFX "Loaded compressed CD Image: %s.\n", cd_fname);
539 free(cd_index_table);
540 cd_index_table = NULL;
546 #define FUNC(n) { #n, n }
548 static const struct {
561 FUNC(CDRgetBufferSub),
567 void cdrcimg_set_fname(const char *fname)
572 void *cdrcimg_get_sym(const char *sym)
575 for (i = 0; i < sizeof(plugin_funcs) / sizeof(plugin_funcs[0]); i++)
576 if (strcmp(plugin_funcs[i].name, sym) == 0)
577 return plugin_funcs[i].func;