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