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