wok-6.x view linld/stuff/src/ISO9660.CPP @ rev 20528
linld: pascal convention calls
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Fri Nov 09 16:37:55 2018 +0100 (2018-11-09) |
parents | 77cbb661144f |
children | af959ebf1305 |
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+512]; // RR overflow
9 struct isostate isostate;
11 static int readsector(const unsigned long *offset)
12 {
13 return (isolseek(offset) != 0
14 && read(isostate.fd, buffer, sizeof(buffer)) >= SECTORSZ);
15 }
17 int isoreset(char *name)
18 {
19 static const unsigned long root = 16UL * 2048;
20 struct isostate *x=&isostate;
21 if (name)
22 //x->fd = open(name, O_RDONLY);
23 x->fd = open(name);
24 if (!readsector(&root) || strhead(buffer+1,"CD001")) {
25 //close(x->fd);
26 return -1;
27 }
28 x->dirofs = (* (unsigned long *) (buffer + 0x9E)) << SECTORBITS;
29 x->dirsize = filesize2dirsize(* (unsigned long *) (buffer + 0xA6));
30 return 0;
31 }
33 int isoreaddir(int restart)
34 {
35 int size;
36 char *p;
37 #ifdef __ROCKRIDGE
38 char *endname;
39 #endif
40 struct isostate *x=&isostate;
42 if (restart) {
43 x->curdirsize = x->dirsize;
44 x->curdirofs = x->dirofs;
45 goto restarted;
46 }
47 if (x->curpos >= SECTORSZ || * (short *) (buffer + x->curpos) == 0) {
48 if (x->curdirsize < DIRSECTORSZ) return -1;
49 restarted:
50 readsector(&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 = buffer + x->curpos;
57 if ((size = * (short *) p) == 0)
58 return -1;
59 x->fileofs = (* (unsigned long *) (p + 2)) << SECTORBITS;
60 x->filesize = * (unsigned long *) (p + 10);
61 x->filemod = (p[25] & 2) ? 0040755 : 0100755;
62 //x->filemod = 0100755 - ((p[25] & (char)2) << 13);
63 #ifdef __ROCKRIDGE
64 endname = NULL;
65 // p += 34 + (p[32] & -2); ?
66 p = buffer + 34 + ((p[32] + x->curpos) & -2);
67 do {
68 int len = p[2];
69 switch (* (short *) p) {
70 case 0x4D4E: // NM
71 x->filename = p + 5;
72 endname = p + len;
73 break;
74 case 0x5850: // PX
75 x->filemod = * (short *) (p + 4);
76 break;
77 }
78 p += len;
79 } while (buffer + x->curpos + size - p > 2);
80 if (endname)
81 *endname = 0;
82 else
83 #endif
84 {
85 p = x->filename = buffer + x->curpos + 33;
86 p--;
87 if (((* (short *) p) & 0xFEFF) -1 == 0) {
88 x->filename = "..";
89 if ((* (short *) p) == 1)
90 x->filename++;
91 }
92 else {
93 p += *p; p--;
94 if (* (short *) (p) != 0x313B) {
95 p++; p++; // no ;1 to remove
96 }
97 if (p[-1] == '.') p--;
98 *p = 0;
99 }
100 }
101 x->curpos += size;
102 return 0;
103 }
105 //#define IS_DIR(x)( ((x) & ~0777) == 040000)
106 //#define IS_DIR(x)( (char)((x) >> 9) == (char)040)
107 //#define IS_DIR(x)( (*((char*) &x + 1) & (char)0776) == (char)0100)
108 #define IS_DIR(x)( (*((char*) &x + 1) & (char)0676) == (char)0)
109 int isoopen(const char *filename)
110 {
111 int restart;
112 char *name, *s, c;
113 char _64bits = cpuhaslm();
114 struct isostate *x=&isostate;
116 do {
117 for (s = (char *) filename; *s == '/' ; s++) {
118 isoreset(NULL);
119 }
120 next:
121 for (name = s; *s && *s != '/'; s++);
122 c = *s;
123 *s = 0;
124 for (restart = 1; isoreaddir(restart) == 0; restart = 0) {
125 const char *n = name, *i = x->filename;
126 if (_64bits) {
127 if (strhead(i, n)) continue;
128 n = "64";
129 i += s - name; // strlen(name);
130 }
131 if (strcmp(i, n)) continue;
132 *s++ = c;
133 if (IS_DIR(x->filemod)) {
134 x->dirofs = x->fileofs;
135 x->dirsize = filesize2dirsize(x->filesize);
136 if (c) goto next;
137 }
138 isolseek(&x->fileofs);
139 return 0;
140 }
141 } while ((_64bits ^= CPUMASKLM) == 0);
142 return -1;
143 }