"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/Buffer.h" (10 Oct 2018, 3638 Bytes) of package /windows/misc/VeraCrypt_1.23-Hotfix-2_Source.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Buffer.h" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.18_Source_vs_1.19_Source.

    1 /*
    2  Derived from source code of TrueCrypt 7.1a, which is
    3  Copyright (c) 2008-2012 TrueCrypt Developers Association and which is governed
    4  by the TrueCrypt License 3.0.
    5 
    6  Modifications and additions to the original source code (contained in this file)
    7  and all other portions of this file are Copyright (c) 2013-2017 IDRIX
    8  and are governed by the Apache License 2.0 the full text of which is
    9  contained in the file License.txt included in VeraCrypt binary and source
   10  code distribution packages.
   11 */
   12 
   13 #ifndef TC_HEADER_Platform_Buffer
   14 #define TC_HEADER_Platform_Buffer
   15 
   16 #include "PlatformBase.h"
   17 #include "Memory.h"
   18 
   19 namespace VeraCrypt
   20 {
   21 
   22     class ConstBufferPtr
   23     {
   24     public:
   25         ConstBufferPtr ()
   26             : DataPtr (nullptr), DataSize (0) { }
   27         ConstBufferPtr (const byte *data, size_t size)
   28             : DataPtr (data), DataSize (size) { }
   29         virtual ~ConstBufferPtr () { }
   30 
   31         operator const byte * () const { return DataPtr; }
   32 
   33         bool IsDataEqual (const ConstBufferPtr &other) const { return Memory::Compare (DataPtr, DataSize, other.DataPtr, other.DataSize) == 0; }
   34         const byte *Get () const { return DataPtr; }
   35         ConstBufferPtr GetRange (size_t offset, size_t size) const;
   36         void Set (const byte *data, size_t size) { DataPtr = data; DataSize = size; }
   37         size_t Size () const { return DataSize; }
   38 
   39     protected:
   40         const byte *DataPtr;
   41         size_t DataSize;
   42     };
   43 
   44 
   45     class BufferPtr
   46     {
   47     public:
   48         BufferPtr ()
   49             : DataPtr (nullptr), DataSize (0) { }
   50         BufferPtr (byte *data, size_t size)
   51             : DataPtr (data), DataSize (size) { }
   52         virtual ~BufferPtr () { }
   53 
   54         operator byte * () const { return DataPtr; }
   55         void CopyFrom (const ConstBufferPtr &bufferPtr) const;
   56         void Erase () const { Zero(); }
   57         byte *Get () const { return DataPtr; }
   58         BufferPtr GetRange (size_t offset, size_t size) const;
   59         void Set (byte *data, size_t size) { DataPtr = data; DataSize = size; }
   60         size_t Size () const { return DataSize; }
   61         void Zero () const { Memory::Zero (DataPtr, DataSize); }
   62 
   63         operator ConstBufferPtr () const { return ConstBufferPtr (DataPtr, DataSize); }
   64 
   65     protected:
   66         byte *DataPtr;
   67         size_t DataSize;
   68     };
   69 
   70     class Buffer
   71     {
   72     public:
   73         Buffer ();
   74         Buffer (size_t size, size_t alignment = 0);
   75         Buffer (const ConstBufferPtr &bufferPtr, size_t alignment = 0) { CopyFrom (bufferPtr, alignment); }
   76         virtual ~Buffer ();
   77 
   78         virtual void Allocate (size_t size, size_t alignment = 0);
   79         virtual void CopyFrom (const ConstBufferPtr &bufferPtr, size_t alignment = 0);
   80         virtual byte *Ptr () const { return DataPtr; }
   81         virtual void Erase ();
   82         virtual void Free ();
   83         virtual BufferPtr GetRange (size_t offset, size_t size) const;
   84         virtual size_t Size () const { return DataSize; }
   85         virtual size_t Alignment () const { return DataAlignment; }
   86         virtual bool IsAllocated () const { return DataSize != 0; }
   87         virtual void Zero ();
   88 
   89         virtual operator byte * () const { return DataPtr; }
   90         virtual operator BufferPtr () const { return BufferPtr (DataPtr, DataSize); }
   91         virtual operator ConstBufferPtr () const { return ConstBufferPtr (DataPtr, DataSize); }
   92 
   93     protected:
   94         byte *DataPtr;
   95         size_t DataSize;
   96         size_t DataAlignment;
   97 
   98     private:
   99         Buffer (const Buffer &);
  100         Buffer &operator= (const Buffer &);
  101     };
  102 
  103     class SecureBuffer : public Buffer
  104     {
  105     public:
  106         SecureBuffer () { }
  107         SecureBuffer (size_t size, size_t alignment = 0);
  108         SecureBuffer (const ConstBufferPtr &bufferPtr) { CopyFrom (bufferPtr); }
  109         virtual ~SecureBuffer ();
  110 
  111         virtual void Allocate (size_t size, size_t alignment = 0);
  112         virtual void Free ();
  113 
  114     private:
  115         SecureBuffer (const SecureBuffer &);
  116         SecureBuffer &operator= (const SecureBuffer &);
  117     };
  118 
  119 }
  120 
  121 #endif // TC_HEADER_Platform_Buffer