InputStream.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Copyright (C) Teemu Suutari */
  2. #ifndef INPUTSTREAM_HPP
  3. #define INPUTSTREAM_HPP
  4. #include <cstddef>
  5. #include <cstdint>
  6. #include <algorithm>
  7. #include "common/Buffer.hpp"
  8. namespace ancient::internal
  9. {
  10. class BackwardInputStream;
  11. class ForwardInputStream
  12. {
  13. friend class BackwardInputStream;
  14. public:
  15. ForwardInputStream(const Buffer &buffer,size_t startOffset,size_t endOffset,bool allowOverrun=false);
  16. ~ForwardInputStream();
  17. uint8_t readByte();
  18. const uint8_t *consume(size_t bytes,uint8_t *buffer=nullptr);
  19. bool eof() const { return _currentOffset==_endOffset; }
  20. size_t getOffset() const { return _currentOffset; }
  21. size_t getEndOffset() const { return _endOffset; }
  22. void link(BackwardInputStream &stream) { _linkedInputStream=&stream; }
  23. private:
  24. void setOffset(size_t offset) { _endOffset=offset; }
  25. const uint8_t *_bufPtr;
  26. size_t _currentOffset;
  27. size_t _endOffset;
  28. bool _allowOverrun;
  29. BackwardInputStream *_linkedInputStream=nullptr;
  30. };
  31. class BackwardInputStream
  32. {
  33. friend class ForwardInputStream;
  34. public:
  35. BackwardInputStream(const Buffer &buffer,size_t startOffset,size_t endOffset,bool allowOverrun=false);
  36. ~BackwardInputStream();
  37. uint8_t readByte();
  38. const uint8_t *consume(size_t bytes,uint8_t *buffer=nullptr);
  39. bool eof() const { return _currentOffset==_endOffset; }
  40. size_t getOffset() const { return _currentOffset; }
  41. void link(ForwardInputStream &stream) { _linkedInputStream=&stream; }
  42. private:
  43. void setOffset(size_t offset) { _endOffset=offset; }
  44. const uint8_t *_bufPtr;
  45. size_t _currentOffset;
  46. size_t _endOffset;
  47. bool _allowOverrun;
  48. ForwardInputStream *_linkedInputStream=nullptr;
  49. };
  50. template<typename T>
  51. class LSBBitReader
  52. {
  53. public:
  54. LSBBitReader(T &inputStream) :
  55. _inputStream(inputStream)
  56. {
  57. // nothing needed
  58. }
  59. ~LSBBitReader()
  60. {
  61. // nothing needed
  62. }
  63. uint32_t readBits8(uint32_t count)
  64. {
  65. return readBitsInternal(count,[&](){
  66. _bufContent=_inputStream.readByte();
  67. _bufLength=8;
  68. });
  69. }
  70. uint32_t readBitsBE16(uint32_t count)
  71. {
  72. return readBitsInternal(count,[&](){
  73. uint8_t tmp[2];
  74. const uint8_t *buf=_inputStream.consume(2,tmp);
  75. _bufContent=(uint32_t(buf[0])<<8)|uint32_t(buf[1]);
  76. _bufLength=16;
  77. });
  78. }
  79. uint32_t readBitsBE32(uint32_t count)
  80. {
  81. return readBitsInternal(count,[&](){
  82. uint8_t tmp[4];
  83. const uint8_t *buf=_inputStream.consume(4,tmp);
  84. _bufContent=(uint32_t(buf[0])<<24)|(uint32_t(buf[1])<<16)|
  85. (uint32_t(buf[2])<<8)|uint32_t(buf[3]);
  86. _bufLength=32;
  87. });
  88. }
  89. // RNC
  90. uint32_t readBits16Limit(uint32_t count)
  91. {
  92. return readBitsInternal(count,[&](){
  93. _bufContent=_inputStream.readByte();
  94. if (_inputStream.eof())
  95. {
  96. _bufLength=8;
  97. } else {
  98. _bufContent=_bufContent|(uint32_t(_inputStream.readByte())<<8);
  99. _bufLength=16;
  100. }
  101. });
  102. }
  103. void reset(uint32_t bufContent=0,uint8_t bufLength=0)
  104. {
  105. _bufContent=bufContent;
  106. _bufLength=bufLength;
  107. }
  108. private:
  109. template<typename F>
  110. uint32_t readBitsInternal(uint32_t count,F readWord)
  111. {
  112. uint32_t ret=0,pos=0;
  113. while (count)
  114. {
  115. if (!_bufLength)
  116. readWord();
  117. uint8_t maxCount=std::min(uint8_t(count),_bufLength);
  118. ret|=(_bufContent&((1<<maxCount)-1))<<pos;
  119. _bufContent>>=maxCount;
  120. _bufLength-=maxCount;
  121. count-=maxCount;
  122. pos+=maxCount;
  123. }
  124. return ret;
  125. }
  126. T &_inputStream;
  127. uint32_t _bufContent=0;
  128. uint8_t _bufLength=0;
  129. };
  130. template<typename T>
  131. class MSBBitReader
  132. {
  133. public:
  134. MSBBitReader(T &inputStream) :
  135. _inputStream(inputStream)
  136. {
  137. // nothing needed
  138. }
  139. ~MSBBitReader()
  140. {
  141. // nothing needed
  142. }
  143. uint32_t readBits8(uint32_t count)
  144. {
  145. return readBitsInternal(count,[&](){
  146. _bufContent=_inputStream.readByte();
  147. _bufLength=8;
  148. });
  149. }
  150. uint32_t readBitsBE16(uint32_t count)
  151. {
  152. return readBitsInternal(count,[&](){
  153. uint8_t tmp[2];
  154. const uint8_t *buf=_inputStream.consume(2,tmp);
  155. _bufContent=(uint32_t(buf[0])<<8)|uint32_t(buf[1]);
  156. _bufLength=16;
  157. });
  158. }
  159. uint32_t readBitsBE32(uint32_t count)
  160. {
  161. return readBitsInternal(count,[&](){
  162. uint8_t tmp[4];
  163. const uint8_t *buf=_inputStream.consume(4,tmp);
  164. _bufContent=(uint32_t(buf[0])<<24)|(uint32_t(buf[1])<<16)|
  165. (uint32_t(buf[2])<<8)|uint32_t(buf[3]);
  166. _bufLength=32;
  167. });
  168. }
  169. void reset(uint32_t bufContent=0,uint8_t bufLength=0)
  170. {
  171. _bufContent=bufContent;
  172. _bufLength=bufLength;
  173. }
  174. private:
  175. template<typename F>
  176. uint32_t readBitsInternal(uint32_t count,F readWord)
  177. {
  178. uint32_t ret=0;
  179. while (count)
  180. {
  181. if (!_bufLength)
  182. readWord();
  183. uint8_t maxCount=std::min(uint8_t(count),_bufLength);
  184. _bufLength-=maxCount;
  185. ret=(ret<<maxCount)|((_bufContent>>_bufLength)&((1<<maxCount)-1));
  186. count-=maxCount;
  187. }
  188. return ret;
  189. }
  190. T &_inputStream;
  191. uint32_t _bufContent=0;
  192. uint8_t _bufLength=0;
  193. };
  194. }
  195. #endif