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.
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS kernel
23 * FILE: services/fs/cdfs/misc.c
24 * PURPOSE: CDROM (ISO 9660) filesystem driver
25 * PROGRAMMER: Eric Kohl
29 /* INCLUDES *****************************************************************/
36 /* FUNCTIONS ****************************************************************/
39 CdfsSwapString(PWCHAR Out
,
43 PUCHAR t
= (PUCHAR
)Out
;
46 for (i
= 0; i
< Count
; i
+= 2)
50 if (t
[i
+1] == 0 && t
[i
] == ';')
53 if ((i
>2)&&(t
[i
-2] == '.'))
64 CdfsDateTimeToSystemTime(PFCB Fcb
,
65 PLARGE_INTEGER SystemTime
)
67 TIME_FIELDS TimeFields
;
68 LARGE_INTEGER LocalTime
;
70 TimeFields
.Milliseconds
= 0;
71 TimeFields
.Second
= Fcb
->Entry
.Second
;
72 TimeFields
.Minute
= Fcb
->Entry
.Minute
;
73 TimeFields
.Hour
= Fcb
->Entry
.Hour
;
75 TimeFields
.Day
= Fcb
->Entry
.Day
;
76 TimeFields
.Month
= Fcb
->Entry
.Month
;
77 TimeFields
.Year
= Fcb
->Entry
.Year
+ 1900;
79 RtlTimeFieldsToTime(&TimeFields
,
81 ExLocalTimeToSystemTime(&LocalTime
, SystemTime
);
86 CdfsFileFlagsToAttributes(PFCB Fcb
,
87 PULONG FileAttributes
)
89 /* FIXME: Fix attributes */
91 *FileAttributes
= // FILE_ATTRIBUTE_READONLY |
92 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_HIDDEN
) ? FILE_ATTRIBUTE_HIDDEN
: 0) |
93 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_DIRECTORY
) ? FILE_ATTRIBUTE_DIRECTORY
: 0) |
94 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_SYSTEM
) ? FILE_ATTRIBUTE_SYSTEM
: 0) |
95 ((Fcb
->Entry
.FileFlags
& FILE_FLAG_READONLY
) ? FILE_ATTRIBUTE_READONLY
: 0);
99 CdfsIsNameLegalDOS8Dot3(IN UNICODE_STRING FileName
104 CHAR DbcsNameBuffer
[12];
106 /* 8dot3 filename is max 12 length */
107 if (FileName
.Length
/ sizeof(WCHAR
) > 12)
112 for (i
= 0; i
< FileName
.Length
/ sizeof(WCHAR
) ; i
++)
114 /* Don't allow spaces in FileName */
115 if (FileName
.Buffer
[i
] == L
' ')
119 /* If FileName is finishing with a dot, remove it */
120 if (FileName
.Buffer
[FileName
.Length
/ sizeof(WCHAR
) - 1] == '.')
122 FileName
.Length
-= sizeof(WCHAR
);
125 /* Finally, convert the string to call the FsRtl function */
126 DbcsName
.MaximumLength
= 12;
127 DbcsName
.Buffer
= DbcsNameBuffer
;
128 if (!NT_SUCCESS(RtlUnicodeStringToCountedOemString(&DbcsName
,
135 return FsRtlIsFatDbcsLegal(DbcsName
, FALSE
, FALSE
, FALSE
);
139 CdfsShortNameCacheGet
141 PLARGE_INTEGER StreamOffset
,
142 PUNICODE_STRING LongName
,
143 PUNICODE_STRING ShortName
)
146 PCDFS_SHORT_NAME ShortNameEntry
;
147 GENERATE_NAME_CONTEXT Context
= { 0 };
149 DPRINT("CdfsShortNameCacheGet(%I64u,%wZ)\n", StreamOffset
->QuadPart
, LongName
);
151 /* Get the name list resource */
152 ExAcquireResourceExclusiveLite(&DirectoryFcb
->NameListResource
, TRUE
);
154 /* Try to find the name in our cache */
155 for (Entry
= DirectoryFcb
->ShortNameList
.Flink
;
156 Entry
!= &DirectoryFcb
->ShortNameList
;
157 Entry
= Entry
->Flink
)
159 ShortNameEntry
= CONTAINING_RECORD(Entry
, CDFS_SHORT_NAME
, Entry
);
160 if (ShortNameEntry
->StreamOffset
.QuadPart
== StreamOffset
->QuadPart
)
164 (ShortName
->Buffer
, ShortNameEntry
->Name
.Buffer
,
165 ShortNameEntry
->Name
.Length
);
166 ShortName
->Length
= ShortNameEntry
->Name
.Length
;
167 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
168 DPRINT("Yield short name %wZ from cache\n", ShortName
);
174 if (!CdfsIsNameLegalDOS8Dot3(*LongName
))
176 RtlGenerate8dot3Name(LongName
, FALSE
, &Context
, ShortName
);
180 /* copy short name */
181 RtlUpcaseUnicodeString
187 DPRINT("Initial Guess %wZ\n", ShortName
);
189 /* Make it unique by scanning the cache and bumping */
190 /* Note that incrementing the ambiguous name is enough, since we add new
191 * entries at the tail. We'll scan over all collisions. */
192 /* XXX could perform better. */
193 for (Entry
= DirectoryFcb
->ShortNameList
.Flink
;
194 Entry
!= &DirectoryFcb
->ShortNameList
;
195 Entry
= Entry
->Flink
)
197 ShortNameEntry
= CONTAINING_RECORD(Entry
, CDFS_SHORT_NAME
, Entry
);
198 if (RtlCompareUnicodeString
200 &ShortNameEntry
->Name
,
201 TRUE
) == 0) /* Match */
203 RtlGenerate8dot3Name(LongName
, FALSE
, &Context
, ShortName
);
204 DPRINT("Collide; try %wZ\n", ShortName
);
208 /* We've scanned over all entries and now have a unique one. Cache it. */
209 ShortNameEntry
= ExAllocatePoolWithTag(PagedPool
, sizeof(CDFS_SHORT_NAME
), TAG_FCB
);
212 /* We couldn't cache it, but we can return it. We run the risk of
213 * generating a non-unique name later. */
214 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
215 DPRINT1("Couldn't cache potentially clashing 8.3 name %wZ\n", ShortName
);
219 ShortNameEntry
->StreamOffset
= *StreamOffset
;
220 ShortNameEntry
->Name
.Buffer
= ShortNameEntry
->NameBuffer
;
221 ShortNameEntry
->Name
.Length
= ShortName
->Length
;
222 ShortNameEntry
->Name
.MaximumLength
= sizeof(ShortNameEntry
->NameBuffer
);
224 (ShortNameEntry
->NameBuffer
,
227 InsertTailList(&DirectoryFcb
->ShortNameList
, &ShortNameEntry
->Entry
);
228 ExReleaseResourceLite(&DirectoryFcb
->NameListResource
);
230 DPRINT("Returning short name %wZ for long name %wZ\n", ShortName
, LongName
);