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.

152 lines
4.1 KiB

  1. /*
  2. ** compress.c - Main compression routine for LZA file compression program.
  3. **
  4. ** Author: DavidDi
  5. */
  6. // Headers
  7. ///////////
  8. #ifndef LZA_DLL
  9. #include <dos.h>
  10. #include <errno.h>
  11. #include <io.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #endif
  15. #include "lz_common.h"
  16. #include "lzcommon.h"
  17. #include "lz_buffers.h"
  18. #include "lz_header.h"
  19. /*
  20. ** N.b., one reason DOS file handles are used for file references in this
  21. ** module is that using FILE *'s for file references poses a problem.
  22. ** fclose()'ing a file which was fopen()'ed in write "w" or append "a" mode
  23. ** stamps the file with the current date. This undoes the intended effect of
  24. ** CopyDateTimeStamp(). We could also get around this fclose() problem by
  25. ** first fclose()'ing the file, and then fopen()'ing it again in read "r"
  26. ** mode.
  27. **
  28. ** Using file handles also allows us to bypass stream buffering, so reads and
  29. ** writes may be done with whatever buffer size we choose. Also, the
  30. ** lower-level DOS file handle functions are faster than their stream
  31. ** counterparts.
  32. */
  33. /*
  34. ** int Compress(char ARG_PTR *pszSource, char ARG_PTR *pszDest,
  35. ** BYTE byteAlgorithm, BYTE byteExtensionChar);
  36. **
  37. ** Compress one file to another.
  38. **
  39. ** Arguments: pszSource - name of file to compress
  40. ** pszDest - name of compressed output file
  41. ** byteAlgorithm - compression algorithm to use
  42. ** byteExtensionChar - compressed file name extension character
  43. **
  44. ** Returns: int - TRUE if compression finished successfully. One of the
  45. ** LZERROR_ codes if not.
  46. **
  47. ** Globals: none
  48. */
  49. INT Compress(
  50. NOTIFYPROC pfnNotify,
  51. CHAR ARG_PTR *pszSource,
  52. CHAR ARG_PTR *pszDest,
  53. BYTE byteAlgorithm,
  54. BOOL bDoRename,
  55. PLZINFO pLZI)
  56. {
  57. INT doshSource, // input file handle
  58. doshDest, // output file handle
  59. nRetVal = TRUE;
  60. FH FHOut; // compressed header info struct
  61. CHAR szDestFileName[MAX_PATH];
  62. BYTE byteExtensionChar;
  63. // Sanity check
  64. if (!pLZI) {
  65. return(LZERROR_GLOBLOCK);
  66. }
  67. // Set up input file handle. Set cblInSize to length of input file.
  68. if ((nRetVal = GetIOHandle(pszSource, READ_IT, & doshSource, &pLZI->cblInSize)) != TRUE)
  69. return(nRetVal);
  70. // Rewind input file.
  71. if (FSEEK(doshSource, 0L, SEEK_SET) != 0L)
  72. {
  73. FCLOSE(doshSource);
  74. return(LZERROR_BADINHANDLE);
  75. }
  76. // Create destination file name.
  77. lstrcpyn(szDestFileName, pszDest, sizeof(szDestFileName)/sizeof(szDestFileName[0]));
  78. if (bDoRename == TRUE)
  79. // Rename output file.
  80. byteExtensionChar = MakeCompressedName(szDestFileName);
  81. else
  82. byteExtensionChar = '\0';
  83. // Ask if we should compress this file.
  84. if (! (*pfnNotify)(pszSource, szDestFileName, NOTIFY_START_COMPRESS))
  85. {
  86. // Don't compress file. This error condition should be handled in
  87. // pfnNotify, so indicate that it is not necessary for the caller to
  88. // display an error message.
  89. FCLOSE(doshSource);
  90. return(BLANK_ERROR);
  91. }
  92. // Set up output file handle.
  93. if ((nRetVal = GetIOHandle(szDestFileName, WRITE_IT, & doshDest, &pLZI->cblInSize)) != TRUE)
  94. {
  95. FCLOSE(doshSource);
  96. return(nRetVal);
  97. }
  98. // Fill in compressed file header.
  99. MakeHeader(& FHOut, byteAlgorithm, byteExtensionChar, pLZI);
  100. // Write compressed file header to output file.
  101. if ((nRetVal = WriteHdr(& FHOut, doshDest, pLZI)) != TRUE)
  102. goto COMPRESS_EXIT;
  103. // Compress input file into output file.
  104. switch (byteAlgorithm)
  105. {
  106. case ALG_FIRST:
  107. #if 0
  108. case ALG_LZ:
  109. #endif
  110. nRetVal = LZEncode(doshSource, doshDest, pLZI);
  111. break;
  112. default:
  113. nRetVal = LZERROR_UNKNOWNALG;
  114. break;
  115. }
  116. if (nRetVal != TRUE)
  117. goto COMPRESS_EXIT;
  118. // Copy date and time stamp from source file to destination file.
  119. nRetVal = CopyDateTimeStamp(doshSource, doshDest);
  120. COMPRESS_EXIT:
  121. // Close files.
  122. FCLOSE(doshSource);
  123. FCLOSE(doshDest);
  124. return(nRetVal);
  125. }