xref: /netbsd-src/external/bsd/elftosb/dist/common/Blob.cpp (revision 993229b6fea628ff8b1fa09146c80b0cfb2768eb)
1*993229b6Sjkunz /*
2*993229b6Sjkunz  * File:	Blob.cpp
3*993229b6Sjkunz  *
4*993229b6Sjkunz  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5*993229b6Sjkunz  * See included license file for license details.
6*993229b6Sjkunz  */
7*993229b6Sjkunz 
8*993229b6Sjkunz #include "Blob.h"
9*993229b6Sjkunz #include <stdexcept>
10*993229b6Sjkunz #include <stdlib.h>
11*993229b6Sjkunz #include <string.h>
12*993229b6Sjkunz 
Blob()13*993229b6Sjkunz Blob::Blob()
14*993229b6Sjkunz :	m_data(0),
15*993229b6Sjkunz 	m_length(0)
16*993229b6Sjkunz {
17*993229b6Sjkunz }
18*993229b6Sjkunz 
19*993229b6Sjkunz //! Makes a local copy of the \a data argument.
20*993229b6Sjkunz //!
Blob(const uint8_t * data,unsigned length)21*993229b6Sjkunz Blob::Blob(const uint8_t * data, unsigned length)
22*993229b6Sjkunz :	m_data(0),
23*993229b6Sjkunz 	m_length(length)
24*993229b6Sjkunz {
25*993229b6Sjkunz 	m_data = reinterpret_cast<uint8_t*>(malloc(length));
26*993229b6Sjkunz 	memcpy(m_data, data, length);
27*993229b6Sjkunz }
28*993229b6Sjkunz 
29*993229b6Sjkunz //! Makes a local copy of the data owned by \a other.
30*993229b6Sjkunz //!
Blob(const Blob & other)31*993229b6Sjkunz Blob::Blob(const Blob & other)
32*993229b6Sjkunz :	m_data(0),
33*993229b6Sjkunz 	m_length(other.m_length)
34*993229b6Sjkunz {
35*993229b6Sjkunz 	m_data = reinterpret_cast<uint8_t *>(malloc(m_length));
36*993229b6Sjkunz 	memcpy(m_data, other.m_data, m_length);
37*993229b6Sjkunz }
38*993229b6Sjkunz 
39*993229b6Sjkunz //! Disposes of the binary data associated with this object.
~Blob()40*993229b6Sjkunz Blob::~Blob()
41*993229b6Sjkunz {
42*993229b6Sjkunz 	if (m_data)
43*993229b6Sjkunz 	{
44*993229b6Sjkunz 		free(m_data);
45*993229b6Sjkunz 	}
46*993229b6Sjkunz }
47*993229b6Sjkunz 
48*993229b6Sjkunz //! Copies \a data onto the blob's data. The blob does not assume ownership
49*993229b6Sjkunz //! of \a data.
50*993229b6Sjkunz //!
51*993229b6Sjkunz //! \param data Pointer to a buffer containing the data which will be copied
52*993229b6Sjkunz //!		into the blob.
53*993229b6Sjkunz //! \param length Number of bytes pointed to by \a data.
setData(const uint8_t * data,unsigned length)54*993229b6Sjkunz void Blob::setData(const uint8_t * data, unsigned length)
55*993229b6Sjkunz {
56*993229b6Sjkunz 	setLength(length);
57*993229b6Sjkunz 	memcpy(m_data, data, length);
58*993229b6Sjkunz }
59*993229b6Sjkunz 
60*993229b6Sjkunz //! Sets the #m_length member variable to \a length and resizes #m_data to
61*993229b6Sjkunz //! the new length. The contents of #m_data past any previous contents are undefined.
62*993229b6Sjkunz //! If the new \a length is 0 then the data will be freed and a subsequent call
63*993229b6Sjkunz //! to getData() will return NULL.
64*993229b6Sjkunz //!
65*993229b6Sjkunz //! \param length New length of the blob's data in bytes.
setLength(unsigned length)66*993229b6Sjkunz void Blob::setLength(unsigned length)
67*993229b6Sjkunz {
68*993229b6Sjkunz 	if (length == 0)
69*993229b6Sjkunz 	{
70*993229b6Sjkunz 		clear();
71*993229b6Sjkunz 		return;
72*993229b6Sjkunz 	}
73*993229b6Sjkunz 
74*993229b6Sjkunz 	// Allocate new block.
75*993229b6Sjkunz 	if (!m_data)
76*993229b6Sjkunz 	{
77*993229b6Sjkunz 		m_data = reinterpret_cast<uint8_t*>(malloc(length));
78*993229b6Sjkunz 		if (!m_data)
79*993229b6Sjkunz 		{
80*993229b6Sjkunz 			throw std::runtime_error("failed to allocate memory");
81*993229b6Sjkunz 		}
82*993229b6Sjkunz 	}
83*993229b6Sjkunz 	// Reallocate previous block.
84*993229b6Sjkunz 	else
85*993229b6Sjkunz 	{
86*993229b6Sjkunz 		void * newBlob = realloc(m_data, length);
87*993229b6Sjkunz 		if (!newBlob)
88*993229b6Sjkunz 		{
89*993229b6Sjkunz 			throw std::runtime_error("failed to reallocate memory");
90*993229b6Sjkunz 		}
91*993229b6Sjkunz 		m_data = reinterpret_cast<uint8_t*>(newBlob);
92*993229b6Sjkunz 	}
93*993229b6Sjkunz 
94*993229b6Sjkunz 	// Set length.
95*993229b6Sjkunz 	m_length = length;
96*993229b6Sjkunz }
97*993229b6Sjkunz 
append(const uint8_t * newData,unsigned newDataLength)98*993229b6Sjkunz void Blob::append(const uint8_t * newData, unsigned newDataLength)
99*993229b6Sjkunz {
100*993229b6Sjkunz 	unsigned oldLength = m_length;
101*993229b6Sjkunz 
102*993229b6Sjkunz 	setLength(m_length + newDataLength);
103*993229b6Sjkunz 
104*993229b6Sjkunz 	memcpy(m_data + oldLength, newData, newDataLength);
105*993229b6Sjkunz }
106*993229b6Sjkunz 
clear()107*993229b6Sjkunz void Blob::clear()
108*993229b6Sjkunz {
109*993229b6Sjkunz 	if (m_data)
110*993229b6Sjkunz 	{
111*993229b6Sjkunz 		free(m_data);
112*993229b6Sjkunz 		m_data = NULL;
113*993229b6Sjkunz 	}
114*993229b6Sjkunz 
115*993229b6Sjkunz 	m_length = 0;
116*993229b6Sjkunz }
117*993229b6Sjkunz 
relinquish()118*993229b6Sjkunz void Blob::relinquish()
119*993229b6Sjkunz {
120*993229b6Sjkunz 	m_data = NULL;
121*993229b6Sjkunz 	m_length = 0;
122*993229b6Sjkunz }
123*993229b6Sjkunz 
124