Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / WINNT / afsadmsvr / TaAfsAdmSvrCommon.cpp
blob0df11b54b3eccea030d96fef5d34a4fa0f1aa373
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
8 */
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
13 extern "C" {
14 #include <afs/param.h>
15 #include <afs/stds.h>
18 #include <WINNT/TaAfsAdmSvr.h>
22 * DEFINITIONS ________________________________________________________________
26 #define cREALLOC_ASIDLIST 64
28 #define cREALLOC_OBJPROPLIST 32
30 #define cREALLOC_ACTIONLIST 8
34 * REALLOC ____________________________________________________________________
36 * This thing, RPC_REALLOC, is a specialized version of the REALLOC() code that
37 * you'll find in most libraries. It's intended to deal with structures that
38 * look like this:
40 * typedef struct {
41 * DWORD dwGarbage1;
42 * size_t cElements; <- The interesting bit
43 * DWORD dwGarbage2;
44 * MYTYPE aElements[1]; <- <- <- The really interesting bit
45 * } MYSTRUCT;
47 * Remember that since the array is growable, it must be the last element
48 * in the structure. This thing's called RPC_REALLOC because these growable
49 * arrays are well-suited for transferring via rpc--just make the aElements
50 * line in your .IDL file look like:
52 * [size_is(cElements) length_is(cElements)] MYTYPE aElements[*];
54 * As an example of its use, say we want to fill in element 15:
56 * void FillInElement15 (MYSTRUCT *pStruct, MYTYPE *pNewData)
57 * {
58 * int iElement = 15;
59 * if (RPC_REALLOC (MYSTRUCT, pStruct, aElements, cElements, 1+iElement, cREALLOC_MYSTRUCT))
60 * {
61 * memcpy (&pStruct->aElements[ iElement ], pNewData, sizeof(MYTYPE));
62 * }
63 * }
65 * As always, the "cREALLOC_MYSTRUCT" can be any positive value; it specifies
66 * the granularity with which the the array will be over-allocated. Note that
67 * unlike the normal REALLOC() routine, the {aElements} and {cElements}
68 * parameters are presumed to be members of the specified pointer-to-structure.
69 * If {*pStruct} is NULL upon entry, it will be allocated and zero-filled.
73 #define OFFSETOF(_type,_a) (size_t)&(((_type *)0)->_a)
74 #define RPC_REALLOC(_type,_str,_a,_c,_r,_ci) AfsAdmSvr_ReallocFunction ((PVOID*)&(_str), OFFSETOF(_type,_a), OFFSETOF(_type,_c), sizeof((_str)->_a[0]), (size_t)_r, (size_t)_ci, 0x00)
76 BOOL AfsAdmSvr_ReallocFunction (PVOID *ppStructure, size_t cbHeader, size_t iposCount, size_t cbElement, size_t cReq, size_t cInc, BYTE chFill)
78 // Use cInc to over-estimate how much space is really required; that
79 // way, allocating space for one element actually allocates space for
80 // many--so that next time we get here, we won't have to do any work.
82 if (cInc)
83 cReq = cInc * ( (cReq + cInc - 1) / cInc );
84 cReq = max (cReq, 1);
86 // See how much space is allocated now. If we have no structure to start
87 // with, obviously we have no array elements either.
89 size_t cNow = 0;
90 if (*ppStructure)
91 cNow = *(size_t *)( ((PBYTE)(*ppStructure)) + iposCount );
93 if (cNow < cReq)
95 // Hmmm... there wasn't enough space. Allocate a new structure.
97 size_t cbAlloc = cbHeader + cbElement * cReq;
98 PVOID pNewStructure;
99 if ((pNewStructure = Allocate (cbAlloc)) == NULL)
100 return FALSE;
102 memset (pNewStructure, 0x00, cbHeader);
103 memset ((PBYTE)pNewStructure + cbHeader, chFill, cbAlloc - cbHeader);
104 if (*ppStructure)
105 memcpy (pNewStructure, *ppStructure, cbHeader);
107 *(size_t *)( ((PBYTE)pNewStructure) + iposCount ) = cReq;
109 // Transfer any information from the old structure's elements
111 if (cNow)
112 memcpy ((PBYTE)pNewStructure + cbHeader, ((PBYTE)(*ppStructure)) + cbHeader, cNow * cbElement);
114 // If there was one, free the old structure
116 if (*ppStructure)
117 Free (*ppStructure);
118 *ppStructure = pNewStructure;
121 return TRUE;
126 * ROUTINES ___________________________________________________________________
130 // ASIDLIST - Managed type for lists of cell objects
132 LPASIDLIST AfsAdmSvr_CreateAsidList (void)
134 LPASIDLIST pList = NULL;
135 if (!RPC_REALLOC (ASIDLIST, pList, aEntries, cEntriesAllocated, 0, cREALLOC_ASIDLIST))
136 return NULL;
137 return pList;
140 LPASIDLIST AfsAdmSvr_CopyAsidList (LPASIDLIST pListSource)
142 LPASIDLIST pList = NULL;
143 if (!RPC_REALLOC (ASIDLIST, pList, aEntries, cEntriesAllocated, pListSource->cEntries, cREALLOC_ASIDLIST))
144 return NULL;
145 if (pList->cEntriesAllocated)
146 memcpy (pList->aEntries, pListSource->aEntries, sizeof(pList->aEntries[0]) * pList->cEntriesAllocated);
147 pList->cEntries = pListSource->cEntries;
148 return pList;
151 BOOL AfsAdmSvr_AddToAsidList (LPASIDLIST *ppList, ASID idObject, LPARAM lp)
153 if (!ppList || !*ppList)
154 return FALSE;
156 if (!RPC_REALLOC (ASIDLIST, *ppList, aEntries, cEntriesAllocated, (*ppList)->cEntries +1, cREALLOC_ASIDLIST))
157 return FALSE;
159 (*ppList)->aEntries[ (*ppList)->cEntries ].idObject = idObject;
160 (*ppList)->aEntries[ (*ppList)->cEntries ].lParam = lp;
161 (*ppList)->cEntries ++;
162 return TRUE;
165 BOOL AfsAdmSvr_RemoveFromAsidList (LPASIDLIST *ppList, ASID idObject)
167 if (!ppList || !(*ppList) || !(*ppList)->cEntries)
168 return FALSE;
170 BOOL fFound = FALSE;
171 for (ULONG iEntry = 0; iEntry < (*ppList)->cEntries; )
173 if ((*ppList)->aEntries[ iEntry ].idObject != idObject)
174 iEntry ++;
175 else if ((fFound = AfsAdmSvr_RemoveFromAsidListByIndex (ppList, iEntry)) == FALSE)
176 break;
179 return fFound;
183 BOOL AfsAdmSvr_RemoveFromAsidListByIndex (LPASIDLIST *ppList, size_t iIndex)
185 if (iIndex >= (*ppList)->cEntries)
186 return FALSE;
188 if (iIndex < (*ppList)->cEntries -1)
189 memcpy (&(*ppList)->aEntries[ iIndex ], &(*ppList)->aEntries[ (*ppList)->cEntries-1 ], sizeof((*ppList)->aEntries[0]));
190 (*ppList)->cEntries --;
191 return TRUE;
195 BOOL AfsAdmSvr_SetAsidListParam (LPASIDLIST *ppList, ASID idObject, LPARAM lp)
197 BOOL fFound = FALSE;
198 for (ULONG iEntry = 0; iEntry < (*ppList)->cEntries; ++iEntry)
200 if ((*ppList)->aEntries[ iEntry ].idObject == idObject)
202 (*ppList)->aEntries[ iEntry ].lParam = lp;
203 fFound = TRUE;
206 return fFound;
210 BOOL AfsAdmSvr_SetAsidListParamByIndex (LPASIDLIST *ppList, size_t iIndex, LPARAM lp)
212 if (iIndex >= (*ppList)->cEntries)
213 return FALSE;
215 (*ppList)->aEntries[ iIndex ].lParam = lp;
216 return TRUE;
220 BOOL AfsAdmSvr_IsInAsidList (LPASIDLIST *ppList, ASID idObject, LPARAM *pParam)
222 if (!ppList || !(*ppList) || !(*ppList)->cEntries)
223 return FALSE;
225 for (ULONG iEntry = 0; iEntry < (*ppList)->cEntries; ++iEntry)
227 if ((*ppList)->aEntries[ iEntry ].idObject == idObject)
229 if (pParam)
230 *pParam = (*ppList)->aEntries[ iEntry ].lParam;
231 return TRUE;
235 return FALSE;
239 void AfsAdmSvr_FreeAsidList (LPASIDLIST *ppList)
241 if (ppList && *ppList)
243 Free (*ppList);
244 (*ppList) = 0;
249 // ASOBJPROPLIST - Managed type for lists of cell objects
251 LPASOBJPROPLIST AfsAdmSvr_CreateObjPropList (void)
253 LPASOBJPROPLIST pList = NULL;
254 if (!RPC_REALLOC (ASOBJPROPLIST, pList, aEntries, cEntriesAllocated, 0, cREALLOC_OBJPROPLIST))
255 return NULL;
256 return pList;
259 LPASOBJPROPLIST AfsAdmSvr_CopyObjPropList (LPASOBJPROPLIST pListSource)
261 LPASOBJPROPLIST pList = NULL;
262 if (!RPC_REALLOC (ASOBJPROPLIST, pList, aEntries, cEntriesAllocated, pListSource->cEntries, cREALLOC_OBJPROPLIST))
263 return NULL;
264 if (pList->cEntriesAllocated)
265 memcpy (pList->aEntries, pListSource->aEntries, sizeof(pList->aEntries[0]) * pList->cEntriesAllocated);
266 pList->cEntries = pListSource->cEntries;
267 return pList;
270 BOOL AfsAdmSvr_AddToObjPropList (LPASOBJPROPLIST *ppList, LPASOBJPROP pProperties, LPARAM lp)
272 if (!ppList || !*ppList)
273 return FALSE;
275 if (!RPC_REALLOC (ASOBJPROPLIST, *ppList, aEntries, cEntriesAllocated, (*ppList)->cEntries +1, cREALLOC_OBJPROPLIST))
276 return NULL;
278 memcpy (&(*ppList)->aEntries[ (*ppList)->cEntries ].ObjectProperties, pProperties, sizeof(ASOBJPROP));
279 (*ppList)->aEntries[ (*ppList)->cEntries ].lParam = lp;
280 (*ppList)->cEntries ++;
281 return TRUE;
284 BOOL AfsAdmSvr_RemoveFromObjPropList (LPASOBJPROPLIST *ppList, ASID idObject)
286 if (!ppList || !(*ppList) || !(*ppList)->cEntries)
287 return FALSE;
289 BOOL fFound = FALSE;
290 for (ULONG iEntry = 0; iEntry < (*ppList)->cEntries; )
292 if ((*ppList)->aEntries[ iEntry ].ObjectProperties.idObject != idObject)
293 iEntry ++;
294 else
296 fFound = TRUE;
297 if (iEntry < (*ppList)->cEntries -1)
298 memcpy (&(*ppList)->aEntries[ iEntry ], &(*ppList)->aEntries[ (*ppList)->cEntries-1 ], sizeof((*ppList)->aEntries[0]));
299 (*ppList)->cEntries --;
303 return fFound;
306 BOOL AfsAdmSvr_IsInObjPropList (LPASOBJPROPLIST *ppList, ASID idObject, LPASOBJPROP pProperties, LPARAM *pParam)
308 if (!ppList || !(*ppList) || !(*ppList)->cEntries)
309 return FALSE;
311 for (ULONG iEntry = 0; iEntry < (*ppList)->cEntries; ++iEntry)
313 if ((*ppList)->aEntries[ iEntry ].ObjectProperties.idObject == idObject)
315 if (pProperties)
316 memcpy (pProperties, &(*ppList)->aEntries[ iEntry ].ObjectProperties, sizeof(ASOBJPROP));
317 if (pParam)
318 *pParam = (*ppList)->aEntries[ iEntry ].lParam;
319 return TRUE;
323 return FALSE;
326 void AfsAdmSvr_FreeObjPropList (LPASOBJPROPLIST *ppList)
328 if (ppList && *ppList)
330 Free (*ppList);
331 (*ppList) = 0;
336 // ASACTIONLIST - Managed type for lists of cell objects
338 LPASACTIONLIST AfsAdmSvr_CreateActionList (void)
340 LPASACTIONLIST pList = NULL;
341 if (!RPC_REALLOC (ASACTIONLIST, pList, aEntries, cEntriesAllocated, 0, cREALLOC_ACTIONLIST))
342 return NULL;
343 return pList;
346 LPASACTIONLIST AfsAdmSvr_CopyActionList (LPASACTIONLIST pListSource)
348 LPASACTIONLIST pList = NULL;
349 if (!RPC_REALLOC (ASACTIONLIST, pList, aEntries, cEntriesAllocated, pListSource->cEntries, cREALLOC_ACTIONLIST))
350 return NULL;
351 if (pList->cEntriesAllocated)
352 memcpy (pList->aEntries, pListSource->aEntries, sizeof(pList->aEntries[0]) * pList->cEntriesAllocated);
353 pList->cEntries = pListSource->cEntries;
354 return pList;
357 BOOL AfsAdmSvr_AddToActionList (LPASACTIONLIST *ppList, LPASACTION pAction)
359 if (!ppList || !*ppList)
360 return FALSE;
362 if (!RPC_REALLOC (ASACTIONLIST, *ppList, aEntries, cEntriesAllocated, (*ppList)->cEntries +1, cREALLOC_OBJPROPLIST))
363 return NULL;
365 memcpy (&(*ppList)->aEntries[ (*ppList)->cEntries ].Action, pAction, sizeof(ASACTION));
366 (*ppList)->cEntries ++;
367 return TRUE;
370 BOOL AfsAdmSvr_RemoveFromActionList (LPASACTIONLIST *ppList, DWORD idAction)
372 if (!ppList || !(*ppList) || !(*ppList)->cEntries)
373 return FALSE;
375 BOOL fFound = FALSE;
376 for (ULONG iEntry = 0; iEntry < (*ppList)->cEntries; )
378 if ((*ppList)->aEntries[ iEntry ].Action.idAction != idAction)
379 iEntry ++;
380 else
382 fFound = TRUE;
383 if (iEntry < (*ppList)->cEntries -1)
384 memcpy (&(*ppList)->aEntries[ iEntry ], &(*ppList)->aEntries[ (*ppList)->cEntries-1 ], sizeof((*ppList)->aEntries[0]));
385 (*ppList)->cEntries --;
389 return fFound;
392 BOOL AfsAdmSvr_IsInActionList (LPASACTIONLIST *ppList, DWORD idAction, LPASACTION pAction)
394 if (!ppList || !(*ppList) || !(*ppList)->cEntries)
395 return FALSE;
397 for (ULONG iEntry = 0; iEntry < (*ppList)->cEntries; ++iEntry)
399 if ((*ppList)->aEntries[ iEntry ].Action.idAction == idAction)
401 if (pAction)
402 memcpy (pAction, &(*ppList)->aEntries[ iEntry ].Action, sizeof(ASACTION));
403 return TRUE;
407 return FALSE;
410 void AfsAdmSvr_FreeActionList (LPASACTIONLIST *ppList)
412 if (ppList && *ppList)
414 Free (*ppList);
415 (*ppList) = 0;