Adjust comment
[platform/core/connectivity/smartcard-service.git] / common / TLVHelper.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 "TLVHelper.h"
26
27 namespace smartcard_service_api
28 {
29         void TLVHelper::initialize(TLVHelper *parent)
30         {
31                 parentTLV = parent;
32                 childTLV = NULL;
33                 currentTLV = this;
34                 offset = 0;
35                 currentT = 0;
36                 currentL = 0;
37         }
38
39         TLVHelper::TLVHelper()
40         {
41                 initialize();
42         }
43
44         TLVHelper::TLVHelper(TLVHelper *parent)
45         {
46                 initialize(parent);
47         }
48
49         TLVHelper::TLVHelper(const ByteArray &array)
50         {
51                 setTLVBuffer(array);
52         }
53
54         TLVHelper::TLVHelper(const ByteArray &array, TLVHelper *parent)
55         {
56                 setTLVBuffer(array, parent);
57         }
58
59         TLVHelper::~TLVHelper()
60         {
61         }
62
63         bool TLVHelper::setTLVBuffer(const ByteArray &array, TLVHelper *parent)
64         {
65                 initialize(parent);
66
67                 if (array.getLength() == 0)
68                         return false;
69
70                 tlvBuffer = array;
71
72                 return true;
73         }
74
75         bool TLVHelper::setTLVBuffer(unsigned char *buffer, unsigned int length, TLVHelper *parent)
76         {
77                 return setTLVBuffer(ByteArray(buffer, length), parent);
78         }
79
80         bool TLVHelper::_decodeTLV()
81         {
82                 int result;
83
84                 currentT = 0;
85                 currentL = 0;
86                 currentV.releaseBuffer();
87
88                 if (isEndOfBuffer())
89                         return false;
90
91                 /* T */
92                 if ((result = decodeTag(tlvBuffer.getBuffer(offset))) < 0)
93                         return false;
94
95                 offset += result;
96
97                 /* L */
98                 if ((result = decodeLength(tlvBuffer.getBuffer(offset))) < 0)
99                         return false;
100
101                 offset += result;
102
103                 if (currentL > 0)
104                 {
105                         /* V */
106                         if ((result = decodeValue(tlvBuffer.getBuffer(offset))) < 0)
107                                 return false;
108
109                         offset += result;
110                 }
111
112                 return true;
113         }
114
115         const char *TLVHelper::toString()
116         {
117                 memset(strBuffer, 0, sizeof(strBuffer));
118
119                 if (currentL == 0)
120                 {
121                         snprintf(strBuffer, sizeof(strBuffer), "T [%X], L [%d]", getTag(), getLength());
122                 }
123                 else
124                 {
125                         snprintf(strBuffer, sizeof(strBuffer), "T [%X], L [%d], V %s", getTag(), getLength(), getValue().toString());
126                 }
127
128                 return strBuffer;
129         }
130
131         TLVHelper *TLVHelper::getParentTLV()
132         {
133                 return parentTLV;
134         }
135
136         bool TLVHelper::enterToValueTLV()
137         {
138                 bool result = false;
139                 TLVHelper *temp = NULL;
140
141                 if (getLength() >= 2)
142                 {
143                         temp = currentTLV->getChildTLV(getValue());
144
145                         if (temp != NULL)
146                         {
147                                 currentTLV = temp;
148                                 result = true;
149                         }
150                 }
151
152                 return result;
153         }
154
155         bool TLVHelper::returnToParentTLV()
156         {
157                 bool result = true;
158
159 //              SCARD_DEBUG("current [%p], parent [%p]", currentTLV, currentTLV->getParentTLV());
160
161                 if (currentTLV->getParentTLV() != NULL)
162                 {
163                         currentTLV = currentTLV->getParentTLV();
164                 }
165                 else
166                 {
167                         /* top tlv */
168                 }
169
170                 return result;
171         }
172
173 } /* namespace smartcard_service_api */