Add rule for 'All devices apps' when access rule is empty.
[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 <cstdio>
19 #include <cstring>
20 #include <sstream>
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         TLVHelper::TLVHelper() : currentTLV(this), parentTLV(NULL),
31                 childTLV(NULL), offset(0), currentT(0), currentL(0)
32         {
33         }
34
35         TLVHelper::TLVHelper(TLVHelper *parent) : currentTLV(this),
36                 parentTLV(parent), childTLV(NULL),
37                 offset(0), currentT(0), currentL(0)
38         {
39         }
40
41         TLVHelper::TLVHelper(const ByteArray &array) : currentTLV(this),
42                 parentTLV(NULL), childTLV(NULL),
43                 offset(0), currentT(0), currentL(0)
44         {
45                 setTLVBuffer(array);
46         }
47
48         TLVHelper::TLVHelper(const ByteArray &array, TLVHelper *parent) :
49                 currentTLV(this), parentTLV(NULL), childTLV(NULL),
50                 offset(0), currentT(0), currentL(0)
51         {
52                 setTLVBuffer(array, parent);
53         }
54
55         TLVHelper::~TLVHelper()
56         {
57         }
58
59         bool TLVHelper::setTLVBuffer(const ByteArray &array, TLVHelper *parent)
60         {
61                 if (array.size() == 0)
62                         return false;
63
64                 currentTLV = this;
65                 parentTLV = parent;
66                 childTLV = NULL;
67                 offset = 0;
68                 currentT = 0;
69                 currentL = 0;
70
71                 tlvBuffer = array;
72
73                 return true;
74         }
75
76         bool TLVHelper::setTLVBuffer(const 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                 int temp = 0;
85
86                 currentT = 0;
87                 currentL = 0;
88                 currentV.clear();
89
90                 if (isEndOfBuffer())
91                         return false;
92
93                 /* T */
94                 if ((result = decodeTag(tlvBuffer.getBuffer(offset + temp))) < 0)
95                         return false;
96
97                 temp += result;
98
99                 /* L */
100                 if ((result = decodeLength(tlvBuffer.getBuffer(offset + temp))) < 0)
101                         return false;
102
103                 temp += result;
104
105                 if (currentL > 0)
106                 {
107                         if (currentL > (tlvBuffer.size() - (offset + temp)))
108                                 return false;
109
110                         /* V */
111                         if ((result = decodeValue(tlvBuffer.getBuffer(offset + temp))) < 0)
112                                 return false;
113
114                         temp += result;
115                 }
116
117                 offset += temp;
118
119                 return true;
120         }
121
122         const string TLVHelper::toString() const
123         {
124                 stringstream ss;
125
126                 ss << "T [" << getTag() << "], L [" << size() << "]";
127                 if (currentL > 0)
128                 {
129                         ss << ", V " << getValue().toString();
130                 }
131
132                 return ss.str();
133         }
134
135         TLVHelper *TLVHelper::getParentTLV()
136         {
137                 return parentTLV;
138         }
139
140         bool TLVHelper::enterToValueTLV()
141         {
142                 bool result = false;
143                 TLVHelper *temp = NULL;
144
145                 if (size() >= 2)
146                 {
147                         temp = currentTLV->getChildTLV(getValue());
148
149                         if (temp != NULL)
150                         {
151                                 currentTLV = temp;
152                                 result = true;
153                         }
154                 }
155
156                 return result;
157         }
158
159         bool TLVHelper::returnToParentTLV()
160         {
161                 bool result = true;
162
163                 if (currentTLV->getParentTLV() != NULL)
164                 {
165                         currentTLV = currentTLV->getParentTLV();
166                 }
167                 else
168                 {
169                         /* top tlv */
170                 }
171
172                 return result;
173         }
174
175 } /* namespace smartcard_service_api */