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.

161 lines
3.7 KiB

  1. /*
  2. * decapi.c
  3. *
  4. * API entry points.
  5. */
  6. #define ALLOC_VARS
  7. #include "decoder.h"
  8. #include <stdio.h>
  9. #include <memory.h>
  10. #pragma intrinsic(memcpy)
  11. bool __stdcall LZX_DecodeInit(
  12. t_decoder_context **dec_context,
  13. long compression_window_size,
  14. PFNALLOC pfnma,
  15. HANDLE hAllocator
  16. )
  17. {
  18. t_decoder_context *context= pfnma( hAllocator, sizeof( t_decoder_context ));
  19. if ( context == NULL ) {
  20. return false;
  21. }
  22. *dec_context = context;
  23. context->dec_malloc = pfnma;
  24. context->dec_mallochandle = hAllocator;
  25. context->dec_window_size = compression_window_size;
  26. context->dec_window_mask = context->dec_window_size - 1;
  27. /*
  28. * Window size must be a power of 2
  29. */
  30. if (context->dec_window_size & context->dec_window_mask)
  31. return false;
  32. if (allocate_decompression_memory(context) == false)
  33. return false;
  34. LZX_DecodeNewGroup(context);
  35. return true;
  36. }
  37. void __fastcall LZX_DecodeNewGroup(t_decoder_context *context)
  38. {
  39. reset_decoder_trees(context);
  40. decoder_misc_init(context);
  41. init_decoder_translation(context);
  42. context->dec_num_cfdata_frames = 0;
  43. }
  44. int __stdcall LZX_Decode(
  45. t_decoder_context *context,
  46. long bytes_to_decode,
  47. byte * compressed_input_buffer,
  48. long compressed_input_size,
  49. byte * uncompressed_output_buffer,
  50. long uncompressed_output_size,
  51. long * bytes_decoded
  52. )
  53. {
  54. long result;
  55. context->dec_input_curpos = compressed_input_buffer;
  56. context->dec_end_input_pos = (compressed_input_buffer + compressed_input_size + 4);
  57. context->dec_output_buffer = uncompressed_output_buffer;
  58. init_decoder_input(context);
  59. result = decode_data(context, bytes_to_decode);
  60. context->dec_num_cfdata_frames++;
  61. if (result < 0)
  62. {
  63. *bytes_decoded = 0;
  64. return 1; /* failure */
  65. }
  66. else
  67. {
  68. *bytes_decoded = result;
  69. context->dec_position_at_start += result;
  70. return 0; /* success */
  71. }
  72. }
  73. //
  74. // Warning, this dictionary is inserted verbatim, and is not E8
  75. // translated. If the encoder did E8 translation on its preloaded
  76. // dictionary, this won't work.
  77. //
  78. #ifdef TRACING
  79. ulong TracingOldDataSize;
  80. #endif
  81. #define ROUNDUP2( x, n ) ((((ULONG)(x)) + (((ULONG)(n)) - 1 )) & ~(((ULONG)(n)) - 1 ))
  82. bool __fastcall LZX_DecodeInsertDictionary(
  83. t_decoder_context * context,
  84. const byte * data,
  85. unsigned long data_size
  86. )
  87. {
  88. if (data_size > context->dec_window_size)
  89. return false;
  90. #ifdef TRACING
  91. TracingOldDataSize = context->dec_window_size + ROUNDUP2( data_size, CHUNK_SIZE );
  92. #endif
  93. memcpy(
  94. &context->dec_mem_window[context->dec_window_size - data_size],
  95. data,
  96. data_size
  97. );
  98. return true;
  99. }
  100. #ifdef TRACING
  101. void
  102. __stdcall
  103. TracingMatch(
  104. ulong BufPos,
  105. ulong MatchPos,
  106. ulong WindowSize,
  107. ulong MatchLength,
  108. ulong MatchPosSlot
  109. )
  110. {
  111. printf( "MATCH: At %08X, From %08X, Length %5d\n", BufPos + TracingOldDataSize, MatchPos + TracingOldDataSize, MatchLength );
  112. }
  113. void
  114. __stdcall
  115. TracingLiteral(
  116. ulong BufPos,
  117. ulong ch
  118. )
  119. {
  120. printf( "LITER: At %08X, 0x%02X\n", BufPos + TracingOldDataSize, ch );
  121. }
  122. #endif /* TRACING */