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 #define LOG_TAG "FlexibleType"
16 #include "flexible_type.h"
17 #include "logger.h"
18 #include "udmf_utils.h"
19 #include "base32_utils.h"
20
21 namespace OHOS {
22 namespace UDMF {
EscapeStr(const std::string & chs)23 std::string FlexibleType::EscapeStr(const std::string &chs)
24 {
25 std::string output;
26 for (auto ch : chs) {
27 if (ch == '?' || ch == ':' || ch == '=' || ch == ',' || ch == '\\') {
28 output += '\\';
29 }
30 output += ch;
31 }
32 return output;
33 }
34
ParseFlexibleUtd(const std::string & typeId,TypeDescriptorCfg & flexibleTypeDescriptorCfg)35 bool FlexibleType::ParseFlexibleUtd(const std::string &typeId, TypeDescriptorCfg &flexibleTypeDescriptorCfg)
36 {
37 flexibleTypeDescriptorCfg.typeId = typeId;
38 std::string flexibleUtd = typeId;
39 std::string flexibleFlag = FLEXIBLE_TYPE_FLAG;
40 flexibleUtd.erase(0, flexibleFlag.size());
41 std::string flexibleUtdDecode = Base32::Decode(flexibleUtd);
42
43 LOG_DEBUG(UDMF_CLIENT, "The typeId be parsed, flexibleUtdDecode: %{public}s", flexibleUtdDecode.c_str());
44 if (flexibleUtdDecode.empty() || flexibleUtdDecode[0] != '?' ||
45 flexibleUtdDecode.find("=") == flexibleUtdDecode.npos) {
46 LOG_WARN(UDMF_CLIENT, "The typeId cannot be parsed, %{public}s ", typeId.c_str());
47 return false;
48 }
49 std::vector<std::string> flexibleTypeAttrs = UTILS::StrSplit(flexibleUtdDecode, ":");
50 for (auto attr : flexibleTypeAttrs) {
51 std::vector<std::string> attrkv = UTILS::StrSplit(attr, "=");
52 if (attrkv.size() != ATTRIBUTE_PAIR_SIZE) {
53 continue;
54 }
55 if (attrkv[1].length() > MAX_TYPE_SIZE) {
56 LOG_ERROR(UDMF_CLIENT, "Attribute too long, attribute: %{public}s", attr.c_str());
57 return false;
58 }
59 std::string attrName = attrkv[0];
60 if (attrName.find(std::to_string(BELONGINGTO_TYPE)) != attrName.npos) {
61 flexibleTypeDescriptorCfg.belongingToTypes.push_back(attrkv[1]);
62 } else if (attrName.find(std::to_string(MIMETYPE)) != attrName.npos) {
63 flexibleTypeDescriptorCfg.mimeTypes.push_back(attrkv[1]);
64 } else if (attrName.find(std::to_string(FILE_EXTENTSION)) != attrName.npos) {
65 flexibleTypeDescriptorCfg.filenameExtensions.push_back(attrkv[1]);
66 }
67 }
68 return true;
69 }
70
GenFlexibleUtd(const std::string & mimeType,const std::string & fileExtension,const std::string & belongsTo)71 std::string FlexibleType::GenFlexibleUtd(const std::string &mimeType, const std::string &fileExtension,
72 const std::string &belongsTo)
73 {
74 std::string flexibleUtd = "?";
75 if (!belongsTo.empty() && belongsTo!=DEFAULT_TYPE_ID) {
76 flexibleUtd += std::to_string(BELONGINGTO_TYPE) + "=" + belongsTo;
77 }
78 if (!mimeType.empty()) {
79 flexibleUtd += ":" + std::to_string(MIMETYPE) + "=" + mimeType;
80 }
81 if (!fileExtension.empty()) {
82 flexibleUtd += ":" + std::to_string(FILE_EXTENTSION) + "=" + fileExtension;
83 }
84 std::string encodeUtd = Base32::Encode(flexibleUtd);
85 LOG_DEBUG(UDMF_CLIENT, "FlexibleUtd typeId is: %{public}s, encodeUtd is: %{public}s",
86 flexibleUtd.c_str(), encodeUtd.c_str());
87 return FLEXIBLE_TYPE_FLAG + encodeUtd;
88 }
89
90 } // namespace UDMF
91 } // namespace OHOS
92
93