mirror of
https://github.com/openharmony/developtools_syscap_codec.git
synced 2026-08-27 02:11:19 -04:00
@@ -29,6 +29,7 @@ sources_platform_common = [
|
||||
"./src/create_pcid.c",
|
||||
"./src/endian_internal.c",
|
||||
"./src/context_tool.c",
|
||||
"./src/common_method.c",
|
||||
]
|
||||
|
||||
ohos_executable("syscap_tool_bin") {
|
||||
@@ -97,6 +98,7 @@ if (defined(ohos_lite)) {
|
||||
"./src/context_tool.c",
|
||||
"./src/endian_internal.c",
|
||||
"./src/syscap_tool.c",
|
||||
"./src/common_method.c",
|
||||
]
|
||||
|
||||
deps += [
|
||||
@@ -121,6 +123,7 @@ if (defined(ohos_lite)) {
|
||||
"./src/context_tool.c",
|
||||
"./src/endian_internal.c",
|
||||
"./src/syscap_tool.c",
|
||||
"./src/common_method.c",
|
||||
]
|
||||
external_deps = [
|
||||
"bounds_checking_function:libsec_static",
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "endian_internal.h"
|
||||
#include "syscap_interface.h"
|
||||
#include "context_tool.h"
|
||||
#include "common_method.h"
|
||||
|
||||
#ifdef SYSCAP_DEFINE_EXTERN_ENABLE
|
||||
#include "syscap_define_custom.h"
|
||||
@@ -292,6 +293,10 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson
|
||||
char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t);
|
||||
RPCIDHead *rpcidHeader = (RPCIDHead *)input;
|
||||
cJSON *sysCapJson = cJSON_CreateArray();
|
||||
if (sysCapJson == NULL) {
|
||||
PRINT_ERR("Get sysCapJson failed, sysCapJson is empty.\n");
|
||||
ret = -1;
|
||||
}
|
||||
for (i = 0; i < sysCapCount; i++) {
|
||||
char *temp = sysCapBegin + i * SINGLE_FEAT_LEN;
|
||||
if (strlen(temp) >= SINGLE_FEAT_LEN) {
|
||||
@@ -443,7 +448,7 @@ static char *FreeAfterDecodeRpcidToString(struct FreeAfterDecodeRpcidInfo freeAf
|
||||
{
|
||||
switch (type) {
|
||||
case FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID:
|
||||
free(freeAfterDecodeRpcidInfo.priSyscap);
|
||||
SafeFree(freeAfterDecodeRpcidInfo.priSyscap);
|
||||
free(freeAfterDecodeRpcidInfo.osSysCapIndex);
|
||||
cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine);
|
||||
cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot);
|
||||
@@ -495,6 +500,11 @@ char *DecodeRpcidToStringFormat(const char *inputFile)
|
||||
|
||||
// parse rpcid to json
|
||||
freeAfterDecodeRpcidInfo.rpcidRoot = cJSON_CreateObject();
|
||||
if (freeAfterDecodeRpcidInfo.rpcidRoot == NULL) {
|
||||
PRINT_ERR("Failed to create cJSON object for rpcidRoot\n");
|
||||
return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_CONTEXT_OUT_AFTER_DECODE_RPCID, outBuffer);
|
||||
}
|
||||
|
||||
if (ParseRpcidToJson(freeAfterDecodeRpcidInfo.contextBuffer, bufferLen, freeAfterDecodeRpcidInfo.rpcidRoot)) {
|
||||
PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile);
|
||||
return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_RPCID_ROOT_AFTER_DECODE_RPCID, outBuffer);
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "common_method.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void SafeFree(char *pointer)
|
||||
{
|
||||
if ((pointer) != NULL) {
|
||||
free(pointer);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
/*
|
||||
* Copyright (c) 2024-2024 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef COMMON_METHOD_H
|
||||
#define COMMON_METHOD_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void SafeFree(char *pointer);
|
||||
|
||||
#endif
|
||||
+4
-2
@@ -155,23 +155,25 @@ int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len)
|
||||
}
|
||||
if (bufferLen < (2 * sizeof(uint32_t))) { // 2, header of rpcid.sc
|
||||
PRINT_ERR("Parse file failed(format is invalid), input file : %s\n", inputFile);
|
||||
free(contextBuffer);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
return -1;
|
||||
}
|
||||
rpcidHeader = (RPCIDHead *)contextBuffer;
|
||||
if (rpcidHeader->apiVersionType != 1) {
|
||||
PRINT_ERR("Parse file failed(apiVersionType != 1), input file : %s\n", inputFile);
|
||||
free(contextBuffer);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
return -1;
|
||||
}
|
||||
sysCaptype = NtohsInter(*(uint16_t *)(rpcidHeader + 1));
|
||||
if (sysCaptype != 2) { // 2, app syscap type
|
||||
PRINT_ERR("Parse file failed(sysCaptype != 2), input file : %s\n", inputFile);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
return -1;
|
||||
}
|
||||
sysCapLength = NtohsInter(*(uint16_t *)((char *)(rpcidHeader + 1) + sizeof(uint16_t)));
|
||||
if (bufferLen < sizeof(RPCIDHead) + sizeof(uint32_t) + sysCapLength) {
|
||||
PRINT_ERR("Parse file failed(SysCap length exceeded), input file : %s\n", inputFile);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
+27
-20
@@ -25,6 +25,7 @@
|
||||
#include "endian_internal.h"
|
||||
#include "create_pcid.h"
|
||||
#include "context_tool.h"
|
||||
#include "common_method.h"
|
||||
|
||||
#ifdef SYSCAP_DEFINE_EXTERN_ENABLE
|
||||
#include "syscap_define_custom.h"
|
||||
@@ -58,7 +59,6 @@ struct FreeEncodePcidInfo {
|
||||
char *outDirPathFinal;
|
||||
};
|
||||
|
||||
|
||||
struct FreeDecodePcidJsonInfo {
|
||||
char *strJson;
|
||||
char *contextBuffer;
|
||||
@@ -271,6 +271,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath)
|
||||
|
||||
cJSON *jsonOsSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "os");
|
||||
cJSON *jsonPriSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "private");
|
||||
|
||||
ret = GetOsAndPriSyscapSize(jsonOsSyscapObj, jsonPriSyscapObj, &osCapSize, &privateCapSize);
|
||||
if (ret != 0) {
|
||||
return PreFreeAfterCreatePCID(NULL, allOsSyscapObj, jsonRootObj, contextBuffer, 0);
|
||||
@@ -526,15 +527,18 @@ int32_t DecodePCID(char *inputFile, char *outDirPath)
|
||||
return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_ROOT_OUT, ret);
|
||||
}
|
||||
|
||||
freePcidJsonInfo.sysCapObj = NULL; // avoid being released repeatedly.
|
||||
freePcidJsonInfo.strJson = cJSON_Print(freePcidJsonInfo.jsonRootObj);
|
||||
if (freePcidJsonInfo.strJson == NULL) {
|
||||
PRINT_ERR("cJSON_Print failed to generate JSON string\n");
|
||||
return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_ROOT_OUT, -1);
|
||||
}
|
||||
|
||||
char *strJson = cJSON_Print(freePcidJsonInfo.jsonRootObj);
|
||||
const char outputFileName[] = "pcid.json";
|
||||
ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, strJson, strlen(strJson));
|
||||
ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, freePcidJsonInfo.strJson,
|
||||
strlen(freePcidJsonInfo.strJson));
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, outputFileName);
|
||||
}
|
||||
free(strJson);
|
||||
return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONVERT_OUT, ret);
|
||||
}
|
||||
|
||||
@@ -665,7 +669,6 @@ static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapSt
|
||||
cJSON *sysCapArray = cJSON_CreateArray();
|
||||
if (sysCapArray == NULL) {
|
||||
PRINT_ERR("Create cJSON array failed.\n");
|
||||
free(sysCapArray);
|
||||
return -1;
|
||||
}
|
||||
if (priSyscapStringLen == 0) {
|
||||
@@ -701,7 +704,6 @@ static int32_t GetSyscapStr(char *input, char const *priSyscapStr, uint32_t* osS
|
||||
uint32_t fileContextLen;
|
||||
if (GetFileContext(input, &ctx, (uint32_t *)&fileContextLen) != 0) {
|
||||
PRINT_ERR("GetFileContext failed, input file : %s\n", input);
|
||||
free(ctx);
|
||||
return -1;
|
||||
}
|
||||
if (ParseStringSyscap(ctx, osSyscap, OS_SYSCAP_NUM, pcidHeader, PCID_HEADER) != 0) {
|
||||
@@ -719,6 +721,7 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath)
|
||||
uint32_t osSyscap[OS_SYSCAP_NUM] = {0};
|
||||
uint32_t pcidHeader[PCID_HEADER];
|
||||
char *priSyscapStr = NULL;
|
||||
char *jsonBuffer = NULL;
|
||||
ret = GetSyscapStr(input, priSyscapStr, osSyscap, pcidHeader);
|
||||
if (ret == -1) {
|
||||
return ret;
|
||||
@@ -727,40 +730,44 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath)
|
||||
// add to json object
|
||||
cJSON *sysCapObj = cJSON_CreateObject();
|
||||
cJSON *rootObj = cJSON_CreateObject();
|
||||
if (sysCapObj == NULL || rootObj == NULL) {
|
||||
PRINT_ERR("Failed to create cJSON objects.\n");
|
||||
goto FAILED;
|
||||
}
|
||||
|
||||
if (!cJSON_AddItemToObject(rootObj, "syscap", sysCapObj)) {
|
||||
PRINT_ERR("Add syscap to json failed.\n");
|
||||
goto ADD_JSON_FAILED;
|
||||
goto FAILED;
|
||||
}
|
||||
if (AddHeaderToJsonObj(pcidHeader, PCID_HEADER, rootObj) != 0) {
|
||||
PRINT_ERR("Add header to json object failed.\n");
|
||||
goto ADD_JSON_FAILED;
|
||||
goto FAILED;
|
||||
}
|
||||
if (AddOsSyscapToJsonObj(osSyscap, OS_SYSCAP_NUM, sysCapObj) != 0) {
|
||||
PRINT_ERR("Add os syscap json object failed.\n");
|
||||
goto ADD_JSON_FAILED;
|
||||
goto FAILED;
|
||||
}
|
||||
|
||||
if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t)strlen(priSyscapStr), sysCapObj) != 0) {
|
||||
if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t) strlen(priSyscapStr), sysCapObj) != 0) {
|
||||
PRINT_ERR("Add private syscap json object failed.\n");
|
||||
goto ADD_JSON_FAILED;
|
||||
goto FAILED;
|
||||
}
|
||||
// save as json file
|
||||
char *jsonBuffer = cJSON_Print(rootObj);
|
||||
jsonBuffer = cJSON_Print(rootObj);
|
||||
if (jsonBuffer == NULL) {
|
||||
PRINT_ERR("json buffer is null.\n");
|
||||
goto ADD_JSON_FAILED;
|
||||
goto FAILED;
|
||||
}
|
||||
const char outputFileName[] = "pcid.json";
|
||||
if (ConvertedContextSaveAsFile(outDirPath, outputFileName,
|
||||
jsonBuffer, strlen(jsonBuffer)) != 0) {
|
||||
if (ConvertedContextSaveAsFile(outDirPath, outputFileName, jsonBuffer, strlen(jsonBuffer)) != 0) {
|
||||
PRINT_ERR("Save as json file failed.\n");
|
||||
goto SAVE_FAILED;
|
||||
goto FAILED;
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
SAVE_FAILED:
|
||||
free(jsonBuffer);
|
||||
ADD_JSON_FAILED:
|
||||
FAILED:
|
||||
SafeFree(jsonBuffer);
|
||||
SafeFree(priSyscapStr);
|
||||
cJSON_Delete(sysCapObj);
|
||||
cJSON_Delete(rootObj);
|
||||
return ret;
|
||||
|
||||
+22
-7
@@ -27,6 +27,7 @@
|
||||
#include "create_pcid.h"
|
||||
#include "syscap_tool.h"
|
||||
#include "context_tool.h"
|
||||
#include "common_method.h"
|
||||
|
||||
#ifdef SYSCAP_DEFINE_EXTERN_ENABLE
|
||||
#include "syscap_define_custom.h"
|
||||
@@ -71,6 +72,14 @@ struct FreeAfterEncodeRpcidscInfo {
|
||||
int16_t flag;
|
||||
};
|
||||
|
||||
static void FreePointerMemory(char *pcidContent, char *rpcidContent, char *pcidPriSyscap, char *rpcidPriSyscap)
|
||||
{
|
||||
SafeFree(pcidContent);
|
||||
SafeFree(rpcidPriSyscap);
|
||||
SafeFree(rpcidContent);
|
||||
SafeFree(pcidPriSyscap);
|
||||
}
|
||||
|
||||
static int32_t FillOsCapLength(char *convertedBuffer, char *contextBuffer, struct JsonObjectSysCap gJsonObjectSysCap,
|
||||
uint32_t sysCapSize, int32_t ret)
|
||||
{
|
||||
@@ -204,6 +213,11 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson
|
||||
char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t);
|
||||
RPCIDHead *rpcidHeader = (RPCIDHead *)input;
|
||||
cJSON *sysCapJson = cJSON_CreateArray();
|
||||
if (sysCapJson == NULL) {
|
||||
PRINT_ERR("Failed to create sysCapJson array\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < sysCapCount; i++) {
|
||||
char *temp = sysCapBegin + i * SINGLE_FEAT_LEN;
|
||||
if (strlen(temp) >= SINGLE_FEAT_LEN || strlen(temp) == 0) {
|
||||
@@ -263,13 +277,16 @@ int32_t RPCIDDecode(char *inputFile, char *outputPath)
|
||||
|
||||
// save to json file
|
||||
convertedBuffer = cJSON_Print(rpcidRoot);
|
||||
if (convertedBuffer == NULL) {
|
||||
PRINT_ERR("cJSON_Print failed to create JSON string\n");
|
||||
goto FREE_RPCID_ROOT;
|
||||
}
|
||||
|
||||
ret = ConvertedContextSaveAsFile(outputPath, "rpcid.json", convertedBuffer, strlen(convertedBuffer));
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.json\n", outputPath);
|
||||
free(convertedBuffer);
|
||||
goto FREE_RPCID_ROOT;
|
||||
}
|
||||
free(convertedBuffer);
|
||||
|
||||
FREE_RPCID_ROOT:
|
||||
cJSON_Delete(rpcidRoot);
|
||||
@@ -705,10 +722,7 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ
|
||||
&rpcidPriSyscap, &rpcidPriSyscapLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret);
|
||||
free(pcidContent);
|
||||
free(rpcidContent);
|
||||
free(pcidPriSyscap);
|
||||
free(rpcidPriSyscap);
|
||||
FreePointerMemory(pcidContent, rpcidContent, pcidPriSyscap, rpcidPriSyscap);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -720,5 +734,6 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ
|
||||
} else {
|
||||
printf("Fail! The pcid does not meet the rpcid\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,6 +41,7 @@ if (defined(ohos_lite)) {
|
||||
"../../../src/create_pcid.c",
|
||||
"../../../src/endian_internal.c",
|
||||
"../../../src/syscap_tool.c",
|
||||
"../../../src/common_method.c",
|
||||
"syscap_codec_test.cpp",
|
||||
]
|
||||
|
||||
@@ -82,6 +83,7 @@ if (defined(ohos_lite)) {
|
||||
"../../../src/create_pcid.c",
|
||||
"../../../src/endian_internal.c",
|
||||
"../../../src/syscap_tool.c",
|
||||
"../../../src/common_method.c",
|
||||
"syscap_codec_test.cpp",
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user