wok-next view syslinux/stuff/iso2exe/iso9660.c @ rev 14317

elementary-dev: update deps
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Apr 07 13:15:40 2013 +0200 (2013-04-07)
parents
children 6aed6fc5819d
line source
1 #include <sys/types.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include "iso9660.h"
5 #define __ROCKRIDGE
7 char *isofilename;
8 unsigned long isofileofs, isofilesize;
9 unsigned short isofilemod;
10 int isofd;
12 #define SECTORSZ 2048
13 #define SECTORBITS 11
14 static char buffer[SECTORSZ];
16 static int readsector(unsigned long offset)
17 {
18 return (lseek(isofd, offset, SEEK_SET) != -1
19 && read(isofd, buffer, SECTORSZ) == SECTORSZ);
20 }
22 int isoread(char *data, unsigned size)
23 {
24 int get, n;
26 if (size > isofilesize)
27 size = isofilesize;
28 if (lseek(isofd, isofileofs, SEEK_SET) == -1)
29 return -1;
30 for (get = size; get; get -= n, data += n) {
31 n = read(isofd,data,get);
32 if (n < 0)
33 return n;
34 if (n == 0)
35 break;
36 isofileofs += n;
37 isofilesize -= n;
38 }
39 return size - get;
40 }
42 static unsigned long isodirofs, isodirsize;
43 int isoreset(char *name)
44 {
45 if (name)
46 isofd = open(name, O_RDONLY);
47 if (!readsector(16UL * 2048) || strncmp(buffer+1,"CD001",5))
48 return -1;
49 isodirofs = * (unsigned long *) (buffer + 0x9E);
50 isodirofs <<= SECTORBITS;
51 isodirsize = * (unsigned long *) (buffer + 0xA6);
52 return 0;
53 }
55 int isoreaddir(int restart)
56 {
57 static unsigned long pos, dirofs, dirsize;
58 static char dots[] = "..";
59 int size, n;
60 #ifdef __ROCKRIDGE
61 char *endname;
62 #endif
64 if (restart) {
65 dirofs = isodirofs;
66 dirsize = isodirsize;
67 pos = SECTORSZ;
68 }
69 if (pos >= SECTORSZ) {
70 if (dirsize < SECTORSZ) return -1;
71 readsector(dirofs);
72 dirofs += SECTORSZ;
73 dirsize -= SECTORSZ;
74 pos = 0;
75 }
76 size = * (short *) (buffer + pos);
77 if (size == 0)
78 return -1;
79 isofileofs = (* (unsigned long *) (buffer + pos + 2)) << SECTORBITS;
80 isofilesize = * (unsigned long *) (buffer + pos + 10);
81 isofilemod = (buffer[pos + 25] & 2) ? 0040755 : 0100755;
82 #ifdef __ROCKRIDGE
83 endname = NULL;
84 n = (buffer[pos + 32] + pos + 34) & -2;
85 do {
86 int len = buffer[n + 2];
87 switch (* (short *) (buffer + n)) {
88 case 0x4D4E: // NM
89 isofilename = buffer + n + 5;
90 endname = buffer + n + len;
91 break;
92 case 0x5850: // PX
93 isofilemod = * (short *) (buffer + n + 4);
94 break;
95 }
96 n += len;
97 }
98 while (n + 2 < pos + size);
99 if (endname)
100 *endname = 0;
101 else
102 #endif
103 {
104 isofilename = buffer + pos + 33;
105 switch (* (short *) (isofilename - 1)) {
106 case 0x0101:
107 isofilename = dots;
108 break;
109 case 0x0001:
110 isofilename = dots + 1;
111 break;
112 default:
113 n = isofilename[-1];
114 if (* (short *) (isofilename + n - 2) == 0x313B)
115 n -= 2; // remove ;1
116 if (isofilename[n - 1] == '.') n--;
117 isofilename[n] = 0;
118 }
119 }
120 pos += size;
121 return 0;
122 }
124 #define IS_DIR(x)( ((x) & ~0777) == 040000)
125 int isoopen(char *name)
126 {
127 int restart;
128 char *s, c;
130 while (*name == '/') {
131 name++;
132 isoreset(NULL);
133 }
134 s = name;
135 while (1) {
136 while (*s && *s != '/') s++;
137 c = *s;
138 *s = 0;
139 for (restart = 1; isoreaddir(restart) == 0; restart = 0) {
140 if (strcmp(name, isofilename)) continue;
141 if (IS_DIR(isofilemod)) {
142 isodirofs = isofileofs;
143 isodirsize = isofilesize;
144 if (c) {
145 *s++ = c;
146 name = s;
147 goto next;
148 }
149 }
150 return 0;
151 }
152 return -1;
153 next: ;
154 }
155 }