!487 代码规范与安全整改

Merge pull request !487 from 刘昊苏/master
This commit is contained in:
openharmony_ci 2024-11-07 03:46:35 +00:00 committed by Gitee
commit d42904f2cb
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
18 changed files with 253 additions and 153 deletions

View File

@ -19,6 +19,7 @@
#include "ability.h"
#include "napi_base_context.h"
#include "scan_log.h"
#include "scan_util.h"
#include "securec.h"
namespace OHOS::Scan {
@ -376,7 +377,11 @@ bool NapiScanUtils::DecodeExtensionCid(const std::string &cid, std::string &exte
return false;
}
extensionId = cid.substr(0, pos);
callbackId = static_cast<uint32_t>(atoi(cid.substr(pos + 1).c_str()));
int32_t callbackIdTmp = 0;
if (!ScanUtil::ConvertToInt(cid.substr(pos + 1), callbackIdTmp)) {
return false;
}
callbackId = static_cast<uint32_t>(callbackIdTmp);
return true;
}

View File

@ -15,14 +15,15 @@
#include "print_utils.h"
#include <fcntl.h>
#include "ability.h"
#include "securec.h"
#include <chrono>
#include <sstream>
#include <cstdlib>
#include <ctime>
#include <fcntl.h>
#include <random>
#include <sstream>
#include "ability.h"
#include "print_util.h"
#include "securec.h"
namespace OHOS::Print {
@ -88,7 +89,11 @@ bool PrintUtils::DecodeExtensionCid(const std::string &cid, std::string &extensi
return false;
}
extensionId = cid.substr(0, pos);
callbackId = static_cast<uint32_t>(atoi(cid.substr(pos + 1).c_str()));
int32_t callbackIdTmp = 0;
if (!PrintUtil::ConvertToInt(cid.substr(pos + 1), callbackIdTmp)) {
return false;
}
callbackId = static_cast<uint32_t>(callbackIdTmp);
return true;
}
@ -300,7 +305,7 @@ std::string PrintUtils::GetPrintJobId()
ss << timestamp;
std::random_device rd;
std::mt19937 gen((unsigned int)time(NULL));
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(MINIMUN_RANDOM_NUMBER_100, MAXIMUN_RANDOM_NUMBER_999);
int32_t randomNumber = dis(gen);
std::string jobId = ss.str() + "_" + std::to_string(randomNumber);

View File

@ -55,8 +55,7 @@ bool ConvertStringToInt(const char *src, int &dst)
if (src == nullptr) {
return false;
}
dst = atoi(src);
if (errno == ERANGE) {
if (!PrintUtil::ConvertToInt(std::string(src), dst)) {
PRINT_HILOGW("ConvertStringToInt fail: %{public}s", src);
return false;
}

View File

@ -23,6 +23,7 @@
#include "ability_manager_client.h"
#include "print_converter.h"
#include "print_manager_client.h"
#include "print_util.h"
using json = nlohmann::json;
@ -473,23 +474,31 @@ void ParsePrinterPreference(const std::string &printerPreference, Print_PrinterI
json setting = preferenceJson["setting"];
std::string defaultDuplex = GetSettingItemString(DUPLEX_STRING, defaultSetting, setting);
if (!defaultDuplex.empty()) {
ConvertDuplexMode(std::atoi(defaultDuplex.c_str()), nativePrinterInfo.defaultValue.defaultDuplexMode);
int32_t defaultDuplexNum = 0;
if (!PrintUtil::ConvertToInt(defaultDuplex, defaultDuplexNum)) {
PRINT_HILOGE("defaultDuplex %{public}s can not parse to number", defaultDuplex.c_str());
return;
}
ConvertDuplexMode(defaultDuplexNum, nativePrinterInfo.defaultValue.defaultDuplexMode);
std::string defaultOrientation = GetSettingItemString(ORIENTATION_STRING, defaultSetting, setting);
if (!defaultOrientation.empty()) {
ConvertOrientationMode(
std::atoi(defaultOrientation.c_str()), nativePrinterInfo.defaultValue.defaultOrientation);
int32_t defaultOrientationNum = 0;
if (!PrintUtil::ConvertToInt(defaultOrientation, defaultOrientationNum)) {
PRINT_HILOGE("%{public}s is incorrectly formatted.", defaultOrientation.c_str());
return;
}
ConvertOrientationMode(defaultOrientationNum, nativePrinterInfo.defaultValue.defaultOrientation);
nativePrinterInfo.defaultValue.defaultPageSizeId =
CopyString(GetSettingItemString(PAGESIZEID_STRING, defaultSetting, setting));
std::string defaultQuality = GetSettingItemString(QUALITY_STRING, defaultSetting, setting);
if (!defaultQuality.empty()) {
ConvertQuality(std::atoi(defaultQuality.c_str()), nativePrinterInfo.defaultValue.defaultPrintQuality);
int32_t defaultQualityNum = 0;
if (!PrintUtil::ConvertToInt(defaultQuality, defaultQualityNum)) {
PRINT_HILOGE("defaultQuality %{public}s can not parse to number", defaultQuality.c_str());
return;
}
ConvertQuality(defaultQualityNum, nativePrinterInfo.defaultValue.defaultPrintQuality);
}
Print_PrinterInfo *ConvertToNativePrinterInfo(const PrinterInfo &info)

View File

@ -23,6 +23,7 @@
#include "scan_manager_client.h"
#include "scan_constant.h"
#include "scan_log.h"
#include "scan_util.h"
#include "scanner_info.h"
#include "scan_option_value.h"
#include "ohscan.h"
@ -300,6 +301,37 @@ Scan_ScannerOptions* GetScanParaValue(ScanParaTable &paraTable)
}
return scannerOptions;
}
int32_t GetScanOptionValue(const uint32_t& valueType, const ValueMap& valueMap,
const char* value, ScanOptionValue& scanOptionValue)
{
std::string strvalue = std::string(value);
if (valueType == SCAN_INT_TYPE) {
if (!valueMap.numList.count(strvalue)) {
SCAN_HILOGE("not exit this value: %{public}s", strvalue.c_str());
return SCAN_ERROR_INVALID_PARAMETER;
}
int32_t intValue = 0;
if (!ScanUtil::ConvertToInt(strvalue, intValue)) {
SCAN_HILOGE("strvalue : %{public}s can not parse to number.", strvalue.c_str());
return SCAN_ERROR_GENERIC_FAILURE;
}
scanOptionValue.SetNumValue(intValue);
scanOptionValue.SetScanOptionValueType(SCAN_VALUE_NUM);
} else if (valueType == SCAN_STRING_TYPE) {
if (!valueMap.strList.count(strvalue)) {
SCAN_HILOGE("not exit this value: %{public}s", strvalue.c_str());
return SCAN_ERROR_INVALID_PARAMETER;
}
scanOptionValue.SetStrValue(strvalue);
scanOptionValue.SetScanOptionValueType(SCAN_VALUE_STR);
} else {
SCAN_HILOGE("not exist this type: %{public}u", valueType);
return SCAN_ERROR_GENERIC_FAILURE;
}
return SCAN_ERROR_NONE;
}
}
int32_t OH_Scan_Init()
@ -428,33 +460,18 @@ int32_t OH_Scan_SetScannerParameter(const char* scannerId, const int32_t option,
return SCAN_ERROR_INVALID_PARAMETER;
}
auto t = g_valueMap[scannerId].find(option);
ScanOptionValue scanOptionValue;
uint32_t valueType = g_valueMap[scannerId][option].valueType;
std::string strvalue = std::string(value);
ScanOptionValue optionValue;
if (valueType == SCAN_INT_TYPE) {
if (!t->second.numList.count(strvalue)) {
SCAN_HILOGE("not exit this value: [%{public}s]", strvalue.c_str());
return SCAN_ERROR_INVALID_PARAMETER;
}
optionValue.SetNumValue(std::stoi(strvalue));
optionValue.SetScanOptionValueType(SCAN_VALUE_NUM);
} else if (valueType == SCAN_STRING_TYPE) {
if (!t->second.strList.count(strvalue)) {
SCAN_HILOGE("not exit this value: [%{public}s]", strvalue.c_str());
return SCAN_ERROR_INVALID_PARAMETER;
}
optionValue.SetStrValue(strvalue);
optionValue.SetScanOptionValueType(SCAN_VALUE_STR);
} else {
SCAN_HILOGI("not exist this type ");
return SCAN_ERROR_GENERIC_FAILURE;
int32_t ret = GetScanOptionValue(valueType, t->second, value, scanOptionValue);
if (ret != SCAN_ERROR_NONE) {
SCAN_HILOGE("GetScanOptionValue failed, ErxrorCode: [%{public}d]", ret);
return ret;
}
int32_t optionIndex = t->second.optionIndex;
int32_t info;
int32_t ret = client->OpScanOptionValue(std::string(scannerId),
optionIndex, SCAN_ACTION_SET_VALUE, optionValue, info);
int32_t info = 0;
ret = client->OpScanOptionValue(std::string(scannerId),
optionIndex, SCAN_ACTION_SET_VALUE, scanOptionValue, info);
if (ret != SCAN_ERROR_NONE) {
SCAN_HILOGE("SetScannerParameter failed, ErxrorCode: [%{public}d]", ret);
return ret;

View File

@ -47,7 +47,7 @@ struct PrintTypeInfo {
public:
PrintSecurityGuardInfo(const std::string callPkg, const std::vector<std::string> &fileList);
std::string ToJsonStr();
void setPrintTypeInfo(const PrinterInfo &printerInfo, const PrintJob &printJob);
void SetPrintTypeInfo(const PrinterInfo &printerInfo, const PrintJob &printJob);
private:
int32_t subType_{};

View File

@ -16,23 +16,30 @@
#ifndef PRINT_USER_DATA_H
#define PRINT_USER_DATA_H
#include <charconv>
#include <string>
#include <map>
#include <deque>
#include <mutex>
#include "printer_info.h"
#include "print_util.h"
#include "iprint_callback.h"
namespace OHOS {
namespace Print {
using namespace std;
struct JobIdCmp {
bool operator()(const std::string a, const std::string b) const
{
return atoi(a.c_str()) > atoi(b.c_str());
int32_t numA = 0;
int32_t numB = 0;
if (PrintUtil::ConvertToInt(a, numA) && PrintUtil::ConvertToInt(b, numB)) {
return numA > numB;
} else {
// If invalid, By default, the number A is greater than the number B.
return true;
}
}
};

View File

@ -34,7 +34,7 @@ bool ParseAttributeToValue(ipp_t *response, const std::string &keyword, T &value
PRINT_HILOGW("attrPtr is null");
return false;
}
const char *attrString = ippGetString(attrPtr, 0, NULL);
const char *attrString = ippGetString(attrPtr, 0, nullptr);
if (attrString == nullptr) {
PRINT_HILOGW("attrString is null");
return false;
@ -63,7 +63,7 @@ bool ParseAttributesToList(ipp_t *response, const std::string &keyword, std::vec
int num = ippGetCount(attrPtr);
PRINT_HILOGD("number of values %{public}d", num);
for (int i = 0; i < num; i++) {
const char *attrString = ippGetString(attrPtr, i, NULL);
const char *attrString = ippGetString(attrPtr, i, nullptr);
if (attrString == nullptr) {
PRINT_HILOGW("attrString is null");
continue;
@ -104,7 +104,7 @@ std::string ConvertIppAttributesToJsonString(ipp_t *response, const std::string
}
nlohmann::json jsonArray = nlohmann::json::array();
for (int i = 0; i < ippGetCount(attrPtr); i++) {
const char *attrString = ippGetString(attrPtr, i, NULL);
const char *attrString = ippGetString(attrPtr, i, nullptr);
if (attrString == nullptr) {
continue;
}
@ -116,15 +116,15 @@ std::string ConvertIppAttributesToJsonString(ipp_t *response, const std::string
void SetCapabilityGroupAttribute(ipp_t *response, PrinterCapability &printerCaps)
{
ipp_attribute_t *attrPtr;
if ((attrPtr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != NULL) {
if ((attrPtr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM)) != nullptr) {
printerCaps.SetPrinterAttrNameAndValue("printer-state",
ippEnumString("printer-state", ippGetInteger(attrPtr, 0)));
}
if ((attrPtr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXTLANG)) != NULL) {
printerCaps.SetPrinterAttrNameAndValue("printer-info", ippGetString(attrPtr, 0, NULL));
if ((attrPtr = ippFindAttribute(response, "printer-info", IPP_TAG_TEXTLANG)) != nullptr) {
printerCaps.SetPrinterAttrNameAndValue("printer-info", ippGetString(attrPtr, 0, nullptr));
}
if ((attrPtr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != NULL) {
printerCaps.SetPrinterAttrNameAndValue("printer-location", ippGetString(attrPtr, 0, NULL));
if ((attrPtr = ippFindAttribute(response, "printer-location", IPP_TAG_TEXT)) != nullptr) {
printerCaps.SetPrinterAttrNameAndValue("printer-location", ippGetString(attrPtr, 0, nullptr));
}
}
@ -337,32 +337,32 @@ void ParseMediaColDefaultAttributes(ipp_t *response, PrinterCapability &printerC
}
attrPtr = ippFindAttribute(defaultMediaCol, "media-source", IPP_TAG_KEYWORD);
if (attrPtr != nullptr) {
PRINT_HILOGD("media-source-default found: %{public}s", ippGetString(attrPtr, 0, NULL));
printerCaps.SetPrinterAttrNameAndValue("media-source-default", ippGetString(attrPtr, 0, NULL));
PRINT_HILOGD("media-source-default found: %{public}s", ippGetString(attrPtr, 0, nullptr));
printerCaps.SetPrinterAttrNameAndValue("media-source-default", ippGetString(attrPtr, 0, nullptr));
}
attrPtr = ippFindAttribute(defaultMediaCol, "media-type", IPP_TAG_KEYWORD);
if (attrPtr != nullptr) {
PRINT_HILOGD("media-type-default found: %{public}s", ippGetString(attrPtr, 0, NULL));
printerCaps.SetPrinterAttrNameAndValue("media-type-default", ippGetString(attrPtr, 0, NULL));
PRINT_HILOGD("media-type-default found: %{public}s", ippGetString(attrPtr, 0, nullptr));
printerCaps.SetPrinterAttrNameAndValue("media-type-default", ippGetString(attrPtr, 0, nullptr));
}
}
void ParseMediaMarginAttributes(ipp_t *response, PrinterCapability &printerCaps)
{
ipp_attribute_t *attrPtr;
if ((attrPtr = ippFindAttribute(response, "media-bottom-margin-supported", IPP_TAG_INTEGER)) != NULL) {
if ((attrPtr = ippFindAttribute(response, "media-bottom-margin-supported", IPP_TAG_INTEGER)) != nullptr) {
printerCaps.SetPrinterAttrNameAndValue("media-bottom-margin-supported",
std::to_string(ippGetInteger(attrPtr, 0)).c_str());
}
if ((attrPtr = ippFindAttribute(response, "media-top-margin-supported", IPP_TAG_INTEGER)) != NULL) {
if ((attrPtr = ippFindAttribute(response, "media-top-margin-supported", IPP_TAG_INTEGER)) != nullptr) {
printerCaps.SetPrinterAttrNameAndValue("media-top-margin-supported",
std::to_string(ippGetInteger(attrPtr, 0)).c_str());
}
if ((attrPtr = ippFindAttribute(response, "media-left-margin-supported", IPP_TAG_INTEGER)) != NULL) {
if ((attrPtr = ippFindAttribute(response, "media-left-margin-supported", IPP_TAG_INTEGER)) != nullptr) {
printerCaps.SetPrinterAttrNameAndValue("media-left-margin-supported",
std::to_string(ippGetInteger(attrPtr, 0)).c_str());
}
if ((attrPtr = ippFindAttribute(response, "media-right-margin-supported", IPP_TAG_INTEGER)) != NULL) {
if ((attrPtr = ippFindAttribute(response, "media-right-margin-supported", IPP_TAG_INTEGER)) != nullptr) {
printerCaps.SetPrinterAttrNameAndValue("media-right-margin-supported",
std::to_string(ippGetInteger(attrPtr, 0)).c_str());
}
@ -372,14 +372,14 @@ void ParseOrientationAttributes(ipp_t *response, PrinterCapability &printerCaps)
{
std::string keyword = "orientation-requested-default";
ipp_attribute_t *attrPtr = ippFindAttribute(response, keyword.c_str(), IPP_TAG_ENUM);
if (attrPtr != NULL) {
if (attrPtr != nullptr) {
int orientationEnum = ippGetInteger(attrPtr, 0);
printerCaps.SetPrinterAttrNameAndValue(keyword.c_str(), std::to_string(orientationEnum).c_str());
PRINT_HILOGD("orientation-default found: %{public}d", orientationEnum);
}
keyword = "orientation-requested-supported";
attrPtr = ippFindAttribute(response, keyword.c_str(), IPP_TAG_ENUM);
if (attrPtr != NULL) {
if (attrPtr != nullptr) {
int num = ippGetCount(attrPtr);
if (num > 0) {
nlohmann::json supportedOrientationArray = nlohmann::json::array();
@ -418,14 +418,14 @@ void SetOptionAttribute(ipp_t *response, PrinterCapability &printerCaps)
{
ipp_attribute_t *attrPtr;
nlohmann::json options;
if ((attrPtr = ippFindAttribute(response, "printer-make-and-model", IPP_TAG_TEXT)) != NULL) {
options["make"] = ippGetString(attrPtr, 0, NULL);
if ((attrPtr = ippFindAttribute(response, "printer-make-and-model", IPP_TAG_TEXT)) != nullptr) {
options["make"] = ippGetString(attrPtr, 0, nullptr);
}
if ((attrPtr = ippFindAttribute(response, "printer-uuid", IPP_TAG_URI)) != NULL) {
options["uuid"] = ippGetString(attrPtr, 0, NULL);
if ((attrPtr = ippFindAttribute(response, "printer-uuid", IPP_TAG_URI)) != nullptr) {
options["uuid"] = ippGetString(attrPtr, 0, nullptr);
}
if ((attrPtr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != NULL) {
options["printerName"] = ippGetString(attrPtr, 0, NULL);
if ((attrPtr = ippFindAttribute(response, "printer-name", IPP_TAG_NAME)) != nullptr) {
options["printerName"] = ippGetString(attrPtr, 0, nullptr);
}
std::string keyword = "media-type-supported";
std::string supportTypes;
@ -436,7 +436,7 @@ void SetOptionAttribute(ipp_t *response, PrinterCapability &printerCaps)
} else {
nlohmann::json jsonArray = nlohmann::json::array();
for (int i = 0; i < ippGetCount(attrPtr); i++) {
const char *attrString = ippGetString(attrPtr, i, NULL);
const char *attrString = ippGetString(attrPtr, i, nullptr);
if (attrString == nullptr) {
continue;
}
@ -479,7 +479,7 @@ void ParsePrinterAttributes(ipp_t *response, PrinterCapability &printerCaps)
bool ParsePrinterStatusAttributes(ipp_t *response, PrinterStatus &status)
{
ipp_attribute_t *attrPtr = ippFindAttribute(response, "printer-state", IPP_TAG_ENUM);
if (attrPtr != NULL) {
if (attrPtr != nullptr) {
int enumValue = ippGetInteger(attrPtr, 0) - IPP_PSTATE_IDLE;
if (enumValue >= PRINTER_STATUS_IDLE && enumValue <= PRINTER_STATUS_UNAVAILABLE) {
status = static_cast<PrinterStatus>(enumValue);

View File

@ -426,7 +426,7 @@ void PrintCupsClient::QueryPPDInformation(const char *makeModel, std::vector<std
return;
}
if (makeModel) {
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-make-and-model", NULL, makeModel);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, "ppd-make-and-model", nullptr, makeModel);
}
PRINT_HILOGD("CUPS_GET_PPDS start.");
@ -506,17 +506,17 @@ int32_t PrintCupsClient::AddPrinterToCups(const std::string &printerUri, const s
return E_PRINT_SERVER_FAILURE;
}
request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s",
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", nullptr, "localhost", 0, "/printers/%s",
standardName.c_str());
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", NULL, standardName.c_str());
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL, printerUri.c_str());
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", nullptr, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", nullptr, cupsUser());
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", nullptr, standardName.c_str());
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", nullptr, printerUri.c_str());
ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", IPP_PRINTER_IDLE);
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "ppd-name", NULL, ppd.c_str());
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_NAME, "ppd-name", nullptr, ppd.c_str());
ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
PRINT_HILOGD("IPP_OP_CUPS_ADD_MODIFY_PRINTER cupsDoRequest");
ippDelete(printAbility_->DoRequest(NULL, request, "/admin/"));
ippDelete(printAbility_->DoRequest(nullptr, request, "/admin/"));
if (cupsLastError() > IPP_STATUS_OK_EVENTS_COMPLETE) {
PRINT_HILOGE("add error: %s", cupsLastErrorString());
return E_PRINT_SERVER_FAILURE;
@ -538,18 +538,18 @@ int32_t PrintCupsClient::AddPrinterToCupsWithPpd(const std::string &printerUri,
return E_PRINT_NONE;
}
ippSetPort(CUPS_SEVER_PORT);
_cupsSetError(IPP_STATUS_OK, NULL, 0);
_cupsSetError(IPP_STATUS_OK, nullptr, 0);
request = ippNewRequest(IPP_OP_CUPS_ADD_MODIFY_PRINTER);
if (request == nullptr) {
PRINT_HILOGW("request is null");
return E_PRINT_SERVER_FAILURE;
}
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s",
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", nullptr, "localhost", 0, "/printers/%s",
standardName.c_str());
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", NULL, standardName.c_str());
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", NULL, printerUri.c_str());
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", nullptr, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", nullptr, cupsUser());
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_TEXT, "printer-info", nullptr, standardName.c_str());
ippAddString(request, IPP_TAG_PRINTER, IPP_TAG_URI, "device-uri", nullptr, printerUri.c_str());
ippAddInteger(request, IPP_TAG_PRINTER, IPP_TAG_ENUM, "printer-state", IPP_PRINTER_IDLE);
ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-accepting-jobs", 1);
ippAddBoolean(request, IPP_TAG_PRINTER, "printer-is-shared", 1);
@ -589,10 +589,11 @@ int32_t PrintCupsClient::DeleteCupsPrinter(const char *printerName)
return E_PRINT_SERVER_FAILURE;
}
request = ippNewRequest(IPP_OP_CUPS_DELETE_PRINTER);
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s", printerName);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
ippDelete(printAbility_->DoRequest(NULL, request, "/admin/"));
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri),
"ipp", nullptr, "localhost", 0, "/printers/%s", printerName);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", nullptr, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", nullptr, cupsUser());
ippDelete(printAbility_->DoRequest(nullptr, request, "/admin/"));
if (cupsLastError() > IPP_STATUS_OK_EVENTS_COMPLETE) {
PRINT_HILOGW("DeleteCupsPrinter error: %{public}s", cupsLastErrorString());
}
@ -618,19 +619,20 @@ ipp_t *PrintCupsClient::QueryPrinterAttributesByUri(const std::string &printerUr
httpSeparateURI(HTTP_URI_CODING_ALL, printerUri.c_str(), scheme, sizeof(scheme), username, sizeof(username), host,
sizeof(host), &port, resource, sizeof(resource));
if (nic.empty()) {
http = httpConnect2(host, port, NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, TIME_OUT, NULL);
http = httpConnect2(host, port, nullptr, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, TIME_OUT, nullptr);
} else {
http = httpConnect3(host, port, NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, TIME_OUT, NULL, nic.c_str());
http = httpConnect3(host, port, nullptr, AF_UNSPEC,
HTTP_ENCRYPTION_IF_REQUESTED, 1, TIME_OUT, nullptr, nic.c_str());
}
if (http == nullptr) {
PRINT_HILOGW("connect printer failed");
return nullptr;
}
_cupsSetError(IPP_STATUS_OK, NULL, 0);
_cupsSetError(IPP_STATUS_OK, nullptr, 0);
request = ippNewRequest(IPP_OP_GET_PRINTER_ATTRIBUTES);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, printerUri.c_str());
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", num, NULL, pattrs);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", nullptr, printerUri.c_str());
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", nullptr, cupsUser());
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", num, nullptr, pattrs);
response = printAbility_->DoRequest(http, request, "/");
httpClose(http);
http = nullptr;
@ -700,7 +702,7 @@ int32_t PrintCupsClient::QueryPrinterCapabilityFromPPD(const std::string &printe
PRINT_HILOGW("printAbility_ is null");
return E_PRINT_SERVER_FAILURE;
}
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, standardName.c_str(), NULL);
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, standardName.c_str(), nullptr);
if (dest == nullptr) {
PRINT_HILOGE("the printer is not found");
return E_PRINT_SERVER_FAILURE;
@ -895,20 +897,21 @@ int32_t PrintCupsClient::SetDefaultPrinter(const std::string &printerName)
return E_PRINT_SERVER_FAILURE;
}
ippSetPort(CUPS_SEVER_PORT);
http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, LONG_TIME_OUT, NULL);
http = httpConnect2(cupsServer(), ippPort(), nullptr,
AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, LONG_TIME_OUT, nullptr);
if (http == nullptr) {
PRINT_HILOGE("cups server is not alive");
return E_PRINT_SERVER_FAILURE;
}
ipp_t *request; /* IPP Request */
char uri[HTTP_MAX_URI] = {0}; /* URI for printer/class */
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", nullptr,
"localhost", 0, "/printers/%s", printerName.c_str());
request = ippNewRequest(IPP_OP_CUPS_SET_DEFAULT);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
"printer-uri", nullptr, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
nullptr, cupsUser());
ippDelete(printAbility_->DoRequest(http, request, "/admin/"));
httpClose(http);
@ -968,7 +971,7 @@ int32_t PrintCupsClient::QueryPrinterAttrList(const std::string &printerName, co
return E_PRINT_SERVER_FAILURE;
}
cups_dest_t *dest = nullptr;
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, printerName.c_str(), NULL);
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, printerName.c_str(), nullptr);
if (dest == nullptr) {
PRINT_HILOGW("the printer is not found");
return E_PRINT_SERVER_FAILURE;
@ -994,7 +997,7 @@ int32_t PrintCupsClient::QueryPrinterInfoByPrinterId(const std::string& printerI
return E_PRINT_SERVER_FAILURE;
}
cups_dest_t *dest = nullptr;
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, info.GetPrinterName().c_str(), NULL);
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, info.GetPrinterName().c_str(), nullptr);
if (dest == nullptr) {
PRINT_HILOGW("the printer is not found");
return E_PRINT_SERVER_FAILURE;
@ -1049,7 +1052,7 @@ bool PrintCupsClient::CheckPrinterMakeModel(JobParameters *jobParams)
}
const uint32_t GET_OPTION_TIMES = 40;
while (retryCount < GET_OPTION_TIMES) {
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, jobParams->printerName.c_str(), NULL);
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, jobParams->printerName.c_str(), nullptr);
if (dest != nullptr) {
const char *makeModel = cupsGetOption("printer-make-and-model", dest->num_options, dest->options);
PRINT_HILOGD("makeModel=%{private}s", makeModel);
@ -1227,7 +1230,8 @@ void PrintCupsClient::MonitorJobState(JobMonitorParam *param, CallbackFunc callb
http_t *http = nullptr;
uint32_t fail_connect_times = 0;
ippSetPort(CUPS_SEVER_PORT);
http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, LONG_TIME_OUT, NULL);
http = httpConnect2(cupsServer(), ippPort(), nullptr, AF_UNSPEC,
HTTP_ENCRYPTION_IF_REQUESTED, 1, LONG_TIME_OUT, nullptr);
if (http == nullptr) {
return;
}
@ -1251,8 +1255,8 @@ void PrintCupsClient::MonitorJobState(JobMonitorParam *param, CallbackFunc callb
break;
}
if (httpGetFd(http) < 0) {
PRINT_HILOGE("http is NULL");
httpReconnect2(http, LONG_LONG_TIME_OUT, NULL);
PRINT_HILOGE("http is nullptr");
httpReconnect2(http, LONG_LONG_TIME_OUT, nullptr);
}
if (httpGetFd(http) < 0 || !CheckPrinterOnline(param)) {
PRINT_HILOGE("unable connect to printer, retry: %{public}d", fail_connect_times);
@ -1311,7 +1315,7 @@ void PrintCupsClient::JobStatusCallback(JobMonitorParam *param, JobStatus *jobSt
QueryPrinterStatusByUri(param->printerUri, printerStatus) == E_PRINT_NONE;
PRINT_HILOGD("is printer status available: %{public}d", isPrinterStatusAvailable);
if ((isPrinterStatusAvailable && printerStatus == PRINTER_STATUS_UNAVAILABLE) ||
(strstr(jobStatus->printer_state_reasons, PRINTER_STATE_ERROR.c_str()) != NULL)) {
(strstr(jobStatus->printer_state_reasons, PRINTER_STATE_ERROR.c_str()) != nullptr)) {
ReportBlockedReason(param, jobStatus);
} else {
param->serviceAbility->UpdatePrintJobState(param->serviceJobId, PRINT_JOB_RUNNING,
@ -1335,11 +1339,11 @@ void PrintCupsClient::ReportBlockedReason(JobMonitorParam *param, JobStatus *job
PRINT_HILOGE("ReportBlockedReason parameter is nullptr");
return;
}
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_OFFLINE.c_str()) != NULL) {
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_OFFLINE.c_str()) != nullptr) {
param->serviceAbility->UpdatePrintJobState(param->serviceJobId, PRINT_JOB_BLOCKED, PRINT_JOB_BLOCKED_OFFLINE);
return;
}
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_PAUSED.c_str()) != NULL) {
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_PAUSED.c_str()) != nullptr) {
param->serviceAbility->UpdatePrintJobState(
param->serviceJobId, PRINT_JOB_BLOCKED, PRINT_JOB_BLOCKED_SERVICE_REQUEST);
return;
@ -1362,29 +1366,29 @@ uint32_t PrintCupsClient::GetBlockedSubstate(JobStatus *jobStatus)
return PRINT_JOB_COMPLETED_FAILED;
}
uint32_t substate = PRINT_JOB_COMPLETED_SUCCESS;
if ((strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MEDIA_EMPTY.c_str()) != NULL) ||
(strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MEDIA_NEEDED.c_str()) != NULL)) {
if ((strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MEDIA_EMPTY.c_str()) != nullptr) ||
(strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MEDIA_NEEDED.c_str()) != nullptr)) {
substate = substate * NUMBER_FOR_SPLICING_SUBSTATE + PRINT_JOB_BLOCKED_OUT_OF_PAPER;
}
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MEDIA_JAM.c_str()) != NULL) {
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MEDIA_JAM.c_str()) != nullptr) {
substate = substate * NUMBER_FOR_SPLICING_SUBSTATE + PRINT_JOB_BLOCKED_JAMMED;
}
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_TONER_EMPTY.c_str()) != NULL) {
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_TONER_EMPTY.c_str()) != nullptr) {
substate = substate * NUMBER_FOR_SPLICING_SUBSTATE + PRINT_JOB_BLOCKED_OUT_OF_TONER;
} else if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_TONER_LOW.c_str()) != NULL) {
} else if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_TONER_LOW.c_str()) != nullptr) {
substate = substate * NUMBER_FOR_SPLICING_SUBSTATE + PRINT_JOB_BLOCKED_LOW_ON_TONER;
}
if ((strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MARKER_EMPTY.c_str()) != NULL) ||
(strstr(jobStatus->printer_state_reasons, PRINTER_STATE_INK_EMPTY.c_str()) != NULL)) {
if ((strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MARKER_EMPTY.c_str()) != nullptr) ||
(strstr(jobStatus->printer_state_reasons, PRINTER_STATE_INK_EMPTY.c_str()) != nullptr)) {
substate = substate * NUMBER_FOR_SPLICING_SUBSTATE + PRINT_JOB_BLOCKED_OUT_OF_INK;
} else if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MARKER_LOW.c_str()) != NULL) {
} else if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_MARKER_LOW.c_str()) != nullptr) {
substate = substate * NUMBER_FOR_SPLICING_SUBSTATE + PRINT_JOB_BLOCKED_LOW_ON_INK;
}
if ((strstr(jobStatus->printer_state_reasons, PRINTER_STATE_DOOR_EMPTY.c_str()) != NULL) ||
(strstr(jobStatus->printer_state_reasons, PRINTER_STATE_COVER_OPEN.c_str()) != NULL)) {
if ((strstr(jobStatus->printer_state_reasons, PRINTER_STATE_DOOR_EMPTY.c_str()) != nullptr) ||
(strstr(jobStatus->printer_state_reasons, PRINTER_STATE_COVER_OPEN.c_str()) != nullptr)) {
substate = substate * NUMBER_FOR_SPLICING_SUBSTATE + PRINT_JOB_BLOCKED_DOOR_OPEN;
}
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_OTHER.c_str()) != NULL) {
if (strstr(jobStatus->printer_state_reasons, PRINTER_STATE_OTHER.c_str()) != nullptr) {
substate = substate * NUMBER_FOR_SPLICING_SUBSTATE + PRINT_JOB_BLOCKED_UNKNOWN;
}
return substate;
@ -1392,9 +1396,9 @@ uint32_t PrintCupsClient::GetBlockedSubstate(JobStatus *jobStatus)
void PrintCupsClient::QueryJobState(http_t *http, JobMonitorParam *param, JobStatus *jobStatus)
{
ipp_t *request; /* IPP request */
ipp_t *response; /* IPP response */
ipp_attribute_t *attr; /* Attribute in response */
ipp_t *request = nullptr; /* IPP request */
ipp_t *response = nullptr; /* IPP response */
ipp_attribute_t *attr = nullptr; /* Attribute in response */
int jattrsLen = 3;
static const char * const jattrs[] = {
"job-state",
@ -1405,18 +1409,27 @@ void PrintCupsClient::QueryJobState(http_t *http, JobMonitorParam *param, JobSta
PRINT_HILOGW("printAbility_ is null");
return;
}
if (param == nullptr || jobStatus == nullptr) {
if (http == nullptr || param == nullptr || jobStatus == nullptr) {
PRINT_HILOGE("QueryJobState param is null");
return;
}
if (param->cupsJobId > 0) {
request = ippNewRequest(IPP_OP_GET_JOB_ATTRIBUTES);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, param->printerUri.c_str());
if (request == nullptr) {
PRINT_HILOGE("Failed to create IPP request.");
return;
}
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", nullptr, param->printerUri.c_str());
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", param->cupsJobId);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, DEFAULT_USER.c_str());
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", jattrsLen, NULL, jattrs);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", nullptr, DEFAULT_USER.c_str());
ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", jattrsLen, nullptr, jattrs);
PRINT_HILOGD("get job state from cups service: start");
response = printAbility_->DoRequest(http, request, "/");
if (response == nullptr) {
PRINT_HILOGE("Failed to get response from CUPS service.");
ippDelete(request);
return;
}
if ((attr = ippFindAttribute(response, "job-state", IPP_TAG_ENUM)) != nullptr) {
jobStatus->job_state = (ipp_jstate_t)ippGetInteger(attr, 0);
}
@ -1466,10 +1479,10 @@ bool PrintCupsClient::CheckPrinterOnline(JobMonitorParam *param, const uint32_t
}
std::string nic;
if (IsIpConflict(printerId, nic)) {
http = httpConnect3(host, port, NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, timeout, NULL,
http = httpConnect3(host, port, nullptr, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, timeout, nullptr,
nic.c_str());
} else {
http = httpConnect2(host, port, NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, timeout, NULL);
http = httpConnect2(host, port, nullptr, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, timeout, nullptr);
}
if (http == nullptr) {
PRINT_HILOGE("httpConnect2 printer failed");
@ -1493,6 +1506,10 @@ void PrintCupsClient::CancelCupsJob(std::string serviceJobId)
PRINT_HILOGI("jobIndex: %{public}d", jobIndex);
if (jobIndex >= 0) {
PRINT_HILOGI("job in queue, delete");
JobParameters* erasedJob = jobQueue_.at(jobIndex);
if (erasedJob != nullptr) {
delete erasedJob;
}
jobQueue_.erase(jobQueue_.begin() + jobIndex);
PrintServiceAbility::GetInstance()->UpdatePrintJobState(serviceJobId, PRINT_JOB_COMPLETED,
PRINT_JOB_COMPLETED_CANCELLED);
@ -1647,7 +1664,8 @@ bool PrintCupsClient::IsCupsServerAlive()
{
http_t *http;
ippSetPort(CUPS_SEVER_PORT);
http = httpConnect2(cupsServer(), ippPort(), NULL, AF_UNSPEC, HTTP_ENCRYPTION_IF_REQUESTED, 1, LONG_TIME_OUT, NULL);
http = httpConnect2(cupsServer(), ippPort(), nullptr, AF_UNSPEC,
HTTP_ENCRYPTION_IF_REQUESTED, 1, LONG_TIME_OUT, nullptr);
if (http == nullptr) {
PRINT_HILOGE("cups server is not alive");
return false;
@ -1671,7 +1689,7 @@ bool PrintCupsClient::IsPrinterExist(const char *printerUri, const char *printer
PRINT_HILOGW("printAbility_ is null");
return printerExist;
}
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, printerName, NULL);
dest = printAbility_->GetNamedDest(CUPS_HTTP_DEFAULT, printerName, nullptr);
if (dest != nullptr) {
const char *deviceUri = cupsGetOption("device-uri", dest->num_options, dest->options);
if (deviceUri == nullptr) {
@ -1694,11 +1712,11 @@ bool PrintCupsClient::IsPrinterExist(const char *printerUri, const char *printer
}
if (strcmp(ppdName, DEFAULT_PPD_NAME.c_str()) == 0) {
// 查到everywhere或remote printer驱动
printerExist = (strstr(makeModel, DEFAULT_MAKE_MODEL.c_str()) != NULL) ||
(strstr(makeModel, REMOTE_PRINTER_MAKE_MODEL.c_str()) != NULL);
printerExist = (strstr(makeModel, DEFAULT_MAKE_MODEL.c_str()) != nullptr) ||
(strstr(makeModel, REMOTE_PRINTER_MAKE_MODEL.c_str()) != nullptr);
} else {
// 查到驱动
printerExist = !(strstr(makeModel, DEFAULT_MAKE_MODEL.c_str()) != NULL);
printerExist = !(strstr(makeModel, DEFAULT_MAKE_MODEL.c_str()) != nullptr);
if (!printerExist) {
// 私有驱动已卸载,需要先删除打印机再添加,不然下发任务找不到驱动
DeleteCupsPrinter(printerName);
@ -1786,13 +1804,13 @@ int32_t PrintCupsClient::ResumePrinter(const std::string &printerName)
}
ipp_t *request = nullptr;
char uri[HTTP_MAX_URI] = {0};
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/printers/%s",
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", nullptr, "localhost", 0, "/printers/%s",
printerName.c_str());
request = ippNewRequest(IPP_OP_RESUME_PRINTER);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", NULL, cupsUser());
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", nullptr, uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name", nullptr, cupsUser());
PRINT_HILOGD("IPP_OP_RESUME_PRINTER cupsDoRequest");
ippDelete(printAbility_->DoRequest(NULL, request, "/admin/"));
ippDelete(printAbility_->DoRequest(nullptr, request, "/admin/"));
if (cupsLastError() > IPP_STATUS_OK_EVENTS_COMPLETE) {
PRINT_HILOGE("resume printer error: %s", cupsLastErrorString());
return E_PRINT_SERVER_FAILURE;

View File

@ -18,6 +18,7 @@
#include <cmath>
#include "print_log.h"
#include "print_ipp_over_usb_util.h"
#include "print_util.h"
#include "usb_errors.h"
namespace OHOS::Print {
@ -106,7 +107,12 @@ void PrintHttpRequestProcess::GetContentLength(const std::vector<uint8_t> &readT
lenStr += readTempBuffer[lenIndex];
lenIndex++;
}
contentLength = static_cast<size_t>(std::stoi(lenStr));
int32_t contentLengthTmp = 0;
if (!PrintUtil::ConvertToInt(lenStr, contentLengthTmp)) {
PRINT_HILOGE("lenStr [%{public}s] can not parse to number.", lenStr.c_str());
return;
}
contentLength = static_cast<size_t>(contentLengthTmp);
PRINT_HILOGD("contentLength = %{public}s, %{public}lu", lenStr.c_str(), contentLength);
}
}

View File

@ -154,7 +154,7 @@ void PrintHttpServerManager::DealUsbDevDetach(const std::string &devStr)
return;
}
cJSON *jsonTemp = cJSON_GetObjectItem(devJson, "name");
if (jsonTemp == nullptr || jsonTemp->valuestring == NULL) {
if (jsonTemp == nullptr || jsonTemp->valuestring == nullptr) {
PRINT_HILOGE("The devJson does not have a necessary attribute.");
cJSON_Delete(devJson);
return;

View File

@ -35,7 +35,7 @@ PrintSecurityGuardInfo::PrintSecurityGuardInfo(const std::string callPkg, const
#endif
}
void PrintSecurityGuardInfo::setPrintTypeInfo(const PrinterInfo &printerInfo, const PrintJob &printJob)
void PrintSecurityGuardInfo::SetPrintTypeInfo(const PrinterInfo &printerInfo, const PrintJob &printJob)
{
std::string printerId = printerInfo.GetPrinterId();
std::string description = printerInfo.GetDescription();
@ -78,7 +78,7 @@ void PrintSecurityGuardInfo::setPrintTypeInfo(const PrinterInfo &printerInfo, co
outcome_ = "failed";
break;
default:
PRINT_HILOGD("PrintSecurityGuardInfo setPrintTypeInfo unknown subState:%{public}d", subState);
PRINT_HILOGD("PrintSecurityGuardInfo SetPrintTypeInfo unknown subState:%{public}d", subState);
break;
}
}

View File

@ -36,13 +36,13 @@ void PrintSecurityGuardManager::receiveJobStateUpdate(const std::string jobId, c
if (it != securityMap_.end() && it->second != nullptr) {
PRINT_HILOGI("find PrintSecurityGuardInfo");
auto securityGuard = it->second;
securityGuard->setPrintTypeInfo(printerInfo, printJob);
securityGuard->SetPrintTypeInfo(printerInfo, printJob);
securityInfo = securityGuard->ToJsonStr();
} else {
PRINT_HILOGI("find PrintSecurityGuardInfo empty");
std::vector<std::string> fileList;
auto securityGuard = std::make_shared<PrintSecurityGuardInfo>("", fileList);
securityGuard->setPrintTypeInfo(printerInfo, printJob);
securityGuard->SetPrintTypeInfo(printerInfo, printJob);
securityInfo = securityGuard->ToJsonStr();
}
ReportSecurityInfo(EVENT_ID, VERSION, securityInfo);

View File

@ -780,7 +780,11 @@ bool PrintSystemData::ParseUserListJsonV1(nlohmann::json &jsonObject, std::vecto
if (userIdStr.empty()) {
continue;
}
int32_t userId = std::stoi(userIdStr);
int32_t userId = 0;
if (!PrintUtil::ConvertToInt(userIdStr, userId)) {
PRINT_HILOGE("userIdStr [%{public}s] can not parse to number.", userIdStr.c_str());
return false;
}
PRINT_HILOGI("ParseUserListJsonV1 userId: %{public}d", userId);
allPrintUserList.push_back(userId);
}

View File

@ -382,8 +382,17 @@ bool ScanServiceAbility::GetUsbDevicePort(const std::string &deviceId, std::stri
if (std::regex_match(deviceId, match, pattern)) {
constexpr size_t STRING_POS_THREE = 3;
constexpr size_t STRING_POS_FOUR = 4;
firstId = std::to_string(std::stoi(match[STRING_POS_THREE]));
secondId = std::to_string(std::stoi(match[STRING_POS_FOUR]));
std::string firstIdTmp = match[STRING_POS_THREE].str();
std::string secondIdTmp = match[STRING_POS_FOUR].str();
int32_t firstNumTmp = 0;
int32_t secondNumTmp = 0;
if (!ScanUtil::ConvertToInt(firstIdTmp, firstNumTmp) ||
!ScanUtil::ConvertToInt(secondIdTmp, secondNumTmp)) {
SCAN_HILOGE("parse [%{public}s]:[%{public}s] fail", firstIdTmp.c_str(), secondIdTmp.c_str());
return false;
}
firstId = std::to_string(firstNumTmp);
secondId = std::to_string(secondNumTmp);
return true;
} else {
SCAN_HILOGE("In USB mode, the deviceId string format does not match");

View File

@ -44,7 +44,7 @@ HWTEST_F(PrintSecurityGuardInfoTest, PrintSecurityGuardInfoTest_0001, TestSize.L
PrintSecurityGuardInfo printSecurityGuardInfo("callPkg", fileList);
PrinterInfo printerInfo;
PrintJob printJob;
printSecurityGuardInfo.setPrintTypeInfo(printerInfo, printJob);
printSecurityGuardInfo.SetPrintTypeInfo(printerInfo, printJob);
EXPECT_NE("", printSecurityGuardInfo.ToJsonStr());
}
@ -55,7 +55,7 @@ HWTEST_F(PrintSecurityGuardInfoTest, PrintSecurityGuardInfoTest_0002, TestSize.L
PrinterInfo printerInfo;
printerInfo.SetPrinterId("EPRINT_printer");
PrintJob printJob;
printSecurityGuardInfo.setPrintTypeInfo(printerInfo, printJob);
printSecurityGuardInfo.SetPrintTypeInfo(printerInfo, printJob);
EXPECT_NE("", printSecurityGuardInfo.ToJsonStr());
}
@ -69,7 +69,7 @@ HWTEST_F(PrintSecurityGuardInfoTest, PrintSecurityGuardInfoTest_0003, TestSize.L
printerInfo.SetOption(option);
PrintJob printJob;
printJob.SetSubState(PRINT_JOB_COMPLETED_SUCCESS);
printSecurityGuardInfo.setPrintTypeInfo(printerInfo, printJob);
printSecurityGuardInfo.SetPrintTypeInfo(printerInfo, printJob);
EXPECT_NE("", printSecurityGuardInfo.ToJsonStr());
}
@ -83,7 +83,7 @@ HWTEST_F(PrintSecurityGuardInfoTest, PrintSecurityGuardInfoTest_0004, TestSize.L
printerInfo.SetOption(option);
PrintJob printJob;
printJob.SetSubState(PRINT_JOB_COMPLETED_CANCELLED);
printSecurityGuardInfo.setPrintTypeInfo(printerInfo, printJob);
printSecurityGuardInfo.SetPrintTypeInfo(printerInfo, printJob);
EXPECT_NE("", printSecurityGuardInfo.ToJsonStr());
}
@ -97,7 +97,7 @@ HWTEST_F(PrintSecurityGuardInfoTest, PrintSecurityGuardInfoTest_0005, TestSize.L
printerInfo.SetOption(option);
PrintJob printJob;
printJob.SetSubState(PRINT_JOB_COMPLETED_FAILED);
printSecurityGuardInfo.setPrintTypeInfo(printerInfo, printJob);
printSecurityGuardInfo.SetPrintTypeInfo(printerInfo, printJob);
EXPECT_NE("", printSecurityGuardInfo.ToJsonStr());
}
@ -111,7 +111,7 @@ HWTEST_F(PrintSecurityGuardInfoTest, PrintSecurityGuardInfoTest_0006, TestSize.L
printerInfo.SetOption(option);
PrintJob printJob;
printJob.SetSubState(PRINT_JOB_COMPLETED_FILE_CORRUPT);
printSecurityGuardInfo.setPrintTypeInfo(printerInfo, printJob);
printSecurityGuardInfo.SetPrintTypeInfo(printerInfo, printJob);
EXPECT_NE("", printSecurityGuardInfo.ToJsonStr());
}
} // namespace Print

View File

@ -17,6 +17,7 @@
#define PRINT_UTIL_H
#include <algorithm>
#include <charconv>
#include <iostream>
#include <sstream>
#include <list>
@ -48,6 +49,8 @@ public:
static void Str2VecStr(std::string& str, std::vector<std::string>& vec);
static bool startsWith(const std::string& str, const std::string& prefix);
static bool ConvertToInt(const std::string& str, int32_t& value);
};
inline std::vector<uint32_t> PrintUtil::Str2Vec(std::string str)
@ -61,7 +64,11 @@ inline std::vector<uint32_t> PrintUtil::Str2Vec(std::string str)
std::istringstream is(str);
std::string temp;
while (getline(is, temp, ',')) {
vec.push_back(stoi(temp));
int32_t tempNum = 0;
if (!ConvertToInt(temp, tempNum)) {
return {};
}
vec.push_back(tempNum);
}
return vec;
}
@ -157,6 +164,13 @@ inline bool PrintUtil::startsWith(const std::string& str, const std::string& pre
}
return str.compare(0, prefix.length(), prefix) == 0;
}
inline bool PrintUtil::ConvertToInt(const std::string& str, int32_t& value)
{
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value);
return ec == std::errc{} && ptr == str.data() + str.size();
}
} // namespace OHOS::Print
#endif // PRINT_UTIL_H

View File

@ -17,6 +17,7 @@
#define SCAN_UTIL_H
#include <algorithm>
#include <charconv>
#include <iostream>
#include <sstream>
#include <list>
@ -31,6 +32,7 @@ public:
#ifdef SANE_ENABLE
static ScanErrorCode ConvertErro(const SANE_Status status);
#endif
static bool ConvertToInt(const std::string& str, int32_t& value);
};
#ifdef SANE_ENABLE
inline ScanErrorCode ScanUtil::ConvertErro(const SANE_Status status)
@ -38,6 +40,11 @@ inline ScanErrorCode ScanUtil::ConvertErro(const SANE_Status status)
return static_cast<ScanErrorCode> (status + E_SCAN_GOOD);
}
#endif
inline bool ScanUtil::ConvertToInt(const std::string& str, int32_t& value)
{
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), value);
return ec == std::errc{} && ptr == str.data() + str.size();
}
} // namespace OHOS::Scan
#endif // SCAN_UTIL_H