Apply coding rule
[platform/core/connectivity/smartcard-service.git] / common / ByteArray.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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 <cstdio>
19 #include <cstring>
20 #include <cerrno>
21 #include <sstream>
22
23 /* SLP library header */
24
25 /* local header */
26 #include "Debug.h"
27 #include "ByteArray.h"
28
29 namespace smartcard_service_api
30 {
31         ByteArray ByteArray::EMPTY = ByteArray();
32
33         ByteArray::ByteArray() : buffer(NULL), length(0)
34         {
35         }
36
37         ByteArray::ByteArray(const uint8_t *array, size_t size) :
38                 buffer(NULL), length(0)
39         {
40                 assign(array, size);
41         }
42
43         ByteArray::ByteArray(const ByteArray &T) : buffer(NULL), length(0)
44         {
45                 assign(T.buffer, T.length);
46         }
47
48         ByteArray::~ByteArray()
49         {
50                 clear();
51         }
52
53         bool ByteArray::assign(const uint8_t *array, size_t size)
54         {
55                 if (array == NULL || size == 0)
56                 {
57                         return false;
58                 }
59
60                 clear();
61
62                 buffer = new uint8_t[size];
63                 if (buffer == NULL)
64                 {
65                         _ERR("alloc failed");
66                         return false;
67                 }
68
69                 memcpy(buffer, array, size);
70                 length = size;
71
72                 return true;
73         }
74
75         bool ByteArray::_assign(uint8_t *array, size_t size)
76         {
77                 if (array == NULL || size == 0)
78                 {
79                         return false;
80                 }
81
82                 clear();
83
84                 buffer = array;
85                 length = size;
86
87                 return true;
88         }
89
90         size_t ByteArray::size() const
91         {
92                 return length;
93         }
94
95         uint8_t *ByteArray::getBuffer()
96         {
97                 return getBuffer(0);
98         }
99
100         const uint8_t *ByteArray::getBuffer() const
101         {
102                 return getBuffer(0);
103         }
104
105         uint8_t *ByteArray::getBuffer(size_t offset)
106         {
107                 if (length == 0)
108                         return NULL;
109
110                 if (offset >= length)
111                 {
112                         _ERR("buffer overflow, offset [%d], length [%d]", offset, length);
113                         return NULL;
114                 }
115
116                 return buffer + offset;
117         }
118
119         const uint8_t *ByteArray::getBuffer(size_t offset) const
120         {
121                 if (length == 0)
122                         return NULL;
123
124                 if (offset >= length)
125                 {
126                         _ERR("buffer overflow, offset [%d], length [%d]", offset, length);
127                         return NULL;
128                 }
129
130                 return buffer + offset;
131         }
132
133         uint8_t ByteArray::at(size_t index) const
134         {
135                 if (index >= length)
136                 {
137                         _ERR("buffer overflow, index [%d], length [%d]", index, length);
138                         if (length > 0) {
139                                 return buffer[length - 1];
140                         } else {
141                                 return 0;
142                         }
143                 }
144
145                 return buffer[index];
146         }
147
148         uint8_t ByteArray::reverseAt(size_t index) const
149         {
150                 if (index >= length)
151                 {
152                         _ERR("buffer underflow, index [%d], length [%d]", index, length);
153                         if (length > 0) {
154                                 return buffer[0];
155                         } else {
156                                 return 0;
157                         }
158                 }
159
160                 return buffer[length - index - 1];
161         }
162
163         size_t ByteArray::extract(uint8_t *array, size_t size) const
164         {
165                 uint32_t min_len = 0;
166
167                 if (array == NULL || size == 0)
168                 {
169                         _ERR("invalid param");
170                         return min_len;
171                 }
172
173                 min_len = (size < length) ? size : length;
174
175                 memcpy(array, buffer, min_len);
176
177                 return min_len;
178         }
179
180         const ByteArray ByteArray::sub(size_t offset, size_t size) const
181         {
182                 if (length == 0 || offset >= length || (offset + size) > length)
183                 {
184                         _DBG("length is zero");
185
186                         return ByteArray();
187                 }
188
189                 return ByteArray(buffer + offset, size);
190         }
191
192         void ByteArray::clear()
193         {
194                 if (buffer != NULL)
195                 {
196                         delete []buffer;
197                         buffer = NULL;
198                 }
199                 length = 0;
200         }
201
202         /* operator overloading */
203         ByteArray ByteArray::operator +(const ByteArray &T)
204         {
205                 size_t newLen;
206                 uint8_t *newBuffer;
207                 ByteArray newArray;
208
209                 if (length == 0)
210                 {
211                         _DBG("length is zero");
212
213                         return T;
214                 }
215
216                 newLen = T.length;
217
218                 if (newLen == 0)
219                         return *this;
220
221                 newLen += length;
222
223                 newBuffer = new uint8_t[newLen];
224                 if (newBuffer == NULL)
225                 {
226                         /* assert.... */
227                         _ERR("alloc failed");
228
229                         return *this;
230                 }
231
232                 memcpy(newBuffer, buffer, length);
233                 memcpy(newBuffer + length, T.buffer, T.length);
234
235                 newArray._assign(newBuffer, newLen);
236
237                 return newArray;
238         }
239
240         ByteArray &ByteArray::operator =(const ByteArray &T)
241         {
242                 if (this != &T)
243                 {
244                         assign(T.buffer, T.length);
245                 }
246
247                 return *this;
248         }
249
250         ByteArray &ByteArray::operator +=(const ByteArray &T)
251         {
252                 *this = *this + T;
253
254                 return *this;
255         }
256
257         bool ByteArray::operator ==(const ByteArray &T) const
258         {
259                 if (length != T.length)
260                         return false;
261
262                 return (memcmp(buffer, T.buffer, length) == 0);
263         }
264
265         bool ByteArray::operator !=(const ByteArray &T) const
266         {
267                 return !(*this == T);
268         }
269
270         bool ByteArray::operator <(const ByteArray &T) const
271         {
272                 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) < 0);
273         }
274
275         bool ByteArray::operator >(const ByteArray &T) const
276         {
277                 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) > 0);
278         }
279
280         uint8_t ByteArray::operator[](size_t index) const
281         {
282                 if (index >= length)
283                 {
284                         _ERR("buffer overflow, index [%d], length [%d]", index, length);
285                         if (length > 0) {
286                                 return buffer[length -1];
287                         } else {
288                                 return 0;
289                         }
290                 }
291
292                 return buffer[index];
293         }
294
295         const string ByteArray::toString() const
296         {
297 #ifdef TO_STRING_ALL
298                 return toString(true);
299 #else
300                 return toString(false);
301 #endif
302         }
303
304         const string ByteArray::toString(bool entire) const
305         {
306                 stringstream ss;
307
308                 if (length > 0) {
309                         char temp[20];
310                         int count;
311                         int i = 0;
312                         bool ellipsis = false;
313
314                         count = length;
315                         if (entire == false && count > 20)
316                         {
317                                 count = 20;
318                                 ellipsis = true;
319                         }
320
321                         ss << "{ ";
322
323                         for (i = 0; i < count; i++)
324                         {
325                                 snprintf(temp, sizeof(temp), "%02X ", buffer[i]);
326                                 ss << temp;
327                         }
328
329                         if (ellipsis)
330                         {
331                                 ss << "... ";
332                         }
333
334                         ss << "}";
335                 } else {
336                         ss << "buffer is empty";
337                 }
338
339                 return ss.str();
340         }
341
342         void ByteArray::save(const char *filePath)
343         {
344                 FILE *file = NULL;
345
346                 if (filePath == NULL || buffer == NULL || length == 0)
347                         return;
348
349                 if ((file = fopen(filePath, "w")) != NULL) {
350                         fwrite(buffer, 1, length, file);
351                         fflush(file);
352                         fclose(file);
353
354                         SECURE_LOGD("file has written, file [%s], length[%d]", filePath, length);
355                 } else {
356                         _ERR("file open failed, [%d]", errno);
357                 }
358         }
359
360 } /* namespace smartcard_service_api */