Add rule for 'All devices apps' when access rule is empty.
[platform/core/connectivity/smartcard-service.git] / common / SimpleTLV.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 <stdio.h>
19 #include <string.h>
20
21 /* SLP library header */
22
23 /* local header */
24 #include "Debug.h"
25 #include "SimpleTLV.h"
26
27 namespace smartcard_service_api
28 {
29         SimpleTLV::SimpleTLV() : TLVHelper()
30         {
31         }
32
33         SimpleTLV::SimpleTLV(TLVHelper *parent) : TLVHelper(parent)
34         {
35         }
36
37         SimpleTLV::SimpleTLV(const ByteArray &array) : TLVHelper(array)
38         {
39         }
40
41         SimpleTLV::SimpleTLV(const ByteArray &array, TLVHelper *parent) :
42                 TLVHelper(array, parent)
43         {
44         }
45
46         SimpleTLV::~SimpleTLV()
47         {
48                 if (childTLV != NULL)
49                 {
50                         delete childTLV;
51                         childTLV = NULL;
52                 }
53         }
54
55         int SimpleTLV::decodeTag(const unsigned char *buffer)
56         {
57                 /* 0x00 or 0xFF is invalid tag value */
58                 if (buffer[0] == 0x00 || buffer[0] == 0xFF)
59                 {
60                         return -1;
61                 }
62
63                 currentT = buffer[0];
64
65                 return 1;
66         }
67
68         int SimpleTLV::decodeLength(const unsigned char *buffer)
69         {
70                 int count = 0;
71
72                 if (buffer[0] == 0xFF)
73                 {
74                         /* 3 bytes length */
75                         currentL = (buffer[1] << 8) | buffer[2];
76                         count = 3;
77                 }
78                 else
79                 {
80                         /* 1 byte length */
81                         currentL = buffer[0];
82                         count = 1;
83                 }
84
85                 return count;
86         }
87
88         int SimpleTLV::decodeValue(const unsigned char *buffer)
89         {
90                 if (currentL == 0)
91                         return 0;
92
93                 currentV.assign(buffer, currentL);
94
95                 return currentL;
96         }
97
98         const ByteArray SimpleTLV::encode(unsigned int tag, const ByteArray &buffer)
99         {
100                 bool isLongBuffer = false;
101                 ByteArray result;
102                 unsigned int total_len = 0;
103                 unsigned int current = 0;
104                 unsigned char *temp_buffer = NULL;
105
106                 /* add tag's length */
107                 total_len += 1;
108
109                 /* add length's length */
110                 if (buffer.size() < 255)
111                 {
112                         total_len += 1;
113                 }
114                 else if (buffer.size() < 65536)
115                 {
116                         total_len += 3;
117                         isLongBuffer = true;
118                 }
119                 else
120                 {
121                         return result;
122                 }
123
124                 /* add buffer's length */
125                 total_len += buffer.size();
126
127                 /* alloc new buffer */
128                 temp_buffer = new unsigned char[total_len];
129                 if (temp_buffer == NULL)
130                 {
131                         return result;
132                 }
133                 memset(temp_buffer, 0, total_len);
134
135                 /* fill tag */
136                 temp_buffer[current++] = (unsigned char)tag;
137
138                 /* fill length */
139                 if (isLongBuffer == true)
140                 {
141                         temp_buffer[current++] = (unsigned char)(0xFF);
142                         temp_buffer[current++] = (unsigned char)(buffer.size() >> 8);
143                         temp_buffer[current++] = (unsigned char)(buffer.size());
144                 }
145                 else
146                 {
147                         temp_buffer[current++] = (unsigned char)(buffer.size());
148                 }
149
150                 /* fill value */
151                 if (buffer.size() > 0)
152                         memcpy(temp_buffer + current, buffer.getBuffer(), buffer.size());
153
154                 result.assign(temp_buffer, total_len);
155
156                 delete []temp_buffer;
157
158                 return result;
159         }
160
161         const ByteArray SimpleTLV::encode(unsigned int tag, unsigned char *buffer, unsigned int length)
162         {
163                 return encode(tag, ByteArray(buffer, length));
164         }
165
166         TLVHelper *SimpleTLV::getChildTLV(const ByteArray &data)
167         {
168                 if (childTLV != NULL)
169                 {
170                         delete childTLV;
171                         childTLV = NULL;
172                 }
173                 childTLV = new SimpleTLV(data, this);
174
175                 return (TLVHelper *)childTLV;
176         }
177
178         const ByteArray SimpleTLV::getOctetString(const ByteArray &array)
179         {
180                 SimpleTLV tlv(array);
181
182                 return SimpleTLV::getOctetString(tlv);
183         }
184
185         const ByteArray SimpleTLV::getOctetString(SimpleTLV &tlv)
186         {
187                 ByteArray result;
188
189                 if (tlv.decodeTLV() == true && tlv.getTag() == 0x04) /* OCTET STRING */
190                 {
191                         result = tlv.getValue();
192                 }
193                 else
194                 {
195                         _ERR("getOctetString failed (0x%02X)", tlv.getTag());
196                 }
197
198                 return result;
199         }
200
201         bool SimpleTLV::getBoolean(const ByteArray &array)
202         {
203                 SimpleTLV tlv(array);
204
205                 return SimpleTLV::getBoolean(tlv);
206         }
207
208         bool SimpleTLV::getBoolean(SimpleTLV &tlv)
209         {
210                 bool result = false;
211
212                 if (tlv.decodeTLV() == true && tlv.getTag() == 0x80) /* BOOLEAN */
213                 {
214                         if (tlv.getValue().at(0) == 0)
215                                 result = false;
216                         else
217                                 result = true;
218                 }
219                 else
220                 {
221                         _ERR("getBoolean failed (0x%02X)", tlv.getTag());
222                 }
223
224                 return result;
225         }
226
227         int SimpleTLV::getInteger(const ByteArray &array)
228         {
229                 SimpleTLV tlv(array);
230
231                 return SimpleTLV::getInteger(tlv);
232         }
233
234         int SimpleTLV::getInteger(SimpleTLV &tlv)
235         {
236                 int result = 0;
237
238                 if (tlv.decodeTLV() == true && tlv.getTag() == 0x80) /* TODO : INTEGER */
239                 {
240                 }
241                 else
242                 {
243                         _ERR("getInteger failed (0x%02X)", tlv.getTag());
244                 }
245
246                 return result;
247         }
248 } /* namespace smartcard_service_api */