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)
38 SimpleTLV::SimpleTLV(const ByteArray &array):TLVHelper(array)
42 SimpleTLV::SimpleTLV(const ByteArray &array, TLVHelper *parent):TLVHelper(array, parent)
47 SimpleTLV::~SimpleTLV()
56 int SimpleTLV::decodeTag(unsigned char *buffer)
58 /* 0x00 or 0xFF is invalid tag value */
59 if (buffer[0] == 0x00 || buffer[0] == 0xFF)
69 int SimpleTLV::decodeLength(unsigned char *buffer)
73 if (buffer[0] == 0xFF)
76 currentL = (buffer[1] << 8) | buffer[2];
89 int SimpleTLV::decodeValue(unsigned char *buffer)
94 currentV.setBuffer(buffer, currentL);
99 ByteArray SimpleTLV::encode(unsigned int tag, ByteArray buffer)
101 bool isLongBuffer = false;
103 unsigned int total_len = 0;
104 unsigned int current = 0;
105 unsigned char *temp_buffer = NULL;
107 /* add tag's length */
110 /* add length's length */
111 if (buffer.getLength() < 255)
115 else if (buffer.getLength() < 65536)
125 /* add buffer's length */
126 total_len += buffer.getLength();
128 /* alloc new buffer */
129 temp_buffer = new unsigned char[total_len];
130 if (temp_buffer == NULL)
134 memset(temp_buffer, 0, total_len);
137 temp_buffer[current++] = (unsigned char)tag;
140 if (isLongBuffer == true)
142 temp_buffer[current++] = (unsigned char)(0xFF);
143 temp_buffer[current++] = (unsigned char)(buffer.getLength() >> 8);
144 temp_buffer[current++] = (unsigned char)(buffer.getLength());
148 temp_buffer[current++] = (unsigned char)(buffer.getLength());
152 if (buffer.getLength() > 0)
153 memcpy(temp_buffer + current, buffer.getBuffer(), buffer.getLength());
155 result.setBuffer(temp_buffer, total_len);
157 delete []temp_buffer;
162 ByteArray SimpleTLV::encode(unsigned int tag, unsigned char *buffer, unsigned int length)
164 return encode(tag, ByteArray(buffer, length));
167 TLVHelper *SimpleTLV::getChildTLV(ByteArray data)
169 if (childTLV != NULL)
174 childTLV = new SimpleTLV(data, this);
176 return (TLVHelper *)childTLV;
179 ByteArray SimpleTLV::getOctetString(const ByteArray &array)
181 SimpleTLV tlv(array);
183 return SimpleTLV::getOctetString(tlv);
186 ByteArray SimpleTLV::getOctetString(SimpleTLV &tlv)
190 if (tlv.decodeTLV() == true && tlv.getTag() == 0x04) /* OCTET STRING */
192 result = tlv.getValue();
196 _ERR("getOctetString failed (0x%02X)", tlv.getTag());
202 bool SimpleTLV::getBoolean(const ByteArray &array)
204 SimpleTLV tlv(array);
206 return SimpleTLV::getBoolean(tlv);
209 bool SimpleTLV::getBoolean(SimpleTLV &tlv)
213 if (tlv.decodeTLV() == true && tlv.getTag() == 0x80) /* BOOLEAN */
215 if (tlv.getValue().getAt(0) == 0)
222 _ERR("getBoolean failed (0x%02X)", tlv.getTag());
228 int SimpleTLV::getInteger(const ByteArray &array)
230 SimpleTLV tlv(array);
232 return SimpleTLV::getInteger(tlv);
235 int SimpleTLV::getInteger(SimpleTLV &tlv)
239 if (tlv.decodeTLV() == true && tlv.getTag() == 0x80) /* TODO : INTEGER */
244 _ERR("getInteger failed (0x%02X)", tlv.getTag());
249 } /* namespace smartcard_service_api */