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.

279 lines
7.1 KiB

  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <time.h>
  7. #include <direct.h>
  8. #include "crc-32.h"
  9. // length of the string we expect with the /m option
  10. #define RPC_LENGTH 5
  11. // (max) length of the _SUFFIX strings
  12. #define RPC_SUFFIX_LENGTH 3
  13. #define STEPUPFLAG TEXT("_STEPUP_")
  14. #define BASE 'a'
  15. #define DEFAULT_RPC "00000"
  16. #define SELECT_SUFFIX "270"
  17. #define MSDN_SUFFIX "335"
  18. #define RETAIL_SUFFIX "000"
  19. #define OEM_SUFFIX "OEM"
  20. DWORD WINAPI CRC_32(LPBYTE pb, DWORD cb)
  21. {
  22. // CRC-32 algorithm used in PKZip, AUTODIN II, Ethernet, and FDDI
  23. // but xor out (xorot) has been changed from 0xFFFFFFFF to 0 so
  24. // we can store the CRC at the end of the block and expect 0 to be
  25. // the value of the CRC of the resulting block (including the stored
  26. // CRC).
  27. cm_t cmt = {
  28. 32, // cm_width Parameter: Width in bits [8,32].
  29. 0x04C11DB7, // cm_poly Parameter: The algorithm's polynomial.
  30. 0xFFFFFFFF, // cm_init Parameter: Initial register value.
  31. TRUE, // cm_refin Parameter: Reflect input bytes?
  32. TRUE, // cm_refot Parameter: Reflect output CRC?
  33. 0, // cm_xorot Parameter: XOR this to output CRC.
  34. 0 // cm_reg Context: Context during execution.
  35. };
  36. // Documented test case for CRC-32:
  37. // Checking "123456789" should return 0xCBF43926
  38. cm_ini(&cmt);
  39. cm_blk(&cmt, pb, cb);
  40. return cm_crc(&cmt);
  41. }
  42. VOID GiveUsage() {
  43. _tprintf(TEXT("pidinit -d <flags> -g <outputname> -m <mpccode> -s[smro] -z -h -?\n"));
  44. _tprintf(TEXT("writes a signature to <outputname> based on <flags>\n"));
  45. _tprintf(TEXT("-s (SiteType) s (select) m (msdn) r (retail) o (oem)\n"));
  46. _tprintf(TEXT("-z (decode)\n"));
  47. _tprintf(TEXT("-m <mpccode> : mpccode is a 5 digit number\n"));
  48. _tprintf(TEXT("-h or -? this message\n"));
  49. }
  50. int _cdecl
  51. main(
  52. int argc,
  53. char *argvA[]
  54. )
  55. /*++
  56. Routine Description:
  57. Entry point to the setup program
  58. Arguments:
  59. argc - Number of args.
  60. argvA - the commandline arguments.
  61. Return Value:
  62. --*/
  63. {
  64. LPTSTR *argv;
  65. int argcount = 0;
  66. long rslt;
  67. char data[10+4+1] = {0};
  68. char buf[1000] = {0};
  69. DWORD value, crcvalue,outval;//,tmp;
  70. BOOL StepUp = FALSE;
  71. BOOL decode = FALSE;
  72. BOOL bMpc = FALSE;
  73. LPSTR SiteSuffix[] = { SELECT_SUFFIX,
  74. MSDN_SUFFIX,
  75. RETAIL_SUFFIX,
  76. OEM_SUFFIX
  77. };
  78. typedef enum SiteType {
  79. Select = 0,
  80. Msdn,
  81. Retail,
  82. Oem,
  83. } ;
  84. enum SiteType st = Select;
  85. int i, randval;
  86. char *outname = NULL;
  87. char path[MAX_PATH];
  88. char *mpcName = NULL;
  89. char tString[RPC_LENGTH+RPC_SUFFIX_LENGTH+1];
  90. // do commandline stuff
  91. #ifdef UNICODE
  92. argv = CommandLineToArgvW( GetCommandLine(), &argc );
  93. #else
  94. argv = argvA;
  95. #endif
  96. // check for commandline switches
  97. for (argcount=0; argcount<argc; argcount++) {
  98. if ((argv[argcount][0] == L'/') || (argv[argcount][0] == L'-')) {
  99. switch (towlower(argv[argcount][1])) {
  100. case 'd':
  101. if (lstrcmpi(&argv[argcount][2],STEPUPFLAG ) == 0) {
  102. StepUp = TRUE;
  103. }
  104. break;
  105. case 'g':
  106. outname = argv[argcount + 1];
  107. break;
  108. case 's':
  109. switch (towlower(argv[argcount+1][0])) {
  110. case 's':
  111. st = Select;
  112. break;
  113. case 'm':
  114. st = Msdn;
  115. break;
  116. case 'r':
  117. st = Retail;
  118. break;
  119. case 'o':
  120. st = Oem;
  121. break;
  122. default:
  123. st = Select;
  124. break;
  125. }
  126. break;
  127. case 'z':
  128. decode = TRUE;
  129. break;
  130. case 'm':
  131. bMpc = TRUE;
  132. mpcName = argv[argcount + 1];
  133. break;
  134. case 'h':
  135. case '?':
  136. GiveUsage();
  137. return 1;
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. }
  144. if (!outname) {
  145. _tprintf(TEXT("you must supply a filename\n"));
  146. GiveUsage();
  147. return 1;
  148. }
  149. //
  150. // the decode section is really only for testing
  151. //
  152. if (decode) {
  153. _getcwd ( path, MAX_PATH );
  154. sprintf( path, "%s\\%s", path, outname );
  155. GetPrivateProfileStruct( "Pid",
  156. "ExtraData",
  157. data,
  158. 14,
  159. path
  160. );
  161. crcvalue = CRC_32( (LPBYTE)data, 10 );
  162. memcpy(&outval,&data[10],sizeof(DWORD));
  163. if (crcvalue != outval ) {
  164. printf("CRC doesn't match %x %x!\n", crcvalue, outval);
  165. return 1;
  166. }
  167. if ((data[3]-BASE)%2) {
  168. if ((data[5]-BASE)%2) {
  169. printf("stepup mode\n");
  170. return 1;
  171. } else {
  172. printf("bogus!\n");
  173. return -1;
  174. }
  175. } else
  176. if ((data[5]-BASE)%2) {
  177. printf("bogus!\n");
  178. return -1;
  179. } else {
  180. printf("full retail mode\n");
  181. return 1;
  182. }
  183. }
  184. srand( (unsigned)time( NULL ) );
  185. for (i=0;i< 10; i++ ) {
  186. randval = rand() ;
  187. data[i] = BASE + (randval % 26);
  188. }
  189. if (StepUp) {
  190. if (!((data[3]- BASE)%2)) {
  191. data[3] = data[3] + 1;
  192. }
  193. if (!((data[5]- BASE )%2)) {
  194. data[5] = data[5] + 1;
  195. }
  196. } else {
  197. if ((data[3]- BASE)%2) {
  198. data[3] = data[3] + 1;
  199. }
  200. if ((data[5]- BASE)%2) {
  201. data[5] = data[5] + 1;
  202. }
  203. }
  204. //printf( "data : %s\n" , data );
  205. crcvalue = CRC_32( (LPBYTE)data, strlen(data) );
  206. memcpy(&data[10],&crcvalue,4);
  207. _getcwd ( path, MAX_PATH );
  208. sprintf( path, "%s\\%s", path, outname );
  209. WritePrivateProfileStruct( "Pid",
  210. "ExtraData",
  211. data,
  212. 14,
  213. path
  214. );
  215. //
  216. // Allow another to specify the RPC code
  217. //
  218. if (bMpc){
  219. lstrcpyn(tString,mpcName,RPC_LENGTH+1);
  220. } else {
  221. lstrcpyn(tString,DEFAULT_RPC,RPC_LENGTH+1);
  222. }
  223. lstrcpy(&tString[RPC_LENGTH],SiteSuffix[st]);
  224. WritePrivateProfileString( "Pid",
  225. "Pid",
  226. tString,
  227. path
  228. );
  229. return 1;
  230. }