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.

78 lines
1.5 KiB

  1. //
  2. // infinput.c
  3. //
  4. // Bitwise inputting for inflate (decompressor)
  5. //
  6. #include <stdio.h>
  7. #include <crtdbg.h>
  8. #include "inflate.h"
  9. #include "infmacro.h"
  10. void dumpBits(t_decoder_context *context, int n)
  11. {
  12. context->bitbuf >>= n;
  13. context->bitcount -= n;
  14. }
  15. // retrieve n bits from the bit buffer, and dump them when done
  16. // n can be up to 16
  17. int getBits(t_decoder_context *context, int n)
  18. {
  19. int result;
  20. context->bitcount -= n;
  21. result = (context->bitbuf & g_BitMask[n]);
  22. context->bitbuf >>= n;
  23. return result;
  24. }
  25. //
  26. // Ensure that <num_bits> bits are in the bit buffer
  27. //
  28. // Returns FALSE if there are not and there was insufficient input to make this true
  29. //
  30. BOOL ensureBitsContext(t_decoder_context *context, int num_bits)
  31. {
  32. if (context->bitcount + 16 < num_bits)
  33. {
  34. if (INPUT_EOF())
  35. return FALSE;
  36. context->bitbuf |= ((*context->input_curpos++) << (context->bitcount+16));
  37. context->bitcount += 8;
  38. if (context->bitcount + 16 < num_bits)
  39. {
  40. if (INPUT_EOF())
  41. return FALSE;
  42. context->bitbuf |= ((*context->input_curpos++) << (context->bitcount+16));
  43. context->bitcount += 8;
  44. }
  45. }
  46. return TRUE;
  47. }
  48. // initialise the bit buffer
  49. BOOL initBitBuffer(t_decoder_context *context)
  50. {
  51. if (context->input_curpos < context->end_input_buffer)
  52. {
  53. context->bitbuf = *context->input_curpos++;
  54. context->bitcount = -8;
  55. context->state = STATE_READING_BFINAL;
  56. return TRUE;
  57. }
  58. else
  59. {
  60. context->bitcount = -16;
  61. context->bitbuf = 0;
  62. return FALSE;
  63. }
  64. }