1 /*
2 * Copyright (c) 2022 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
16 #include "print_utils.h"
17
18 #include <fcntl.h>
19 #include "ability.h"
20 #include "print_constant.h"
21 #include "print_log.h"
22 #include "securec.h"
23
24 namespace OHOS::Print {
25 static std::map<uint32_t, std::string> jobStateMap_;
26 const std::string GLOBAL_ID_DELIMITER = ":";
27 const std::string EXTENSION_CID_DELIMITER = ":";
28 const std::string TASK_EVENT_DELIMITER = "-";
29
ToLower(const std::string & s)30 std::string PrintUtils::ToLower(const std::string &s)
31 {
32 std::string res = s;
33 std::transform(res.begin(), res.end(), res.begin(), tolower);
34 return res;
35 }
36
GetExtensionId(const std::string & globalId)37 std::string PrintUtils::GetExtensionId(const std::string &globalId)
38 {
39 auto pos = globalId.find(GLOBAL_ID_DELIMITER);
40 if (pos == std::string::npos) {
41 return "";
42 }
43 return globalId.substr(0, pos);
44 }
45
GetGlobalId(const std::string & extensionId,const std::string & localId)46 std::string PrintUtils::GetGlobalId(const std::string& extensionId, const std::string& localId)
47 {
48 return extensionId + GLOBAL_ID_DELIMITER + localId;
49 }
50
GetLocalId(const std::string & globalId,const std::string & extensionId)51 std::string PrintUtils::GetLocalId(const std::string& globalId, const std::string& extensionId)
52 {
53 auto pos = globalId.find(GLOBAL_ID_DELIMITER);
54 if (pos == std::string::npos) {
55 return "";
56 }
57
58 if (globalId.substr(0, pos) != extensionId) {
59 return "";
60 }
61 return globalId.substr(pos + 1);
62 }
63
EncodeExtensionCid(const std::string & extensionId,uint32_t callbackId)64 std::string PrintUtils::EncodeExtensionCid(const std::string &extensionId, uint32_t callbackId)
65 {
66 return extensionId + EXTENSION_CID_DELIMITER + std::to_string(callbackId);
67 }
68
DecodeExtensionCid(const std::string & cid,std::string & extensionId,uint32_t & callbackId)69 bool PrintUtils::DecodeExtensionCid(const std::string &cid, std::string &extensionId, uint32_t &callbackId)
70 {
71 auto pos = cid.find(EXTENSION_CID_DELIMITER);
72 if (pos == std::string::npos) {
73 return false;
74 }
75 extensionId = cid.substr(0, pos);
76 callbackId = static_cast<uint32_t>(atoi(cid.substr(pos + 1).c_str()));
77 return true;
78 }
79
GetTaskEventId(const std::string & taskId,const std::string & type)80 std::string PrintUtils::GetTaskEventId(const std::string &taskId, const std::string &type)
81 {
82 return type + TASK_EVENT_DELIMITER + taskId;
83 }
84
OpenFile(const std::string & filePath)85 int32_t PrintUtils::OpenFile(const std::string &filePath)
86 {
87 if (!IsPathValid(filePath)) {
88 return PRINT_INVALID_ID;
89 }
90 int32_t fd = open(filePath.c_str(), O_RDONLY);
91 PRINT_HILOGD("fd: %{public}d", fd);
92 if (fd < 0) {
93 PRINT_HILOGE("Failed to open file errno: %{public}s", std::to_string(errno).c_str());
94 return PRINT_INVALID_ID;
95 }
96 return fd;
97 }
98
IsPathValid(const std::string & filePath)99 bool PrintUtils::IsPathValid(const std::string &filePath)
100 {
101 auto path = filePath.substr(0, filePath.rfind('/'));
102 char resolvedPath[PATH_MAX + 1] = { 0 };
103 if (path.length() > PATH_MAX || realpath(path.c_str(), resolvedPath) == nullptr ||
104 strncmp(resolvedPath, path.c_str(), path.length()) != 0) {
105 PRINT_HILOGE("invalid file path!");
106 return false;
107 }
108 return true;
109 }
110
GetIdFromFdPath(const std::string & fdPath)111 uint32_t PrintUtils::GetIdFromFdPath(const std::string &fdPath)
112 {
113 std::string fd_str = fdPath.substr(fdPath.rfind('/') + 1, fdPath.length());
114 std::stringstream getStrStream(fd_str);
115 uint32_t fd;
116 if (!(getStrStream >> fd)) {
117 PRINT_HILOGD("failed to convert to uint32");
118 }
119 return fd;
120 }
121
GetJobStateChar(const uint32_t state)122 std::string PrintUtils::GetJobStateChar(const uint32_t state)
123 {
124 if (jobStateMap_.size() == 0) {
125 jobStateMap_[PRINT_JOB_PREPARED] = "PRINT_JOB_PREPARED";
126 jobStateMap_[PRINT_JOB_QUEUED] = "PRINT_JOB_QUEUED";
127 jobStateMap_[PRINT_JOB_RUNNING] = "PRINT_JOB_RUNNING";
128 jobStateMap_[PRINT_JOB_BLOCKED] = "PRINT_JOB_BLOCKED";
129 jobStateMap_[PRINT_JOB_COMPLETED] = "PRINT_JOB_COMPLETED";
130 jobStateMap_[PRINT_JOB_UNKNOWN] = "PRINT_JOB_UNKNOWN";
131 }
132 auto it = jobStateMap_.find(state);
133 if (it != jobStateMap_.end()) {
134 return it -> second;
135 }
136 return "PRINT_JOB_UNKNOWN";
137 }
138 } // namespace OHOS::Print
139