xref: /reactos/dll/win32/lsasrv/utils.c (revision 845faec4)
1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         Security Account Manager (SAM) Server
4  * FILE:            reactos/dll/win32/lsasrv/utils.c
5  * PURPOSE:         Utility functions
6  *
7  * PROGRAMMERS:     Eric Kohl
8  */
9 
10 #include "lsasrv.h"
11 
12 #include <winuser.h>
13 
14 /* FUNCTIONS ***************************************************************/
15 
16 INT
17 LsapLoadString(HINSTANCE hInstance,
18                UINT uId,
19                LPWSTR lpBuffer,
20                INT nBufferMax)
21 {
22     HGLOBAL hmem;
23     HRSRC hrsrc;
24     WCHAR *p;
25     int string_num;
26     int i;
27 
28     /* Use loword (incremented by 1) as resourceid */
29     hrsrc = FindResourceW(hInstance,
30                           MAKEINTRESOURCEW((LOWORD(uId) >> 4) + 1),
31                           (LPWSTR)RT_STRING);
32     if (!hrsrc)
33         return 0;
34 
35     hmem = LoadResource(hInstance, hrsrc);
36     if (!hmem)
37         return 0;
38 
39     p = LockResource(hmem);
40     string_num = uId & 0x000f;
41     for (i = 0; i < string_num; i++)
42         p += *p + 1;
43 
44     i = min(nBufferMax - 1, *p);
45     if (i > 0)
46     {
47         memcpy(lpBuffer, p + 1, i * sizeof(WCHAR));
48         lpBuffer[i] = 0;
49     }
50     else
51     {
52         if (nBufferMax > 1)
53         {
54             lpBuffer[0] = 0;
55             return 0;
56         }
57     }
58 
59     return i;
60 }
61 
62 
63 INT
64 LsapGetResourceStringLengthEx(
65     _In_ HINSTANCE hInstance,
66     _In_ UINT uId,
67     _In_ USHORT usLanguage)
68 {
69     HGLOBAL hmem;
70     HRSRC hrsrc;
71     WCHAR *p;
72     UINT i, string_num;
73 
74     /* Use loword (incremented by 1) as resourceid */
75 //    hrsrc = FindResourceExW(hInstance,
76 //                            MAKEINTRESOURCEW((LOWORD(uId) >> 4) + 1),
77 //                            (LPWSTR)RT_STRING,
78 //                            usLanguage);
79     hrsrc = FindResourceW(hInstance,
80                           MAKEINTRESOURCEW((LOWORD(uId) >> 4) + 1),
81                           (LPWSTR)RT_STRING);
82     if (!hrsrc)
83         return 0;
84 
85     hmem = LoadResource(hInstance, hrsrc);
86     if (!hmem)
87         return 0;
88 
89     p = LockResource(hmem);
90     string_num = uId & 0x000f;
91     for (i = 0; i < string_num; i++)
92         p += *p + 1;
93 
94     return *p + 1;
95 }
96 
97 
98 INT
99 LsapLoadStringEx(
100     _In_ HINSTANCE hInstance,
101     _In_ UINT uId,
102     _In_ USHORT usLanguage,
103     _Out_ LPWSTR lpBuffer,
104     _Out_ INT nBufferMax)
105 {
106     HGLOBAL hmem;
107     HRSRC hrsrc;
108     WCHAR *p;
109     int string_num;
110     int i;
111 
112     /* Use loword (incremented by 1) as resourceid */
113 //    hrsrc = FindResourceExW(hInstance,
114 //                            MAKEINTRESOURCEW((LOWORD(uId) >> 4) + 1),
115 //                            (LPWSTR)RT_STRING,
116 //                            usLanguage);
117     hrsrc = FindResourceW(hInstance,
118                           MAKEINTRESOURCEW((LOWORD(uId) >> 4) + 1),
119                           (LPWSTR)RT_STRING);
120     if (!hrsrc)
121         return 0;
122 
123     hmem = LoadResource(hInstance, hrsrc);
124     if (!hmem)
125         return 0;
126 
127     p = LockResource(hmem);
128     string_num = uId & 0x000f;
129     for (i = 0; i < string_num; i++)
130         p += *p + 1;
131 
132     i = min(nBufferMax - 1, *p);
133     if (i > 0)
134     {
135         memcpy(lpBuffer, p + 1, i * sizeof(WCHAR));
136         lpBuffer[i] = 0;
137     }
138     else
139     {
140         if (nBufferMax > 1)
141         {
142             lpBuffer[0] = 0;
143             return 0;
144         }
145     }
146 
147     return i;
148 }
149 
150 
151 PSID
152 LsapAppendRidToSid(
153     PSID SrcSid,
154     ULONG Rid)
155 {
156     ULONG Rids[8] = {0, 0, 0, 0, 0, 0, 0, 0};
157     UCHAR RidCount;
158     PSID DstSid;
159     ULONG i;
160 
161     RidCount = *RtlSubAuthorityCountSid(SrcSid);
162     if (RidCount >= 8)
163         return NULL;
164 
165     for (i = 0; i < RidCount; i++)
166         Rids[i] = *RtlSubAuthoritySid(SrcSid, i);
167 
168     Rids[RidCount] = Rid;
169     RidCount++;
170 
171     RtlAllocateAndInitializeSid(RtlIdentifierAuthoritySid(SrcSid),
172                                 RidCount,
173                                 Rids[0],
174                                 Rids[1],
175                                 Rids[2],
176                                 Rids[3],
177                                 Rids[4],
178                                 Rids[5],
179                                 Rids[6],
180                                 Rids[7],
181                                 &DstSid);
182 
183     return DstSid;
184 }
185 
186 /* EOF */
187