1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License"){}
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "vcard_decoder_v30.h"
16 
17 #include "telephony_errors.h"
18 #include "telephony_log_wrapper.h"
19 #include "vcard_constant.h"
20 #include "vcard_file_utils.h"
21 #include "vcard_utils.h"
22 
23 namespace OHOS {
24 namespace Telephony {
25 
ReadBegin()26 bool VCardDecoderV30::ReadBegin()
27 {
28     return VCardDecoderV21::ReadBegin();
29 }
30 
GetLine()31 std::string VCardDecoderV30::GetLine()
32 {
33     std::string line = "";
34     if (!preLine_.empty()) {
35         line = preLine_;
36         preLine_ = "";
37         return line;
38     }
39     fileUtils_.ReadLine(line);
40     return line;
41 }
42 
PeekLine()43 std::string VCardDecoderV30::PeekLine()
44 {
45     if (!preLine_.empty()) {
46         return preLine_;
47     }
48     std::string line = "";
49     fileUtils_.PeekLine(line);
50     return line;
51 }
52 
GetNonEmptyLine()53 std::string VCardDecoderV30::GetNonEmptyLine()
54 {
55     std::string line;
56     std::string ret = "";
57     while (fileUtils_.ReadLine(line)) {
58         if (line.empty()) {
59             continue;
60         }
61         if (line[0] == ' ' || line[0] == '\t') {
62             if (!preLine_.empty()) {
63                 ret += preLine_;
64                 preLine_ = "";
65             }
66             ret += line.substr(1);
67             continue;
68         }
69         if (!ret.empty() || !preLine_.empty()) {
70             break;
71         }
72         preLine_ = line;
73     }
74     if (ret.empty()) {
75         ret = preLine_;
76     }
77     preLine_ = line;
78     return VCardUtils::Trim(ret);
79 }
80 
DealParams(const std::string & params,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)81 void VCardDecoderV30::DealParams(const std::string &params, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
82 {
83     VCardDecoderV21::DealParams(params, rawData, errorCode);
84     if (errorCode == TELEPHONY_SUCCESS) {
85         return;
86     }
87     auto strs = VCardUtils::Split(params, "=");
88     if (static_cast<int32_t>(strs.size()) == SIZE_TWO) {
89         std::string name = VCardUtils::ToUpper(VCardUtils::Trim(strs[0]));
90         std::string value = VCardUtils::Trim(strs[1]);
91         DealAnyParam(name, value, rawData, errorCode);
92         errorCode = TELEPHONY_SUCCESS;
93         return;
94     }
95     errorCode = TELEPHONY_ERR_VCARD_FILE_INVALID;
96 }
97 
EncodeParamValue(const std::string & value)98 std::string VCardDecoderV30::EncodeParamValue(const std::string &value)
99 {
100     int32_t errorCode = TELEPHONY_SUCCESS;
101     return VCardUtils::ConvertCharset(value, "", DEFAULT_IMPORT_CHARSET, errorCode);
102 }
103 
DealAnyParam(const std::string & param,const std::string & paramValue,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)104 void VCardDecoderV30::DealAnyParam(
105     const std::string &param, const std::string &paramValue, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
106 {
107     DealParamV30(param, paramValue, rawData, errorCode);
108 }
109 
DealNoNameParam(const std::string & paramValue,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)110 void VCardDecoderV30::DealNoNameParam(
111     const std::string &paramValue, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
112 {
113     DealTypeParam(paramValue, rawData, errorCode);
114 }
115 
DealTypeParam(const std::string & paramValue,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)116 void VCardDecoderV30::DealTypeParam(
117     const std::string &paramValue, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
118 {
119     DealParamV30(VCARD_PARAM_TYPE, paramValue, rawData, errorCode);
120 }
121 
DealAgent(std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)122 void VCardDecoderV30::DealAgent(std::shared_ptr<VCardRawData> rawData, int32_t &errorCode) {}
123 
UnescapeText(const std::string & from)124 std::string VCardDecoderV30::UnescapeText(const std::string &from)
125 {
126     std::string result;
127     for (size_t i = 0; i < from.length(); i++) {
128         auto ch = from[i];
129         if (ch == '\\' && i < from.length() - 1) {
130             char nextCh = from[++i];
131             if (nextCh == 'n' || nextCh == 'N') {
132                 result.append("\n");
133                 continue;
134             }
135             result += nextCh;
136             continue;
137         }
138         result += ch;
139     }
140     return result;
141 }
142 
UnescapeChar(char ch)143 std::string VCardDecoderV30::UnescapeChar(char ch)
144 {
145     if (ch == 'n' || ch == 'N') {
146         return "\n";
147     }
148     return std::string(1, ch);
149 }
150 
DealParamV30(const std::string & param,const std::string & paramValue,std::shared_ptr<VCardRawData> rawData,int32_t & errorCode)151 void VCardDecoderV30::DealParamV30(
152     const std::string &param, const std::string &paramValue, std::shared_ptr<VCardRawData> rawData, int32_t &errorCode)
153 {
154     auto quoted = false;
155     std::string value = "";
156     for (size_t i = 0; i < paramValue.length(); i++) {
157         auto ch = paramValue[i];
158         if (ch == '"') {
159             if (quoted) {
160                 rawData->AppendParameter(param, EncodeParamValue(value));
161                 value.clear();
162                 quoted = false;
163                 continue;
164             }
165             quoted = true;
166             continue;
167         }
168         if (ch == ',' && !quoted) {
169             if (value.empty()) {
170                 continue;
171             }
172             rawData->AppendParameter(param, EncodeParamValue(value));
173             value.clear();
174             continue;
175         }
176         value += ch;
177     }
178     if (quoted) {
179         TELEPHONY_LOGI("non quote at end");
180     }
181     if (value.empty()) {
182         TELEPHONY_LOGI("value is empty");
183         return;
184     }
185     rawData->AppendParameter(param, EncodeParamValue(value));
186 }
187 
GetVersion()188 std::string VCardDecoderV30::GetVersion()
189 {
190     return VERSION_30;
191 }
192 
GetBase64(const std::string & value,int32_t & errorCode)193 std::string VCardDecoderV30::GetBase64(const std::string &value, int32_t &errorCode)
194 {
195     return value;
196 }
197 
GetSupportType()198 std::vector<std::string> VCardDecoderV30::GetSupportType()
199 {
200     return { VCARD_TYPE_BEGIN, VCARD_TYPE_END, VCARD_TYPE_LOGO, VCARD_TYPE_PHOTO, "LABEL", VCARD_TYPE_FN, "TITLE",
201         "SOUND", VCARD_TYPE_VERSION, VCARD_TYPE_TEL, VCARD_TYPE_EMAIL, "TZ", "GEO", VCARD_TYPE_NOTE, VCARD_TYPE_URL,
202         VCARD_TYPE_BDAY, "ROLE", "REV", "UID", "KEY", "MAILER", "NAME", "PROFILE", "SOURCE", VCARD_TYPE_NICKNAME,
203         "CLASS", "SORT-STRING", "CATEGORIES", "PRODID", "IMPP" };
204 }
205 } // namespace Telephony
206 } // namespace OHOS
207