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.

270 lines
5.4 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <limits.h>
  5. #include <windows.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include "error.h"
  9. #include "ntstatus.dbg"
  10. #include "winerror.dbg"
  11. #define HEAP_INCREMENT 100
  12. #define HEAP_FLAGS 0
  13. typedef struct _PAIRLIST {
  14. ULONG Status;
  15. ULONG WinError;
  16. } PAIRLIST, *PPAIRLIST;
  17. PPAIRLIST PairList;
  18. ULONG PairCount;
  19. ULONG MaxPairs;
  20. PUCHAR ProgramName;
  21. void
  22. usage(
  23. void
  24. );
  25. void
  26. error(
  27. PUCHAR String
  28. );
  29. void
  30. ReconstructPairs(
  31. void
  32. );
  33. void
  34. StatusFromWinError(
  35. ULONG WinError
  36. );
  37. PUCHAR
  38. ntsSymbolicName(
  39. NTSTATUS Id
  40. );
  41. PUCHAR
  42. weSymbolicName(
  43. DWORD Id
  44. );
  45. int
  46. __cdecl
  47. main(
  48. int argc,
  49. char **argv
  50. )
  51. {
  52. int i;
  53. BOOL IsWinError;
  54. ULONG WinError;
  55. NTSTATUS Status;
  56. ProgramName = argv[0];
  57. if (argc < 2) {
  58. usage();
  59. }
  60. ReconstructPairs();
  61. //
  62. // parse cmdline
  63. //
  64. IsWinError = TRUE;
  65. for (i = 1; i < argc; i++) {
  66. if (argv[i][0] == '-') {
  67. switch (argv[i][1]) {
  68. case 's':
  69. case 'S':
  70. IsWinError = FALSE;
  71. break;
  72. default:
  73. usage();
  74. break;
  75. }
  76. continue;
  77. } else {
  78. if (IsWinError) {
  79. WinError = strtoul(argv[i], NULL, 0);
  80. StatusFromWinError(WinError);
  81. } else {
  82. Status = strtoul(argv[i], NULL, 16);
  83. printf("%6d %s <--> %08lx %s\n",
  84. RtlNtStatusToDosError(Status),
  85. weSymbolicName(RtlNtStatusToDosError(Status)),
  86. Status,
  87. ntsSymbolicName(Status)
  88. );
  89. }
  90. }
  91. }
  92. return 0;
  93. }
  94. void
  95. StatusFromWinError(
  96. ULONG WinError
  97. )
  98. {
  99. ULONG Index;
  100. BOOL Hit = FALSE;
  101. for (Index = 0; Index < PairCount; Index++) {
  102. if (WinError == PairList[Index].WinError) {
  103. printf("%6d %s <--> 0x%08lx %s\n",
  104. WinError,
  105. weSymbolicName(WinError),
  106. PairList[Index].Status,
  107. ntsSymbolicName(PairList[Index].Status)
  108. );
  109. Hit = TRUE;
  110. }
  111. }
  112. if (!Hit) {
  113. printf("%6d %s <--> No NTSTATUS matched\n",
  114. WinError,
  115. weSymbolicName(WinError)
  116. );
  117. }
  118. }
  119. void
  120. AddPair(
  121. ULONG Status,
  122. ULONG WinError
  123. )
  124. {
  125. if (PairCount >= MaxPairs) {
  126. MaxPairs += HEAP_INCREMENT;
  127. if (PairList == NULL) {
  128. PairList = (PPAIRLIST)RtlAllocateHeap(RtlProcessHeap(),
  129. HEAP_FLAGS,
  130. MaxPairs * sizeof(PAIRLIST)
  131. );
  132. if (PairList == NULL) {
  133. error("out of memory");
  134. }
  135. } else {
  136. PPAIRLIST NewPairList;
  137. NewPairList = (PPAIRLIST)RtlReAllocateHeap(RtlProcessHeap(),
  138. HEAP_FLAGS,
  139. PairList,
  140. MaxPairs * sizeof(PAIRLIST)
  141. );
  142. if (NewPairList == NULL) {
  143. error("out of memory");
  144. } else {
  145. PairList = NewPairList;
  146. }
  147. }
  148. }
  149. PairList[PairCount].Status = Status;
  150. PairList[PairCount].WinError = WinError;
  151. PairCount++;
  152. }
  153. void
  154. ReconstructPairs(
  155. void
  156. )
  157. {
  158. ULONG Index;
  159. ULONG Entry;
  160. ULONG Offset;
  161. ULONG Status;
  162. ULONG WinError;
  163. Index = 0;
  164. for (Entry = 0; RtlpRunTable[Entry].RunLength != 0; Entry++) {
  165. Status = RtlpRunTable[Entry].BaseCode;
  166. for (Offset = 0; Offset < RtlpRunTable[Entry].RunLength; Offset++, Status++ ) {
  167. if (RtlpRunTable[Entry].CodeSize == 1) {
  168. WinError = (ULONG)RtlpStatusTable[Index];
  169. Index += 1;
  170. } else {
  171. WinError = (((ULONG)RtlpStatusTable[Index + 1] << 16) |
  172. (ULONG)RtlpStatusTable[Index]);
  173. Index += 2;
  174. }
  175. AddPair(Status, WinError);
  176. }
  177. }
  178. }
  179. void
  180. usage(
  181. void
  182. )
  183. {
  184. fprintf(stderr,
  185. "usage: %s errorcode ... [-s ntstatus ...]\n",
  186. ProgramName);
  187. ExitProcess(1);
  188. }
  189. void
  190. error(
  191. PUCHAR String
  192. )
  193. {
  194. fprintf(stderr, "%s: %s\n", ProgramName, String);
  195. ExitProcess(2);
  196. }
  197. PUCHAR
  198. ntsSymbolicName(
  199. NTSTATUS Id
  200. )
  201. {
  202. int i = 0;
  203. while (ntstatusSymbolicNames[i].SymbolicName) {
  204. if (ntstatusSymbolicNames[i].MessageId == Id) {
  205. return ntstatusSymbolicNames[i].SymbolicName;
  206. }
  207. ++i;
  208. }
  209. return "No Symbolic Name";
  210. }
  211. PUCHAR
  212. weSymbolicName(
  213. DWORD Id
  214. )
  215. {
  216. int i = 0;
  217. while (winerrorSymbolicNames[i].SymbolicName) {
  218. if (winerrorSymbolicNames[i].MessageId == Id) {
  219. return winerrorSymbolicNames[i].SymbolicName;
  220. }
  221. ++i;
  222. }
  223. return "No Symbolic Name";
  224. }