xref: /reactos/sdk/lib/rtl/encode.c (revision 8a978a17)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS system libraries
4  * PURPOSE:         Security descriptor functions
5  * FILE:            lib/rtl/encode.c
6  * PROGRAMMER:      KJK::Hyperion <noog@libero.it>
7  *                  (code contributed by crazylord <crazyl0rd@minithins.net>)
8  */
9 
10 /* INCLUDES *****************************************************************/
11 
12 #include <rtl.h>
13 
14 #define NDEBUG
15 #include <debug.h>
16 
17 /* FUNCTIONS ***************************************************************/
18 
19 VOID NTAPI
20 RtlRunDecodeUnicodeString (IN UCHAR Hash,
21                            IN OUT PUNICODE_STRING String)
22 {
23    PUCHAR ptr;
24    USHORT i;
25 
26    ptr = (PUCHAR)String->Buffer;
27    if (String->Length > 1)
28    {
29       for (i = String->Length; i > 1; i--)
30       {
31          ptr[i - 1] ^= ptr[i - 2] ^ Hash;
32       }
33    }
34 
35    if (String->Length >= 1)
36    {
37       ptr[0] ^= Hash | (UCHAR)0x43;
38    }
39 }
40 
41 
42 VOID NTAPI
43 RtlRunEncodeUnicodeString (IN OUT PUCHAR Hash,
44                            IN OUT PUNICODE_STRING String)
45 {
46    LARGE_INTEGER CurrentTime;
47    PUCHAR ptr;
48    USHORT i;
49    NTSTATUS Status;
50 
51    ptr = (PUCHAR) String->Buffer;
52    if (*Hash == 0)
53    {
54       Status = NtQuerySystemTime (&CurrentTime);
55       if (NT_SUCCESS(Status))
56       {
57          for (i = 1; i < sizeof(LARGE_INTEGER) && (*Hash == 0); i++)
58             *Hash |= *(PUCHAR)(((PUCHAR)&CurrentTime) + i);
59       }
60 
61       if (*Hash == 0)
62          *Hash = 1;
63    }
64 
65    if (String->Length >= 1)
66    {
67       ptr[0] ^= (*Hash) | (UCHAR)0x43;
68       if (String->Length > 1)
69       {
70          for (i = 1; i < String->Length; i++)
71          {
72             ptr[i] ^= ptr[i - 1] ^ (*Hash);
73          }
74       }
75    }
76 }
77 
78 /* EOF */
79