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.

223 lines
6.1 KiB

  1. /*
  2. ** header.c - Routines used to access compressed file header information.
  3. **
  4. ** written by DavidDi
  5. */
  6. // Headers
  7. ///////////
  8. #ifndef LZA_DLL
  9. #include <io.h>
  10. #include <dos.h>
  11. #include <fcntl.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #endif
  15. #include "lz_common.h"
  16. #include "lz_buffers.h"
  17. #include "lz_header.h"
  18. /*
  19. ** int WriteHdr(PFH pFH, int doshDest);
  20. **
  21. ** Write compressed file header to output file.
  22. **
  23. ** Arguments: pFH - pointer to source header information structure
  24. ** doshDest - DOS file handle of open output file
  25. **
  26. ** Returns: int - TRUE if successful. LZERROR_BADOUTHANDLE if
  27. ** unsuccessful.
  28. **
  29. ** Globals: none
  30. **
  31. ** header format:
  32. ** 8 bytes --> compressed file signature
  33. ** 1 byte --> algorithm label
  34. ** 1 byte --> extension char
  35. ** 4 bytes --> uncompressed file size (LSB to MSB)
  36. **
  37. ** length = 14 bytes
  38. */
  39. INT WriteHdr(PFH pFH, INT doshDest, PLZINFO pLZI)
  40. {
  41. INT i, j;
  42. DWORD ucbWritten;
  43. BYTE rgbyteHeaderBuf[HEADER_LEN]; // temporary storage for next header byte to write
  44. // Sanity check
  45. if (!pLZI) {
  46. return(LZERROR_GLOBLOCK);
  47. }
  48. // Copy the compressed file signature.
  49. for (i = 0; i < COMP_SIG_LEN; i++)
  50. rgbyteHeaderBuf[i] = pFH->rgbyteMagic[i];
  51. // Copy the algorithm label and file name extension character.
  52. rgbyteHeaderBuf[i++] = pFH->byteAlgorithm;
  53. rgbyteHeaderBuf[i++] = pFH->byteExtensionChar;
  54. // Copy input file size (long ==> 4 bytes),
  55. // LSB first to MSB last.
  56. for (j = 0; j < 4; j++)
  57. rgbyteHeaderBuf[i++] = (BYTE)((pFH->cbulUncompSize >> (8 * j)) &
  58. (DWORD)BYTE_MASK);
  59. // Write header to file.
  60. if ((ucbWritten = FWRITE(doshDest, rgbyteHeaderBuf, HEADER_LEN)) != HEADER_LEN)
  61. {
  62. #ifdef LZA_DLL
  63. if (ucbWritten == (DWORD)(-1))
  64. #else
  65. if (_error != 0U)
  66. #endif
  67. // Bad DOS file handle.
  68. return(LZERROR_BADOUTHANDLE);
  69. else
  70. // Insufficient space on destination drive.
  71. return(LZERROR_WRITE);
  72. }
  73. // Keep track of bytes written.
  74. pLZI->cblOutSize += (LONG)ucbWritten;
  75. // Header written ok.
  76. return(TRUE);
  77. }
  78. /*
  79. ** int GetHdr(PFH pFH, int doshSource);
  80. **
  81. ** Get compressed file header.
  82. **
  83. ** Arguments: pFH - pointer to destination header information structure
  84. ** doshSource - DOS file handle of open input file
  85. **
  86. ** Returns: int - TRUE if compressed file header read successfully. One
  87. ** the LZERROR_ codes if not.
  88. **
  89. ** Globals: none
  90. */
  91. INT GetHdr(PFH pFH, INT doshSource, LONG *pcblInSize)
  92. {
  93. DWORD ucbRead;
  94. BYTE rgbyteHeaderBuf[HEADER_LEN];
  95. INT i, j;
  96. // Get input file length and move back to beginning of input file.
  97. if ((*pcblInSize = FSEEK(doshSource, 0L, SEEK_END)) < 0L ||
  98. FSEEK(doshSource, 0L, SEEK_SET) != 0L)
  99. return(LZERROR_BADINHANDLE);
  100. if ((ucbRead = FREAD(doshSource, rgbyteHeaderBuf, HEADER_LEN))
  101. != HEADER_LEN)
  102. {
  103. #ifdef LZA_DLL
  104. if (ucbRead == (DWORD)(-1))
  105. #else
  106. if (_error != 0U)
  107. #endif
  108. // We were handed a bad input file handle.
  109. return((INT)LZERROR_BADINHANDLE);
  110. else
  111. // Input file shorter than compressed header size.
  112. return(LZERROR_READ);
  113. }
  114. // Put compressed file signature into rgbyteMagic[] of header info struct.
  115. for (i = 0; i < COMP_SIG_LEN; i++)
  116. pFH->rgbyteMagic[i] = rgbyteHeaderBuf[i];
  117. // Get algorithm label and file name extension character.
  118. pFH->byteAlgorithm = rgbyteHeaderBuf[i++];
  119. pFH->byteExtensionChar = rgbyteHeaderBuf[i++];
  120. // Extract uncompressed file size, LSB --> MSB (4 bytes in long).
  121. pFH->cbulUncompSize = 0UL;
  122. for (j = 0; j < 4; j++)
  123. pFH->cbulUncompSize |= ((DWORD)(rgbyteHeaderBuf[i++]) << (8 * j));
  124. // Stick compressed file size into header info struct.
  125. pFH->cbulCompSize = (DWORD)*pcblInSize;
  126. // File header read ok.
  127. return(TRUE);
  128. }
  129. /*
  130. ** BOOL IsCompressed(PFH pFHIn);
  131. **
  132. ** See if a file is in compressed form by comparing its file signature with
  133. ** the expected compressed file signature.
  134. **
  135. ** Arguments: pFHIn - pointer to header info struct to check
  136. **
  137. ** Returns: BOOL - TRUE if file signature matches expected compressed file
  138. ** signature. FALSE if not.
  139. **
  140. ** Globals: none
  141. */
  142. BOOL IsCompressed(PFH pFHIn)
  143. {
  144. INT i;
  145. // storage for FHIn's compressed file signature (used to make it an sz)
  146. CHAR rgchBuf[COMP_SIG_LEN + 1];
  147. // Copy file info struct's compressed file signature into rgchBuf[] to
  148. // make it an sz.
  149. for (i = 0; i < COMP_SIG_LEN; i++)
  150. rgchBuf[i] = pFHIn->rgbyteMagic[i];
  151. rgchBuf[i] = '\0';
  152. return((STRCMP(rgchBuf, COMP_SIG) == 0) ? TRUE : FALSE);
  153. }
  154. /*
  155. ** void MakeHeader(PFH pFHBlank, BYTE byteAlgorithm,
  156. ** BYTE byteExtensionChar);
  157. **
  158. ** Arguments: pFHBlank - pointer to compressed file header struct
  159. ** that is to be filled in
  160. ** byteAlgorithm - algorithm label
  161. ** byteExtensionChar - uncompressed file name extension character
  162. **
  163. ** Returns: void
  164. **
  165. ** Globals: none
  166. **
  167. ** Global cblInSize is used to fill in expanded file length field.
  168. ** Compressed file length field is set to 0 since it isn't written.
  169. **
  170. */
  171. VOID MakeHeader(PFH pFHBlank, BYTE byteAlgorithm,
  172. BYTE byteExtensionChar, PLZINFO pLZI)
  173. {
  174. INT i;
  175. // !!! Assumes pLZI parm is valid. No sanity check (should be done above in caller).
  176. // Fill in compressed file signature.
  177. for (i = 0; i < COMP_SIG_LEN; i++)
  178. pFHBlank->rgbyteMagic[i] = (BYTE)(*(COMP_SIG + i));
  179. // Fill in algorithm and extesion character.
  180. pFHBlank->byteAlgorithm = byteAlgorithm;
  181. pFHBlank->byteExtensionChar = byteExtensionChar;
  182. // Fill in file sizes. (cbulCompSize not written to compressed file
  183. // header, so just set it to 0UL.)
  184. pFHBlank->cbulUncompSize = (DWORD)pLZI->cblInSize;
  185. pFHBlank->cbulCompSize = 0UL;
  186. }