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