update the latest source
[framework/system/smartcard-service.git] / common / ByteArray.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 /* standard library header */
18 #include <stdio.h>
19 #include <string.h>
20 #include <errno.h>
21
22 /* SLP library header */
23
24 /* local header */
25 #include "Debug.h"
26 #include "ByteArray.h"
27
28 namespace smartcard_service_api
29 {
30         ByteArray ByteArray::EMPTY = ByteArray();
31
32         ByteArray::ByteArray()
33         {
34                 buffer = NULL;
35                 length = 0;
36         }
37
38         ByteArray::ByteArray(uint8_t *array, uint32_t bufferLen)
39         {
40                 buffer = NULL;
41                 length = 0;
42
43                 setBuffer(array, bufferLen);
44         }
45
46         ByteArray::ByteArray(const ByteArray &T)
47         {
48                 buffer = NULL;
49                 length = 0;
50
51                 setBuffer(T.buffer, T.length);
52         }
53
54         ByteArray::~ByteArray()
55         {
56                 releaseBuffer();
57         }
58
59         bool ByteArray::setBuffer(uint8_t *array, uint32_t bufferLen)
60         {
61                 if (array == NULL || bufferLen == 0)
62                 {
63                         return false;
64                 }
65
66                 releaseBuffer();
67
68                 buffer = new uint8_t[bufferLen];
69                 if (buffer == NULL)
70                 {
71                         SCARD_DEBUG_ERR("alloc failed");
72                         return false;
73                 }
74
75                 memcpy(buffer, array, bufferLen);
76                 length = bufferLen;
77
78                 return true;
79         }
80
81         bool ByteArray::_setBuffer(uint8_t *array, uint32_t bufferLen)
82         {
83                 if (array == NULL || bufferLen == 0)
84                 {
85                         return false;
86                 }
87
88                 releaseBuffer();
89
90                 buffer = array;
91                 length = bufferLen;
92
93                 return true;
94         }
95
96         uint32_t ByteArray::getLength() const
97         {
98                 return length;
99         }
100
101         uint8_t *ByteArray::getBuffer() const
102         {
103                 return getBuffer(0);
104         }
105
106         uint8_t *ByteArray::getBuffer(uint32_t offset) const
107         {
108                 if (length == 0)
109                         return NULL;
110
111                 if (offset >= length)
112                 {
113                         SCARD_DEBUG_ERR("buffer overflow, offset [%d], length [%d]", offset, length);
114                         return NULL;
115                 }
116
117                 return buffer + offset;
118         }
119
120         uint8_t ByteArray::getAt(uint32_t index) const
121         {
122                 if (index >= length)
123                 {
124                         SCARD_DEBUG_ERR("buffer overflow, index [%d], length [%d]", index, length);
125                         return buffer[length -1];
126                 }
127
128                 return buffer[index];
129         }
130
131         uint8_t ByteArray::getReverseAt(uint32_t index) const
132         {
133                 if (index >= length)
134                 {
135                         SCARD_DEBUG_ERR("buffer underflow, index [%d], length [%d]", index, length);
136                         return buffer[0];
137                 }
138
139                 return buffer[length - index - 1];
140         }
141
142         uint32_t ByteArray::copyFromArray(uint8_t *array, uint32_t bufferLen) const
143         {
144                 uint32_t min_len = 0;
145
146                 if (array == NULL || bufferLen == 0)
147                 {
148                         SCARD_DEBUG_ERR("invaild param");
149                         return false;
150                 }
151
152                 min_len = (bufferLen < length) ? bufferLen : length;
153
154                 memcpy(array, buffer, min_len);
155
156                 return min_len;
157         }
158
159         void ByteArray::releaseBuffer()
160         {
161                 if (buffer != NULL)
162                 {
163                         delete []buffer;
164                         buffer = NULL;
165                 }
166                 length = 0;
167         }
168
169         /* operator overloading */
170         ByteArray ByteArray::operator +(const ByteArray &T)
171         {
172                 uint32_t newLen;
173                 uint8_t *newBuffer;
174                 ByteArray newArray;
175
176                 if (length == 0)
177                 {
178                         SCARD_DEBUG("length is zero");
179
180                         return T;
181                 }
182
183                 newLen = T.length;
184
185                 if (newLen == 0)
186                         return *this;
187
188                 newLen += length;
189
190                 newBuffer = new uint8_t[newLen];
191                 if (newBuffer == NULL)
192                 {
193                         /* assert.... */
194                         SCARD_DEBUG_ERR("alloc failed");
195
196                         return *this;
197                 }
198
199                 memcpy(newBuffer, buffer, length);
200                 memcpy(newBuffer + length, T.buffer, T.length);
201
202                 newArray._setBuffer(newBuffer, newLen);
203
204                 return newArray;
205         }
206
207         ByteArray &ByteArray::operator =(const ByteArray &T)
208         {
209                 if (this != &T)
210                 {
211                         setBuffer(T.buffer, T.length);
212                 }
213
214                 return *this;
215         }
216
217         ByteArray &ByteArray::operator +=(const ByteArray &T)
218         {
219                 *this = *this + T;
220
221                 return *this;
222         }
223
224         bool ByteArray::operator ==(const ByteArray &T) const
225         {
226                 if (length != T.length)
227                         return false;
228
229                 return (memcmp(buffer, T.buffer, length) == 0);
230         }
231
232         bool ByteArray::operator !=(const ByteArray &T) const
233         {
234                 return !(*this == T);
235         }
236
237         bool ByteArray::operator <(const ByteArray &T) const
238         {
239                 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) < 0);
240         }
241
242         bool ByteArray::operator >(const ByteArray &T) const
243         {
244                 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) > 0);
245         }
246
247         uint8_t &ByteArray::operator [](uint32_t index) const
248         {
249                 if (index >= length)
250                 {
251                         SCARD_DEBUG_ERR("buffer overflow, index [%d], length [%d]", index, length);
252                         return buffer[length -1];
253                 }
254
255                 return buffer[index];
256         }
257
258         const char *ByteArray::toString()
259         {
260                 memset(strBuffer, 0, sizeof(strBuffer));
261
262                 if (length == 0)
263                 {
264                         snprintf(strBuffer, sizeof(strBuffer), "buffer is empty");
265                 }
266                 else
267                 {
268                         int count;
269                         int i, offset = 0;
270                         bool ellipsis = false;
271
272                         count = length;
273                         if (count > 20)
274                         {
275                                 count = 20;
276                                 ellipsis = true;
277                         }
278
279                         snprintf(strBuffer + offset, sizeof(strBuffer) - offset, "{ ");
280                         offset += 2;
281
282                         for (i = 0; i < count; i++)
283                         {
284                                 snprintf(strBuffer + offset, sizeof(strBuffer) - offset, "%02X ", buffer[i]);
285                                 offset += 3;
286                         }
287
288                         if (ellipsis)
289                         {
290                                 snprintf(strBuffer + offset, sizeof(strBuffer) - offset, "... }");
291                         }
292                         else
293                         {
294                                 snprintf(strBuffer + offset, sizeof(strBuffer) - offset, "}");
295                         }
296                 }
297
298                 return (const char *)strBuffer;
299         }
300
301         void ByteArray::save(const char *filePath)
302         {
303                 FILE *file = NULL;
304
305                 if (filePath == NULL || buffer == NULL || length == 0)
306                         return;
307
308                 if ((file = fopen(filePath, "w")) != NULL)
309                 {
310                         fwrite(buffer, 1, length, file);
311                         fflush(file);
312
313                         fclose(file);
314                         SCARD_DEBUG("file has written, file [%s], length[%d]", filePath, length);
315                 }
316                 else
317                 {
318                         SCARD_DEBUG_ERR("file open failed, [%d]", errno);
319                 }
320         }
321
322 } /* namespace smartcard_service_api */
323