update the latest source
[platform/core/connectivity/smartcard-service.git] / common / include / TLVHelper.h
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
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
18 #ifndef TLVHELPER_H_
19 #define TLVHELPER_H_
20
21 /* standard library header */
22 #include <stddef.h>
23
24 /* SLP library header */
25
26 /* local header */
27 #include "ByteArray.h"
28
29 namespace smartcard_service_api
30 {
31         class TLVHelper
32         {
33         protected:
34                 TLVHelper *currentTLV;
35                 TLVHelper *parentTLV;
36                 TLVHelper *childTLV;
37
38                 char strBuffer[200];
39                 ByteArray tlvBuffer;
40                 unsigned int offset;
41
42                 unsigned int currentT;
43                 unsigned int currentL;
44                 ByteArray currentV;
45
46                 void initialize(TLVHelper *parent = NULL);
47                 TLVHelper(TLVHelper *parent);
48                 TLVHelper(const ByteArray &array, TLVHelper *parent);
49
50                 virtual int decodeTag(unsigned char *buffer) = 0;
51                 virtual int decodeLength(unsigned char *buffer) = 0;
52                 virtual int decodeValue(unsigned char *buffer) = 0;
53
54                 virtual TLVHelper *getChildTLV(ByteArray data) = 0;
55                 TLVHelper *getParentTLV();
56
57                 bool setTLVBuffer(const ByteArray &array, TLVHelper *parent);
58                 bool setTLVBuffer(unsigned char *buffer, unsigned int length, TLVHelper *parent);
59
60                 bool _isEndOfBuffer() { return offset >= tlvBuffer.getLength(); }
61                 bool _decodeTLV();
62
63                 unsigned int _getTag() { return currentT; }
64                 unsigned int _getLength() { return currentL; }
65                 ByteArray _getValue() { return currentV; }
66
67         public:
68                 TLVHelper();
69                 TLVHelper(const ByteArray &array);
70                 ~TLVHelper();
71
72                 bool setTLVBuffer(const ByteArray &array) { return setTLVBuffer(array, NULL); }
73                 bool setTLVBuffer(unsigned char *buffer, unsigned int length) { return setTLVBuffer(buffer, length, NULL); }
74
75                 bool isEndOfBuffer() { return currentTLV->_isEndOfBuffer(); }
76                 bool decodeTLV() { return currentTLV->_decodeTLV(); }
77
78                 unsigned int getTag() { return currentTLV->_getTag(); }
79                 unsigned int getLength() { return currentTLV->_getLength(); }
80                 ByteArray getValue() { return currentTLV->_getValue(); }
81
82                 const char *toString();
83
84                 bool enterToValueTLV();
85                 bool returnToParentTLV();
86         };
87
88 } /* namespace smartcard_service_api */
89 #endif /* TLVHELPER_H_ */