1 | /*␊ |
2 | * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.␊ |
3 | *␊ |
4 | * @APPLE_LICENSE_HEADER_START@␊ |
5 | * ␊ |
6 | * The contents of this file constitute Original Code as defined in and␊ |
7 | * are subject to the Apple Public Source License Version 2.0 (the␊ |
8 | * "License"). You may not use this file except in compliance with the␊ |
9 | * License. Please obtain a copy of the License at␊ |
10 | * http://www.apple.com/publicsource and read it before using this file.␊ |
11 | * ␊ |
12 | * This Original Code and all software distributed under the License are␊ |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER␊ |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,␊ |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,␊ |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the␊ |
17 | * License for the specific language governing rights and limitations␊ |
18 | * under the License.␊ |
19 | * ␊ |
20 | * @APPLE_LICENSE_HEADER_END@␊ |
21 | */␊ |
22 | /*␊ |
23 | * Copyright (c) 1998 Robert Nordier␊ |
24 | * All rights reserved.␊ |
25 | *␊ |
26 | * Redistribution and use in source and binary forms, with or without␊ |
27 | * modification, are permitted provided that the following conditions␊ |
28 | * are met:␊ |
29 | * 1. Redistributions of source code must retain the above copyright␊ |
30 | * notice, this list of conditions and the following disclaimer.␊ |
31 | * 2. Redistributions in binary form must reproduce the above copyright␊ |
32 | * notice, this list of conditions and the following disclaimer in␊ |
33 | * the documentation and/or other materials provided with the␊ |
34 | * distribution.␊ |
35 | *␊ |
36 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS␊ |
37 | * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED␊ |
38 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE␊ |
39 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY␊ |
40 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL␊ |
41 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE␊ |
42 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS␊ |
43 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER␊ |
44 | * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR␊ |
45 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN␊ |
46 | * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.␊ |
47 | */␊ |
48 | ␊ |
49 | #define tolower(c) (((c)>='A' && c<='Z')?((c) | 0x20):(c))␊ |
50 | #include "libsaio.h"␊ |
51 | #include "sl.h"␊ |
52 | ␊ |
53 | #include "msdos_private.h"␊ |
54 | #include "msdos.h"␊ |
55 | ␊ |
56 | #define LABEL_LENGTH␉␉11␊ |
57 | #define MAX_DOS_BLOCKSIZE␉2048␊ |
58 | #define MAX_CACHE_BLOCKSIZE 32768␊ |
59 | ␊ |
60 | #define␉CLUST_FIRST␉␉2/* reserved cluster range */␊ |
61 | #define␉CLUST_RSRVD32␉0x0ffffff8␉/* reserved cluster range */␊ |
62 | #define␉CLUST_RSRVD16␉0xfff8␉/* reserved cluster range */␊ |
63 | #define␉CLUST_RSRVD12␉0xff8␉/* reserved cluster range */␊ |
64 | ␊ |
65 | ␊ |
66 | #define false 0␊ |
67 | #define true 1␊ |
68 | ␊ |
69 | static int msdosressector=0;␊ |
70 | static int msdosnfats = 0;␊ |
71 | static int msdosfatsecs = 0;␊ |
72 | static int msdosbps = 0;␊ |
73 | static int msdosclustersize = 0;␊ |
74 | static int msdosrootDirSectors = 0;␊ |
75 | static CICell msdoscurrent = 0;␊ |
76 | static int msdosrootcluster = 0;␊ |
77 | static int msdosfatbits = 0;␊ |
78 | ␊ |
79 | #if UNUSED␊ |
80 | /*␊ |
81 | * Check a volume label.␊ |
82 | */␊ |
83 | static int␊ |
84 | oklabel(const char *src)␊ |
85 | {␊ |
86 | int c, i;␊ |
87 | ␊ |
88 | for (i = 0, c = 0; i <= 11; i++) {␊ |
89 | c = (u_char)*src++;␊ |
90 | if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))␊ |
91 | break;␊ |
92 | }␊ |
93 | return i && !c;␊ |
94 | }␊ |
95 | #endif /* UNUSED */␊ |
96 | ␊ |
97 | void MSDOSFree(CICell ih)␊ |
98 | {␊ |
99 | ␉if(msdoscurrent == ih)␊ |
100 | msdoscurrent = 0;␊ |
101 | free(ih);␊ |
102 | }␊ |
103 | ␊ |
104 | int MSDOSProbe(const void * buffer)␊ |
105 | {␊ |
106 | union bootsector *bsp;␊ |
107 | struct bpb33 *b33;␊ |
108 | struct bpb50 *b50;␊ |
109 | struct bpb710 *b710;␊ |
110 | u_int16_t␉ bps;␊ |
111 | u_int8_t␉␉spc;␊ |
112 | ␉␊ |
113 | bsp = (union bootsector *)buffer;␊ |
114 | b33 = (struct bpb33 *)bsp->bs33.bsBPB;␊ |
115 | b50 = (struct bpb50 *)bsp->bs50.bsBPB;␊ |
116 | b710 = (struct bpb710 *)bsp->bs710.bsBPB;␊ |
117 | ␊ |
118 | /* We only work with 512, 1024, and 2048 byte sectors */␊ |
119 | bps = OSSwapLittleToHostInt16(b33->bpbBytesPerSec);␊ |
120 | if ((bps < 0x200) || (bps & (bps - 1)) || (bps > 0x800)) ␊ |
121 | return 0;␊ |
122 | ␊ |
123 | ␉/* Check to make sure valid sectors per cluster */␊ |
124 | spc = b33->bpbSecPerClust;␊ |
125 | if ((spc == 0 ) || (spc & (spc - 1))) ␊ |
126 | return 0;␉␊ |
127 | ␉␊ |
128 | ␉if (OSSwapLittleToHostInt16(b50->bpbRootDirEnts) == 0) { /* It's FAT32 */␊ |
129 | ␉␉if (!memcmp(((struct extboot *)bsp->bs710.bsExt)->exFileSysType, "FAT32 ", 8))␊ |
130 | ␉␉␉return 32;␊ |
131 | ␉}␊ |
132 | ␉else if (((struct extboot *)bsp->bs50.bsExt)->exBootSignature == EXBOOTSIG) {␊ |
133 | ␉␉if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT16 ", 8))␊ |
134 | ␉␉␉return 16;␊ |
135 | ␉␉if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT12 ", 8))␊ |
136 | ␉␉␉return 12;␊ |
137 | ␉}␉␊ |
138 | ␉␉␊ |
139 | ␉return 0;␊ |
140 | }␊ |
141 | ␊ |
142 | ␊ |
143 | long␊ |
144 | MSDOSInitPartition (CICell ih)␊ |
145 | {␊ |
146 | union bootsector *bsp;␊ |
147 | struct bpb33 *b33;␊ |
148 | struct bpb50 *b50;␊ |
149 | struct bpb710 *b710;␊ |
150 | u_int8_t␉␉spc;␊ |
151 | char *buf;␊ |
152 | ␉␊ |
153 | ␉if (msdoscurrent == ih)␊ |
154 | ␉{␊ |
155 | ␉␉CacheInit(ih, msdosclustersize);␊ |
156 | ␉␉return 0;␊ |
157 | ␉}␊ |
158 | ␉␊ |
159 | ␉buf=malloc (512);␊ |
160 | ␉/*␊ |
161 | * Read the boot sector of the filesystem, and then check the␊ |
162 | * boot signature. If not a dos boot sector then error out.␊ |
163 | *␊ |
164 | * NOTE: 2048 is a maximum sector size in current...␊ |
165 | */␊ |
166 | Seek(ih, 0);␊ |
167 | Read(ih, (long)buf, 512);␊ |
168 | ␉␊ |
169 | bsp = (union bootsector *)buf;␊ |
170 | b33 = (struct bpb33 *)bsp->bs33.bsBPB;␊ |
171 | b50 = (struct bpb50 *)bsp->bs50.bsBPB;␊ |
172 | b710 = (struct bpb710 *)bsp->bs710.bsBPB;␊ |
173 | ␊ |
174 | ␉␊ |
175 | /* We only work with 512, 1024, and 2048 byte sectors */␊ |
176 | msdosbps = OSSwapLittleToHostInt16(b33->bpbBytesPerSec);␊ |
177 | if ((msdosbps < 0x200) || (msdosbps & (msdosbps - 1)) || (msdosbps > 0x800)) ␊ |
178 | ␉{␊ |
179 | ␉␉free (buf);␊ |
180 | ␉␉return -1;␊ |
181 | ␉}␊ |
182 | ␉␊ |
183 | /* Check to make sure valid sectors per cluster */␊ |
184 | spc = b33->bpbSecPerClust;␊ |
185 | if ((spc == 0 ) || (spc & (spc - 1))) ␊ |
186 | ␉␉return -1;␊ |
187 | ␉␊ |
188 | ␉if (OSSwapLittleToHostInt16(b50->bpbRootDirEnts) == 0) { /* It's FAT32 */␊ |
189 | ␉␉if (memcmp(((struct extboot *)bsp->bs710.bsExt)->exFileSysType, "FAT32 ", 8))␊ |
190 | ␉␉{␊ |
191 | ␉␉␉free (buf);␊ |
192 | ␉␉␉return -1;␊ |
193 | ␉␉}␊ |
194 | ␉␉msdosressector = OSSwapLittleToHostInt16(b710->bpbResSectors);␊ |
195 | ␉␉msdosnfats = b710->bpbFATs;␊ |
196 | ␉␉msdosfatsecs = OSSwapLittleToHostInt16(b710->bpbBigFATsecs);␊ |
197 | ␉␉msdosrootcluster = OSSwapLittleToHostInt32(b710->bpbRootClust);␊ |
198 | ␉␉msdosrootDirSectors = 0;␊ |
199 | ␉␉msdosfatbits = 32;␊ |
200 | ␉}␊ |
201 | ␉else if (((struct extboot *)bsp->bs50.bsExt)->exBootSignature == EXBOOTSIG) {␊ |
202 | ␉␉if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT16 ", 8))␊ |
203 | ␉␉␉msdosfatbits = 16;␊ |
204 | ␉␉else if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT12 ", 8))␊ |
205 | ␉␉␉msdosfatbits = 12;␊ |
206 | ␉␉else␊ |
207 | ␉␉{␊ |
208 | ␉␉␉free (buf);␊ |
209 | ␉␉␉return -1;␊ |
210 | ␉␉}␉␉␊ |
211 | ␉␉␊ |
212 | ␉␉msdosressector = OSSwapLittleToHostInt16(b33->bpbResSectors);␊ |
213 | ␉␉msdosnfats = b33->bpbFATs;␊ |
214 | ␉␉msdosfatsecs = OSSwapLittleToHostInt16(b33->bpbFATsecs);␊ |
215 | ␉␉msdosrootcluster = 0;␉␉␊ |
216 | ␉␉msdosrootDirSectors = ((OSSwapLittleToHostInt16(b50->bpbRootDirEnts) * sizeof(struct direntry)) +␊ |
217 | ␉␉␉␉␉␉␉ (msdosbps-1)) / msdosbps;␊ |
218 | ␉} else {␊ |
219 | ␉␉free (buf);␊ |
220 | ␉␉return -1;␊ |
221 | ␉}␊ |
222 | ␉␊ |
223 | ␉msdosclustersize = msdosbps * spc;␊ |
224 | ␉msdoscurrent = ih;␊ |
225 | ␉␊ |
226 | ␉CacheInit(ih, msdosclustersize);␊ |
227 | ␉free (buf);␊ |
228 | ␉return 0;␊ |
229 | }␊ |
230 | ␊ |
231 | static int␊ |
232 | readSectorAligned(CICell ih, off_t readOffset, char *buf, int size)␊ |
233 | {␊ |
234 | long long sectorOffset = (uint64_t)readOffset / msdosclustersize * msdosclustersize;␊ |
235 | long relOffset = readOffset % msdosclustersize;␊ |
236 | char *cacheBuffer;␊ |
237 | ␊ |
238 | cacheBuffer = malloc(msdosclustersize);␊ |
239 | CacheRead(ih, cacheBuffer, sectorOffset, msdosclustersize, true);␊ |
240 | bcopy(cacheBuffer + relOffset, buf, size);␊ |
241 | free(cacheBuffer);␊ |
242 | ␊ |
243 | return 0;␊ |
244 | }␊ |
245 | ␊ |
246 | static int␊ |
247 | msdosreadcluster (CICell ih, uint8_t *buf, int size, off_t *cluster)␊ |
248 | {␊ |
249 | off_t readOffset;␊ |
250 | ␉char tmpbuf[8];␊ |
251 | ␉off_t clusn;␊ |
252 | ␉␊ |
253 | ␉switch (msdosfatbits) {␊ |
254 | ␉␉case 32:␊ |
255 | ␉␉␉if (*cluster < CLUST_FIRST ||*cluster >= CLUST_RSRVD32)␊ |
256 | ␉␉␉␉return 0;␊ |
257 | ␉␉␉clusn = *cluster - CLUST_FIRST;␊ |
258 | ␉␉␉break;␊ |
259 | ␉␉case 16:␊ |
260 | ␉␉␉if (*cluster < CLUST_FIRST ||*cluster >= CLUST_RSRVD16)␊ |
261 | ␉␉␉␉return 0;␊ |
262 | ␉␉␉clusn = *cluster - CLUST_FIRST;␊ |
263 | ␉␉␉break;␉␉␉␊ |
264 | ␉␉case 12:␊ |
265 | ␉␉␉if (*cluster < CLUST_FIRST ||*cluster >= CLUST_RSRVD12)␊ |
266 | ␉␉␉␉return 0;␊ |
267 | ␉␉␉clusn = *cluster - CLUST_FIRST;␊ |
268 | ␉␉␉break;␊ |
269 | ␉␉default:␊ |
270 | ␉␉␉return 0;␊ |
271 | ␉}␊ |
272 | ␉␊ |
273 | ␉/* Find sector where clusters start */␊ |
274 | ␉readOffset = (msdosressector +␊ |
275 | ␉␉␉␉ (msdosnfats * msdosfatsecs)+msdosrootDirSectors)*msdosbps;␊ |
276 | ␉/* Find sector where "cluster" starts */␊ |
277 | ␉readOffset += clusn * msdosclustersize;␊ |
278 | ␉␊ |
279 | ␉/* Read in "cluster" */␊ |
280 | ␉if (buf)␊ |
281 | ␉{␊ |
282 | readSectorAligned(ih, readOffset, (char *)buf, size);␊ |
283 | ␉}␊ |
284 | ␊ |
285 | ␉/* Find first sector of FAT */␊ |
286 | ␉readOffset = msdosressector * msdosbps;␊ |
287 | ␊ |
288 | ␉/* Find sector containing "cluster" entry in FAT */␊ |
289 | ␉readOffset += ((uint64_t)*cluster * (uint64_t)msdosfatbits) / 8;␊ |
290 | ␉␊ |
291 | ␉/* Read one sector of the FAT */␊ |
292 | ␉readSectorAligned(ih, readOffset, tmpbuf, 4);␊ |
293 | ␉␊ |
294 | ␉switch (msdosfatbits) {␊ |
295 | ␉␉case 32:␊ |
296 | ␉␉␉*cluster = OSReadLittleInt32(tmpbuf, 0);␊ |
297 | ␉␉␉*cluster &= 0x0FFFFFFF;␉// ignore reserved upper bits␊ |
298 | ␉␉␉return 1;␊ |
299 | ␉␉case 16:␊ |
300 | ␉␉␉*cluster = OSReadLittleInt16(tmpbuf, 0);␊ |
301 | ␉␉␉return 1;␉␉␉␊ |
302 | ␉␉case 12:␊ |
303 | ␉␉␉*cluster = OSReadLittleInt16(tmpbuf, 0)>>(((uint64_t)*cluster * (uint64_t)msdosfatbits)%8);␊ |
304 | ␉␉␉*cluster &= 0xfff;␊ |
305 | ␉␉␉return 1;␊ |
306 | ␉␉default:␊ |
307 | ␉␉␉return 0;␊ |
308 | ␉}␊ |
309 | }␊ |
310 | ␊ |
311 | struct msdosdirstate␊ |
312 | {␊ |
313 | ␉struct direntry *buf;␊ |
314 | ␉uint8_t vfatchecksum;␊ |
315 | ␉int root16;␊ |
316 | ␉off_t cluster;␊ |
317 | ␉int nument;␊ |
318 | ␉int vfatnumber;␊ |
319 | };␊ |
320 | ␊ |
321 | static struct direntry *␊ |
322 | getnextdirent (CICell ih, uint16_t *longname, struct msdosdirstate *st)␊ |
323 | {␊ |
324 | ␉struct direntry *dirp;␊ |
325 | ␉while (1)␊ |
326 | ␉{␊ |
327 | ␉␉if (st->root16)␊ |
328 | ␉␉{␊ |
329 | ␉␉␉if (st->cluster >= msdosrootDirSectors && st->nument == 0)␊ |
330 | ␉␉␉␉return 0;␊ |
331 | ␉␉␉if (st->nument == 0)␊ |
332 | ␉␉␉{␊ |
333 | ␉␉␉␉Seek(ih, (msdosressector +␊ |
334 | ␉␉␉␉␉␉ (msdosnfats * msdosfatsecs)+st->cluster)*msdosbps);␊ |
335 | ␉␉␉␉Read(ih, (long)st->buf, msdosbps);␊ |
336 | ␉␉␉␉st->cluster++;␊ |
337 | ␉␉␉}␊ |
338 | ␉␉} else if (st->nument == 0 && !msdosreadcluster (ih, (uint8_t *)st->buf, msdosclustersize, &(st->cluster)))␊ |
339 | ␉␉␉return 0;␊ |
340 | ␉␉␊ |
341 | ␉␉dirp=st->buf+st->nument;␊ |
342 | ␉␉␊ |
343 | ␉␉if (dirp->deName[0] == SLOT_EMPTY)␊ |
344 | ␉␉␉return 0;␊ |
345 | ␉␉else if (dirp->deName[0] == SLOT_DELETED)␊ |
346 | ␉␉␉st->vfatnumber = 0;␊ |
347 | ␉␉else if (dirp->deAttributes == ATTR_WIN95)␊ |
348 | ␉␉{␊ |
349 | ␉␉␉struct winentry *wdirp = (struct winentry *)dirp;␊ |
350 | ␉␉␉int num;␊ |
351 | ␉␉␉if (wdirp->weCnt & 0x80)␊ |
352 | ␉␉␉␉continue;␊ |
353 | ␉␉␉num=(wdirp->weCnt&0x3f);␊ |
354 | ␉␉␉if (WIN_CHARS * num > WIN_MAXLEN)␊ |
355 | ␉␉␉␉continue;␊ |
356 | ␉␉␉if (st->vfatchecksum!=wdirp->weChksum)␊ |
357 | ␉␉␉{␊ |
358 | ␉␉␉␉st->vfatnumber = 0;␊ |
359 | ␉␉␉␉st->vfatchecksum = wdirp->weChksum;␊ |
360 | ␉␉␉}␊ |
361 | ␉␉␉if (st->vfatnumber < num)␊ |
362 | ␉␉␉␉st->vfatnumber = num;␊ |
363 | ␉␉␉bcopy (&(wdirp->wePart1),longname+WIN_CHARS*(num-1),sizeof (wdirp->wePart1));␊ |
364 | ␉␉␉bcopy (&(wdirp->wePart2),longname+WIN_CHARS*(num-1)+5,sizeof (wdirp->wePart2));␊ |
365 | ␉␉␉bcopy (&(wdirp->wePart3),longname+WIN_CHARS*(num-1)+11,sizeof (wdirp->wePart3));␊ |
366 | ␉␉} else {␊ |
367 | ␉␉␉uint8_t labelchecksum;␊ |
368 | ␉␉␉int i;␊ |
369 | ␉␉␉longname[st->vfatnumber*WIN_CHARS]=0;␊ |
370 | ␉␉␉␊ |
371 | ␉␉␉labelchecksum=0;␊ |
372 | ␉␉␉for(i=0;i<LABEL_LENGTH;i++)␊ |
373 | ␉␉␉␉labelchecksum=(labelchecksum>>1)+(labelchecksum<<7)+dirp->deName[i];␊ |
374 | ␉␉␉if (!(labelchecksum==st->vfatchecksum && st->vfatnumber))␊ |
375 | ␉␉␉␉longname[0]=0;␊ |
376 | ␉␉␉st->vfatnumber = 0;␊ |
377 | ␉␉␉st->vfatchecksum = 0;␊ |
378 | ␉␉␉st->vfatnumber = 0;␊ |
379 | ␉␉␉st->nument++;␊ |
380 | ␉␉␉if ((!st->root16 &&st->nument * sizeof (struct direntry)>=msdosclustersize)␊ |
381 | ␉␉␉␉|| (st->root16 &&st->nument * sizeof (struct direntry)>=msdosbps))␊ |
382 | ␉␉␉␉st->nument = 0;␊ |
383 | ␉␉␉return dirp;␊ |
384 | ␉␉}␊ |
385 | ␉␉st->nument++;␊ |
386 | ␉␉if ((!st->root16 &&st->nument * sizeof (struct direntry)>=msdosclustersize)␊ |
387 | ␉␉␉|| (st->root16 &&st->nument * sizeof (struct direntry)>=msdosbps))␊ |
388 | ␉␉␉st->nument = 0;␉␉␊ |
389 | ␉}␊ |
390 | }␊ |
391 | ␊ |
392 | static void␊ |
393 | initRoot (struct msdosdirstate *st)␊ |
394 | {␊ |
395 | ␉if (msdosrootDirSectors) {␉␉␉/* FAT12 or FAT16 */␊ |
396 | ␉␉st->root16 = 1;␊ |
397 | ␉␉st->vfatchecksum = 0;␊ |
398 | ␉␉st->nument = 0;␊ |
399 | ␉␉st->cluster = 0;␊ |
400 | ␉␉st->vfatnumber = 0;␊ |
401 | ␉} else {␉/* FAT32 */␊ |
402 | ␉␉st->root16 = 0;␊ |
403 | ␉␉st->vfatchecksum = 0;␊ |
404 | ␉␉st->nument = 0;␊ |
405 | ␉␉st->cluster = msdosrootcluster;␊ |
406 | ␉␉st->vfatnumber = 0;␊ |
407 | ␉}␊ |
408 | }␊ |
409 | ␊ |
410 | /* First comes lowercase, then uppercase*/␊ |
411 | static uint16_t cp850[128][2]=␊ |
412 | {␊ |
413 | {0x00E7,0x00C7},␊ |
414 | {0x00FC,0x00DC},␊ |
415 | {0x00E9,0x00C9},␊ |
416 | {0x00E2,0x00C2},␊ |
417 | {0x00E4,0x00C4},␊ |
418 | {0x00E0,0x00C0},␊ |
419 | {0x00E5,0x00C5},␊ |
420 | {0x00E7,0x00C7},␊ |
421 | {0x00EA,0x00CA},␊ |
422 | {0x00EB,0x00CB},␊ |
423 | {0x00E8,0x00C8},␊ |
424 | {0x00EF,0x00CF},␊ |
425 | {0x00EE,0x00CE},␊ |
426 | {0x00EC,0x00CC},␊ |
427 | {0x00E4,0x00C4},␊ |
428 | {0x00E5,0x00C5},␊ |
429 | {0x00E9,0x00C9},␊ |
430 | {0x00E6,0x00C6},␊ |
431 | {0x00E6,0x00C6},␊ |
432 | {0x00F4,0x00D4},␊ |
433 | {0x00F6,0x00D6},␊ |
434 | {0x00F2,0x00D2},␊ |
435 | {0x00FB,0x00DB},␊ |
436 | {0x00F9,0x00D9},␊ |
437 | {0x00FF,0x0178},␊ |
438 | {0x00F6,0x00D6},␊ |
439 | {0x00FC,0x00DC},␊ |
440 | {0x00F8,0x00D8},␊ |
441 | {0x00A3,0x00A3},␊ |
442 | {0x00F8,0x00D8},␊ |
443 | {0x00D7,0x00D7},␊ |
444 | {0x0192,0x0191},␊ |
445 | {0x00E1,0x00C1},␊ |
446 | {0x00ED,0x00CD},␊ |
447 | {0x00F3,0x00D3},␊ |
448 | {0x00FA,0x00DA},␊ |
449 | {0x00F1,0x00D1},␊ |
450 | {0x00F1,0x00D1},␊ |
451 | {0x00AA,0x00AA},␊ |
452 | {0x00BA,0x00BA},␊ |
453 | {0x00BF,0x00BF},␊ |
454 | {0x00AE,0x00AE},␊ |
455 | {0x00AC,0x00AC},␊ |
456 | {0x00BD,0x00BD},␊ |
457 | {0x00BC,0x00BC},␊ |
458 | {0x00A1,0x00A1},␊ |
459 | {0x00AB,0x00AB},␊ |
460 | {0x00BB,0x00BB},␊ |
461 | {0x2591,0x2591},␊ |
462 | {0x2592,0x2592},␊ |
463 | {0x2593,0x2593},␊ |
464 | {0x2502,0x2502},␊ |
465 | {0x2524,0x2524},␊ |
466 | {0x00E1,0x00C1},␊ |
467 | {0x00E2,0x00C2},␊ |
468 | {0x00E0,0x00C0},␊ |
469 | {0x00A9,0x00A9},␊ |
470 | {0x2563,0x2563},␊ |
471 | {0x2551,0x2551},␊ |
472 | {0x2557,0x2557},␊ |
473 | {0x255D,0x255D},␊ |
474 | {0x00A2,0x00A2},␊ |
475 | {0x00A5,0x00A5},␊ |
476 | {0x2510,0x2510},␊ |
477 | {0x2514,0x2514},␊ |
478 | {0x2534,0x2534},␊ |
479 | {0x252C,0x252C},␊ |
480 | {0x251C,0x251C},␊ |
481 | {0x2500,0x2500},␊ |
482 | {0x253C,0x253C},␊ |
483 | {0x00E3,0x00C3},␊ |
484 | {0x00E3,0x00C3},␊ |
485 | {0x255A,0x255A},␊ |
486 | {0x2554,0x2554},␊ |
487 | {0x2569,0x2569},␊ |
488 | {0x2566,0x2566},␊ |
489 | {0x2560,0x2560},␊ |
490 | {0x2550,0x2550},␊ |
491 | {0x256C,0x256C},␊ |
492 | {0x00A4,0x00A4},␊ |
493 | {0x00F0,0x00D0},␊ |
494 | {0x00F0,0x00D0},␊ |
495 | {0x00EA,0x00CA},␊ |
496 | {0x00EB,0x00CB},␊ |
497 | {0x00E8,0x00C8},␊ |
498 | {0x0131,0x0049},␊ |
499 | {0x00ED,0x00CD},␊ |
500 | {0x00EE,0x00CE},␊ |
501 | {0x00EF,0x00CF},␊ |
502 | {0x2518,0x2518},␊ |
503 | {0x250C,0x250C},␊ |
504 | {0x2588,0x2588},␊ |
505 | {0x2584,0x2584},␊ |
506 | {0x00A6,0x00A6},␊ |
507 | {0x00EC,0x00CC},␊ |
508 | {0x2580,0x2580},␊ |
509 | {0x00F3,0x00D3},␊ |
510 | {0x00DF,0x00DF},␊ |
511 | {0x00F4,0x00D4},␊ |
512 | {0x00F2,0x00D2},␊ |
513 | {0x00F5,0x00D5},␊ |
514 | {0x00F5,0x00D5},␊ |
515 | {0x00B5,0x00B5},␊ |
516 | {0x00FE,0x00DE},␊ |
517 | {0x00FE,0x00DE},␊ |
518 | {0x00FA,0x00DA},␊ |
519 | {0x00FB,0x00DB},␊ |
520 | {0x00F9,0x00D9},␊ |
521 | {0x00FD,0x00DD},␊ |
522 | {0x00FD,0x00DD},␊ |
523 | {0x00AF,0x00AF},␊ |
524 | {0x00B4,0x00B4},␊ |
525 | {0x00AD,0x00AD},␊ |
526 | {0x00B1,0x00B1},␊ |
527 | {0x2017,0x2017},␊ |
528 | {0x00BE,0x00BE},␊ |
529 | {0x00B6,0x00B6},␊ |
530 | {0x00A7,0x00A7},␊ |
531 | {0x00F7,0x00F7},␊ |
532 | {0x00B8,0x00B8},␊ |
533 | {0x00B0,0x00B0},␊ |
534 | {0x00A8,0x00A8},␊ |
535 | {0x00B7,0x00B7},␊ |
536 | {0x00B9,0x00B9},␊ |
537 | {0x00B3,0x00B3},␊ |
538 | {0x00B2,0x00B2},␊ |
539 | {0x25A0,0x25A0},␊ |
540 | {0x00A0,0x00A0}␊ |
541 | };␊ |
542 | ␊ |
543 | static int␊ |
544 | checkname (uint16_t *ucsname, int ucslen, struct direntry *dirp, uint16_t *vfatname)␊ |
545 | {␊ |
546 | ␉uint16_t tmp[15];␊ |
547 | ␉if (vfatname[0])␊ |
548 | ␉{␊ |
549 | ␉␉int i;␊ |
550 | ␉␉for (i=0;vfatname[i];i++);␊ |
551 | ␉␉return !FastUnicodeCompare (ucsname, ucslen, vfatname, i, OSLittleEndian);␊ |
552 | ␉}␊ |
553 | ␉else␊ |
554 | ␉{␊ |
555 | ␉␉int i, j, k;␊ |
556 | ␉␉for (i=7;i>=0;i--)␊ |
557 | ␉␉␉if (dirp->deName[i]!=' ')␊ |
558 | ␉␉␉␉break;␊ |
559 | ␉␉j=i+1;␊ |
560 | ␉␉tmp[i+1]=0;␊ |
561 | ␉␉for(;i>=0;i--)␊ |
562 | ␉␉␉tmp[i]=SWAP_LE16((dirp->deName[i]>=128)?cp850[dirp->deName[i]-128][0]:tolower(dirp->deName[i]));␊ |
563 | ␉␉for (i=2;i>=0;i--)␊ |
564 | ␉␉␉if (dirp->deName[8+i]!=' ')␊ |
565 | ␉␉␉␉break;␊ |
566 | ␉␉if (i>=0)␊ |
567 | ␉␉{␊ |
568 | ␉␉␉tmp[j++]='.';␊ |
569 | ␉␉␉tmp[j+i+1]=0;␊ |
570 | ␉␉␉k=j+i+1;␊ |
571 | ␉␉␉for(;i>=0;i--)␊ |
572 | ␉␉␉␉tmp[j+i]=SWAP_LE16((dirp->deName[i+8]>=128)?cp850[dirp->deName[i+8]-128][0]:tolower(dirp->deName[i+8]));␊ |
573 | ␉␉␉j=k;␊ |
574 | ␉␉}␊ |
575 | ␉␉return !FastUnicodeCompare (ucsname, ucslen, tmp, j, OSLittleEndian);␊ |
576 | ␉}␊ |
577 | ␉␊ |
578 | }␊ |
579 | ␊ |
580 | static struct direntry *␊ |
581 | getdirpfrompath (CICell ih, char *dirspec, uint8_t *buf)␊ |
582 | {␊ |
583 | ␉struct msdosdirstate st;␊ |
584 | ␉struct direntry *dirp;␊ |
585 | ␉uint8_t * ptr;␊ |
586 | ␉uint8_t *slash;␊ |
587 | ␉char c;␊ |
588 | ␉uint16_t␉␉vfatname[WIN_MAXLEN+2*WIN_CHARS];␊ |
589 | ␉uint16_t ucsname[WIN_MAXLEN+1];␊ |
590 | ␉uint16_t ucslen;␊ |
591 | ␉int ucslenhost;␊ |
592 | ␉initRoot (&st);␊ |
593 | ␉st.buf = (struct direntry *)buf;␊ |
594 | ␉ptr=(uint8_t*)dirspec;␊ |
595 | ␉for (slash=ptr;*slash && *slash!='/';slash++);␊ |
596 | ␉c=*slash;␊ |
597 | ␉*slash=0;␊ |
598 | ␉utf_decodestr (ptr, ucsname, &ucslen, WIN_MAXLEN, OSLittleEndian);␊ |
599 | ␉ucslenhost = OSReadLittleInt16 (&ucslen,0);␊ |
600 | ␉ucsname[ucslenhost]=0;␊ |
601 | ␉*slash=c;␉␊ |
602 | ␉while ((dirp = getnextdirent (ih, vfatname, &st)))␊ |
603 | ␉{␊ |
604 | ␉␉if (checkname (ucsname, ucslenhost, dirp, vfatname))␊ |
605 | ␉␉{␊ |
606 | ␉␉␉for (;*ptr && *ptr!='/';ptr++);␊ |
607 | ␉␉␉if (!*ptr)␊ |
608 | ␉␉␉␉return dirp;␊ |
609 | ␉␉␉ptr++;␊ |
610 | ␉␉␉if (!*ptr)␊ |
611 | ␉␉␉␉return dirp;␊ |
612 | ␉␉␉for (slash=ptr;*slash && *slash!='/';slash++);␊ |
613 | ␉␉␉c=*slash;␊ |
614 | ␉␉␉*slash=0;␊ |
615 | ␉␉␉utf_decodestr (ptr, ucsname, &ucslen, WIN_MAXLEN, OSLittleEndian);␊ |
616 | ␉␉␉ucslenhost = OSReadLittleInt16 (&ucslen,0);␊ |
617 | ␉␉␉ucsname[ucslenhost]=0;␊ |
618 | ␉␉␉*slash=c;␊ |
619 | ␉␉␉if (!(dirp->deAttributes & ATTR_DIRECTORY))␊ |
620 | ␉␉␉␉return 0;␊ |
621 | ␉␉␉st.root16 = 0;␊ |
622 | ␉␉␉st.vfatchecksum = 0;␊ |
623 | ␉␉␉st.nument = 0;␊ |
624 | ␉␉␉st.cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);␊ |
625 | ␉␉␉if (msdosfatbits == 32)␊ |
626 | ␉␉␉␉st.cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;␊ |
627 | ␉␉␉st.vfatnumber = 0;␊ |
628 | ␉␉}␊ |
629 | ␉}␊ |
630 | ␉return 0;␊ |
631 | }␊ |
632 | ␊ |
633 | long MSDOSGetDirEntry(CICell ih, char * dirPath, long long * dirIndex,␊ |
634 | ␉␉␉␉␉ char ** name, long * flags, long * time,␊ |
635 | ␉␉␉␉␉ FinderInfo * finderInfo, long * infoValid)␊ |
636 | {␊ |
637 | ␉struct msdosdirstate *st;␊ |
638 | ␉struct direntry *dirp;␊ |
639 | ␉uint16_t␉␉vfatname[WIN_MAXLEN+2*WIN_CHARS];␊ |
640 | ␉if (MSDOSInitPartition (ih)<0)␊ |
641 | ␉␉return -1;␊ |
642 | ␉if (dirPath[0] == '/')␊ |
643 | ␉␉dirPath++;␊ |
644 | ␉st = (struct msdosdirstate *)(long) *dirIndex;␊ |
645 | ␉if (!st)␊ |
646 | ␉{␊ |
647 | ␉␉st=malloc (sizeof (*st));␊ |
648 | ␉␉if (dirPath[0])␊ |
649 | ␉␉{␊ |
650 | ␉␉␉uint8_t *buf=malloc(msdosclustersize);␊ |
651 | ␉␉␉dirp = getdirpfrompath (ih, dirPath, buf);␊ |
652 | ␉␉␉if (!dirp || !(dirp->deAttributes & ATTR_DIRECTORY))␊ |
653 | ␉␉␉{␊ |
654 | ␉␉␉␉free (buf);␊ |
655 | ␉␉␉␉free (st);␊ |
656 | ␉␉␉␉return -1;␊ |
657 | ␉␉␉}␊ |
658 | ␉␉␉st->buf = (struct direntry *)buf;␊ |
659 | ␉␉␉st->root16 = 0;␊ |
660 | ␉␉␉st->vfatchecksum = 0;␊ |
661 | ␉␉␉st->nument = 0;␊ |
662 | ␉␉␉st->cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);␊ |
663 | ␉␉␉st->vfatnumber = 0;␊ |
664 | ␉␉␉if (msdosfatbits == 32)␊ |
665 | ␉␉␉␉st->cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;␊ |
666 | ␉␉}␊ |
667 | ␉␉else␊ |
668 | ␉␉␉initRoot (st);␊ |
669 | ␉␉*dirIndex = (long long) (long) st;␊ |
670 | ␉}␊ |
671 | ␉while((dirp = getnextdirent (ih, vfatname, st))&& (dirp->deAttributes & ATTR_VOLUME));␊ |
672 | ␉if (!dirp)␊ |
673 | ␉{␊ |
674 | ␉␉free (st->buf);␊ |
675 | ␉␉free (st);␊ |
676 | ␉␉return -1;␊ |
677 | ␉}␊ |
678 | ␉if (vfatname[0])␊ |
679 | ␉{␊ |
680 | ␉␉int i;␊ |
681 | ␉␉for (i=0;vfatname[i];i++);␊ |
682 | ␉␉*name = malloc (256);␊ |
683 | ␉␉utf_encodestr(vfatname, i, (u_int8_t *)*name, 255, OSLittleEndian );␉␊ |
684 | ␉}␊ |
685 | ␉else␊ |
686 | ␉{␊ |
687 | ␉␉int i, j, k;␊ |
688 | ␉␉uint16_t tmp[13];␊ |
689 | ␉␉*name = malloc (26);␊ |
690 | ␉␉for (i=7;i>=0;i--)␊ |
691 | ␉␉␉if (dirp->deName[i]!=' ')␊ |
692 | ␉␉␉␉break;␊ |
693 | ␉␉j=i+1;␊ |
694 | ␉␉tmp[i+1]=0;␊ |
695 | ␉␉for(;i>=0;i--)␊ |
696 | ␉␉␉tmp[i]=(dirp->deName[i]>=128)?cp850[dirp->deName[i]-128][0]:tolower(dirp->deName[i]);␊ |
697 | ␉␉for (i=2;i>=0;i--)␊ |
698 | ␉␉␉if (dirp->deName[8+i]!=' ')␊ |
699 | ␉␉␉␉break;␊ |
700 | ␉␉␊ |
701 | ␉␉if (i>=0)␊ |
702 | ␉␉{␊ |
703 | ␉␉␉tmp[j++]='.';␊ |
704 | ␉␉␉tmp[j+i+1]=0;␊ |
705 | ␉␉␉k=j+i+1;␊ |
706 | ␉␉␉for(;i>=0;i--)␊ |
707 | ␉␉␉␉tmp[j+i]=(dirp->deName[i]>=128)?cp850[dirp->deName[i+8]-128][0]:tolower(dirp->deName[i+8]);␊ |
708 | ␉␉␉j=k;␊ |
709 | ␉␉}␊ |
710 | ␉␉␊ |
711 | ␉␉utf_encodestr(tmp, j, (uint8_t*)*name, 25, OSHostByteOrder() );␊ |
712 | ␉}␊ |
713 | ␉if (dirp->deAttributes & ATTR_DIRECTORY)␊ |
714 | ␉␉*flags = kFileTypeDirectory;␊ |
715 | ␉else␊ |
716 | ␉␉*flags = kFileTypeFlat;␊ |
717 | ␉␊ |
718 | ␉// Calculate a fake timestamp using modification date and time values.␊ |
719 | ␉*time = (dirp->deMDate & 0x7FFF) << 16 + dirp->deMTime;␊ |
720 | ␉␊ |
721 | ␉if (infoValid)␊ |
722 | ␉␉*infoValid = 1;␊ |
723 | ␊ |
724 | ␉return 0;␊ |
725 | }␊ |
726 | ␊ |
727 | long ␊ |
728 | MSDOSReadFile(CICell ih, char * filePath, void *base, uint64_t offset, uint64_t length)␊ |
729 | {␊ |
730 | ␉uint8_t *buf;␊ |
731 | ␉off_t cluster;␊ |
732 | ␉uint64_t size;␊ |
733 | ␉uint64_t nskip;␊ |
734 | ␉int toread, wastoread;␊ |
735 | ␉char *ptr = (char *)base;␊ |
736 | ␉struct direntry *dirp;␊ |
737 | ␉int i;␊ |
738 | char devStr[12];␊ |
739 | ␊ |
740 | ␉if (MSDOSInitPartition (ih)<0)␊ |
741 | ␉␉return -1;␊ |
742 | ␉if (filePath[0] == '/')␊ |
743 | ␉␉filePath++;␊ |
744 | ␉buf = malloc(msdosclustersize);␊ |
745 | ␉dirp = getdirpfrompath (ih, filePath, buf);␊ |
746 | ␉␊ |
747 | ␉if (!dirp || (dirp->deAttributes & ATTR_DIRECTORY))␊ |
748 | ␉{␊ |
749 | ␉␉free (buf);␊ |
750 | ␉␉return -1;␊ |
751 | ␉}␊ |
752 | ␉cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);␊ |
753 | ␉if (msdosfatbits == 32)␊ |
754 | ␉␉cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;␊ |
755 | ␉size = (uint32_t)OSReadLittleInt32 ((dirp->deFileSize),0);␊ |
756 | ␉if (size<=offset)␊ |
757 | ␉␉return -1;␊ |
758 | ␉nskip=offset/msdosclustersize;␊ |
759 | ␉for (i=0;i<nskip;i++)␊ |
760 | ␉␉msdosreadcluster (ih, 0, 0, &cluster);␊ |
761 | ␉msdosreadcluster (ih, buf, msdosclustersize, &cluster);␊ |
762 | ␉toread=length;␊ |
763 | ␉if (length==0 || length>size-offset)␊ |
764 | ␉␉toread=size-offset;␊ |
765 | ␉wastoread=toread;␊ |
766 | ␉bcopy (buf+(offset%msdosclustersize),ptr,min(msdosclustersize-(offset%msdosclustersize), toread));␊ |
767 | ␉ptr+=msdosclustersize-(offset%msdosclustersize);␊ |
768 | ␉toread-=msdosclustersize-(offset%msdosclustersize);␊ |
769 | ␉while (toread>0 && msdosreadcluster (ih, (uint8_t *)ptr, min(msdosclustersize,toread), &cluster))␊ |
770 | ␉{␊ |
771 | ␉␉ptr+=msdosclustersize;␊ |
772 | ␉␉toread-=msdosclustersize;␊ |
773 | ␉}␊ |
774 | ␉␊ |
775 | getDeviceDescription(ih, devStr);␊ |
776 | ␉verbose("Read FAT%d file: [%s/%s] %d bytes.\n",␊ |
777 | msdosfatbits, devStr, filePath, (uint32_t)( toread<0 ) ? wastoread : wastoread-toread);␊ |
778 | ␉free (buf);␊ |
779 | ␉if (toread<0)␊ |
780 | ␉␉return wastoread;␊ |
781 | ␉else␊ |
782 | ␉␉return wastoread-toread;␊ |
783 | }␊ |
784 | ␊ |
785 | long ␊ |
786 | MSDOSGetFileBlock(CICell ih, char *filePath, unsigned long long *firstBlock)␊ |
787 | {␊ |
788 | ␉uint8_t *buf;␊ |
789 | ␉off_t cluster;␊ |
790 | ␉struct direntry *dirp;␊ |
791 | ␉if (MSDOSInitPartition (ih)<0)␊ |
792 | ␉␉return -1;␊ |
793 | ␉if (filePath[0] == '/')␊ |
794 | ␉␉filePath++;␊ |
795 | ␉buf = malloc(msdosclustersize);␊ |
796 | ␉dirp = getdirpfrompath (ih, filePath, buf);␊ |
797 | ␉if (!dirp || (dirp->deAttributes & ATTR_DIRECTORY))␊ |
798 | ␉{␊ |
799 | ␉␉free (buf);␊ |
800 | ␉␉return -1;␊ |
801 | ␉}␊ |
802 | ␉cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);␊ |
803 | ␉if (msdosfatbits == 32)␊ |
804 | ␉␉cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;␊ |
805 | ␉␊ |
806 | ␉off_t clusn;␊ |
807 | ␉␊ |
808 | ␉switch (msdosfatbits) {␊ |
809 | ␉␉case 32:␊ |
810 | ␉␉␉if (cluster < CLUST_FIRST ||cluster >= CLUST_RSRVD32)␊ |
811 | ␉␉␉␉return -1;␊ |
812 | ␉␉␉clusn = cluster - CLUST_FIRST;␊ |
813 | ␉␉␉break;␊ |
814 | ␉␉case 16:␊ |
815 | ␉␉␉if (cluster < CLUST_FIRST ||cluster >= CLUST_RSRVD16)␊ |
816 | ␉␉␉␉return 0;␊ |
817 | ␉␉␉clusn = cluster - CLUST_FIRST;␊ |
818 | ␉␉␉break;␉␉␉␊ |
819 | ␉␉case 12:␊ |
820 | ␉␉␉␊ |
821 | ␉␉␉if (cluster < CLUST_FIRST ||cluster >= CLUST_RSRVD12)␊ |
822 | ␉␉␉␉return 0;␊ |
823 | ␉␉␉clusn = cluster - CLUST_FIRST;␊ |
824 | ␉␉␉break;␊ |
825 | ␉␉default:␊ |
826 | ␉␉␉return 0;␊ |
827 | ␉}␊ |
828 | ␉␊ |
829 | ␉*firstBlock = ((msdosressector +␊ |
830 | ␉␉␉␉ (msdosnfats * msdosfatsecs))*msdosbps + clusn * msdosclustersize)/512;␊ |
831 | ␉free (buf);␊ |
832 | ␉return 0;␊ |
833 | }␊ |
834 | ␊ |
835 | ␊ |
836 | long MSDOSLoadFile(CICell ih, char * filePath)␊ |
837 | {␊ |
838 | return MSDOSReadFile(ih, filePath, (void *)gFSLoadAddress, 0, 0);␊ |
839 | }␊ |
840 | ␊ |
841 | /* Fix up volume label. */␊ |
842 | static void␊ |
843 | fixLabel(uint8_t *label, char *str, long strMaxLen)␊ |
844 | {␊ |
845 | int␉␉␉i, len;␊ |
846 | ␉uint16_t␉labelucs[13];␊ |
847 | //unsigned char␉labelUTF8[LABEL_LENGTH*3];␊ |
848 | ␉␊ |
849 | /* Convert leading 0x05 to 0xE5 for multibyte languages like Japanese */␊ |
850 | if (label[0] == 0x05)␊ |
851 | label[0] = 0xE5;␊ |
852 | ␉␊ |
853 | /* Remove any trailing spaces */␊ |
854 | for (i=LABEL_LENGTH-1; i>=0; --i) {␊ |
855 | if (label[i] == ' ')␊ |
856 | label[i] = 0;␊ |
857 | else␊ |
858 | break;␊ |
859 | }␊ |
860 | ␉labelucs[i++]=0;␊ |
861 | ␉len=i;␊ |
862 | ␉for (;i>=0;--i)␊ |
863 | ␉␉labelucs[i]=label[i]>=128?cp850[label[i]-128][1]:(label[i]);␊ |
864 | ␉␊ |
865 | ␉utf_encodestr(labelucs, len, (uint8_t *)str, strMaxLen, OSHostByteOrder() );␊ |
866 | }␊ |
867 | ␊ |
868 | ␊ |
869 | void␊ |
870 | MSDOSGetDescription(CICell ih, char *str, long strMaxLen)␊ |
871 | {␊ |
872 | struct direntry *dirp;␊ |
873 | uint8_t␉ label[LABEL_LENGTH+1];␊ |
874 | ␉uint16_t␉␉vfatlabel[WIN_MAXLEN+2*WIN_CHARS];␊ |
875 | ␉struct msdosdirstate st;␊ |
876 | ␉int labelfound = 0;␊ |
877 | ␊ |
878 | ␉if (MSDOSInitPartition (ih)<0)␊ |
879 | ␉{␊ |
880 | ␉␉str[0]=0;␊ |
881 | ␉␉return;␊ |
882 | ␉}␊ |
883 | ␊ |
884 | ␉label[0] = '\0';␊ |
885 | ␉␊ |
886 | ␉initRoot (&st);␊ |
887 | ␉st.buf = malloc(msdosclustersize);␊ |
888 | ␉while ((dirp = getnextdirent (ih, vfatlabel, &st)))␊ |
889 | ␉␉if (dirp->deAttributes & ATTR_VOLUME) {␊ |
890 | ␉␉␉strncpy((char *)label, (char *)dirp->deName, LABEL_LENGTH);␊ |
891 | ␉␉␉labelfound = 1;␊ |
892 | ␉␉␉break;␊ |
893 | ␉␉}␊ |
894 | ␉␉␊ |
895 | ␉free(st.buf);␉␉␊ |
896 | ␊ |
897 | ␉if (vfatlabel[0] && labelfound)␊ |
898 | ␉{␊ |
899 | ␉␉int i;␊ |
900 | ␉␉for (i=0;vfatlabel[i];i++);␊ |
901 | ␉␉utf_encodestr(vfatlabel, i, (u_int8_t *)str, strMaxLen, OSLittleEndian );␉␉␊ |
902 | ␉}␉␊ |
903 | ␉else if (labelfound)␊ |
904 | ␉␉fixLabel(label, str, strMaxLen);␊ |
905 | ␉␊ |
906 | /* else look in the boot blocks */␊ |
907 | if (!labelfound || str[0] == '\0') {␊ |
908 | ␉␉char *buf = malloc (512);␊ |
909 | ␉␉union bootsector *bsp = (union bootsector *)buf;␊ |
910 | ␉␉Seek(ih, 0);␊ |
911 | ␉␉Read(ih, (long)buf, 512);␊ |
912 | if (msdosfatbits == 32) { /* It's FAT32 */␊ |
913 | strncpy((char *)label, (char *)((struct extboot *)bsp->bs710.bsExt)->exVolumeLabel, LABEL_LENGTH);␊ |
914 | }␊ |
915 | else if (msdosfatbits == 16) {␊ |
916 | strncpy((char *)label, (char *)((struct extboot *)bsp->bs50.bsExt)->exVolumeLabel, LABEL_LENGTH);␊ |
917 | }␊ |
918 | ␉␉free (buf);␊ |
919 | ␉␉fixLabel(label, str, strMaxLen);␊ |
920 | }␊ |
921 | ␊ |
922 | return;␊ |
923 | }␊ |
924 | ␊ |
925 | long ␊ |
926 | MSDOSGetUUID(CICell ih, char *uuidStr)␊ |
927 | {␊ |
928 | ␉char *buf = malloc (512);␊ |
929 | ␉union bootsector *bsp = (union bootsector *)buf;␊ |
930 | ␉␊ |
931 | ␉if (MSDOSInitPartition (ih)<0)␊ |
932 | ␉{␊ |
933 | ␉␉return -1;␊ |
934 | ␉}␊ |
935 | ␉bzero (uuidStr, 16);␊ |
936 | ␉Seek(ih, 0);␊ |
937 | ␉Read(ih, (long)buf, 512);␊ |
938 | ␉if (msdosfatbits == 32) { /* It's FAT32 */␊ |
939 | ␉␉memcpy(uuidStr+12, (char *)((struct extboot *)bsp->bs710.bsExt)->exVolumeID, 4);␊ |
940 | ␉}␊ |
941 | ␉else if (msdosfatbits == 16) {␊ |
942 | ␉␉memcpy(uuidStr+12, (char *)((struct extboot *)bsp->bs50.bsExt)->exVolumeID, 4);␊ |
943 | ␉}␊ |
944 | ␉free (buf);␊ |
945 | return 0;␊ |
946 | ␉␊ |
947 | }␊ |
948 | |