diff --git a/BUILD.gn b/BUILD.gn index 630ec5883ce355e987d43b5e3048481d02f3a709..8e0545db127896914240f14d8e756ffc4700e686 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -99,9 +99,15 @@ config("syscap_interface_public_config") { if (defined(ohos_lite)) { shared_library("syscap_interface_shared") { - include_dirs = [ "include" ] + include_dirs = [ + "include", + "src", + ] public_configs = [ ":syscap_interface_public_config" ] - sources = [ "./interfaces/inner_api/syscap_interface.c" ] + sources = [ + "./interfaces/inner_api/syscap_interface.c", + "./src/endian_internal.c", + ] deps = [ "//build/lite/config/component/cJSON:cjson_static", @@ -110,10 +116,15 @@ if (defined(ohos_lite)) { } } else { ohos_shared_library("syscap_interface_shared") { - include_dirs = [ "include" ] + include_dirs = [ + "include", + "src", + ] public_configs = [ ":syscap_interface_public_config" ] - sources = [ "./interfaces/inner_api/syscap_interface.c" ] - + sources = [ + "./interfaces/inner_api/syscap_interface.c", + "./src/endian_internal.c", + ] deps = [ "//third_party/bounds_checking_function:libsec_static", "//third_party/cJSON:cjson_static", diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index bde2735de5dc838b1484e2c9e0afb4c0ddfafe56..f5a53ad91aa1712ad6f32fa4c4a8e49e80eee505 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -22,39 +22,75 @@ #include #include #include +#include "cJSON.h" #include "syscap_define.h" +#include "endian_internal.h" #include "syscap_interface.h" #define OS_SYSCAP_BYTES 120 #define PCID_MAIN_BYTES 128 -#define SYSCAP_STR_LEN_MAX 128 +#define SINGLE_FEAT_LENGTH 128 +#define RPCID_OUT_BUFFER 32 +#define PCID_OUT_BUFFER RPCID_OUT_BUFFER +#define UINT8_BIT 8 +#define INT_BIT 32 +#define U32_TO_STR_MAX_LEN 11 + #define PRINT_ERR(...) \ do { \ - printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); \ + 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; + uint16_t systemType : 3; + uint16_t reserved : 13; + uint32_t manufacturerID; + uint8_t osSyscap[OS_SYSCAP_BYTES]; +} PCIDMain; + +static const char *pcidPath = "/system/etc/PCID.sc"; + static void FreeContextBuffer(char *contextBuffer) { (void)free(contextBuffer); } -static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) +static int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen) { int32_t ret; FILE *fp = NULL; struct stat statBuf; char *contextBuffer = NULL; - const char *inputFile = "/system/etc/PCID.sc"; + 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(inputFile, &statBuf); + ret = stat(path, &statBuf); if (ret != 0) { - PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", inputFile, errno); + 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", inputFile); + PRINT_ERR("don't have permission to read the file(%s)\n", path); return -1; } contextBuffer = (char *)malloc(statBuf.st_size + 1); @@ -62,16 +98,15 @@ static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen) PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno); return -1; } - - fp = fopen(inputFile, "rb"); + fp = fopen(path, "rb"); if (fp == NULL) { - PRINT_ERR("open file(%s) failed, errno = %d\n", inputFile, errno); + 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", inputFile, errno); + PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno); FreeContextBuffer(contextBuffer); (void)fclose(fp); return -1; @@ -96,7 +131,7 @@ bool EncodeOsSyscap(char *output, int len) return false; } - ret = GetFileContext(&contextBuffer, &bufferLen); + ret = GetFileContext(pcidPath, &contextBuffer, &bufferLen); if (ret != 0) { PRINT_ERR("GetFileContext failed, input file : /system/etc/PCID.sc\n"); return false; @@ -120,7 +155,7 @@ bool EncodePrivateSyscap(char **output, int *outputLen) char *outputStr = NULL; uint32_t bufferLen; - ret = GetFileContext(&contextBuffer, &bufferLen); + ret = GetFileContext(pcidPath, &contextBuffer, &bufferLen); if (ret != 0) { PRINT_ERR("GetFileContext failed, input file : /system/etc/PCID.sc\n"); return false; @@ -167,21 +202,21 @@ bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt) } *outputCnt = countOfSyscap; - char (*strSyscap)[SYSCAP_STR_LEN_MAX] = NULL; - strSyscap = (char (*)[SYSCAP_STR_LEN_MAX])malloc(countOfSyscap * SYSCAP_STR_LEN_MAX); + char (*strSyscap)[SINGLE_FEAT_LENGTH] = NULL; + strSyscap = (char (*)[SINGLE_FEAT_LENGTH])malloc(countOfSyscap * SINGLE_FEAT_LENGTH); if (strSyscap == NULL) { PRINT_ERR("malloc failed."); *outputCnt = 0; return false; } - (void)memset_s(strSyscap, countOfSyscap * SYSCAP_STR_LEN_MAX, \ - 0, countOfSyscap * SYSCAP_STR_LEN_MAX); + (void)memset_s(strSyscap, countOfSyscap * SINGLE_FEAT_LENGTH, \ + 0, countOfSyscap * SINGLE_FEAT_LENGTH); *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, SYSCAP_STR_LEN_MAX, arraySyscap[j].syscapStr); + nRet = strcpy_s(*strSyscap, SINGLE_FEAT_LENGTH, arraySyscap[j].syscapStr); if (nRet != EOK) { printf("strcpy_s failed. error = %d\n", nRet); *outputCnt = 0; @@ -200,7 +235,7 @@ bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt) bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt) { - char (*outputArray)[SYSCAP_STR_LEN_MAX] = NULL; + char (*outputArray)[SINGLE_FEAT_LENGTH] = NULL; char *inputPos = input; int bufferLen, ret; int syscapCnt = 0; @@ -213,8 +248,8 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt) } inputPos = input; - bufferLen = SYSCAP_STR_LEN_MAX * syscapCnt; - outputArray = (char (*)[SYSCAP_STR_LEN_MAX])malloc(bufferLen); + bufferLen = SINGLE_FEAT_LENGTH * syscapCnt; + outputArray = (char (*)[SINGLE_FEAT_LENGTH])malloc(bufferLen); if (outputArray == NULL) { PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", bufferLen, errno); *outputCnt = 0; @@ -223,12 +258,12 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt) (void)memset_s(outputArray, bufferLen, 0, bufferLen); *output = outputArray; - char buffer[SYSCAP_STR_LEN_MAX - 17] = {0}; // 17. size of "SystemCapability." + char buffer[SINGLE_FEAT_LENGTH - 17] = {0}; // 17. size of "SystemCapability." char *bufferPos = buffer; while (*inputPos != '\0') { if (*inputPos == ',') { *bufferPos = '\0'; - ret = sprintf_s(*outputArray, SYSCAP_STR_LEN_MAX, "SystemCapability.%s", buffer); + ret = sprintf_s(*outputArray, SINGLE_FEAT_LENGTH, "SystemCapability.%s", buffer); if (ret == -1) { PRINT_ERR("sprintf_s failed\n"); *outputCnt = 0; @@ -246,4 +281,382 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt) *outputCnt = syscapCnt; return true; +} + +static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, uint16_t *index, uint16_t indexLen) +{ + uint16_t sector, pos; + + if (outLen != OS_SYSCAP_BYTES) { + PRINT_ERR("Input array error.\n"); + return -1; + } + + for (uint16_t i = 0; i < indexLen; i++) { + sector = index[i] / UINT8_BIT; + pos = index[i] % UINT8_BIT; + if (sector >= OS_SYSCAP_BYTES) { + PRINT_ERR("Syscap num(%u) out of range(120).\n", sector); + return -1; + } + out[sector] |= (1 << pos); + } + return 0; +} + +static cJSON *CreateWholeSyscapJsonObj(void) +{ + size_t numOfSyscapAll = sizeof(arraySyscap) / sizeof(SyscapWithNum); + cJSON *root = cJSON_CreateObject(); + for (size_t i = 0; i < numOfSyscapAll; i++) { + cJSON_AddItemToObject(root, arraySyscap[i].syscapStr, cJSON_CreateNumber(arraySyscap[i].num)); + } + return root; +} + +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; + 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) { + 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." + + ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LENGTH); + if (ret != EOK) { + PRINT_ERR("strncat_s failed.\n"); + goto FREE_SYSCAP_OUT; + } + + if (!cJSON_AddItemToArray(sysCapJson, cJSON_CreateString(buffer))) { + PRINT_ERR("Add syscap string to json failed.\n"); + ret = -1; + goto FREE_SYSCAP_OUT; + } + } + + if (!cJSON_AddNumberToObject(rpcidJson, "api_version", NtohsInter(rpcidHeader->apiVersion))) { + PRINT_ERR("Add api_version to json failed.\n"); + ret = -1; + goto FREE_SYSCAP_OUT; + } + 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); + return -1; + } + + *Buffer = contextBuffer; + *Len = bufferLen; + return 0; +} + +char *DecodeRpcidToStringFormat(char *inputFile) +{ + int32_t ret = 0; + int32_t sysCapArraySize; + uint32_t bufferLen, i; + uint16_t indexOs = 0; + uint16_t indexPri = 0; + uint16_t *osSysCapIndex; + char *contextBuffer = NULL; + char *priSyscapArray = NULL; + char *priSyscap = NULL; + char *outBuffer = NULL; + cJSON *cJsonTemp = NULL; + cJSON *rpcidRoot = NULL; + cJSON *sysCapDefine = NULL; + cJSON *sysCapArray = NULL; + + // check rpcid.sc + if (CheckRpcidFormat(inputFile, &contextBuffer, &bufferLen)) { + PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile); + goto FREE_CONTEXT_OUT; + } + + // parse rpcid to json + rpcidRoot = cJSON_CreateObject(); + if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot)) { + PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile); + goto FREE_RPCID_ROOT; + } + + // trans to string format + sysCapDefine = CreateWholeSyscapJsonObj(); + sysCapArray = cJSON_GetObjectItem(rpcidRoot, "syscap"); + if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) { + PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile); + goto FREE_WHOLE_SYSCAP; + } + sysCapArraySize = cJSON_GetArraySize(sysCapArray); + if (sysCapArraySize < 0) { + PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile); + goto FREE_WHOLE_SYSCAP; + } + // malloc for save os syscap index + osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) * sysCapArraySize); + if (osSysCapIndex == NULL) { + PRINT_ERR("malloc failed.\n"); + goto FREE_WHOLE_SYSCAP; + } + (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); + if (priSyscapArray == NULL) { + PRINT_ERR("malloc(%u) failed.\n", sysCapArraySize * SINGLE_FEAT_LENGTH); + goto FREE_MALLOC_OSSYSCAP; + } + (void)memset_s(priSyscapArray, sysCapArraySize * SINGLE_FEAT_LENGTH, + 0, sysCapArraySize * SINGLE_FEAT_LENGTH); + priSyscap = priSyscapArray; + // part os syscap and ptivate syscap + for (i = 0; i < (uint32_t)sysCapArraySize; i++) { + cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i); + cJsonTemp = cJSON_GetObjectItem(sysCapDefine, cJsonItem->valuestring); + if (cJsonTemp != NULL) { + osSysCapIndex[indexOs++] = cJsonTemp->valueint; + } else { + ret = strncpy_s(priSyscapArray, sysCapArraySize * SINGLE_FEAT_LENGTH, + cJsonItem->valuestring, SINGLE_FEAT_LENGTH - 1); + if (ret != EOK) { + PRINT_ERR("strcpy_s failed.\n"); + goto FREE_MALLOC_PRISYSCAP; + } + priSyscapArray += SINGLE_FEAT_LENGTH; + indexPri++; + } + } + uint32_t outUint[RPCID_OUT_BUFFER] = {0}; + outUint[0] = *(uint32_t *)contextBuffer; + outUint[1] = *(uint32_t *)(contextBuffer + sizeof(uint32_t)); + uint8_t *osOutUint = (uint8_t *)(outUint + 2); + if (SetOsSysCapBitMap(osOutUint, 120, osSysCapIndex, indexOs)) { // 120, len of osOutUint + PRINT_ERR("Set os syscap bit map failed.\n"); + goto FREE_MALLOC_PRISYSCAP; + } + + uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER + + (SINGLE_FEAT_LENGTH + 1) * sysCapArraySize; + outBuffer = (char *)malloc(outBufferLen); + if (outBuffer == NULL) { + PRINT_ERR("malloc(%u) failed.\n", outBufferLen); + goto FREE_MALLOC_PRISYSCAP; + } + (void)memset_s(outBuffer, outBufferLen, 0, outBufferLen); + + ret = sprintf_s(outBuffer, outBufferLen, "%u", outUint[0]); + if (ret == -1) { + PRINT_ERR("sprintf_s failed.\n"); + outBuffer = NULL; + goto FREE_MALLOC_PRISYSCAP; + } + for (i = 1; i < RPCID_OUT_BUFFER; i++) { + ret = sprintf_s(outBuffer, outBufferLen, "%s,%u", outBuffer, outUint[i]); + if (ret == -1) { + PRINT_ERR("sprintf_s failed.\n"); + outBuffer = NULL; + goto FREE_MALLOC_PRISYSCAP; + } + } + + for (i = 0; i < indexPri; i++) { + ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, priSyscap + i * SINGLE_FEAT_LENGTH); + if (ret == -1) { + PRINT_ERR("sprintf_s failed.\n"); + outBuffer = NULL; + goto FREE_MALLOC_PRISYSCAP; + } + } + +FREE_MALLOC_PRISYSCAP: + free(priSyscap); +FREE_MALLOC_OSSYSCAP: + free(osSysCapIndex); +FREE_WHOLE_SYSCAP: + cJSON_Delete(sysCapDefine); +FREE_RPCID_ROOT: + cJSON_Delete(rpcidRoot); +FREE_CONTEXT_OUT: + FreeContextBuffer(contextBuffer); + 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 ret; + int32_t versionFlag = 0; + int32_t ossyscapFlag = 0; + int32_t prisyscapFlag = 0; + char *pcidPriSyscap = NULL; + char *rpcidPriSyscap = NULL; + bool priSysFound; + uint32_t pcidPriSyscapLen, rpcidPriSyscapLen; + uint32_t i, j, temp1, temp2; + uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0}; + uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0}; + + ret = SeparateSyscapFromString(pcidString, pcidOsAarry, PCID_OUT_BUFFER, + &pcidPriSyscap, &pcidPriSyscapLen); + ret += SeparateSyscapFromString(rpcidString, rpcidOsAarry, RPCID_OUT_BUFFER, + &rpcidPriSyscap, &rpcidPriSyscapLen); + if (ret != 0) { + PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret); + return false; + } + // 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); + versionFlag = 1; + } + // compare os sysscap + for (i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid + temp1 = pcidOsAarry[i] ^ rpcidOsAarry[i]; + temp2 = temp1 & rpcidOsAarry[i]; + if (!temp2) { + continue; + } + for (uint8_t k = 0; k < INT_BIT; k++) { + if (temp2 & (0x1 << k)) { + // 2, header of pcid & rpcid + printf("Miss: %s\n", arraySyscap[(i - 2) * INT_BIT + k].syscapStr); + ossyscapFlag += 1; + } + } + } + // 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) { + priSysFound = true; + break; + } + } + if (priSysFound != true) { + printf("Miss: %s\n", pcidPriSyscap + SINGLE_FEAT_LENGTH * i); + prisyscapFlag += 1; + } + priSysFound = false; + } + + if (!versionFlag && !ossyscapFlag && !prisyscapFlag) { + return true; + } else { + return false; + } } \ No newline at end of file diff --git a/interfaces/inner_api/syscap_interface.h b/interfaces/inner_api/syscap_interface.h index 6267de670553ff4bebce732f30b98fc002f84437..36532f57f73099f400c84071dd34e1ebb38323b8 100644 --- a/interfaces/inner_api/syscap_interface.h +++ b/interfaces/inner_api/syscap_interface.h @@ -29,6 +29,8 @@ bool EncodeOsSyscap(char *output, int len); bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt); bool EncodePrivateSyscap(char **output, int *outputLen); bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt); +char *DecodeRpcidToStringFormat(char *inputFile); +bool ComparePcidString(char *pcidString, char *rpcidString); #ifdef __cplusplus #if __cplusplus diff --git a/napi/BUILD.gn b/napi/BUILD.gn index 4f796c8d53d2fae4d6f9350422b57c7d70031c1b..e7db3b1b5d9dc6c321fbe0e1192c36c45bdd54ea 100644 --- a/napi/BUILD.gn +++ b/napi/BUILD.gn @@ -33,6 +33,7 @@ ohos_shared_library("systemcapability") { include_dirs = [ "//third_party/node/src", "//foundation/arkui/napi/interfaces/kits", + "//developtools/syscap_codec/src", "//developtools/syscap_codec/napi", "//developtools/syscap_codec/interfaces/inner_api", "//developtools/syscap_codec/include", diff --git a/src/syscap_tool.c b/src/syscap_tool.c index f1f791bd071df66a44092b1f4d4a3a59d7286c00..a6be5d38a160a9b65861b4832585958ff5ab35b6 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -15,6 +15,7 @@ #include #include +#include #include #include #include @@ -502,8 +503,8 @@ int32_t DecodeRpcidToString(char *inputFile, char *outDirPath) } } uint32_t outUint[RPCID_OUT_BUFFER] = {0}; - outUint[0] = NtohsInter(((RPCIDHead *)contextBuffer)->apiVersion); - outUint[1] = NtohsInter(*(uint16_t *)(contextBuffer + sizeof(uint32_t))); + outUint[0] = *(uint32_t *)contextBuffer; + outUint[1] = *(uint32_t *)(contextBuffer + sizeof(uint32_t)); uint8_t *osOutUint = (uint8_t *)(outUint + 2); if (SetOsSysCapBitMap(osOutUint, 120, osSysCapIndex, indexOs)) { // 120, len of osOutUint PRINT_ERR("Set os syscap bit map failed.\n"); @@ -633,7 +634,9 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile) char *rpcidContent = NULL; char *pcidPriSyscap = NULL; char *rpcidPriSyscap = NULL; - uint32_t pcidContentLen, rpcidContentLen, pcidPriSyscapLen, rpcidPriSyscapLen, i; + uint32_t pcidContentLen, rpcidContentLen, pcidPriSyscapLen, rpcidPriSyscapLen; + uint32_t i, j; + bool priSysFound; uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0}; uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0}; @@ -658,19 +661,34 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile) if (pcidVersion < rpcidVersion) { PRINT_ERR("Pcid version(%u) less than rpcid version(%u).\n", pcidVersion, rpcidVersion); } - // compare os sysscap + // compare os syscap for (i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid uint32_t temp1 = pcidOsAarry[i] ^ rpcidOsAarry[i]; uint32_t temp2 = temp1 & rpcidOsAarry[i]; if (!temp2) { continue; } - for (uint8_t j = 0; j < INT_BIT; j++) { - if (temp2 & (0x1 << j)) { + for (uint8_t k = 0; k < INT_BIT; k++) { + if (temp2 & (0x1 << k)) { // 2, header of pcid & rpcid - printf("Miss: %s\n", arraySyscap[(i - 2) * INT_BIT + j].syscapStr); + printf("Miss: %s\n", arraySyscap[(i - 2) * INT_BIT + k].syscapStr); } } } + // 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) { + priSysFound = true; + break; + } + } + if (priSysFound != true) { + printf("Miss: %s\n", pcidPriSyscap + SINGLE_FEAT_LENGTH * i); + } + priSysFound = false; + } return ret; } \ No newline at end of file diff --git a/test/unittest/common/BUILD.gn b/test/unittest/common/BUILD.gn index db64b61c9f2f434589d78781f2d5d6e384a82b58..233b6dfa786a19ae915fa72c548db0e31e5760df 100644 --- a/test/unittest/common/BUILD.gn +++ b/test/unittest/common/BUILD.gn @@ -35,6 +35,7 @@ if (defined(ohos_lite)) { sources = [ "//developtools/syscap_codec/interfaces/inner_api/syscap_interface.c", "//developtools/syscap_codec/src/create_pcid.c", + "//developtools/syscap_codec/src/endian_internal.c", "syscap_codec_test.cpp", ] @@ -72,6 +73,7 @@ if (defined(ohos_lite)) { sources = [ "//developtools/syscap_codec/interfaces/inner_api/syscap_interface.c", "//developtools/syscap_codec/src/create_pcid.c", + "//developtools/syscap_codec/src/endian_internal.c", "syscap_codec_test.cpp", ] diff --git a/test/unittest/common/syscap_codec_test.cpp b/test/unittest/common/syscap_codec_test.cpp index fb33bec740889844605872ae2d771c61cc686fe9..829ca2f6d1e51fe92964b634851cb513fef8d9fb 100644 --- a/test/unittest/common/syscap_codec_test.cpp +++ b/test/unittest/common/syscap_codec_test.cpp @@ -99,4 +99,40 @@ HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1) EXPECT_EQ(decodePriCnt, 5); free(priOutput); } + +/* + * @tc.name: DecodeRpcidToStringFormat + * @tc.desc: Check the DecodeRpcidToStringFormat Decoding. + * @tc.type: FUNC + */ +HWTEST_F(SyscapCodecTest, DecodeRpcidToStringFormat, TestSize.Level1) +{ + char inputfile[] = "/system/etc/rpcid.sc"; + char *out = DecodeRpcidToStringFormat(inputfile); + EXPECT_TRUE(out); + if (out != NULL) { + printf("%s\n", out); + free(out); + } +} + +/* + * @tc.name: ComparePcidString + * @tc.desc: Check the DecodeRpcidToStringFormat Decoding. + * @tc.type: FUNC + */ +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)); +} } // namespace Syscap