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.

172 lines
3.5 KiB

  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include <winioctl.h>
  4. int main(int argc, char *argv[ ])
  5. {
  6. BOOL b;
  7. DWORD FsFlags;
  8. LPSTR lp;
  9. HANDLE hMod;
  10. UINT w;
  11. CHAR FileName[MAX_PATH];
  12. HANDLE hFile;
  13. DWORD Nbytes;
  14. DWORD FileSize;
  15. WORD State;
  16. DWORD Length;
  17. DWORD wrap;
  18. b = GetVolumeInformation(NULL,NULL,0,NULL,NULL,&FsFlags,NULL,0);
  19. if ( !b ) {
  20. printf("compstrs: Failure getting volumeinformation %d\n",GetLastError());
  21. return 0;
  22. }
  23. if ( !(FsFlags & FS_FILE_COMPRESSION) ) {
  24. printf("compstrs: File system does not support per-file compression %x\n",FsFlags);
  25. return 0;
  26. }
  27. //
  28. // Get a temp file
  29. //
  30. w = GetTempFileName(".","cstr",0,FileName);
  31. if ( !w ) {
  32. printf("compstrs: unable to get temp file name\n");
  33. return 0;
  34. }
  35. //
  36. // Create the tempfile
  37. //
  38. hFile = CreateFile(
  39. FileName,
  40. GENERIC_WRITE | GENERIC_READ,
  41. FILE_SHARE_READ | FILE_SHARE_WRITE,
  42. NULL,
  43. CREATE_ALWAYS,
  44. FILE_ATTRIBUTE_NORMAL,
  45. NULL
  46. );
  47. if ( hFile == INVALID_HANDLE_VALUE ) {
  48. printf("compstrs: failure creating %s %d\n",FileName,GetLastError());
  49. return 0;
  50. }
  51. //
  52. // Write the file that we want to compress. It is a copy of kernel32 and ntdll
  53. //
  54. hMod = GetModuleHandle("kernel32");
  55. if ( !hMod ) {
  56. printf("compstrs: failure getting handle to kernel32.dll\n");
  57. CloseHandle(hFile);
  58. DeleteFile(FileName);
  59. return 0;
  60. }
  61. lp = (LPSTR)hMod;
  62. b = TRUE;
  63. FileSize = 0;
  64. while(b) {
  65. b = WriteFile(hFile,lp,512, &Nbytes, NULL);
  66. if ( b ) {
  67. FileSize += Nbytes;
  68. lp += Nbytes;
  69. }
  70. }
  71. hMod = GetModuleHandle("ntdll");
  72. if ( !hMod ) {
  73. printf("compstrs: failure getting handle to ntdll\n");
  74. CloseHandle(hFile);
  75. DeleteFile(FileName);
  76. return 0;
  77. }
  78. lp = (LPSTR)hMod;
  79. b = TRUE;
  80. while(b) {
  81. b = WriteFile(hFile,lp,512, &Nbytes, NULL);
  82. if ( b ) {
  83. FileSize += Nbytes;
  84. lp += Nbytes;
  85. }
  86. }
  87. wrap = 0;
  88. while(1) {
  89. //
  90. // compress and de-compress this file forever
  91. //
  92. State = 1;
  93. b = DeviceIoControl(
  94. hFile,
  95. FSCTL_SET_COMPRESSION,
  96. &State,
  97. sizeof(WORD),
  98. NULL,
  99. 0,
  100. &Length,
  101. NULL
  102. );
  103. if ( !b ) {
  104. printf("compstrs: compress failed %d\n",GetLastError());
  105. wrap = 0;
  106. }
  107. else {
  108. FlushFileBuffers(hFile);
  109. printf("C");
  110. wrap++;
  111. }
  112. Sleep(500);
  113. //
  114. // Decompress
  115. //
  116. State = 0;
  117. b = DeviceIoControl(
  118. hFile,
  119. FSCTL_SET_COMPRESSION,
  120. &State,
  121. sizeof(WORD),
  122. NULL,
  123. 0,
  124. &Length,
  125. NULL
  126. );
  127. if ( !b ) {
  128. printf("compstrs: uncompress failed %d\n",GetLastError());
  129. wrap = 0;
  130. }
  131. else {
  132. FlushFileBuffers(hFile);
  133. printf("U");
  134. wrap++;
  135. }
  136. if ( wrap > 50 ) {
  137. printf("\n");
  138. wrap = 0;
  139. }
  140. }
  141. CloseHandle(hFile);
  142. DeleteFile(FileName);
  143. }