3 * Copyright (C) 2002, 2004 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS kernel
22 * FILE: services/fs/cdfs/misc.c
23 * PURPOSE: CDROM (ISO 9660) filesystem driver
24 * PROGRAMMER: Eric Kohl
28 /* INCLUDES *****************************************************************/
35 /* FUNCTIONS ****************************************************************/
38 CdfsSwapString(PWCHAR Out
,
42 PUCHAR t
= (PUCHAR
)Out
;
45 for (i
= 0; i
< Count
; i
+= 2)
49 if (t
[i
+1] == 0 && t
[i
] == ';')
52 if ((i
>2)&&(t
[i
-2] == '.'))
63 CdfsDateTimeToSystemTime(PFCB Fcb
,
64 PLARGE_INTEGER SystemTime
)
66 TIME_FIELDS TimeFields
;
67 LARGE_INTEGER LocalTime
;
69 TimeFields
.Milliseconds
= 0;
70 TimeFields
.Second
= Fcb
->Entry
.Second
;
71 TimeFields
.Minute
= Fcb
->Entry
.Minute
;
72 TimeFields
.Hour
= Fcb
->Entry
.Hour
;
74 TimeFields
.Day
= Fcb
->Entry
.Day
;
75 TimeFields
.Month
= Fcb
->Entry
.Month
;
76 TimeFields
.Year
= Fcb
->Entry
.Year
+ 1900;
78 RtlTimeFieldsToTime(&TimeFields
,
80 ExLocalTimeToSystemTime(&LocalTime
, SystemTime
);
85 CdfsFileFlagsToAttributes(PFCB Fcb
,
86 PULONG FileAttributes
)
88 /* FIXME: Fix attributes */
90 *FileAttributes
= // FILE_ATTRIBUTE_READONLY |
91 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_HIDDEN
) ? FILE_ATTRIBUTE_HIDDEN
: 0) |
92 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_DIRECTORY
) ? FILE_ATTRIBUTE_DIRECTORY
: 0) |
93 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_SYSTEM
) ? FILE_ATTRIBUTE_SYSTEM
: 0) |
94 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_READONLY
) ? FILE_ATTRIBUTE_READONLY
: 0);
98 CdfsIsNameLegalDOS8Dot3(IN UNICODE_STRING FileName
103 CHAR DbcsNameBuffer
[12];
105 /* 8dot3 filename is max 12 length */
106 if (FileName
.Length
/ sizeof(WCHAR
) > 12)
111 for (i
= 0; i
< FileName
.Length
/ sizeof(WCHAR
) ; i
++)
113 /* Don't allow spaces in FileName */
114 if (FileName
.Buffer
[i
] == L
' ')
118 /* If FileName is finishing with a dot, remove it */
119 if (FileName
.Buffer
[FileName
.Length
/ sizeof(WCHAR
) - 1] == '.')
121 FileName
.Length
-= sizeof(WCHAR
);
124 /* Finally, convert the string to call the FsRtl function */
125 DbcsName
.MaximumLength
= 12;
126 DbcsName
.Buffer
= DbcsNameBuffer
;
127 if (!NT_SUCCESS(RtlUnicodeStringToCountedOemString(&DbcsName
,
134 return FsRtlIsFatDbcsLegal(DbcsName
, FALSE
, FALSE
, FALSE
);
138 CdfsShortNameCacheGet
140 PLARGE_INTEGER StreamOffset
,
141 PUNICODE_STRING LongName
,
142 PUNICODE_STRING ShortName
)
145 PCDFS_SHORT_NAME ShortNameEntry
;
146 GENERATE_NAME_CONTEXT Context
= { 0 };
148 DPRINT("CdfsShortNameCacheGet(%I64u,%wZ)\n", StreamOffset
->QuadPart
, LongName
);
150 /* Get the name list resource */
151 ExAcquireResourceExclusiveLite(&DirectoryFcb
->NameListResource
, TRUE
);
153 /* Try to find the name in our cache */
154 for (Entry
= DirectoryFcb
->ShortNameList
.Flink
;
155 Entry
!= &DirectoryFcb
->ShortNameList
;
156 Entry
= Entry
->Flink
)
158 ShortNameEntry
= CONTAINING_RECORD(Entry
, CDFS_SHORT_NAME
, Entry
);
159 if (ShortNameEntry
->StreamOffset
.QuadPart
== StreamOffset
->QuadPart
)
163 (ShortName
->Buffer
, ShortNameEntry
->Name
.Buffer
,
164 ShortNameEntry
->Name
.Length
);
165 ShortName
->Length
= ShortNameEntry
->Name
.Length
;
166 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
167 DPRINT("Yield short name %wZ from cache\n", ShortName
);
173 if (!CdfsIsNameLegalDOS8Dot3(*LongName
))
175 RtlGenerate8dot3Name(LongName
, FALSE
, &Context
, ShortName
);
179 /* copy short name */
180 RtlUpcaseUnicodeString
186 DPRINT("Initial Guess %wZ\n", ShortName
);
188 /* Make it unique by scanning the cache and bumping */
189 /* Note that incrementing the ambiguous name is enough, since we add new
190 * entries at the tail. We'll scan over all collisions. */
191 /* XXX could perform better. */
192 for (Entry
= DirectoryFcb
->ShortNameList
.Flink
;
193 Entry
!= &DirectoryFcb
->ShortNameList
;
194 Entry
= Entry
->Flink
)
196 ShortNameEntry
= CONTAINING_RECORD(Entry
, CDFS_SHORT_NAME
, Entry
);
197 if (RtlCompareUnicodeString
199 &ShortNameEntry
->Name
,
200 TRUE
) == 0) /* Match */
202 RtlGenerate8dot3Name(LongName
, FALSE
, &Context
, ShortName
);
203 DPRINT("Collide; try %wZ\n", ShortName
);
207 /* We've scanned over all entries and now have a unique one. Cache it. */
208 ShortNameEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(CDFS_SHORT_NAME
), TAG_FCB
);
211 /* We couldn't cache it, but we can return it. We run the risk of
212 * generating a non-unique name later. */
213 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
214 DPRINT1("Couldn't cache potentially clashing 8.3 name %wZ\n", ShortName
);
218 ShortNameEntry
->StreamOffset
= *StreamOffset
;
219 ShortNameEntry
->Name
.Buffer
= ShortNameEntry
->NameBuffer
;
220 ShortNameEntry
->Name
.Length
= ShortName
->Length
;
221 ShortNameEntry
->Name
.MaximumLength
= sizeof(ShortNameEntry
->NameBuffer
);
223 (ShortNameEntry
->NameBuffer
,
226 InsertTailList(&DirectoryFcb
->ShortNameList
, &ShortNameEntry
->Entry
);
227 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
229 DPRINT("Returning short name %wZ for long name %wZ\n", ShortName
, LongName
);