2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* standard library header */
22 /* SLP library header */
26 #include "TLVHelper.h"
28 namespace smartcard_service_api
30 TLVHelper::TLVHelper() : currentTLV(this), parentTLV(NULL),
31 childTLV(NULL), offset(0), currentT(0), currentL(0)
35 TLVHelper::TLVHelper(TLVHelper *parent) : currentTLV(this),
36 parentTLV(parent), childTLV(NULL),
37 offset(0), currentT(0), currentL(0)
41 TLVHelper::TLVHelper(const ByteArray &array) : currentTLV(this),
42 parentTLV(NULL), childTLV(NULL),
43 offset(0), currentT(0), currentL(0)
48 TLVHelper::TLVHelper(const ByteArray &array, TLVHelper *parent) :
49 currentTLV(this), parentTLV(NULL), childTLV(NULL),
50 offset(0), currentT(0), currentL(0)
52 setTLVBuffer(array, parent);
55 TLVHelper::~TLVHelper()
59 bool TLVHelper::setTLVBuffer(const ByteArray &array, TLVHelper *parent)
61 if (array.size() == 0)
76 bool TLVHelper::setTLVBuffer(const unsigned char *buffer, unsigned int length, TLVHelper *parent)
78 return setTLVBuffer(ByteArray(buffer, length), parent);
81 bool TLVHelper::_decodeTLV()
94 if ((result = decodeTag(tlvBuffer.getBuffer(offset + temp))) < 0)
100 if ((result = decodeLength(tlvBuffer.getBuffer(offset + temp))) < 0)
107 if (currentL > (tlvBuffer.size() - (offset + temp)))
111 if ((result = decodeValue(tlvBuffer.getBuffer(offset + temp))) < 0)
122 const string TLVHelper::toString() const
126 ss << "T [" << getTag() << "], L [" << size() << "]";
129 ss << ", V " << getValue().toString();
135 TLVHelper *TLVHelper::getParentTLV()
140 bool TLVHelper::enterToValueTLV()
143 TLVHelper *temp = NULL;
147 temp = currentTLV->getChildTLV(getValue());
159 bool TLVHelper::returnToParentTLV()
163 if (currentTLV->getParentTLV() != NULL)
165 currentTLV = currentTLV->getParentTLV();
175 } /* namespace smartcard_service_api */