From e055ba209842f424755054bdb10617be9e7e6ae1 Mon Sep 17 00:00:00 2001 From: yudechen Date: Tue, 21 Jun 2022 11:18:20 +0800 Subject: [PATCH 1/2] add compare return information. Signed-off-by: yudechen Change-Id: Ia89a3c72a7334856c7083d4c9d4129ed032f95a3 --- BUILD.gn | 2 + include/syscap_tool.h | 2 + interfaces/inner_api/syscap_interface.c | 197 ++++++++---------- interfaces/inner_api/syscap_interface.h | 20 +- napi/napi_query_syscap.cpp | 5 +- src/syscap_tool.c | 4 +- test/unittest/common/BUILD.gn | 2 + .../common/include/syscap_codec_test.h | 1 - test/unittest/common/syscap_codec_test.cpp | 47 +++-- 9 files changed, 139 insertions(+), 141 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 8e0545d..4b40b7f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -107,6 +107,7 @@ if (defined(ohos_lite)) { sources = [ "./interfaces/inner_api/syscap_interface.c", "./src/endian_internal.c", + "./src/syscap_tool.c", ] deps = [ @@ -124,6 +125,7 @@ if (defined(ohos_lite)) { sources = [ "./interfaces/inner_api/syscap_interface.c", "./src/endian_internal.c", + "./src/syscap_tool.c", ] deps = [ "//third_party/bounds_checking_function:libsec_static", diff --git a/include/syscap_tool.h b/include/syscap_tool.h index 54ac106..42ff366 100644 --- a/include/syscap_tool.h +++ b/include/syscap_tool.h @@ -36,6 +36,8 @@ int32_t RPCIDDecode(char *inputFile, char *outputPath); int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath); /* in: pcidFile, rpcidFile */ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile); +int32_t SeparateSyscapFromString(char *input, uint32_t *osArray, uint32_t osArraySize, + char **priSyscap, uint32_t *priSyscapLen); #ifdef __cplusplus #if __cplusplus diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index 9c24f07..92cd54b 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -24,12 +24,13 @@ #include #include "cJSON.h" #include "syscap_define.h" +#include "syscap_tool.h" #include "endian_internal.h" #include "syscap_interface.h" #define OS_SYSCAP_BYTES 120 -#define PCID_MAIN_BYTES 128 -#define SINGLE_FEAT_LENGTH 128 +#define SYSCAP_PREFIX_LEN 17 +#define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN) #define RPCID_OUT_BUFFER 32 #define PCID_OUT_BUFFER RPCID_OUT_BUFFER #define UINT8_BIT 8 @@ -127,7 +128,7 @@ bool EncodeOsSyscap(char *output, int len) uint32_t bufferLen; if (len != PCID_MAIN_BYTES) { - PRINT_ERR("Os Syscap input len must be equal to 128.\n"); + PRINT_ERR("Os Syscap input len(%d) must be equal to 128.\n", len); return false; } @@ -184,7 +185,7 @@ bool EncodePrivateSyscap(char **output, int *outputLen) return true; } -bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt) +bool DecodeOsSyscap(char input[PCID_MAIN_BYTES], char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt) { errno_t nRet = 0; uint16_t indexOfSyscap[CHAR_BIT * OS_SYSCAP_BYTES] = {0}; @@ -202,21 +203,21 @@ bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt) } *outputCnt = countOfSyscap; - char (*strSyscap)[SINGLE_FEAT_LENGTH] = NULL; - strSyscap = (char (*)[SINGLE_FEAT_LENGTH])malloc(countOfSyscap * SINGLE_FEAT_LENGTH); + char (*strSyscap)[SINGLE_SYSCAP_LEN] = NULL; + strSyscap = (char (*)[SINGLE_SYSCAP_LEN])malloc(countOfSyscap * SINGLE_SYSCAP_LEN); if (strSyscap == NULL) { PRINT_ERR("malloc failed."); *outputCnt = 0; return false; } - (void)memset_s(strSyscap, countOfSyscap * SINGLE_FEAT_LENGTH, \ - 0, countOfSyscap * SINGLE_FEAT_LENGTH); + (void)memset_s(strSyscap, countOfSyscap * SINGLE_SYSCAP_LEN, \ + 0, countOfSyscap * SINGLE_SYSCAP_LEN); *output = strSyscap; for (i = 0; i < countOfSyscap; i++) { for (j = 0; j < sizeof(arraySyscap) / sizeof(SyscapWithNum); j++) { if (arraySyscap[j].num == indexOfSyscap[i]) { - nRet = strcpy_s(*strSyscap, SINGLE_FEAT_LENGTH, arraySyscap[j].syscapStr); + nRet = strcpy_s(*strSyscap, SINGLE_SYSCAP_LEN, arraySyscap[j].syscapStr); if (nRet != EOK) { printf("strcpy_s failed. error = %d\n", nRet); *outputCnt = 0; @@ -233,9 +234,9 @@ bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt) return true; } -bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt) +bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt) { - char (*outputArray)[SINGLE_FEAT_LENGTH] = NULL; + char (*outputArray)[SINGLE_SYSCAP_LEN] = NULL; char *inputPos = input; int bufferLen, ret; int syscapCnt = 0; @@ -248,8 +249,8 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt) } inputPos = input; - bufferLen = SINGLE_FEAT_LENGTH * syscapCnt; - outputArray = (char (*)[SINGLE_FEAT_LENGTH])malloc(bufferLen); + bufferLen = SINGLE_SYSCAP_LEN * syscapCnt; + outputArray = (char (*)[SINGLE_SYSCAP_LEN])malloc(bufferLen); if (outputArray == NULL) { PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", bufferLen, errno); *outputCnt = 0; @@ -258,12 +259,12 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt) (void)memset_s(outputArray, bufferLen, 0, bufferLen); *output = outputArray; - char buffer[SINGLE_FEAT_LENGTH - 17] = {0}; // 17. size of "SystemCapability." + char buffer[SINGLE_FEAT_LEN] = {0}; char *bufferPos = buffer; while (*inputPos != '\0') { if (*inputPos == ',') { *bufferPos = '\0'; - ret = sprintf_s(*outputArray, SINGLE_FEAT_LENGTH, "SystemCapability.%s", buffer); + ret = sprintf_s(*outputArray, SINGLE_SYSCAP_LEN, "SystemCapability.%s", buffer); if (ret == -1) { PRINT_ERR("sprintf_s failed\n"); *outputCnt = 0; @@ -319,20 +320,20 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson uint32_t i; int32_t ret = 0; uint16_t sysCapLength = NtohsInter(*(uint16_t *)(input + sizeof(uint32_t))); - uint16_t sysCapCount = sysCapLength / SINGLE_FEAT_LENGTH; + uint16_t sysCapCount = sysCapLength / SINGLE_FEAT_LEN; char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t); RPCIDHead *rpcidHeader = (RPCIDHead *)input; cJSON *sysCapJson = cJSON_CreateArray(); for (i = 0; i < sysCapCount; i++) { - char *temp = sysCapBegin + i * SINGLE_FEAT_LENGTH; - if (strlen(temp) >= SINGLE_FEAT_LENGTH) { + char *temp = sysCapBegin + i * SINGLE_FEAT_LEN; + if (strlen(temp) >= SINGLE_FEAT_LEN) { PRINT_ERR("Get SysCap failed, string length too long.\n"); ret = -1; goto FREE_SYSCAP_OUT; } - char buffer[SINGLE_FEAT_LENGTH + 17] = "SystemCapability."; // 17, sizeof "SystemCapability." + char buffer[SINGLE_SYSCAP_LEN] = "SystemCapability."; - ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LENGTH); + ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LEN); if (ret != EOK) { PRINT_ERR("strncat_s failed.\n"); goto FREE_SYSCAP_OUT; @@ -449,13 +450,13 @@ char *DecodeRpcidToStringFormat(char *inputFile) (void)memset_s(osSysCapIndex, sizeof(uint16_t) * sysCapArraySize, 0, sizeof(uint16_t) * sysCapArraySize); // malloc for save private syscap string - priSyscapArray = (char *)malloc(sysCapArraySize * SINGLE_FEAT_LENGTH); + priSyscapArray = (char *)malloc(sysCapArraySize * SINGLE_SYSCAP_LEN); if (priSyscapArray == NULL) { - PRINT_ERR("malloc(%u) failed.\n", (uint32_t)sysCapArraySize * SINGLE_FEAT_LENGTH); + PRINT_ERR("malloc(%u) failed.\n", (uint32_t)sysCapArraySize * SINGLE_SYSCAP_LEN); goto FREE_MALLOC_OSSYSCAP; } - (void)memset_s(priSyscapArray, sysCapArraySize * SINGLE_FEAT_LENGTH, - 0, sysCapArraySize * SINGLE_FEAT_LENGTH); + (void)memset_s(priSyscapArray, sysCapArraySize * SINGLE_SYSCAP_LEN, + 0, sysCapArraySize * SINGLE_SYSCAP_LEN); priSyscap = priSyscapArray; // part os syscap and ptivate syscap for (i = 0; i < (uint32_t)sysCapArraySize; i++) { @@ -464,13 +465,12 @@ char *DecodeRpcidToStringFormat(char *inputFile) if (cJsonTemp != NULL) { osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint); } else { - ret = strncpy_s(priSyscapArray, sysCapArraySize * SINGLE_FEAT_LENGTH, - cJsonItem->valuestring, SINGLE_FEAT_LENGTH - 1); + ret = strcpy_s(priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring); if (ret != EOK) { PRINT_ERR("strcpy_s failed.\n"); goto FREE_MALLOC_PRISYSCAP; } - priSyscapArray += SINGLE_FEAT_LENGTH; + priSyscapArray += SINGLE_SYSCAP_LEN; indexPri++; } } @@ -483,8 +483,8 @@ char *DecodeRpcidToStringFormat(char *inputFile) goto FREE_MALLOC_PRISYSCAP; } - uint32_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER - + (SINGLE_FEAT_LENGTH + 1) * (uint32_t)sysCapArraySize; + uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER + + SINGLE_SYSCAP_LEN * indexPri; outBuffer = (char *)malloc(outBufferLen); if (outBuffer == NULL) { PRINT_ERR("malloc(%u) failed.\n", outBufferLen); @@ -508,7 +508,8 @@ char *DecodeRpcidToStringFormat(char *inputFile) } for (i = 0; i < indexPri; i++) { - ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, priSyscap + i * SINGLE_FEAT_LENGTH); + ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, + priSyscapArray + i * SINGLE_SYSCAP_LEN); if (ret == -1) { PRINT_ERR("sprintf_s failed.\n"); outBuffer = NULL; @@ -529,71 +530,7 @@ FREE_CONTEXT_OUT: return outBuffer; } -static int32_t SeparateSyscapFromString(char *input, uint32_t *osArray, uint32_t osArraySize, - char **priSyscap, uint32_t *priSyscapLen) -{ - int32_t ret = 0; - uint32_t i, inputLen; - uint32_t count = 0; - char *temp = NULL; - char *tok = NULL; - char *private = NULL; - - if (osArraySize != PCID_OUT_BUFFER) { - return -1; - } - - inputLen = strlen(input); - for (i = 0; i < PCID_OUT_BUFFER; i++) { - ret = sscanf_s(input, "%u,%s", &osArray[i], input, inputLen); - if (ret == -1) { - PRINT_ERR("sscanf_s failed.\n"); - return -1; - } - } - - // count private syscap - if (*input == '\0') { - *priSyscap = 0; - *priSyscapLen = 0; - goto SKIP_PRI_SYSCAP; - } - for (i = 0; *(input + i) != '\0'; i++) { - if (*(input + i) == ',') { - count++; - } - } - count++; - // get private syscap string - char *priSysCapOut = (char *)malloc(SINGLE_FEAT_LENGTH * count); - if (priSysCapOut == NULL) { - PRINT_ERR("sscanf_s failed.\n"); - return -1; - } - (void)memset_s(priSysCapOut, SINGLE_FEAT_LENGTH * count, 0, SINGLE_FEAT_LENGTH * count); - private = priSysCapOut; - - temp = strtok_r(input, ",", &tok); - while (temp) { - ret = strncpy_s(priSysCapOut, SINGLE_FEAT_LENGTH * count, - temp, SINGLE_FEAT_LENGTH - 1); - if (ret != EOK) { - PRINT_ERR("strncpy_s failed.\n"); - free(priSysCapOut); - return -1; - } - temp = strtok_r(NULL, ",", &tok); - priSysCapOut += SINGLE_FEAT_LENGTH; - } - - *priSyscap = private; - *priSyscapLen = count; - -SKIP_PRI_SYSCAP: - return ret; -} - -bool ComparePcidString(char *pcidString, char *rpcidString) +int32_t ComparePcidString(char *pcidString, char *rpcidString, CompareError *result) { int32_t ret; int32_t versionFlag = 0; @@ -613,13 +550,14 @@ bool ComparePcidString(char *pcidString, char *rpcidString) &rpcidPriSyscap, &rpcidPriSyscapLen); if (ret != 0) { PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret); - return false; + return -1; } + result->missSyscapNum = 0; // compare version uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion); uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion); if (pcidVersion < rpcidVersion) { - PRINT_ERR("Pcid version(%u) less than rpcid version(%u).\n", pcidVersion, rpcidVersion); + result->targetApiVersion = rpcidVersion; versionFlag = 1; } // compare os sysscap @@ -631,32 +569,71 @@ bool ComparePcidString(char *pcidString, char *rpcidString) } for (uint8_t k = 0; k < INT_BIT; k++) { if (temp2 & (0x1 << k)) { - // 2, header of pcid & rpcid - printf("Missing: %s\n", arraySyscap[(i - 2) * INT_BIT + k].syscapStr); - ossyscapFlag += 1; + char *temp = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN); + if (temp == NULL) { + PRINT_ERR("malloc failed.\n"); + FreeCompareError(result); + return -1; + } + result->missSyscapNum++; + ret = strcpy_s(temp, sizeof(char) * SINGLE_SYSCAP_LEN, + arraySyscap[(i - 2) * INT_BIT + k].syscapStr); // 2, header of pcid & rpcid + if (ret != EOK) { + PRINT_ERR("strcpy_s failed.\n"); + FreeCompareError(result); + return -1; + } + result->syscap[ossyscapFlag++] = temp; } } } // compare pri syscap priSysFound = false; - for (i = 0; i < pcidPriSyscapLen; i++) { - for (j = 0; j < rpcidPriSyscapLen; j++) { - if (strcmp(pcidPriSyscap + SINGLE_FEAT_LENGTH * i, - rpcidPriSyscap + SINGLE_FEAT_LENGTH * j) == 0) { + for (i = 0; i < rpcidPriSyscapLen; i++) { + for (j = 0; j < pcidPriSyscapLen; j++) { + if (strcmp(rpcidPriSyscap + SINGLE_SYSCAP_LEN * i, + pcidPriSyscap + SINGLE_SYSCAP_LEN * j) == 0) { priSysFound = true; break; } } if (priSysFound != true) { - printf("Missing: %s\n", pcidPriSyscap + SINGLE_FEAT_LENGTH * i); - prisyscapFlag += 1; + char *temp = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN); + if (temp == NULL) { + PRINT_ERR("malloc failed.\n"); + FreeCompareError(result); + return -1; + } + result->missSyscapNum++; + ret = strcpy_s(temp, sizeof(char) * SINGLE_SYSCAP_LEN, + rpcidPriSyscap + SINGLE_SYSCAP_LEN * i); + if (ret != EOK) { + FreeCompareError(result); + PRINT_ERR("strcpy_s failed.\n"); + return -1; + } + result->syscap[ossyscapFlag + prisyscapFlag] = temp; + ++prisyscapFlag; } priSysFound = false; } - if (!versionFlag && !ossyscapFlag && !prisyscapFlag) { - return true; - } else { - return false; + if (versionFlag) { + ret |= 0x1 << 0; + } + if (ossyscapFlag || prisyscapFlag) { + ret |= 0x1 << 1; + } + return ret; +} + +int32_t FreeCompareError(CompareError *result) +{ + if (result == NULL) { + return 0; } + for (int i = 0; i < result->missSyscapNum; i++) { + free(result->syscap[i]); + } + return 0; } \ No newline at end of file diff --git a/interfaces/inner_api/syscap_interface.h b/interfaces/inner_api/syscap_interface.h index 36532f5..45f690f 100644 --- a/interfaces/inner_api/syscap_interface.h +++ b/interfaces/inner_api/syscap_interface.h @@ -19,18 +19,32 @@ #include #include +#define MAX_MISS_SYSCAP 512 +#define SINGLE_SYSCAP_LEN 256 +#define PCID_MAIN_BYTES 128 +#define E_OK 0 +#define E_APIVERSION 1 +#define E_SYSCAP 2 + #ifdef __cplusplus #if __cplusplus extern "C" { #endif /* __cplusplus */ #endif /* __cplusplus */ +typedef struct CompareErrorMessage { + char *syscap[MAX_MISS_SYSCAP]; + uint16_t missSyscapNum; + uint16_t targetApiVersion; +} CompareError; + bool EncodeOsSyscap(char *output, int len); -bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt); +bool DecodeOsSyscap(char input[PCID_MAIN_BYTES], char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt); bool EncodePrivateSyscap(char **output, int *outputLen); -bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt); +bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt); char *DecodeRpcidToStringFormat(char *inputFile); -bool ComparePcidString(char *pcidString, char *rpcidString); +int32_t ComparePcidString(char *pcidString, char *rpcidString, CompareError *result); +int32_t FreeCompareError(CompareError *result); #ifdef __cplusplus #if __cplusplus diff --git a/napi/napi_query_syscap.cpp b/napi/napi_query_syscap.cpp index 94df7bb..63a62f0 100644 --- a/napi/napi_query_syscap.cpp +++ b/napi/napi_query_syscap.cpp @@ -27,7 +27,6 @@ EXTERN_C_START constexpr size_t OS_SYSCAP_U32_NUM = 30; constexpr size_t PCID_MAIN_U32 = OS_SYSCAP_U32_NUM + 2; constexpr size_t U32_TO_STR_MAX_LEN = 11; -constexpr size_t SYSCAP_STR_MAX_LEN = 128; constexpr size_t PCID_MAIN_LEN = 128; constexpr size_t KEY_BUFFER_SIZE = 32; @@ -63,14 +62,14 @@ static char* getSystemCapability() bool retBool; int retError, priOutputLen, priCapArrayCnt, sumLen; int i = 0; - char osOutput[SYSCAP_STR_MAX_LEN] = {}; + char osOutput[SINGLE_SYSCAP_LEN] = {}; errno_t err = EOK; uint32_t *osCapU32 = nullptr; char *priOutput = nullptr; char *temp = nullptr; char *allSyscapBUffer = nullptr; char osCapArray[PCID_MAIN_U32][U32_TO_STR_MAX_LEN] = {}; - char (*priCapArray)[SYSCAP_STR_MAX_LEN] = nullptr; + char (*priCapArray)[SINGLE_SYSCAP_LEN] = nullptr; retBool = EncodeOsSyscap(osOutput, PCID_MAIN_LEN); if (!retBool) { diff --git a/src/syscap_tool.c b/src/syscap_tool.c index 404bf2a..29ba6a4 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -565,8 +565,8 @@ FREE_CONTEXT_OUT: return ret; } -static int32_t SeparateSyscapFromString(char *input, uint32_t *osArray, uint32_t osArraySize, - char **priSyscap, uint32_t *priSyscapLen) +int32_t SeparateSyscapFromString(char *input, uint32_t *osArray, uint32_t osArraySize, + char **priSyscap, uint32_t *priSyscapLen) { int32_t ret = 0; uint32_t i, inputLen; diff --git a/test/unittest/common/BUILD.gn b/test/unittest/common/BUILD.gn index 233b6df..0048ecf 100644 --- a/test/unittest/common/BUILD.gn +++ b/test/unittest/common/BUILD.gn @@ -36,6 +36,7 @@ if (defined(ohos_lite)) { "//developtools/syscap_codec/interfaces/inner_api/syscap_interface.c", "//developtools/syscap_codec/src/create_pcid.c", "//developtools/syscap_codec/src/endian_internal.c", + "//developtools/syscap_codec/src/syscap_tool.c", "syscap_codec_test.cpp", ] @@ -74,6 +75,7 @@ if (defined(ohos_lite)) { "//developtools/syscap_codec/interfaces/inner_api/syscap_interface.c", "//developtools/syscap_codec/src/create_pcid.c", "//developtools/syscap_codec/src/endian_internal.c", + "//developtools/syscap_codec/src/syscap_tool.c", "syscap_codec_test.cpp", ] diff --git a/test/unittest/common/include/syscap_codec_test.h b/test/unittest/common/include/syscap_codec_test.h index 25c016c..6c53be6 100644 --- a/test/unittest/common/include/syscap_codec_test.h +++ b/test/unittest/common/include/syscap_codec_test.h @@ -17,7 +17,6 @@ #define _SYSCAP_CODEC_TEST_H #include -#include "syscap_interface.h" #ifdef __cplusplus #if __cplusplus diff --git a/test/unittest/common/syscap_codec_test.cpp b/test/unittest/common/syscap_codec_test.cpp index 354f4d5..eb46e91 100644 --- a/test/unittest/common/syscap_codec_test.cpp +++ b/test/unittest/common/syscap_codec_test.cpp @@ -13,20 +13,17 @@ * limitations under the License. */ -#include"syscap_codec_test.h" #include +#include "syscap_interface.h" +#include "syscap_codec_test.h" using namespace testing::ext; using namespace std; namespace Syscap { -constexpr size_t SYSCAP_STR_LEN_MAX = 128; void SyscapCodecTest::SetUpTestCase() {} - void SyscapCodecTest::TearDownTestCase() {} - void SyscapCodecTest::SetUp() {} - void SyscapCodecTest::TearDown() {} /* @@ -36,8 +33,8 @@ void SyscapCodecTest::TearDown() {} */ HWTEST_F(SyscapCodecTest, EncodeOsSyscap, TestSize.Level1) { - int pcidLen = SYSCAP_STR_LEN_MAX; - char OsInput[SYSCAP_STR_LEN_MAX] = {0}; + int pcidLen = PCID_MAIN_BYTES; + char OsInput[SINGLE_SYSCAP_LEN] = {0}; EXPECT_TRUE(EncodeOsSyscap(OsInput, pcidLen)); } @@ -62,12 +59,12 @@ HWTEST_F(SyscapCodecTest, EncodePrivateSyscap, TestSize.Level1) HWTEST_F(SyscapCodecTest, DecodeOsSyscap, TestSize.Level1) { int osSyscap[32] = {1, 3, 3}; - char (*osOutput)[SYSCAP_STR_LEN_MAX] = NULL; + char (*osOutput)[SINGLE_SYSCAP_LEN] = NULL; int decodeOsCnt; char expectOsOutput001[] = "SystemCapability.Account.AppAccount"; char expectOsOutput002[] = "SystemCapability.Account.OsAccount"; EXPECT_TRUE(DecodeOsSyscap((char *)osSyscap, &osOutput, &decodeOsCnt)); - char (*tmpOsOutput)[SYSCAP_STR_LEN_MAX] = osOutput; + char (*tmpOsOutput)[SINGLE_SYSCAP_LEN] = osOutput; EXPECT_STREQ(*tmpOsOutput, expectOsOutput001); EXPECT_STREQ(*(tmpOsOutput + 1), expectOsOutput002); EXPECT_EQ(decodeOsCnt, 2); @@ -81,7 +78,7 @@ HWTEST_F(SyscapCodecTest, DecodeOsSyscap, TestSize.Level1) */ HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1) { - char (*priOutput)[SYSCAP_STR_LEN_MAX] = NULL; + char (*priOutput)[SINGLE_SYSCAP_LEN] = NULL; char priSyscap[] = "Device.syscap1GEDR,Device.syscap2WREGW,Vendor.syscap3RGD,Vendor.syscap4RWEG,Vendor.syscap5REWGWE,"; int decodePriCnt; char expectPriOutput001[] = "SystemCapability.Device.syscap1GEDR"; @@ -90,7 +87,7 @@ HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1) char expectPriOutput004[] = "SystemCapability.Vendor.syscap4RWEG"; char expectPriOutput005[] = "SystemCapability.Vendor.syscap5REWGWE"; EXPECT_TRUE(DecodePrivateSyscap(priSyscap, &priOutput, &decodePriCnt)); - char (*tmpPtiOutput)[SYSCAP_STR_LEN_MAX] = priOutput; + char (*tmpPtiOutput)[SINGLE_SYSCAP_LEN] = priOutput; EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput001); EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput002); EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput003); @@ -107,16 +104,22 @@ HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1) */ HWTEST_F(SyscapCodecTest, ComparePcidString, TestSize.Level1) { - char pcidString[] = "132096,385875968,12,17268736,2,344130,4,0,0,0,0,\ - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\ - SystemCapability.Device.xxx1,SystemCapability.Device.xxx2,\ - SystemCapability.Vendor.xxx1,SystemCapability.Vendor.xxx2"; - char rpcidString[] = "33588224,1665236995,4,262144,0,0,4,0,0,0,0,0,\ - 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\ - SystemCapability.Device.xxx1,\ - SystemCapability.Device.xxx2,\ - SystemCapability.Vendor.xxx1,\ - SystemCapability.Vendor.xxx2"; - EXPECT_TRUE(ComparePcidString(pcidString, rpcidString)); + CompareError result = {{0}, 0, 0}; + char pcidString[] = "263168,0,3473408,0,0,0,1634,0,0,0,0,0,0,0,0,0,0,\ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,SystemCapability.vendor.xxxxx3,\ + SystemCapability.device.xxxxx4"; + char rpcidString[] = "33588992,1766370052,65536,276824064,0,0,0,0,0,0,\ + 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\ + SystemCapability.vendor.xxxxx1,SystemCapability.device.xxxxx2"; + char expect[][256] = {"SystemCapability.HiviewDFX.HiLog", "SystemCapability.HiviewDFX.Hiview", + "SystemCapability.vendor.xxxxx1", "SystemCapability.device.xxxxx2"}; + int32_t ret = ComparePcidString(pcidString, rpcidString, &result); + EXPECT_EQ(ret, 3); + EXPECT_EQ(result.targetApiVersion, 7); + EXPECT_EQ(result.missSyscapNum, 4); + for (int i = 0; i < result.missSyscapNum; i++) { + EXPECT_STREQ(result.syscap[i], expect[i]); + } + (void)FreeCompareError(&result); } } // namespace Syscap -- Gitee From 8372015f1870651339262471a5ea8686e866fc17 Mon Sep 17 00:00:00 2001 From: yudechen Date: Thu, 23 Jun 2022 19:08:54 +0800 Subject: [PATCH 2/2] Uniform syscap length. Signed-off-by: yudechen Change-Id: I68c43b300e2a729aae0a1aa7a287ee1916b1c5a1 --- include/create_pcid.h | 4 +-- include/syscap_define.h | 6 +++- interfaces/inner_api/syscap_interface.c | 7 ++++- interfaces/inner_api/syscap_interface.h | 6 +++- napi/napi_query_syscap.cpp | 3 +- src/create_pcid.c | 40 ++++++++++++------------- src/main.c | 14 ++++----- src/syscap_tool.c | 1 - 8 files changed, 46 insertions(+), 35 deletions(-) diff --git a/include/create_pcid.h b/include/create_pcid.h index 8f491de..7262e7e 100644 --- a/include/create_pcid.h +++ b/include/create_pcid.h @@ -18,7 +18,7 @@ #include -#define MAX_OS_SYSCAP_NUM 960 +#define OS_SYSCAP_BYTES 120 #define TYPE_FILE 1 #define TYPE_STRING 2 @@ -28,7 +28,7 @@ typedef struct ProductCompatibilityID { uint16_t systemType : 3; uint16_t reserved : 13; uint32_t manufacturerID; - uint8_t osSyscap[MAX_OS_SYSCAP_NUM / 8]; + uint8_t osSyscap[OS_SYSCAP_BYTES]; } PCIDMain; typedef struct pcidHeader { diff --git a/include/syscap_define.h b/include/syscap_define.h index d7ceb8f..c30fd92 100644 --- a/include/syscap_define.h +++ b/include/syscap_define.h @@ -16,8 +16,12 @@ #ifndef _SYSCAP_DEFINE_H #define _SYSCAP_DEFINE_H +#include + +#define SINGLE_SYSCAP_LEN (256 + 17) + typedef struct SystemCapabilityWithNum { - char syscapStr[128]; + char syscapStr[SINGLE_SYSCAP_LEN]; uint16_t num; } SyscapWithNum; diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index 92cd54b..b543f9d 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -247,8 +247,13 @@ bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *o } inputPos++; } - inputPos = input; + if (syscapCnt == 0) { + *output = outputArray; + *outputCnt = syscapCnt; + return true; + } + inputPos = input; bufferLen = SINGLE_SYSCAP_LEN * syscapCnt; outputArray = (char (*)[SINGLE_SYSCAP_LEN])malloc(bufferLen); if (outputArray == NULL) { diff --git a/interfaces/inner_api/syscap_interface.h b/interfaces/inner_api/syscap_interface.h index 45f690f..433f573 100644 --- a/interfaces/inner_api/syscap_interface.h +++ b/interfaces/inner_api/syscap_interface.h @@ -19,9 +19,13 @@ #include #include +#ifndef SINGLE_SYSCAP_LEN +#define SINGLE_SYSCAP_LEN (256 + 17) +#endif // SINGLE_SYSCAP_LEN #define MAX_MISS_SYSCAP 512 -#define SINGLE_SYSCAP_LEN 256 #define PCID_MAIN_BYTES 128 +#define PCID_MAIN_INTS 32 + #define E_OK 0 #define E_APIVERSION 1 #define E_SYSCAP 2 diff --git a/napi/napi_query_syscap.cpp b/napi/napi_query_syscap.cpp index 63a62f0..8228f6e 100644 --- a/napi/napi_query_syscap.cpp +++ b/napi/napi_query_syscap.cpp @@ -27,7 +27,6 @@ EXTERN_C_START constexpr size_t OS_SYSCAP_U32_NUM = 30; constexpr size_t PCID_MAIN_U32 = OS_SYSCAP_U32_NUM + 2; constexpr size_t U32_TO_STR_MAX_LEN = 11; -constexpr size_t PCID_MAIN_LEN = 128; constexpr size_t KEY_BUFFER_SIZE = 32; #define PRINT_ERR(...) \ @@ -71,7 +70,7 @@ static char* getSystemCapability() char osCapArray[PCID_MAIN_U32][U32_TO_STR_MAX_LEN] = {}; char (*priCapArray)[SINGLE_SYSCAP_LEN] = nullptr; - retBool = EncodeOsSyscap(osOutput, PCID_MAIN_LEN); + retBool = EncodeOsSyscap(osOutput, PCID_MAIN_BYTES); if (!retBool) { PRINT_ERR("get encoded os syscap failed."); return nullptr; diff --git a/src/create_pcid.c b/src/create_pcid.c index fadf9e1..38aa53f 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -30,12 +30,12 @@ #include "syscap_define.h" #include "create_pcid.h" -#define SINGLE_FEAT_LENGTH (32 * 8) -#define PER_SYSCAP_LEN_MAX 128 +#define SYSCAP_PREFIX_LEN 17 +#define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN) #define PCID_OUT_BUFFER 32 #define PRIVATE_SYSCAP_SIZE 1000 #define UINT8_BIT 8 -#define BYTES_OF_OS_SYSCAP 120 + #define U32_TO_STR_MAX_LEN 11 #define PRINT_ERR(...) \ @@ -264,7 +264,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) } sectorOfBits = (osCapIndex->valueint) / UINT8_BIT; posOfBits = (osCapIndex->valueint) % UINT8_BIT; - if (sectorOfBits >= BYTES_OF_OS_SYSCAP) { + if (sectorOfBits >= OS_SYSCAP_BYTES) { PRINT_ERR("num of \"os syscap\" is out of 960\n"); ret = -1; goto FREE_PCID_BUFFER_OUT; @@ -341,8 +341,8 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) int32_t ret; errno_t nRet = 0; char *contextBuffer = NULL; - uint8_t osSyscap[BYTES_OF_OS_SYSCAP] = {0}; - uint16_t indexOfSyscap[BYTES_OF_OS_SYSCAP * UINT8_BIT] = {0}; + uint8_t osSyscap[OS_SYSCAP_BYTES] = {0}; + uint16_t indexOfSyscap[OS_SYSCAP_BYTES * UINT8_BIT] = {0}; uint32_t i, j, contextBufLen, countOfSyscap = 0; ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen); @@ -369,7 +369,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) ret = -1; goto FREE_CONTEXT_OUT; } - + cJSON *capVectorPtr = cJSON_CreateArray(); if (capVectorPtr == NULL) { PRINT_ERR("cJSON_CreateArray failed\n"); @@ -377,13 +377,13 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) goto FREE_CONTEXT_OUT; } - nRet = memcpy_s(osSyscap, BYTES_OF_OS_SYSCAP, (uint8_t *)pcidMain + 8, BYTES_OF_OS_SYSCAP); // 8, bytes of pcid header + nRet = memcpy_s(osSyscap, OS_SYSCAP_BYTES, (uint8_t *)pcidMain + 8, OS_SYSCAP_BYTES); // 8, bytes of pcid header if (EOK != nRet) { PRINT_ERR("memcpy_s failed."); ret = -1; goto FREE_VECTOR_OUT; } - for (i = 0; i < BYTES_OF_OS_SYSCAP; i++) { + for (i = 0; i < OS_SYSCAP_BYTES; i++) { for (j = 0; j < UINT8_BIT; j++) { if (osSyscap[i] & (0x01 << j)) { indexOfSyscap[countOfSyscap++] = i * UINT8_BIT + j; @@ -423,9 +423,9 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) char *ptrPrivateSyscap = (char *)(pcidMain + 1); uint16_t privateSyscapLen = contextBufLen - sizeof(PCIDMain) - 1; - char priSyscapStr[PER_SYSCAP_LEN_MAX] = {0}; + char priSyscapStr[SINGLE_SYSCAP_LEN] = {0}; char *tempPriSyscapStr = priSyscapStr; - char fullPriSyscapStr[PER_SYSCAP_LEN_MAX] = {0}; + char fullPriSyscapStr[SINGLE_SYSCAP_LEN] = {0}; if (privateSyscapLen < 0) { PRINT_ERR("parse private syscap failed."); ret = -1; @@ -437,7 +437,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) while (*ptrPrivateSyscap != '\0') { if (*ptrPrivateSyscap == ',') { *tempPriSyscapStr = '\0'; - ret = sprintf_s(fullPriSyscapStr, PER_SYSCAP_LEN_MAX, "SystemCapability.%s", priSyscapStr); + ret = sprintf_s(fullPriSyscapStr, SINGLE_SYSCAP_LEN, "SystemCapability.%s", priSyscapStr); if (ret == -1) { printf("sprintf_s failed\n"); goto FREE_VECTOR_OUT; @@ -594,8 +594,8 @@ static int32_t AddOsSyscapToJsonObj(uint32_t *osSyscapArray, uint32_t osSyscapAr uint32_t i, j; uint32_t osSyscapCount = 0; - uint16_t index[BYTES_OF_OS_SYSCAP * UINT8_BIT] = {0}; - for (i = 0; i < BYTES_OF_OS_SYSCAP; i++) { + uint16_t index[OS_SYSCAP_BYTES * UINT8_BIT] = {0}; + for (i = 0; i < OS_SYSCAP_BYTES; i++) { for (j = 0; j < UINT8_BIT; j++) { if (osSysCapArrayUint8[i] & (0x01 << j)) { index[osSyscapCount++] = i * UINT8_BIT + j; @@ -761,19 +761,19 @@ int32_t EncodePcidscToString(char *inputFile, char *outDirPath) if (priSyscapCount == 0) { goto OUT_PUT; } - priSyscapFull = (char *)malloc(priSyscapCount * SINGLE_FEAT_LENGTH); + priSyscapFull = (char *)malloc(priSyscapCount * SINGLE_SYSCAP_LEN); if (priSyscapFull == NULL) { PRINT_ERR("malloc failed\n"); goto FREE_PRISYSCAP_FULL; } - (void)memset_s(priSyscapFull, priSyscapCount * SINGLE_FEAT_LENGTH, - 0, priSyscapCount * SINGLE_FEAT_LENGTH); - char tempSyscap[SINGLE_FEAT_LENGTH] = {0}; + (void)memset_s(priSyscapFull, priSyscapCount * SINGLE_SYSCAP_LEN, + 0, priSyscapCount * SINGLE_SYSCAP_LEN); + char tempSyscap[SINGLE_SYSCAP_LEN] = {0}; char *temp = tempSyscap; for (i = 0, j = 0; i < privateSyscapLen; i++) { if (*privateSyscap == ',') { *temp = '\0'; - ret = sprintf_s(priSyscapFull + j * SINGLE_FEAT_LENGTH, SINGLE_FEAT_LENGTH, + ret = sprintf_s(priSyscapFull + j * SINGLE_SYSCAP_LEN, SINGLE_SYSCAP_LEN, "SystemCapability.%s", tempSyscap); if (ret == -1) { PRINT_ERR("sprintf_s failed\n"); @@ -809,7 +809,7 @@ OUT_PUT: } } for (i = 0; i < priSyscapCount; i++) { - ret = sprintf_s(output, outputLen, "%s,%s", output, priSyscapFull + i * SINGLE_FEAT_LENGTH); + ret = sprintf_s(output, outputLen, "%s,%s", output, priSyscapFull + i * SINGLE_SYSCAP_LEN); if (ret == -1) { PRINT_ERR("sprintf_s failed\n"); goto FREE_OUTPUT; diff --git a/src/main.c b/src/main.c index af20a34..760f67e 100644 --- a/src/main.c +++ b/src/main.c @@ -125,19 +125,19 @@ int main(int argc, char **argv) ret = RPCIDEncode(inputfile, outputpath); break; case 0x10A: // 0x10A, -Rdi inputfile ret = RPCIDDecode(inputfile, outputpath); break; - case 0x10D: + case 0x10D: // 0x10D, -Resi inputfile ret = EncodeRpcidscToString(inputfile, outputpath); break; - case 0x115: + case 0x115: // 0x115, -Pesi inputfile ret = EncodePcidscToString(inputfile, outputpath); break; - case 0x60: + case 0x60: // 0x60, -C PCID.txt RPCID.txt ret = ComparePcidWithRpcidString(pcidfile, rpcidfile); break; - case 0x111: + case 0x111: // 0x111, -Pei inputfile ret = CreatePCID(inputfile, outputpath); break; - case 0x112: + case 0x112: // 0x112, -Pdi inputfile ret = DecodePCID(inputfile, outputpath); break; - case 0x116: + case 0x116: // 0x116, -Pdsi inputfile ret = DecodeStringPCIDToJson(inputfile, outputpath, TYPE_FILE); break; - case 0x80: + case 0x80: // 0x80, -v (void)OutputVersion(argv[optind], optind); break; default: (void)OutputHelp(); diff --git a/src/syscap_tool.c b/src/syscap_tool.c index 29ba6a4..9fba6b9 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -44,7 +44,6 @@ typedef struct RequiredProductCompatibilityIDHead { uint16_t apiVersionType : 1; } RPCIDHead; -#define SINGLE_SYSCAP_LEN 256 #define SYSCAP_PREFIX_LEN 17 #define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN) #define UINT8_BIT 8 -- Gitee