Open-Transactions  0.93.0-ge03d287
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
opentxs::OTData Class Reference

#include <OTData.hpp>

Inheritance diagram for opentxs::OTData:

Public Member Functions

EXPORT OTData ()
 
EXPORT OTData (const void *data, uint32_t size)
 
EXPORT OTData (const OTData &source)
 
EXPORT OTData (const OTASCIIArmor &source)
 
virtual EXPORT ~OTData ()
 
virtual EXPORT void Release ()
 
EXPORT void Release_Data ()
 
void SetSize (uint32_t size)
 
const void * GetPointer () const
 
EXPORT OTDataoperator= (OTData rhs)
 
EXPORT void swap (OTData &rhs)
 
EXPORT bool operator== (const OTData &rhs) const
 
EXPORT bool operator!= (const OTData &rhs) const
 
EXPORT OTDataoperator+= (const OTData &rhs)
 
EXPORT bool IsEmpty () const
 
uint32_t GetSize () const
 
EXPORT void Assign (const OTData &source)
 
EXPORT void Assign (const void *data, uint32_t size)
 
EXPORT void Concatenate (const void *data, uint32_t size)
 
EXPORT bool Randomize (uint32_t size)
 
EXPORT void zeroMemory () const
 
EXPORT uint32_t OTfread (uint8_t *data, uint32_t size)
 
void reset ()
 

Protected Member Functions

void Initialize ()
 

Friends

class OTASCIIArmor
 
class OTIdentifier
 
class OTEnvelope
 
class OTCrypto
 

Detailed Description

Definition at line 143 of file OTData.hpp.

Constructor & Destructor Documentation

opentxs::OTData::OTData ( )

Definition at line 145 of file OTData.cpp.

146  : data_(nullptr)
147  , position_(0)
148  , size_(0)
149 {
150 }
opentxs::OTData::OTData ( const void *  data,
uint32_t  size 
)

Definition at line 170 of file OTData.cpp.

171  : data_(nullptr)
172  , position_(0)
173  , size_(0)
174 {
175  Assign(data, size);
176 }
EXPORT void Assign(const OTData &source)
Definition: OTData.cpp:275
opentxs::OTData::OTData ( const OTData source)

Definition at line 152 of file OTData.cpp.

153  : data_(nullptr)
154  , position_(0)
155  , size_(0)
156 {
157  Assign(source);
158 }
EXPORT void Assign(const OTData &source)
Definition: OTData.cpp:275
opentxs::OTData::OTData ( const OTASCIIArmor source)

Definition at line 160 of file OTData.cpp.

161  : data_(nullptr)
162  , position_(0)
163  , size_(0)
164 {
165  if (source.Exists()) {
166  source.GetData(*this);
167  }
168 }
opentxs::OTData::~OTData ( )
virtual

Definition at line 178 of file OTData.cpp.

179 {
180  Release_Data();
181 }
EXPORT void Release_Data()
Definition: OTData.cpp:245

Member Function Documentation

void opentxs::OTData::Assign ( const OTData source)

Definition at line 275 of file OTData.cpp.

276 {
277  // can't assign to self.
278  if (&source == this) {
279  return;
280  }
281 
282  if (!source.IsEmpty()) {
283  Assign(source.data_, source.size_);
284  }
285  else {
286  // Otherwise if it's empty, then empty this also.
287  Release();
288  }
289 }
EXPORT void Assign(const OTData &source)
Definition: OTData.cpp:275
virtual EXPORT void Release()
Definition: OTData.cpp:257
void opentxs::OTData::Assign ( const void *  data,
uint32_t  size 
)

Definition at line 296 of file OTData.cpp.

297 {
298  // This releases all memory and zeros out all members.
299  Release();
300 
301  if (data != nullptr && size > 0) {
302  data_ = static_cast<void*>(new uint8_t[size]);
303  OT_ASSERT(data_ != nullptr);
304  OTPassword::safe_memcpy(data_, size, data, size);
305  size_ = size;
306  }
307  // TODO: else error condition. Could just ASSERT() this.
308 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
virtual EXPORT void Release()
Definition: OTData.cpp:257
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
void opentxs::OTData::Concatenate ( const void *  data,
uint32_t  size 
)

Definition at line 333 of file OTData.cpp.

334 {
335  OT_ASSERT(data != nullptr);
336  OT_ASSERT(size > 0);
337 
338  if (size == 0) {
339  return;
340  }
341 
342  if (size_ == 0) {
343  Assign(data, size);
344  return;
345  }
346 
347  void* newData = nullptr;
348  uint32_t newSize = GetSize() + size;
349 
350  if (newSize > 0) {
351  newData = static_cast<void*>(new uint8_t[newSize]);
352  OT_ASSERT(newData != nullptr);
353  OTPassword::zeroMemory(newData, newSize);
354  }
355  // If there's a new memory buffer (for the combined..)
356  if (newData != nullptr) {
357  // if THIS object has data inside of it...
358  if (!IsEmpty()) {
359  // Copy THIS object into the new
360  // buffer, starting at the
361  // beginning.
362  OTPassword::safe_memcpy(newData, newSize, data_, GetSize());
363  }
364 
365  // Next we copy the data being appended...
366  OTPassword::safe_memcpy(static_cast<uint8_t*>(newData) + GetSize(),
367  newSize - GetSize(), data, size);
368  }
369 
370  if (data_ != nullptr) {
371  delete[] static_cast<uint8_t*>(data_);
372  }
373 
374  data_ = newData;
375  size_ = newSize;
376 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
EXPORT void Assign(const OTData &source)
Definition: OTData.cpp:275
#define OT_ASSERT(x)
Definition: Assert.hpp:150
EXPORT bool IsEmpty() const
Definition: OTData.cpp:291
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
uint32_t GetSize() const
Definition: OTData.hpp:174
const void* opentxs::OTData::GetPointer ( ) const
inline

Definition at line 162 of file OTData.hpp.

163  {
164  return data_;
165  }
uint32_t opentxs::OTData::GetSize ( ) const
inline

Definition at line 174 of file OTData.hpp.

175  {
176  return size_;
177  }
void opentxs::OTData::Initialize ( )
inlineprotected

Definition at line 192 of file OTData.hpp.

193  {
194  data_ = nullptr;
195  size_ = 0;
196  position_ = 0;
197  }
bool opentxs::OTData::IsEmpty ( ) const

Definition at line 291 of file OTData.cpp.

292 {
293  return size_ < 1;
294 }
bool opentxs::OTData::operator!= ( const OTData rhs) const

Definition at line 202 of file OTData.cpp.

203 {
204  return !operator==(rhs);
205 }
EXPORT bool operator==(const OTData &rhs) const
Definition: OTData.cpp:183
OTData & opentxs::OTData::operator+= ( const OTData rhs)

Definition at line 378 of file OTData.cpp.

379 {
380  if (rhs.GetSize() > 0) {
381  Concatenate(rhs.data_, rhs.GetSize());
382  }
383  return *this;
384 }
EXPORT void Concatenate(const void *data, uint32_t size)
Definition: OTData.cpp:333
OTData & opentxs::OTData::operator= ( OTData  rhs)

Definition at line 262 of file OTData.cpp.

263 {
264  swap(rhs);
265  return *this;
266 }
EXPORT void swap(OTData &rhs)
Definition: OTData.cpp:268
bool opentxs::OTData::operator== ( const OTData rhs) const

Definition at line 183 of file OTData.cpp.

184 {
185  if (size_ != rhs.size_) {
186  return false;
187  }
188 
189  if (size_ == 0 && rhs.size_ == 0) {
190  return true;
191  }
192  // TODO security: replace memcmp with a more secure
193  // version. Still, though, I am managing it internal to
194  // the class.
195  if (std::memcmp(data_, rhs.data_, size_) == 0) {
196  return true;
197  }
198 
199  return false;
200 }
uint32_t opentxs::OTData::OTfread ( uint8_t *  data,
uint32_t  size 
)

Definition at line 214 of file OTData.cpp.

215 {
216  OT_ASSERT(data != nullptr && size > 0);
217 
218  uint32_t sizeToRead = 0;
219 
220  if (data_ != nullptr && position_ < GetSize()) {
221  // If the size is 20, and position is 5 (I've already read the first 5
222  // bytes) then the size remaining to read is 15. That is, GetSize()
223  // minus position_.
224  sizeToRead = GetSize() - position_;
225 
226  if (size < sizeToRead) {
227  sizeToRead = size;
228  }
229  OTPassword::safe_memcpy(data, size,
230  static_cast<uint8_t*>(data_) + position_,
231  static_cast<uint32_t>(sizeToRead));
232  position_ += sizeToRead;
233  }
234 
235  return sizeToRead;
236 }
#define OT_ASSERT(x)
Definition: Assert.hpp:150
static EXPORT void * safe_memcpy(void *dest, uint32_t dsize, const void *src, uint32_t ssize, bool zeroSource=false)
Definition: OTPassword.cpp:357
uint32_t GetSize() const
Definition: OTData.hpp:174
bool opentxs::OTData::Randomize ( uint32_t  size)

Definition at line 310 of file OTData.cpp.

311 {
312  Release(); // This releases all memory and zeros out all members.
313  if (size > 0) {
314  data_ = static_cast<void*>(new uint8_t[size]);
315  OT_ASSERT(data_ != nullptr);
316 
317  if (!OTPassword::randomizeMemory_uint8(static_cast<uint8_t*>(data_),
318  size)) {
319  // randomizeMemory already logs, so I'm not logging again twice
320  // here.
321  delete[] static_cast<uint8_t*>(data_);
322  data_ = nullptr;
323  return false;
324  }
325 
326  size_ = size;
327  return true;
328  }
329  // else error condition. Could just ASSERT() this.
330  return false;
331 }
static EXPORT bool randomizeMemory_uint8(uint8_t *destination, uint32_t size)
Definition: OTPassword.cpp:881
#define OT_ASSERT(x)
Definition: Assert.hpp:150
virtual EXPORT void Release()
Definition: OTData.cpp:257
void opentxs::OTData::Release ( )
virtual

Definition at line 257 of file OTData.cpp.

258 {
259  Release_Data();
260 }
EXPORT void Release_Data()
Definition: OTData.cpp:245
void opentxs::OTData::Release_Data ( )

Definition at line 245 of file OTData.cpp.

246 {
247  if (data_ != nullptr) {
248  // For security reasons, we clear the memory to 0 when deleting the
249  // object. (Seems smart.)
250  OTPassword::zeroMemory(data_, size_);
251  delete[] static_cast<uint8_t*>(data_);
252  // If data_ was already nullptr, no need to re-Initialize().
253  Initialize();
254  }
255 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
void Initialize()
Definition: OTData.hpp:192
void opentxs::OTData::reset ( )
inline

Definition at line 186 of file OTData.hpp.

187  {
188  position_ = 0;
189  }
void opentxs::OTData::SetSize ( uint32_t  size)

Definition at line 386 of file OTData.cpp.

387 {
388  Release();
389 
390  if (size > 0) {
391  data_ = static_cast<void*>(new uint8_t[size]);
392  OT_ASSERT(data_ != nullptr);
393  OTPassword::zeroMemory(data_, size);
394  size_ = size;
395  }
396 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281
#define OT_ASSERT(x)
Definition: Assert.hpp:150
virtual EXPORT void Release()
Definition: OTData.cpp:257
void opentxs::OTData::swap ( OTData rhs)

Definition at line 268 of file OTData.cpp.

269 {
270  std::swap(data_, rhs.data_);
271  std::swap(position_, rhs.position_);
272  std::swap(size_, rhs.size_);
273 }
void opentxs::OTData::zeroMemory ( ) const

Definition at line 238 of file OTData.cpp.

239 {
240  if (data_ != nullptr) {
241  OTPassword::zeroMemory(data_, size_);
242  }
243 }
EXPORT void zeroMemory()
Definition: OTPassword.cpp:281

Friends And Related Function Documentation

friend class OTASCIIArmor
friend

Definition at line 145 of file OTData.hpp.

friend class OTCrypto
friend

Definition at line 148 of file OTData.hpp.

friend class OTEnvelope
friend

Definition at line 147 of file OTData.hpp.

friend class OTIdentifier
friend

Definition at line 146 of file OTData.hpp.


The documentation for this class was generated from the following files: