"Fossies" - the Fresh Open Source Software Archive

Member "src/Platform/Buffer.cpp" (10 Oct 2018, 3433 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.cpp" 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 #include "Buffer.h"
   14 #include "Exception.h"
   15 
   16 namespace VeraCrypt
   17 {
   18     Buffer::Buffer () : DataPtr (nullptr), DataSize (0), DataAlignment (0)
   19     {
   20     }
   21 
   22     Buffer::Buffer (size_t size, size_t alignment) : DataPtr (nullptr), DataSize (0), DataAlignment (0)
   23     {
   24         Allocate (size, alignment);
   25     }
   26 
   27     Buffer::~Buffer ()
   28     {
   29         if (DataPtr != nullptr)
   30             Free ();
   31     }
   32 
   33     void Buffer::Allocate (size_t size, size_t alignment)
   34     {
   35         if (size < 1)
   36             throw ParameterIncorrect (SRC_POS);
   37 
   38         if (DataPtr != nullptr)
   39         {
   40             if ((DataSize == size) && (DataAlignment == alignment))
   41                 return;
   42             Free();
   43         }
   44 
   45         try
   46         {
   47             DataPtr = static_cast<byte *> ((alignment > 0)? Memory::AllocateAligned (size, alignment) : Memory::Allocate (size));
   48             DataSize = size;
   49             DataAlignment = alignment;
   50         }
   51         catch (...)
   52         {
   53             DataPtr = nullptr;
   54             DataSize = 0;
   55             DataAlignment = 0;
   56             throw;
   57         }
   58     }
   59 
   60     void Buffer::CopyFrom (const ConstBufferPtr &bufferPtr, size_t alignment)
   61     {
   62         if (!IsAllocated () || ((bufferPtr.Size()) && (DataAlignment != alignment)))
   63         {
   64             if (IsAllocated ())
   65                 Free ();
   66 
   67             if (bufferPtr.Size())
   68                 Allocate (bufferPtr.Size(), alignment);
   69         }
   70         else if (bufferPtr.Size() > DataSize)
   71             throw ParameterTooLarge (SRC_POS);
   72 
   73         if (bufferPtr.Size())
   74             Memory::Copy (DataPtr, bufferPtr.Get(), bufferPtr.Size());
   75     }
   76 
   77     void Buffer::Erase ()
   78     {
   79         if (DataSize > 0)
   80             Memory::Erase (DataPtr, DataSize);
   81     }
   82 
   83     void Buffer::Free ()
   84     {
   85         if (DataPtr == nullptr)
   86             throw NotInitialized (SRC_POS);
   87 
   88         if (DataAlignment > 0)
   89             Memory::FreeAligned (DataPtr);
   90         else
   91             Memory::Free (DataPtr);
   92         DataPtr = nullptr;
   93         DataSize = 0;
   94         DataAlignment = 0;
   95     }
   96 
   97     BufferPtr Buffer::GetRange (size_t offset, size_t size) const
   98     {
   99         if (offset + size > DataSize)
  100             throw ParameterIncorrect (SRC_POS);
  101 
  102         return BufferPtr (DataPtr + offset, size);
  103     }
  104 
  105     void Buffer::Zero ()
  106     {
  107         if (DataSize > 0)
  108             Memory::Zero (DataPtr, DataSize);
  109     }
  110 
  111     SecureBuffer::SecureBuffer (size_t size, size_t alignment)
  112     {
  113         Allocate (size, alignment);
  114     }
  115 
  116     SecureBuffer::~SecureBuffer ()
  117     {
  118         if (DataPtr != nullptr && DataSize != 0)
  119             Free ();
  120     }
  121 
  122     void SecureBuffer::Allocate (size_t size, size_t alignment)
  123     {
  124         Buffer::Allocate (size, alignment);
  125     }
  126 
  127     void SecureBuffer::Free ()
  128     {
  129         if (DataPtr == nullptr)
  130             throw NotInitialized (SRC_POS);
  131 
  132         Erase ();
  133         Buffer::Free ();
  134     }
  135 
  136     void BufferPtr::CopyFrom (const ConstBufferPtr &bufferPtr) const
  137     {
  138         if (bufferPtr.Size() > DataSize)
  139             throw ParameterTooLarge (SRC_POS);
  140 
  141         Memory::Copy (DataPtr, bufferPtr.Get(), bufferPtr.Size());
  142     }
  143 
  144     BufferPtr BufferPtr::GetRange (size_t offset, size_t size) const
  145     {
  146         if (offset + size > DataSize)
  147             throw ParameterIncorrect (SRC_POS);
  148 
  149         return BufferPtr (DataPtr + offset, size);
  150     }
  151 
  152     ConstBufferPtr ConstBufferPtr::GetRange (size_t offset, size_t size) const
  153     {
  154         if (offset + size > DataSize)
  155             throw ParameterIncorrect (SRC_POS);
  156 
  157         return ConstBufferPtr (DataPtr + offset, size);
  158     }
  159 }