2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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 */
22 /* SLP library header */
26 #include "ByteArray.h"
28 namespace smartcard_service_api
30 ByteArray ByteArray::EMPTY = ByteArray();
32 ByteArray::ByteArray()
38 ByteArray::ByteArray(uint8_t *array, uint32_t bufferLen)
43 setBuffer(array, bufferLen);
46 ByteArray::ByteArray(const ByteArray &T)
51 setBuffer(T.buffer, T.length);
54 ByteArray::~ByteArray()
59 bool ByteArray::setBuffer(uint8_t *array, uint32_t bufferLen)
61 if (array == NULL || bufferLen == 0)
68 buffer = new uint8_t[bufferLen];
71 SCARD_DEBUG_ERR("alloc failed");
75 memcpy(buffer, array, bufferLen);
81 bool ByteArray::_setBuffer(uint8_t *array, uint32_t bufferLen)
83 if (array == NULL || bufferLen == 0)
96 uint32_t ByteArray::getLength() const
101 uint8_t *ByteArray::getBuffer() const
106 uint8_t *ByteArray::getBuffer(uint32_t offset) const
111 if (offset >= length)
113 SCARD_DEBUG_ERR("buffer overflow, offset [%d], length [%d]", offset, length);
117 return buffer + offset;
120 uint8_t ByteArray::getAt(uint32_t index) const
124 SCARD_DEBUG_ERR("buffer overflow, index [%d], length [%d]", index, length);
125 return buffer[length -1];
128 return buffer[index];
131 uint8_t ByteArray::getReverseAt(uint32_t index) const
135 SCARD_DEBUG_ERR("buffer underflow, index [%d], length [%d]", index, length);
139 return buffer[length - index - 1];
142 uint32_t ByteArray::copyFromArray(uint8_t *array, uint32_t bufferLen) const
144 uint32_t min_len = 0;
146 if (array == NULL || bufferLen == 0)
148 SCARD_DEBUG_ERR("invaild param");
152 min_len = (bufferLen < length) ? bufferLen : length;
154 memcpy(array, buffer, min_len);
159 void ByteArray::releaseBuffer()
169 /* operator overloading */
170 ByteArray ByteArray::operator +(const ByteArray &T)
178 SCARD_DEBUG("length is zero");
190 newBuffer = new uint8_t[newLen];
191 if (newBuffer == NULL)
194 SCARD_DEBUG_ERR("alloc failed");
199 memcpy(newBuffer, buffer, length);
200 memcpy(newBuffer + length, T.buffer, T.length);
202 newArray._setBuffer(newBuffer, newLen);
207 ByteArray &ByteArray::operator =(const ByteArray &T)
211 setBuffer(T.buffer, T.length);
217 ByteArray &ByteArray::operator +=(const ByteArray &T)
224 bool ByteArray::operator ==(const ByteArray &T) const
226 if (length != T.length)
229 return (memcmp(buffer, T.buffer, length) == 0);
232 bool ByteArray::operator !=(const ByteArray &T) const
234 return !(*this == T);
237 bool ByteArray::operator <(const ByteArray &T) const
239 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) < 0);
242 bool ByteArray::operator >(const ByteArray &T) const
244 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) > 0);
247 uint8_t &ByteArray::operator [](uint32_t index) const
251 SCARD_DEBUG_ERR("buffer overflow, index [%d], length [%d]", index, length);
252 return buffer[length -1];
255 return buffer[index];
258 const char *ByteArray::toString()
260 memset(strBuffer, 0, sizeof(strBuffer));
264 snprintf(strBuffer, sizeof(strBuffer), "buffer is empty");
270 bool ellipsis = false;
279 snprintf(strBuffer + offset, sizeof(strBuffer) - offset, "{ ");
282 for (i = 0; i < count; i++)
284 snprintf(strBuffer + offset, sizeof(strBuffer) - offset, "%02X ", buffer[i]);
290 snprintf(strBuffer + offset, sizeof(strBuffer) - offset, "... }");
294 snprintf(strBuffer + offset, sizeof(strBuffer) - offset, "}");
298 return (const char *)strBuffer;
301 void ByteArray::save(const char *filePath)
305 if (filePath == NULL || buffer == NULL || length == 0)
308 if ((file = fopen(filePath, "w")) != NULL)
310 fwrite(buffer, 1, length, file);
314 SCARD_DEBUG("file has written, file [%s], length[%d]", filePath, length);
318 SCARD_DEBUG_ERR("file open failed, [%d]", errno);
322 } /* namespace smartcard_service_api */