Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
6.7 KiB

  1. //================================================================================
  2. // Copyright (C) 1997 Microsoft Corporation
  3. // Author: RameshV
  4. // Description: implements the basic structures for a list of class defintitions
  5. // ThreadSafe: no
  6. // Locks: none
  7. // Please read stdinfo.txt for programming style.
  8. //================================================================================
  9. #include <mm.h>
  10. #include <array.h>
  11. #include <wchar.h>
  12. #include "classdefl.h"
  13. #include "server\uniqid.h"
  14. //BeginExport(function)
  15. DWORD
  16. MemClassDefListFindClassDefInternal( // dont use this fn outside of classdefl.c
  17. IN PM_CLASSDEFLIST ClassDefList,
  18. IN DWORD ClassId,
  19. IN LPWSTR Name,
  20. IN LPBYTE ActualBytes,
  21. IN DWORD nBytes,
  22. IN LPBOOL pIsVendor,
  23. OUT PARRAY_LOCATION Location
  24. ) //EndExport(function)
  25. {
  26. DWORD Error;
  27. PM_CLASSDEF ThisClassDef;
  28. for( Error = MemArrayInitLoc(&ClassDefList->ClassDefArray, Location)
  29. ; ERROR_FILE_NOT_FOUND != Error ;
  30. Error = MemArrayNextLoc(&ClassDefList->ClassDefArray, Location)
  31. ) {
  32. Require(ERROR_SUCCESS == Error);
  33. Error = MemArrayGetElement(
  34. &ClassDefList->ClassDefArray,
  35. Location,
  36. (LPVOID *)&ThisClassDef
  37. );
  38. Require(ERROR_SUCCESS == Error && ThisClassDef);
  39. if( pIsVendor != NULL && ThisClassDef->IsVendor != *pIsVendor)
  40. continue;
  41. if( ThisClassDef->ClassId == ClassId ) {
  42. return ERROR_SUCCESS;
  43. }
  44. if( nBytes == ThisClassDef->nBytes ) {
  45. if( 0 == memcmp(ActualBytes, ThisClassDef->ActualBytes, nBytes)) {
  46. return ERROR_SUCCESS;
  47. }
  48. }
  49. if( Name && 0 == wcscmp(ThisClassDef->Name, Name) ) {
  50. return ERROR_SUCCESS;
  51. }
  52. }
  53. return ERROR_FILE_NOT_FOUND;
  54. } // MemClassDefListFindClassDefInternal()
  55. DWORD
  56. MemClassDefListDelClassDef(
  57. IN OUT PM_CLASSDEFLIST ClassDefList,
  58. IN DWORD ClassId,
  59. IN LPWSTR Name,
  60. IN LPBYTE ActualBytes,
  61. IN DWORD nBytes
  62. )
  63. {
  64. ARRAY_LOCATION Location;
  65. DWORD Error;
  66. PM_CLASSDEF ThisClassDef;
  67. Error = MemClassDefListFindClassDefInternal(
  68. ClassDefList,
  69. ClassId,
  70. Name,
  71. ActualBytes,
  72. nBytes,
  73. NULL,
  74. &Location
  75. );
  76. if( ERROR_SUCCESS != Error ) return Error;
  77. // Delete this class def from the database
  78. Error = MemArrayGetElement( &ClassDefList->ClassDefArray,
  79. &Location,
  80. &ThisClassDef );
  81. Require( ERROR_SUCCESS == Error );
  82. if ( ERROR_SUCCESS != Error ) {
  83. return Error;
  84. }
  85. Error = DeleteRecord( ThisClassDef->UniqId );
  86. if ( ERROR_SUCCESS != Error ) {
  87. return Error;
  88. }
  89. Error = MemArrayDelElement(
  90. &ClassDefList->ClassDefArray,
  91. &Location,
  92. &ThisClassDef
  93. );
  94. Require(ERROR_SUCCESS == Error && ThisClassDef);
  95. MemFree(ThisClassDef);
  96. return ERROR_SUCCESS;
  97. } // MemClassDefListDelClassDef()
  98. //BeginExport(function)
  99. DWORD
  100. MemClassDefListAddClassDef( // Add or replace option
  101. IN OUT PM_CLASSDEFLIST ClassDefList,
  102. IN DWORD ClassId,
  103. IN BOOL IsVendor,
  104. IN DWORD Type,
  105. IN LPWSTR Name,
  106. IN LPWSTR Comment,
  107. IN LPBYTE ActualBytes,
  108. IN DWORD nBytes,
  109. IN ULONG UniqId
  110. ) //EndExport(function)
  111. {
  112. ARRAY_LOCATION Location;
  113. DWORD Error;
  114. DWORD Size;
  115. PM_CLASSDEF ThisClassDef;
  116. PM_CLASSDEF OldClassDef;
  117. AssertRet(ClassDefList && ClassId && Name && ActualBytes && nBytes, ERROR_INVALID_PARAMETER );
  118. Error = MemClassDefListFindClassDefInternal(
  119. ClassDefList,
  120. ClassId,
  121. Name,
  122. ActualBytes,
  123. nBytes,
  124. &IsVendor,
  125. &Location
  126. );
  127. Size = sizeof(M_CLASSDEF)+nBytes;
  128. Size = ROUND_UP_COUNT(Size, ALIGN_WORST);
  129. Size += (1+wcslen(Name))*sizeof(WCHAR);
  130. if( Comment ) Size += (1+wcslen(Comment))*sizeof(WCHAR);
  131. ThisClassDef = MemAlloc(Size);
  132. if( NULL == ThisClassDef ) return ERROR_NOT_ENOUGH_MEMORY;
  133. ThisClassDef->RefCount = 1;
  134. ThisClassDef->ClassId = ClassId;
  135. ThisClassDef->IsVendor = IsVendor;
  136. ThisClassDef->Type = Type;
  137. ThisClassDef->nBytes = nBytes;
  138. ThisClassDef->ActualBytes = sizeof(M_CLASSDEF) + (LPBYTE)ThisClassDef;
  139. memcpy(ThisClassDef->ActualBytes, ActualBytes, nBytes);
  140. ThisClassDef->Name = (LPWSTR)(ROUND_UP_COUNT(sizeof(M_CLASSDEF)+nBytes, ALIGN_WORST) + (LPBYTE)ThisClassDef);
  141. wcscpy(ThisClassDef->Name, Name);
  142. if( Comment ) {
  143. ThisClassDef->Comment = 1 + wcslen(Name) + ThisClassDef->Name;
  144. wcscpy(ThisClassDef->Comment, Comment);
  145. } else {
  146. ThisClassDef->Comment = NULL;
  147. }
  148. ThisClassDef->UniqId = UniqId;
  149. if( ERROR_SUCCESS == Error ) {
  150. DebugPrint2("Overwriting class definition for class-id 0x%lx\n", ClassId);
  151. Error = MemArrayGetElement(
  152. &ClassDefList->ClassDefArray,
  153. &Location,
  154. (LPVOID *)&OldClassDef
  155. );
  156. Require(ERROR_SUCCESS == Error);
  157. Error = DeleteRecord( OldClassDef->UniqId );
  158. MemFree(OldClassDef);
  159. Require( ERROR_SUCCESS == Error );
  160. if ( ERROR_SUCCESS != Error ) {
  161. return Error;
  162. }
  163. Error = MemArraySetElement(
  164. &ClassDefList->ClassDefArray,
  165. &Location,
  166. ThisClassDef
  167. );
  168. Require(ERROR_SUCCESS == Error);
  169. return Error;
  170. } // if
  171. Error = MemArrayAddElement(
  172. &ClassDefList->ClassDefArray,
  173. ThisClassDef
  174. );
  175. if( ERROR_SUCCESS != Error ) MemFree(ThisClassDef);
  176. return Error;
  177. } // MemClassDefListAddClassDef()
  178. ULONG ClassIdRunningCount = 100;
  179. //BeginExport(function)
  180. DWORD
  181. MemNewClassId(
  182. VOID
  183. ) //EndExport(function)
  184. {
  185. return InterlockedIncrement(&ClassIdRunningCount);
  186. }
  187. //================================================================================
  188. // end of file
  189. //================================================================================