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.
17 /* standard library header */
23 /* SLP library header */
27 #include "ByteArray.h"
29 namespace smartcard_service_api
31 ByteArray ByteArray::EMPTY = ByteArray();
33 ByteArray::ByteArray() : buffer(NULL), length(0)
37 ByteArray::ByteArray(const uint8_t *array, size_t size) :
38 buffer(NULL), length(0)
43 ByteArray::ByteArray(const ByteArray &T) : buffer(NULL), length(0)
45 assign(T.buffer, T.length);
48 ByteArray::~ByteArray()
53 bool ByteArray::assign(const uint8_t *array, size_t size)
55 if (array == NULL || size == 0)
62 buffer = new uint8_t[size];
69 memcpy(buffer, array, size);
75 bool ByteArray::_assign(uint8_t *array, size_t size)
77 if (array == NULL || size == 0)
90 size_t ByteArray::size() const
95 uint8_t *ByteArray::getBuffer()
100 const uint8_t *ByteArray::getBuffer() const
105 uint8_t *ByteArray::getBuffer(size_t offset)
110 if (offset >= length)
112 _ERR("buffer overflow, offset [%d], length [%d]", offset, length);
116 return buffer + offset;
119 const uint8_t *ByteArray::getBuffer(size_t offset) const
124 if (offset >= length)
126 _ERR("buffer overflow, offset [%d], length [%d]", offset, length);
130 return buffer + offset;
133 uint8_t ByteArray::at(size_t index) const
137 _ERR("buffer overflow, index [%d], length [%d]", index, length);
139 return buffer[length - 1];
145 return buffer[index];
148 uint8_t ByteArray::reverseAt(size_t index) const
152 _ERR("buffer underflow, index [%d], length [%d]", index, length);
160 return buffer[length - index - 1];
163 size_t ByteArray::extract(uint8_t *array, size_t size) const
165 uint32_t min_len = 0;
167 if (array == NULL || size == 0)
169 _ERR("invalid param");
173 min_len = (size < length) ? size : length;
175 memcpy(array, buffer, min_len);
180 const ByteArray ByteArray::sub(size_t offset, size_t size) const
182 if (length == 0 || offset >= length || (offset + size) > length)
184 _DBG("length is zero");
189 return ByteArray(buffer + offset, size);
192 void ByteArray::clear()
202 /* operator overloading */
203 ByteArray ByteArray::operator +(const ByteArray &T)
211 _DBG("length is zero");
223 newBuffer = new uint8_t[newLen];
224 if (newBuffer == NULL)
227 _ERR("alloc failed");
232 memcpy(newBuffer, buffer, length);
233 memcpy(newBuffer + length, T.buffer, T.length);
235 newArray._assign(newBuffer, newLen);
240 ByteArray &ByteArray::operator =(const ByteArray &T)
244 assign(T.buffer, T.length);
250 ByteArray &ByteArray::operator +=(const ByteArray &T)
257 bool ByteArray::operator ==(const ByteArray &T) const
259 if (length != T.length)
262 return (memcmp(buffer, T.buffer, length) == 0);
265 bool ByteArray::operator !=(const ByteArray &T) const
267 return !(*this == T);
270 bool ByteArray::operator <(const ByteArray &T) const
272 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) < 0);
275 bool ByteArray::operator >(const ByteArray &T) const
277 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) > 0);
280 uint8_t ByteArray::operator[](size_t index) const
284 _ERR("buffer overflow, index [%d], length [%d]", index, length);
286 return buffer[length -1];
292 return buffer[index];
295 const string ByteArray::toString() const
298 return toString(true);
300 return toString(false);
304 const string ByteArray::toString(bool entire) const
312 bool ellipsis = false;
315 if (entire == false && count > 20)
323 for (i = 0; i < count; i++)
325 snprintf(temp, sizeof(temp), "%02X ", buffer[i]);
336 ss << "buffer is empty";
342 void ByteArray::save(const char *filePath)
346 if (filePath == NULL || buffer == NULL || length == 0)
349 if ((file = fopen(filePath, "w")) != NULL) {
350 fwrite(buffer, 1, length, file);
354 SECURE_LOGD("file has written, file [%s], length[%d]", filePath, length);
356 _ERR("file open failed, [%d]", errno);
360 } /* namespace smartcard_service_api */