Update README.md
[nemesis.git] / Platform / Buffer.cpp
1 /*
2  Copyright (c) 2008 TrueCrypt Developers Association. All rights reserved.
3
4  Governed by the TrueCrypt License 3.0 the full text of which is contained in
5  the file License.txt included in TrueCrypt binary and source code distribution
6  packages.
7 */
8
9 #include "Buffer.h"
10 #include "Exception.h"
11
12 namespace TrueCrypt
13 {
14         Buffer::Buffer () : DataPtr (nullptr), DataSize (0)
15         {
16         }
17
18         Buffer::Buffer (size_t size) : DataPtr (nullptr), DataSize (0)
19         {
20                 Allocate (size);
21         }
22
23         Buffer::~Buffer ()
24         {
25                 if (DataPtr != nullptr)
26                         Free ();
27         }
28
29         void Buffer::Allocate (size_t size)
30         {
31                 if (size < 1)
32                         throw ParameterIncorrect (SRC_POS);
33
34                 if (DataPtr != nullptr)
35                 {
36                         if (DataSize == size)
37                                 return;
38                         Free();
39                 }
40
41                 try
42                 {
43                         DataPtr = static_cast<byte *> (Memory::Allocate (size));
44                         DataSize = size;
45                 }
46                 catch (...)
47                 {
48                         DataPtr = nullptr;
49                         DataSize = 0;
50                         throw;
51                 }
52         }
53
54         void Buffer::CopyFrom (const ConstBufferPtr &bufferPtr)
55         {
56                 if (!IsAllocated ())
57                         Allocate (bufferPtr.Size());
58                 else if (bufferPtr.Size() > DataSize)
59                         throw ParameterTooLarge (SRC_POS);
60
61                 Memory::Copy (DataPtr, bufferPtr.Get(), bufferPtr.Size());
62         }
63
64         void Buffer::Erase ()
65         {
66                 if (DataSize > 0)
67                         Memory::Erase (DataPtr, DataSize);
68         }
69
70         void Buffer::Free ()
71         {
72                 if (DataPtr == nullptr)
73                         throw NotInitialized (SRC_POS);
74
75                 Memory::Free (DataPtr);
76                 DataPtr = nullptr;
77                 DataSize = 0;
78         }
79
80         BufferPtr Buffer::GetRange (size_t offset, size_t size) const
81         {
82                 if (offset + size > DataSize)
83                         throw ParameterIncorrect (SRC_POS);
84
85                 return BufferPtr (DataPtr + offset, size);
86         }
87         
88         void Buffer::Zero ()
89         {
90                 if (DataSize > 0)
91                         Memory::Zero (DataPtr, DataSize);
92         }
93
94         SecureBuffer::SecureBuffer (size_t size)
95         {
96                 Allocate (size);
97         }
98
99         SecureBuffer::~SecureBuffer ()
100         {
101                 if (DataPtr != nullptr && DataSize != 0)
102                         Free ();
103         }
104
105         void SecureBuffer::Allocate (size_t size)
106         {
107                 Buffer::Allocate (size);
108         }
109
110         void SecureBuffer::Free ()
111         {
112                 if (DataPtr == nullptr)
113                         throw NotInitialized (SRC_POS);
114
115                 Erase ();
116                 Buffer::Free ();
117         }
118
119         void BufferPtr::CopyFrom (const ConstBufferPtr &bufferPtr) const
120         {
121                 if (bufferPtr.Size() > DataSize)
122                         throw ParameterTooLarge (SRC_POS);
123
124                 Memory::Copy (DataPtr, bufferPtr.Get(), bufferPtr.Size());
125         }
126
127         BufferPtr BufferPtr::GetRange (size_t offset, size_t size) const
128         {
129                 if (offset + size > DataSize)
130                         throw ParameterIncorrect (SRC_POS);
131
132                 return BufferPtr (DataPtr + offset, size);
133         }
134
135         ConstBufferPtr ConstBufferPtr::GetRange (size_t offset, size_t size) const
136         {
137                 if (offset + size > DataSize)
138                         throw ParameterIncorrect (SRC_POS);
139
140                 return ConstBufferPtr (DataPtr + offset, size);
141         }
142 }
This page took 0.023967 seconds and 3 git commands to generate. Download a nemesis OSX (sierra+high sierra, tested/working) binary, with fuse-ext3 via e2fsprogs, at this link. application and installer are signed by screwjack, llc. must install fuse with macFUSE layer first.