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 */
21 /* SLP library header */
25 #include "SimpleTLV.h"
27 namespace smartcard_service_api
29 SimpleTLV::SimpleTLV() : TLVHelper()
33 SimpleTLV::SimpleTLV(TLVHelper *parent) : TLVHelper(parent)
37 SimpleTLV::SimpleTLV(const ByteArray &array) : TLVHelper(array)
41 SimpleTLV::SimpleTLV(const ByteArray &array, TLVHelper *parent) :
42 TLVHelper(array, parent)
46 SimpleTLV::~SimpleTLV()
55 int SimpleTLV::decodeTag(const unsigned char *buffer)
57 /* 0x00 or 0xFF is invalid tag value */
58 if (buffer[0] == 0x00 || buffer[0] == 0xFF)
68 int SimpleTLV::decodeLength(const unsigned char *buffer)
72 if (buffer[0] == 0xFF)
75 currentL = (buffer[1] << 8) | buffer[2];
88 int SimpleTLV::decodeValue(const unsigned char *buffer)
93 currentV.assign(buffer, currentL);
98 const ByteArray SimpleTLV::encode(unsigned int tag, const ByteArray &buffer)
100 bool isLongBuffer = false;
102 unsigned int total_len = 0;
103 unsigned int current = 0;
104 unsigned char *temp_buffer = NULL;
106 /* add tag's length */
109 /* add length's length */
110 if (buffer.size() < 255)
114 else if (buffer.size() < 65536)
124 /* add buffer's length */
125 total_len += buffer.size();
127 /* alloc new buffer */
128 temp_buffer = new unsigned char[total_len];
129 if (temp_buffer == NULL)
133 memset(temp_buffer, 0, total_len);
136 temp_buffer[current++] = (unsigned char)tag;
139 if (isLongBuffer == true)
141 temp_buffer[current++] = (unsigned char)(0xFF);
142 temp_buffer[current++] = (unsigned char)(buffer.size() >> 8);
143 temp_buffer[current++] = (unsigned char)(buffer.size());
147 temp_buffer[current++] = (unsigned char)(buffer.size());
151 if (buffer.size() > 0)
152 memcpy(temp_buffer + current, buffer.getBuffer(), buffer.size());
154 result.assign(temp_buffer, total_len);
156 delete []temp_buffer;
161 const ByteArray SimpleTLV::encode(unsigned int tag, unsigned char *buffer, unsigned int length)
163 return encode(tag, ByteArray(buffer, length));
166 TLVHelper *SimpleTLV::getChildTLV(const ByteArray &data)
168 if (childTLV != NULL)
173 childTLV = new SimpleTLV(data, this);
175 return (TLVHelper *)childTLV;
178 const ByteArray SimpleTLV::getOctetString(const ByteArray &array)
180 SimpleTLV tlv(array);
182 return SimpleTLV::getOctetString(tlv);
185 const ByteArray SimpleTLV::getOctetString(SimpleTLV &tlv)
189 if (tlv.decodeTLV() == true && tlv.getTag() == 0x04) /* OCTET STRING */
191 result = tlv.getValue();
195 _ERR("getOctetString failed (0x%02X)", tlv.getTag());
201 bool SimpleTLV::getBoolean(const ByteArray &array)
203 SimpleTLV tlv(array);
205 return SimpleTLV::getBoolean(tlv);
208 bool SimpleTLV::getBoolean(SimpleTLV &tlv)
212 if (tlv.decodeTLV() == true && tlv.getTag() == 0x80) /* BOOLEAN */
214 if (tlv.getValue().at(0) == 0)
221 _ERR("getBoolean failed (0x%02X)", tlv.getTag());
227 int SimpleTLV::getInteger(const ByteArray &array)
229 SimpleTLV tlv(array);
231 return SimpleTLV::getInteger(tlv);
234 int SimpleTLV::getInteger(SimpleTLV &tlv)
238 if (tlv.decodeTLV() == true && tlv.getTag() == 0x80) /* TODO : INTEGER */
243 _ERR("getInteger failed (0x%02X)", tlv.getTag());
248 } /* namespace smartcard_service_api */