1 /*
2 * Copyright (c) 2024-2024 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 "string_utils.h"
16 #include <cstring>
17 #include <algorithm>
18
19 #include "securec.h"
20
21 namespace OHOS {
22 namespace SignatureTools {
23
IsEmpty(const std::string & cs)24 bool StringUtils::IsEmpty(const std::string& cs)
25 {
26 return cs.empty();
27 }
28
ContainsCase(const std::vector<std::string> & strs,const std::string & str)29 bool StringUtils::ContainsCase(const std::vector<std::string> &strs, const std::string& str)
30 {
31 std::string fileSuffix = str;
32 std::transform(fileSuffix.begin(), fileSuffix.end(), fileSuffix.begin(),
33 [](unsigned char c) { return std::tolower(c); });
34 return std::any_of(strs.begin(), strs.end(), [&fileSuffix](const std::string& val) {return val == fileSuffix; });
35 }
36
CaseCompare(const std::string & str1,const std::string & str2)37 bool StringUtils::CaseCompare(const std::string& str1, const std::string& str2)
38 {
39 return str1 == str2;
40 }
SplitString(const std::string & str,char delimiter)41 std::vector<std::string> StringUtils::SplitString(const std::string& str, char delimiter)
42 {
43 std::vector<std::string> tokens;
44 std::istringstream tokenStream(str);
45 std::string token;
46 while (std::getline(tokenStream, token, delimiter)) {
47 tokens.push_back(token);
48 }
49 return tokens;
50 }
Trim(const std::string & str)51 std::string StringUtils::Trim(const std::string& str)
52 {
53 size_t startpos = str.find_first_not_of(" \n\r\f\v");
54 if (std::string::npos == startpos) {
55 return "";
56 }
57 size_t endpos = str.find_last_not_of(" \n\r\f\v");
58 return str.substr(startpos, endpos - startpos + 1);
59 }
FormatLoading(std::string & dealStr)60 std::string StringUtils::FormatLoading(std::string& dealStr)
61 {
62 char comma = ',';
63 char slash = '/';
64 std::string del = dealStr.substr(dealStr.find_first_of("/") + 1, dealStr.size());
65 int position = 0;
66 while ((position = del.find(slash, position)) != std::string::npos) {
67 del.insert(position + 1, " ");
68 position++;
69 }
70 std::replace(del.begin(), del.end(), slash, comma);
71 return del.append("\n");
72 }
Pkcs7ToString(PKCS7 * p7)73 std::string StringUtils::Pkcs7ToString(PKCS7* p7)
74 {
75 unsigned char* out = NULL;
76 int outSize = i2d_PKCS7(p7, &out);
77 if (out == NULL || outSize <= 0) {
78 SIGNATURE_TOOLS_LOGE("pkcs7 to string failed");
79 return "";
80 }
81 std::string ret;
82 ret.clear();
83 ret.resize(outSize);
84 std::copy(out, out + outSize, &ret[0]);
85 OPENSSL_free(out);
86 return ret;
87 }
x509CertToString(X509 * cert)88 std::string StringUtils::x509CertToString(X509* cert)
89 {
90 VerifyHapOpensslUtils::GetOpensslErrorMessage();
91 BIO* bio = BIO_new(BIO_s_mem());
92 PEM_write_bio_X509(bio, cert);
93 char* buffer;
94 long length = BIO_get_mem_data(bio, &buffer);
95 std::string certStr(buffer, length);
96 BIO_free(bio);
97 return certStr;
98 }
SubjectToString(X509 * cert)99 std::string StringUtils::SubjectToString(X509* cert)
100 {
101 VerifyHapOpensslUtils::GetOpensslErrorMessage();
102 X509_NAME* subjectName = X509_get_subject_name(cert);
103 if (!subjectName) {
104 SIGNATURE_TOOLS_LOGE("Error getting subject name");
105 return "";
106 }
107 VerifyHapOpensslUtils::GetOpensslErrorMessage();
108 char* subjectStr = X509_NAME_oneline(subjectName, NULL, 0);
109 if (!subjectStr) {
110 SIGNATURE_TOOLS_LOGE("Error create subject string");
111 return "";
112 }
113 std::string subjectString(subjectStr);
114 std::string result = FormatLoading(subjectString);
115 OPENSSL_free(subjectStr);
116 return result;
117 }
CheckStringToint(const std::string & in,int & out)118 bool StringUtils::CheckStringToint(const std::string& in, int& out)
119 {
120 std::istringstream iss(in);
121 if ((iss >> out) && iss.eof()) {
122 return true;
123 }
124 SIGNATURE_TOOLS_LOGE("Cannot convert string:%s to integer", in.c_str());
125 return false;
126 }
127 } // namespace SignatureTools
128 } // namespace OHOS
129