- Use correct checks for illegal chars
- Build context and 8.3 name on the basis of it (context can be already presented)

* Fixes 34 tests for ntdll:RtlGenerate8dot3Name (remained 16 tests will be fixed after correction of a problem with NLS files)

svn path=/trunk/; revision=72668
This commit is contained in:
Dmitry Chapyshev 2016-09-14 00:31:30 +00:00
parent f693e1e83a
commit a4cd19ec42

View file

@ -13,81 +13,66 @@
#define NDEBUG #define NDEBUG
#include <debug.h> #include <debug.h>
extern PUSHORT NlsUnicodeToMbOemTable;
/* CONSTANTS *****************************************************************/ /* CONSTANTS *****************************************************************/
const PCHAR RtlpShortIllegals = ";+=[],\"*\\<>/?:|"; const ULONG RtlpShortIllegals[] = { 0xFFFFFFFF, 0xFC009C04, 0x38000000, 0x10000000 };
/* FUNCTIONS *****************************************************************/ /* FUNCTIONS *****************************************************************/
BOOLEAN
NTAPI
RtlIsValidOemCharacter(IN PWCHAR Char);
static BOOLEAN static BOOLEAN
RtlpIsShortIllegal(CHAR Char) RtlpIsShortIllegal(const WCHAR Char)
{ {
return strchr(RtlpShortIllegals, Char) ? TRUE : FALSE; return (Char < 128 && (RtlpShortIllegals[Char / 32] & (1 << (Char % 32))));
} }
static USHORT static USHORT
RtlpGetCheckSum(PUNICODE_STRING Name) RtlpGetCheckSum(PUNICODE_STRING Name)
{ {
USHORT Hash, Saved; PWCHAR CurrentChar;
WCHAR* CurChar; USHORT Hash;
USHORT Len; USHORT Saved;
USHORT Length;
if (Name->Length == 0) if (!Name->Length)
{
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; CurrentChar = Name->Buffer;
Hash = (*CurChar << 8) + *(CurChar + 1); Hash = (*CurrentChar << 8) + *(CurrentChar + 1);
if (Name->Length == 2 * sizeof(WCHAR)) if (Name->Length == 2 * sizeof(WCHAR))
{
return Hash; return Hash;
}
Saved = Hash; Saved = Hash;
Len = 2; Length = 2;
do do
{ {
CurChar = CurChar + 2; CurrentChar += 2;
Hash = (Hash << 7) + *CurChar; Hash = (Hash << 7) + *CurrentChar;
Hash = (Saved >> 1) + (Hash << 8); Hash = (Saved >> 1) + (Hash << 8);
if (Len + 1 < Name->Length / sizeof(WCHAR)) if (Length + 1 < Name->Length / sizeof(WCHAR))
{ {
Hash += *(CurChar + 1); Hash += *(CurrentChar + 1);
} }
Saved = Hash; Saved = Hash;
Len += 2; Length += 2;
} while (Len < Name->Length / sizeof(WCHAR)); }
while (Length < Name->Length / sizeof(WCHAR));
return Hash; return Hash;
} }
static ULONG
RtlpGetIndexLength(ULONG Index)
{
ULONG Length = 0;
while (Index)
{
Index /= 10;
Length++;
}
return Length ? Length : 1;
}
/* /*
* @implemented * @implemented
*/ */
@ -98,205 +83,146 @@ RtlGenerate8dot3Name(IN PUNICODE_STRING Name,
IN OUT PGENERATE_NAME_CONTEXT Context, IN OUT PGENERATE_NAME_CONTEXT Context,
OUT PUNICODE_STRING Name8dot3) OUT PUNICODE_STRING Name8dot3)
{ {
ULONG Count; ULONG Length = Name->Length / sizeof(WCHAR);
WCHAR NameBuffer[8];
WCHAR ExtBuffer[4];
ULONG StrLength;
ULONG NameLength;
ULONG ExtLength;
ULONG CopyLength;
ULONG DotPos;
ULONG i, j;
ULONG IndexLength; ULONG IndexLength;
ULONG CurrentIndex; ULONG Index;
ULONG DotPos;
WCHAR IndexBuffer[8];
WCHAR Char;
USHORT Checksum; USHORT Checksum;
WCHAR c;
StrLength = Name->Length / sizeof(WCHAR); if (!Context->NameLength)
{
DPRINT("StrLength: %lu\n", StrLength); DotPos = Length;
/* Find last dot in Name */ /* Find last dot in Name */
DotPos = StrLength; for (Index = 0; Index < Length; Index++)
for (i = 0; i < StrLength; i++)
{ {
if (Name->Buffer[i] == L'.') if (Name->Buffer[Index] == L'.')
{ DotPos = Index;
DotPos = i;
} }
}
DPRINT("DotPos: %lu\n", DotPos);
/* Copy name (6 valid characters max) */ /* Copy name (6 valid characters max) */
for (i = 0, NameLength = 0; NameLength < 6 && i < DotPos; i++) for (Index = 0; Index < DotPos && Context->NameLength < 6; Index++)
{ {
c = UNICODE_NULL; Char = Name->Buffer[Index];
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)) if ((Char > L' ') && (Char != L'.') &&
((Char < 127) || (AllowExtendedCharacters && RtlIsValidOemCharacter(&Char))))
{ {
NameBuffer[NameLength++] = L'_'; if (RtlpIsShortIllegal(Char))
} Char = L'_';
else if (c != L'.' && c != L' ') else if (Char >= L'a' && Char <= L'z')
{ Char = RtlUpcaseUnicodeChar(Char);
if (isgraph(c) || (AllowExtendedCharacters && iswgraph(c)))
{
NameBuffer[NameLength++] = c;
}
}
}
DPRINT("NameBuffer: '%.08S'\n", NameBuffer); Context->NameBuffer[Context->NameLength] = Char;
DPRINT("NameLength: %lu\n", NameLength); ++Context->NameLength;
/* Copy extension (4 valid characters max) */
if (DotPos < StrLength)
{
for (i = DotPos, ExtLength = 0; ExtLength < 4 && i < StrLength; 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))
{
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->CheckSumInserted = TRUE;
Context->LastIndexValue = 1;
Context->Checksum = RtlpGetCheckSum(Name);
}
}
else
{
Context->LastIndexValue = 1;
if (NameLength == 0)
{
Context->CheckSumInserted = TRUE;
Context->Checksum = RtlpGetCheckSum(Name);
}
else
{
Context->CheckSumInserted = FALSE;
} }
} }
IndexLength = RtlpGetIndexLength(Context->LastIndexValue); /* Copy extension (4 valid characters max) */
Context->ExtensionLength = 0;
DPRINT("CurrentIndex: %lu, IndexLength %lu\n", Context->LastIndexValue, IndexLength); if (DotPos < Length)
if (Context->CheckSumInserted)
{ {
CopyLength = min(NameLength, 8 - 4 - 1 - IndexLength); Context->ExtensionBuffer[0] = L'.';
} Context->ExtensionLength = 1;
else
while (DotPos < Length && Context->ExtensionLength < 4)
{ {
CopyLength = min(NameLength, 8 - 1 - IndexLength); Char = Name->Buffer[DotPos];
if ((Char > L' ') && (Char != L'.') &&
((Char < 127) || (AllowExtendedCharacters && RtlIsValidOemCharacter(&Char))))
{
if (RtlpIsShortIllegal(Char))
Char = L'_';
else if (Char >= L'a' && Char <= L'z')
Char = RtlUpcaseUnicodeChar(Char);
Context->ExtensionBuffer[Context->ExtensionLength++] = Char;
} }
/* Build the short name */ Char = UNICODE_NULL;
memcpy(Name8dot3->Buffer, NameBuffer, CopyLength * sizeof(WCHAR)); ++DotPos;
}
j = CopyLength; if (Char != UNICODE_NULL)
Context->ExtensionBuffer[Context->ExtensionLength - 1] = L'~';
}
if (Context->CheckSumInserted) if (Context->NameLength <= 2)
{ {
Checksum = Context->Checksum; Checksum = Context->Checksum = RtlpGetCheckSum(Name);
for (i = 0; i < 4; i++) for (Index = 0; Index < 4; Index++)
{ {
Name8dot3->Buffer[j++] = (Checksum & 0xF) > 9 ? (Checksum & 0xF) + L'A' - 10 : (Checksum & 0xF) + L'0'; Context->NameBuffer[Context->NameLength + Index] =
(Checksum & 0xF) > 9 ? (Checksum & 0xF) + L'A' - 10 : (Checksum & 0xF) + L'0';
Checksum >>= 4; Checksum >>= 4;
} }
j = CopyLength + 4; Context->CheckSumInserted = TRUE;
Context->NameLength += 4;
}
} }
Name8dot3->Buffer[j++] = L'~'; ++Context->LastIndexValue;
j += IndexLength - 1;
CurrentIndex = Context->LastIndexValue;
for (i = 0; i < IndexLength; i++) if (Context->LastIndexValue > 4 && !Context->CheckSumInserted)
{ {
Name8dot3->Buffer[j--] = (CurrentIndex % 10) + L'0'; Checksum = Context->Checksum = RtlpGetCheckSum(Name);
CurrentIndex /= 10;
for (Index = 2; Index < 6; Index++)
{
Context->NameBuffer[Index] =
(Checksum & 0xF) > 9 ? (Checksum & 0xF) + L'A' - 10 : (Checksum & 0xF) + L'0';
Checksum >>= 4;
} }
j += IndexLength + 1; Context->CheckSumInserted = TRUE;
Context->NameLength = 6;
Context->LastIndexValue = 1;
}
memcpy(Name8dot3->Buffer + j, ExtBuffer, ExtLength * sizeof(WCHAR)); /* Calculate index length and index buffer */
Name8dot3->Length = (USHORT)(j + ExtLength) * sizeof(WCHAR); Index = Context->LastIndexValue;
for (IndexLength = 1; IndexLength <= 7 && Index > 0; IndexLength++)
{
IndexBuffer[8 - IndexLength] = L'0' + (Index % 10);
Index /= 10;
}
DPRINT("Name8dot3: '%wZ'\n", Name8dot3); IndexBuffer[8 - IndexLength] = L'~';
/* Update context */ /* Reset name length */
Context->NameLength = (UCHAR)CopyLength; Name8dot3->Length = 0;
Context->ExtensionLength = ExtLength;
memcpy(Context->NameBuffer, NameBuffer, CopyLength * sizeof(WCHAR)); /* If name present */
memcpy(Context->ExtensionBuffer, ExtBuffer, ExtLength * sizeof(WCHAR)); if (Context->NameLength)
{
/* Copy name buffer */
Length = Context->NameLength * sizeof(WCHAR);
RtlCopyMemory(Name8dot3->Buffer, Context->NameBuffer, Length);
Name8dot3->Length = Length;
}
/* Copy index buffer */
Length = IndexLength * sizeof(WCHAR);
RtlCopyMemory(Name8dot3->Buffer + (Name8dot3->Length / sizeof(WCHAR)),
IndexBuffer + (8 - IndexLength),
Length);
Name8dot3->Length += Length;
/* If extension present */
if (Context->ExtensionLength)
{
/* Copy extension buffer */
Length = Context->ExtensionLength * sizeof(WCHAR);
RtlCopyMemory(Name8dot3->Buffer + (Name8dot3->Length / sizeof(WCHAR)),
Context->ExtensionBuffer,
Length);
Name8dot3->Length += Length;
}
} }