Windows NT 4.0 source code leak
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.

384 lines
7.8 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. UMisc.C
  5. Abstract:
  6. Helper functions for OR test applications.
  7. Author:
  8. Mario Goertzel [mariogo] Apr-23-95
  9. Revision History:
  10. --*/
  11. #include <or.hxx>
  12. #include <stdio.h>
  13. #include <umisc.h>
  14. // Copied from string.cxx
  15. // Types
  16. struct PROTSEQ_MAPPING {
  17. USHORT iProtseqId;
  18. USHORT *pwszProtseq;
  19. };
  20. // Globals
  21. static const PROTSEQ_MAPPING
  22. Mappings[] =
  23. {
  24. { 0x00, 0 },
  25. { 0x01, L"mswmsg" },
  26. { 0x02, 0 },
  27. { 0x03, 0 },
  28. { 0x04, L"ncacn_dnet_dsp" },
  29. { 0x05, 0 },
  30. { 0x06, 0 },
  31. { 0x07, L"ncacn_ip_tcp" },
  32. { 0x08, L"ncadg_ip_udp" },
  33. { 0x09, L"ncacn_nb_tcp" },
  34. { 0x0A, 0 },
  35. { 0x0B, 0 },
  36. { 0x0C, L"ncacn_spx" },
  37. { 0x0D, L"ncacn_nb_ipx"},
  38. { 0x0E, L"ncadg_ipx" },
  39. { 0x0F, L"ncacn_np"},
  40. { 0x10, L"ncalrpc"},
  41. { 0x11, 0},
  42. { 0x12, L"ncacn_nb_nb"},
  43. };
  44. #define PROTSEQ_IDS (sizeof(Mappings)/sizeof(PROTSEQ_MAPPING) - 1)
  45. // Public functions
  46. PWSTR
  47. GetProtseq(
  48. IN USHORT ProtseqId
  49. )
  50. {
  51. ASSERT(ProtseqId != 0);
  52. if (ProtseqId > PROTSEQ_IDS)
  53. {
  54. return 0;
  55. }
  56. wchar_t *pwszProtseq = Mappings[ProtseqId].pwszProtseq;
  57. ASSERT(ProtseqId == Mappings[ProtseqId].iProtseqId);
  58. return(pwszProtseq);
  59. }
  60. USHORT
  61. GetProtseqId(
  62. IN PWSTR pwszProtseq
  63. )
  64. {
  65. ASSERT(pwszProtseq);
  66. for (USHORT i = 0; i <= PROTSEQ_IDS; i++)
  67. {
  68. if ( Mappings[i].pwszProtseq
  69. && 0 == wcscmp(Mappings[i].pwszProtseq, pwszProtseq) )
  70. {
  71. ASSERT(Mappings[i].iProtseqId == i);
  72. return(i);
  73. }
  74. }
  75. return(0);
  76. }
  77. #if 0
  78. // Construct/Transform various STRINGARRAYS
  79. ORSTATUS ConvertStringArray(
  80. IN STRINGARRAY *psa,
  81. OUT STRINGARRAY **ppsaNew,
  82. IN BOOL fHasIds
  83. )
  84. /* ++
  85. Parameters
  86. psa - Original array in compressed or regular form.
  87. Note: Maybe written to and restored during copy.
  88. ppsaNew - Will contain the new compressed or regular
  89. form of psa.
  90. fHasIds - If TRUE, psa is assumed to be compressed and
  91. ppsaNew will contain the regular (non-compressed) version.
  92. If FALSE, psa is assume to be regular and ppsaNew is compressed.
  93. -- */
  94. {
  95. int i, size;
  96. USHORT *p1, *p2, *p3;
  97. PWSTR pwstr;
  98. // Compute size
  99. size = 1; // final null terminator.
  100. p1 = psa->awszStringArray;
  101. if (*p1 == 0)
  102. {
  103. // ASSERT(psa->size == 2); // bogus for padded (0 mod 8) arrays
  104. size = 2; // two null terminators ONLY.
  105. }
  106. while(*p1)
  107. {
  108. int sizeT = wcslen(p1);
  109. if (fHasIds == TRUE)
  110. {
  111. pwstr = GetProtseq(*p1);
  112. if (pwstr != 0)
  113. {
  114. size += sizeT + wcslen(pwstr) + 1;
  115. }
  116. else
  117. {
  118. // Except for interop with future platforms, this
  119. // should not be hit.
  120. ASSERT(pwstr);
  121. // This string will be thrown away from the result.
  122. }
  123. }
  124. else
  125. {
  126. p2 = wcschr(p1, L':'); // ':' is not valid in protseq.
  127. if (p2)
  128. {
  129. size += sizeT + 1 - (p2 - p1); // proseq len (p2 - p1) become 1 for Id.
  130. }
  131. else
  132. {
  133. ASSERT(p2);
  134. }
  135. }
  136. p1 = wcschr(p1, 0);
  137. ASSERT(*p1 == 0);
  138. p1++; // Start of next string or final NULL.
  139. }
  140. *ppsaNew = (STRINGARRAY *)MIDL_user_allocate(sizeof(STRINGARRAY) + size * sizeof(USHORT));
  141. if (0 == *ppsaNew)
  142. {
  143. return(OR_NOMEM);
  144. }
  145. (*ppsaNew)->size = size;
  146. p3 = (*ppsaNew)->awszStringArray;
  147. *p3 = 0;
  148. p1 = psa->awszStringArray;
  149. if (*p1 == 0)
  150. {
  151. // Two null terminators only.
  152. ASSERT(size == 2);
  153. *(p3 + 1) = 0;
  154. return(0);
  155. }
  156. while(*p1)
  157. {
  158. if (fHasIds == TRUE)
  159. {
  160. pwstr = GetProtseq(*p1);
  161. p1++;
  162. if (pwstr != 0)
  163. {
  164. wcscpy(p3, pwstr);
  165. wcscat(p3, L":");
  166. wcscat(p3, p1);
  167. // Move p3 to start of next string (if any);
  168. p3 = wcschr(p3, 0);
  169. p3++;
  170. }
  171. else
  172. {
  173. // String not used, don't know protseq.
  174. // Would ASSERT during sizing.
  175. }
  176. }
  177. else
  178. {
  179. // Must change the protseq to a protseq ID.
  180. p2 = wcschr(p1, L':');
  181. if (p2)
  182. {
  183. *p2 = 0;
  184. *p3 = GetProtseqId(p1);
  185. *p2 = L':';
  186. if (*p3 != 0)
  187. {
  188. p3++;
  189. p1 = p2 + 1; // Just after ':'
  190. wcscpy(p3, p1);
  191. // Move p3 to start of next string (if any)
  192. p3 = wcschr(p3, 0);
  193. p3++;
  194. }
  195. }
  196. }
  197. p1 = wcschr(p1, 0);
  198. ASSERT(*p1 == 0);
  199. p1++; // Start of next string or final NULL.
  200. }
  201. // Second terminator, p3 already points to it.
  202. *p3 = 0;
  203. return(OR_OK);
  204. }
  205. #endif
  206. void StringArrayEqual(DUALSTRINGARRAY *pa1, DUALSTRINGARRAY *pa2)
  207. {
  208. wchar_t *p1, *p2;
  209. EQUAL(pa1->wNumEntries, pa2->wNumEntries);
  210. p1 = pa1->aStringArray;
  211. while(*p1)
  212. {
  213. p2 = pa2->aStringArray;
  214. while(*p2)
  215. {
  216. if (wcscmp(p1, p2) == 0)
  217. {
  218. break;
  219. }
  220. // Try next string.
  221. while (*p2)
  222. {
  223. p2++;
  224. }
  225. if (*(p2 + 1) == 0)
  226. {
  227. // End of array, didn't find it.
  228. EQUAL(0,1);
  229. return;
  230. }
  231. }
  232. // Next string
  233. while(*p1)
  234. {
  235. p1++;
  236. }
  237. }
  238. return;
  239. }
  240. void UuidsEqual(UUID *p, UUID *p2)
  241. {
  242. EQUAL(memcmp(p, p2, sizeof(UUID)), 0);
  243. }
  244. void PrintDualStringArray(
  245. IN PSZ pszComment,
  246. IN DUALSTRINGARRAY *pdsaIn,
  247. IN BOOL fCompressed
  248. )
  249. {
  250. RPC_STATUS status;
  251. DUALSTRINGARRAY *pdsa = pdsaIn;
  252. PrintToConsole("%s: dual string array of %d words:\n", pszComment, pdsa->wNumEntries);
  253. PrintToConsole("\tString Bindings:\n");
  254. PWSTR pProtseq;
  255. PWSTR pT = pdsa->aStringArray;
  256. while(*pT != 0)
  257. {
  258. if (fCompressed)
  259. {
  260. pProtseq = GetProtseq(*pT);
  261. PrintToConsole("\t%S:%S\n", pProtseq, pT + 1);
  262. }
  263. else
  264. {
  265. PrintToConsole("\t%S\n", pT);
  266. }
  267. pT = wcschr(pT, 0);
  268. pT++;
  269. }
  270. PrintToConsole("\t0\n");
  271. PrintToConsole("\tSecurity Bindings:\n");
  272. pT = &pdsa->aStringArray[pdsa->wSecurityOffset];
  273. while(*pT != 0)
  274. {
  275. PrintToConsole("\tAuthn %i, Authz %i, Principal %S\n", pT[0], pT[1], &pT[2]);
  276. pT = wcschr(pT, 0);
  277. pT++;
  278. }
  279. PrintToConsole("\t0\n");
  280. }
  281. void PrintSid(SID *psid)
  282. {
  283. BYTE *p;
  284. int i;
  285. __int64 idauth = 0;
  286. printf("\tS-%u", psid->Revision);
  287. p = (PBYTE) GetSidIdentifierAuthority(psid);
  288. for (i = 0; i < 6; i++, p++ )
  289. {
  290. *(((PBYTE)&idauth) + 5 - i) = *p;
  291. }
  292. printf("-%u", (DWORD)idauth);
  293. p = GetSidSubAuthorityCount(psid);
  294. for(i = 0; i < *p; i++)
  295. {
  296. PDWORD pdw;
  297. pdw = GetSidSubAuthority(psid, i);
  298. printf("-%u", *pdw);
  299. }
  300. printf("\n");
  301. return;
  302. }
  303. LPVOID __RPC_USER MIDL_user_allocate(UINT size)
  304. {
  305. return(HeapAlloc(GetProcessHeap(), 0, size));
  306. }
  307. void __RPC_USER MIDL_user_free(LPVOID p)
  308. {
  309. HeapFree(GetProcessHeap(), 0, p);
  310. }