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.

259 lines
6.9 KiB

  1. /* DEC/CMS REPLACEMENT HISTORY, Element UTIL.C */
  2. /* *1 14-NOV-1996 10:27:03 ANIGBOGU "[113914]Utility functions to support the compression/decompression library" */
  3. /* DEC/CMS REPLACEMENT HISTORY, Element UTIL.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/util.c
  17. **
  18. ** PURPOSE
  19. **
  20. ** Utility functions for compress/decompression support
  21. **
  22. ** SPECIAL REQUIREMENTS & NOTES
  23. **
  24. ** AUTHOR
  25. **
  26. ** J. C. Anigbogu
  27. ** Austin Systems Center
  28. ** Nov 1996
  29. **
  30. ******************************************************************************
  31. */
  32. #include "comppriv.h"
  33. /* ===========================================================================
  34. * Clear input and output buffers
  35. */
  36. void
  37. ClearBuffers(
  38. CompParam_t *Comp
  39. )
  40. {
  41. Comp->OutBytes = 0;
  42. Comp->InputSize = Comp->Index = 0;
  43. Comp->BytesIn = Comp->BytesOut = 0L;
  44. }
  45. /* ===========================================================================
  46. * Fill the input buffer. This is called only when the buffer is empty.
  47. */
  48. int
  49. FillInputBuffer(
  50. int EOF_OK, /* set if EOF acceptable as a result */
  51. CompParam_t *Comp
  52. )
  53. {
  54. unsigned long Length;
  55. /* Read as much as possible */
  56. Comp->InputSize = 0;
  57. Length = MIN(Comp->GlobalSize - Comp->BytesIn,
  58. (unsigned long)INBUFSIZ);
  59. Comp->Input = Comp->GlobalInput + Comp->BytesIn;
  60. Comp->InputSize += Length;
  61. if ((Comp->InputSize == 0) && (EOF_OK == 1))
  62. return EOF;
  63. Comp->BytesIn += Comp->InputSize;
  64. Comp->Index = 1;
  65. return (int)Comp->Input[0];
  66. }
  67. /* ===========================================================================
  68. * Write the output buffer Output[0..OutBytes-1] and update BytesOut.
  69. * (used for the compressed data only)
  70. */
  71. CompressStatus_t
  72. FlushOutputBuffer(
  73. CompParam_t *Comp
  74. )
  75. {
  76. CompressStatus_t Status = COMPRESS_OK;
  77. if (Comp->OutBytes == 0)
  78. return Status;
  79. Status = WriteBuffer(Comp, (char *)Comp->Output, Comp->OutBytes);
  80. if (COMPRESS_OK != Status)
  81. return Status;
  82. Comp->BytesOut += (unsigned long)Comp->OutBytes;
  83. Comp->OutBytes = 0;
  84. return Status;
  85. }
  86. /* ===========================================================================
  87. * Write the output window Window[0..OutBytes-1] and update crc and BytesOut.
  88. * (Used for the decompressed data only.)
  89. */
  90. CompressStatus_t
  91. FlushWindow(
  92. CompParam_t *Comp
  93. )
  94. {
  95. CompressStatus_t Status = COMPRESS_OK;
  96. if (Comp->OutBytes == 0)
  97. return Status;
  98. Comp->pCRC->Compute(Comp->Window, Comp->OutBytes);
  99. Status = WriteBuffer(Comp, (char *)Comp->Window,
  100. Comp->OutBytes);
  101. if (COMPRESS_OK != Status)
  102. return Status;
  103. Comp->BytesOut += (unsigned long)Comp->OutBytes;
  104. Comp->OutBytes = 0;
  105. return Status;
  106. }
  107. /* ===========================================================================
  108. * Flushes output buffer
  109. */
  110. CompressStatus_t
  111. WriteBuffer(
  112. CompParam_t *Comp,
  113. void *Buffer,
  114. unsigned int Size
  115. )
  116. {
  117. CompressStatus_t Status;
  118. unsigned char *temp =
  119. (unsigned char *)CompressMalloc(Size, &Status);
  120. if (COMPRESS_OK != Status)
  121. return Status;
  122. if (Comp->PtrOutput == NULL)
  123. {
  124. Comp->CompressedOutput =
  125. (CompData_t *)CompressMalloc(sizeof(CompData_t), &Status);
  126. if (COMPRESS_OK == Status)
  127. Comp->PtrOutput = Comp->CompressedOutput;
  128. }
  129. else
  130. {
  131. Comp->CompressedOutput->next =
  132. (CompData_t *)CompressMalloc(sizeof(CompData_t), &Status);
  133. if (COMPRESS_OK == Status)
  134. Comp->CompressedOutput = Comp->CompressedOutput->next;
  135. }
  136. if (COMPRESS_OK != Status)
  137. {
  138. CompressFree(temp);
  139. return Status;
  140. }
  141. Comp->CompressedOutput->Data = temp;
  142. Comp->CompressedOutput->next = NULL;
  143. memcpy((char *)Comp->CompressedOutput->Data, Buffer, (int)Size);
  144. Comp->CompressedOutput->Size = (int)Size;
  145. return COMPRESS_OK;
  146. }
  147. /* ========================================================================
  148. * Error translator.
  149. */
  150. void
  151. TranslateErrorMsg(
  152. char *Message,
  153. CompressStatus_t ErrorCode
  154. )
  155. {
  156. switch(ErrorCode)
  157. {
  158. case COMPRESS_OK:
  159. strcpy(Message, "This is not an error message.");
  160. break;
  161. case BAD_COMPRESSION_LEVEL:
  162. strcpy(Message, "Invalid compression level--valid values are 0-9.");
  163. break;
  164. case BAD_MAGIC_HEADER:
  165. strcpy(Message, "Bad magic header.");
  166. break;
  167. case BAD_COMPRESSED_DATA:
  168. strcpy(Message, "Bad compressed data.");
  169. break;
  170. case BAD_BLOCK_TYPE:
  171. strcpy(Message, "Invalid block type.");
  172. break;
  173. case BAD_CODE_LENGTHS:
  174. strcpy(Message, "Bad code lengths.");
  175. break;
  176. case BAD_INPUT:
  177. strcpy(Message, "Bad input--more codes than bits.");
  178. break;
  179. case EXTRA_BITS:
  180. strcpy(Message, "Too many bits.");
  181. break;
  182. case UNKNOWN_COMPRESSION_METHOD:
  183. strcpy(Message, "Unknown compression method.");
  184. break;
  185. case INCOMPLETE_CODE_SET:
  186. strcpy(Message, "Incomplete code set.");
  187. break;
  188. case END_OF_BLOCK:
  189. strcpy(Message, "End of block.");
  190. break;
  191. case BLOCK_VANISHED:
  192. strcpy(Message, "Block to compress disappeared--memory trashed.");
  193. break;
  194. case FORMAT_VIOLATED:
  195. strcpy(Message, "Invalid compressed data--format violated.");
  196. break;
  197. case CRC_ERROR:
  198. strcpy(Message, "Invalid compressed data--crc error.");
  199. break;
  200. case LENGTH_ERROR:
  201. strcpy(Message, "Invalid compressed data--length error.");
  202. break;
  203. case INSUFFICIENT_MEMORY:
  204. strcpy(Message, "Insufficient memory--ould not allocate space requested.");
  205. break;
  206. default: sprintf(Message, "Unknown error code %d", ErrorCode);
  207. }
  208. }
  209. /* ========================================================================
  210. * Semi-safe malloc -- never returns NULL.
  211. */
  212. void *
  213. CompressMalloc(
  214. unsigned int Size,
  215. CompressStatus_t *Status
  216. )
  217. {
  218. void *DynamicSpace = malloc ((int)Size);
  219. if (DynamicSpace == NULL)
  220. *Status = INSUFFICIENT_MEMORY;
  221. else
  222. *Status = COMPRESS_OK;
  223. return DynamicSpace;
  224. }
  225. void
  226. CompressFree(void *Address)
  227. {
  228. free(Address);
  229. }