551f90fecaebcca823219fd56f6b7ab0efaa475c
[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 NTAPI RtlRunDecodeUnicodeString
22 (
23  IN UCHAR hash,
24  IN OUT PUNICODE_STRING uString
25 )
26 {
27    UCHAR *ptr;
28    WORD   i;
29    
30    ptr = (UCHAR *) uString->Buffer;
31    if (uString->Length > 1) {
32       for (i=uString->Length; i>1; i--) {
33          ptr[i-1] ^= ptr[i-2] ^ hash;
34       }
35    }
36    
37    if (uString->Length >= 1) {
38       ptr[0] ^= hash | (UCHAR) 0x43;
39    }
40 }
41
42 VOID NTAPI RtlRunEncodeUnicodeString
43 (
44  IN OUT PUCHAR hash,
45  IN OUT PUNICODE_STRING uString
46 )
47 {
48    NTSTATUS ntS;
49    UCHAR *ptr;
50    TIME  CurrentTime;
51    WORD   i;
52
53    ptr = (UCHAR *) uString->Buffer;   
54    if (*hash == 0) {
55       ntS = NtQuerySystemTime(&CurrentTime);
56       if (NT_SUCCESS(ntS)) {
57          for (i=1; i<sizeof(TIME) && (*hash == 0); i++)
58             *hash |= *(PUCHAR) (((PUCHAR) &CurrentTime)+i);
59       }
60       
61       if (*hash == 0)
62          *hash = 1;
63    }
64  
65    if (uString->Length >= 1) {
66       ptr[0] ^= (*hash) | (UCHAR) 0x43;
67       if (uString->Length > 1) {
68          for (i=1; i<uString->Length; i++) {
69             ptr[i] ^= ptr[i-1] ^ (*hash);
70          }
71       }
72    }
73 }
74
75 /* EOF */