diff --git a/src/resource_dumper.cpp b/src/resource_dumper.cpp index ec106f3eb908c70a126e1e7bdd127e4860516503..a313ab58d1f1864e717c87905c1d94a860c337f0 100644 --- a/src/resource_dumper.cpp +++ b/src/resource_dumper.cpp @@ -156,22 +156,18 @@ uint32_t CommonDumper::DumpRes(std::string &out) const return RESTOOL_ERROR; } if (!bundleName_.empty() && !moduleName_.empty()) { - cJSON *bundleName = cJSON_CreateString(bundleName_.c_str()); - cJSON *moduleName = cJSON_CreateString(moduleName_.c_str()); - if (bundleName) { - cJSON_AddItemToObject(root.get(), "bundleName", bundleName); + if (!cJSON_AddStringToObject(root.get(), "bundleName", bundleName_.c_str())) { + std::cout << "Warning: unable to add bundleName to the root object." << std::endl; } - if (moduleName) { - cJSON_AddItemToObject(root.get(), "moduleName", moduleName); + if (!cJSON_AddStringToObject(root.get(), "moduleName", moduleName_.c_str())) { + std::cout << "Warning: unable to add moduleName to the root object." << std::endl; } } - cJSON *resource = cJSON_CreateArray(); + cJSON *resource = cJSON_AddArrayToObject(root.get(), "resource"); if (!resource) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR) - .FormatCause("unable to create cJSON object for resource array")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add resource array")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(root.get(), "resource", resource); for (auto it = resInfos_.begin(); it != resInfos_.end(); it++) { if (AddResourceToJson(it->first, it->second, resource)) { return RESTOOL_ERROR; @@ -196,12 +192,12 @@ uint32_t CommonDumper::AddKeyParamsToJson(const std::vector &keyParams } for (const auto &keyParam : keyParams) { std::string valueStr = ResourceUtil::GetKeyParamValue(keyParam); - cJSON *value = cJSON_CreateString(valueStr.c_str()); + cJSON *value = + cJSON_AddStringToObject(json, ResourceUtil::KeyTypeToStr(keyParam.keyType).c_str(), valueStr.c_str()); if (!value) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for keyparam")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add cJSON object for keyparam")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(json, ResourceUtil::KeyTypeToStr(keyParam.keyType).c_str(), value); } return RESTOOL_SUCCESS; } @@ -212,26 +208,23 @@ uint32_t CommonDumper::AddItemCommonPropToJson(int32_t resId, const ResourceItem PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("add item common property to null json object")); return RESTOOL_ERROR; } - cJSON *id = cJSON_CreateNumber(resId); + cJSON *id = cJSON_AddNumberToObject(json, "id", resId); if (!id) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for resource id")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add cJSON object for resource id")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(json, "id", id); - cJSON *name = cJSON_CreateString(item.GetName().c_str()); + cJSON *name = cJSON_AddStringToObject(json, "name", item.GetName().c_str()); if (!name) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for resource name")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add cJSON object for resource name")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(json, "name", name); - cJSON *type = cJSON_CreateString((ResourceUtil::ResTypeToString(item.GetResType())).c_str()); + cJSON *type = cJSON_AddStringToObject(json, "type", (ResourceUtil::ResTypeToString(item.GetResType())).c_str()); if (!type) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for resource type")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add cJSON object for resource type")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(json, "type", type); return RESTOOL_SUCCESS; } @@ -250,34 +243,35 @@ uint32_t CommonDumper::AddResourceToJson(int64_t resId, const std::vector rawValues = item.SplitValue(); uint32_t index = 1; if (rawValues.size() % PAIR_SIZE != 0) { - cJSON *parent = cJSON_CreateString(rawValues[0].c_str()); + cJSON *parent = cJSON_AddStringToObject(json, "parent", rawValues[0].c_str()); if (!parent) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for parent")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add cJSON object for parent")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(json, "parent", parent); index++; } for (; index < rawValues.size(); index += PAIR_SIZE) { - cJSON *item = cJSON_CreateString(rawValues[index].c_str()); + cJSON *item = cJSON_AddStringToObject(value, rawValues[index - 1].c_str(), rawValues[index].c_str()); if (!item) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR) - .FormatCause("unable to create cJSON object for value item")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add cJSON object for value item")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(value, rawValues[index -1].c_str(), item); } return RESTOOL_SUCCESS; } @@ -330,13 +320,11 @@ uint32_t CommonDumper::AddValueToJson(const ResourceItem &item, cJSON *json) con return RESTOOL_ERROR; } if (item.IsArray()) { - cJSON *values = cJSON_CreateArray(); + cJSON *values = cJSON_AddArrayToObject(json, "value"); if (!values) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR) - .FormatCause("unable to create cJSON object for value array")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add cJSON object for value array")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(json, "value", values); const std::vector rawValues = item.SplitValue(); for (const std::string &value : rawValues) { cJSON *valueItem = cJSON_CreateString(value.c_str()); @@ -345,20 +333,25 @@ uint32_t CommonDumper::AddValueToJson(const ResourceItem &item, cJSON *json) con GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for value item")); return RESTOOL_ERROR; } - cJSON_AddItemToArray(values, valueItem); + if (!cJSON_AddItemToArray(values, valueItem)) { + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add value item")); + return RESTOOL_ERROR; + }; } return RESTOOL_SUCCESS; } if (item.IsPair()) { return AddPairVauleToJson(item, json); } - std::string rawValue = std::string(reinterpret_cast(item.GetData()), item.GetDataLength()); - cJSON *value = cJSON_CreateString(rawValue.c_str()); + std::string rawValue; + if (item.GetData() != nullptr) { + rawValue = std::string(reinterpret_cast(item.GetData()), item.GetDataLength()); + } + cJSON *value = cJSON_AddStringToObject(json, "value", rawValue.c_str()); if (!value) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for value")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add cJSON object for value")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(json, "value", value); return RESTOOL_SUCCESS; } @@ -370,12 +363,11 @@ uint32_t ConfigDumper::DumpRes(std::string &out) const PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for root object")); return RESTOOL_ERROR; } - cJSON *configs = cJSON_CreateArray(); + cJSON *configs = cJSON_AddArrayToObject(root.get(), "config"); if (!configs) { - PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for config array")); + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add cJSON object for config array")); return RESTOOL_ERROR; } - cJSON_AddItemToObject(root.get(), "config", configs); std::set configSet; for (auto it = resInfos_.cbegin(); it != resInfos_.cend(); it++) { @@ -391,7 +383,10 @@ uint32_t ConfigDumper::DumpRes(std::string &out) const GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to create cJSON object for limitkey")); return RESTOOL_ERROR; } - cJSON_AddItemToArray(configs, config); + if (!cJSON_AddItemToArray(configs, config)) { + PrintError(GetError(ERR_CODE_UNDEFINED_ERROR).FormatCause("unable to add limitkey")); + return RESTOOL_ERROR; + }; } } char *rawStr = cJSON_Print(root.get()); diff --git a/src/resource_table.cpp b/src/resource_table.cpp index 2e53c7f95e14d63b4c8b2fab3b22b1faefd1c3fd..2afe3c52364d00e58fd25c62da2fa84eae3c9940 100644 --- a/src/resource_table.cpp +++ b/src/resource_table.cpp @@ -368,13 +368,26 @@ void ResourceTable::SaveIdSets(const map &idSets, ostringstream & bool ResourceTable::ReadFileHeader(basic_istream &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length) { pos += sizeof(indexHeader); + ErrorInfo lengthErr = GetError(ERR_CODE_INVALID_RESOURCE_INDEX).FormatCause("header length error"); if (pos > length) { - PrintError(GetError(ERR_CODE_INVALID_RESOURCE_INDEX).FormatCause("header length error")); + PrintError(lengthErr); return false; } in.read(reinterpret_cast(indexHeader.version), VERSION_MAX_LEN); + if (in.gcount() != VERSION_MAX_LEN) { + PrintError(lengthErr); + return false; + } in.read(reinterpret_cast(&indexHeader.fileSize), INT_TO_BYTES); + if (in.gcount() != INT_TO_BYTES) { + PrintError(lengthErr); + return false; + } in.read(reinterpret_cast(&indexHeader.limitKeyConfigSize), INT_TO_BYTES); + if (in.gcount() != INT_TO_BYTES) { + PrintError(lengthErr); + return false; + } return true; }