getbits.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "rar.hpp"
  2. BitInput::BitInput(bool AllocBuffer)
  3. {
  4. ExternalBuffer=false;
  5. if (AllocBuffer)
  6. {
  7. // getbits32 attempts to read data from InAddr, ... InAddr+3 positions.
  8. // So let's allocate 3 additional bytes for situation, when we need to
  9. // read only 1 byte from the last position of buffer and avoid a crash
  10. // from access to next 3 bytes, which contents we do not need.
  11. size_t BufSize=MAX_SIZE+3;
  12. InBuf=new byte[BufSize];
  13. // Ensure that we get predictable results when accessing bytes in area
  14. // not filled with read data.
  15. memset(InBuf,0,BufSize);
  16. }
  17. else
  18. InBuf=NULL;
  19. }
  20. BitInput::~BitInput()
  21. {
  22. if (!ExternalBuffer)
  23. delete[] InBuf;
  24. }
  25. void BitInput::faddbits(uint Bits)
  26. {
  27. // Function wrapped version of inline addbits to save code size.
  28. addbits(Bits);
  29. }
  30. uint BitInput::fgetbits()
  31. {
  32. // Function wrapped version of inline getbits to save code size.
  33. return getbits();
  34. }
  35. void BitInput::SetExternalBuffer(byte *Buf)
  36. {
  37. if (InBuf!=NULL && !ExternalBuffer)
  38. delete[] InBuf;
  39. InBuf=Buf;
  40. ExternalBuffer=true;
  41. }