Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/msdos.c

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_LENGTH11
57#define MAX_DOS_BLOCKSIZE2048
58
59#defineCLUST_FIRST2/* reserved cluster range */
60#defineCLUST_RSRVD320x0ffffff8/* reserved cluster range */
61#defineCLUST_RSRVD160xfff8/* reserved cluster range */
62#defineCLUST_RSRVD120xff8/* reserved cluster range */
63
64
65#define false 0
66#define true 1
67
68static int msdosressector=0;
69static int msdosnfats = 0;
70static int msdosfatsecs = 0;
71static int msdosbps = 0;
72static int msdosclustersize = 0;
73static int msdosrootDirSectors = 0;
74static CICell msdoscurrent = 0;
75static int msdosrootcluster = 0;
76static int msdosfatbits = 0;
77
78#if UNUSED
79/*
80 * Check a volume label.
81 */
82static int
83oklabel(const char *src)
84{
85 int c, i;
86
87 for (i = 0, c = 0; i <= 11; i++) {
88 c = (u_char)*src++;
89 if (c < ' ' + !i || strchr("\"*+,./:;<=>?[\\]|", c))
90 break;
91 }
92 return i && !c;
93}
94#endif /* UNUSED */
95
96void MSDOSFree(CICell ih)
97{
98if(msdoscurrent == ih)
99 msdoscurrent = 0;
100 free(ih);
101}
102
103int
104MSDOSProbe(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_tspc;
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 FALSE;
122
123/* Check to make sure valid sectors per cluster */
124 spc = b33->bpbSecPerClust;
125 if ((spc == 0 ) || (spc & (spc - 1)))
126 return FALSE;
127
128if (OSSwapLittleToHostInt16(b50->bpbRootDirEnts) == 0) { /* It's FAT32 */
129if (!memcmp(((struct extboot *)bsp->bs710.bsExt)->exFileSysType, "FAT32 ", 8))
130return 32;
131}
132else if (((struct extboot *)bsp->bs50.bsExt)->exBootSignature == EXBOOTSIG) {
133if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT16 ", 8))
134return 16;
135if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT12 ", 8))
136return 12;
137}
138
139return FALSE;
140}
141
142
143long
144MSDOSInitPartition (CICell ih)
145{
146 union bootsector *bsp;
147 struct bpb33 *b33;
148 struct bpb50 *b50;
149 struct bpb710 *b710;
150 u_int8_tspc;
151 char *buf;
152
153if (msdoscurrent == ih)
154{
155CacheInit(ih, msdosclustersize);
156return 0;
157}
158
159buf=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{
179free (buf);
180return -1;
181}
182
183 /* Check to make sure valid sectors per cluster */
184 spc = b33->bpbSecPerClust;
185 if ((spc == 0 ) || (spc & (spc - 1)))
186return -1;
187
188if (OSSwapLittleToHostInt16(b50->bpbRootDirEnts) == 0) { /* It's FAT32 */
189if (memcmp(((struct extboot *)bsp->bs710.bsExt)->exFileSysType, "FAT32 ", 8))
190{
191free (buf);
192return -1;
193}
194msdosressector = OSSwapLittleToHostInt16(b710->bpbResSectors);
195msdosnfats = b710->bpbFATs;
196msdosfatsecs = OSSwapLittleToHostInt16(b710->bpbBigFATsecs);
197msdosrootcluster = OSSwapLittleToHostInt32(b710->bpbRootClust);
198msdosrootDirSectors = 0;
199msdosfatbits = 32;
200}
201else if (((struct extboot *)bsp->bs50.bsExt)->exBootSignature == EXBOOTSIG) {
202if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT16 ", 8))
203msdosfatbits = 16;
204else if (!memcmp((char *)((struct extboot *)bsp->bs50.bsExt)->exFileSysType, "FAT12 ", 8))
205msdosfatbits = 12;
206else
207{
208free (buf);
209return -1;
210}
211
212msdosressector = OSSwapLittleToHostInt16(b33->bpbResSectors);
213msdosnfats = b33->bpbFATs;
214msdosfatsecs = OSSwapLittleToHostInt16(b33->bpbFATsecs);
215msdosrootcluster = 0;
216msdosrootDirSectors = ((OSSwapLittleToHostInt16(b50->bpbRootDirEnts) * sizeof(struct direntry)) +
217 (msdosbps-1)) / msdosbps;
218} else {
219free (buf);
220return -1;
221}
222
223msdosclustersize = msdosbps * spc;
224
225msdoscurrent = ih;
226CacheInit(ih, msdosclustersize);
227free (buf);
228return 0;
229}
230
231static int
232msdosreadcluster (CICell ih, uint8_t *buf, int size, off_t *cluster)
233{
234 off_t readOffset;
235char tmpbuf[8];
236off_t clusn;
237
238switch (msdosfatbits) {
239case 32:
240if (*cluster < CLUST_FIRST ||*cluster >= CLUST_RSRVD32)
241return 0;
242clusn = *cluster - CLUST_FIRST;
243break;
244case 16:
245if (*cluster < CLUST_FIRST ||*cluster >= CLUST_RSRVD16)
246return 0;
247clusn = *cluster - CLUST_FIRST;
248break;
249case 12:
250if (*cluster < CLUST_FIRST ||*cluster >= CLUST_RSRVD12)
251return 0;
252clusn = *cluster - CLUST_FIRST;
253break;
254default:
255return 0;
256}
257
258/* Find sector where clusters start */
259readOffset = (msdosressector +
260 (msdosnfats * msdosfatsecs)+msdosrootDirSectors)*msdosbps;
261/* Find sector where "cluster" starts */
262readOffset += clusn * msdosclustersize;
263
264/* Read in "cluster" */
265if (buf)
266{
267Seek(ih, readOffset);
268Read(ih, (long)buf, size);
269}
270
271/* Find first sector of FAT */
272readOffset = msdosressector*msdosbps;
273/* Find sector containing "cluster" entry in FAT */
274readOffset += ((uint64_t)*cluster * (uint64_t)msdosfatbits)/8;
275
276/* Read one sector of the FAT */
277Seek(ih, readOffset);
278Read(ih, (long)tmpbuf, 4);
279
280switch (msdosfatbits) {
281case 32:
282*cluster = OSReadLittleInt32(tmpbuf, 0);
283*cluster &= 0x0FFFFFFF;// ignore reserved upper bits
284return 1;
285case 16:
286*cluster = OSReadLittleInt16(tmpbuf, 0);
287return 1;
288case 12:
289*cluster = OSReadLittleInt16(tmpbuf, 0)>>(((uint64_t)*cluster * (uint64_t)msdosfatbits)%8);
290*cluster &= 0xfff;
291return 1;
292default:
293return 0;
294}
295}
296
297struct msdosdirstate
298{
299struct direntry *buf;
300uint8_t vfatchecksum;
301int root16;
302off_t cluster;
303int nument;
304int vfatnumber;
305};
306
307static struct direntry *
308getnextdirent (CICell ih, uint16_t *longname, struct msdosdirstate *st)
309{
310struct direntry *dirp;
311while (1)
312{
313if (st->root16)
314{
315if (st->cluster >= msdosrootDirSectors && st->nument == 0)
316return 0;
317if (st->nument == 0)
318{
319Seek(ih, (msdosressector +
320 (msdosnfats * msdosfatsecs)+st->cluster)*msdosbps);
321Read(ih, (long)st->buf, msdosbps);
322st->cluster++;
323}
324} else if (st->nument == 0 && !msdosreadcluster (ih, (uint8_t *)st->buf, msdosclustersize, &(st->cluster)))
325return 0;
326
327dirp=st->buf+st->nument;
328
329if (dirp->deName[0] == SLOT_EMPTY)
330return 0;
331else if (dirp->deName[0] == SLOT_DELETED)
332st->vfatnumber = 0;
333else if (dirp->deAttributes == ATTR_WIN95)
334{
335struct winentry *wdirp = (struct winentry *)dirp;
336int num;
337if (wdirp->weCnt & 0x80)
338continue;
339num=(wdirp->weCnt&0x3f);
340if (WIN_CHARS * num > WIN_MAXLEN)
341continue;
342if (st->vfatchecksum!=wdirp->weChksum)
343{
344st->vfatnumber = 0;
345st->vfatchecksum = wdirp->weChksum;
346}
347if (st->vfatnumber < num)
348st->vfatnumber = num;
349bcopy (&(wdirp->wePart1),longname+WIN_CHARS*(num-1),sizeof (wdirp->wePart1));
350bcopy (&(wdirp->wePart2),longname+WIN_CHARS*(num-1)+5,sizeof (wdirp->wePart2));
351bcopy (&(wdirp->wePart3),longname+WIN_CHARS*(num-1)+11,sizeof (wdirp->wePart3));
352} else {
353uint8_t labelchecksum;
354int i;
355longname[st->vfatnumber*WIN_CHARS]=0;
356
357labelchecksum=0;
358for(i=0;i<LABEL_LENGTH;i++)
359labelchecksum=(labelchecksum>>1)+(labelchecksum<<7)+dirp->deName[i];
360if (!(labelchecksum==st->vfatchecksum && st->vfatnumber))
361longname[0]=0;
362st->vfatnumber = 0;
363st->vfatchecksum = 0;
364st->vfatnumber = 0;
365st->nument++;
366if ((!st->root16 &&st->nument * sizeof (struct direntry)>=msdosclustersize)
367|| (st->root16 &&st->nument * sizeof (struct direntry)>=msdosbps))
368st->nument = 0;
369return dirp;
370}
371st->nument++;
372if ((!st->root16 &&st->nument * sizeof (struct direntry)>=msdosclustersize)
373|| (st->root16 &&st->nument * sizeof (struct direntry)>=msdosbps))
374st->nument = 0;
375}
376}
377
378static void
379initRoot (struct msdosdirstate *st)
380{
381if (msdosrootDirSectors) {/* FAT12 or FAT16 */
382st->root16 = 1;
383st->vfatchecksum = 0;
384st->nument = 0;
385st->cluster = 0;
386st->vfatnumber = 0;
387} else {/* FAT32 */
388st->root16 = 0;
389st->vfatchecksum = 0;
390st->nument = 0;
391st->cluster = msdosrootcluster;
392st->vfatnumber = 0;
393}
394}
395
396/* First comes lowercase, then uppercase*/
397static uint16_t cp850[128][2]=
398{
399{0x00E7,0x00C7},
400{0x00FC,0x00DC},
401{0x00E9,0x00C9},
402{0x00E2,0x00C2},
403{0x00E4,0x00C4},
404{0x00E0,0x00C0},
405{0x00E5,0x00C5},
406{0x00E7,0x00C7},
407{0x00EA,0x00CA},
408{0x00EB,0x00CB},
409{0x00E8,0x00C8},
410{0x00EF,0x00CF},
411{0x00EE,0x00CE},
412{0x00EC,0x00CC},
413{0x00E4,0x00C4},
414{0x00E5,0x00C5},
415{0x00E9,0x00C9},
416{0x00E6,0x00C6},
417{0x00E6,0x00C6},
418{0x00F4,0x00D4},
419{0x00F6,0x00D6},
420{0x00F2,0x00D2},
421{0x00FB,0x00DB},
422{0x00F9,0x00D9},
423{0x00FF,0x0178},
424{0x00F6,0x00D6},
425{0x00FC,0x00DC},
426{0x00F8,0x00D8},
427{0x00A3,0x00A3},
428{0x00F8,0x00D8},
429{0x00D7,0x00D7},
430{0x0192,0x0191},
431{0x00E1,0x00C1},
432{0x00ED,0x00CD},
433{0x00F3,0x00D3},
434{0x00FA,0x00DA},
435{0x00F1,0x00D1},
436{0x00F1,0x00D1},
437{0x00AA,0x00AA},
438{0x00BA,0x00BA},
439{0x00BF,0x00BF},
440{0x00AE,0x00AE},
441{0x00AC,0x00AC},
442{0x00BD,0x00BD},
443{0x00BC,0x00BC},
444{0x00A1,0x00A1},
445{0x00AB,0x00AB},
446{0x00BB,0x00BB},
447{0x2591,0x2591},
448{0x2592,0x2592},
449{0x2593,0x2593},
450{0x2502,0x2502},
451{0x2524,0x2524},
452{0x00E1,0x00C1},
453{0x00E2,0x00C2},
454{0x00E0,0x00C0},
455{0x00A9,0x00A9},
456{0x2563,0x2563},
457{0x2551,0x2551},
458{0x2557,0x2557},
459{0x255D,0x255D},
460{0x00A2,0x00A2},
461{0x00A5,0x00A5},
462{0x2510,0x2510},
463{0x2514,0x2514},
464{0x2534,0x2534},
465{0x252C,0x252C},
466{0x251C,0x251C},
467{0x2500,0x2500},
468{0x253C,0x253C},
469{0x00E3,0x00C3},
470{0x00E3,0x00C3},
471{0x255A,0x255A},
472{0x2554,0x2554},
473{0x2569,0x2569},
474{0x2566,0x2566},
475{0x2560,0x2560},
476{0x2550,0x2550},
477{0x256C,0x256C},
478{0x00A4,0x00A4},
479{0x00F0,0x00D0},
480{0x00F0,0x00D0},
481{0x00EA,0x00CA},
482{0x00EB,0x00CB},
483{0x00E8,0x00C8},
484{0x0131,0x0049},
485{0x00ED,0x00CD},
486{0x00EE,0x00CE},
487{0x00EF,0x00CF},
488{0x2518,0x2518},
489{0x250C,0x250C},
490{0x2588,0x2588},
491{0x2584,0x2584},
492{0x00A6,0x00A6},
493{0x00EC,0x00CC},
494{0x2580,0x2580},
495{0x00F3,0x00D3},
496{0x00DF,0x00DF},
497{0x00F4,0x00D4},
498{0x00F2,0x00D2},
499{0x00F5,0x00D5},
500{0x00F5,0x00D5},
501{0x00B5,0x00B5},
502{0x00FE,0x00DE},
503{0x00FE,0x00DE},
504{0x00FA,0x00DA},
505{0x00FB,0x00DB},
506{0x00F9,0x00D9},
507{0x00FD,0x00DD},
508{0x00FD,0x00DD},
509{0x00AF,0x00AF},
510{0x00B4,0x00B4},
511{0x00AD,0x00AD},
512{0x00B1,0x00B1},
513{0x2017,0x2017},
514{0x00BE,0x00BE},
515{0x00B6,0x00B6},
516{0x00A7,0x00A7},
517{0x00F7,0x00F7},
518{0x00B8,0x00B8},
519{0x00B0,0x00B0},
520{0x00A8,0x00A8},
521{0x00B7,0x00B7},
522{0x00B9,0x00B9},
523{0x00B3,0x00B3},
524{0x00B2,0x00B2},
525{0x25A0,0x25A0},
526{0x00A0,0x00A0}
527};
528
529static int
530checkname (uint16_t *ucsname, int ucslen, struct direntry *dirp, uint16_t *vfatname)
531{
532uint16_t tmp[15];
533if (vfatname[0])
534{
535int i;
536for (i=0;vfatname[i];i++);
537return !FastUnicodeCompare (ucsname, ucslen, vfatname, i, OSLittleEndian);
538}
539else
540{
541int i, j, k;
542for (i=7;i>=0;i--)
543if (dirp->deName[i]!=' ')
544break;
545j=i+1;
546tmp[i+1]=0;
547for(;i>=0;i--)
548tmp[i]=SWAP_LE16((dirp->deName[i]>=128)?cp850[dirp->deName[i]-128][0]:tolower(dirp->deName[i]));
549for (i=2;i>=0;i--)
550if (dirp->deName[8+i]!=' ')
551break;
552if (i>=0)
553{
554tmp[j++]='.';
555tmp[j+i+1]=0;
556k=j+i+1;
557for(;i>=0;i--)
558tmp[j+i]=SWAP_LE16((dirp->deName[i+8]>=128)?cp850[dirp->deName[i+8]-128][0]:tolower(dirp->deName[i+8]));
559j=k;
560}
561return !FastUnicodeCompare (ucsname, ucslen, tmp, j, OSLittleEndian);
562}
563
564}
565
566static struct direntry *
567getdirpfrompath (CICell ih, char *dirspec, uint8_t *buf)
568{
569struct msdosdirstate st;
570struct direntry *dirp;
571uint8_t * ptr;
572uint8_t *slash;
573char c;
574uint16_tvfatname[WIN_MAXLEN+2*WIN_CHARS];
575uint16_t ucsname[WIN_MAXLEN+1];
576uint16_t ucslen;
577int ucslenhost;
578initRoot (&st);
579st.buf = (struct direntry *)buf;
580ptr=(uint8_t*)dirspec;
581for (slash=ptr;*slash && *slash!='/';slash++);
582c=*slash;
583*slash=0;
584utf_decodestr (ptr, ucsname, &ucslen, WIN_MAXLEN, OSLittleEndian);
585ucslenhost = OSReadLittleInt16 (&ucslen,0);
586ucsname[ucslenhost]=0;
587*slash=c;
588while ((dirp = getnextdirent (ih, vfatname, &st)))
589{
590if (checkname (ucsname, ucslenhost, dirp, vfatname))
591{
592for (;*ptr && *ptr!='/';ptr++);
593if (!*ptr)
594return dirp;
595ptr++;
596if (!*ptr)
597return dirp;
598for (slash=ptr;*slash && *slash!='/';slash++);
599c=*slash;
600*slash=0;
601utf_decodestr (ptr, ucsname, &ucslen, WIN_MAXLEN, OSLittleEndian);
602ucslenhost = OSReadLittleInt16 (&ucslen,0);
603ucsname[ucslenhost]=0;
604*slash=c;
605if (!(dirp->deAttributes & ATTR_DIRECTORY))
606return 0;
607st.root16 = 0;
608st.vfatchecksum = 0;
609st.nument = 0;
610st.cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);
611if (msdosfatbits == 32)
612st.cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;
613st.vfatnumber = 0;
614}
615}
616return 0;
617}
618
619long MSDOSGetDirEntry(CICell ih, char * dirPath, long * dirIndex,
620 char ** name, long * flags, long * time,
621 FinderInfo * finderInfo, long * infoValid)
622{
623struct msdosdirstate *st;
624struct direntry *dirp;
625uint16_tvfatname[WIN_MAXLEN+2*WIN_CHARS];
626if (MSDOSInitPartition (ih)<0)
627return -1;
628if (dirPath[0] == '/')
629dirPath++;
630st = (struct msdosdirstate *)*dirIndex;
631if (!st)
632{
633st=malloc (sizeof (*st));
634if (dirPath[0])
635{
636uint8_t *buf=malloc(msdosclustersize);
637dirp = getdirpfrompath (ih, dirPath, buf);
638if (!dirp || !(dirp->deAttributes & ATTR_DIRECTORY))
639{
640free (buf);
641free (st);
642return -1;
643}
644st->buf = (struct direntry *)buf;
645st->root16 = 0;
646st->vfatchecksum = 0;
647st->nument = 0;
648st->cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);
649st->vfatnumber = 0;
650if (msdosfatbits == 32)
651st->cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;
652}
653else
654initRoot (st);
655*dirIndex = (long)st;
656}
657while((dirp = getnextdirent (ih, vfatname, st))&& (dirp->deAttributes & ATTR_VOLUME));
658if (!dirp)
659{
660free (st->buf);
661free (st);
662return -1;
663}
664if (vfatname[0])
665{
666int i;
667for (i=0;vfatname[i];i++);
668*name = malloc (256);
669utf_encodestr(vfatname, i, (u_int8_t *)*name, 255, OSLittleEndian );
670}
671else
672{
673int i, j, k;
674uint16_t tmp[13];
675*name = malloc (26);
676for (i=7;i>=0;i--)
677if (dirp->deName[i]!=' ')
678break;
679j=i+1;
680tmp[i+1]=0;
681for(;i>=0;i--)
682tmp[i]=(dirp->deName[i]>=128)?cp850[dirp->deName[i]-128][0]:tolower(dirp->deName[i]);
683for (i=2;i>=0;i--)
684if (dirp->deName[8+i]!=' ')
685break;
686
687if (i>=0)
688{
689tmp[j++]='.';
690tmp[j+i+1]=0;
691k=j+i+1;
692for(;i>=0;i--)
693tmp[j+i]=(dirp->deName[i]>=128)?cp850[dirp->deName[i+8]-128][0]:tolower(dirp->deName[i+8]);
694j=k;
695}
696
697utf_encodestr(tmp, j, (uint8_t*)*name, 25, OSHostByteOrder() );
698}
699if (dirp->deAttributes & ATTR_DIRECTORY)
700*flags = kFileTypeDirectory;
701else
702*flags = kFileTypeFlat;
703
704// Calculate a fake timestamp using modification date and time values.
705*time = (dirp->deMDate & 0x7FFF) << 16 + dirp->deMTime;
706
707if (infoValid)
708*infoValid = 1;
709
710return 0;
711}
712
713long
714MSDOSReadFile(CICell ih, char * filePath, void *base, uint64_t offset, uint64_t length)
715{
716uint8_t *buf;
717off_t cluster;
718uint64_t size;
719uint64_t nskip;
720int toread, wastoread;
721char *ptr = (char *)base;
722struct direntry *dirp;
723int i;
724if (MSDOSInitPartition (ih)<0)
725return -1;
726if (filePath[0] == '/')
727filePath++;
728buf = malloc(msdosclustersize);
729dirp = getdirpfrompath (ih, filePath, buf);
730
731if (!dirp || (dirp->deAttributes & ATTR_DIRECTORY))
732{
733free (buf);
734return -1;
735}
736cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);
737if (msdosfatbits == 32)
738cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;
739size = (uint32_t)OSReadLittleInt32 ((dirp->deFileSize),0);
740if (size<=offset)
741return -1;
742nskip=offset/msdosclustersize;
743for (i=0;i<nskip;i++)
744msdosreadcluster (ih, 0, 0, &cluster);
745msdosreadcluster (ih, buf, msdosclustersize, &cluster);
746toread=length;
747if (length==0 || length>size-offset)
748toread=size-offset;
749wastoread=toread;
750bcopy (buf+(offset%msdosclustersize),ptr,min(msdosclustersize-(offset%msdosclustersize), toread));
751ptr+=msdosclustersize-(offset%msdosclustersize);
752toread-=msdosclustersize-(offset%msdosclustersize);
753while (toread>0 && msdosreadcluster (ih, (uint8_t *)ptr, min(msdosclustersize,toread), &cluster))
754{
755ptr+=msdosclustersize;
756toread-=msdosclustersize;
757}
758verbose("Loaded FAT%d file: [%s] %d bytes from %x.\n",
759 msdosfatbits, filePath, (uint32_t)( toread<0 ) ? wastoread : wastoread-toread, ih);
760free (buf);
761if (toread<0)
762return wastoread;
763else
764return wastoread-toread;
765}
766
767long
768MSDOSGetFileBlock(CICell ih, char *filePath, unsigned long long *firstBlock)
769{
770uint8_t *buf;
771off_t cluster;
772struct direntry *dirp;
773if (MSDOSInitPartition (ih)<0)
774return -1;
775if (filePath[0] == '/')
776filePath++;
777buf = malloc(msdosclustersize);
778dirp = getdirpfrompath (ih, filePath, buf);
779if (!dirp || (dirp->deAttributes & ATTR_DIRECTORY))
780{
781free (buf);
782return -1;
783}
784cluster = OSReadLittleInt16 ((dirp->deStartCluster),0);
785if (msdosfatbits == 32)
786cluster |= ((uint32_t)OSReadLittleInt16 ((dirp->deHighClust),0)) <<16;
787
788off_t clusn;
789
790switch (msdosfatbits) {
791case 32:
792if (cluster < CLUST_FIRST ||cluster >= CLUST_RSRVD32)
793return -1;
794clusn = cluster - CLUST_FIRST;
795break;
796case 16:
797if (cluster < CLUST_FIRST ||cluster >= CLUST_RSRVD16)
798return 0;
799clusn = cluster - CLUST_FIRST;
800break;
801case 12:
802
803if (cluster < CLUST_FIRST ||cluster >= CLUST_RSRVD12)
804return 0;
805clusn = cluster - CLUST_FIRST;
806break;
807default:
808return 0;
809}
810
811*firstBlock = ((msdosressector +
812 (msdosnfats * msdosfatsecs))*msdosbps + clusn * msdosclustersize)/512;
813free (buf);
814return 0;
815}
816
817
818long MSDOSLoadFile(CICell ih, char * filePath)
819{
820 return MSDOSReadFile(ih, filePath, (void *)gFSLoadAddress, 0, 0);
821}
822
823/* Fix up volume label. */
824static void
825fixLabel(uint8_t *label, char *str, long strMaxLen)
826{
827 inti, len;
828uint16_tlabelucs[13];
829 //unsigned charlabelUTF8[LABEL_LENGTH*3];
830
831 /* Convert leading 0x05 to 0xE5 for multibyte languages like Japanese */
832 if (label[0] == 0x05)
833 label[0] = 0xE5;
834
835 /* Remove any trailing spaces */
836 for (i=LABEL_LENGTH-1; i>=0; --i) {
837 if (label[i] == ' ')
838 label[i] = 0;
839 else
840 break;
841 }
842labelucs[i++]=0;
843len=i;
844for (;i>=0;--i)
845labelucs[i]=label[i]>=128?cp850[label[i]-128][1]:(label[i]);
846
847utf_encodestr(labelucs, len, (uint8_t *)str, strMaxLen, OSHostByteOrder() );
848}
849
850
851void
852MSDOSGetDescription(CICell ih, char *str, long strMaxLen)
853{
854 struct direntry *dirp;
855 uint8_t label[LABEL_LENGTH+1];
856uint16_tvfatlabel[WIN_MAXLEN+2*WIN_CHARS];
857struct msdosdirstate st;
858int labelfound = 0;
859
860if (MSDOSInitPartition (ih)<0)
861{
862str[0]=0;
863return;
864}
865
866label[0] = '\0';
867
868initRoot (&st);
869st.buf = malloc(msdosclustersize);
870while ((dirp = getnextdirent (ih, vfatlabel, &st)))
871if (dirp->deAttributes & ATTR_VOLUME) {
872strncpy((char *)label, (char *)dirp->deName, LABEL_LENGTH);
873labelfound = 1;
874break;
875}
876
877free(st.buf);
878
879if (vfatlabel[0] && labelfound)
880{
881int i;
882for (i=0;vfatlabel[i];i++);
883utf_encodestr(vfatlabel, i, (u_int8_t *)str, strMaxLen, OSLittleEndian );
884}
885else if (labelfound)
886fixLabel(label, str, strMaxLen);
887
888 /* else look in the boot blocks */
889 if (!labelfound || str[0] == '\0') {
890char *buf = malloc (512);
891union bootsector *bsp = (union bootsector *)buf;
892Seek(ih, 0);
893Read(ih, (long)buf, 512);
894 if (msdosfatbits == 32) { /* It's FAT32 */
895 strncpy((char *)label, (char *)((struct extboot *)bsp->bs710.bsExt)->exVolumeLabel, LABEL_LENGTH);
896 }
897 else if (msdosfatbits == 16) {
898 strncpy((char *)label, (char *)((struct extboot *)bsp->bs50.bsExt)->exVolumeLabel, LABEL_LENGTH);
899 }
900free (buf);
901fixLabel(label, str, strMaxLen);
902 }
903
904 return;
905}
906
907long
908MSDOSGetUUID(CICell ih, char *uuidStr)
909{
910char *buf = malloc (512);
911union bootsector *bsp = (union bootsector *)buf;
912
913if (MSDOSInitPartition (ih)<0)
914{
915return -1;
916}
917bzero (uuidStr, 16);
918Seek(ih, 0);
919Read(ih, (long)buf, 512);
920if (msdosfatbits == 32) { /* It's FAT32 */
921memcpy(uuidStr+12, (char *)((struct extboot *)bsp->bs710.bsExt)->exVolumeID, 4);
922}
923else if (msdosfatbits == 16) {
924memcpy(uuidStr+12, (char *)((struct extboot *)bsp->bs50.bsExt)->exVolumeID, 4);
925}
926free (buf);
927 return 0;
928
929}
930

Archive Download this file

Revision: 1