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.

268 lines
7.4 KiB

  1. /*
  2. * LCI.C
  3. */
  4. /* --- preprocessor ------------------------------------------------------- */
  5. #include <stdio.h> /* for NULL */
  6. #include <string.h> /* for memcpy() */
  7. #include "encoder.h"
  8. #include "lci.h" /* types, prototype verification, error codes */
  9. /*
  10. * Default file size for E8 translation
  11. */
  12. #define DEFAULT_FILE_XLAT_SIZE 12000000
  13. /* MAKE_SIGNATURE - Construct a structure signature
  14. *
  15. * Entry:
  16. * a,b,c,d - four characters
  17. *
  18. * Exit:
  19. * Returns constructed SIGNATURE
  20. *
  21. * Example:
  22. * strct->signature = MAKE_SIGNATURE('b','e','n','s')
  23. */
  24. #define MAKE_SIGNATURE(a,b,c,d) (a + (b<<8) + (c<<16) + (d<<24))
  25. #define BAD_SIGNATURE (0L)
  26. #define LCI_SIGNATURE MAKE_SIGNATURE('L','C','I','C')
  27. /* --- LCI context structure ---------------------------------------------- */
  28. typedef ULONG SIGNATURE; /* structure signature */
  29. struct LCI_CONTEXT /* private structure */
  30. {
  31. SIGNATURE signature; /* for validation */
  32. PFNALLOC pfnAlloc; /* memory alloc function */
  33. PFNFREE pfnFree; /* memory free function */
  34. UINT cbDataBlockMax; /* promised max data size */
  35. unsigned long file_translation_size;
  36. t_encoder_context *encoder_context;
  37. };
  38. typedef struct LCI_CONTEXT FAR *PMCC_CONTEXT; /* a pointer to one */
  39. #define PMCCfromHMC(h) ((PMCC_CONTEXT)(h)) /* handle to pointer */
  40. #define HMCfromPMCC(p) ((LCI_CONTEXT_HANDLE)(p)) /* pointer to handle */
  41. /* --- local variables ---------------------------------------------------- */
  42. int DIAMONDAPI LCICreateCompression(
  43. UINT * pcbDataBlockMax, /* max uncompressed data block */
  44. void FAR * pvConfiguration, /* implementation-defined */
  45. PFNALLOC pfnma, /* Memory allocation function */
  46. PFNFREE pfnmf, /* Memory free function */
  47. UINT * pcbDstBufferMin, /* gets required output buffer */
  48. LCI_CONTEXT_HANDLE * pmchHandle, /* gets newly-created handle */
  49. int FAR (DIAMONDAPI *pfnlzx_output_callback)(
  50. void * pfol,
  51. unsigned char * compressed_data,
  52. long compressed_size,
  53. long uncompressed_size
  54. ),
  55. void FAR * fci_data
  56. )
  57. {
  58. PMCC_CONTEXT context; /* new context */
  59. FAR PLZXCONFIGURATION plConfiguration;
  60. *pmchHandle = (LCI_CONTEXT_HANDLE) 0; /* wait until it's valid */
  61. plConfiguration = (PLZXCONFIGURATION) pvConfiguration;
  62. context = pfnma(sizeof(struct LCI_CONTEXT));
  63. if (context == NULL)
  64. return(MCI_ERROR_NOT_ENOUGH_MEMORY); /* if can't allocate */
  65. context->file_translation_size = DEFAULT_FILE_XLAT_SIZE;
  66. context->encoder_context = pfnma(sizeof(*context->encoder_context));
  67. if (context->encoder_context == NULL)
  68. {
  69. pfnmf(context);
  70. return(MCI_ERROR_NOT_ENOUGH_MEMORY); /* if can't allocate */
  71. }
  72. if (LZX_EncodeInit(
  73. context->encoder_context,
  74. plConfiguration->WindowSize,
  75. plConfiguration->SecondPartitionSize,
  76. pfnma,
  77. pfnmf,
  78. pfnlzx_output_callback,
  79. fci_data
  80. ) == false)
  81. {
  82. pfnmf(context->encoder_context);
  83. pfnmf(context);
  84. return MCI_ERROR_NOT_ENOUGH_MEMORY;
  85. }
  86. context->pfnAlloc = pfnma;
  87. context->pfnFree = pfnmf;
  88. context->cbDataBlockMax = *pcbDataBlockMax; /* remember agreement */
  89. context->signature = LCI_SIGNATURE;
  90. *pcbDstBufferMin = /* we'll expand sometimes */
  91. *pcbDataBlockMax + MAX_GROWTH;
  92. /* pass context back to caller */
  93. *pmchHandle = HMCfromPMCC(context);
  94. return(MCI_ERROR_NO_ERROR); /* tell caller all is well */
  95. }
  96. int DIAMONDAPI LCICompress(
  97. LCI_CONTEXT_HANDLE hmc, /* compression context */
  98. void FAR * pbSrc, /* source buffer */
  99. UINT cbSrc, /* source actual size */
  100. void FAR * pbDst, /* target buffer */
  101. UINT cbDst, /* size of target buffer */
  102. ULONG * pcbResult) /* gets target actual size */
  103. {
  104. PMCC_CONTEXT context; /* pointer to the context */
  105. long estimated_leftover_bytes;
  106. context = PMCCfromHMC(hmc); /* get pointer from handle */
  107. if (context->signature != LCI_SIGNATURE)
  108. {
  109. return(MCI_ERROR_BAD_PARAMETERS); /* missing signature */
  110. }
  111. if (cbSrc > context->cbDataBlockMax)
  112. {
  113. return(MCI_ERROR_BAD_PARAMETERS); /* violated max block promise */
  114. }
  115. if (cbDst < (context->cbDataBlockMax + MAX_GROWTH))
  116. {
  117. return(MCI_ERROR_BAD_PARAMETERS); /* violated min buffer request */
  118. }
  119. if (ENCODER_SUCCESS == LZX_Encode(
  120. context->encoder_context,
  121. pbSrc,
  122. cbSrc,
  123. &estimated_leftover_bytes,
  124. context->file_translation_size))
  125. {
  126. *pcbResult = estimated_leftover_bytes;
  127. return MCI_ERROR_NO_ERROR;
  128. }
  129. else
  130. {
  131. *pcbResult = 0;
  132. return MCI_ERROR_FAILED;
  133. }
  134. }
  135. int DIAMONDAPI LCIFlushCompressorOutput(LCI_CONTEXT_HANDLE hmc)
  136. {
  137. PMCC_CONTEXT context; /* pointer to context */
  138. context = PMCCfromHMC(hmc); /* get pointer from handle */
  139. if (context->signature != LCI_SIGNATURE)
  140. {
  141. return(MCI_ERROR_BAD_PARAMETERS); /* missing signature */
  142. }
  143. (void) LZX_EncodeFlush(context->encoder_context);
  144. return MCI_ERROR_NO_ERROR;
  145. }
  146. /* --- LCIResetCompression() ---------------------------------------------- */
  147. int DIAMONDAPI LCIResetCompression(LCI_CONTEXT_HANDLE hmc)
  148. {
  149. PMCC_CONTEXT context; /* pointer to the context */
  150. context = PMCCfromHMC(hmc); /* get pointer from handle */
  151. if (context->signature != LCI_SIGNATURE)
  152. {
  153. return(MCI_ERROR_BAD_PARAMETERS); /* missing signature */
  154. }
  155. LZX_EncodeNewGroup(context->encoder_context);
  156. return(MCI_ERROR_NO_ERROR); /* if tag is OK */
  157. }
  158. /* --- LCIDestroyCompression() -------------------------------------------- */
  159. int DIAMONDAPI LCIDestroyCompression(LCI_CONTEXT_HANDLE hmc)
  160. {
  161. PMCC_CONTEXT context; /* pointer to context */
  162. context = PMCCfromHMC(hmc); /* get pointer from handle */
  163. if (context->signature != LCI_SIGNATURE)
  164. {
  165. return(MCI_ERROR_BAD_PARAMETERS); /* missing signature */
  166. }
  167. LZX_EncodeFree(context->encoder_context);
  168. context->pfnFree(context->encoder_context);
  169. context->signature = BAD_SIGNATURE; /* destroy signature */
  170. context->pfnFree(context); /* self-destruct */
  171. return(MCI_ERROR_NO_ERROR); /* success */
  172. }
  173. int DIAMONDAPI LCISetTranslationSize(LCI_CONTEXT_HANDLE hmc, unsigned long size)
  174. {
  175. PMCC_CONTEXT context; /* pointer to context */
  176. context = PMCCfromHMC(hmc); /* get pointer from handle */
  177. if (context->signature != LCI_SIGNATURE)
  178. {
  179. return(MCI_ERROR_BAD_PARAMETERS); /* missing signature */
  180. }
  181. context->file_translation_size = size;
  182. return(MCI_ERROR_NO_ERROR); /* success */
  183. }
  184. unsigned char * FAR DIAMONDAPI LCIGetInputData(
  185. LCI_CONTEXT_HANDLE hmc,
  186. unsigned long *input_position,
  187. unsigned long *bytes_available
  188. )
  189. {
  190. PMCC_CONTEXT context; /* pointer to context */
  191. context = PMCCfromHMC(hmc); /* get pointer from handle */
  192. if (context->signature != LCI_SIGNATURE)
  193. {
  194. *bytes_available = 0;
  195. return (unsigned char *) NULL;
  196. }
  197. return LZX_GetInputData(context->encoder_context, input_position, bytes_available);
  198. }