- Formatting only (no code changes)

svn path=/trunk/; revision=72557
This commit is contained in:
Dmitry Chapyshev 2016-09-03 22:43:29 +00:00
parent 6d4d06afbb
commit 76de8ee7ce

View file

@ -24,259 +24,279 @@ const PCHAR RtlpShortIllegals = ";+=[],\"*\\<>/?:|";
static BOOLEAN static BOOLEAN
RtlpIsShortIllegal(CHAR Char) RtlpIsShortIllegal(CHAR Char)
{ {
return strchr(RtlpShortIllegals, Char) ? TRUE : FALSE; return strchr(RtlpShortIllegals, Char) ? TRUE : FALSE;
} }
static USHORT static USHORT
RtlpGetCheckSum(PUNICODE_STRING Name) RtlpGetCheckSum(PUNICODE_STRING Name)
{ {
USHORT Hash, Saved; USHORT Hash, Saved;
WCHAR * CurChar; WCHAR* CurChar;
USHORT Len; USHORT Len;
if (Name->Length == 0) if (Name->Length == 0)
{ {
return 0; return 0;
} }
if (Name->Length == sizeof(WCHAR)) if (Name->Length == sizeof(WCHAR))
{ {
return Name->Buffer[0]; return Name->Buffer[0];
} }
CurChar = Name->Buffer; CurChar = Name->Buffer;
Hash = (*CurChar << 8) + *(CurChar + 1); Hash = (*CurChar << 8) + *(CurChar + 1);
if (Name->Length == 2 * sizeof(WCHAR))
{
return Hash;
}
Saved = Hash; if (Name->Length == 2 * sizeof(WCHAR))
Len = 2; {
do return Hash;
{ }
CurChar = CurChar + 2;
Hash = (Hash << 7) + *CurChar;
Hash = (Saved >> 1) + (Hash << 8);
if (Len + 1 < Name->Length / sizeof(WCHAR))
{
Hash += *(CurChar + 1);
}
Saved = Hash;
Len += 2;
} while (Len < Name->Length / sizeof(WCHAR));
return Hash; Saved = Hash;
Len = 2;
do
{
CurChar = CurChar + 2;
Hash = (Hash << 7) + *CurChar;
Hash = (Saved >> 1) + (Hash << 8);
if (Len + 1 < Name->Length / sizeof(WCHAR))
{
Hash += *(CurChar + 1);
}
Saved = Hash;
Len += 2;
} while (Len < Name->Length / sizeof(WCHAR));
return Hash;
} }
static ULONG static ULONG
RtlpGetIndexLength(ULONG Index) RtlpGetIndexLength(ULONG Index)
{ {
ULONG Length = 0; ULONG Length = 0;
while (Index)
{ while (Index)
Index /= 10; {
Length++; Index /= 10;
} Length++;
return Length ? Length : 1; }
return Length ? Length : 1;
} }
/* /*
* @implemented * @implemented
*/ */
VOID NTAPI VOID
NTAPI
RtlGenerate8dot3Name(IN PUNICODE_STRING Name, RtlGenerate8dot3Name(IN PUNICODE_STRING Name,
IN BOOLEAN AllowExtendedCharacters, IN BOOLEAN AllowExtendedCharacters,
IN OUT PGENERATE_NAME_CONTEXT Context, IN OUT PGENERATE_NAME_CONTEXT Context,
OUT PUNICODE_STRING Name8dot3) OUT PUNICODE_STRING Name8dot3)
{ {
ULONG Count; ULONG Count;
WCHAR NameBuffer[8]; WCHAR NameBuffer[8];
WCHAR ExtBuffer[4]; WCHAR ExtBuffer[4];
ULONG StrLength; ULONG StrLength;
ULONG NameLength; ULONG NameLength;
ULONG ExtLength; ULONG ExtLength;
ULONG CopyLength; ULONG CopyLength;
ULONG DotPos; ULONG DotPos;
ULONG i, j; ULONG i, j;
ULONG IndexLength; ULONG IndexLength;
ULONG CurrentIndex; ULONG CurrentIndex;
USHORT Checksum; USHORT Checksum;
WCHAR c; WCHAR c;
StrLength = Name->Length / sizeof(WCHAR); StrLength = Name->Length / sizeof(WCHAR);
DPRINT("StrLength: %lu\n", StrLength);
/* Find last dot in Name */ DPRINT("StrLength: %lu\n", StrLength);
DotPos = StrLength;
for (i = 0; i < StrLength; i++)
{
if (Name->Buffer[i] == L'.')
{
DotPos = i;
}
}
DPRINT("DotPos: %lu\n", DotPos); /* Find last dot in Name */
DotPos = StrLength;
for (i = 0; i < StrLength; i++)
{
if (Name->Buffer[i] == L'.')
{
DotPos = i;
}
}
/* Copy name (6 valid characters max) */ DPRINT("DotPos: %lu\n", DotPos);
for (i = 0, NameLength = 0; NameLength < 6 && i < DotPos; i++)
{
c = UNICODE_NULL;
if (AllowExtendedCharacters)
{
c = RtlUpcaseUnicodeChar(Name->Buffer[i]);
Count = 1;
}
else
{
RtlUpcaseUnicodeToOemN((CHAR *)&c, sizeof(CHAR), &Count, &Name->Buffer[i], sizeof(WCHAR));
}
if (Count != 1 || c == UNICODE_NULL || RtlpIsShortIllegal(c)) /* Copy name (6 valid characters max) */
{ for (i = 0, NameLength = 0; NameLength < 6 && i < DotPos; i++)
NameBuffer[NameLength++] = L'_'; {
} c = UNICODE_NULL;
else if (c != L'.' && c != L' ') if (AllowExtendedCharacters)
{ {
if (isgraph(c) || (AllowExtendedCharacters && iswgraph(c))) c = RtlUpcaseUnicodeChar(Name->Buffer[i]);
{ Count = 1;
NameBuffer[NameLength++] = c; }
} else
} {
} RtlUpcaseUnicodeToOemN((CHAR *)&c, sizeof(CHAR), &Count, &Name->Buffer[i], sizeof(WCHAR));
}
DPRINT("NameBuffer: '%.08S'\n", NameBuffer); if (Count != 1 || c == UNICODE_NULL || RtlpIsShortIllegal(c))
DPRINT("NameLength: %lu\n", NameLength); {
NameBuffer[NameLength++] = L'_';
/* Copy extension (4 valid characters max) */ }
if (DotPos < StrLength) else if (c != L'.' && c != L' ')
{ {
for (i = DotPos, ExtLength = 0; ExtLength < 4 && i < StrLength; i++) if (isgraph(c) || (AllowExtendedCharacters && iswgraph(c)))
{
c = UNICODE_NULL;
if (AllowExtendedCharacters)
{
c = RtlUpcaseUnicodeChar(Name->Buffer[i]);
Count = 1;
}
else
{
RtlUpcaseUnicodeToOemN((CHAR *)&c, sizeof(CHAR), &Count, &Name->Buffer[i], sizeof(WCHAR));
}
if (Count != 1 || c == UNICODE_NULL || RtlpIsShortIllegal(c))
{
ExtBuffer[ExtLength++] = L'_';
}
else if (c != L' ')
{
if (isgraph(c) || c == L'.' || (AllowExtendedCharacters && iswgraph(c)))
{ {
ExtBuffer[ExtLength++] = c; NameBuffer[NameLength++] = c;
} }
} }
} }
}
else
{
ExtLength = 0;
}
DPRINT("ExtBuffer: '%.04S'\n", ExtBuffer);
DPRINT("ExtLength: %lu\n", ExtLength);
/* Determine next index */ DPRINT("NameBuffer: '%.08S'\n", NameBuffer);
IndexLength = RtlpGetIndexLength(Context->LastIndexValue); DPRINT("NameLength: %lu\n", NameLength);
if (Context->CheckSumInserted)
{
CopyLength = min(NameLength, 8 - 4 - 1 - IndexLength);
Checksum = RtlpGetCheckSum(Name);
}
else
{
CopyLength = min(NameLength, 8 - 1 - IndexLength);
Checksum = 0;
}
DPRINT("CopyLength: %lu\n", CopyLength); /* Copy extension (4 valid characters max) */
if (DotPos < StrLength)
{
for (i = DotPos, ExtLength = 0; ExtLength < 4 && i < StrLength; i++)
{
c = UNICODE_NULL;
if ((Context->NameLength == CopyLength) && if (AllowExtendedCharacters)
(wcsncmp(Context->NameBuffer, NameBuffer, CopyLength) == 0) && {
(Context->ExtensionLength == ExtLength) && c = RtlUpcaseUnicodeChar(Name->Buffer[i]);
(wcsncmp(Context->ExtensionBuffer, ExtBuffer, ExtLength) == 0) && Count = 1;
(Checksum == Context->Checksum) && }
(Context->LastIndexValue < 999)) else
{ {
Context->LastIndexValue++; RtlUpcaseUnicodeToOemN((CHAR *)&c, sizeof(CHAR), &Count, &Name->Buffer[i], sizeof(WCHAR));
if (Context->CheckSumInserted == FALSE && }
if (Count != 1 || c == UNICODE_NULL || RtlpIsShortIllegal(c))
{
ExtBuffer[ExtLength++] = L'_';
}
else if (c != L' ')
{
if (isgraph(c) || c == L'.' || (AllowExtendedCharacters && iswgraph(c)))
{
ExtBuffer[ExtLength++] = c;
}
}
}
}
else
{
ExtLength = 0;
}
DPRINT("ExtBuffer: '%.04S'\n", ExtBuffer);
DPRINT("ExtLength: %lu\n", ExtLength);
/* Determine next index */
IndexLength = RtlpGetIndexLength(Context->LastIndexValue);
if (Context->CheckSumInserted)
{
CopyLength = min(NameLength, 8 - 4 - 1 - IndexLength);
Checksum = RtlpGetCheckSum(Name);
}
else
{
CopyLength = min(NameLength, 8 - 1 - IndexLength);
Checksum = 0;
}
DPRINT("CopyLength: %lu\n", CopyLength);
if ((Context->NameLength == CopyLength) &&
(wcsncmp(Context->NameBuffer, NameBuffer, CopyLength) == 0) &&
(Context->ExtensionLength == ExtLength) &&
(wcsncmp(Context->ExtensionBuffer, ExtBuffer, ExtLength) == 0) &&
(Checksum == Context->Checksum) &&
(Context->LastIndexValue < 999))
{
Context->LastIndexValue++;
if (Context->CheckSumInserted == FALSE &&
Context->LastIndexValue > 9) Context->LastIndexValue > 9)
{ {
Context->CheckSumInserted = TRUE; Context->CheckSumInserted = TRUE;
Context->LastIndexValue = 1; Context->LastIndexValue = 1;
Context->Checksum = RtlpGetCheckSum(Name); Context->Checksum = RtlpGetCheckSum(Name);
} }
} }
else else
{ {
Context->LastIndexValue = 1; Context->LastIndexValue = 1;
if (NameLength == 0)
{
Context->CheckSumInserted = TRUE;
Context->Checksum = RtlpGetCheckSum(Name);
}
else
{
Context->CheckSumInserted = FALSE;
}
}
IndexLength = RtlpGetIndexLength(Context->LastIndexValue); if (NameLength == 0)
{
Context->CheckSumInserted = TRUE;
Context->Checksum = RtlpGetCheckSum(Name);
}
else
{
Context->CheckSumInserted = FALSE;
}
}
DPRINT("CurrentIndex: %lu, IndexLength %lu\n", Context->LastIndexValue, IndexLength); IndexLength = RtlpGetIndexLength(Context->LastIndexValue);
if (Context->CheckSumInserted) DPRINT("CurrentIndex: %lu, IndexLength %lu\n", Context->LastIndexValue, IndexLength);
{
CopyLength = min(NameLength, 8 - 4 - 1 - IndexLength);
}
else
{
CopyLength = min(NameLength, 8 - 1 - IndexLength);
}
/* Build the short name */ if (Context->CheckSumInserted)
memcpy(Name8dot3->Buffer, NameBuffer, CopyLength * sizeof(WCHAR)); {
j = CopyLength; CopyLength = min(NameLength, 8 - 4 - 1 - IndexLength);
if (Context->CheckSumInserted) }
{ else
Checksum = Context->Checksum; {
for (i = 0; i < 4; i++) CopyLength = min(NameLength, 8 - 1 - IndexLength);
{ }
Name8dot3->Buffer[j++] = (Checksum & 0xF) > 9 ? (Checksum & 0xF) + L'A' - 10 : (Checksum & 0xF) + L'0';
Checksum >>= 4;
}
j = CopyLength + 4;
}
Name8dot3->Buffer[j++] = L'~';
j += IndexLength - 1;
CurrentIndex = Context->LastIndexValue;
for (i = 0; i < IndexLength; i++)
{
Name8dot3->Buffer[j--] = (CurrentIndex % 10) + L'0';
CurrentIndex /= 10;
}
j += IndexLength + 1;
memcpy(Name8dot3->Buffer + j, ExtBuffer, ExtLength * sizeof(WCHAR)); /* Build the short name */
Name8dot3->Length = (USHORT)(j + ExtLength) * sizeof(WCHAR); memcpy(Name8dot3->Buffer, NameBuffer, CopyLength * sizeof(WCHAR));
DPRINT("Name8dot3: '%wZ'\n", Name8dot3); j = CopyLength;
/* Update context */ if (Context->CheckSumInserted)
Context->NameLength = (UCHAR)CopyLength; {
Context->ExtensionLength = ExtLength; Checksum = Context->Checksum;
memcpy(Context->NameBuffer, NameBuffer, CopyLength * sizeof(WCHAR));
memcpy(Context->ExtensionBuffer, ExtBuffer, ExtLength * sizeof(WCHAR)); for (i = 0; i < 4; i++)
{
Name8dot3->Buffer[j++] = (Checksum & 0xF) > 9 ? (Checksum & 0xF) + L'A' - 10 : (Checksum & 0xF) + L'0';
Checksum >>= 4;
}
j = CopyLength + 4;
}
Name8dot3->Buffer[j++] = L'~';
j += IndexLength - 1;
CurrentIndex = Context->LastIndexValue;
for (i = 0; i < IndexLength; i++)
{
Name8dot3->Buffer[j--] = (CurrentIndex % 10) + L'0';
CurrentIndex /= 10;
}
j += IndexLength + 1;
memcpy(Name8dot3->Buffer + j, ExtBuffer, ExtLength * sizeof(WCHAR));
Name8dot3->Length = (USHORT)(j + ExtLength) * sizeof(WCHAR);
DPRINT("Name8dot3: '%wZ'\n", Name8dot3);
/* Update context */
Context->NameLength = (UCHAR)CopyLength;
Context->ExtensionLength = ExtLength;
memcpy(Context->NameBuffer, NameBuffer, CopyLength * sizeof(WCHAR));
memcpy(Context->ExtensionBuffer, ExtBuffer, ExtLength * sizeof(WCHAR));
} }
@ -290,10 +310,9 @@ _Must_inspect_result_
NTSYSAPI NTSYSAPI
BOOLEAN BOOLEAN
NTAPI NTAPI
RtlIsNameLegalDOS8Dot3 ( RtlIsNameLegalDOS8Dot3(IN PCUNICODE_STRING Name,
_In_ PCUNICODE_STRING Name, IN OUT POEM_STRING OemName,
_Inout_opt_ POEM_STRING OemName, OUT PBOOLEAN NameContainsSpaces OPTIONAL)
_Out_opt_ PBOOLEAN NameContainsSpaces)
{ {
static const char Illegal[] = "*?<>|\"+=,;[]:/\\\345"; static const char Illegal[] = "*?<>|\"+=,;[]:/\\\345";
int Dot = -1; int Dot = -1;
@ -322,6 +341,7 @@ RtlIsNameLegalDOS8Dot3 (
{ {
if (OemName->Length != 1 && (OemName->Length != 2 || OemName->Buffer[1] != '.')) return FALSE; if (OemName->Length != 1 && (OemName->Length != 2 || OemName->Buffer[1] != '.')) return FALSE;
if (NameContainsSpaces) *NameContainsSpaces = FALSE; if (NameContainsSpaces) *NameContainsSpaces = FALSE;
return TRUE; return TRUE;
} }
@ -329,18 +349,20 @@ RtlIsNameLegalDOS8Dot3 (
{ {
switch (OemName->Buffer[i]) switch (OemName->Buffer[i])
{ {
case ' ': case ' ':
/* leading/trailing spaces not allowed */ /* leading/trailing spaces not allowed */
if (!i || i == OemName->Length-1 || OemName->Buffer[i+1] == '.') return FALSE; if (!i || i == OemName->Length-1 || OemName->Buffer[i+1] == '.') return FALSE;
GotSpace = TRUE; GotSpace = TRUE;
break; break;
case '.':
if (Dot != -1) return FALSE; case '.':
Dot = i; if (Dot != -1) return FALSE;
break; Dot = i;
default: break;
if (strchr(Illegal, OemName->Buffer[i])) return FALSE;
break; default:
if (strchr(Illegal, OemName->Buffer[i])) return FALSE;
break;
} }
} }
/* check file part is shorter than 8, extension shorter than 3 /* check file part is shorter than 8, extension shorter than 3
@ -354,7 +376,9 @@ RtlIsNameLegalDOS8Dot3 (
{ {
if (Dot > 8 || (OemName->Length - Dot > 4) || Dot == OemName->Length - 1) return FALSE; if (Dot > 8 || (OemName->Length - Dot > 4) || Dot == OemName->Length - 1) return FALSE;
} }
if (NameContainsSpaces) *NameContainsSpaces = GotSpace; if (NameContainsSpaces) *NameContainsSpaces = GotSpace;
return TRUE; return TRUE;
} }