wok-6.x view linld/stuff/src/ISO9660.CPP @ rev 19636

linld: check asm instructions again processor type
author Pascal Bellard <pascal.bellard@slitaz.org>
date Fri Jan 20 09:55:25 2017 +0100 (2017-01-20)
parents e428345df29a
children 6f494adb2c71
line source
1 #include "crtl.h"
2 #include "crtlx.h"
3 #include "iso9660.h"
4 #define __ROCKRIDGE
6 #define SECTORSZ 2048
7 #define SECTORBITS 11
8 static char buffer[SECTORSZ];
9 struct isostate isostate;
11 static int readsector(unsigned long offset)
12 {
13 return (isolseek(offset) != -1
14 && read(isostate.fd, buffer, SECTORSZ) == SECTORSZ);
15 }
17 int isoread(char *data, unsigned size)
18 {
19 int get, n;
21 struct isostate *x=&isostate;
22 if (isolseek(x->fileofs) == -1)
23 return -1;
24 if (size < x->filesize)
25 size = x->filesize;
26 for (get = size; get; get -= n, data += n) {
27 n = read(x->fd,data,get);
28 if (n < 0)
29 return n;
30 if (n == 0)
31 break;
32 x->fileofs += n;
33 x->filesize -= n;
34 }
35 return size - get;
36 }
38 int isoreset(char *name)
39 {
40 struct isostate *x=&isostate;
41 if (name)
42 //x->fd = open(name, O_RDONLY);
43 x->fd = open(name);
44 if (!readsector(16UL * 2048) || strhead(buffer+1,"CD001")) {
45 //close(x->fd);
46 return -1;
47 }
48 x->dirofs = * (unsigned long *) (buffer + 0x9E);
49 x->dirofs <<= SECTORBITS;
50 x->dirsize = * (unsigned long *) (buffer + 0xA6);
51 return 0;
52 }
54 int isoreaddir(int restart)
55 {
56 static char dots[] = "..";
57 int size;
58 char *p;
59 #ifdef __ROCKRIDGE
60 char *endname;
61 #endif
62 struct isostate *x=&isostate;
64 if (restart) {
65 x->curpos = SECTORSZ;
66 x->curdirofs = x->dirofs;
67 x->curdirsize = x->dirsize;
68 }
69 if (x->curpos >= SECTORSZ || * (short *) (buffer + x->curpos) == 0) {
70 if (x->curdirsize < SECTORSZ) return -1;
71 readsector(x->curdirofs);
72 x->curdirofs += SECTORSZ;
73 x->curdirsize -= SECTORSZ;
74 x->curpos = 0;
75 }
76 p = buffer + x->curpos;
77 size = * (short *) p;
78 if (size == 0)
79 return -1;
80 x->fileofs = (* (unsigned long *) (p + 2)) << SECTORBITS;
81 x->filesize = * (unsigned long *) (p + 10);
82 x->filemod = (p[25] & 2) ? 0040755 : 0100755;
83 #ifdef __ROCKRIDGE
84 endname = NULL;
85 // p += 34 + (p[32] & -2); ?
86 p = buffer + 34 + ((p[32] + x->curpos) & -2);
87 do {
88 int len = p[2];
89 switch (* (short *) p) {
90 case 0x4D4E: // NM
91 x->filename = p + 5;
92 endname = p + len;
93 break;
94 case 0x5850: // PX
95 x->filemod = * (short *) (p + 4);
96 break;
97 }
98 p += len;
99 }
100 while (buffer + x->curpos + size > p + 2);
101 if (endname)
102 *endname = 0;
103 else
104 #endif
105 {
106 p = x->filename = buffer + x->curpos + 33;
107 p--;
108 switch (* (short *) p) {
109 case 0x0101:
110 x->filename = dots;
111 break;
112 case 0x0001:
113 x->filename = dots + 1;
114 break;
115 default:
116 p += *p; p--;
117 if (* (short *) (p) != 0x313B) {
118 p++; p++; // no ;1 to remove
119 }
120 if (p[-1] == '.') p--;
121 *p = 0;
122 }
123 }
124 x->curpos += size;
125 return 0;
126 }
128 #define IS_DIR(x)( ((x) & ~0777) == 040000)
129 int isoopen(char *filename)
130 {
131 int restart;
132 char *name, *s;
133 int _64bits = cpuhaslm();
134 struct isostate *x=&isostate;
136 retry32:
137 name = filename;
138 while (*name == '/') {
139 name++;
140 isoreset(NULL);
141 }
142 s = name;
143 while (1) {
144 char c;
145 while (*s && *s != '/') s++;
146 c = *s;
147 *s = 0;
148 for (restart = 1; isoreaddir(restart) == 0; restart = 0) {
149 const char *n = name, *i = x->filename;
150 if (_64bits) {
151 if (strhead(x->filename, name)) continue;
152 n = "64";
153 i += strlen(name);
154 }
155 if (strcmp(n, i)) continue;
156 if (IS_DIR(x->filemod)) {
157 x->dirofs = x->fileofs;
158 x->dirsize = x->filesize;
159 if (c) {
160 *s++ = c;
161 name = s;
162 goto next;
163 }
164 }
165 isolseek(x->fileofs);
166 return 0;
167 }
168 if (_64bits) {
169 _64bits = 0;
170 *s = c;
171 goto retry32;
172 }
173 return -1;
174 next: ;
175 }
176 }