Fix the Prevent problems
[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                         {
140                                 return buffer[length - 1];
141                         }
142                         else
143                         {
144                                 return 0;
145                         }
146                 }
147
148                 return buffer[index];
149         }
150
151         uint8_t ByteArray::reverseAt(size_t index) const
152         {
153                 if (index >= length)
154                 {
155                         _ERR("buffer underflow, index [%d], length [%d]", index, length);
156                         if (length > 0)
157                         {
158                                 return buffer[0];
159                         }
160                         else
161                         {
162                                 return 0;
163                         }
164                 }
165
166                 return buffer[length - index - 1];
167         }
168
169         size_t ByteArray::extract(uint8_t *array, size_t size) const
170         {
171                 uint32_t min_len = 0;
172
173                 if (array == NULL || size == 0)
174                 {
175                         _ERR("invalid param");
176                         return min_len;
177                 }
178
179                 min_len = (size < length) ? size : length;
180
181                 memcpy(array, buffer, min_len);
182
183                 return min_len;
184         }
185
186         const ByteArray ByteArray::sub(size_t offset, size_t size) const
187         {
188                 if (length == 0 || offset >= length || (offset + size) > length)
189                 {
190                         _DBG("length is zero");
191
192                         return ByteArray();
193                 }
194
195                 return ByteArray(buffer + offset, size);
196         }
197
198         void ByteArray::clear()
199         {
200                 if (buffer != NULL)
201                 {
202                         delete []buffer;
203                         buffer = NULL;
204                 }
205                 length = 0;
206         }
207
208         /* operator overloading */
209         ByteArray ByteArray::operator +(const ByteArray &T)
210         {
211                 size_t newLen;
212                 uint8_t *newBuffer;
213                 ByteArray newArray;
214
215                 if (length == 0)
216                 {
217                         _DBG("length is zero");
218
219                         return T;
220                 }
221
222                 newLen = T.length;
223
224                 if (newLen == 0)
225                         return *this;
226
227                 newLen += length;
228
229                 newBuffer = new uint8_t[newLen];
230                 if (newBuffer == NULL)
231                 {
232                         /* assert.... */
233                         _ERR("alloc failed");
234
235                         return *this;
236                 }
237
238                 memcpy(newBuffer, buffer, length);
239                 memcpy(newBuffer + length, T.buffer, T.length);
240
241                 newArray._assign(newBuffer, newLen);
242
243                 return newArray;
244         }
245
246         ByteArray &ByteArray::operator =(const ByteArray &T)
247         {
248                 if (this != &T)
249                 {
250                         assign(T.buffer, T.length);
251                 }
252
253                 return *this;
254         }
255
256         ByteArray &ByteArray::operator +=(const ByteArray &T)
257         {
258                 *this = *this + T;
259
260                 return *this;
261         }
262
263         bool ByteArray::operator ==(const ByteArray &T) const
264         {
265                 if (length != T.length)
266                         return false;
267
268                 return (memcmp(buffer, T.buffer, length) == 0);
269         }
270
271         bool ByteArray::operator !=(const ByteArray &T) const
272         {
273                 return !(*this == T);
274         }
275
276         bool ByteArray::operator <(const ByteArray &T) const
277         {
278                 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) < 0);
279         }
280
281         bool ByteArray::operator >(const ByteArray &T) const
282         {
283                 return (memcmp(buffer, T.buffer, (length < T.length) ? length : T.length) > 0);
284         }
285
286         uint8_t ByteArray::operator [](size_t index) const
287         {
288                 if (index >= length)
289                 {
290                         _ERR("buffer overflow, index [%d], length [%d]", index, length);
291                         if (length > 0)
292                         {
293                                 return buffer[length -1];
294                         }
295                         else
296                         {
297                                 return 0;
298                         }
299                 }
300
301                 return buffer[index];
302         }
303         const string ByteArray::toString() const
304         {
305                 stringstream ss;
306
307                 if (length > 0)
308                 {
309                         char temp[20];
310                         int count;
311                         int i = 0;
312                         bool ellipsis = false;
313
314                         count = length;
315                         if (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                 }
336                 else
337                 {
338                         ss << "buffer is empty";
339                 }
340
341                 return ss.str();
342         }
343
344         void ByteArray::save(const char *filePath)
345         {
346                 FILE *file = NULL;
347
348                 if (filePath == NULL || buffer == NULL || length == 0)
349                         return;
350
351                 if ((file = fopen(filePath, "w")) != NULL)
352                 {
353                         fwrite(buffer, 1, length, file);
354                         fflush(file);
355                         fclose(file);
356                 }
357                 else
358                 {
359                         _ERR("file open failed, [%d]", errno);
360                 }
361         }
362
363 } /* namespace smartcard_service_api */
364