PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
cdfs.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9*/
10
11#include "accdvd_internal.h"
12
13static struct cdfs_softc Cdfsc;
14
15static void cdfs_reset(struct cdfs_softc *cdfsc)
16{
17 cdfsc->all = 0;
18 cdfsc->ptable = 0;
19 cdfsc->ptnum = 0;
20 cdfsc->rootlsn = 0;
21 cdfsc->rootsize = 0;
22 cdfsc->rootidx = -1;
23 cdfsc->dcache = 0;
24 cdfsc->dclsn = 0;
25 cdfsc->dcsize = 0;
26 cdfsc->pcache = 0;
27 cdfsc->pcsize = 0;
28}
29
30static int cdfs_path_init(struct cdfs_ptable *ptable, struct iso9660_path *path, unsigned int size)
31{
32 int count;
33
34 for ( count = 0; size; ++count )
35 {
36 unsigned int v4;
37
38 v4 = ((path->name_len[0] | (path->name_len[1] << 8)) + 1) & 0xFFFFFFFE;
39 if ( ptable )
40 {
41 ptable->path = path;
42 ptable->size = 0;
43 ++ptable;
44 }
45 size = size - 8 - v4;
46 path = (struct iso9660_path *)((char *)path + v4 + 8);
47 }
48 return count;
49}
50
51static void cdfs_ready_done(struct acd *acd, const struct cdfs_softc *arg, int ret)
52{
53 int flg;
54
55 flg = 0;
56 if ( ret >= -255 )
57 {
58 flg = 1;
59 }
60 else
61 {
62 int asc;
63 int delay;
64
65 asc = (acUint16) - (ret & 0xFFFF);
66 if ( -ret >> 16 == 6 )
67 {
68 delay = 0;
69 if ( asc == 10496 )
70 delay = acd_delay();
71 }
72 else
73 {
74 delay = -1;
75 if ( asc == 1025 )
76 delay = 1000000;
77 }
78 if ( delay < 0 )
79 flg = 1;
80 else
81 {
82 if ( delay > 0 )
83 DelayThread(delay);
84 if ( acd_ready(acd) < 0 )
85 {
86 flg = 1;
87 }
88 }
89 }
90 if ( flg )
91 {
92 if ( arg->semid > 0 )
93 SignalSema(arg->semid);
94 }
95}
96
97static void cdfs_detach(struct cdfs_softc *cdfsc)
98{
99 void *ptr;
100 void *ptr_v1;
101 void *ptr_v2;
102
103 ptr = cdfsc->ptable;
104 if ( ptr )
105 FreeSysMemory(ptr);
106 ptr_v1 = cdfsc->pcache;
107 if ( ptr_v1 )
108 FreeSysMemory(ptr_v1);
109 ptr_v2 = cdfsc->dcache;
110 if ( ptr_v2 )
111 FreeSysMemory(ptr_v2);
112 cdfs_reset(cdfsc);
113}
114
115static int cdfs_attach(struct cdfs_softc *cdfsc, struct acd *acd, int remount)
116{
117 int semid;
118 int active;
119 int active_v3;
120 int active_v6;
121 int ret;
122 struct iso9660_path *pcache_v17;
123 struct cdfs_ptable *ptable_v19;
124
125 semid = cdfsc->semid;
126 if ( semid <= 0 )
127 active = -9;
128 else
129 active = WaitSema(semid);
130 if ( active >= 0 )
131 {
132 acd_setup((struct acd *)cdfsc->buf, (acd_done_t)cdfs_ready_done, cdfsc, -5000000);
133 if ( acd_ready((struct acd *)cdfsc->buf) < 0 )
134 {
135 if ( cdfsc->semid > 0 )
136 SignalSema(cdfsc->semid);
137 }
138 }
139 active_v3 = -9;
140 if ( cdfsc->semid > 0 )
141 active_v3 = WaitSema(cdfsc->semid);
142 if ( active_v3 < 0 )
143 {
144 return -6;
145 }
146 active_v6 = cdfsc->all;
147 if ( remount )
148 {
149 if ( active_v6 != 0 )
150 {
151 cdfs_detach(cdfsc);
152 active_v6 = 0;
153 }
154 }
155 ret = -16;
156 if ( active_v6 == 0 )
157 {
158 acUint8 *buf;
159 acd_lsn_t v17;
160
161 buf = cdfsc->buf;
162 v17 = 16;
163 while ( 1 )
164 {
165 int active_v12;
166
167 FlushDcache();
168 active_v12 = acd_read(acd, v17, buf, 1);
169 ret = active_v12;
170 if ( active_v12 < 0 )
171 {
172 printf("cdfs:super:read: error %d\n", active_v12);
173 break;
174 }
175 if ( !strncmp((const char *)buf + 1, "CD001", 5u) )
176 {
177 int active_v13;
178
179 active_v13 = *buf;
180 if ( active_v13 == 255 )
181 {
182 printf("cdfs:%u:invalid descriptor\n", (unsigned int)v17);
183 ret = -6;
184 break;
185 }
186 if ( active_v13 == 1 )
187 {
188 cdfsc->all = *((acUint32 *)buf + 20);
189 cdfsc->rootlsn = *(acUint32 *)(buf + 158);
190 cdfsc->rootsize = *(acUint32 *)(buf + 166);
191 ret = 0;
192 break;
193 }
194 }
195 ++v17;
196 ret = -6;
197 if ( v17 >= 0x64 )
198 break;
199 }
200 if ( !ret )
201 {
202 int ret_v14;
203 acd_lsn_t lsn;
204 int ret_v16;
205
206 ret_v14 = *(acUint32 *)&cdfsc->buf[132];
207 lsn = *(acUint32 *)&cdfsc->buf[140];
208 cdfsc->pcsize = ret_v14;
209 ret_v16 = (unsigned int)(ret_v14 + 2047) >> 11;
210 FlushDcache();
211 pcache_v17 = (struct iso9660_path *)AllocSysMemory(0, ret_v16 << 11, 0);
212 cdfsc->pcache = pcache_v17;
213 if ( pcache_v17 == 0 )
214 {
215 ret = -12;
216 }
217 else
218 {
219 ret = acd_read(acd, lsn, pcache_v17, ret_v16);
220 if ( ret >= 0 )
221 {
222 int active_v18;
223
224 active_v18 = cdfs_path_init(0, cdfsc->pcache, cdfsc->pcsize);
225 cdfsc->ptnum = active_v18;
226 if (
227 (8 * active_v18 == 0)
228 || (ptable_v19 = (struct cdfs_ptable *)AllocSysMemory(0, 8 * active_v18, 0), (cdfsc->ptable = ptable_v19) != 0) )
229 {
230 int n;
231 int i;
232 struct cdfs_ptable *ptable;
233 int active_v24;
234
235 n = cdfs_path_init(cdfsc->ptable, cdfsc->pcache, cdfsc->pcsize);
236 i = 0;
237 ptable = cdfsc->ptable;
238 active_v24 = 0;
239 while ( i < n )
240 {
241 if ( cdfsc->rootlsn == *(acUint32 *)ptable[active_v24].path->extent )
242 {
243 ptable[active_v24].size = cdfsc->rootsize;
244 cdfsc->rootidx = i;
245 break;
246 }
247 i++;
248 active_v24 = i;
249 }
250 ret = 0;
251 }
252 else
253 {
254 void *ptr;
255
256 printf("accdvd:cdfs:path_read: malloc failed\n");
257 ptr = cdfsc->pcache;
258 if ( ptr )
259 FreeSysMemory(ptr);
260 cdfsc->pcache = 0;
261 ret = -12;
262 }
263 }
264 }
265 }
266 }
267 if ( cdfsc->semid > 0 )
268 {
269 SignalSema(cdfsc->semid);
270 }
271 return ret;
272}
273
274int cdfs_mount()
275{
276 struct acd v1;
277
278 acd_setup(&v1, 0, 0, 5000000);
279 return cdfs_attach(&Cdfsc, &v1, 0);
280}
281
282int cdfs_umount()
283{
284 int ret;
285
286 if ( Cdfsc.semid <= 0 )
287 return -9;
288 ret = WaitSema(Cdfsc.semid);
289 if ( ret < 0 )
290 {
291 return ret;
292 }
293 cdfs_detach(&Cdfsc);
294 if ( Cdfsc.semid > 0 )
295 {
296 SignalSema(Cdfsc.semid);
297 }
298 return 0;
299}
300
301int cdfs_recover(int ret)
302{
303 int v1;
304 struct acd acd_data;
305
306 v1 = -ret;
307 while ( 1 )
308 {
309 int v3;
310
311 if ( !(v1 >= 255 && (v1 >> 16 == 6 || (v1 & 0xFFFF) == 1025 || (v1 & 0xFFFF) == 10240)) )
312 return -5;
313 v3 = cdfs_attach(&Cdfsc, &acd_data, 1);
314 v1 = -v3;
315 if ( v3 >= 0 )
316 return 1;
317 }
318}
319
320int cdfs_lookup(struct cdfs_dirent *result, const char *path, int pathlen)
321{
322 const char *v9;
323 char *dirent;
324 int len;
325 int idx;
326 int namlen;
327 struct iso9660_dirent *name_v8;
328 int namlen_v9;
329 int idx_v10;
330 int high;
331 int low;
332 int ret;
333 int v21;
334 struct iso9660_path *path_v16;
335 int v23;
336 int ret_v18;
337 int len_v19;
338 unsigned char *s2;
339 int idx_v21;
340 struct iso9660_dirent *dirent_v22;
341 int len_v23;
342 struct iso9660_path *path_v24;
343 unsigned int dcsize;
344 unsigned char *pt;
345 unsigned int v33;
346 unsigned int lsn;
347 signed int size_v29;
348 int ret_v30;
349 int ret_v31;
350 struct iso9660_dirent *dirent_v32;
351 int idx_v33;
352 int v40;
353 unsigned int n;
354 struct iso9660_path *path_v36;
355 void *dirent_v37;
356 struct iso9660_dirent *dirent_v38;
357 int n_v39;
358 int i_v40;
359 int rest;
360 int v48;
361 const acUint8 *len_v43;
362 int idx_v44;
363 unsigned char *s2_v45;
364 int v52;
365 int v53;
366 char *path_v48;
367 int v55;
368 int v57;
369 unsigned int t0;
370 size_t d_namlen;
371 struct acd acd_data;
372 struct acd *acd;
373
374 acd_setup(&acd_data, 0, 0, 5000000);
375 while ( 1 )
376 {
377 int v6;
378 int v8;
379
380 v6 = -9;
381 if ( Cdfsc.semid > 0 )
382 v6 = WaitSema(Cdfsc.semid);
383 if ( v6 < 0 )
384 return -6;
385 if ( Cdfsc.all )
386 {
387 int ret_v57;
388
389 acd = &acd_data;
390 ret_v57 = -2;
391 v9 = path + 1;
392 if ( pathlen <= 0 || *path == 92 )
393 {
394 len = pathlen - 1;
395 idx = Cdfsc.rootidx;
396 while ( 1 )
397 {
398 namlen = len;
399 name_v8 = (struct iso9660_dirent *)v9;
400 while ( len > 0 )
401 {
402 if ( *v9 == 92 )
403 break;
404 --len;
405 ++v9;
406 }
407 if ( len <= 0 )
408 {
409 path_v24 = (struct iso9660_path *)(8 * idx);
410 dirent = 0;
411 if ( namlen )
412 {
413 dcsize = Cdfsc.dcsize;
414 pt = &path_v24->name_len[(unsigned int)Cdfsc.ptable];
415 v33 = *(acUint32 *)&path_v24->extent[(unsigned int)Cdfsc.ptable + 2];
416 lsn = *(acUint32 *)(*(acUint32 *)pt + 2);
417 FlushDcache();
418 size_v29 = 0;
419 if ( lsn != Cdfsc.dclsn )
420 {
421 ret_v30 = acd_read(acd, lsn, Cdfsc.buf, 1);
422 if ( ret_v30 < 0 )
423 {
424 Cdfsc.dclsn = 0;
425 }
426 else
427 {
428 ret_v31 = v33 - 1;
429 if ( !v33 )
430 {
431 dirent_v32 = (struct iso9660_dirent *)Cdfsc.buf;
432 idx_v33 = 2048;
433 v33 = 0;
434 while ( idx_v33 )
435 {
436 v40 = dirent_v32->length[0];
437 if ( !dirent_v32->length[0] )
438 break;
439 if ( dirent_v32->name_len[0] == 1 && !dirent_v32->name[0] )
440 {
441 v33 = *(acUint32 *)dirent_v32->size;
442 break;
443 }
444 idx_v33 -= v40;
445 dirent_v32 = (struct iso9660_dirent *)((char *)dirent_v32 + v40);
446 }
447 *((acUint32 *)pt + 1) = v33;
448 ret_v31 = v33 - 1;
449 }
450 if ( !v33 )
451 {
452 ret_v30 = 0;
453 }
454 else
455 {
456 n = (unsigned int)ret_v31 >> 11;
457 if ( dcsize >= (unsigned int)ret_v31 >> 11 )
458 {
459 dirent_v37 = Cdfsc.dcache;
460 }
461 else
462 {
463 if ( Cdfsc.dcache )
464 FreeSysMemory(Cdfsc.dcache);
465 path_v36 = (struct iso9660_path *)AllocSysMemory(0, n << 11, 0);
466 dirent_v37 = path_v36;
467 Cdfsc.dcache = (struct iso9660_dirent *)path_v36;
468 if ( path_v36 )
469 Cdfsc.dcsize = n;
470 else
471 Cdfsc.dcsize = 0;
472 if ( !path_v36 )
473 lsn = 0;
474 }
475 ret_v30 = 0;
476 if ( lsn )
477 {
478 if ( n )
479 {
480 ret_v30 = v33;
481 if ( acd_read(acd, lsn + 1, dirent_v37, n) < 0 )
482 {
483 lsn = 0;
484 ret_v30 = 0;
485 }
486 }
487 else
488 {
489 ret_v30 = v33;
490 }
491 }
492 Cdfsc.dclsn = lsn;
493 }
494 }
495 size_v29 = ret_v30;
496 }
497 dirent_v38 = (struct iso9660_dirent *)Cdfsc.buf;
498 if ( size_v29 >= 0 )
499 {
500 n_v39 = 1;
501 for ( i_v40 = 1; i_v40 >= 0; --i_v40 )
502 {
503 while ( 1 )
504 {
505 --n_v39;
506 rest = 2048;
507 if ( n_v39 < 0 )
508 break;
509 if ( size_v29 < 2048 )
510 rest = size_v29;
511 size_v29 -= rest;
512 while ( rest > 0 )
513 {
514 v48 = dirent_v38->length[0];
515 if ( !v48 )
516 {
517 break;
518 }
519 if ( namlen == dirent_v38->name_len[0] )
520 {
521 len_v43 = (acUint8 *)name_v8;
522 idx_v44 = namlen - 1;
523 s2_v45 = dirent_v38->name;
524 while ( idx_v44 >= 0 )
525 {
526 v52 = *len_v43++;
527 v53 = *s2_v45 - v52;
528 if ( *s2_v45++ != v52 )
529 break;
530 idx_v44--;
531 }
532 if ( idx_v44 < 0 )
533 {
534 v53 = 0;
535 }
536 rest -= v48;
537 if ( !v53 )
538 {
539 ret_v57 = 0;
540 path_v48 = (char *)dirent_v38;
541 dirent = path_v48;
542 }
543 }
544 else
545 {
546 rest -= dirent_v38->length[0];
547 }
548 if ( dirent )
549 {
550 break;
551 }
552 dirent_v38 = (struct iso9660_dirent *)((char *)dirent_v38 + v48);
553 }
554 if ( dirent )
555 {
556 break;
557 }
558 dirent_v38 = (struct iso9660_dirent *)((char *)dirent_v38 + rest);
559 }
560 if ( dirent )
561 {
562 break;
563 }
564 dirent_v38 = Cdfsc.dcache;
565 v55 = size_v29 + 2047;
566 if ( size_v29 + 2047 < 0 )
567 v55 = size_v29 + 4094;
568 n_v39 = v55 >> 11;
569 }
570 }
571 else
572 {
573 ret_v57 = size_v29;
574 }
575 }
576 if ( dirent )
577 {
578 result->d_lsn = *(acUint32 *)(dirent + 2);
579 result->d_size = *(acUint32 *)(dirent + 10);
580 result->d_vol = (dirent[28] & 0xFF) | ((dirent[29] & 0xFF) << 8);
581 v57 = dirent[32];
582 result->d_ftype = dirent[25];
583 result->d_namlen = v57;
584 t0 = v57 & 0xFF;
585 result->d_name[t0] = 0;
586 d_namlen = result->d_namlen;
587 result->d_time.t_mon = dirent[19];
588 result->d_time.t_day = dirent[20];
589 result->d_time.t_hour = dirent[21];
590 result->d_time.t_padding = 0;
591 result->d_time.t_min = dirent[22];
592 result->d_time.t_sec = dirent[23];
593 result->d_time.t_year = (dirent[18] & 0xFF) + 1900;
594 memcpy(result->d_name, dirent + 33, d_namlen);
595 ret_v57 = 0;
596 }
597 break;
598 }
599 namlen_v9 = namlen - len;
600 idx_v10 = idx;
601 if ( namlen_v9 && (namlen_v9 != 1 || name_v8->length[0] != 46) )
602 {
603 if ( namlen_v9 == 2 && name_v8->length[0] == 46 && name_v8->ext_attr_length[0] == 46 )
604 {
605 idx = (Cdfsc.ptable[idx].path->parent[0] | (Cdfsc.ptable[idx].path->parent[1] << 8)) - 1;
606 }
607 else
608 {
609 high = Cdfsc.ptnum;
610 low = idx;
611 v21 = high;
612 for ( ;; )
613 {
614 ret = low + high;
615 if ( low >= v21 )
616 {
617 idx = -1;
618 break;
619 }
620 v21 = ret / 2;
621 path_v16 = Cdfsc.ptable[ret / 2].path;
622 v23 = path_v16->parent[1] << 8;
623 ret_v18 = (path_v16->parent[0] | v23) - 1 - idx_v10;
624 if ( (path_v16->parent[0] | v23) - 1 == idx_v10 )
625 {
626 len_v19 = path_v16->name_len[0] | (path_v16->name_len[1] << 8);
627 s2 = path_v16->name;
628 if ( namlen_v9 < len_v19 )
629 len_v19 = namlen_v9;
630 idx_v21 = len_v19 - 1;
631 dirent_v22 = name_v8;
632 while ( idx_v21 >= 0 )
633 {
634 len_v23 = dirent_v22->length[0];
635 dirent_v22 = (struct iso9660_dirent *)((char *)dirent_v22 + 1);
636 ret_v18 = *s2 - len_v23;
637 if ( *s2++ != len_v23 )
638 break;
639 idx_v21--;
640 }
641 if ( idx_v21 < 0 )
642 {
643 ret_v18 = 0;
644 }
645 }
646 if ( ret_v18 < 0 )
647 {
648 low = v21 + 1;
649 v21 = high;
650 }
651 else
652 {
653 high = v21;
654 if ( ret_v18 <= 0 )
655 {
656 idx = v21;
657 break;
658 }
659 }
660 }
661 }
662 }
663 ++v9;
664 if ( idx < 0 )
665 break;
666 --len;
667 }
668 }
669 if ( Cdfsc.semid > 0 )
670 SignalSema(Cdfsc.semid);
671 return ret_v57;
672 }
673 if ( Cdfsc.semid > 0 )
674 SignalSema(Cdfsc.semid);
675 v8 = cdfs_attach(&Cdfsc, &acd_data, 1);
676 if ( v8 < 0 )
677 return v8;
678 }
679}
680
681int cdfs_read(struct cdfs_file *file, void *buf, int size)
682{
683 int ret;
684 unsigned int cpos;
685 unsigned int f_size;
686 unsigned int off;
687 unsigned int npos;
688 unsigned char *data;
689 int rest;
690 unsigned int cpos_v14;
691 unsigned int npos_v15;
692 unsigned int npos_v16;
693 unsigned int lsn_v17;
694 int len_v18;
695 unsigned char *data_v33;
696 int rest_v34;
697 unsigned int cpos_v35;
698 unsigned int npos_v36;
699 int len_v37;
700 unsigned int lsn_v38;
701 int rest_v42;
702 struct acd acd_data;
703
704 if ( !Cdfsc.semid )
705 return -6;
706 acd_setup(&acd_data, 0, 0, 5000000);
707 cpos = file->f_pos;
708 f_size = file->f_size;
709 off = cpos & 0x7FF;
710 npos = cpos - (off - 2048);
711 if ( (cpos & 0x7FF) == 0 )
712 {
713 ret = 0;
714 }
715 else
716 {
717 signed int len;
718 unsigned int lsn;
719
720 if ( cpos + size < f_size )
721 {
722 f_size = cpos + size;
723 }
724 len = 2048 - (cpos & 0x7FF);
725 if ( f_size < npos )
726 {
727 npos = f_size;
728 len = f_size - cpos;
729 }
730 lsn = file->f_lsn + (cpos >> 11);
731 if ( len <= 0 )
732 {
733 ret = len;
734 }
735 else
736 {
737 int ret_v8;
738
739 ret_v8 = -9;
740 if ( Cdfsc.semid > 0 )
741 ret_v8 = WaitSema(Cdfsc.semid);
742 ret = -6;
743 if ( ret_v8 >= 0 )
744 {
745 int ret_v10;
746
747 ret_v10 = -11;
748 if ( Cdfsc.all )
749 {
750 if ( lsn )
751 {
752 if ( Cdfsc.dclsn == lsn )
753 {
754 ret_v10 = 1;
755 }
756 }
757 if ( ret_v10 <= 0 )
758 {
759 ret_v10 = acd_read(&acd_data, lsn, Cdfsc.buf, 1);
760 }
761 }
762 if ( ret_v10 <= 0 )
763 {
764 len = ret_v10;
765 lsn = 0;
766 }
767 else
768 {
769 memcpy(buf, &Cdfsc.buf[off], len);
770 file->f_pos = npos;
771 }
772 Cdfsc.dclsn = lsn;
773 if ( Cdfsc.semid > 0 )
774 SignalSema(Cdfsc.semid);
775 ret = len;
776 }
777 }
778 }
779 data = (unsigned char *)buf + ret;
780 if ( ret < 0 )
781 return ret;
782 rest = size - ret;
783 cpos_v14 = file->f_pos;
784 npos_v15 = cpos_v14 + rest;
785 if ( file->f_size < cpos_v14 + rest )
786 npos_v15 = file->f_size;
787 npos_v16 = npos_v15 - (npos_v15 & 0x7FF);
788 lsn_v17 = file->f_lsn + (cpos_v14 >> 11);
789 len_v18 = npos_v16 - cpos_v14;
790 if ( (int)(npos_v16 - cpos_v14) < 2048 )
791 {
792 len_v18 = 0;
793 ret = len_v18;
794 }
795 else
796 {
797 unsigned char *ptr;
798 ptr = data;
799 if ( ((uiptr)data & 3) == 0 )
800 {
801 int ret_v32;
802
803 ret_v32 = acd_read(&acd_data, file->f_lsn + (cpos_v14 >> 11), data, len_v18 / 2048);
804 if ( ret_v32 <= 0 )
805 len_v18 = ret_v32;
806 else
807 file->f_pos = npos_v16;
808 ret = len_v18;
809 }
810 else
811 {
812 signed int rest_v20;
813 int flg;
814
815 flg = 0;
816 rest_v20 = npos_v16 - cpos_v14;
817 while ( 1 )
818 {
819 int ret_v21;
820 int ret_v22;
821
822 if ( rest_v20 <= 0 )
823 {
824 flg = 1;
825 break;
826 }
827 ret_v21 = -9;
828 if ( Cdfsc.semid > 0 )
829 ret_v21 = WaitSema(Cdfsc.semid);
830 ret = -6;
831 if ( ret_v21 < 0 )
832 break;
833 ret_v22 = -11;
834 if ( Cdfsc.all )
835 {
836 if ( lsn_v17 )
837 {
838 if ( Cdfsc.dclsn == lsn_v17 )
839 {
840 ret_v22 = 1;
841 }
842 }
843 if ( ret_v22 <= 0 )
844 {
845 ret_v22 = acd_read(&acd_data, lsn_v17, Cdfsc.buf, 1);
846 }
847 }
848 if ( ret_v22 <= 0 )
849 {
850 Cdfsc.dclsn = 0;
851 len_v18 = ret_v22;
852 flg = 1;
853 break;
854 }
855 memcpy(ptr, &Cdfsc, sizeof(Cdfsc));
856 ptr += 2048;
857 rest_v20 -= 2048;
858 Cdfsc.dclsn = lsn_v17++;
859 if ( Cdfsc.semid > 0 )
860 SignalSema(Cdfsc.semid);
861 }
862 if ( flg )
863 {
864 ret = len_v18;
865 if ( !rest_v20 )
866 file->f_pos = npos_v16;
867 }
868 }
869 }
870 data_v33 = &data[ret];
871 if ( ret < 0 )
872 return ret;
873 rest_v34 = rest - ret;
874 cpos_v35 = file->f_pos;
875 npos_v36 = cpos_v35 + rest_v34;
876 if ( file->f_size < cpos_v35 + rest_v34 )
877 npos_v36 = file->f_size;
878 len_v37 = npos_v36 - cpos_v35;
879 lsn_v38 = file->f_lsn + (cpos_v35 >> 11);
880 ret = len_v37;
881 if ( (int)(npos_v36 - cpos_v35) > 0 )
882 {
883 int ret_v39;
884
885 ret_v39 = -9;
886 if ( Cdfsc.semid > 0 )
887 ret_v39 = WaitSema(Cdfsc.semid);
888 if ( ret_v39 < 0 )
889 {
890 ret = -6;
891 }
892 else
893 {
894 int ret_v40;
895
896 ret_v40 = -11;
897 if ( Cdfsc.all )
898 {
899 if ( lsn_v38 )
900 {
901 if ( Cdfsc.dclsn == lsn_v38 )
902 {
903 ret_v40 = 1;
904 }
905 }
906 if ( ret_v40 <= 0 )
907 {
908 ret_v40 = acd_read(&acd_data, lsn_v38, Cdfsc.buf, 1);
909 }
910 }
911 if ( ret_v40 <= 0 )
912 {
913 len_v37 = ret_v40;
914 lsn_v38 = 0;
915 }
916 else
917 {
918 memcpy(data_v33, Cdfsc.buf, len_v37);
919 file->f_pos = npos_v36;
920 }
921 Cdfsc.dclsn = lsn_v38;
922 if ( Cdfsc.semid > 0 )
923 SignalSema(Cdfsc.semid);
924 ret = len_v37;
925 }
926 }
927 rest_v42 = rest_v34 - ret;
928 if ( ret >= 0 )
929 return size - rest_v42;
930 return ret;
931}
932
933int cdfs_module_status()
934{
935 return Cdfsc.semid > 0;
936}
937
938int cdfs_module_start(int argc, char **argv)
939{
940 int ret;
941 int v4;
942 acInt32 v5;
943 iop_sema_t param;
944
945 (void)argc;
946 (void)argv;
947 ret = cdfs_module_status();
948 if ( ret != 0 )
949 {
950 return ret;
951 }
952 param.attr = 0;
953 param.initial = 1;
954 param.max = 1;
955 param.option = 0;
956 v4 = CreateSema(&param);
957 v5 = v4;
958 if ( v4 <= 0 )
959 printf("accdvd:cdfs:sem_alloc: error %d\n", v4);
960 if ( v5 <= 0 )
961 return -6;
962 Cdfsc.semid = v5;
963 cdfs_reset(&Cdfsc);
964 return 0;
965}
966
967int cdfs_module_stop()
968{
969 int ret;
970 int semid;
971
972 ret = cdfs_module_status();
973 if ( ret == 0 )
974 {
975 return ret;
976 }
977 semid = Cdfsc.semid;
978 if ( Cdfsc.semid )
979 {
980 if ( Cdfsc.semid > 0 )
981 WaitSema(Cdfsc.semid);
982 cdfs_detach(&Cdfsc);
983 Cdfsc.semid = 0;
984 if ( semid > 0 )
985 SignalSema(semid);
986 DeleteSema(semid);
987 }
988 return 0;
989}
990
991int cdfs_module_restart(int argc, char **argv)
992{
993 (void)argc;
994 (void)argv;
995
996 return -88;
997}
u32 count
start sector of fragmented bd/file