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];
71 memcpy(buffer, array, size);
78 bool ByteArray::_assign(uint8_t *array, size_t size)
80 if (array == NULL || size == 0)
93 size_t ByteArray::size() const
98 uint8_t *ByteArray::getBuffer()
103 const uint8_t *ByteArray::getBuffer() const
108 uint8_t *ByteArray::getBuffer(size_t offset)
113 if (offset >= length)
115 _ERR("buffer overflow, offset [%zu], length [%zu]", offset, length);
119 return buffer + offset;
122 const uint8_t *ByteArray::getBuffer(size_t offset) const
127 if (offset >= length)
129 _ERR("buffer overflow, offset [%zu], length [%zu]", offset, length);
133 return buffer + offset;
136 uint8_t ByteArray::at(size_t index) const
140 _ERR("buffer overflow, index [%zu], length [%zu]", index, length);
142 return buffer[length - 1];
148 return buffer[index];
151 uint8_t ByteArray::reverseAt(size_t index) const
155 _ERR("buffer underflow, index [%zu], length [%zu]", index, length);
163 return buffer[length - index - 1];
166 size_t ByteArray::extract(uint8_t *array, size_t size) const
168 uint32_t min_len = 0;
170 if (array == NULL || size == 0)
172 _ERR("invalid param");
176 min_len = (size < length) ? size : length;
178 memcpy(array, buffer, min_len);
183 const ByteArray ByteArray::sub(size_t offset, size_t size) const
185 if (length == 0 || offset >= length || (offset + size) > length)
187 _DBG("length is zero");
192 return ByteArray(buffer + offset, size);
195 void ByteArray::clear()
205 /* operator overloading */
206 ByteArray ByteArray::operator +(const ByteArray &T)
214 _DBG("length is zero");
226 newBuffer = new uint8_t[newLen];
227 if (newBuffer == NULL)
230 _ERR("alloc failed");
235 memcpy(newBuffer, buffer, length);
236 memcpy(newBuffer + length, T.buffer, T.length);
238 newArray._assign(newBuffer, newLen);
243 ByteArray &ByteArray::operator =(const ByteArray &T)
247 assign(T.buffer, T.length);
253 ByteArray &ByteArray::operator +=(const ByteArray &T)
260 bool ByteArray::operator ==(const ByteArray &T) const
262 if (length != T.length)
265 return (memcmp(buffer, T.buffer, length) == 0);
268 bool ByteArray::operator !=(const ByteArray &T) const
270 return !(*this == T);
273 bool ByteArray::operator <(const ByteArray &T) const
275 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) < 0);
278 bool ByteArray::operator >(const ByteArray &T) const
280 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) > 0);
283 uint8_t ByteArray::operator[](size_t index) const
287 _ERR("buffer overflow, index [%zu], length [%zu]", index, length);
289 return buffer[length -1];
295 return buffer[index];
298 const string ByteArray::toString() const
301 return toString(true);
303 return toString(false);
307 const string ByteArray::toString(bool entire) const
315 bool ellipsis = false;
318 if (entire == false && count > 20)
326 for (i = 0; i < count; i++)
328 snprintf(temp, sizeof(temp), "%02X ", buffer[i]);
339 ss << "buffer is empty";
345 void ByteArray::save(const char *filePath)
349 if (filePath == NULL || buffer == NULL || length == 0)
352 if ((file = fopen(filePath, "w")) != NULL) {
353 fwrite(buffer, 1, length, file);
357 SECURE_LOGD("file has written, file [%s], length[%zu]", filePath, length);
359 _ERR("file open failed, [%d]", errno);
364 } /* namespace smartcard_service_api */