tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

InBuffer.h (2075B)


      1 // InBuffer.h
      2 
      3 #ifndef __IN_BUFFER_H
      4 #define __IN_BUFFER_H
      5 
      6 #include "../../Common/MyException.h"
      7 #include "../IStream.h"
      8 
      9 #ifndef _NO_EXCEPTIONS
     10 struct CInBufferException: public CSystemException
     11 {
     12  CInBufferException(HRESULT errorCode): CSystemException(errorCode) {}
     13 };
     14 #endif
     15 
     16 class CInBufferBase
     17 {
     18 protected:
     19  Byte *_buf;
     20  Byte *_bufLim;
     21  Byte *_bufBase;
     22 
     23  ISequentialInStream *_stream;
     24  UInt64 _processedSize;
     25  size_t _bufSize; // actually it's number of Bytes for next read. The buf can be larger
     26                   // only up to 32-bits values now are supported!
     27  bool _wasFinished;
     28 
     29  bool ReadBlock();
     30  bool ReadByte_FromNewBlock(Byte &b);
     31  Byte ReadByte_FromNewBlock();
     32 
     33 public:
     34  #ifdef _NO_EXCEPTIONS
     35  HRESULT ErrorCode;
     36  #endif
     37  UInt32 NumExtraBytes;
     38 
     39  CInBufferBase() throw();
     40 
     41  UInt64 GetStreamSize() const { return _processedSize + (_buf - _bufBase); }
     42  UInt64 GetProcessedSize() const { return _processedSize + NumExtraBytes + (_buf - _bufBase); }
     43  bool WasFinished() const { return _wasFinished; }
     44 
     45  void SetStream(ISequentialInStream *stream) { _stream = stream; }
     46  
     47  void SetBuf(Byte *buf, size_t bufSize, size_t end, size_t pos)
     48  {
     49    _bufBase = buf;
     50    _bufSize = bufSize;
     51    _processedSize = 0;
     52    _buf = buf + pos;
     53    _bufLim = buf + end;
     54    _wasFinished = false;
     55    #ifdef _NO_EXCEPTIONS
     56    ErrorCode = S_OK;
     57    #endif
     58    NumExtraBytes = 0;
     59  }
     60 
     61  void Init() throw();
     62  
     63  MY_FORCE_INLINE
     64  bool ReadByte(Byte &b)
     65  {
     66    if (_buf >= _bufLim)
     67      return ReadByte_FromNewBlock(b);
     68    b = *_buf++;
     69    return true;
     70  }
     71  
     72  MY_FORCE_INLINE
     73  Byte ReadByte()
     74  {
     75    if (_buf >= _bufLim)
     76      return ReadByte_FromNewBlock();
     77    return *_buf++;
     78  }
     79  
     80  size_t ReadBytes(Byte *buf, size_t size);
     81  size_t Skip(size_t size);
     82 };
     83 
     84 class CInBuffer: public CInBufferBase
     85 {
     86 public:
     87  ~CInBuffer() { Free(); }
     88  bool Create(size_t bufSize) throw(); // only up to 32-bits values now are supported!
     89  void Free() throw();
     90 };
     91 
     92 #endif