Source code of Windows XP (NT5)
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.

264 lines
4.9 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. } else {
  133. PairList = (PPAIRLIST)RtlReAllocateHeap(RtlProcessHeap(),
  134. HEAP_FLAGS,
  135. PairList,
  136. MaxPairs * sizeof(PAIRLIST)
  137. );
  138. }
  139. if (PairList == NULL) {
  140. error("out of memory");
  141. }
  142. }
  143. PairList[PairCount].Status = Status;
  144. PairList[PairCount].WinError = WinError;
  145. PairCount++;
  146. }
  147. void
  148. ReconstructPairs(
  149. void
  150. )
  151. {
  152. ULONG Index;
  153. ULONG Entry;
  154. ULONG Offset;
  155. ULONG Status;
  156. ULONG WinError;
  157. Index = 0;
  158. for (Entry = 0; RtlpRunTable[Entry].RunLength != 0; Entry++) {
  159. Status = RtlpRunTable[Entry].BaseCode;
  160. for (Offset = 0; Offset < RtlpRunTable[Entry].RunLength; Offset++, Status++ ) {
  161. if (RtlpRunTable[Entry].CodeSize == 1) {
  162. WinError = (ULONG)RtlpStatusTable[Index];
  163. Index += 1;
  164. } else {
  165. WinError = (((ULONG)RtlpStatusTable[Index + 1] << 16) |
  166. (ULONG)RtlpStatusTable[Index]);
  167. Index += 2;
  168. }
  169. AddPair(Status, WinError);
  170. }
  171. }
  172. }
  173. void
  174. usage(
  175. void
  176. )
  177. {
  178. fprintf(stderr,
  179. "usage: %s errorcode ... [-s ntstatus ...]\n",
  180. ProgramName);
  181. ExitProcess(1);
  182. }
  183. void
  184. error(
  185. PUCHAR String
  186. )
  187. {
  188. fprintf(stderr, "%s: %s\n", ProgramName, String);
  189. ExitProcess(2);
  190. }
  191. PUCHAR
  192. ntsSymbolicName(
  193. NTSTATUS Id
  194. )
  195. {
  196. int i = 0;
  197. while (ntstatusSymbolicNames[i].SymbolicName) {
  198. if (ntstatusSymbolicNames[i].MessageId == Id) {
  199. return ntstatusSymbolicNames[i].SymbolicName;
  200. }
  201. ++i;
  202. }
  203. return "No Symbolic Name";
  204. }
  205. PUCHAR
  206. weSymbolicName(
  207. DWORD Id
  208. )
  209. {
  210. int i = 0;
  211. while (winerrorSymbolicNames[i].SymbolicName) {
  212. if (winerrorSymbolicNames[i].MessageId == Id) {
  213. return winerrorSymbolicNames[i].SymbolicName;
  214. }
  215. ++i;
  216. }
  217. return "No Symbolic Name";
  218. }