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

Up par2 (0.8.0)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Feb 10 12:08:41 2019 +0100 (2019-02-10)
parents ab907169f156
children cbcb33ee9044
line source
1 #include "crtl.h"
2 #include "crtlx.h"
3 #include "iso9660.h"
4 #define __ROCKRIDGE
6 struct isostate isostate;
8 #if 1
9 #define setdirofs(to,sec) (*(int*)((char*)&(to)+1) = *(int*)&(sec)<<(SECTORBITS-8))
10 #define cpydirofs(to,from) (*(int*)((char*)&(to)+1) = *(int*)((char*)&(from)+1))
11 #else
12 #define setdirofs(to,sec) (to = (sec)<<SECTORBITS)
13 #define cpydirofs(to,from) ((to) = (from))
14 #endif
16 int isoreset(char *name)
17 {
18 static const unsigned long root = 16UL * 2048;
19 struct isostate *x=&isostate;
20 if (name) {
21 //x->fd = open(name, O_RDONLY);
22 x->fd = open(name);
23 }
24 if (isoreadsector(&root) < 0 || strhead(x->buffer+1,"CD001")) {
25 //close(x->fd);
26 return -1;
27 }
28 setdirofs(x->dirofs, (* (unsigned long *) (isostate.buffer + 0x9E)));
29 x->dirsize = filesize2dirsize(* (unsigned long *) (isostate.buffer + 0xA6));
30 return 0;
31 }
33 int isoreaddir(void)
34 {
35 int size;
36 char *p;
37 #ifdef __ROCKRIDGE
38 char *endname;
39 #endif
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 ((size = * (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 endname = NULL;
67 // p += 34 + (p[32] & -2); ?
68 p = x->buffer + 34 + ((p[32] + x->curpos) & -2);
69 do {
70 int len = p[2];
71 switch (* (short *) p) {
72 case 0x4D4E: // NM
73 x->filename = p + 5;
74 endname = p + len;
75 break;
76 case 0x5850: // PX
77 x->filemod = * (short *) (p + 4);
78 break;
79 }
80 p += len;
81 } while (x->buffer + x->curpos + size - p > 2);
82 if (endname)
83 *endname = 0;
84 else
85 #endif
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) == 1)
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 x->curpos += size;
104 return 0;
105 }
107 //#define IS_DIR(x)( ((x) & ~0777) == 040000)
108 //#define IS_DIR(x)( (char)((x) >> 9) == (char)040)
109 //#define IS_DIR(x)( (*((char*) &x + 1) & (char)0776) == (char)0100)
110 #define IS_DIR(x)( (*((char*) &x + 1) & (char)0676) == (char)0)
111 int isoopen(const char *filename)
112 {
113 char *name, *s, c;
114 char _64bits = cpuhaslm();
115 struct isostate *x=&isostate;
117 do {
118 for (s = (char *) filename; *s == '/' ; s++) {
119 isoreset(NULL);
120 }
121 next:
122 for (name = s; *s && *s != '/'; s++);
123 c = *s;
124 *s = 0;
125 for (x->curdirsize = 0xFFFF; isoreaddir() == 0;) {
126 const char *n = name, *i = x->filename;
127 if (_64bits) {
128 if (strhead(i, n)) continue;
129 n = "64";
130 i += s - name; // strlen(name);
131 }
132 if (strcmp(i, n)) continue;
133 *s++ = c;
134 if (IS_DIR(x->filemod)) {
135 cpydirofs(x->dirofs, x->fileofs);
136 x->dirsize = filesize2dirsize(x->filesize);
137 if (c) goto next;
138 }
139 isolseek(&x->fileofs);
140 return 0;
141 }
142 } while ((_64bits ^= CPUMASKLM) == 0);
143 return -1;
144 }