+ if (compr_img != NULL) {
+ free(compr_img);
+ compr_img = NULL;
+ }
+ return -1;
+}
+
+static int handlecbin(const char *isofile) {
+ struct
+ {
+ char magic[4];
+ unsigned int header_size;
+ unsigned long long total_bytes;
+ unsigned int block_size;
+ unsigned char ver; // 1
+ unsigned char align;
+ unsigned char rsv_06[2];
+ } ciso_hdr;
+ const char *ext = NULL;
+ unsigned int index = 0, plain;
+ int i, ret;
+
+ if (strlen(isofile) >= 5)
+ ext = isofile + strlen(isofile) - 5;
+ if (ext == NULL || (strcasecmp(ext + 1, ".cbn") != 0 && strcasecmp(ext, ".cbin") != 0))
+ return -1;
+
+ ret = fread(&ciso_hdr, 1, sizeof(ciso_hdr), cdHandle);
+ if (ret != sizeof(ciso_hdr)) {
+ fprintf(stderr, "failed to read ciso header\n");
+ return -1;
+ }
+
+ if (strncmp(ciso_hdr.magic, "CISO", 4) != 0 || ciso_hdr.total_bytes <= 0 || ciso_hdr.block_size <= 0) {
+ fprintf(stderr, "bad ciso header\n");
+ return -1;
+ }
+ if (ciso_hdr.header_size != 0 && ciso_hdr.header_size != sizeof(ciso_hdr)) {
+ ret = fseek(cdHandle, ciso_hdr.header_size, SEEK_SET);
+ if (ret != 0) {
+ fprintf(stderr, "failed to seek to %x\n", ciso_hdr.header_size);
+ return -1;
+ }
+ }
+
+ compr_img = calloc(1, sizeof(*compr_img));
+ if (compr_img == NULL)
+ goto fail_io;
+
+ compr_img->block_shift = 0;
+ compr_img->current_block = (unsigned int)-1;
+
+ compr_img->index_len = ciso_hdr.total_bytes / ciso_hdr.block_size;
+ compr_img->index_table = malloc((compr_img->index_len + 1) * sizeof(compr_img->index_table[0]));
+ if (compr_img->index_table == NULL)
+ goto fail_io;
+
+ ret = fread(compr_img->index_table, sizeof(compr_img->index_table[0]), compr_img->index_len, cdHandle);
+ if (ret != compr_img->index_len) {
+ fprintf(stderr, "failed to read index table\n");
+ goto fail_index;
+ }
+
+ for (i = 0; i < compr_img->index_len + 1; i++) {
+ index = compr_img->index_table[i];
+ plain = index & 0x80000000;
+ index &= 0x7fffffff;
+ compr_img->index_table[i] = (index << ciso_hdr.align) | plain;
+ }
+ if ((long long)index << ciso_hdr.align >= 0x80000000ll)
+ fprintf(stderr, "warning: ciso img too large, expect problems\n");
+
+ return 0;
+
+fail_index:
+ free(compr_img->index_table);
+ compr_img->index_table = NULL;
+fail_io:
+ if (compr_img != NULL) {
+ free(compr_img);
+ compr_img = NULL;
+ }