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

linld: tiny shrink
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Nov 11 10:57:19 2017 +0100 (2017-11-11)
parents 6f494adb2c71
children 46b511e941a7
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) != -1
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 static char dots[] = "..";
36 int size;
37 char *p;
38 #ifdef __ROCKRIDGE
39 char *endname;
40 #endif
41 struct isostate *x=&isostate;
43 if (restart) {
44 x->curdirsize = x->dirsize;
45 x->curdirofs = x->dirofs;
46 goto restarted;
47 }
48 if (x->curpos >= SECTORSZ || * (short *) (buffer + x->curpos) == 0) {
49 if (x->curdirsize < DIRSECTORSZ) return -1;
50 restarted:
51 readsector(&x->curdirofs);
52 x->curdirofs += SECTORSZ;
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 #ifdef __ROCKRIDGE
63 endname = NULL;
64 // p += 34 + (p[32] & -2); ?
65 p = buffer + 34 + ((p[32] + x->curpos) & -2);
66 do {
67 int len = p[2];
68 switch (* (short *) p) {
69 case 0x4D4E: // NM
70 x->filename = p + 5;
71 endname = p + len;
72 break;
73 case 0x5850: // PX
74 x->filemod = * (short *) (p + 4);
75 break;
76 }
77 p += len;
78 }
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 switch (* (short *) p) {
88 case 0x0101:
89 x->filename = dots;
90 break;
91 case 0x0001:
92 x->filename = dots + 1;
93 break;
94 default:
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 int isoopen(const char *filename)
110 {
111 int restart;
112 char *name, *s, c;
113 char _64bits = cpuhaslm();
114 struct isostate *x=&isostate;
116 retry32:
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 if (_64bits) {
142 _64bits = 0;
143 goto retry32;
144 }
145 return -1;
146 }