/[pcsx2_0.9.7]/branch/debug/0.X/0.9.X/0.9.7/r3113/plugins/CDVDiso/src/libiso.cpp
ViewVC logotype

Contents of /branch/debug/0.X/0.9.X/0.9.7/r3113/plugins/CDVDiso/src/libiso.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 171 - (show annotations) (download)
Mon Sep 13 16:38:21 2010 UTC (10 years, 2 months ago) by william
File size: 21993 byte(s)
fix warnings

1 #define __USE_LARGEFILE64
2 #define __USE_FILE_OFFSET64
3 #define _FILE_OFFSET_BITS 64
4
5 #ifdef __GNUC__
6 # ifndef _LARGEFILE_SOURCE
7 # define _LARGEFILE_SOURCE
8 # endif
9
10 # ifndef _LARGEFILE64_SOURCE
11 # define _LARGEFILE64_SOURCE
12 # endif
13 #endif
14
15 #ifdef _WIN32
16 #include <windows.h>
17 #endif
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25
26 #include <zlib/zlib.h>
27 #include <bzip2/bzlib.h>
28
29 #include "CDVDiso.h"
30 #include "libiso.h"
31
32 /* some structs from libcdvd by Hiryu & Sjeep (C) 2002 */
33
34 #if defined(_WIN32)
35 #pragma pack(1)
36 #endif
37
38 struct rootDirTocHeader
39 {
40 u16 length; //+00
41 u32 tocLBA; //+02
42 u32 tocLBA_bigend; //+06
43 u32 tocSize; //+0A
44 u32 tocSize_bigend; //+0E
45 u8 dateStamp[8]; //+12
46 u8 reserved[6]; //+1A
47 u8 reserved2; //+20
48 u8 reserved3; //+21
49 #if defined(_WIN32)
50 }; //+22
51 #else
52 } __attribute__((packed));
53 #endif
54
55 struct asciiDate
56 {
57 char year[4];
58 char month[2];
59 char day[2];
60 char hours[2];
61 char minutes[2];
62 char seconds[2];
63 char hundreths[2];
64 char terminator[1];
65 #if defined(_WIN32)
66 };
67 #else
68 } __attribute__((packed));
69 #endif
70
71 struct cdVolDesc
72 {
73 u8 filesystemType; // 0x01 = ISO9660, 0x02 = Joliet, 0xFF = NULL
74 u8 volID[5]; // "CD001"
75 u8 reserved2;
76 u8 reserved3;
77 u8 sysIdName[32];
78 u8 volName[32]; // The ISO9660 Volume Name
79 u8 reserved5[8];
80 u32 volSize; // Volume Size
81 u32 volSizeBig; // Volume Size Big-Endian
82 u8 reserved6[32];
83 u32 unknown1;
84 u32 unknown1_bigend;
85 u16 volDescSize; //+80
86 u16 volDescSize_bigend; //+82
87 u32 unknown3; //+84
88 u32 unknown3_bigend; //+88
89 u32 priDirTableLBA; // LBA of Primary Dir Table //+8C
90 u32 reserved7; //+90
91 u32 secDirTableLBA; // LBA of Secondary Dir Table //+94
92 u32 reserved8; //+98
93 struct rootDirTocHeader rootToc;
94 u8 volSetName[128];
95 u8 publisherName[128];
96 u8 preparerName[128];
97 u8 applicationName[128];
98 u8 copyrightFileName[37];
99 u8 abstractFileName[37];
100 u8 bibliographyFileName[37];
101 struct asciiDate creationDate;
102 struct asciiDate modificationDate;
103 struct asciiDate effectiveDate;
104 struct asciiDate expirationDate;
105 u8 reserved10;
106 u8 reserved11[1166];
107 #if defined(_WIN32)
108 };
109 #else
110 } __attribute__((packed));
111 #endif
112
113
114 #ifdef _WIN32
115 void *_openfile(const char *filename, int flags)
116 {
117 HANDLE handle;
118
119 // printf("_openfile %s, %d\n", filename, flags & O_RDONLY);
120 if (flags & O_WRONLY)
121 {
122 int _flags = CREATE_NEW;
123 if (flags & O_CREAT) _flags = CREATE_ALWAYS;
124 handle = CreateFile(filename, GENERIC_WRITE, 0, NULL, _flags, 0, NULL);
125 }
126 else
127 {
128 handle = CreateFile(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);
129 }
130
131 return handle == INVALID_HANDLE_VALUE ? NULL : handle;
132 }
133
134 u32 _tellfile(void *handle)
135 {
136 u32 ofs;
137 PLONG _ofs = (LONG*) & ofs;
138 _ofs[1] = 0;
139 _ofs[0] = SetFilePointer(handle, 0, &_ofs[1], FILE_CURRENT);
140 return ofs;
141 }
142
143 int _seekfile(void *handle, u64 offset, int whence)
144 {
145 u64 ofs = (u64)offset;
146 PLONG _ofs = (LONG*) & ofs;
147 // printf("_seekfile %p, %d_%d\n", handle, _ofs[1], _ofs[0]);
148 if (whence == SEEK_SET)
149 {
150 SetFilePointer(handle, _ofs[0], &_ofs[1], FILE_BEGIN);
151 }
152 else
153 {
154 SetFilePointer(handle, _ofs[0], &_ofs[1], FILE_END);
155 }
156 return 0;
157 }
158
159 int _readfile(void *handle, void *dst, int size)
160 {
161 DWORD ret;
162
163 // printf("_readfile %p %d\n", handle, size);
164 ReadFile(handle, dst, size, &ret, NULL);
165 // printf("_readfile ret %d; %d\n", ret, GetLastError());
166 return ret;
167 }
168
169 int _writefile(void *handle, void *src, int size)
170 {
171 DWORD ret;
172
173 // printf("_writefile %p, %d\n", handle, size);
174 // _seekfile(handle, _tellfile(handle));
175 WriteFile(handle, src, size, &ret, NULL);
176 // printf("_writefile ret %d\n", ret);
177 return ret;
178 }
179
180 void _closefile(void *handle)
181 {
182 CloseHandle(handle);
183 }
184
185 #else
186
187 void *_openfile(const char *filename, int flags)
188 {
189 printf("_openfile %s %x\n", filename, flags);
190
191 if (flags & O_WRONLY)
192 return fopen64(filename, "wb");
193 else
194 return fopen64(filename, "rb");
195 }
196
197 #include <errno.h>
198
199 u64 _tellfile(void *handle)
200 {
201 s64 cursize = ftell(handle);
202 if (cursize == -1)
203 {
204 // try 64bit
205 cursize = ftello64(handle);
206 if (cursize < -1)
207 {
208 // zero top 32 bits
209 cursize &= 0xffffffff;
210 }
211 }
212 return cursize;
213 }
214
215 int _seekfile(void *handle, u64 offset, int whence)
216 {
217 int seekerr = fseeko64(handle, offset, whence);
218
219 if (seekerr == -1) printf("failed to seek\n");
220
221 return seekerr;
222 }
223
224 int _readfile(void *handle, void *dst, int size)
225 {
226 return fread(dst, 1, size, handle);
227 }
228
229 int _writefile(void *handle, void *src, int size)
230 {
231 return fwrite(src, 1, size, handle);
232 }
233
234 void _closefile(void *handle)
235 {
236 fclose(handle);
237 }
238
239 #endif
240
241 int detect(isoFile *iso)
242 {
243 u8 buf[2448];
244 struct cdVolDesc *volDesc;
245
246 if (isoReadBlock(iso, buf, 16) == -1) return -1;
247
248 volDesc = (struct cdVolDesc *)(buf + 24);
249
250 if (strncmp((char*)volDesc->volID, "CD001", 5)) return 0;
251
252 if (volDesc->rootToc.tocSize == 2048)
253 iso->type = ISOTYPE_CD;
254 else
255 iso->type = ISOTYPE_DVD;
256
257 return 1;
258 }
259
260 int _isoReadZtable(isoFile *iso)
261 {
262 void *handle;
263 char table[256];
264 int size;
265
266 sprintf(table, "%s.table", iso->filename);
267 handle = _openfile(table, O_RDONLY);
268 if (handle == NULL)
269 {
270 printf("Error loading %s\n", table);
271 return -1;
272 }
273
274 _seekfile(handle, 0, SEEK_END);
275 size = _tellfile(handle);
276 iso->Ztable = (char*)malloc(size);
277
278 if (iso->Ztable == NULL)
279 {
280 return -1;
281 }
282
283 _seekfile(handle, 0, SEEK_SET);
284 _readfile(handle, iso->Ztable, size);
285 _closefile(handle);
286
287 iso->blocks = size / 6;
288
289 return 0;
290 }
291
292 int _isoReadZ2table(isoFile *iso)
293 {
294 void *handle;
295 char table[256];
296 u32 *Ztable;
297 int ofs, size, i;
298
299 sprintf(table, "%s.table", iso->filename);
300 handle = _openfile(table, O_RDONLY);
301
302 if (handle == NULL)
303 {
304 printf("Error loading %s\n", table);
305 return -1;
306 }
307
308 _seekfile(handle, 0, SEEK_END);
309 size = _tellfile(handle);
310 Ztable = (u32*)malloc(size);
311
312 if (Ztable == NULL)
313 {
314 return -1;
315 }
316
317 _seekfile(handle, 0, SEEK_SET);
318 _readfile(handle, Ztable, size);
319 _closefile(handle);
320
321 iso->Ztable = (char*)malloc(iso->blocks * 8);
322
323 if (iso->Ztable == NULL)
324 {
325 return -1;
326 }
327
328 ofs = 16;
329
330 for (i = 0; i < iso->blocks; i++)
331 {
332 *(u32*)&iso->Ztable[i*8+0] = ofs;
333 *(u32*)&iso->Ztable[i*8+4] = Ztable[i];
334 ofs += Ztable[i];
335 }
336
337 free(Ztable);
338
339 return 0;
340 }
341
342 int _isoReadBZ2table(isoFile *iso)
343 {
344 void *handle;
345 char table[256];
346 u32 *Ztable;
347 int ofs;
348 int size;
349 int i;
350
351 sprintf(table, "%s.table", iso->filename);
352 handle = _openfile(table, O_RDONLY);
353 if (handle == NULL)
354 {
355 printf("Error loading %s\n", table);
356 return -1;
357 }
358
359 _seekfile(handle, 0, SEEK_END);
360 size = _tellfile(handle);
361 Ztable = (u32*)malloc(size);
362 if (Ztable == NULL) return -1;
363
364 _seekfile(handle, 0, SEEK_SET);
365 _readfile(handle, Ztable, size);
366 _closefile(handle);
367
368 iso->Ztable = (char*)malloc(iso->blocks * 8);
369 if (iso->Ztable == NULL) return -1;
370
371 ofs = 16;
372
373 for (i = 0; i < iso->blocks / 16; i++)
374 {
375 *(u32*)&iso->Ztable[i*8+0] = ofs;
376 *(u32*)&iso->Ztable[i*8+4] = Ztable[i];
377 ofs += Ztable[i];
378 }
379
380 if (iso->blocks & 0xf)
381 {
382 *(u32*)&iso->Ztable[i*8+0] = ofs;
383 *(u32*)&iso->Ztable[i*8+4] = Ztable[i];
384 ofs += Ztable[i];
385 }
386
387 free(Ztable);
388
389 return 0;
390 }
391
392 int _isoReadDtable(isoFile *iso)
393 {
394 int ret;
395 int i;
396
397 _seekfile(iso->handle, 0, SEEK_END);
398 iso->dtablesize = (_tellfile(iso->handle) - 16) / (iso->blocksize + 4);
399 iso->dtable = (u32*)malloc(iso->dtablesize * 4);
400
401 for (i = 0; i < iso->dtablesize; i++)
402 {
403 _seekfile(iso->handle, 16 + (iso->blocksize + 4)*i, SEEK_SET);
404 ret = _readfile(iso->handle, &iso->dtable[i], 4);
405 if (ret < 4) return -1;
406 }
407
408 return 0;
409 }
410
411 int isoDetect(isoFile *iso) // based on florin's CDVDbin detection code :)
412 {
413 char buf[32];
414 int len;
415
416 iso->type = ISOTYPE_ILLEGAL;
417
418 len = strlen(iso->filename);
419 if (len >= 2)
420 {
421 if (!strncmp(iso->filename + (len - 2), ".Z", 2))
422 {
423 iso->flags = ISOFLAGS_Z;
424 iso->blocksize = 2352;
425 _isoReadZtable(iso);
426 return detect(iso) == 1 ? 0 : -1;
427 }
428 }
429
430 _seekfile(iso->handle, 0, SEEK_SET);
431 _readfile(iso->handle, buf, 4);
432
433 if (strncmp(buf, "BDV2", 4) == 0)
434 {
435 iso->flags = ISOFLAGS_BLOCKDUMP;
436 _readfile(iso->handle, &iso->blocksize, 4);
437 _readfile(iso->handle, &iso->blocks, 4);
438 _readfile(iso->handle, &iso->blockofs, 4);
439 _isoReadDtable(iso);
440 return detect(iso) == 1 ? 0 : -1;
441 }
442 else if (strncmp(buf, "Z V2", 4) == 0)
443 {
444 iso->flags = ISOFLAGS_Z2;
445 _readfile(iso->handle, &iso->blocksize, 4);
446 _readfile(iso->handle, &iso->blocks, 4);
447 _readfile(iso->handle, &iso->blockofs, 4);
448 _isoReadZ2table(iso);
449 return detect(iso) == 1 ? 0 : -1;
450 }
451 else if (strncmp(buf, "BZV2", 4) == 0)
452 {
453 iso->flags = ISOFLAGS_BZ2;
454 _readfile(iso->handle, &iso->blocksize, 4);
455 _readfile(iso->handle, &iso->blocks, 4);
456 _readfile(iso->handle, &iso->blockofs, 4);
457 iso->buflsn = -1;
458 iso->buffer = (u8*)malloc(iso->blocksize * 16);
459 if (iso->buffer == NULL) return -1;
460 _isoReadBZ2table(iso);
461 return detect(iso) == 1 ? 0 : -1;
462 }
463 else
464 {
465 iso->blocks = 16;
466 }
467
468 // ISO 2048
469 iso->blocksize = 2048;
470 iso->offset = 0;
471 iso->blockofs = 24;
472 if (detect(iso) == 1) return 0;
473
474 // RAW 2336
475 iso->blocksize = 2336;
476 iso->offset = 0;
477 iso->blockofs = 16;
478 if (detect(iso) == 1) return 0;
479
480 // RAW 2352
481 iso->blocksize = 2352;
482 iso->offset = 0;
483 iso->blockofs = 0;
484 if (detect(iso) == 1) return 0;
485
486 // RAWQ 2448
487 iso->blocksize = 2448;
488 iso->offset = 0;
489 iso->blockofs = 0;
490 if (detect(iso) == 1) return 0;
491
492 // NERO ISO 2048
493 iso->blocksize = 2048;
494 iso->offset = 150 * 2048;
495 iso->blockofs = 24;
496 if (detect(iso) == 1) return 0;
497
498 // NERO RAW 2352
499 iso->blocksize = 2352;
500 iso->offset = 150 * 2048;
501 iso->blockofs = 0;
502 if (detect(iso) == 1) return 0;
503
504 // NERO RAWQ 2448
505 iso->blocksize = 2448;
506 iso->offset = 150 * 2048;
507 iso->blockofs = 0;
508 if (detect(iso) == 1) return 0;
509
510 // ISO 2048
511 iso->blocksize = 2048;
512 iso->offset = -8;
513 iso->blockofs = 24;
514 if (detect(iso) == 1) return 0;
515
516 // RAW 2352
517 iso->blocksize = 2352;
518 iso->offset = -8;
519 iso->blockofs = 0;
520 if (detect(iso) == 1) return 0;
521
522 // RAWQ 2448
523 iso->blocksize = 2448;
524 iso->offset = -8;
525 iso->blockofs = 0;
526 if (detect(iso) == 1) return 0;
527
528 iso->offset = 0;
529 iso->blocksize = 2352;
530 iso->type = ISOTYPE_AUDIO;
531 return 0;
532
533 return -1;
534 }
535
536 isoFile *isoOpen(const char *filename)
537 {
538 isoFile *iso;
539 int i;
540
541 iso = (isoFile*)malloc(sizeof(isoFile));
542 if (iso == NULL) return NULL;
543
544 memset(iso, 0, sizeof(isoFile));
545 strcpy(iso->filename, filename);
546
547 iso->handle = _openfile(iso->filename, O_RDONLY);
548 if (iso->handle == NULL)
549 {
550 printf("Error loading %s\n", iso->filename);
551 return NULL;
552 }
553
554 if (isoDetect(iso) == -1) return NULL;
555
556 printf("detected blocksize = %d\n", iso->blocksize);
557
558 if (strlen(iso->filename) > 3 && strncmp(iso->filename + (strlen(iso->filename) - 3), "I00", 3) == 0)
559 {
560 _closefile(iso->handle);
561 iso->flags |= ISOFLAGS_MULTI;
562 iso->blocks = 0;
563 for (i = 0; i < 8; i++)
564 {
565 iso->filename[strlen(iso->filename) - 1] = '0' + i;
566 iso->multih[i].handle = _openfile(iso->filename, O_RDONLY);
567 if (iso->multih[i].handle == NULL)
568 {
569 break;
570 }
571 iso->multih[i].slsn = iso->blocks;
572 _seekfile(iso->multih[i].handle, 0, SEEK_END);
573 iso->blocks += (u32)((_tellfile(iso->multih[i].handle) - iso->offset) /
574 (iso->blocksize));
575 iso->multih[i].elsn = iso->blocks - 1;
576 }
577
578 if (i == 0)
579 {
580 return NULL;
581 }
582 }
583
584 if (iso->flags == 0)
585 {
586 _seekfile(iso->handle, 0, SEEK_END);
587 iso->blocks = (u32)((_tellfile(iso->handle) - iso->offset) /
588 (iso->blocksize));
589 }
590
591
592 printf("isoOpen: %s ok\n", iso->filename);
593 printf("offset = %d\n", iso->offset);
594 printf("blockofs = %d\n", iso->blockofs);
595 printf("blocksize = %d\n", iso->blocksize);
596 printf("blocks = %d\n", iso->blocks);
597 printf("type = %d\n", iso->type);
598
599 return iso;
600 }
601
602 isoFile *isoCreate(const char *filename, int flags)
603 {
604 isoFile *iso;
605 char Zfile[256];
606
607 iso = (isoFile*)malloc(sizeof(isoFile));
608 if (iso == NULL) return NULL;
609
610 memset(iso, 0, sizeof(isoFile));
611 strcpy(iso->filename, filename);
612 iso->flags = flags;
613 iso->offset = 0;
614 iso->blockofs = 24;
615 iso->blocksize = CD_FRAMESIZE_RAW;
616 iso->blocksize = 2048;
617
618 if (iso->flags & (ISOFLAGS_Z | ISOFLAGS_Z2 | ISOFLAGS_BZ2))
619 {
620 sprintf(Zfile, "%s.table", iso->filename);
621 iso->htable = _openfile(Zfile, O_WRONLY);
622 if (iso->htable == NULL)
623 {
624 return NULL;
625 }
626 }
627
628 iso->handle = _openfile(iso->filename, O_WRONLY | O_CREAT);
629 if (iso->handle == NULL)
630 {
631 printf("Error loading %s\n", iso->filename);
632 return NULL;
633 }
634 printf("isoCreate: %s ok\n", iso->filename);
635 printf("offset = %d\n", iso->offset);
636
637 return iso;
638 }
639
640 int isoSetFormat(isoFile *iso, int blockofs, int blocksize, int blocks)
641 {
642 iso->blocksize = blocksize;
643 iso->blocks = blocks;
644 iso->blockofs = blockofs;
645 printf("blockofs = %d\n", iso->blockofs);
646 printf("blocksize = %d\n", iso->blocksize);
647 printf("blocks = %d\n", iso->blocks);
648 if (iso->flags & ISOFLAGS_Z2)
649 {
650 if (_writefile(iso->handle, "Z V2", 4) < 4) return -1;
651 if (_writefile(iso->handle, &blocksize, 4) < 4) return -1;
652 if (_writefile(iso->handle, &blocks, 4) < 4) return -1;
653 if (_writefile(iso->handle, &blockofs, 4) < 4) return -1;
654 }
655 if (iso->flags & ISOFLAGS_BZ2)
656 {
657 if (_writefile(iso->handle, "BZV2", 4) < 4) return -1;
658 if (_writefile(iso->handle, &blocksize, 4) < 4) return -1;
659 if (_writefile(iso->handle, &blocks, 4) < 4) return -1;
660 if (_writefile(iso->handle, &blockofs, 4) < 4) return -1;
661 iso->buflsn = -1;
662 iso->buffer = (u8*)malloc(iso->blocksize * 16);
663 if (iso->buffer == NULL) return -1;
664 }
665 if (iso->flags & ISOFLAGS_BLOCKDUMP)
666 {
667 if (_writefile(iso->handle, "BDV2", 4) < 4) return -1;
668 if (_writefile(iso->handle, &blocksize, 4) < 4) return -1;
669 if (_writefile(iso->handle, &blocks, 4) < 4) return -1;
670 if (_writefile(iso->handle, &blockofs, 4) < 4) return -1;
671 }
672
673 return 0;
674 }
675
676 s32 MSFtoLSN(u8 *Time)
677 {
678 u32 lsn;
679
680 lsn = Time[2];
681 lsn += (Time[1] - 2) * 75;
682 lsn += Time[0] * 75 * 60;
683 return lsn;
684 }
685
686 void LSNtoMSF(u8 *Time, s32 lsn)
687 {
688 u8 m, s, f;
689
690 lsn += 150;
691 m = lsn / 4500; // minuten
692 lsn = lsn - m * 4500; // minuten rest
693 s = lsn / 75; // sekunden
694 f = lsn - (s * 75); // sekunden rest
695 Time[0] = itob(m);
696 Time[1] = itob(s);
697 Time[2] = itob(f);
698 }
699
700 int _isoReadBlock(isoFile *iso, u8 *dst, int lsn)
701 {
702 u64 ofs = (u64)lsn * iso->blocksize + iso->offset;
703 int ret;
704
705 // printf("_isoReadBlock %d, blocksize=%d, blockofs=%d\n", lsn, iso->blocksize, iso->blockofs);
706 memset(dst, 0, iso->blockofs);
707 _seekfile(iso->handle, ofs, SEEK_SET);
708 ret = _readfile(iso->handle, dst + iso->blockofs, iso->blocksize);
709 if (ret < iso->blocksize)
710 {
711 printf("read error %d\n", ret);
712 return -1;
713 }
714
715 return 0;
716 }
717
718 int _isoReadBlockZ(isoFile *iso, u8 *dst, int lsn)
719 {
720 u32 pos, p;
721 uLongf size;
722 u8 Zbuf[CD_FRAMESIZE_RAW*2];
723 int ret;
724
725 // printf("_isoReadBlockZ %d, %d\n", lsn, iso->blocksize);
726 pos = *(unsigned long*) & iso->Ztable[lsn * 6];
727 p = *(unsigned short*) & iso->Ztable[lsn * 6 + 4];
728 // printf("%d, %d\n", pos, p);
729 _seekfile(iso->handle, pos, SEEK_SET);
730 ret = _readfile(iso->handle, Zbuf, p);
731 if (ret < p)
732 {
733 printf("error reading block!!\n");
734 return -1;
735 }
736
737 size = CD_FRAMESIZE_RAW;
738 uncompress(dst, &size, Zbuf, p);
739
740 return 0;
741 }
742
743 int _isoReadBlockZ2(isoFile *iso, u8 *dst, int lsn)
744 {
745 u32 pos, p;
746 uLongf size;
747 u8 Zbuf[16*1024];
748 int ret;
749
750 // printf("_isoReadBlockZ2 %d, %d\n", lsn, iso->blocksize);
751 pos = *(u32*) & iso->Ztable[lsn*8];
752 p = *(u32*) & iso->Ztable[lsn*8+4];
753 // printf("%d, %d\n", pos, p);
754 _seekfile(iso->handle, pos, SEEK_SET);
755 ret = _readfile(iso->handle, Zbuf, p);
756 if (ret < p)
757 {
758 printf("error reading block!!\n");
759 return -1;
760 }
761
762 size = iso->blocksize;
763 uncompress(dst + iso->blockofs, &size, Zbuf, p);
764
765 return 0;
766 }
767
768 int _isoReadBlockBZ2(isoFile *iso, u8 *dst, int lsn)
769 {
770 u32 pos, p;
771 u32 size;
772 u8 Zbuf[64*1024];
773 int ret;
774
775 if ((lsn / 16) == iso->buflsn)
776 {
777 memset(dst, 0, iso->blockofs);
778 memcpy(dst + iso->blockofs, iso->buffer + (iso->blocksize*(lsn&0xf)), iso->blocksize);
779 return 0;
780 }
781
782 iso->buflsn = lsn / 16;
783 // printf("_isoReadBlockBZ2 %d, %d\n", lsn, iso->blocksize);
784 pos = *(u32*) & iso->Ztable[(lsn/16)*8];
785 p = *(u32*) & iso->Ztable[(lsn/16)*8+4];
786 // printf("%d, %d\n", pos, p);
787 _seekfile(iso->handle, pos, SEEK_SET);
788 ret = _readfile(iso->handle, Zbuf, p);
789
790 if (ret < p)
791 {
792 printf("error reading block!!\n");
793 return -1;
794 }
795
796 size = iso->blocksize * 64;
797 ret = BZ2_bzBuffToBuffDecompress((s8*)iso->buffer, &size, (s8*)Zbuf, p, 0, 0);
798
799 if (ret != BZ_OK)
800 {
801 printf("_isoReadBlockBZ2 %d, %d\n", lsn, iso->blocksize);
802 printf("%d, %d\n", pos, p);
803 printf("error on BZ2: %d\n", ret);
804 }
805
806 memset(dst, 0, iso->blockofs);
807 memcpy(dst + iso->blockofs, iso->buffer + (iso->blocksize*(lsn&0xf)), iso->blocksize);
808
809 return 0;
810 }
811
812 int _isoReadBlockD(isoFile *iso, u8 *dst, int lsn)
813 {
814 int ret;
815 int i;
816
817 // printf("_isoReadBlockD %d, blocksize=%d, blockofs=%d\n", lsn, iso->blocksize, iso->blockofs);
818 memset(dst, 0, iso->blockofs);
819 for (i = 0; i < iso->dtablesize;i++)
820 {
821 if (iso->dtable[i] != lsn) continue;
822
823 _seekfile(iso->handle, 16 + i*(iso->blocksize + 4) + 4, SEEK_SET);
824 ret = _readfile(iso->handle, dst + iso->blockofs, iso->blocksize);
825 if (ret < iso->blocksize) return -1;
826
827 return 0;
828 }
829 printf("block %d not found in dump\n", lsn);
830
831 return -1;
832 }
833
834 int _isoReadBlockM(isoFile *iso, u8 *dst, int lsn)
835 {
836 u64 ofs;
837 int ret;
838 int i;
839
840 for (i = 0; i < 8; i++)
841 {
842 if (lsn >= iso->multih[i].slsn &&
843 lsn <= iso->multih[i].elsn)
844 {
845 break;
846 }
847 }
848 if (i == 8) return -1;
849
850 ofs = (u64)(lsn - iso->multih[i].slsn) * iso->blocksize + iso->offset;
851 // printf("_isoReadBlock %d, blocksize=%d, blockofs=%d\n", lsn, iso->blocksize, iso->blockofs);
852 memset(dst, 0, iso->blockofs);
853 _seekfile(iso->multih[i].handle, ofs, SEEK_SET);
854 ret = _readfile(iso->multih[i].handle, dst + iso->blockofs, iso->blocksize);
855
856 if (ret < iso->blocksize)
857 {
858 printf("read error %d\n", ret);
859 return -1;
860 }
861
862 return 0;
863 }
864
865 int isoReadBlock(isoFile *iso, u8 *dst, int lsn)
866 {
867 int ret;
868
869 if (lsn > iso->blocks)
870 {
871 printf("isoReadBlock: %d > %d\n", lsn, iso->blocks);
872 return -1;
873 }
874
875 if (iso->flags & ISOFLAGS_Z)
876 ret = _isoReadBlockZ(iso, dst, lsn);
877 else if (iso->flags & ISOFLAGS_Z2)
878 ret = _isoReadBlockZ2(iso, dst, lsn);
879 else if (iso->flags & ISOFLAGS_BLOCKDUMP)
880 ret = _isoReadBlockD(iso, dst, lsn);
881 else if (iso->flags & ISOFLAGS_MULTI)
882 ret = _isoReadBlockM(iso, dst, lsn);
883 else if (iso->flags & ISOFLAGS_BZ2)
884 ret = _isoReadBlockBZ2(iso, dst, lsn);
885 else
886 ret = _isoReadBlock(iso, dst, lsn);
887
888 if (ret == -1) return ret;
889
890 if (iso->type == ISOTYPE_CD)
891 {
892 LSNtoMSF(dst + 12, lsn);
893 dst[15] = 2;
894 }
895
896 return 0;
897 }
898
899
900 int _isoWriteBlock(isoFile *iso, u8 *src, int lsn)
901 {
902 u64 ofs = (u64)lsn * iso->blocksize + iso->offset;
903 int ret;
904
905 // printf("_isoWriteBlock %d (ofs=%d)\n", iso->blocksize, ofs);
906 _seekfile(iso->handle, ofs, SEEK_SET);
907 ret = _writefile(iso->handle, src + iso->blockofs, iso->blocksize);
908 // printf("_isoWriteBlock %d\n", ret);
909 if (ret < iso->blocksize) return -1;
910
911 return 0;
912 }
913
914 int _isoWriteBlockZ(isoFile *iso, u8 *src, int lsn)
915 {
916 u32 pos;
917 uLongf size;
918 u8 Zbuf[CD_FRAMESIZE_RAW];
919 int ret;
920
921 // printf("_isoWriteBlockZ %d\n", iso->blocksize);
922 size = 2352;
923 compress(Zbuf, &size, src, 2352);
924 // printf("_isoWriteBlockZ %d\n", size);
925
926 pos = (u32)_tellfile(iso->handle);
927 ret = _writefile(iso->htable, &pos, 4);
928 if (ret < 4) return -1;
929 ret = _writefile(iso->htable, &size, 2);
930 if (ret < 2) return -1;
931
932 ret = _writefile(iso->handle, Zbuf, size);
933 // printf("_isoWriteBlockZ %d\n", ret);
934 if (ret < size)
935 {
936 printf("error writing block!!\n");
937 return -1;
938 }
939
940 return 0;
941 }
942
943 int _isoWriteBlockZ2(isoFile *iso, u8 *src, int lsn)
944 {
945 uLongf size;
946 u8 Zbuf[1024*16];
947 int ret;
948
949 // printf("_isoWriteBlockZ %d\n", iso->blocksize);
950 size = 1024 * 16;
951 compress(Zbuf, &size, src + iso->blockofs, iso->blocksize);
952 // printf("_isoWriteBlockZ %d\n", size);
953
954 ret = _writefile(iso->htable, (u8*) & size, 4);
955 if (ret < 4) return -1;
956 ret = _writefile(iso->handle, Zbuf, size);
957 // printf("_isoWriteBlockZ %d\n", ret);
958 if (ret < size)
959 {
960 printf("error writing block!!\n");
961 return -1;
962 }
963
964 return 0;
965 }
966
967 int _isoWriteBlockD(isoFile *iso, u8 *src, int lsn)
968 {
969 int ret;
970
971 // printf("_isoWriteBlock %d (ofs=%d)\n", iso->blocksize, ofs);
972 ret = _writefile(iso->handle, &lsn, 4);
973 if (ret < 4) return -1;
974 ret = _writefile(iso->handle, src + iso->blockofs, iso->blocksize);
975 // printf("_isoWriteBlock %d\n", ret);
976 if (ret < iso->blocksize) return -1;
977
978 return 0;
979 }
980
981 int _isoWriteBlockBZ2(isoFile *iso, u8 *src, int lsn)
982 {
983 u32 size;
984 u8 Zbuf[64*1024];
985 int blocks;
986 int ret;
987
988 memcpy(iso->buffer + (iso->blocksize*(lsn&0xf)), src + iso->blockofs, iso->blocksize);
989
990 if (lsn == (iso->blocks - 1))
991 {
992 blocks = (lsn & 0xf) + 1;
993 }
994 else
995 {
996 blocks = 16;
997 if ((lsn & 0xf) != 0xf) return 0;
998 }
999
1000 // printf("_isoWriteBlockBZ2 %d\n", iso->blocksize);
1001 size = 64 * 1024;
1002 ret = BZ2_bzBuffToBuffCompress((s8*)Zbuf, (u32*) & size, (s8*)iso->buffer, iso->blocksize * blocks, 9, 0, 30);
1003
1004 if (ret != BZ_OK)
1005 {
1006 printf("error on BZ2: %d\n", ret);
1007 }
1008
1009 // printf("_isoWriteBlockBZ2 %d\n", size);
1010
1011 ret = _writefile(iso->htable, (u8*) & size, 4);
1012 if (ret < 4) return -1;
1013 ret = _writefile(iso->handle, Zbuf, size);
1014 // printf("_isoWriteBlockZ %d\n", ret);
1015
1016 if (ret < size)
1017 {
1018 printf("error writing block!!\n");
1019 return -1;
1020 }
1021
1022 return 0;
1023 }
1024
1025 int isoWriteBlock(isoFile *iso, u8 *src, int lsn)
1026 {
1027 int ret;
1028
1029 if (iso->flags & ISOFLAGS_Z)
1030 ret = _isoWriteBlockZ(iso, src, lsn);
1031 else if (iso->flags & ISOFLAGS_Z2)
1032 ret = _isoWriteBlockZ2(iso, src, lsn);
1033 else if (iso->flags & ISOFLAGS_BLOCKDUMP)
1034 ret = _isoWriteBlockD(iso, src, lsn);
1035 else if (iso->flags & ISOFLAGS_BZ2)
1036 ret = _isoWriteBlockBZ2(iso, src, lsn);
1037 else
1038 ret = _isoWriteBlock(iso, src, lsn);
1039
1040 if (ret == -1) return ret;
1041 return 0;
1042 }
1043
1044 void isoClose(isoFile *iso)
1045 {
1046 if (iso->handle) _closefile(iso->handle);
1047 if (iso->htable) _closefile(iso->htable);
1048 if (iso->buffer) free(iso->buffer);
1049
1050 free(iso);
1051 }
1052

  ViewVC Help
Powered by ViewVC 1.1.22