Fix the Prevent problems
[platform/core/connectivity/smartcard-service.git] / common / EFDIR.cpp
1 /*
2  * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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 /* standard library header */
18 #include <stdio.h>
19
20 /* SLP library header */
21
22 /* local header */
23 #include "Debug.h"
24 #include "EFDIR.h"
25 #include "APDUHelper.h"
26 #include "SimpleTLV.h"
27
28 namespace smartcard_service_api
29 {
30         static unsigned char path_efdir[] = { 0x2f, 0x00 };
31         static ByteArray PATH_EFDIR(ARRAY_AND_SIZE(path_efdir));
32
33         EFDIR::EFDIR(Channel *channel) : FileObject(channel)
34         {
35         }
36
37         EFDIR::EFDIR(Channel *channel, const ByteArray &selectResponse) :
38                 FileObject(channel, selectResponse)
39         {
40         }
41
42         EFDIR::~EFDIR()
43         {
44         }
45
46         int EFDIR::select()
47         {
48                 int ret;
49
50                 ret = FileObject::select(PATH_EFDIR, false);
51                 if (ret < SCARD_ERROR_OK)
52                 {
53                         _ERR("EFDIR select failed, [%d]", ret);
54                 }
55
56                 return ret;
57         }
58
59         const ByteArray EFDIR::parseRecord(const Record &record, const ByteArray &aid)
60         {
61                 bool matched = false;
62                 ByteArray result;
63                 SimpleTLV tlv(record.getData());
64
65                 if (tlv.decodeTLV() == true && tlv.getTag() == 0x61)
66                 {
67                         tlv.enterToValueTLV();
68                         while (tlv.decodeTLV() == true)
69                         {
70                                 switch (tlv.getTag())
71                                 {
72                                 case 0x4F : /* aid */
73                                         if (tlv.getValue() == aid)
74                                                 matched = true;
75                                         break;
76                                 case 0x50 : /* label */
77                                         break;
78                                 case 0x51 : /* path */
79                                         result = tlv.getValue();
80                                         break;
81                                 case 0x53 : /* ddo */
82                                         break;
83                                 }
84                         }
85                         tlv.returnToParentTLV();
86
87                         if (matched == true)
88                         {
89                                 _DBG("Found!! : path %s", result.toString().c_str());
90                         }
91                         else
92                         {
93                                 result.clear();
94                         }
95                 }
96
97                 return result;
98         }
99
100         const ByteArray EFDIR::getPathByAID(const ByteArray &aid)
101         {
102                 ByteArray result;
103                 Record record;
104                 unsigned int id;
105                 int status = 0;
106
107                 for (id = 1; status >= 0; id++)
108                 {
109                         status = readRecord(0, id, record);
110                         if (status >= 0)
111                         {
112                                 result = parseRecord(record, aid);
113                                 if (result.size() > 0)
114                                         break;
115                         }
116                 }
117
118                 return result;
119         }
120 } /* namespace smartcard_service_api */