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.

212 lines
5.6 KiB

  1. /* DEC/CMS REPLACEMENT HISTORY, Element COMPRESS.C */
  2. /* *1 14-NOV-1996 10:25:46 ANIGBOGU "[113914]Entry to compression/decompression library via Compress/Decompress" */
  3. /* DEC/CMS REPLACEMENT HISTORY, Element COMPRESS.C */
  4. /* PRIVATE FILE
  5. ******************************************************************************
  6. **
  7. ** (c) Copyright Schlumberger Technology Corp., unpublished work, created 1996.
  8. **
  9. ** This computer program includes Confidential, Proprietary Information and is
  10. ** a Trade Secret of Schlumberger Technology Corp. All use, disclosure, and/or
  11. ** reproduction is prohibited unless authorized in writing by Schlumberger.
  12. ** All Rights Reserved.
  13. **
  14. ******************************************************************************
  15. **
  16. ** compress/compress.c
  17. **
  18. ** PURPOSE
  19. **
  20. ** Compress/Decompress files with zip/unzip algorithm.
  21. **
  22. ** SPECIAL REQUIREMENTS & NOTES
  23. **
  24. ** AUTHOR
  25. **
  26. ** J. C. Anigbogu
  27. ** Austin Systems Center
  28. ** Nov 1996
  29. **
  30. ******************************************************************************
  31. */
  32. /* Compress files with zip algorithm and 'compress' interface.
  33. *
  34. */
  35. #include "comppriv.h"
  36. /* ========================================================================
  37. * Check the magic number of the input buffer.
  38. * Return the compression method, -1 for error.
  39. */
  40. int
  41. GetMethod(
  42. CompParam_t *Comp,
  43. CompressStatus_t *Status
  44. )
  45. {
  46. char Magic[2]; /* magic header */
  47. int Method; /* compression method */
  48. Magic[0] = (char)GetByte(Comp);
  49. Magic[1] = (char)GetByte(Comp);
  50. Comp->HeaderBytes = 0;
  51. if (memcmp(Magic, GZIP_MAGIC, 2) == 0)
  52. {
  53. Method = (int)GetByte(Comp);
  54. if (Method != DEFLATED && Method != STORED)
  55. {
  56. *Status = UNKNOWN_COMPRESSION_METHOD;
  57. return -1;
  58. }
  59. (void)GetByte(Comp); /* Ignore flags */
  60. (void)GetByte(Comp); /* Ignore stamp */
  61. (void)GetByte(Comp); /* ,, */
  62. (void)GetByte(Comp); /* ,, */
  63. (void)GetByte(Comp); /* ,, */
  64. (void)GetByte(Comp); /* Ignore extra flags for the moment */
  65. (void)GetByte(Comp); /* Ignore OS type for the moment */
  66. Comp->HeaderBytes = Comp->Index + 2*sizeof(long); /* include crc and size */
  67. }
  68. else
  69. {
  70. *Status = BAD_MAGIC_HEADER;
  71. return -1;
  72. }
  73. return Method;
  74. }
  75. /* ========================================================================
  76. * Compress input
  77. */
  78. CompressStatus_t
  79. Compress(
  80. unsigned char *Input,
  81. unsigned int InputSize,
  82. unsigned char **Output,
  83. unsigned int *OutputSize,
  84. unsigned int Level /* compression level */
  85. )
  86. {
  87. int Length;
  88. CompData_t *Ptr;
  89. CompParam_t *Comp;
  90. CompressStatus_t Status;
  91. Comp = (CompParam_t *)CompressMalloc(sizeof(CompParam_t), &Status);
  92. if (Status != COMPRESS_OK)
  93. return Status;
  94. Crc32 crcGenerator(0); // for backward compatibility
  95. Comp->pCRC = &crcGenerator;
  96. Length = FillBuffer(Input, InputSize, Comp);
  97. /* Do the compression
  98. */
  99. if ((Status = Zip((int)Level, Comp)) != COMPRESS_OK)
  100. {
  101. CompressFree(Comp);
  102. return Status;
  103. }
  104. *OutputSize = Comp->BytesOut;
  105. *Output = (unsigned char *)CompressMalloc(*OutputSize, &Status);
  106. if (Status != COMPRESS_OK)
  107. {
  108. CompressFree(Comp);
  109. return Status;
  110. }
  111. Length = 0;
  112. while (Comp->PtrOutput != NULL)
  113. {
  114. Ptr = Comp->PtrOutput;
  115. memcpy((char *)*Output+Length, (char *)Comp->PtrOutput->Data,
  116. Comp->PtrOutput->Size);
  117. Length += Comp->PtrOutput->Size;
  118. Comp->PtrOutput = Comp->PtrOutput->next;
  119. CompressFree(Ptr->Data);
  120. CompressFree(Ptr);
  121. }
  122. CompressFree(Comp);
  123. return COMPRESS_OK;
  124. }
  125. /* ========================================================================
  126. * Decompress input
  127. */
  128. CompressStatus_t
  129. Decompress(
  130. unsigned char *Input,
  131. unsigned int InputSize,
  132. unsigned char **Output,
  133. unsigned int *OutputSize
  134. )
  135. {
  136. int Length;
  137. int Method;
  138. CompData_t *Ptr;
  139. CompParam_t *Comp;
  140. CompressStatus_t Status;
  141. Comp = (CompParam_t *)CompressMalloc(sizeof(CompParam_t), &Status);
  142. if (Status != COMPRESS_OK)
  143. return Status;
  144. Crc32 crcGenerator(0); // for backward compatibility
  145. Comp->pCRC = &crcGenerator;
  146. Length = FillBuffer(Input, InputSize, Comp);
  147. /* Do the decompression
  148. */
  149. Method = GetMethod(Comp, &Status);
  150. if (Status != COMPRESS_OK)
  151. {
  152. CompressFree(Comp);
  153. return Status;
  154. }
  155. if ((Status = Unzip(Method, Comp)) != COMPRESS_OK)
  156. {
  157. CompressFree(Comp);
  158. return Status;
  159. }
  160. *OutputSize = Comp->BytesOut;
  161. *Output = (unsigned char *)CompressMalloc(*OutputSize, &Status);
  162. if (Status != COMPRESS_OK)
  163. {
  164. CompressFree(Comp);
  165. return Status;
  166. }
  167. Length = 0;
  168. while (Comp->PtrOutput != NULL)
  169. {
  170. Ptr = Comp->PtrOutput;
  171. memcpy((char *)*Output+Length, (char *)Comp->PtrOutput->Data,
  172. Comp->PtrOutput->Size);
  173. Length += Comp->PtrOutput->Size;
  174. Comp->PtrOutput = Comp->PtrOutput->next;
  175. CompressFree(Ptr->Data);
  176. CompressFree(Ptr);
  177. }
  178. CompressFree(Comp);
  179. return COMPRESS_OK;
  180. }