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.

286 lines
5.5 KiB

  1. /*++
  2. Copyright (c) 1995 Microsoft Corporation
  3. Module Name:
  4. spt.c
  5. Abstract:
  6. A user mode library that allows simple commands to be sent to a
  7. selected scsi device.
  8. Environment:
  9. User mode only
  10. Revision History:
  11. 4/10/2000 - created
  12. --*/
  13. #include "CmdHelpP.h"
  14. static char ValidCharArray[] = {
  15. ' ',
  16. '0', '1', '2', '3',
  17. '4', '5', '6', '7',
  18. '8', '9', 'a', 'b',
  19. 'c', 'd', 'e', 'f',
  20. 'A', 'B', 'C', 'D',
  21. 'E', 'F'
  22. };
  23. #define MaxValidOctalChars ( 9) // space + 8 digits
  24. #define MaxValidDecimalHexChars (11) // + 2 digits
  25. #define MaxValidHexChars (23) // + 12 letters (upper and lower case)
  26. #define MaxValidCharacters (23) // number of safe chars to access
  27. /*++
  28. Routine Description:
  29. Validates a string has valid characters
  30. valid chars are stored in a global static array
  31. --*/
  32. BOOL
  33. PrivateValidateString(
  34. IN PCHAR String,
  35. IN DWORD ValidChars
  36. )
  37. {
  38. if (ValidChars > MaxValidCharacters) {
  39. return FALSE;
  40. }
  41. if (*String == '\0') {
  42. return TRUE; // a NULL string is valid
  43. }
  44. while (*String != '\0') {
  45. DWORD i;
  46. BOOL pass = FALSE;
  47. for (i=0; i<ValidChars; i++) {
  48. if (*String == ValidCharArray[i]) {
  49. pass = TRUE;
  50. break;
  51. }
  52. }
  53. if (!pass) {
  54. return FALSE;
  55. }
  56. String++; // look at next character
  57. }
  58. return TRUE;
  59. }
  60. BOOL
  61. CmdHelpValidateStringHex(
  62. IN PCHAR String
  63. )
  64. {
  65. return PrivateValidateString(String, MaxValidHexChars);
  66. }
  67. BOOL
  68. CmdHelpValidateStringDecimal(
  69. IN PCHAR String
  70. )
  71. {
  72. return PrivateValidateString(String, MaxValidDecimalHexChars);
  73. }
  74. BOOL
  75. CmdHelpValidateStringOctal(
  76. IN PCHAR String
  77. )
  78. {
  79. return PrivateValidateString(String, MaxValidOctalChars);
  80. }
  81. BOOL
  82. CmdHelpValidateStringHexQuoted(
  83. IN PCHAR String
  84. )
  85. {
  86. DWORD i;
  87. if (!PrivateValidateString(String, MaxValidHexChars)) return FALSE;
  88. i=1;
  89. while (*String != '\0') {
  90. if ((*String == ' ') && (i%3)) return FALSE;
  91. if ((*String != ' ') && !(i%3)) return FALSE;
  92. i++; // use next index
  93. String++; // go to next character
  94. }
  95. return TRUE;
  96. }
  97. BOOLEAN
  98. CmdHelpScanQuotedHexString(
  99. IN PUCHAR QuotedHexString,
  100. OUT PUCHAR Data,
  101. OUT PDWORD DataSize
  102. )
  103. {
  104. PUCHAR temp;
  105. DWORD availableSize;
  106. DWORD requiredSize;
  107. DWORD index;
  108. availableSize = *DataSize;
  109. *DataSize = 0;
  110. if (!CmdHelpValidateStringHexQuoted(QuotedHexString)) {
  111. return FALSE;
  112. }
  113. //
  114. // the number format is (number)(number)(space) repeated,
  115. // ending with (number)(number)(null)
  116. // size = 3(n-1) + 2 chars
  117. //
  118. requiredSize = strlen(QuotedHexString);
  119. if (requiredSize % 3 != 2) {
  120. return FALSE;
  121. }
  122. requiredSize /= 3;
  123. requiredSize ++;
  124. //
  125. // cannot set zero bytes of data
  126. //
  127. if (requiredSize == 0) {
  128. return FALSE;
  129. }
  130. //
  131. // validate that we have enough space
  132. //
  133. if (requiredSize > availableSize) {
  134. *DataSize = requiredSize;
  135. return FALSE;
  136. }
  137. //
  138. // the number format is (number)(number)(space) repeated,
  139. // ending with (number)(number)(null)
  140. //
  141. for (index = 0; index < requiredSize; index ++) {
  142. temp = QuotedHexString + (3*index);
  143. if (sscanf(temp, "%x", Data+index) != 1) {
  144. return FALSE;
  145. }
  146. if ((*(temp+0) == '\0') || (*(temp+1) == '\0')) {
  147. // string too short
  148. return FALSE;
  149. }
  150. }
  151. *DataSize = requiredSize;
  152. return TRUE;
  153. }
  154. VOID
  155. CmdHelpUpdatePercentageDisplay(
  156. IN ULONG Numerator,
  157. IN ULONG Denominator
  158. )
  159. {
  160. ULONG percent;
  161. ULONG i;
  162. if (Numerator > Denominator) {
  163. return;
  164. }
  165. // NOTE: Overflow possibility exists for large numerators.
  166. percent = (Numerator * 100) / Denominator;
  167. for (i=0;i<80;i++) {
  168. putchar('\b');
  169. }
  170. printf("Complete: ");
  171. // each block is 2%
  172. // ----=----1----=----2----=----3----=----4----=----5----=----6----=----7----=----8
  173. // Complete: �.....................
  174. for (i=1; i<100; i+=2) {
  175. if (i < percent) {
  176. putchar(178);
  177. } else if (i == percent) {
  178. putchar(177);
  179. } else {
  180. putchar(176);
  181. }
  182. }
  183. printf(" %d%% (%x/%x)", percent, Numerator, Denominator);
  184. }
  185. VOID
  186. CmdHelpPrintBuffer(
  187. IN PUCHAR Buffer,
  188. IN SIZE_T Size
  189. )
  190. {
  191. DWORD offset = 0;
  192. while (Size > 0x10) {
  193. printf( "%08x:"
  194. " %02x %02x %02x %02x %02x %02x %02x %02x"
  195. " %02x %02x %02x %02x %02x %02x %02x %02x"
  196. "\n",
  197. offset,
  198. *(Buffer + 0), *(Buffer + 1), *(Buffer + 2), *(Buffer + 3),
  199. *(Buffer + 4), *(Buffer + 5), *(Buffer + 6), *(Buffer + 7),
  200. *(Buffer + 8), *(Buffer + 9), *(Buffer + 10), *(Buffer + 11),
  201. *(Buffer + 12), *(Buffer + 13), *(Buffer + 14), *(Buffer + 15)
  202. );
  203. Size -= 0x10;
  204. offset += 0x10;
  205. Buffer += 0x10;
  206. }
  207. if (Size != 0) {
  208. DWORD spaceIt;
  209. printf("%08x:", offset);
  210. for (spaceIt = 0; Size != 0; Size--) {
  211. if ((spaceIt%8)==0) {
  212. printf(" "); // extra space every eight chars
  213. }
  214. printf(" %02x", *Buffer);
  215. spaceIt++;
  216. Buffer++;
  217. }
  218. printf("\n");
  219. }
  220. return;
  221. }