wok view linld/stuff/src/ISO9660.CPP @ rev 23871

linld: fix isoreaddir
author Pascal Bellard <pascal.bellard@slitaz.org>
date Tue Jun 23 13:35:04 2020 +0000 (2020-06-23)
parents 26195667e7c9
children 431c14b76522
line source
1 #include "crtl.h"
2 #include "crtlx.h"
3 #include "iso9660.h"
4 #define __ROCKRIDGE
6 #if 1
7 #define setdirofs(to,sec) (*(int*)((char*)&(to)+1) = *(int*)&(sec)<<(SECTORBITS-8))
8 #define cpydirofs(to,from) (*(int*)((char*)&(to)+1) = *(int*)((char*)&(from)+1))
9 #else
10 #define setdirofs(to,sec) (to = (sec)<<SECTORBITS)
11 #define cpydirofs(to,from) ((to) = (from))
12 #endif
14 #ifdef ISOHOOK
15 void isoroot(void)
16 #else
17 int isoroot(void)
18 #endif
19 {
20 static const unsigned long root = 16UL * 2048;
21 #ifdef ISOHOOK
22 isoreadsector(&root);
23 setdirofs(isostate.dirofs, (* (unsigned long *) (isostate.buffer + 0x9E)));
24 isostate.dirsize = filesize2dirsize(* (unsigned long *) (isostate.buffer + 0xA6));
25 #else
26 if (isoreadsector(&root) == -1 || strhead(isostate.buffer+1,"CD001") == -1) {
27 //close(isostate.fd);
28 return -1;
29 }
30 setdirofs(isostate.dirofs, (* (unsigned long *) (isostate.buffer + 0x9E)));
31 isostate.dirsize = filesize2dirsize(* (unsigned long *) (isostate.buffer + 0xA6));
32 return 0;
33 #endif
34 }
36 int isoreaddir(void)
37 {
38 char *p;
40 struct isostate *x=&isostate;
42 if (x->curdirsize == 0xFFFF) {
43 x->curdirsize = x->dirsize;
44 cpydirofs(x->curdirofs, x->dirofs);
45 goto restarted;
46 }
47 if (x->curpos >= SECTORSZ || * (short *) (x->buffer + x->curpos) == 0) {
48 restarted:
49 if (x->curdirsize < DIRSECTORSZ) return -1;
50 isoreadsector(&x->curdirofs);
51 //x->curdirofs += SECTORSZ;
52 *(int *)((char *) &x->curdirofs+1) += SECTORSZ/256;
53 x->curdirsize -= DIRSECTORSZ;
54 x->curpos = 0;
55 }
56 p = x->buffer; p += x->curpos;
57 if ((x->entrysize = * (short *) p) == 0) {
58 return -1;
59 }
60 x->fileofs = (* (unsigned long *) (p + 2)) << SECTORBITS;
61 x->filesize = * (unsigned long *) (p + 10);
62 x->filemod = 0x81ED; if (p[25] & 2) ((char *)&(x->filemod))[1] = 0x41;
63 //x->filemod = (p[25] & 2) ? 0040755 : 0100755;
64 //x->filemod = 0100755 - ((p[25] & (char)2) << 13);
65 #ifdef __ROCKRIDGE
66 // p += 34 + (p[32] & -2); ?
67 p = x->buffer + 34 + ((p[32] + x->curpos) & -2);
68 #ifdef ISOHOOK
69 x->curpos += x->entrysize;
70 #endif
71 do {
72 register len = p[2];
73 if (* (short *) p == 0x4D4E) {
74 x->filename = p + 5;
75 p[len] = 0;
76 goto found;
77 }
78 p += len;
79 #ifdef ISOHOOK
80 } while (x->buffer + x->curpos - 2 > p);
81 #else
82 } while (x->buffer + x->curpos + x->entrysize - 2 > p);
83 #endif
84 #endif
85 #ifndef ISOHOOK
86 {
87 p = x->buffer + 33; x->filename = p += x->curpos;
88 p--;
89 if (((* (short *) p) & 0xFEFF) -1 == 0) {
90 x->filename = "..";
91 if (--(* (short *) p) == 0)
92 x->filename++;
93 }
94 else {
95 p += *p; p--;
96 if (* (short *) (p) != 0x313B) {
97 p++; p++; // no ;1 to remove
98 }
99 if (p[-1] == '.') p--;
100 *p = 0;
101 }
102 }
103 #endif
104 found:
105 #ifndef ISOHOOK
106 x->curpos += x->entrysize;
107 #endif
108 return 0;
109 }
111 //#define IS_DIR(x)( ((x) & ~0777) == 040000)
112 //#define IS_DIR(x)( (char)((x) >> 9) == (char)040)
113 //#define IS_DIR(x)( (*((char*) &x + 1) & (char)0776) == (char)0100)
114 #define IS_DIR(x)( (*((char*) &x + 1) & (char)0676) == (char)0)
115 int _isoopen(void)
116 {
117 // char *name, *s, c;
118 // char _64bits = cpuhaslm();
119 struct isostate *x=&isostate;
120 #define name (x->filename2open)
121 #define c (x->c)
122 #define _64bits (x->_64bits)
123 char *s;
125 #ifdef ISOHOOK
126 extern int _cpuhaslm();
127 _64bits = _cpuhaslm();
128 #else
129 _64bits = cpuhaslm();
130 #endif
131 do {
132 #ifdef ISOHOOK
133 s = (char *) x->filename2open;
134 if (*s == '/') {
135 s++;
136 isoroot();
137 }
138 name = s;
139 while (*++s);
140 #else
141 for (s = (char *) x->filename2open; *s == '/' ; s++) {
142 isoroot();
143 }
144 next:
145 name = s;
146 do s++; while (*s && *s != '/');
147 c = *s; *s = 0;
148 #endif
149 for (x->curdirsize = 0xFFFF; isoreaddir() != -1;) {
150 const char *n = name;
151 #define i (x->tmp)
152 i = x->filename;
153 if (_64bits) {
154 if (strhead(i, n) == -1) continue;
155 n = "64";
156 i += s - name; // strlen(name);
157 }
158 if (strcmp(i, n)) continue;
159 #ifndef ISOHOOK
160 *s++ = c;
161 #endif
162 if (IS_DIR(x->filemod)) {
163 cpydirofs(x->dirofs, x->fileofs);
164 x->dirsize = filesize2dirsize(x->filesize);
165 #ifndef ISOHOOK
166 if (c) goto next;
167 #endif
168 }
169 isolseek(&x->fileofs);
170 return 0;
171 }
172 } while ((_64bits ^= CPUMASKLM) == 0);
173 return -1;
174 }