2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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.
18 /* standard library header */
22 /* SLP library header */
26 #include "SimpleTLV.h"
28 namespace smartcard_service_api
30 SimpleTLV::SimpleTLV():TLVHelper()
34 SimpleTLV::SimpleTLV(TLVHelper *parent):TLVHelper(parent)
39 SimpleTLV::SimpleTLV(const ByteArray &array):TLVHelper(array)
43 SimpleTLV::SimpleTLV(const ByteArray &array, TLVHelper *parent):TLVHelper(array, parent)
48 SimpleTLV::~SimpleTLV()
57 int SimpleTLV::decodeTag(unsigned char *buffer)
59 /* 0x00 or 0xFF is invalid tag value */
60 if (buffer[0] == 0x00 || buffer[0] == 0xFF)
70 int SimpleTLV::decodeLength(unsigned char *buffer)
74 if (buffer[0] == 0xFF)
77 currentL = (buffer[1] << 8) | buffer[2];
90 int SimpleTLV::decodeValue(unsigned char *buffer)
95 currentV.setBuffer(buffer, currentL);
100 ByteArray SimpleTLV::encode(unsigned int tag, ByteArray buffer)
102 bool isLongBuffer = false;
104 unsigned int total_len = 0;
105 unsigned int current = 0;
106 unsigned char *temp_buffer = NULL;
108 /* add tag's length */
111 /* add length's length */
112 if (buffer.getLength() < 255)
116 else if (buffer.getLength() < 65536)
126 /* add buffer's length */
127 total_len += buffer.getLength();
129 /* alloc new buffer */
130 temp_buffer = new unsigned char[total_len];
131 if (temp_buffer == NULL)
135 memset(temp_buffer, 0, total_len);
138 temp_buffer[current++] = (unsigned char)tag;
141 if (isLongBuffer == true)
143 temp_buffer[current++] = (unsigned char)(0xFF);
144 temp_buffer[current++] = (unsigned char)(buffer.getLength() >> 8);
145 temp_buffer[current++] = (unsigned char)(buffer.getLength());
149 temp_buffer[current++] = (unsigned char)(buffer.getLength());
153 if (buffer.getLength() > 0)
154 memcpy(temp_buffer + current, buffer.getBuffer(), buffer.getLength());
156 result.setBuffer(temp_buffer, total_len);
158 delete []temp_buffer;
163 ByteArray SimpleTLV::encode(unsigned int tag, unsigned char *buffer, unsigned int length)
165 return encode(tag, ByteArray(buffer, length));
168 TLVHelper *SimpleTLV::getChildTLV(ByteArray data)
170 if (childTLV != NULL)
175 childTLV = new SimpleTLV(data, this);
177 return (TLVHelper *)childTLV;
180 ByteArray SimpleTLV::getOctetString(const ByteArray &array)
182 SimpleTLV tlv(array);
184 return SimpleTLV::getOctetString(tlv);
187 ByteArray SimpleTLV::getOctetString(SimpleTLV &tlv)
191 if (tlv.decodeTLV() == true && tlv.getTag() == 0x04) /* OCTET STRING */
193 result = tlv.getValue();
197 SCARD_DEBUG_ERR("getOctetString failed (0x%02X)", tlv.getTag());
203 bool SimpleTLV::getBoolean(const ByteArray &array)
205 SimpleTLV tlv(array);
207 return SimpleTLV::getBoolean(tlv);
210 bool SimpleTLV::getBoolean(SimpleTLV &tlv)
214 if (tlv.decodeTLV() == true && tlv.getTag() == 0x80) /* BOOLEAN */
216 if (tlv.getValue().getAt(0) == 0)
223 SCARD_DEBUG_ERR("getBoolean failed (0x%02X)", tlv.getTag());
229 int SimpleTLV::getInteger(const ByteArray &array)
231 SimpleTLV tlv(array);
233 return SimpleTLV::getInteger(tlv);
236 int SimpleTLV::getInteger(SimpleTLV &tlv)
240 if (tlv.decodeTLV() == true && tlv.getTag() == 0x80) /* TODO : INTEGER */
245 SCARD_DEBUG_ERR("getInteger failed (0x%02X)", tlv.getTag());
250 } /* namespace smartcard_service_api */