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