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