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);
140 return buffer[length - 1];
148 return buffer[index];
151 uint8_t ByteArray::reverseAt(size_t index) const
155 _ERR("buffer underflow, index [%d], length [%d]", index, length);
166 return buffer[length - index - 1];
169 size_t ByteArray::extract(uint8_t *array, size_t size) const
171 uint32_t min_len = 0;
173 if (array == NULL || size == 0)
175 _ERR("invalid param");
179 min_len = (size < length) ? size : length;
181 memcpy(array, buffer, min_len);
186 const ByteArray ByteArray::sub(size_t offset, size_t size) const
188 if (length == 0 || offset >= length || (offset + size) > length)
190 _DBG("length is zero");
195 return ByteArray(buffer + offset, size);
198 void ByteArray::clear()
208 /* operator overloading */
209 ByteArray ByteArray::operator +(const ByteArray &T)
217 _DBG("length is zero");
229 newBuffer = new uint8_t[newLen];
230 if (newBuffer == NULL)
233 _ERR("alloc failed");
238 memcpy(newBuffer, buffer, length);
239 memcpy(newBuffer + length, T.buffer, T.length);
241 newArray._assign(newBuffer, newLen);
246 ByteArray &ByteArray::operator =(const ByteArray &T)
250 assign(T.buffer, T.length);
256 ByteArray &ByteArray::operator +=(const ByteArray &T)
263 bool ByteArray::operator ==(const ByteArray &T) const
265 if (length != T.length)
268 return (memcmp(buffer, T.buffer, length) == 0);
271 bool ByteArray::operator !=(const ByteArray &T) const
273 return !(*this == T);
276 bool ByteArray::operator <(const ByteArray &T) const
278 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) < 0);
281 bool ByteArray::operator >(const ByteArray &T) const
283 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) > 0);
286 uint8_t ByteArray::operator [](size_t index) const
290 _ERR("buffer overflow, index [%d], length [%d]", index, length);
293 return buffer[length -1];
301 return buffer[index];
303 const string ByteArray::toString() const
312 bool ellipsis = false;
323 for (i = 0; i < count; i++)
325 snprintf(temp, sizeof(temp), "%02X ", buffer[i]);
338 ss << "buffer is empty";
344 void ByteArray::save(const char *filePath)
348 if (filePath == NULL || buffer == NULL || length == 0)
351 if ((file = fopen(filePath, "w")) != NULL)
353 fwrite(buffer, 1, length, file);
359 _ERR("file open failed, [%d]", errno);
363 } /* namespace smartcard_service_api */