Sync with trunk head (part 1 of 2)
[reactos.git] / drivers / filesystems / cdfs / misc.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2002, 2004 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /* $Id$
20 *
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
26 * UPDATE HISTORY:
27 */
28
29 /* INCLUDES *****************************************************************/
30
31 #include "cdfs.h"
32
33 #define NDEBUG
34 #include <debug.h>
35
36 /* FUNCTIONS ****************************************************************/
37
38 VOID
39 CdfsSwapString(PWCHAR Out,
40 PUCHAR In,
41 ULONG Count)
42 {
43 PUCHAR t = (PUCHAR)Out;
44 ULONG i;
45
46 for (i = 0; i < Count; i += 2)
47 {
48 t[i] = In[i+1];
49 t[i+1] = In[i];
50 if (t[i+1] == 0 && t[i] == ';')
51 break;
52 }
53 if ((i>2)&&(t[i-2] == '.'))
54 {
55 t[i-2] = 0;
56 t[i-1] = 0;
57 }
58 t[i] = 0;
59 t[i+1] = 0;
60 }
61
62
63 VOID
64 CdfsDateTimeToSystemTime(PFCB Fcb,
65 PLARGE_INTEGER SystemTime)
66 {
67 TIME_FIELDS TimeFields;
68 LARGE_INTEGER LocalTime;
69
70 TimeFields.Milliseconds = 0;
71 TimeFields.Second = Fcb->Entry.Second;
72 TimeFields.Minute = Fcb->Entry.Minute;
73 TimeFields.Hour = Fcb->Entry.Hour;
74
75 TimeFields.Day = Fcb->Entry.Day;
76 TimeFields.Month = Fcb->Entry.Month;
77 TimeFields.Year = Fcb->Entry.Year + 1900;
78
79 RtlTimeFieldsToTime(&TimeFields,
80 &LocalTime);
81 ExLocalTimeToSystemTime(&LocalTime, SystemTime);
82 }
83
84
85 VOID
86 CdfsFileFlagsToAttributes(PFCB Fcb,
87 PULONG FileAttributes)
88 {
89 /* FIXME: Fix attributes */
90
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);
96 }
97
98 VOID
99 CdfsShortNameCacheGet
100 (PFCB DirectoryFcb,
101 PLARGE_INTEGER StreamOffset,
102 PUNICODE_STRING LongName,
103 PUNICODE_STRING ShortName)
104 {
105 BOOLEAN HasSpaces;
106 PLIST_ENTRY Entry;
107 PCDFS_SHORT_NAME ShortNameEntry;
108 GENERATE_NAME_CONTEXT Context = { 0 };
109
110 DPRINT("CdfsShortNameCacheGet(%I64u,%wZ)\n", StreamOffset->QuadPart, LongName);
111
112 /* Get the name list resource */
113 ExAcquireResourceExclusiveLite(&DirectoryFcb->NameListResource, TRUE);
114
115 /* Try to find the name in our cache */
116 for (Entry = DirectoryFcb->ShortNameList.Flink;
117 Entry != &DirectoryFcb->ShortNameList;
118 Entry = Entry->Flink)
119 {
120 ShortNameEntry = CONTAINING_RECORD(Entry, CDFS_SHORT_NAME, Entry);
121 if (ShortNameEntry->StreamOffset.QuadPart == StreamOffset->QuadPart)
122 {
123 /* Cache hit */
124 RtlCopyMemory
125 (ShortName->Buffer, ShortNameEntry->Name.Buffer,
126 ShortNameEntry->Name.Length);
127 ShortName->Length = ShortNameEntry->Name.Length;
128 ExReleaseResourceLite(&DirectoryFcb->NameListResource);
129 DPRINT("Yield short name %wZ from cache\n", ShortName);
130 return;
131 }
132 }
133
134 /* Cache miss */
135 if ((RtlIsNameLegalDOS8Dot3(LongName, NULL, &HasSpaces) == FALSE) ||
136 (HasSpaces == TRUE))
137 {
138 RtlGenerate8dot3Name(LongName, FALSE, &Context, ShortName);
139 }
140 else
141 {
142 /* copy short name */
143 RtlUpcaseUnicodeString
144 (ShortName,
145 LongName,
146 FALSE);
147 }
148
149 DPRINT("Initial Guess %wZ\n", ShortName);
150
151 /* Make it unique by scanning the cache and bumping */
152 /* Note that incrementing the ambiguous name is enough, since we add new
153 * entries at the tail. We'll scan over all collisions. */
154 /* XXX could perform better. */
155 for (Entry = DirectoryFcb->ShortNameList.Flink;
156 Entry != &DirectoryFcb->ShortNameList;
157 Entry = Entry->Flink)
158 {
159 ShortNameEntry = CONTAINING_RECORD(Entry, CDFS_SHORT_NAME, Entry);
160 if (RtlCompareUnicodeString
161 (ShortName,
162 &ShortNameEntry->Name,
163 TRUE) == 0) /* Match */
164 {
165 RtlGenerate8dot3Name(LongName, FALSE, &Context, ShortName);
166 DPRINT("Collide; try %wZ\n", ShortName);
167 }
168 }
169
170 /* We've scanned over all entries and now have a unique one. Cache it. */
171 ShortNameEntry = ExAllocatePoolWithTag(PagedPool, sizeof(CDFS_SHORT_NAME), TAG_FCB);
172 if (!ShortNameEntry)
173 {
174 /* We couldn't cache it, but we can return it. We run the risk of
175 * generating a non-unique name later. */
176 ExReleaseResourceLite(&DirectoryFcb->NameListResource);
177 DPRINT1("Couldn't cache potentially clashing 8.3 name %wZ\n", ShortName);
178 return;
179 }
180
181 ShortNameEntry->StreamOffset = *StreamOffset;
182 ShortNameEntry->Name.Buffer = ShortNameEntry->NameBuffer;
183 ShortNameEntry->Name.Length = ShortName->Length;
184 ShortNameEntry->Name.MaximumLength = sizeof(ShortNameEntry->NameBuffer);
185 RtlCopyMemory
186 (ShortNameEntry->NameBuffer,
187 ShortName->Buffer,
188 ShortName->Length);
189 InsertTailList(&DirectoryFcb->ShortNameList, &ShortNameEntry->Entry);
190 ExReleaseResourceLite(&DirectoryFcb->NameListResource);
191
192 DPRINT("Returning short name %wZ for long name %wZ\n", ShortName, LongName);
193 }
194
195 /* EOF */