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.

197 lines
4.3 KiB

  1. #include "precomp.h"
  2. #pragma hdrstop
  3. /*
  4. * Stubs for decompression routines. They just copy the file instead of
  5. * decompressing it.
  6. *
  7. * Only routines actually used by the toolkit are included.
  8. */
  9. #define MIN_BUFFER_SIZE 2048 // must be a multiple of 2
  10. #define MAX_BUFFER_SIZE (2*65536) // must be a multiple of 2
  11. LONG
  12. LcbCopyFile(
  13. HANDLE fhSrc,
  14. HANDLE fhDest,
  15. PFNWFROMW pfn,
  16. INT cProgTicks
  17. )
  18. {
  19. BYTE StaticBuffer[MIN_BUFFER_SIZE],*CopyBuffer;
  20. DWORD br,bw;
  21. DWORD BufferSize,FileSize,TicksPerCycle=0;
  22. LONG DistanceHigh = 0L;
  23. LONG RC;
  24. if((FileSize = GetFileSize(fhSrc,NULL)) == (DWORD)(-1)) {
  25. return(rcReadError);
  26. }
  27. //
  28. // Rewind to beginning of file
  29. //
  30. if(SetFilePointer(fhSrc, 0L, &DistanceHigh, FILE_BEGIN) == 0xFFFFFFFF) {
  31. return (rcReadSeekError);
  32. }
  33. BufferSize = MAX_BUFFER_SIZE;
  34. while((BufferSize > MIN_BUFFER_SIZE)
  35. && ((CopyBuffer = SAlloc(BufferSize)) == NULL))
  36. {
  37. BufferSize /= 2;
  38. }
  39. if(CopyBuffer == NULL) {
  40. CopyBuffer = StaticBuffer;
  41. BufferSize = sizeof(StaticBuffer);
  42. }
  43. if(FileSize) {
  44. TicksPerCycle = BufferSize*(DWORD)cProgTicks/FileSize;
  45. }
  46. if(TicksPerCycle > (DWORD)cProgTicks) {
  47. TicksPerCycle = (DWORD)cProgTicks;
  48. }
  49. while(1) {
  50. if(!ReadFile(fhSrc,CopyBuffer,BufferSize,&br,NULL)) {
  51. RC = rcReadError;
  52. break;
  53. } else if(!br) { // ReadFile returned TRUE and bytesread = 0
  54. RC = rcNoError;
  55. break;
  56. }
  57. FYield();
  58. if ( fUserQuit ) {
  59. RC = rcUserQuit;
  60. break;
  61. }
  62. if(!WriteFile(fhDest,CopyBuffer,br,&bw,NULL) || (br != bw)) {
  63. // the !RC test guesses that br != bw means disk full
  64. if(((RC = GetLastError()) == ERROR_DISK_FULL) || !RC) {
  65. RC = rcDiskFull;
  66. } else {
  67. RC = rcWriteError;
  68. }
  69. break;
  70. }
  71. FYield();
  72. if ( fUserQuit ) {
  73. RC = rcUserQuit;
  74. break;
  75. }
  76. if(pfn != NULL) {
  77. pfn(TicksPerCycle);
  78. }
  79. }
  80. if(CopyBuffer != StaticBuffer) {
  81. SFree(CopyBuffer);
  82. }
  83. return(RC);
  84. }
  85. LONG
  86. LcbDecompFile(
  87. HANDLE fhSrc,
  88. HANDLE fhDest,
  89. PFNWFROMW pfn,
  90. INT cProgTicks
  91. )
  92. {
  93. BYTE StaticBuffer[MIN_BUFFER_SIZE],*CopyBuffer;
  94. DWORD br,bw;
  95. DWORD BufferSize,FileSize,TicksPerCycle=0;
  96. LONG RC;
  97. //
  98. // Use seek to determine file length
  99. //
  100. if(( RC = LZSeek( HandleToUlong(fhSrc), 0L, 2 ) ) < 0) {
  101. return (rcReadSeekError);
  102. }
  103. FileSize = (DWORD)RC;
  104. //
  105. // Rewind to beginning of file
  106. //
  107. if(( RC = LZSeek( HandleToUlong(fhSrc), 0L, 0 ) ) < 0) {
  108. return (rcReadSeekError);
  109. }
  110. //
  111. // Copy source file to destination file BufferSize at a time
  112. //
  113. BufferSize = MAX_BUFFER_SIZE;
  114. while((BufferSize > MIN_BUFFER_SIZE)
  115. && ((CopyBuffer = SAlloc(BufferSize)) == NULL))
  116. {
  117. BufferSize /= 2;
  118. }
  119. if(CopyBuffer == NULL) {
  120. CopyBuffer = StaticBuffer;
  121. BufferSize = sizeof(StaticBuffer);
  122. }
  123. if(FileSize) {
  124. TicksPerCycle = BufferSize*(DWORD)cProgTicks/FileSize;
  125. }
  126. if(TicksPerCycle > (DWORD)cProgTicks) {
  127. TicksPerCycle = (DWORD)cProgTicks;
  128. }
  129. while(1) {
  130. if((RC = LZRead( HandleToUlong(fhSrc),CopyBuffer,BufferSize) ) < 0) {
  131. RC = rcReadError;
  132. break;
  133. } else if(!RC) { // No error and bytesread = 0
  134. RC = rcNoError;
  135. break;
  136. }
  137. br = (DWORD)RC;
  138. FYield();
  139. if ( fUserQuit ) {
  140. RC = rcUserQuit;
  141. break;
  142. }
  143. if(!WriteFile(fhDest,CopyBuffer,br,&bw,NULL) || (br != bw)) {
  144. // the !RC test guesses that br != bw means disk full
  145. if(((RC = GetLastError()) == ERROR_DISK_FULL) || !RC) {
  146. RC = rcDiskFull;
  147. } else {
  148. RC = rcWriteError;
  149. }
  150. break;
  151. }
  152. FYield();
  153. if ( fUserQuit ) {
  154. RC = rcUserQuit;
  155. break;
  156. }
  157. if(pfn != NULL) {
  158. pfn(TicksPerCycle);
  159. }
  160. }
  161. if(CopyBuffer != StaticBuffer) {
  162. SFree(CopyBuffer);
  163. }
  164. return(RC);
  165. }