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.

176 lines
3.5 KiB

  1. /*
  2. * api.c
  3. *
  4. * Externally declared APIs
  5. */
  6. #include <stdio.h>
  7. #include <crtdbg.h>
  8. #define DECLARE_DATA
  9. #include "inflate.h"
  10. #include "deflate.h"
  11. #include "api_int.h"
  12. #include "infgzip.h"
  13. #include "fasttbl.h"
  14. #include "crc32.h"
  15. //
  16. // Initialise global compression
  17. //
  18. HRESULT WINAPI InitCompression(VOID)
  19. {
  20. inflateInit();
  21. return S_OK;
  22. }
  23. //
  24. // Initialise global decompression
  25. //
  26. HRESULT WINAPI InitDecompression(VOID)
  27. {
  28. deflateInit();
  29. return S_OK;
  30. }
  31. //
  32. // De-init global compression
  33. //
  34. VOID WINAPI DeInitCompression(VOID)
  35. {
  36. }
  37. //
  38. // De-init global decompression
  39. //
  40. VOID WINAPI DeInitDecompression(VOID)
  41. {
  42. }
  43. //
  44. // Create a compression context
  45. //
  46. HRESULT WINAPI CreateCompression(PVOID *context, ULONG flags)
  47. {
  48. t_encoder_context *ec;
  49. *context = (PVOID) LocalAlloc(LMEM_FIXED, sizeof(t_encoder_context));
  50. if (*context == NULL)
  51. return E_OUTOFMEMORY;
  52. ec = (t_encoder_context *) (*context);
  53. // no encoders initialised yet
  54. ec->std_encoder = NULL;
  55. ec->optimal_encoder = NULL;
  56. ec->fast_encoder = NULL;
  57. if (flags & COMPRESSION_FLAG_DO_GZIP)
  58. ec->using_gzip = TRUE;
  59. else
  60. ec->using_gzip = FALSE;
  61. InternalResetCompression(ec);
  62. return S_OK;
  63. }
  64. //
  65. // Destroy a compression context
  66. //
  67. VOID WINAPI DestroyCompression(PVOID void_context)
  68. {
  69. t_encoder_context *context = (t_encoder_context *) void_context;
  70. _ASSERT(void_context != NULL);
  71. if (context->std_encoder != NULL)
  72. LocalFree((PVOID) context->std_encoder);
  73. if (context->optimal_encoder != NULL)
  74. LocalFree((PVOID) context->optimal_encoder);
  75. if (context->fast_encoder != NULL)
  76. LocalFree((PVOID) context->fast_encoder);
  77. LocalFree(void_context);
  78. }
  79. //
  80. // Create a decompression context
  81. //
  82. HRESULT WINAPI CreateDecompression(PVOID *context, ULONG flags)
  83. {
  84. *context = (PVOID) LocalAlloc(LMEM_FIXED, sizeof(t_decoder_context));
  85. if (*context == NULL)
  86. return E_OUTOFMEMORY;
  87. if (flags & DECOMPRESSION_FLAG_DO_GZIP)
  88. ((t_decoder_context *) (*context))->using_gzip = TRUE;
  89. else
  90. ((t_decoder_context *) (*context))->using_gzip = FALSE;
  91. return ResetDecompression(*context);
  92. }
  93. //
  94. // Destroy decompression context
  95. //
  96. VOID WINAPI DestroyDecompression(PVOID void_context)
  97. {
  98. LocalFree(void_context);
  99. }
  100. //
  101. // Reset compression context
  102. //
  103. HRESULT WINAPI ResetCompression(PVOID void_context)
  104. {
  105. t_encoder_context *context = (t_encoder_context *) void_context;
  106. InternalResetCompression(context);
  107. // BUGBUG This forces a realloc of the particular compressor we are using
  108. // each time we reset, but if we don't do this then we are stuck with one
  109. // compressor (fast,std,optimal) forever until we destroy the context.
  110. // Should create a workaround for this problem. Luckily, IIS creates a
  111. // new context all the time, and doesn't call reset (so says davidtr).
  112. DestroyIndividualCompressors(context);
  113. return S_OK;
  114. }
  115. //
  116. // Reset decompression context
  117. //
  118. HRESULT WINAPI ResetDecompression(PVOID void_context)
  119. {
  120. t_decoder_context *context = (t_decoder_context *) void_context;
  121. if (context->using_gzip)
  122. {
  123. context->state = STATE_READING_GZIP_HEADER;
  124. context->gzip_header_substate = 0;
  125. DecoderInitGzipVariables(context);
  126. }
  127. else
  128. {
  129. context->state = STATE_READING_BFINAL_NEED_TO_INIT_BITBUF;
  130. }
  131. context->bufpos = 0;
  132. context->bitcount = -16;
  133. return S_OK;
  134. }