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.

365 lines
15 KiB

  1. /*
  2. * UTIL.C
  3. *
  4. * RSM Service : Utilities
  5. *
  6. * Author: ErvinP
  7. *
  8. * (c) 2001 Microsoft Corporation
  9. *
  10. */
  11. #include <windows.h>
  12. #include <stdlib.h>
  13. #include <wtypes.h>
  14. #include <ntmsapi.h>
  15. #include "internal.h"
  16. #include "resource.h"
  17. #include "debug.h"
  18. /*
  19. * WStrNCpy
  20. *
  21. * Like wcsncpy, but terminates the string if truncated.
  22. * Also, tolerates NULL src string.
  23. */
  24. ULONG WStrNCpy(WCHAR *dest, const WCHAR *src, ULONG maxWChars)
  25. {
  26. ULONG wCharsWritten = 0;
  27. ASSERT(dest);
  28. if (src){
  29. while ((maxWChars-- > 1) && (*dest++ = *src++)){
  30. wCharsWritten++;
  31. }
  32. if (maxWChars == 1){
  33. *dest = (WCHAR)NULL;
  34. wCharsWritten++;
  35. }
  36. }
  37. else {
  38. *dest = (WCHAR)NULL;
  39. wCharsWritten++;
  40. }
  41. return wCharsWritten;
  42. }
  43. /*
  44. * AsciiToWChar
  45. *
  46. * Like mbstowcs, but terminates the string if truncated.
  47. * Also, tolerates NULL ascii string.
  48. */
  49. ULONG AsciiToWChar(WCHAR *dest, const char *src, ULONG maxChars)
  50. {
  51. ULONG charsWritten = 0;
  52. if (src){
  53. while ((maxChars-- > 1) && (*dest++ = (WCHAR)*src++)){
  54. charsWritten++;
  55. }
  56. if (maxChars == 1){
  57. *dest = (WCHAR)NULL;
  58. charsWritten++;
  59. }
  60. }
  61. else {
  62. *dest = (WCHAR)NULL;
  63. charsWritten++;
  64. }
  65. return charsWritten;
  66. }
  67. /*
  68. * WCharToAscii
  69. *
  70. * Reverse of AsciiToWChar.
  71. * Terminates the string if truncated.
  72. * Also, tolerates NULL wchar string.
  73. */
  74. ULONG WCharToAscii(char *dest, WCHAR *src, ULONG maxChars)
  75. {
  76. ULONG charsWritten = 0;
  77. if (src){
  78. while ((maxChars-- > 1) && (*dest++ = (char)(unsigned char)*src++)){
  79. charsWritten++;
  80. }
  81. if (maxChars == 1){
  82. *dest = (char)NULL;
  83. charsWritten++;
  84. }
  85. }
  86. else {
  87. *dest = (char)NULL;
  88. charsWritten++;
  89. }
  90. return charsWritten;
  91. }
  92. /*
  93. * WStringsEqual
  94. *
  95. * Like RtlEqualString (but ntrtl.h doesn't compile with our other headers).
  96. */
  97. BOOL WStringsEqualN(PWCHAR s, PWCHAR p, BOOL caseSensitive, ULONG maxLen)
  98. {
  99. BOOL result = TRUE;
  100. while (maxLen > 0){
  101. if (!*s && !*p){
  102. break;
  103. }
  104. else if (!*s || !*p){
  105. result = FALSE;
  106. break;
  107. }
  108. else if (caseSensitive){
  109. if (*s != *p){
  110. result = FALSE;
  111. break;
  112. }
  113. }
  114. else {
  115. if ((*s | 0x20) != (*p | 0x20)){
  116. result = FALSE;
  117. break;
  118. }
  119. }
  120. s++, p++;
  121. maxLen--;
  122. }
  123. return result;
  124. }
  125. /*
  126. * ConvertObjectInfoAToWChar
  127. *
  128. * Just converts the NTMS_OBJECTINFORMATION structure and all its parts
  129. * from ascii to wide-char.
  130. */
  131. VOID ConvertObjectInfoAToWChar(LPNTMS_OBJECTINFORMATIONW wObjInfo, LPNTMS_OBJECTINFORMATIONA aObjInfo)
  132. {
  133. wObjInfo->dwSize = aObjInfo->dwSize;
  134. wObjInfo->dwType = aObjInfo->dwType;
  135. wObjInfo->Created = aObjInfo->Created;
  136. wObjInfo->Modified = aObjInfo->Modified;
  137. wObjInfo->ObjectGuid = aObjInfo->ObjectGuid;
  138. wObjInfo->Enabled = aObjInfo->Enabled;
  139. wObjInfo->dwOperationalState = aObjInfo->dwOperationalState;
  140. AsciiToWChar(wObjInfo->szName, aObjInfo->szName, NTMS_OBJECTNAME_LENGTH);
  141. AsciiToWChar(wObjInfo->szDescription, aObjInfo->szDescription, NTMS_OBJECTNAME_LENGTH);
  142. /*
  143. * Convert the Union portion of the structure, based on its 'union' type.
  144. */
  145. switch (aObjInfo->dwType){
  146. case NTMS_DRIVE:
  147. {
  148. NTMS_DRIVEINFORMATIONA *driveInfoA = &aObjInfo->Info.Drive;
  149. NTMS_DRIVEINFORMATIONW *driveInfoW = &wObjInfo->Info.Drive;
  150. driveInfoW->Number = driveInfoA->Number;
  151. driveInfoW->State = driveInfoA->State;
  152. driveInfoW->DriveType = driveInfoA->DriveType;
  153. AsciiToWChar(driveInfoW->szDeviceName, driveInfoA->szDeviceName, sizeof(driveInfoA->szDeviceName));
  154. AsciiToWChar(driveInfoW->szSerialNumber, driveInfoA->szSerialNumber, sizeof(driveInfoA->szSerialNumber));
  155. AsciiToWChar(driveInfoW->szRevision, driveInfoA->szRevision, sizeof(driveInfoA->szRevision));
  156. driveInfoW->ScsiPort = driveInfoA->ScsiPort;
  157. driveInfoW->ScsiBus = driveInfoA->ScsiBus;
  158. driveInfoW->ScsiTarget = driveInfoA->ScsiTarget;
  159. driveInfoW->ScsiLun = driveInfoA->ScsiLun;
  160. driveInfoW->dwMountCount = driveInfoA->dwMountCount;
  161. driveInfoW->LastCleanedTs = driveInfoA->LastCleanedTs;
  162. driveInfoW->SavedPartitionId = driveInfoA->SavedPartitionId;
  163. driveInfoW->Library = driveInfoA->Library;
  164. driveInfoW->Reserved = driveInfoA->Reserved;
  165. driveInfoW->dwDeferDismountDelay = driveInfoA->dwDeferDismountDelay;
  166. }
  167. break;
  168. case NTMS_DRIVE_TYPE:
  169. {
  170. NTMS_DRIVETYPEINFORMATIONA *driveTypeInfoA = &aObjInfo->Info.DriveType;
  171. NTMS_DRIVETYPEINFORMATIONW *driveTypeInfoW = &wObjInfo->Info.DriveType;
  172. AsciiToWChar(driveTypeInfoW->szVendor, driveTypeInfoA->szVendor, sizeof(driveTypeInfoA->szVendor));
  173. AsciiToWChar(driveTypeInfoW->szProduct, driveTypeInfoA->szProduct, sizeof(driveTypeInfoA->szVendor));
  174. driveTypeInfoW->NumberOfHeads = driveTypeInfoA->NumberOfHeads;
  175. driveTypeInfoW->DeviceType = driveTypeInfoA->DeviceType;
  176. }
  177. break;
  178. case NTMS_LIBRARY:
  179. {
  180. NTMS_LIBRARYINFORMATION *libInfoA = &aObjInfo->Info.Library;
  181. NTMS_LIBRARYINFORMATION *libInfoW = &wObjInfo->Info.Library;
  182. *libInfoW = *libInfoA;
  183. }
  184. break;
  185. case NTMS_CHANGER:
  186. {
  187. NTMS_CHANGERINFORMATIONA *changerInfoA = &aObjInfo->Info.Changer;
  188. NTMS_CHANGERINFORMATIONW *changerInfoW = &wObjInfo->Info.Changer;
  189. changerInfoW->Number = changerInfoA->Number;
  190. changerInfoW->ChangerType = changerInfoA->ChangerType;
  191. AsciiToWChar(changerInfoW->szSerialNumber, changerInfoA->szSerialNumber, sizeof(changerInfoA->szSerialNumber));
  192. AsciiToWChar(changerInfoW->szRevision, changerInfoA->szRevision, sizeof(changerInfoA->szRevision));
  193. AsciiToWChar(changerInfoW->szDeviceName, changerInfoA->szDeviceName, sizeof(changerInfoA->szDeviceName));
  194. changerInfoW->ScsiPort = changerInfoA->ScsiPort;
  195. changerInfoW->ScsiBus = changerInfoA->ScsiBus;
  196. changerInfoW->ScsiTarget = changerInfoA->ScsiTarget;
  197. changerInfoW->ScsiLun = changerInfoA->ScsiLun;
  198. changerInfoW->Library = changerInfoA->Library;
  199. }
  200. break;
  201. case NTMS_CHANGER_TYPE:
  202. {
  203. NTMS_CHANGERTYPEINFORMATIONA *changerTypeInfoA = &aObjInfo->Info.ChangerType;
  204. NTMS_CHANGERTYPEINFORMATIONW *changerTypeInfoW = &wObjInfo->Info.ChangerType;
  205. AsciiToWChar(changerTypeInfoW->szVendor, changerTypeInfoA->szVendor, sizeof(changerTypeInfoA->szVendor));
  206. AsciiToWChar(changerTypeInfoW->szProduct, changerTypeInfoA->szProduct, sizeof(changerTypeInfoA->szProduct));
  207. changerTypeInfoW->DeviceType = changerTypeInfoA->DeviceType;
  208. }
  209. break;
  210. case NTMS_STORAGESLOT:
  211. {
  212. NTMS_STORAGESLOTINFORMATION *slotInfoA = &aObjInfo->Info.StorageSlot;
  213. NTMS_STORAGESLOTINFORMATION *slotInfoW = &wObjInfo->Info.StorageSlot;
  214. *slotInfoW = *slotInfoA;
  215. }
  216. break;
  217. case NTMS_IEDOOR:
  218. {
  219. NTMS_IEDOORINFORMATION *ieDoorA = &aObjInfo->Info.IEDoor;
  220. NTMS_IEDOORINFORMATION *ieDoorW = &wObjInfo->Info.IEDoor;
  221. *ieDoorW = *ieDoorA;
  222. }
  223. break;
  224. case NTMS_IEPORT:
  225. {
  226. NTMS_IEPORTINFORMATION *iePortA = &aObjInfo->Info.IEPort;
  227. NTMS_IEPORTINFORMATION *iePortW = &wObjInfo->Info.IEPort;
  228. *iePortW = *iePortA;
  229. }
  230. break;
  231. case NTMS_PHYSICAL_MEDIA:
  232. {
  233. NTMS_PMIDINFORMATIONA *physMediaA = &aObjInfo->Info.PhysicalMedia;
  234. NTMS_PMIDINFORMATIONW *physMediaW = &wObjInfo->Info.PhysicalMedia;
  235. physMediaW->CurrentLibrary = physMediaA->CurrentLibrary;
  236. physMediaW->MediaPool = physMediaA->MediaPool;
  237. physMediaW->Location = physMediaA->Location;
  238. physMediaW->LocationType = physMediaA->LocationType;
  239. physMediaW->MediaType = physMediaA->MediaType;
  240. physMediaW->HomeSlot = physMediaA->HomeSlot;
  241. AsciiToWChar(physMediaW->szBarCode, physMediaA->szBarCode, sizeof(physMediaA->szBarCode));
  242. physMediaW->BarCodeState = physMediaA->BarCodeState;
  243. AsciiToWChar(physMediaW->szSequenceNumber, physMediaA->szSequenceNumber, sizeof(physMediaA->szSequenceNumber));
  244. physMediaW->MediaState = physMediaA->MediaState;
  245. physMediaW->dwNumberOfPartitions = physMediaA->dwNumberOfPartitions;
  246. physMediaW->dwMediaTypeCode = physMediaA->dwMediaTypeCode;
  247. physMediaW->dwDensityCode = physMediaA->dwDensityCode;
  248. physMediaW->MountedPartition = physMediaA->MountedPartition;
  249. }
  250. break;
  251. case NTMS_LOGICAL_MEDIA:
  252. {
  253. NTMS_LMIDINFORMATION *logMediaA = &aObjInfo->Info.LogicalMedia;
  254. NTMS_LMIDINFORMATION *logMediaW = &wObjInfo->Info.LogicalMedia;
  255. *logMediaW = *logMediaA;
  256. }
  257. break;
  258. case NTMS_PARTITION:
  259. {
  260. NTMS_PARTITIONINFORMATIONA *partitionInfoA = &aObjInfo->Info.Partition;
  261. NTMS_PARTITIONINFORMATIONW *partitionInfoW = &wObjInfo->Info.Partition;
  262. memcpy(&partitionInfoW->PhysicalMedia, &partitionInfoA->PhysicalMedia, sizeof(partitionInfoA->PhysicalMedia));
  263. memcpy(&partitionInfoW->LogicalMedia, &partitionInfoA->LogicalMedia, sizeof(partitionInfoA->LogicalMedia));
  264. partitionInfoW->State = partitionInfoA->State;
  265. partitionInfoW->Side = partitionInfoA->Side;
  266. partitionInfoW->dwOmidLabelIdLength = partitionInfoA->dwOmidLabelIdLength;
  267. memcpy(partitionInfoW->OmidLabelId, partitionInfoA->OmidLabelId, sizeof(partitionInfoA->OmidLabelId));
  268. AsciiToWChar(partitionInfoW->szOmidLabelType, partitionInfoA->szOmidLabelType, sizeof(partitionInfoA->szOmidLabelType));
  269. AsciiToWChar(partitionInfoW->szOmidLabelInfo, partitionInfoA->szOmidLabelInfo, sizeof(partitionInfoA->szOmidLabelInfo));
  270. partitionInfoW->dwMountCount = partitionInfoA->dwMountCount;
  271. partitionInfoW->dwAllocateCount = partitionInfoA->dwAllocateCount;
  272. partitionInfoW->Capacity = partitionInfoA->Capacity;
  273. }
  274. break;
  275. case NTMS_MEDIA_POOL:
  276. {
  277. NTMS_MEDIAPOOLINFORMATION *mediaPoolInfoA = &aObjInfo->Info.MediaPool;
  278. NTMS_MEDIAPOOLINFORMATION *mediaPoolInfoW = &wObjInfo->Info.MediaPool;
  279. *mediaPoolInfoW = *mediaPoolInfoA;
  280. }
  281. break;
  282. case NTMS_MEDIA_TYPE:
  283. {
  284. NTMS_MEDIATYPEINFORMATION *mediaTypeInfoA = &aObjInfo->Info.MediaType;
  285. NTMS_MEDIATYPEINFORMATION *mediaTypeInfoW = &wObjInfo->Info.MediaType;
  286. *mediaTypeInfoW = *mediaTypeInfoA;
  287. }
  288. break;
  289. case NTMS_LIBREQUEST:
  290. {
  291. NTMS_LIBREQUESTINFORMATIONA *libReqInfoA = &aObjInfo->Info.LibRequest;
  292. NTMS_LIBREQUESTINFORMATIONW *libReqInfoW = &wObjInfo->Info.LibRequest;
  293. libReqInfoW->OperationCode = libReqInfoA->OperationCode;
  294. libReqInfoW->OperationOption = libReqInfoA->OperationOption;
  295. libReqInfoW->State = libReqInfoA->State;
  296. memcpy(&libReqInfoW->PartitionId, &libReqInfoA->PartitionId, sizeof(NTMS_GUID));
  297. memcpy(&libReqInfoW->DriveId, &libReqInfoA->DriveId, sizeof(NTMS_GUID));
  298. memcpy(&libReqInfoW->PhysMediaId, &libReqInfoA->PhysMediaId, sizeof(NTMS_GUID));
  299. memcpy(&libReqInfoW->Library, &libReqInfoA->Library, sizeof(NTMS_GUID));
  300. memcpy(&libReqInfoW->SlotId, &libReqInfoA->SlotId, sizeof(NTMS_GUID));
  301. libReqInfoW->TimeQueued = libReqInfoA->TimeQueued;
  302. libReqInfoW->TimeCompleted = libReqInfoA->TimeCompleted;
  303. AsciiToWChar(libReqInfoW->szApplication, libReqInfoA->szApplication, sizeof(libReqInfoA->szApplication));
  304. AsciiToWChar(libReqInfoW->szUser, libReqInfoA->szUser, sizeof(libReqInfoA->szUser));
  305. AsciiToWChar(libReqInfoW->szComputer, libReqInfoA->szComputer, sizeof(libReqInfoA->szComputer));
  306. libReqInfoW->dwErrorCode = libReqInfoA->dwErrorCode;
  307. libReqInfoW->WorkItemId = libReqInfoA->WorkItemId;
  308. libReqInfoW->dwPriority = libReqInfoA->dwPriority;
  309. }
  310. break;
  311. case NTMS_OPREQUEST:
  312. {
  313. NTMS_OPREQUESTINFORMATIONA *opReqInfoA = &aObjInfo->Info.OpRequest;
  314. NTMS_OPREQUESTINFORMATIONW *opReqInfoW = &wObjInfo->Info.OpRequest;
  315. opReqInfoW->Request = opReqInfoA->Request;
  316. opReqInfoW->Submitted = opReqInfoA->Submitted;
  317. opReqInfoW->State = opReqInfoA->State;
  318. AsciiToWChar(opReqInfoW->szMessage, opReqInfoA->szMessage, sizeof(opReqInfoA->szMessage));
  319. opReqInfoW->Arg1Type = opReqInfoA->Arg1Type;
  320. memcpy(&opReqInfoW->Arg1, &opReqInfoA->Arg1, sizeof(NTMS_GUID));
  321. opReqInfoW->Arg2Type = opReqInfoA->Arg2Type;
  322. memcpy(&opReqInfoW->Arg2, &opReqInfoA->Arg2, sizeof(NTMS_GUID));
  323. AsciiToWChar(opReqInfoW->szApplication, opReqInfoA->szApplication, sizeof(opReqInfoA->szApplication));
  324. AsciiToWChar(opReqInfoW->szUser, opReqInfoA->szUser, sizeof(opReqInfoA->szUser));
  325. AsciiToWChar(opReqInfoW->szComputer, opReqInfoA->szComputer, sizeof(opReqInfoA->szComputer));
  326. }
  327. break;
  328. case NTMS_COMPUTER:
  329. {
  330. NTMS_COMPUTERINFORMATION *compInfoA = &aObjInfo->Info.Computer;
  331. NTMS_COMPUTERINFORMATION *compInfoW = &wObjInfo->Info.Computer;
  332. *compInfoW = *compInfoA;
  333. }
  334. break;
  335. default:
  336. DBGERR(("ConvertObjectInfoToWChar: unrecognized dwType"));
  337. break;
  338. }
  339. }