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