From f15097ebb99d479410aefb55592a9d340f9949d0 Mon Sep 17 00:00:00 2001 From: fangyunzhong Date: Tue, 23 Apr 2024 09:32:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E7=A0=81=E5=91=8A=E8=AD=A6=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fangyunzhong --- include/cmd_parser.h | 7 ++-- include/id_worker.h | 4 +-- include/resource_table.h | 8 ++--- src/cmd_parser.cpp | 68 +++++++++++++++++++++++---------------- src/id_defined_parser.cpp | 2 +- src/id_worker.cpp | 37 +++------------------ src/resource_pack.cpp | 5 ++- src/resource_table.cpp | 16 ++++----- 8 files changed, 66 insertions(+), 81 deletions(-) diff --git a/include/cmd_parser.h b/include/cmd_parser.h index b6b458e..0a12bb1 100644 --- a/include/cmd_parser.h +++ b/include/cmd_parser.h @@ -45,7 +45,7 @@ public: const std::vector &GetModuleNames() const; const std::string &GetConfig() const; const std::string &GetRestoolPath() const; - int32_t GetStartId() const; + uint32_t GetStartId() const; bool IsFileList() const; const std::vector &GetAppend() const; bool GetCombine() const; @@ -60,6 +60,7 @@ public: private: void InitCommand(); uint32_t ParseCommand(int argc, char *argv[]); + uint32_t CheckError(int argc, char *argv[], int c, int optIndex); uint32_t AddInput(const std::string& argValue); uint32_t AddPackageName(const std::string& argValue); uint32_t AddOutput(const std::string& argValue); @@ -81,7 +82,7 @@ private: uint32_t SetIdDefinedInputPath(const std::string& argValue); uint32_t AddSysIdDefined(const std::string& argValue); bool IsAscii(const std::string& argValue) const; - bool IsLongOpt(char *argv[]) const; + bool IsLongOpt(int argc, char *argv[]) const; uint32_t IconCheck(); uint32_t ParseTargetConfig(const std::string& argValue); @@ -97,7 +98,7 @@ private: std::vector moduleNames_; std::string configPath_; std::string restoolPath_; - int32_t startId_ = 0; + uint32_t startId_ = 0; bool isFileList_ = false; std::vector append_; bool combine_ = false; diff --git a/include/id_worker.h b/include/id_worker.h index 7e52329..d080ac0 100644 --- a/include/id_worker.h +++ b/include/id_worker.h @@ -32,15 +32,13 @@ public: std::vector GetHeaderId() const; int64_t GetId(ResType resType, const std::string &name) const; int64_t GetSystemId(ResType resType, const std::string &name) const; - bool PushCache(ResType resType, const std::string &name, int64_t id); - void PushDelId(int64_t id); private: int64_t GenerateAppId(ResType resType, const std::string &name); int64_t GenerateSysId(ResType resType, const std::string &name); uint64_t GetMaxId(uint64_t startId) const; int64_t GetCurId(); - int64_t appId_; + uint64_t appId_; uint64_t maxId_; ResourceIdCluster type_; std::map, int64_t> ids_; diff --git a/include/resource_table.h b/include/resource_table.h index 4930e15..fe598ac 100644 --- a/include/resource_table.h +++ b/include/resource_table.h @@ -74,12 +74,12 @@ private: void SaveLimitKeyConfigs(const std::map &limitKeyConfigs, std::ostringstream &out) const; void SaveIdSets(const std::map &idSets, std::ostringstream &out) const; - bool ReadFileHeader(std::ifstream &in, IndexHeader &indexHeader, uint64_t &pos, int64_t length) const; + bool ReadFileHeader(std::ifstream &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length) const; bool ReadLimitKeys(std::ifstream &in, std::map> &limitKeys, - uint32_t count, uint64_t &pos, int64_t length) const; + uint32_t count, uint64_t &pos, uint64_t length) const; bool ReadIdTables(std::ifstream &in, std::map> &datas, - uint32_t count, uint64_t &pos, int64_t length) const; - bool ReadDataRecordPrepare(std::ifstream &in, RecordItem &record, uint64_t &pos, int64_t length) const; + uint32_t count, uint64_t &pos, uint64_t length) const; + bool ReadDataRecordPrepare(std::ifstream &in, RecordItem &record, uint64_t &pos, uint64_t length) const; bool ReadDataRecordStart(std::ifstream &in, RecordItem &record, const std::map> &limitKeys, const std::map> &datas, diff --git a/src/cmd_parser.cpp b/src/cmd_parser.cpp index c266527..82d75cf 100644 --- a/src/cmd_parser.cpp +++ b/src/cmd_parser.cpp @@ -101,7 +101,7 @@ const string &PackageParser::GetRestoolPath() const return restoolPath_; } -int32_t PackageParser::GetStartId() const +uint32_t PackageParser::GetStartId() const { return startId_; } @@ -472,23 +472,49 @@ uint32_t PackageParser::ParseFileList(const string& fileListPath) return RESTOOL_SUCCESS; } +uint32_t PackageParser::CheckError(int argc, char *argv[], int c, int optIndex) +{ + if (optIndex != -1) { + if ((optarg == nullptr && (optind - 1 < 0 || optind - 1 >= argc)) || + (optarg != nullptr && (optind - 2 < 0 || optind - 2 >= argc))) { // 1 or 2 menas optind offset value + return RESTOOL_ERROR; + } + string curOpt = (optarg == nullptr) ? argv[optind - 1] : argv[optind - 2]; + if (curOpt != ("--" + string(CMD_OPTS[optIndex].name))) { + cerr << "Error: unknown option " << curOpt << endl; + return RESTOOL_ERROR; + } + } + if (c == Option::UNKNOWN) { + if (optopt == 0 && (optind - 1 < 0 || optind - 1 >= argc)) { + return RESTOOL_ERROR; + } + string optUnknown = (optopt == 0) ? argv[optind - 1] : ("-" + string(1, optopt)); + cerr << "Error: unknown option " << optUnknown << endl; + return RESTOOL_ERROR; + } + if (c == Option::NO_ARGUMENT) { + if (optind - 1 < 0 || optind - 1 >= argc) { + return RESTOOL_ERROR; + } + if (IsLongOpt(argc, argv)) { + cerr << "Error: option " << argv[optind - 1] << " must have argument" << endl; + } else { + cerr << "Error: unknown option " << argv[optind - 1] << endl; + } + return RESTOOL_ERROR; + } + return RESTOOL_SUCCESS; +} + uint32_t PackageParser::ParseCommand(int argc, char *argv[]) { restoolPath_ = string(argv[0]); while (true) { int optIndex = -1; - opterr = 0; int c = getopt_long(argc, argv, CMD_PARAMS.c_str(), CMD_OPTS, &optIndex); - if (optIndex != -1) { - // 1 or 2 menas optind offset value - if ((optarg == nullptr && optind - 1 < 0) || (optarg != nullptr && optind - 2 < 0)) { - return RESTOOL_ERROR; - } - string curOpt = (optarg == nullptr) ? argv[optind - 1] : argv[optind - 2]; - if (curOpt != ("--" + string(CMD_OPTS[optIndex].name))) { - cerr << "Error: unknown option " << curOpt << endl; - return RESTOOL_ERROR; - } + if (CheckError(argc, argv, c, optIndex) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; } if (c == Option::END) { if (argc == optind) { @@ -501,19 +527,7 @@ uint32_t PackageParser::ParseCommand(int argc, char *argv[]) cerr << errmsg << endl; return RESTOOL_ERROR; } - if (c == Option::UNKNOWN) { - string optUnknown = (optopt == 0) ? argv[optind - 1] : ("-" + string(1, optopt)); - cerr << "Error: unknown option " << optUnknown << endl; - return RESTOOL_ERROR; - } - if (c == Option::NO_ARGUMENT) { - if (IsLongOpt(argv)) { - cerr << "Error: option " << argv[optind - 1] << " must have argument" << endl; - } else { - cerr << "Error: unknown option " << argv[optind - 1] << endl; - } - return RESTOOL_ERROR; - } + string argValue = (optarg != nullptr) ? optarg : ""; if (c == Option::FILELIST) { return ParseFileList(argValue); @@ -525,9 +539,9 @@ uint32_t PackageParser::ParseCommand(int argc, char *argv[]) return RESTOOL_SUCCESS; } -bool PackageParser::IsLongOpt(char *argv[]) const +bool PackageParser::IsLongOpt(int argc, char *argv[]) const { - if (optind - 1 < 0) { + if (optind - 1 < 0 || optind - 1 >= argc) { return false; } for (auto iter : CMD_OPTS) { diff --git a/src/id_defined_parser.cpp b/src/id_defined_parser.cpp index 980c859..41a07f5 100755 --- a/src/id_defined_parser.cpp +++ b/src/id_defined_parser.cpp @@ -38,7 +38,7 @@ uint32_t IdDefinedParser::Init() { InitParser(); string idDefinedInput = packageParser_.GetIdDefinedInputPath(); - int64_t startId = packageParser_.GetStartId(); + int64_t startId = static_cast(packageParser_.GetStartId()); bool combine = packageParser_.GetCombine(); bool isSys = type_ == ResourceIdCluster::RES_ID_SYS; for (const auto &inputPath : packageParser_.GetInputs()) { diff --git a/src/id_worker.cpp b/src/id_worker.cpp index 0fcc02f..f9ea08a 100644 --- a/src/id_worker.cpp +++ b/src/id_worker.cpp @@ -34,7 +34,7 @@ uint32_t IdWorker::Init(ResourceIdCluster &type, int64_t startId) sysDefinedIds_ = idDefinedParser.GetSysDefinedIds(); appDefinedIds_ = idDefinedParser.GetAppDefinedIds(); if (type == ResourceIdCluster::RES_ID_APP) { - appId_ = startId; + appId_ = static_cast(startId); maxId_ = GetMaxId(startId); } return RESTOOL_SUCCESS; @@ -84,33 +84,6 @@ int64_t IdWorker::GetSystemId(ResType resType, const string &name) const return result->second.id; } -bool IdWorker::PushCache(ResType resType, const string &name, int64_t id) -{ - auto result = cacheIds_.emplace(make_pair(resType, name), id); - if (!result.second) { - return false; - } - if (appId_ == id) { - appId_ = id + 1; - return true; - } - - if (id < appId_) { - return false; - } - - for (int64_t i = appId_; i < id; i++) { - delIds_.push_back(i); - } - appId_ = id + 1; - return true; -} - -void IdWorker::PushDelId(int64_t id) -{ - delIds_.push_back(id); -} - int64_t IdWorker::GenerateAppId(ResType resType, const string &name) { auto result = ids_.find(make_pair(resType, name)); @@ -151,15 +124,15 @@ int64_t IdWorker::GenerateAppId(ResType resType, const string &name) int64_t IdWorker::GetCurId() { if (appDefinedIds_.size() == 0) { - return appId_++; + return static_cast(appId_++); } while (appId_ <= maxId_) { - int64_t id = appId_; + uint64_t id = appId_; auto ret = find_if(appDefinedIds_.begin(), appDefinedIds_.end(), [id](const auto &iter) { return id == iter.second.id; }); if (ret == appDefinedIds_.end()) { - return appId_++; + return static_cast(appId_++); } appId_++; } @@ -188,7 +161,7 @@ uint64_t IdWorker::GetMaxId(uint64_t startId) const while ((flag & startId) == 0) { flag = flag << 1; } - return (startId + flag - 1); + return (startId + flag - 1) > 0 ? (startId + flag - 1) : 0; } } } diff --git a/src/resource_pack.cpp b/src/resource_pack.cpp index bb1549b..c3d0cb0 100644 --- a/src/resource_pack.cpp +++ b/src/resource_pack.cpp @@ -72,7 +72,7 @@ uint32_t ResourcePack::InitModule() moduleName_ = configJson_.GetModuleName(); vector moduleNames = packageParser_.GetModuleNames(); IdWorker &idWorker = IdWorker::GetInstance(); - int64_t startId = packageParser_.GetStartId(); + int64_t startId = static_cast(packageParser_.GetStartId()); if (startId > 0) { return idWorker.Init(hapType, startId); } @@ -474,8 +474,7 @@ uint32_t ResourcePack::HandleLabel(vector &items, ConfigParser &co if (it.GetDataLength() - 1 < 0) { return RESTOOL_ERROR; } - if (!it.SetData(reinterpret_cast(data.c_str()), - static_cast(it.GetDataLength() - 1))) { + if (!it.SetData(reinterpret_cast(data.c_str()), it.GetDataLength() - 1)) { return RESTOOL_ERROR; } if (nextId <= 0) { diff --git a/src/resource_table.cpp b/src/resource_table.cpp index 03cf69c..856c5e5 100644 --- a/src/resource_table.cpp +++ b/src/resource_table.cpp @@ -117,26 +117,26 @@ uint32_t ResourceTable::LoadResTable(const string path, map(length))) { in.close(); return RESTOOL_ERROR; } map> limitKeys; - if (!ReadLimitKeys(in, limitKeys, indexHeader.limitKeyConfigSize, pos, length)) { + if (!ReadLimitKeys(in, limitKeys, indexHeader.limitKeyConfigSize, pos, static_cast(length))) { in.close(); return RESTOOL_ERROR; } map> datas; - if (!ReadIdTables(in, datas, indexHeader.limitKeyConfigSize, pos, length)) { + if (!ReadIdTables(in, datas, indexHeader.limitKeyConfigSize, pos, static_cast(length))) { in.close(); return RESTOOL_ERROR; } while (in.tellg() < length) { RecordItem record; - if (!ReadDataRecordPrepare(in, record, pos, length) || + if (!ReadDataRecordPrepare(in, record, pos, static_cast(length)) || !ReadDataRecordStart(in, record, limitKeys, datas, resInfos)) { in.close(); return RESTOOL_ERROR; @@ -347,7 +347,7 @@ void ResourceTable::SaveIdSets(const map &idSets, ostringstream & } } -bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, uint64_t &pos, int64_t length) const +bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length) const { pos += sizeof(indexHeader); if (pos > length) { @@ -361,7 +361,7 @@ bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, uint6 } bool ResourceTable::ReadLimitKeys(ifstream &in, map> &limitKeys, - uint32_t count, uint64_t &pos, int64_t length) const + uint32_t count, uint64_t &pos, uint64_t length) const { for (uint32_t i = 0; i< count; i++) { pos = pos + TAG_LEN + INT_TO_BYTES + INT_TO_BYTES; @@ -397,7 +397,7 @@ bool ResourceTable::ReadLimitKeys(ifstream &in, map> & } bool ResourceTable::ReadIdTables(std::ifstream &in, std::map> &datas, - uint32_t count, uint64_t &pos, int64_t length) const + uint32_t count, uint64_t &pos, uint64_t length) const { for (uint32_t i = 0; i< count; i++) { pos = pos + TAG_LEN + INT_TO_BYTES; @@ -430,7 +430,7 @@ bool ResourceTable::ReadIdTables(std::ifstream &in, std::map length) {