2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include "ByteArray.h"
25 namespace smartcard_service_api
27 ByteArray ByteArray::EMPTY = ByteArray();
29 ByteArray::ByteArray() : buffer(NULL), length(0)
33 ByteArray::ByteArray(const uint8_t *array, size_t size) :
34 buffer(NULL), length(0)
39 ByteArray::ByteArray(const ByteArray &T) : buffer(NULL), length(0)
41 assign(T.buffer, T.length);
44 ByteArray::~ByteArray()
49 bool ByteArray::assign(const uint8_t *array, size_t size)
51 if (array == NULL || size == 0)
58 buffer = new uint8_t[size];
65 memcpy(buffer, array, size);
71 bool ByteArray::_assign(uint8_t *array, size_t size)
73 if (array == NULL || size == 0)
86 size_t ByteArray::size() const
91 uint8_t *ByteArray::getBuffer()
96 const uint8_t *ByteArray::getBuffer() const
101 uint8_t *ByteArray::getBuffer(size_t offset)
106 if (offset >= length)
108 _ERR("buffer overflow, offset [%d], length [%d]", offset, length);
112 return buffer + offset;
115 const uint8_t *ByteArray::getBuffer(size_t offset) const
120 if (offset >= length)
122 _ERR("buffer overflow, offset [%d], length [%d]", offset, length);
126 return buffer + offset;
129 uint8_t ByteArray::at(size_t index) const
133 _ERR("buffer overflow, index [%d], length [%d]", index, length);
136 return buffer[length - 1];
144 return buffer[index];
147 uint8_t ByteArray::reverseAt(size_t index) const
151 _ERR("buffer underflow, index [%d], length [%d]", index, length);
162 return buffer[length - index - 1];
165 size_t ByteArray::extract(uint8_t *array, size_t size) const
167 uint32_t min_len = 0;
169 if (array == NULL || size == 0)
171 _ERR("invalid param");
175 min_len = (size < length) ? size : length;
177 memcpy(array, buffer, min_len);
182 const ByteArray ByteArray::sub(size_t offset, size_t size) const
184 if (length == 0 || offset >= length || (offset + size) > length)
186 _DBG("length is zero");
191 return ByteArray(buffer + offset, size);
194 void ByteArray::clear()
204 /* operator overloading */
205 ByteArray ByteArray::operator +(const ByteArray &T)
213 _DBG("length is zero");
225 newBuffer = new uint8_t[newLen];
226 if (newBuffer == NULL)
229 _ERR("alloc failed");
234 memcpy(newBuffer, buffer, length);
235 memcpy(newBuffer + length, T.buffer, T.length);
237 newArray._assign(newBuffer, newLen);
242 ByteArray &ByteArray::operator =(const ByteArray &T)
246 assign(T.buffer, T.length);
252 ByteArray &ByteArray::operator +=(const ByteArray &T)
259 bool ByteArray::operator ==(const ByteArray &T) const
261 if (length != T.length)
264 return (memcmp(buffer, T.buffer, length) == 0);
267 bool ByteArray::operator !=(const ByteArray &T) const
269 return !(*this == T);
272 bool ByteArray::operator <(const ByteArray &T) const
274 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) < 0);
277 bool ByteArray::operator >(const ByteArray &T) const
279 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) > 0);
282 uint8_t ByteArray::operator [](size_t index) const
286 _ERR("buffer overflow, index [%d], length [%d]", index, length);
289 return buffer[length -1];
297 return buffer[index];
299 const string ByteArray::toString() const
308 bool ellipsis = false;
319 for (i = 0; i < count; i++)
321 snprintf(temp, sizeof(temp), "%02X ", buffer[i]);
334 ss << "buffer is empty";
340 void ByteArray::save(const char *filePath)
344 if (filePath == NULL || buffer == NULL || length == 0)
347 if ((file = fopen(filePath, "w")) != NULL)
349 fwrite(buffer, 1, length, file);
355 _ERR("file open failed, [%d]", errno);
359 } /* namespace smartcard_service_api */