/[pcsx2_0.9.7]/trunk/plugins/CDVDiso/src/libiso.cpp
ViewVC logotype

Contents of /trunk/plugins/CDVDiso/src/libiso.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 185 - (show annotations) (download)
Mon Sep 13 19:49:07 2010 UTC (9 years, 4 months ago) by william
File size: 22041 byte(s)
Auto Commited Import of: pcsx2-0.9.7-DEBUG (upstream: v0.9.7.3761 local: v0.9.7.165-latest) in ./trunk
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.h"
27 #include "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 u64 _tellfile(void *handle)
135 {
136 u64 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, const 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 FILE* fp = (FILE*)handle;
202 s64 cursize = ftell(fp);
203 if (cursize == -1)
204 {
205 // try 64bit
206 cursize = ftello64(fp);
207 if (cursize < -1)
208 {
209 // zero top 32 bits
210 cursize &= 0xffffffff;
211 }
212 }
213 return cursize;
214 }
215
216 int _seekfile(void *handle, u64 offset, int whence)
217 {
218 int seekerr = fseeko64((FILE*)handle, offset, whence);
219
220 if (seekerr == -1) printf("failed to seek\n");
221
222 return seekerr;
223 }
224
225 int _readfile(void *handle, void *dst, int size)
226 {
227 return fread(dst, 1, size, (FILE*)handle);
228 }
229
230 int _writefile(void *handle, const void *src, int size)
231 {
232 return fwrite(src, 1, size, (FILE*)handle);
233 }
234
235 void _closefile(void *handle)
236 {
237 fclose((FILE*)handle);
238 }
239
240 #endif
241
242 int detect(isoFile *iso)
243 {
244 u8 buf[2448];
245 struct cdVolDesc *volDesc;
246
247 if (isoReadBlock(iso, buf, 16) == -1) return -1;
248
249 volDesc = (struct cdVolDesc *)(buf + 24);
250
251 if (strncmp((char*)volDesc->volID, "CD001", 5)) return 0;
252
253 if (volDesc->rootToc.tocSize == 2048)
254 iso->type = ISOTYPE_CD;
255 else
256 iso->type = ISOTYPE_DVD;
257
258 return 1;
259 }
260
261 int _isoReadZtable(isoFile *iso)
262 {
263 void *handle;
264 char table[256];
265 int size;
266
267 sprintf(table, "%s.table", iso->filename);
268 handle = _openfile(table, O_RDONLY);
269 if (handle == NULL)
270 {
271 printf("Error loading %s\n", table);
272 return -1;
273 }
274
275 _seekfile(handle, 0, SEEK_END);
276 size = _tellfile(handle);
277 iso->Ztable = (char*)malloc(size);
278
279 if (iso->Ztable == NULL)
280 {
281 return -1;
282 }
283
284 _seekfile(handle, 0, SEEK_SET);
285 _readfile(handle, iso->Ztable, size);
286 _closefile(handle);
287
288 iso->blocks = size / 6;
289
290 return 0;
291 }
292
293 int _isoReadZ2table(isoFile *iso)
294 {
295 void *handle;
296 char table[256];
297 u32 *Ztable;
298 int ofs, size, i;
299
300 sprintf(table, "%s.table", iso->filename);
301 handle = _openfile(table, O_RDONLY);
302
303 if (handle == NULL)
304 {
305 printf("Error loading %s\n", table);
306 return -1;
307 }
308
309 _seekfile(handle, 0, SEEK_END);
310 size = _tellfile(handle);
311 Ztable = (u32*)malloc(size);
312
313 if (Ztable == NULL)
314 {
315 return -1;
316 }
317
318 _seekfile(handle, 0, SEEK_SET);
319 _readfile(handle, Ztable, size);
320 _closefile(handle);
321
322 iso->Ztable = (char*)malloc(iso->blocks * 8);
323
324 if (iso->Ztable == NULL)
325 {
326 return -1;
327 }
328
329 ofs = 16;
330
331 for (i = 0; i < iso->blocks; i++)
332 {
333 *(u32*)&iso->Ztable[i*8+0] = ofs;
334 *(u32*)&iso->Ztable[i*8+4] = Ztable[i];
335 ofs += Ztable[i];
336 }
337
338 free(Ztable);
339
340 return 0;
341 }
342
343 int _isoReadBZ2table(isoFile *iso)
344 {
345 void *handle;
346 char table[256];
347 u32 *Ztable;
348 int ofs;
349 int size;
350 int i;
351
352 sprintf(table, "%s.table", iso->filename);
353 handle = _openfile(table, O_RDONLY);
354 if (handle == NULL)
355 {
356 printf("Error loading %s\n", table);
357 return -1;
358 }
359
360 _seekfile(handle, 0, SEEK_END);
361 size = _tellfile(handle);
362 Ztable = (u32*)malloc(size);
363 if (Ztable == NULL) return -1;
364
365 _seekfile(handle, 0, SEEK_SET);
366 _readfile(handle, Ztable, size);
367 _closefile(handle);
368
369 iso->Ztable = (char*)malloc(iso->blocks * 8);
370 if (iso->Ztable == NULL) return -1;
371
372 ofs = 16;
373
374 for (i = 0; i < iso->blocks / 16; i++)
375 {
376 *(u32*)&iso->Ztable[i*8+0] = ofs;
377 *(u32*)&iso->Ztable[i*8+4] = Ztable[i];
378 ofs += Ztable[i];
379 }
380
381 if (iso->blocks & 0xf)
382 {
383 *(u32*)&iso->Ztable[i*8+0] = ofs;
384 *(u32*)&iso->Ztable[i*8+4] = Ztable[i];
385 ofs += Ztable[i];
386 }
387
388 free(Ztable);
389
390 return 0;
391 }
392
393 int _isoReadDtable(isoFile *iso)
394 {
395 int ret;
396 int i;
397
398 _seekfile(iso->handle, 0, SEEK_END);
399 iso->dtablesize = (_tellfile(iso->handle) - 16) / (iso->blocksize + 4);
400 iso->dtable = (u32*)malloc(iso->dtablesize * 4);
401
402 for (i = 0; i < iso->dtablesize; i++)
403 {
404 _seekfile(iso->handle, 16 + (iso->blocksize + 4)*i, SEEK_SET);
405 ret = _readfile(iso->handle, &iso->dtable[i], 4);
406 if (ret < 4) return -1;
407 }
408
409 return 0;
410 }
411
412 int isoDetect(isoFile *iso) // based on florin's CDVDbin detection code :)
413 {
414 char buf[32];
415 int len;
416
417 iso->type = ISOTYPE_ILLEGAL;
418
419 len = strlen(iso->filename);
420 if (len >= 2)
421 {
422 if (!strncmp(iso->filename + (len - 2), ".Z", 2))
423 {
424 iso->flags = ISOFLAGS_Z;
425 iso->blocksize = 2352;
426 _isoReadZtable(iso);
427 return detect(iso) == 1 ? 0 : -1;
428 }
429 }
430
431 _seekfile(iso->handle, 0, SEEK_SET);
432 _readfile(iso->handle, buf, 4);
433
434 if (strncmp(buf, "BDV2", 4) == 0)
435 {
436 iso->flags = ISOFLAGS_BLOCKDUMP;
437 _readfile(iso->handle, &iso->blocksize, 4);
438 _readfile(iso->handle, &iso->blocks, 4);
439 _readfile(iso->handle, &iso->blockofs, 4);
440 _isoReadDtable(iso);
441 return detect(iso) == 1 ? 0 : -1;
442 }
443 else if (strncmp(buf, "Z V2", 4) == 0)
444 {
445 iso->flags = ISOFLAGS_Z2;
446 _readfile(iso->handle, &iso->blocksize, 4);
447 _readfile(iso->handle, &iso->blocks, 4);
448 _readfile(iso->handle, &iso->blockofs, 4);
449 _isoReadZ2table(iso);
450 return detect(iso) == 1 ? 0 : -1;
451 }
452 else if (strncmp(buf, "BZV2", 4) == 0)
453 {
454 iso->flags = ISOFLAGS_BZ2;
455 _readfile(iso->handle, &iso->blocksize, 4);
456 _readfile(iso->handle, &iso->blocks, 4);
457 _readfile(iso->handle, &iso->blockofs, 4);
458 iso->buflsn = -1;
459 iso->buffer = (u8*)malloc(iso->blocksize * 16);
460 if (iso->buffer == NULL) return -1;
461 _isoReadBZ2table(iso);
462 return detect(iso) == 1 ? 0 : -1;
463 }
464 else
465 {
466 iso->blocks = 16;
467 }
468
469 // ISO 2048
470 iso->blocksize = 2048;
471 iso->offset = 0;
472 iso->blockofs = 24;
473 if (detect(iso) == 1) return 0;
474
475 // RAW 2336
476 iso->blocksize = 2336;
477 iso->offset = 0;
478 iso->blockofs = 16;
479 if (detect(iso) == 1) return 0;
480
481 // RAW 2352
482 iso->blocksize = 2352;
483 iso->offset = 0;
484 iso->blockofs = 0;
485 if (detect(iso) == 1) return 0;
486
487 // RAWQ 2448
488 iso->blocksize = 2448;
489 iso->offset = 0;
490 iso->blockofs = 0;
491 if (detect(iso) == 1) return 0;
492
493 // NERO ISO 2048
494 iso->blocksize = 2048;
495 iso->offset = 150 * 2048;
496 iso->blockofs = 24;
497 if (detect(iso) == 1) return 0;
498
499 // NERO RAW 2352
500 iso->blocksize = 2352;
501 iso->offset = 150 * 2048;
502 iso->blockofs = 0;
503 if (detect(iso) == 1) return 0;
504
505 // NERO RAWQ 2448
506 iso->blocksize = 2448;
507 iso->offset = 150 * 2048;
508 iso->blockofs = 0;
509 if (detect(iso) == 1) return 0;
510
511 // ISO 2048
512 iso->blocksize = 2048;
513 iso->offset = -8;
514 iso->blockofs = 24;
515 if (detect(iso) == 1) return 0;
516
517 // RAW 2352
518 iso->blocksize = 2352;
519 iso->offset = -8;
520 iso->blockofs = 0;
521 if (detect(iso) == 1) return 0;
522
523 // RAWQ 2448
524 iso->blocksize = 2448;
525 iso->offset = -8;
526 iso->blockofs = 0;
527 if (detect(iso) == 1) return 0;
528
529 iso->offset = 0;
530 iso->blocksize = 2352;
531 iso->type = ISOTYPE_AUDIO;
532 return 0;
533
534 return -1;
535 }
536
537 isoFile *isoOpen(const char *filename)
538 {
539 isoFile *iso;
540 int i;
541
542 iso = (isoFile*)malloc(sizeof(isoFile));
543 if (iso == NULL) return NULL;
544
545 memset(iso, 0, sizeof(isoFile));
546 strcpy(iso->filename, filename);
547
548 iso->handle = _openfile(iso->filename, O_RDONLY);
549 if (iso->handle == NULL)
550 {
551 printf("Error loading %s\n", iso->filename);
552 return NULL;
553 }
554
555 if (isoDetect(iso) == -1) return NULL;
556
557 printf("detected blocksize = %d\n", iso->blocksize);
558
559 if (strlen(iso->filename) > 3 && strncmp(iso->filename + (strlen(iso->filename) - 3), "I00", 3) == 0)
560 {
561 _closefile(iso->handle);
562 iso->flags |= ISOFLAGS_MULTI;
563 iso->blocks = 0;
564 for (i = 0; i < 8; i++)
565 {
566 iso->filename[strlen(iso->filename) - 1] = '0' + i;
567 iso->multih[i].handle = _openfile(iso->filename, O_RDONLY);
568 if (iso->multih[i].handle == NULL)
569 {
570 break;
571 }
572 iso->multih[i].slsn = iso->blocks;
573 _seekfile(iso->multih[i].handle, 0, SEEK_END);
574 iso->blocks += (u32)((_tellfile(iso->multih[i].handle) - iso->offset) /
575 (iso->blocksize));
576 iso->multih[i].elsn = iso->blocks - 1;
577 }
578
579 if (i == 0)
580 {
581 return NULL;
582 }
583 }
584
585 if (iso->flags == 0)
586 {
587 _seekfile(iso->handle, 0, SEEK_END);
588 iso->blocks = (u32)((_tellfile(iso->handle) - iso->offset) /
589 (iso->blocksize));
590 }
591
592
593 printf("isoOpen: %s ok\n", iso->filename);
594 printf("offset = %d\n", iso->offset);
595 printf("blockofs = %d\n", iso->blockofs);
596 printf("blocksize = %d\n", iso->blocksize);
597 printf("blocks = %d\n", iso->blocks);
598 printf("type = %d\n", iso->type);
599
600 return iso;
601 }
602
603 isoFile *isoCreate(const char *filename, int flags)
604 {
605 isoFile *iso;
606 char Zfile[256];
607
608 iso = (isoFile*)malloc(sizeof(isoFile));
609 if (iso == NULL) return NULL;
610
611 memset(iso, 0, sizeof(isoFile));
612 strcpy(iso->filename, filename);
613 iso->flags = flags;
614 iso->offset = 0;
615 iso->blockofs = 24;
616 iso->blocksize = CD_FRAMESIZE_RAW;
617 iso->blocksize = 2048;
618
619 if (iso->flags & (ISOFLAGS_Z | ISOFLAGS_Z2 | ISOFLAGS_BZ2))
620 {
621 sprintf(Zfile, "%s.table", iso->filename);
622 iso->htable = _openfile(Zfile, O_WRONLY);
623 if (iso->htable == NULL)
624 {
625 return NULL;
626 }
627 }
628
629 iso->handle = _openfile(iso->filename, O_WRONLY | O_CREAT);
630 if (iso->handle == NULL)
631 {
632 printf("Error loading %s\n", iso->filename);
633 return NULL;
634 }
635 printf("isoCreate: %s ok\n", iso->filename);
636 printf("offset = %d\n", iso->offset);
637
638 return iso;
639 }
640
641 int isoSetFormat(isoFile *iso, int blockofs, int blocksize, int blocks)
642 {
643 iso->blocksize = blocksize;
644 iso->blocks = blocks;
645 iso->blockofs = blockofs;
646 printf("blockofs = %d\n", iso->blockofs);
647 printf("blocksize = %d\n", iso->blocksize);
648 printf("blocks = %d\n", iso->blocks);
649 if (iso->flags & ISOFLAGS_Z2)
650 {
651 if (_writefile(iso->handle, "Z V2", 4) < 4) return -1;
652 if (_writefile(iso->handle, &blocksize, 4) < 4) return -1;
653 if (_writefile(iso->handle, &blocks, 4) < 4) return -1;
654 if (_writefile(iso->handle, &blockofs, 4) < 4) return -1;
655 }
656 if (iso->flags & ISOFLAGS_BZ2)
657 {
658 if (_writefile(iso->handle, "BZV2", 4) < 4) return -1;
659 if (_writefile(iso->handle, &blocksize, 4) < 4) return -1;
660 if (_writefile(iso->handle, &blocks, 4) < 4) return -1;
661 if (_writefile(iso->handle, &blockofs, 4) < 4) return -1;
662 iso->buflsn = -1;
663 iso->buffer = (u8*)malloc(iso->blocksize * 16);
664 if (iso->buffer == NULL) return -1;
665 }
666 if (iso->flags & ISOFLAGS_BLOCKDUMP)
667 {
668 if (_writefile(iso->handle, "BDV2", 4) < 4) return -1;
669 if (_writefile(iso->handle, &blocksize, 4) < 4) return -1;
670 if (_writefile(iso->handle, &blocks, 4) < 4) return -1;
671 if (_writefile(iso->handle, &blockofs, 4) < 4) return -1;
672 }
673
674 return 0;
675 }
676
677 s32 MSFtoLSN(u8 *Time)
678 {
679 u32 lsn;
680
681 lsn = Time[2];
682 lsn += (Time[1] - 2) * 75;
683 lsn += Time[0] * 75 * 60;
684 return lsn;
685 }
686
687 void LSNtoMSF(u8 *Time, s32 lsn)
688 {
689 u8 m, s, f;
690
691 lsn += 150;
692 m = lsn / 4500; // minuten
693 lsn = lsn - m * 4500; // minuten rest
694 s = lsn / 75; // sekunden
695 f = lsn - (s * 75); // sekunden rest
696 Time[0] = itob(m);
697 Time[1] = itob(s);
698 Time[2] = itob(f);
699 }
700
701 int _isoReadBlock(isoFile *iso, u8 *dst, int lsn)
702 {
703 u64 ofs = (u64)lsn * iso->blocksize + iso->offset;
704 int ret;
705
706 // printf("_isoReadBlock %d, blocksize=%d, blockofs=%d\n", lsn, iso->blocksize, iso->blockofs);
707 memset(dst, 0, iso->blockofs);
708 _seekfile(iso->handle, ofs, SEEK_SET);
709 ret = _readfile(iso->handle, dst + iso->blockofs, iso->blocksize);
710 if (ret < iso->blocksize)
711 {
712 printf("read error %d\n", ret);
713 return -1;
714 }
715
716 return 0;
717 }
718
719 int _isoReadBlockZ(isoFile *iso, u8 *dst, int lsn)
720 {
721 u32 pos, p;
722 uLongf size;
723 u8 Zbuf[CD_FRAMESIZE_RAW*2];
724 int ret;
725
726 // printf("_isoReadBlockZ %d, %d\n", lsn, iso->blocksize);
727 pos = *(unsigned long*) & iso->Ztable[lsn * 6];
728 p = *(unsigned short*) & iso->Ztable[lsn * 6 + 4];
729 // printf("%d, %d\n", pos, p);
730 _seekfile(iso->handle, pos, SEEK_SET);
731 ret = _readfile(iso->handle, Zbuf, p);
732 if (ret < p)
733 {
734 printf("error reading block!!\n");
735 return -1;
736 }
737
738 size = CD_FRAMESIZE_RAW;
739 uncompress(dst, &size, Zbuf, p);
740
741 return 0;
742 }
743
744 int _isoReadBlockZ2(isoFile *iso, u8 *dst, int lsn)
745 {
746 u32 pos, p;
747 uLongf size;
748 u8 Zbuf[16*1024];
749 int ret;
750
751 // printf("_isoReadBlockZ2 %d, %d\n", lsn, iso->blocksize);
752 pos = *(u32*) & iso->Ztable[lsn*8];
753 p = *(u32*) & iso->Ztable[lsn*8+4];
754 // printf("%d, %d\n", pos, p);
755 _seekfile(iso->handle, pos, SEEK_SET);
756 ret = _readfile(iso->handle, Zbuf, p);
757 if (ret < p)
758 {
759 printf("error reading block!!\n");
760 return -1;
761 }
762
763 size = iso->blocksize;
764 uncompress(dst + iso->blockofs, &size, Zbuf, p);
765
766 return 0;
767 }
768
769 int _isoReadBlockBZ2(isoFile *iso, u8 *dst, int lsn)
770 {
771 u32 pos, p;
772 u32 size;
773 u8 Zbuf[64*1024];
774 int ret;
775
776 if ((lsn / 16) == iso->buflsn)
777 {
778 memset(dst, 0, iso->blockofs);
779 memcpy(dst + iso->blockofs, iso->buffer + (iso->blocksize*(lsn&0xf)), iso->blocksize);
780 return 0;
781 }
782
783 iso->buflsn = lsn / 16;
784 // printf("_isoReadBlockBZ2 %d, %d\n", lsn, iso->blocksize);
785 pos = *(u32*) & iso->Ztable[(lsn/16)*8];
786 p = *(u32*) & iso->Ztable[(lsn/16)*8+4];
787 // printf("%d, %d\n", pos, p);
788 _seekfile(iso->handle, pos, SEEK_SET);
789 ret = _readfile(iso->handle, Zbuf, p);
790
791 if (ret < p)
792 {
793 printf("error reading block!!\n");
794 return -1;
795 }
796
797 size = iso->blocksize * 64;
798 ret = BZ2_bzBuffToBuffDecompress((s8*)iso->buffer, &size, (s8*)Zbuf, p, 0, 0);
799
800 if (ret != BZ_OK)
801 {
802 printf("_isoReadBlockBZ2 %d, %d\n", lsn, iso->blocksize);
803 printf("%d, %d\n", pos, p);
804 printf("error on BZ2: %d\n", ret);
805 }
806
807 memset(dst, 0, iso->blockofs);
808 memcpy(dst + iso->blockofs, iso->buffer + (iso->blocksize*(lsn&0xf)), iso->blocksize);
809
810 return 0;
811 }
812
813 int _isoReadBlockD(isoFile *iso, u8 *dst, int lsn)
814 {
815 int ret;
816 int i;
817
818 // printf("_isoReadBlockD %d, blocksize=%d, blockofs=%d\n", lsn, iso->blocksize, iso->blockofs);
819 memset(dst, 0, iso->blockofs);
820 for (i = 0; i < iso->dtablesize;i++)
821 {
822 if (iso->dtable[i] != lsn) continue;
823
824 _seekfile(iso->handle, 16 + i*(iso->blocksize + 4) + 4, SEEK_SET);
825 ret = _readfile(iso->handle, dst + iso->blockofs, iso->blocksize);
826 if (ret < iso->blocksize) return -1;
827
828 return 0;
829 }
830 printf("block %d not found in dump\n", lsn);
831
832 return -1;
833 }
834
835 int _isoReadBlockM(isoFile *iso, u8 *dst, int lsn)
836 {
837 u64 ofs;
838 int ret;
839 int i;
840
841 for (i = 0; i < 8; i++)
842 {
843 if (lsn >= iso->multih[i].slsn &&
844 lsn <= iso->multih[i].elsn)
845 {
846 break;
847 }
848 }
849 if (i == 8) return -1;
850
851 ofs = (u64)(lsn - iso->multih[i].slsn) * iso->blocksize + iso->offset;
852 // printf("_isoReadBlock %d, blocksize=%d, blockofs=%d\n", lsn, iso->blocksize, iso->blockofs);
853 memset(dst, 0, iso->blockofs);
854 _seekfile(iso->multih[i].handle, ofs, SEEK_SET);
855 ret = _readfile(iso->multih[i].handle, dst + iso->blockofs, iso->blocksize);
856
857 if (ret < iso->blocksize)
858 {
859 printf("read error %d\n", ret);
860 return -1;
861 }
862
863 return 0;
864 }
865
866 int isoReadBlock(isoFile *iso, u8 *dst, int lsn)
867 {
868 int ret;
869
870 if (lsn > iso->blocks)
871 {
872 printf("isoReadBlock: %d > %d\n", lsn, iso->blocks);
873 return -1;
874 }
875
876 if (iso->flags & ISOFLAGS_Z)
877 ret = _isoReadBlockZ(iso, dst, lsn);
878 else if (iso->flags & ISOFLAGS_Z2)
879 ret = _isoReadBlockZ2(iso, dst, lsn);
880 else if (iso->flags & ISOFLAGS_BLOCKDUMP)
881 ret = _isoReadBlockD(iso, dst, lsn);
882 else if (iso->flags & ISOFLAGS_MULTI)
883 ret = _isoReadBlockM(iso, dst, lsn);
884 else if (iso->flags & ISOFLAGS_BZ2)
885 ret = _isoReadBlockBZ2(iso, dst, lsn);
886 else
887 ret = _isoReadBlock(iso, dst, lsn);
888
889 if (ret == -1) return ret;
890
891 if (iso->type == ISOTYPE_CD)
892 {
893 LSNtoMSF(dst + 12, lsn);
894 dst[15] = 2;
895 }
896
897 return 0;
898 }
899
900
901 int _isoWriteBlock(isoFile *iso, u8 *src, int lsn)
902 {
903 u64 ofs = (u64)lsn * iso->blocksize + iso->offset;
904 int ret;
905
906 // printf("_isoWriteBlock %d (ofs=%d)\n", iso->blocksize, ofs);
907 _seekfile(iso->handle, ofs, SEEK_SET);
908 ret = _writefile(iso->handle, src + iso->blockofs, iso->blocksize);
909 // printf("_isoWriteBlock %d\n", ret);
910 if (ret < iso->blocksize) return -1;
911
912 return 0;
913 }
914
915 int _isoWriteBlockZ(isoFile *iso, u8 *src, int lsn)
916 {
917 u32 pos;
918 uLongf size;
919 u8 Zbuf[CD_FRAMESIZE_RAW];
920 int ret;
921
922 // printf("_isoWriteBlockZ %d\n", iso->blocksize);
923 size = 2352;
924 compress(Zbuf, &size, src, 2352);
925 // printf("_isoWriteBlockZ %d\n", size);
926
927 pos = (u32)_tellfile(iso->handle);
928 ret = _writefile(iso->htable, &pos, 4);
929 if (ret < 4) return -1;
930 ret = _writefile(iso->htable, &size, 2);
931 if (ret < 2) return -1;
932
933 ret = _writefile(iso->handle, Zbuf, size);
934 // printf("_isoWriteBlockZ %d\n", ret);
935 if (ret < size)
936 {
937 printf("error writing block!!\n");
938 return -1;
939 }
940
941 return 0;
942 }
943
944 int _isoWriteBlockZ2(isoFile *iso, u8 *src, int lsn)
945 {
946 uLongf size;
947 u8 Zbuf[1024*16];
948 int ret;
949
950 // printf("_isoWriteBlockZ %d\n", iso->blocksize);
951 size = 1024 * 16;
952 compress(Zbuf, &size, src + iso->blockofs, iso->blocksize);
953 // printf("_isoWriteBlockZ %d\n", size);
954
955 ret = _writefile(iso->htable, (u8*) & size, 4);
956 if (ret < 4) return -1;
957 ret = _writefile(iso->handle, Zbuf, size);
958 // printf("_isoWriteBlockZ %d\n", ret);
959 if (ret < size)
960 {
961 printf("error writing block!!\n");
962 return -1;
963 }
964
965 return 0;
966 }
967
968 int _isoWriteBlockD(isoFile *iso, u8 *src, int lsn)
969 {
970 int ret;
971
972 // printf("_isoWriteBlock %d (ofs=%d)\n", iso->blocksize, ofs);
973 ret = _writefile(iso->handle, &lsn, 4);
974 if (ret < 4) return -1;
975 ret = _writefile(iso->handle, src + iso->blockofs, iso->blocksize);
976 // printf("_isoWriteBlock %d\n", ret);
977 if (ret < iso->blocksize) return -1;
978
979 return 0;
980 }
981
982 int _isoWriteBlockBZ2(isoFile *iso, u8 *src, int lsn)
983 {
984 u32 size;
985 u8 Zbuf[64*1024];
986 int blocks;
987 int ret;
988
989 memcpy(iso->buffer + (iso->blocksize*(lsn&0xf)), src + iso->blockofs, iso->blocksize);
990
991 if (lsn == (iso->blocks - 1))
992 {
993 blocks = (lsn & 0xf) + 1;
994 }
995 else
996 {
997 blocks = 16;
998 if ((lsn & 0xf) != 0xf) return 0;
999 }
1000
1001 // printf("_isoWriteBlockBZ2 %d\n", iso->blocksize);
1002 size = 64 * 1024;
1003 ret = BZ2_bzBuffToBuffCompress((s8*)Zbuf, (u32*) & size, (s8*)iso->buffer, iso->blocksize * blocks, 9, 0, 30);
1004
1005 if (ret != BZ_OK)
1006 {
1007 printf("error on BZ2: %d\n", ret);
1008 }
1009
1010 // printf("_isoWriteBlockBZ2 %d\n", size);
1011
1012 ret = _writefile(iso->htable, (u8*) & size, 4);
1013 if (ret < 4) return -1;
1014 ret = _writefile(iso->handle, Zbuf, size);
1015 // printf("_isoWriteBlockZ %d\n", ret);
1016
1017 if (ret < size)
1018 {
1019 printf("error writing block!!\n");
1020 return -1;
1021 }
1022
1023 return 0;
1024 }
1025
1026 int isoWriteBlock(isoFile *iso, u8 *src, int lsn)
1027 {
1028 int ret;
1029
1030 if (iso->flags & ISOFLAGS_Z)
1031 ret = _isoWriteBlockZ(iso, src, lsn);
1032 else if (iso->flags & ISOFLAGS_Z2)
1033 ret = _isoWriteBlockZ2(iso, src, lsn);
1034 else if (iso->flags & ISOFLAGS_BLOCKDUMP)
1035 ret = _isoWriteBlockD(iso, src, lsn);
1036 else if (iso->flags & ISOFLAGS_BZ2)
1037 ret = _isoWriteBlockBZ2(iso, src, lsn);
1038 else
1039 ret = _isoWriteBlock(iso, src, lsn);
1040
1041 if (ret == -1) return ret;
1042 return 0;
1043 }
1044
1045 void isoClose(isoFile *iso)
1046 {
1047 if (iso->handle) _closefile(iso->handle);
1048 if (iso->htable) _closefile(iso->htable);
1049 if (iso->buffer) free(iso->buffer);
1050
1051 free(iso);
1052 }
1053

  ViewVC Help
Powered by ViewVC 1.1.22