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.

150 lines
3.6 KiB

  1. #include <stdlib.h>
  2. #include "textmode.h"
  3. #include "spcab.h"
  4. const PRTL_ALLOCATE_STRING_ROUTINE RtlAllocateStringRoutine = SpMemAlloc;
  5. const PRTL_FREE_STRING_ROUTINE RtlFreeStringRoutine = SpMemFree;
  6. WCHAR TemporaryBuffer[16384];
  7. PWSTR SpCabDupStringW(PCWSTR s)
  8. {
  9. if (s == NULL)
  10. return NULL;
  11. else
  12. {
  13. PWSTR q = (PWSTR)RtlAllocateHeap(RtlProcessHeap(), 0, (wcslen(s) + 1) * sizeof(*q));
  14. if (q)
  15. wcscpy(q, s);
  16. return q;
  17. }
  18. }
  19. PVOID SpMemAlloc(SIZE_T n)
  20. {
  21. return RtlAllocateHeap(RtlProcessHeap(), 0, n);
  22. }
  23. VOID SpMemFree(PVOID p)
  24. {
  25. RtlFreeHeap(RtlProcessHeap(), 0, p);
  26. }
  27. PVOID
  28. SpMemRealloc(
  29. IN PVOID Block,
  30. IN SIZE_T NewSize
  31. )
  32. {
  33. return RtlReAllocateHeap(RtlProcessHeap(), 0, Block, NewSize);
  34. }
  35. int g_argi = 0;
  36. void CabTestCreateW(int argc, wchar_t** argv)
  37. {
  38. CCABHANDLE CabHandle;
  39. wchar_t** LocalArgv = argv + g_argi;
  40. CabHandle = SpCabCreateCabinetW(LocalArgv[1], (LocalArgv[1][0] != 0) ? LocalArgv[2] : NULL, (LocalArgv[3][0] != 0) ? LocalArgv[3] : NULL, 0);
  41. SpCabAddFileToCabinetW(CabHandle, LocalArgv[4], (LocalArgv[5] != NULL && LocalArgv[5][0] != 0) ? LocalArgv[5] : LocalArgv[4]);
  42. SpCabFlushAndCloseCabinetEx(CabHandle, NULL, NULL, NULL, NULL);
  43. g_argi += 4;
  44. g_argi += (LocalArgv[5] != NULL);
  45. /*
  46. SpCabCreateCabinetExW(0,0);
  47. SpCabOpenCabinetW(0);
  48. SpCabExtractAllFilesExW(0,0,0);
  49. SpCabCloseCabinetW(0);
  50. SpCabCreateCabinetA(0,0,0,0);
  51. SpCabCreateCabinetExA(0,0);
  52. SpCabOpenCabinetA(0);
  53. SpCabExtractAllFilesExA(0,0,0);
  54. SpCabCloseCabinetA(0);
  55. */
  56. }
  57. BOOL CabTestExtractFileCallbackW(PCWSTR File)
  58. {
  59. printf("extracting %ls\n", File);
  60. return TRUE;
  61. }
  62. BOOL CabTestExtractFileCallbackA(PCSTR File)
  63. {
  64. printf("extracting %s\n", File);
  65. return TRUE;
  66. }
  67. void CabTestExtractW(int argc, wchar_t** argv)
  68. {
  69. CCABHANDLE CabHandle;
  70. wchar_t** LocalArgv = argv + g_argi;
  71. CabHandle = SpCabOpenCabinetW(LocalArgv[1]);
  72. SpCabExtractAllFilesExW(CabHandle, LocalArgv[2], CabTestExtractFileCallbackW);
  73. SpCabCloseCabinet(CabHandle);
  74. g_argi += 2;
  75. }
  76. void CabTestExtractA(int argc, char** argv)
  77. {
  78. CCABHANDLE CabHandle;
  79. char** LocalArgv = argv + g_argi;
  80. CabHandle = SpCabOpenCabinetA(LocalArgv[1]);
  81. SpCabExtractAllFilesExA(CabHandle, LocalArgv[2], CabTestExtractFileCallbackA);
  82. SpCabCloseCabinet(CabHandle);
  83. g_argi += 2;
  84. }
  85. void CabTestCreateA(int argc, char** argv)
  86. {
  87. CCABHANDLE CabHandle;
  88. char** LocalArgv = argv + g_argi;
  89. CabHandle = SpCabCreateCabinetA(LocalArgv[1], (LocalArgv[1][0] != 0) ? LocalArgv[2] : NULL, (LocalArgv[3][0] != 0) ? LocalArgv[3] : NULL, 0);
  90. SpCabAddFileToCabinetA(CabHandle, LocalArgv[4], (LocalArgv[5] != NULL && LocalArgv[5][0] != 0) ? LocalArgv[5] : LocalArgv[4]);
  91. SpCabFlushAndCloseCabinetEx(CabHandle, NULL, NULL, NULL, NULL);
  92. g_argi += 4;
  93. g_argi += (LocalArgv[5] != NULL);
  94. }
  95. void MainA(int argc, char** argv)
  96. {
  97. argv[1] += (argv[1][0] == '-');
  98. if (_stricmp(argv[1], "create") == 0)
  99. CabTestCreateA(argc - 1, argv + 1);
  100. else if (_stricmp(argv[1], "extract") == 0)
  101. CabTestExtractA(argc - 1, argv + 1);
  102. }
  103. void MainW(int argc, wchar_t** argv)
  104. {
  105. argv[1] += (argv[1][0] == '-');
  106. if (_wcsicmp(argv[1], L"create") == 0)
  107. CabTestCreateW(argc - 1, argv + 1);
  108. else if (_wcsicmp(argv[1], L"extract") == 0)
  109. CabTestExtractW(argc - 1, argv + 1);
  110. }
  111. int __cdecl wmain(int argc, wchar_t** argv)
  112. {
  113. MainW(argc, argv);
  114. return 0;
  115. }
  116. void __cdecl wmainCRTStartup(void);
  117. int __cdecl main(int argc, char** argv)
  118. {
  119. MainA(argc, argv);
  120. wmainCRTStartup(); // trick to get A and W coverage in a single binary, this call doesn't return
  121. return 0;
  122. }