From 183f087a5031cb01a830b8ad5dfd2039f8f8cacc Mon Sep 17 00:00:00 2001 From: niuyuan Date: Sat, 26 Aug 2023 15:42:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= =?UTF-8?q?=EF=BC=9A=E9=87=8D=E5=A4=8D=E5=AE=9E=E7=8E=B0=E6=8F=90=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: niuyuan --- BUILD.gn | 3 + include/context_tool.h | 50 ++++++ interfaces/inner_api/syscap_interface.c | 124 ++------------- napi/BUILD.gn | 1 + napi/napi_query_syscap.cpp | 7 +- src/context_tool.c | 173 +++++++++++++++++++++ src/create_pcid.c | 133 ++-------------- src/main.c | 6 +- src/syscap_tool.c | 193 +++--------------------- test/unittest/common/BUILD.gn | 2 + 10 files changed, 270 insertions(+), 422 deletions(-) create mode 100644 include/context_tool.h create mode 100644 src/context_tool.c diff --git a/BUILD.gn b/BUILD.gn index 206f20a..f9d2aee 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -28,6 +28,7 @@ sources_platform_common = [ "./src/syscap_tool.c", "./src/create_pcid.c", "./src/endian_internal.c", + "./src/context_tool.c", ] ohos_executable("syscap_tool_bin") { @@ -93,6 +94,7 @@ if (defined(ohos_lite)) { public_configs = [ ":syscap_interface_public_config" ] sources = [ "./interfaces/inner_api/syscap_interface.c", + "./src/context_tool.c", "./src/endian_internal.c", "./src/syscap_tool.c", ] @@ -116,6 +118,7 @@ if (defined(ohos_lite)) { public_configs = [ ":syscap_interface_public_config" ] sources = [ "./interfaces/inner_api/syscap_interface.c", + "./src/context_tool.c", "./src/endian_internal.c", "./src/syscap_tool.c", ] diff --git a/include/context_tool.h b/include/context_tool.h new file mode 100644 index 0000000..2b5c1c1 --- /dev/null +++ b/include/context_tool.h @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2021-2022 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 CONTEXT_TOOL_H +#define CONTEXT_TOOL_H + +#include +#include + +#define PRINT_ERR(...) \ + do { \ + printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); \ + } while (0) + +#ifdef __cplusplus +#if __cplusplus +extern "C" { +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +typedef struct RequiredProductCompatibilityIDHead { + uint16_t apiVersion : 15; + uint16_t apiVersionType : 1; +} RPCIDHead; + +void FreeContextBuffer(char *contextBuffer); +int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen); +int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, char *convertedBuffer, size_t contextBufLen); +int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len); + +#ifdef __cplusplus +#if __cplusplus +} +#endif /* __cplusplus */ +#endif /* __cplusplus */ + +#endif /* SYSCAP_TOOL_H */ \ No newline at end of file diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index 6e1d402..9bd9597 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -25,6 +25,7 @@ #include "syscap_tool.h" #include "endian_internal.h" #include "syscap_interface.h" +#include "context_tool.h" #ifdef SYSCAP_DEFINE_EXTERN_ENABLE #include "syscap_define_custom.h" @@ -41,18 +42,6 @@ #define INT_BIT 32 #define U32_TO_STR_MAX_LEN 11 - -#define PRINT_ERR(...) \ - do { \ - printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \ - printf(__VA_ARGS__); \ - } while (0) - -typedef struct RequiredProductCompatibilityIDHead { - uint16_t apiVersion : 15; - uint16_t apiVersionType : 1; -} RPCIDHead; - typedef struct ProductCompatibilityID { uint16_t apiVersion : 15; uint16_t apiVersionType : 1; @@ -64,66 +53,6 @@ typedef struct ProductCompatibilityID { static const char *g_pcidPath = "/system/etc/PCID.sc"; -static void FreeContextBuffer(char *contextBuffer) -{ - (void)free(contextBuffer); -} - -static int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen) -{ - int32_t ret; - FILE *fp = NULL; - struct stat statBuf; - char *contextBuffer = NULL; - char path[PATH_MAX + 1] = {0x00}; - -#ifdef _POSIX_ - if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) { - PRINT_ERR("get path(%s) failed\n", inputFile); - return -1; - } -#else - if (strlen(inputFile) > PATH_MAX || realpath(inputFile, path) == NULL) { - PRINT_ERR("get file(%s) real path failed\n", inputFile); - return -1; - } -#endif - - ret = stat(path, &statBuf); - if (ret != 0) { - PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno); - return -1; - } - if (!(statBuf.st_mode & S_IRUSR)) { - PRINT_ERR("don't have permission to read the file(%s)\n", path); - return -1; - } - contextBuffer = (char *)malloc(statBuf.st_size + 1); - if (contextBuffer == NULL) { - PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); - return -1; - } - fp = fopen(path, "rb"); - if (fp == NULL) { - PRINT_ERR("open file(%s) failed, errno = %d\n", path, errno); - FreeContextBuffer(contextBuffer); - return -1; - } - size_t retFread = fread(contextBuffer, statBuf.st_size, 1, fp); - if (retFread != 1) { - PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno); - FreeContextBuffer(contextBuffer); - (void)fclose(fp); - return -1; - } - contextBuffer[statBuf.st_size] = '\0'; - (void)fclose(fp); - - *contextBufPtr = contextBuffer; - *bufferLen = statBuf.st_size + 1; - return 0; -} - bool EncodeOsSyscap(char *output, int len) { int32_t ret; @@ -324,12 +253,17 @@ static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, const uint16_t *inde static cJSON *CreateWholeSyscapJsonObj(void) { - size_t numOfSyscapAll = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); - cJSON *root = cJSON_CreateObject(); - for (size_t i = 0; i < numOfSyscapAll; i++) { - cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num)); + cJSON *syscapJsonObj = cJSON_CreateObject(); + if (syscapJsonObj == NULL) { + PRINT_ERR("interface-create jsonObj failed."); + return NULL; + } + + size_t syscapNums = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); + for (size_t i = 0; i < syscapNums; i++) { + cJSON_AddItemToObject(syscapJsonObj, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num)); } - return root; + return syscapJsonObj; } static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson) @@ -380,42 +314,6 @@ FREE_SYSCAP_OUT: return ret; } -static int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len) -{ - uint32_t bufferLen; - uint16_t sysCaptype, sysCapLength; - char *contextBuffer = NULL; - RPCIDHead *rpcidHeader = NULL; - - if (GetFileContext(inputFile, &contextBuffer, &bufferLen)) { - PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); - return -1; - } - if (bufferLen < (2 * sizeof(uint32_t))) { // 2, header of rpcid.sc - PRINT_ERR("Parse file failed(format is invalid), input file : %s\n", inputFile); - return -1; - } - rpcidHeader = (RPCIDHead *)contextBuffer; - if (rpcidHeader->apiVersionType != 1) { - PRINT_ERR("Parse file failed(apiVersionType != 1), input file : %s\n", inputFile); - 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); - 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); - return -1; - } - - *buffer = contextBuffer; - *len = bufferLen; - return 0; -} - char *DecodeRpcidToStringFormat(const char *inputFile) { int32_t ret = 0; diff --git a/napi/BUILD.gn b/napi/BUILD.gn index 4a23966..dc518c7 100644 --- a/napi/BUILD.gn +++ b/napi/BUILD.gn @@ -27,6 +27,7 @@ sources_platform_common = [ "../src/syscap_tool.c", "../src/create_pcid.c", "../src/endian_internal.c", + "../src/context_tool.c", "../interfaces/inner_api/syscap_interface.c", ] diff --git a/napi/napi_query_syscap.cpp b/napi/napi_query_syscap.cpp index 8788c1b..c3901e2 100644 --- a/napi/napi_query_syscap.cpp +++ b/napi/napi_query_syscap.cpp @@ -20,6 +20,7 @@ #include "napi/native_api.h" #include "napi/native_node_api.h" #include "syscap_interface.h" +#include "context_tool.h" namespace OHOS { EXTERN_C_START @@ -28,12 +29,6 @@ constexpr size_t PCID_MAIN_U32 = OS_SYSCAP_U32_NUM + 2; constexpr size_t U32_TO_STR_MAX_LEN = 11; constexpr size_t KEY_BUFFER_SIZE = 32; -#define PRINT_ERR(...) \ - do { \ - printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \ - printf(__VA_ARGS__); \ - } while (0) - #define GET_PARAMS(env, info, num) \ size_t argc = num; \ napi_value argv[num] = {0}; \ diff --git a/src/context_tool.c b/src/context_tool.c new file mode 100644 index 0000000..d333329 --- /dev/null +++ b/src/context_tool.c @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2023-2023 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 "context_tool.h" + +#include +#include +#include +#include +#include +#include "securec.h" + +#include "endian_internal.h" + +void FreeContextBuffer(char *contextBuffer) +{ + if (contextBuffer != NULL) { + (void)free(contextBuffer); + } +} + +int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen) +{ + int32_t ret; + FILE *fp = NULL; + struct stat statBuf; + char *contextBuffer = NULL; + char path[PATH_MAX + 1] = {0x00}; + +#ifdef _POSIX_ + if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) { + PRINT_ERR("get path(%s) failed\n", inputFile); + return -1; + } +#else + if (strlen(inputFile) > PATH_MAX || realpath(inputFile, path) == NULL) { + PRINT_ERR("get file(%s) real path failed\n", inputFile); + return -1; + } +#endif + + ret = stat(path, &statBuf); + if (ret != 0) { + PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno); + return -1; + } + if (!(statBuf.st_mode & S_IRUSR)) { + PRINT_ERR("don't have permission to read the file(%s)\n", path); + return -1; + } + contextBuffer = (char *)malloc(statBuf.st_size + 1); + if (contextBuffer == NULL) { + PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); + return -1; + } + fp = fopen(path, "rb"); + if (fp == NULL) { + PRINT_ERR("open file(%s) failed, errno = %d\n", path, errno); + FreeContextBuffer(contextBuffer); + return -1; + } + size_t retFread = fread(contextBuffer, statBuf.st_size, 1, fp); + if (retFread != 1) { + PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno); + FreeContextBuffer(contextBuffer); + (void)fclose(fp); + return -1; + } + contextBuffer[statBuf.st_size] = '\0'; + (void)fclose(fp); + + *contextBufPtr = contextBuffer; + *bufferLen = statBuf.st_size + 1; + return 0; +} + +int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, char *convertedBuffer, size_t contextBufLen) +{ + int32_t ret; + FILE *fp = NULL; + char path[PATH_MAX + 1] = {0x00}; + +#ifdef _POSIX_ + if (strlen(outDirPath) > PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) { + PRINT_ERR("get path(%s) failed\n", outDirPath); + return -1; + } +#else + if (strlen(outDirPath) > PATH_MAX || realpath(outDirPath, path) == NULL) { + PRINT_ERR("get file(%s) real path failed\n", outDirPath); + return -1; + } +#endif + int32_t pathLen = strlen(path); + if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') { + path[pathLen] = '/'; + } + + if (strlen(path) + strlen(filename) + 1 > PATH_MAX) { + PRINT_ERR("length of path too long.\n"); + return -1; + } + ret = strncat_s(path, PATH_MAX, filename, strlen(filename) + 1); + if (ret != 0) { + PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n", + path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno); + return -1; + } + + fp = fopen(path, "wb"); + if (fp == NULL) { + PRINT_ERR("can't create file(%s), errno = %d\n", path, errno); + return -1; + } + + if (fwrite(convertedBuffer, contextBufLen, 1, fp) != 1) { + PRINT_ERR("can't write file(%s),errno = %d\n", path, errno); + (void)fclose(fp); + return -1; + } + + (void)fclose(fp); + + return 0; +} + +int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len) +{ + uint32_t bufferLen; + uint16_t sysCaptype, sysCapLength; + char *contextBuffer = NULL; + RPCIDHead *rpcidHeader = NULL; + + if (GetFileContext(inputFile, &contextBuffer, &bufferLen)) { + PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); + return -1; + } + if (bufferLen < (2 * sizeof(uint32_t))) { // 2, header of rpcid.sc + PRINT_ERR("Parse file failed(format is invalid), input file : %s\n", inputFile); + return -1; + } + rpcidHeader = (RPCIDHead *)contextBuffer; + if (rpcidHeader->apiVersionType != 1) { + PRINT_ERR("Parse file failed(apiVersionType != 1), input file : %s\n", inputFile); + 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); + 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); + return -1; + } + + *buffer = contextBuffer; + *len = bufferLen; + return 0; +} diff --git a/src/create_pcid.c b/src/create_pcid.c index 34680e5..136ae9e 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -24,6 +24,7 @@ #include "cJSON.h" #include "endian_internal.h" #include "create_pcid.h" +#include "context_tool.h" #ifdef SYSCAP_DEFINE_EXTERN_ENABLE #include "syscap_define_custom.h" @@ -39,128 +40,14 @@ #define U32_TO_STR_MAX_LEN 11 -#define PRINT_ERR(...) \ - do { \ - printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \ - printf(__VA_ARGS__); \ - } while (0) - -static void FreeContextBuffer(char *contextBuffer) -{ - (void)free(contextBuffer); -} - -static int32_t GetFileContext(char *inputFile, char **contextBufPtr, size_t *contextBufLen) -{ - int32_t ret; - FILE *fp = NULL; - struct stat statBuf; - char *contextBuffer = NULL; - char path[PATH_MAX + 1] = {0x00}; - -#ifdef _POSIX_ - if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) { - PRINT_ERR("get path(%s) failed\n", inputFile); - return -1; - } -#else - if (strlen(inputFile) > PATH_MAX || realpath(inputFile, path) == NULL) { - PRINT_ERR("get file(%s) real path failed\n", inputFile); - return -1; - } -#endif - - ret = stat(path, &statBuf); - if (ret != 0) { - PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno); - return -1; - } - if (!(statBuf.st_mode & S_IRUSR)) { - PRINT_ERR("don't have permission to read the file(%s)\n", path); - return -1; - } - contextBuffer = (char *)malloc(statBuf.st_size + 1); - if (contextBuffer == NULL) { - PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); - return -1; - } - - fp = fopen(path, "rb"); - if (fp == NULL) { - PRINT_ERR("open file(%s) failed, errno = %d\n", path, errno); - FreeContextBuffer(contextBuffer); - return -1; - } - size_t retFread = fread(contextBuffer, statBuf.st_size, 1, fp); - if (retFread != 1) { - PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno); - FreeContextBuffer(contextBuffer); - (void)fclose(fp); - return -1; - } - contextBuffer[statBuf.st_size] = '\0'; - (void)fclose(fp); - - *contextBufPtr = contextBuffer; - *contextBufLen = statBuf.st_size + 1; - return 0; -} - -static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, \ - char *convertedBuffer, size_t contextBufLen) -{ - int32_t ret; - FILE *fp = NULL; - char path[PATH_MAX + 1] = {0x00}; - -#ifdef _POSIX_ - if (strlen(outDirPath) > PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) { - PRINT_ERR("get path(%s) failed\n", outDirPath); - return -1; - } -#else - if (strlen(outDirPath) > PATH_MAX || realpath(outDirPath, path) == NULL) { - PRINT_ERR("get file(%s) real path failed\n", outDirPath); - return -1; - } -#endif - int32_t pathLen = strlen(path); - if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') { - path[pathLen] = '/'; - } - - if (strlen(path) + strlen(filename) + 1 > PATH_MAX) { - PRINT_ERR("length of path too long.\n"); - return -1; - } - ret = strncat_s(path, PATH_MAX, filename, strlen(filename) + 1); - if (ret != 0) { - PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n", - path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno); - return -1; - } - - fp = fopen(path, "wb"); - if (fp == NULL) { - PRINT_ERR("can't create file(%s), errno = %d\n", path, errno); - return -1; - } - - if (fwrite(convertedBuffer, contextBufLen, 1, fp) != 1) { - PRINT_ERR("can't write file(%s),errno = %d\n", path, errno); - (void)fclose(fp); - return -1; - } - - (void)fclose(fp); - - return 0; -} - static cJSON *CreateWholeSyscapJsonObj(void) { size_t numOfSyscapAll = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + PRINT_ERR("picd-create jsonObj failed."); + return NULL; + } for (size_t i = 0; i < numOfSyscapAll; i++) { cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num)); } @@ -310,7 +197,7 @@ int32_t GetPriSyscapLen(uint32_t privateCapSize, cJSON *jsonPriSyscapObj, uint16 int32_t CreatePCID(char *inputFile, char *outDirPath) { uint32_t privateCapSize, osCapSize; - size_t contextBufLen; + uint32_t contextBufLen; char *contextBuffer = NULL; cJSON *allOsSyscapObj = CreateWholeSyscapJsonObj(); @@ -473,7 +360,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) { int32_t ret = 0; char *contextBuffer = NULL; - size_t contextBufLen; + uint32_t contextBufLen; ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen); if (ret != 0) { @@ -725,7 +612,7 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) int32_t ret = -1; uint32_t osSyscap[OS_SYSCAP_NUM] = {0}; uint32_t pcidHeader[PCID_HEADER]; - size_t fileContextLen; + uint32_t fileContextLen; char *ctx = NULL; char *priSyscapStr = NULL; @@ -780,7 +667,7 @@ PARSE_FAILED: int32_t EncodePcidscToString(char *inputFile, char *outDirPath) { int32_t ret = 0; - size_t bufferLen, privateSyscapLen, outputLen; + uint32_t bufferLen, privateSyscapLen, outputLen; uint32_t i, j; uint32_t *mainSyscap = NULL; uint16_t priSyscapCount = 0; @@ -797,7 +684,7 @@ int32_t EncodePcidscToString(char *inputFile, char *outDirPath) } if (bufferLen > 1128) { // 1128, max size of pcid.sc - PRINT_ERR("Input pcid file too large, pcid file size: %zu\n", bufferLen); + PRINT_ERR("Input pcid file too large, pcid file size: %u\n", bufferLen); goto FREE_CONTEXT; } diff --git a/src/main.c b/src/main.c index c639b72..0447faa 100644 --- a/src/main.c +++ b/src/main.c @@ -22,12 +22,8 @@ #include "securec.h" #include "syscap_tool.h" #include "create_pcid.h" +#include "context_tool.h" -#define PRINT_ERR(...) \ - do { \ - printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \ - printf(__VA_ARGS__); \ - } while (0) #define SYSCAP_VERSION "2.0.1" #define OUTPUT_VERSION_LEN 200 #define ENCODE 0 diff --git a/src/syscap_tool.c b/src/syscap_tool.c index 85d62b1..5833f17 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -26,6 +26,7 @@ #include "cJSON.h" #include "create_pcid.h" #include "syscap_tool.h" +#include "context_tool.h" #ifdef SYSCAP_DEFINE_EXTERN_ENABLE #include "syscap_define_custom.h" @@ -33,11 +34,6 @@ #include "syscap_define.h" #endif -typedef struct RequiredProductCompatibilityIDHead { - uint16_t apiVersion : 15; - uint16_t apiVersionType : 1; -} RPCIDHead; - #define SYSCAP_PREFIX_LEN 17 #define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN) #define UINT8_BIT 8 @@ -48,129 +44,15 @@ typedef struct RequiredProductCompatibilityIDHead { #define U32_TO_STR_MAX_LEN 11 #define STRING_FORMAT_LEN_MAX 1024 -#define PRINT_ERR(...) \ - do { \ - printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \ - printf(__VA_ARGS__); \ - } while (0) - -static void FreeContextBuffer(char *contextBuffer) -{ - (void)free(contextBuffer); -} - -static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *bufferLen) -{ - int32_t ret; - FILE *fp = NULL; - struct stat statBuf; - char *contextBuffer = NULL; - char path[PATH_MAX + 1] = {0x00}; - -#ifdef _POSIX_ - if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) { - PRINT_ERR("get path(%s) failed\n", inputFile); - return -1; - } -#else - if (strlen(inputFile) > PATH_MAX || realpath(inputFile, path) == NULL) { - PRINT_ERR("get file(%s) real path failed\n", inputFile); - return -1; - } -#endif - - ret = stat(path, &statBuf); - if (ret != 0) { - PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno); - return -1; - } - if (!(statBuf.st_mode & S_IRUSR)) { - PRINT_ERR("don't have permission to read the file(%s)\n", path); - return -1; - } - contextBuffer = (char *)malloc(statBuf.st_size + 1); - if (contextBuffer == NULL) { - PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); - return -1; - } - fp = fopen(path, "rb"); - if (fp == NULL) { - PRINT_ERR("open file(%s) failed, errno = %d\n", path, errno); - FreeContextBuffer(contextBuffer); - return -1; - } - size_t retFread = fread(contextBuffer, statBuf.st_size, 1, fp); - if (retFread != 1) { - PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno); - FreeContextBuffer(contextBuffer); - (void)fclose(fp); - return -1; - } - contextBuffer[statBuf.st_size] = '\0'; - (void)fclose(fp); - - *contextBufPtr = contextBuffer; - *bufferLen = statBuf.st_size + 1; - return 0; -} - -static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, - char *convertedBuffer, size_t bufferLen) +static cJSON *CreateWholeSyscapJsonObj(void) { - int32_t ret; - FILE *fp = NULL; - char path[PATH_MAX + 1] = {0x00}; - -#ifdef _POSIX_ - if (strlen(outDirPath) >= PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) { - PRINT_ERR("get path(%s) failed\n", outDirPath); - return -1; - } -#else - if (strlen(outDirPath) >= PATH_MAX || realpath(outDirPath, path) == NULL) { - PRINT_ERR("get file(%s) real path failed\n", outDirPath); - return -1; - } -#endif - - int32_t pathLen = strlen(path); - if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') { - path[pathLen] = '/'; - } - - if (strlen(filename) + 1 > PATH_MAX) { - PRINT_ERR("filename(%s) too long.\n", filename); - return -1; - } - ret = strncat_s(path, PATH_MAX, filename, strlen(filename)); - if (ret != 0) { - PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n", - path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno); - return -1; - } - - fp = fopen(path, "wb"); - if (fp == NULL) { - PRINT_ERR("can`t create file(%s), errno = %d\n", path, errno); - return -1; - } - - size_t retFwrite = fwrite(convertedBuffer, bufferLen, 1, fp); - if (retFwrite != 1) { - PRINT_ERR("can`t write file(%s),errno = %d\n", path, errno); - (void)fclose(fp); - return -1; + cJSON *root = cJSON_CreateObject(); + if (root == NULL) { + PRINT_ERR("syscap-create jsonObj failed."); + return NULL; } - (void)fclose(fp); - - return 0; -} - -static cJSON *CreateWholeSyscapJsonObj(void) -{ size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); - cJSON *root = cJSON_CreateObject(); for (size_t i = 0; i < allSyscapNum; i++) { cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num)); } @@ -300,75 +182,37 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson char *temp = sysCapBegin + i * SINGLE_FEAT_LEN; if (strlen(temp) >= SINGLE_FEAT_LEN || strlen(temp) == 0) { PRINT_ERR("Get SysCap failed, string length(%u) error.\n", (uint32_t)strlen(temp)); - ret = -1; - goto FREE_SYSCAP_OUT; + cJSON_Delete(sysCapJson); + return -1; } char buffer[SINGLE_SYSCAP_LEN] = "SystemCapability."; - + ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LEN); if (ret != EOK) { PRINT_ERR("strncat_s failed.\n"); - goto FREE_SYSCAP_OUT; + cJSON_Delete(sysCapJson); + return ret; } if (!cJSON_AddItemToArray(sysCapJson, cJSON_CreateString(buffer))) { PRINT_ERR("Add syscap string to json failed.\n"); - ret = -1; - goto FREE_SYSCAP_OUT; + cJSON_Delete(sysCapJson); + return -1; } } if (!cJSON_AddNumberToObject(rpcidJson, "api_version", NtohsInter(rpcidHeader->apiVersion))) { PRINT_ERR("Add api_version to json failed.\n"); - ret = -1; - goto FREE_SYSCAP_OUT; + cJSON_Delete(sysCapJson); + return -1; } if (!cJSON_AddItemToObject(rpcidJson, "syscap", sysCapJson)) { PRINT_ERR("Add syscap to json failed.\n"); - ret = -1; - goto FREE_SYSCAP_OUT; - } - - return 0; -FREE_SYSCAP_OUT: - cJSON_Delete(sysCapJson); - return ret; -} - -static int32_t CheckRpcidFormat(char *inputFile, char **buffer, uint32_t *len) -{ - uint32_t bufferLen; - uint16_t sysCaptype, sysCapLength; - char *contextBuffer = NULL; - RPCIDHead *rpcidHeader = NULL; - - if (GetFileContext(inputFile, &contextBuffer, &bufferLen)) { - PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); - return -1; - } - if (bufferLen < (2 * sizeof(uint32_t))) { // 2, header of rpcid.sc - PRINT_ERR("Parse file failed(format is invalid), input file : %s\n", inputFile); - return -1; - } - rpcidHeader = (RPCIDHead *)contextBuffer; - if (rpcidHeader->apiVersionType != 1) { - PRINT_ERR("Parse file failed(apiVersionType != 1), input file : %s\n", inputFile); - 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); - 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); + cJSON_Delete(sysCapJson); return -1; } - *buffer = contextBuffer; - *len = bufferLen; - return 0; + return ret; } int32_t RPCIDDecode(char *inputFile, char *outputPath) @@ -611,8 +455,7 @@ int32_t GetPriSyscapData(char *input, char **priSyscap, uint32_t *priSyscapLen) char *tok = NULL; char *temp = strtok_r(input, ",", &tok); while (temp) { - int ret = strncpy_s(private, SINGLE_SYSCAP_LEN, - temp, SINGLE_SYSCAP_LEN - 1); + int ret = strncpy_s(private, SINGLE_SYSCAP_LEN, temp, SINGLE_SYSCAP_LEN - 1); if (ret != EOK) { PRINT_ERR("strncpy_s failed.\n"); free(priSysCapOut); diff --git a/test/unittest/common/BUILD.gn b/test/unittest/common/BUILD.gn index 8560596..9eded74 100644 --- a/test/unittest/common/BUILD.gn +++ b/test/unittest/common/BUILD.gn @@ -37,6 +37,7 @@ if (defined(ohos_lite)) { sources = [ "../../../interfaces/inner_api/syscap_interface.c", + "../../../src/context_tool.c", "../../../src/create_pcid.c", "../../../src/endian_internal.c", "../../../src/syscap_tool.c", @@ -78,6 +79,7 @@ if (defined(ohos_lite)) { sources = [ "../../../interfaces/inner_api/syscap_interface.c", + "../../../src/context_tool.c", "../../../src/create_pcid.c", "../../../src/endian_internal.c", "../../../src/syscap_tool.c", -- Gitee From 48e7bcce5851f3f47ae613329ea17630068b0bcd Mon Sep 17 00:00:00 2001 From: pengfojin Date: Fri, 1 Sep 2023 11:09:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=91=8A=E8=AD=A6=E6=B8=85=E7=90=86?= =?UTF-8?q?=EF=BC=9A=E9=87=8D=E5=A4=8D=E5=AE=9E=E7=8E=B0=E6=8F=90=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bundle.json | 3 +-- include/context_tool.h | 12 ++++++++++++ interfaces/inner_api/syscap_interface.c | 15 -------------- src/context_tool.c | 26 +++++++++++++++++++++++-- src/create_pcid.c | 22 ++++----------------- src/syscap_tool.c | 15 -------------- test/unittest/common/BUILD.gn | 1 - 7 files changed, 41 insertions(+), 53 deletions(-) diff --git a/bundle.json b/bundle.json index a84b959..30b7c9e 100644 --- a/bundle.json +++ b/bundle.json @@ -19,8 +19,7 @@ "ram": "0", "deps": { "components": [ - "napi", - "hiviewdfx_hilog_native" + "napi" ], "third_party": [ "bounds_checking_function", diff --git a/include/context_tool.h b/include/context_tool.h index 2b5c1c1..30e9d2e 100644 --- a/include/context_tool.h +++ b/include/context_tool.h @@ -1,3 +1,14 @@ + + + + + + + + + + + /* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); @@ -40,6 +51,7 @@ void FreeContextBuffer(char *contextBuffer); int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen); int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, char *convertedBuffer, size_t contextBufLen); int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len); +cJSON *CreateWholeSyscapJsonObj(void); #ifdef __cplusplus #if __cplusplus diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index 9bd9597..7ac6a93 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -251,21 +251,6 @@ static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, const uint16_t *inde return 0; } -static cJSON *CreateWholeSyscapJsonObj(void) -{ - cJSON *syscapJsonObj = cJSON_CreateObject(); - if (syscapJsonObj == NULL) { - PRINT_ERR("interface-create jsonObj failed."); - return NULL; - } - - size_t syscapNums = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); - for (size_t i = 0; i < syscapNums; i++) { - cJSON_AddItemToObject(syscapJsonObj, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num)); - } - return syscapJsonObj; -} - static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson) { uint32_t i; diff --git a/src/context_tool.c b/src/context_tool.c index d333329..8a2110a 100644 --- a/src/context_tool.c +++ b/src/context_tool.c @@ -20,10 +20,17 @@ #include #include #include +#include "cJSON.h" #include "securec.h" #include "endian_internal.h" +#ifdef SYSCAP_DEFINE_EXTERN_ENABLE +#include "syscap_define_custom.h" +#else +#include "syscap_define.h" +#endif + void FreeContextBuffer(char *contextBuffer) { if (contextBuffer != NULL) { @@ -93,12 +100,12 @@ int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, char char path[PATH_MAX + 1] = {0x00}; #ifdef _POSIX_ - if (strlen(outDirPath) > PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) { + if (strlen(outDirPath) >= PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) { PRINT_ERR("get path(%s) failed\n", outDirPath); return -1; } #else - if (strlen(outDirPath) > PATH_MAX || realpath(outDirPath, path) == NULL) { + if (strlen(outDirPath) >= PATH_MAX || realpath(outDirPath, path) == NULL) { PRINT_ERR("get file(%s) real path failed\n", outDirPath); return -1; } @@ -171,3 +178,18 @@ int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len) *len = bufferLen; return 0; } + +cJSON *CreateWholeSyscapJsonObj(void) +{ + cJSON *syscapJsonObj = cJSON_CreateObject(); + if (syscapJsonObj == NULL) { + PRINT_ERR("interface-create jsonObj failed."); + return NULL; + } + + size_t syscapNums = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); + for (size_t i = 0; i < syscapNums; i++) { + cJSON_AddItemToObject(syscapJsonObj, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num)); + } + return syscapJsonObj; +} diff --git a/src/create_pcid.c b/src/create_pcid.c index 136ae9e..701d594 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -40,20 +40,6 @@ #define U32_TO_STR_MAX_LEN 11 -static cJSON *CreateWholeSyscapJsonObj(void) -{ - size_t numOfSyscapAll = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); - cJSON *root = cJSON_CreateObject(); - if (root == NULL) { - PRINT_ERR("picd-create jsonObj failed."); - return NULL; - } - for (size_t i = 0; i < numOfSyscapAll; i++) { - cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num)); - } - return root; -} - int32_t SetOsSyscap(PCIDMain *pcidBuffer, uint32_t osCapSize, const cJSON *jsonOsSyscapObj, const cJSON *allOsSyscapObj) { @@ -201,7 +187,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) char *contextBuffer = NULL; cJSON *allOsSyscapObj = CreateWholeSyscapJsonObj(); - int32_t ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen); + int32_t ret = GetFileContext(inputFile, &contextBuffer, (uint32_t *)&contextBufLen); if (ret != 0) { PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); goto FREE_CONVERT_OUT; @@ -362,7 +348,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) char *contextBuffer = NULL; uint32_t contextBufLen; - ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen); + ret = GetFileContext(inputFile, &contextBuffer, (uint32_t *)&contextBufLen); if (ret != 0) { PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); return -1; @@ -616,7 +602,7 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) char *ctx = NULL; char *priSyscapStr = NULL; - if (GetFileContext(input, &ctx, &fileContextLen) != 0) { + if (GetFileContext(input, &ctx, (uint32_t *)&fileContextLen) != 0) { PRINT_ERR("GetFileContext failed, input file : %s\n", input); goto PARSE_FAILED; } @@ -677,7 +663,7 @@ int32_t EncodePcidscToString(char *inputFile, char *outDirPath) char *output = NULL; PCIDMain *pcidMain = NULL; - ret = GetFileContext(inputFile, &contextBuffer, &bufferLen); + ret = GetFileContext(inputFile, &contextBuffer, (uint32_t *)&bufferLen); if (ret != 0) { PRINT_ERR("Get pcid file failed, pcid file path: %s\n", inputFile); return -1; diff --git a/src/syscap_tool.c b/src/syscap_tool.c index 5833f17..a0c3882 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -44,21 +44,6 @@ #define U32_TO_STR_MAX_LEN 11 #define STRING_FORMAT_LEN_MAX 1024 -static cJSON *CreateWholeSyscapJsonObj(void) -{ - cJSON *root = cJSON_CreateObject(); - if (root == NULL) { - PRINT_ERR("syscap-create jsonObj failed."); - return NULL; - } - - size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); - for (size_t i = 0; i < allSyscapNum; i++) { - cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num)); - } - return root; -} - int32_t RPCIDEncode(char *inputFile, char *outputPath) { int32_t ret; diff --git a/test/unittest/common/BUILD.gn b/test/unittest/common/BUILD.gn index 9eded74..c2cfecc 100644 --- a/test/unittest/common/BUILD.gn +++ b/test/unittest/common/BUILD.gn @@ -47,7 +47,6 @@ if (defined(ohos_lite)) { defines = [ "NAPI_TEST" ] deps = [ - "//base//hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", "//build/lite/config/component/cJSON:cjson_static", "//test/testfwk/developer_test/third_party/lib/cpp:gtest_main", "//third_party/bounds_checking_function:libsec_static", -- Gitee