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.

165 lines
3.5 KiB

  1. /*
  2. * encapi.c
  3. *
  4. * Encoder API entrypoints.
  5. */
  6. #define ALLOC_VARS
  7. #include "encoder.h"
  8. bool LZX_EncodeInit(
  9. t_encoder_context *context,
  10. long compression_window_size,
  11. long second_partition_size,
  12. PFNALLOC pfnma,
  13. PFNFREE pfnmf,
  14. int FAR (DIAMONDAPI *pfnlzx_output_callback)(
  15. void * pfol,
  16. unsigned char * compressed_data,
  17. long compressed_size,
  18. long uncompressed_size
  19. ),
  20. void *fci_data
  21. )
  22. {
  23. #ifdef _DEBUG
  24. _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE);
  25. _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT);
  26. _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
  27. _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDOUT);
  28. _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
  29. _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDOUT);
  30. #endif
  31. /* to pass back in lzx_output_callback() */
  32. context->enc_fci_data = fci_data;
  33. context->enc_window_size = compression_window_size;
  34. /*
  35. * The second partition size must be a multiple of 32K
  36. */
  37. if (second_partition_size & (CHUNK_SIZE-1))
  38. second_partition_size &= (~(CHUNK_SIZE-1));
  39. /*
  40. * The minimum allowed is 32K because of the way that
  41. * our translation works.
  42. */
  43. if (second_partition_size < CHUNK_SIZE)
  44. second_partition_size = CHUNK_SIZE;
  45. /*
  46. * Our window size must be at least 32K
  47. */
  48. if (compression_window_size < CHUNK_SIZE)
  49. return false;
  50. context->enc_encoder_second_partition_size = second_partition_size;
  51. context->enc_output_callback_function = pfnlzx_output_callback;
  52. context->enc_malloc = pfnma;
  53. context->enc_free = pfnmf;
  54. /* Error allocating memory? */
  55. if (comp_alloc_compress_memory(context) == false)
  56. return false;
  57. LZX_EncodeNewGroup(context);
  58. return true;
  59. }
  60. /*
  61. * Cleanup (frees memory)
  62. */
  63. void LZX_EncodeFree(t_encoder_context *context)
  64. {
  65. comp_free_compress_memory(context);
  66. }
  67. /*
  68. * Sets up the encoder for a new group of files.
  69. *
  70. * All this does is reset the lookup table, re-initialise to the
  71. * default match estimation tables for the optimal parser, and
  72. * reset a few variables.
  73. */
  74. void LZX_EncodeNewGroup(t_encoder_context *context)
  75. {
  76. init_compression_memory(context);
  77. }
  78. long LZX_Encode(
  79. t_encoder_context *context,
  80. byte *input_data,
  81. long input_size,
  82. long *estimated_bytes_compressed,
  83. long file_size_for_translation
  84. )
  85. {
  86. context->enc_input_ptr = input_data;
  87. context->enc_input_left = input_size;
  88. context->enc_file_size_for_translation = file_size_for_translation;
  89. /* perform the encoding */
  90. encoder_start(context);
  91. if (context->enc_output_overflow)
  92. {
  93. *estimated_bytes_compressed = 0;
  94. return ENCODER_WRITE_FAILURE;
  95. }
  96. *estimated_bytes_compressed = estimate_buffer_contents(context);
  97. return ENCODER_SUCCESS;
  98. }
  99. bool LZX_EncodeFlush(t_encoder_context *context)
  100. {
  101. flush_all_pending_blocks(context);
  102. if (context->enc_output_overflow)
  103. return false;
  104. return true;
  105. }
  106. unsigned char *LZX_GetInputData(
  107. t_encoder_context *context,
  108. unsigned long *input_position,
  109. unsigned long *bytes_available
  110. )
  111. {
  112. unsigned long filepos;
  113. // note that BufPos-window_size is the real position in the file
  114. filepos = context->enc_BufPos - context->enc_window_size;
  115. if (filepos < context->enc_window_size)
  116. {
  117. *input_position = 0;
  118. *bytes_available = filepos;
  119. return &context->enc_MemWindow[context->enc_window_size];
  120. }
  121. else
  122. {
  123. *input_position = filepos - context->enc_window_size;
  124. *bytes_available = context->enc_window_size;
  125. return &context->enc_MemWindow[context->enc_BufPos - context->enc_window_size];
  126. }
  127. }