diff --git a/interfaces/inner_api/syscap_interface.c b/interfaces/inner_api/syscap_interface.c index 1b17c15ae2aa67b51cabe451d82d9e5912f18bfc..73e9cdd89cb1fc3c279b12d4406cebfba8282502 100644 --- a/interfaces/inner_api/syscap_interface.c +++ b/interfaces/inner_api/syscap_interface.c @@ -42,6 +42,12 @@ #define INT_BIT 32 #define U32_TO_STR_MAX_LEN 11 +#define FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID 1 +#define FREE_MALLOC_OSSYSCAP_AFTER_DECODE_RPCID 2 +#define FREE_WHOLE_SYSCAP_AFTER_DECODE_RPCID 3 +#define FREE_RPCID_ROOT_AFTER_DECODE_RPCID 4 +#define FREE_CONTEXT_OUT_AFTER_DECODE_RPCID 5 + typedef struct ProductCompatibilityID { uint16_t apiVersion : 15; uint16_t apiVersionType : 1; @@ -53,6 +59,24 @@ typedef struct ProductCompatibilityID { static const char *g_pcidPath = "/system/etc/PCID.sc"; +struct FreeAfterDecodeRpcidInfo { + char *priSyscap; + char *contextBuffer; + cJSON *sysCapDefine; + cJSON *rpcidRoot; + uint16_t *osSysCapIndex; + int32_t sysCapArraySize; +}; + +struct PcidPriSyscapInfo { + uint32_t rpcidPriSyscapLen; + uint32_t pcidPriSyscapLen; + char *rpcidPriSyscap; + char *pcidPriSyscap; + uint16_t ossyscapFlag; + int32_t ret; +}; + bool EncodeOsSyscap(char *output, int len) { int32_t ret; @@ -299,96 +323,50 @@ FREE_SYSCAP_OUT: return ret; } -char *DecodeRpcidToStringFormat(const char *inputFile) +static int32_t TransStringFormatAndSaveSyscap(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, + cJSON *sysCapArray, const 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"); + freeAfterDecodeRpcidInfo.sysCapDefine = CreateWholeSyscapJsonObj(); + sysCapArray = cJSON_GetObjectItem(freeAfterDecodeRpcidInfo.rpcidRoot, "syscap"); if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) { PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile); - goto FREE_WHOLE_SYSCAP; + return -1; } - sysCapArraySize = cJSON_GetArraySize(sysCapArray); - if (sysCapArraySize < 0) { + freeAfterDecodeRpcidInfo.sysCapArraySize = cJSON_GetArraySize(sysCapArray); + if (freeAfterDecodeRpcidInfo.sysCapArraySize < 0) { PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile); - goto FREE_WHOLE_SYSCAP; + return -1; } // malloc for save os syscap index - osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) * sysCapArraySize); - if (osSysCapIndex == NULL) { + freeAfterDecodeRpcidInfo.osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) + * freeAfterDecodeRpcidInfo.sysCapArraySize); + if (freeAfterDecodeRpcidInfo.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_SYSCAP_LEN); - if (priSyscapArray == NULL) { - PRINT_ERR("malloc(%u) failed.\n", (uint32_t)sysCapArraySize * SINGLE_SYSCAP_LEN); - goto FREE_MALLOC_OSSYSCAP; - } - (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++) { - cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i); - cJsonTemp = cJSON_GetObjectItem(sysCapDefine, cJsonItem->valuestring); - if (cJsonTemp != NULL) { - osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint); - } else { - ret = strcpy_s(priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring); - if (ret != EOK) { - PRINT_ERR("strcpy_s failed.\n"); - goto FREE_MALLOC_PRISYSCAP; - } - priSyscapArray += SINGLE_SYSCAP_LEN; - indexPri++; - } + return -1; } + return 0; +} + +static void PrintResultToOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer, + char *priSyscapArray, uint16_t indexOs, uint16_t indexPri) +{ + int32_t ret = 0; + uint32_t i; uint32_t outUint[RPCID_OUT_BUFFER] = {0}; - outUint[0] = *(uint32_t *)contextBuffer; - outUint[1] = *(uint32_t *)(contextBuffer + sizeof(uint32_t)); + outUint[0] = *(uint32_t *)freeAfterDecodeRpcidInfo.contextBuffer; + outUint[1] = *(uint32_t *)(freeAfterDecodeRpcidInfo.contextBuffer + sizeof(uint32_t)); uint8_t *osOutUint = (uint8_t *)(outUint + 2); - if (SetOsSysCapBitMap(osOutUint, 120, osSysCapIndex, indexOs)) { // 120, len of osOutUint + if (SetOsSysCapBitMap(osOutUint, 120, freeAfterDecodeRpcidInfo.osSysCapIndex, indexOs)) { // 120, len of osOutUint PRINT_ERR("Set os syscap bit map failed.\n"); - goto FREE_MALLOC_PRISYSCAP; + return; } - uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER + - SINGLE_SYSCAP_LEN * indexPri; + 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); - goto FREE_MALLOC_PRISYSCAP; + return; } (void)memset_s(outBuffer, outBufferLen, 0, outBufferLen); @@ -397,7 +375,7 @@ char *DecodeRpcidToStringFormat(const char *inputFile) PRINT_ERR("sprintf_s failed.\n"); free(outBuffer); outBuffer = NULL; - goto FREE_MALLOC_PRISYSCAP; + return; } for (i = 1; i < RPCID_OUT_BUFFER; i++) { ret = sprintf_s(outBuffer, outBufferLen, "%s,%u", outBuffer, outUint[i]); @@ -405,104 +383,197 @@ char *DecodeRpcidToStringFormat(const char *inputFile) PRINT_ERR("sprintf_s failed.\n"); free(outBuffer); outBuffer = NULL; - goto FREE_MALLOC_PRISYSCAP; + return; } } for (i = 0; i < indexPri; i++) { - ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, - priSyscapArray + i * SINGLE_SYSCAP_LEN); + ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, priSyscapArray + i * SINGLE_SYSCAP_LEN); if (ret == -1) { PRINT_ERR("sprintf_s failed.\n"); free(outBuffer); outBuffer = NULL; - goto FREE_MALLOC_PRISYSCAP; + return; } } +} -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); +static void PartSysCapAndOutBuffer(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, char *outBuffer, + char *priSyscapArray, cJSON *sysCapArray) +{ + uint32_t i; + int32_t ret = 0; + uint16_t indexOs = 0; + uint16_t indexPri = 0; + cJSON *cJsonTemp = NULL; + + (void)memset_s(priSyscapArray, freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN, + 0, freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN); + freeAfterDecodeRpcidInfo.priSyscap = priSyscapArray; + // part os syscap and ptivate syscap + for (i = 0; i < (uint32_t)freeAfterDecodeRpcidInfo.sysCapArraySize; i++) { + cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i); + cJsonTemp = cJSON_GetObjectItem(freeAfterDecodeRpcidInfo.sysCapDefine, cJsonItem->valuestring); + if (cJsonTemp != NULL) { + freeAfterDecodeRpcidInfo.osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint); + } else { + ret = strcpy_s(freeAfterDecodeRpcidInfo.priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring); + if (ret != EOK) { + PRINT_ERR("strcpy_s failed.\n"); + return; + } + priSyscapArray += SINGLE_SYSCAP_LEN; + indexPri++; + } + } + PrintResultToOutBuffer(freeAfterDecodeRpcidInfo, outBuffer, priSyscapArray, indexOs, indexPri); +} + +static char *FreeAfterDecodeRpcidToString(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo, int32_t type, + char *outBuffer) +{ + switch (type) { + case FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID: + free(freeAfterDecodeRpcidInfo.priSyscap); + /* fall-through */ + case FREE_MALLOC_OSSYSCAP_AFTER_DECODE_RPCID: + free(freeAfterDecodeRpcidInfo.osSysCapIndex); + /* fall-through */ + case FREE_WHOLE_SYSCAP_AFTER_DECODE_RPCID: + cJSON_Delete(freeAfterDecodeRpcidInfo.sysCapDefine); + /* fall-through */ + case FREE_RPCID_ROOT_AFTER_DECODE_RPCID: + cJSON_Delete(freeAfterDecodeRpcidInfo.rpcidRoot); + /* fall-through */ + case FREE_CONTEXT_OUT_AFTER_DECODE_RPCID: + default: + FreeContextBuffer(freeAfterDecodeRpcidInfo.contextBuffer); + } return outBuffer; } -int32_t ComparePcidString(const char *pcidString, const char *rpcidString, CompareError *result) +char *DecodeRpcidToStringFormat(const char *inputFile) { - int32_t ret; - uint16_t versionFlag = 0; - uint16_t ossyscapFlag = 0; - uint16_t prisyscapFlag = 0; - char *pcidPriSyscap = NULL; - char *rpcidPriSyscap = NULL; - bool priSysFound; - uint32_t pcidPriSyscapLen, rpcidPriSyscapLen; - uint32_t i, j; - uint32_t retFlag = 0; - uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0}; - uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0}; - const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); + int32_t ret = 0; + uint32_t bufferLen; + char *priSyscapArray = NULL; + char *outBuffer = NULL; + cJSON *sysCapArray = NULL; - 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); + struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo; + freeAfterDecodeRpcidInfo.priSyscap = NULL; + freeAfterDecodeRpcidInfo.osSysCapIndex = 0; + freeAfterDecodeRpcidInfo.sysCapDefine = NULL; + freeAfterDecodeRpcidInfo.rpcidRoot = NULL; + freeAfterDecodeRpcidInfo.contextBuffer = NULL; + freeAfterDecodeRpcidInfo.sysCapArraySize = 0; + + // check rpcid.sc + if (CheckRpcidFormat(inputFile, &freeAfterDecodeRpcidInfo.contextBuffer, &bufferLen)) { + PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile); + return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_CONTEXT_OUT_AFTER_DECODE_RPCID, outBuffer); + } + + // parse rpcid to json + freeAfterDecodeRpcidInfo.rpcidRoot = cJSON_CreateObject(); + if (ParseRpcidToJson(freeAfterDecodeRpcidInfo.contextBuffer, bufferLen, freeAfterDecodeRpcidInfo.rpcidRoot)) { + PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile); + return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_RPCID_ROOT_AFTER_DECODE_RPCID, outBuffer); + } + + ret = TransStringFormatAndSaveSyscap(freeAfterDecodeRpcidInfo, sysCapArray, inputFile); + if (ret == -1) { + return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_WHOLE_SYSCAP_AFTER_DECODE_RPCID, outBuffer); + } + + (void)memset_s(freeAfterDecodeRpcidInfo.osSysCapIndex, sizeof(uint16_t) * freeAfterDecodeRpcidInfo + .sysCapArraySize, 0, sizeof(uint16_t) * freeAfterDecodeRpcidInfo.sysCapArraySize); + // malloc for save private syscap string + priSyscapArray = (char *)malloc(freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN); + if (priSyscapArray == NULL) { + PRINT_ERR("malloc(%u) failed.\n", (uint32_t)freeAfterDecodeRpcidInfo.sysCapArraySize * SINGLE_SYSCAP_LEN); + return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_MALLOC_OSSYSCAP_AFTER_DECODE_RPCID, + outBuffer); + } + + PartSysCapAndOutBuffer(freeAfterDecodeRpcidInfo, outBuffer, priSyscapArray, sysCapArray); + return FreeAfterDecodeRpcidToString(freeAfterDecodeRpcidInfo, FREE_MALLOC_PRISYSCAP_AFTER_DECODE_RPCID, outBuffer); +} + +static int32_t CopySyscopToRet(struct PcidPriSyscapInfo pcidPriSyscapInfo, const size_t allSyscapNum, + char *tempSyscap, uint32_t i, uint8_t k) +{ + uint32_t pos = (i - 2) * INT_BIT + k; + uint32_t t; + for (t = 0; t < allSyscapNum; t++) { + if (g_arraySyscap[t].num == pos) { + break; + } + } + pcidPriSyscapInfo.ret = strcpy_s(tempSyscap, sizeof(char) * SINGLE_SYSCAP_LEN, g_arraySyscap[t].str); + // 2, header of pcid & rpcid + if (pcidPriSyscapInfo.ret != EOK) { return -1; } - result->missSyscapNum = 0; - // compare version - uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion); - uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion); - if (pcidVersion < rpcidVersion) { - result->targetApiVersion = rpcidVersion; - versionFlag = 1; + return 0; +} + +static int32_t CheckPcidEachBit(struct PcidPriSyscapInfo pcidPriSyscapInfo, CompareError *result, + const size_t allSyscapNum, uint32_t i, uint32_t blockBits) +{ + int32_t flag = 0; + for (uint8_t k = 0; k < INT_BIT; k++) { + if (blockBits & (1U << k)) { + char *tempSyscap = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN); + if (tempSyscap == NULL) { + PRINT_ERR("malloc failed.\n"); + FreeCompareError(result); + return -1; + } + flag = CopySyscopToRet(pcidPriSyscapInfo, allSyscapNum, tempSyscap, i, k); + if (flag != EOK) { + PRINT_ERR("strcpy_s failed.\n"); + FreeCompareError(result); + return -1; + } + result->syscap[pcidPriSyscapInfo.ossyscapFlag++] = tempSyscap; + } } - // compare os sysscap + return 0; +} + +static int32_t ComparePcidWithOsSyscap(struct PcidPriSyscapInfo pcidPriSyscapInfo, + uint32_t pcidOsAarry[PCID_OUT_BUFFER], uint32_t rpcidOsAarry[PCID_OUT_BUFFER], CompareError *result, + const size_t allSyscapNum) +{ + uint32_t i; + int32_t ret = 0; + for (i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid uint32_t blockBits = (pcidOsAarry[i] ^ rpcidOsAarry[i]) & rpcidOsAarry[i]; if (!blockBits) { continue; } - for (uint8_t k = 0; k < INT_BIT; k++) { - if (blockBits & (1U << k)) { - char *tempSyscap = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN); - if (tempSyscap == NULL) { - PRINT_ERR("malloc failed.\n"); - FreeCompareError(result); - return -1; - } - uint32_t pos = (i - 2) * INT_BIT + k; - uint32_t t; - for (t = 0; t < allSyscapNum; t++) { - if (g_arraySyscap[t].num == pos) { - break; - } - } - ret = strcpy_s(tempSyscap, sizeof(char) * SINGLE_SYSCAP_LEN, - g_arraySyscap[t].str); // 2, header of pcid & rpcid - if (ret != EOK) { - PRINT_ERR("strcpy_s failed.\n"); - FreeCompareError(result); - return -1; - } - result->syscap[ossyscapFlag++] = tempSyscap; - } + ret = CheckPcidEachBit(pcidPriSyscapInfo, result, allSyscapNum, i, blockBits); + if (ret == -1) { + return -1; } } - // compare pri syscap - priSysFound = false; - 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) { + return 0; +} + +static int32_t ComparePcidWithPriSyscap(struct PcidPriSyscapInfo pcidPriSyscapInfo, CompareError *result, + uint16_t versionFlag) +{ + uint32_t i, j; + uint16_t prisyscapFlag = 0; + uint32_t retFlag = 0; + bool priSysFound = false; + + for (i = 0; i < pcidPriSyscapInfo.rpcidPriSyscapLen; i++) { + for (j = 0; j < pcidPriSyscapInfo.pcidPriSyscapLen; j++) { + if (strcmp(pcidPriSyscapInfo.rpcidPriSyscap + SINGLE_SYSCAP_LEN * i, + pcidPriSyscapInfo.pcidPriSyscap + SINGLE_SYSCAP_LEN * j) == 0) { priSysFound = true; break; } @@ -514,14 +585,14 @@ int32_t ComparePcidString(const char *pcidString, const char *rpcidString, Compa FreeCompareError(result); return -1; } - ret = strcpy_s(temp, sizeof(char) * SINGLE_SYSCAP_LEN, - rpcidPriSyscap + SINGLE_SYSCAP_LEN * i); - if (ret != EOK) { + pcidPriSyscapInfo.ret = strcpy_s(temp, sizeof(char) * SINGLE_SYSCAP_LEN, + pcidPriSyscapInfo.rpcidPriSyscap + SINGLE_SYSCAP_LEN * i); + if (pcidPriSyscapInfo.ret != EOK) { FreeCompareError(result); PRINT_ERR("strcpy_s failed.\n"); return -1; } - result->syscap[ossyscapFlag + prisyscapFlag] = temp; + result->syscap[pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag] = temp; ++prisyscapFlag; } priSysFound = false; @@ -530,13 +601,55 @@ int32_t ComparePcidString(const char *pcidString, const char *rpcidString, Compa if (versionFlag > 0) { retFlag |= 1U << 0; } - if (ossyscapFlag > 0 || prisyscapFlag > 0) { + if (pcidPriSyscapInfo.ossyscapFlag > 0 || prisyscapFlag > 0) { retFlag |= 1U << 1; - result->missSyscapNum = ossyscapFlag + prisyscapFlag; + result->missSyscapNum = pcidPriSyscapInfo.ossyscapFlag + prisyscapFlag; } return (int32_t)retFlag; } +int32_t ComparePcidString(const char *pcidString, const char *rpcidString, CompareError *result) +{ + uint16_t versionFlag = 0; + int32_t errorFlag = 0; + struct PcidPriSyscapInfo pcidPriSyscapInfo; + pcidPriSyscapInfo.pcidPriSyscap = NULL; + pcidPriSyscapInfo.rpcidPriSyscap = NULL; + pcidPriSyscapInfo.pcidPriSyscapLen = 0; + pcidPriSyscapInfo.rpcidPriSyscapLen = 0; + pcidPriSyscapInfo.ossyscapFlag = 0; + pcidPriSyscapInfo.ret = 0; + uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0}; + uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0}; + const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum); + + pcidPriSyscapInfo.ret = SeparateSyscapFromString(pcidString, pcidOsAarry, PCID_OUT_BUFFER, + &pcidPriSyscapInfo.pcidPriSyscap, &pcidPriSyscapInfo.pcidPriSyscapLen); + pcidPriSyscapInfo.ret += SeparateSyscapFromString(rpcidString, rpcidOsAarry, RPCID_OUT_BUFFER, + &pcidPriSyscapInfo.rpcidPriSyscap, &pcidPriSyscapInfo.rpcidPriSyscapLen); + if (pcidPriSyscapInfo.ret != 0) { + PRINT_ERR("Separate syscap from string failed. ret = %d\n", pcidPriSyscapInfo.ret); + return -1; + } + result->missSyscapNum = 0; + // compare version + uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion); + uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion); + if (pcidVersion < rpcidVersion) { + result->targetApiVersion = rpcidVersion; + versionFlag = 1; + } + + // compare os sysscap + errorFlag = ComparePcidWithOsSyscap(pcidPriSyscapInfo, pcidOsAarry, rpcidOsAarry, result, allSyscapNum); + if (errorFlag == -1) { + return errorFlag; + } + + // compare pri syscap + return ComparePcidWithPriSyscap(pcidPriSyscapInfo, result, versionFlag); +} + int32_t FreeCompareError(CompareError *result) { if (result == NULL) { diff --git a/napi/napi_query_syscap.cpp b/napi/napi_query_syscap.cpp index c3901e22249cc4ec5d7b0cbfbb9d3e62a269ae38..f01836f2baafbf21765a8a457f51786283d86c42 100644 --- a/napi/napi_query_syscap.cpp +++ b/napi/napi_query_syscap.cpp @@ -50,47 +50,20 @@ struct SystemCapabilityAsyncContext { int status = 0; }; -static char* GetSystemCapability() +static char* CalculateAllStringLength(char osCapArray[PCID_MAIN_U32][U32_TO_STR_MAX_LEN], + char (*priCapArray)[SINGLE_SYSCAP_LEN], bool retBool, int priCapArrayCnt) { - bool retBool; - int retError, priOutputLen, priCapArrayCnt, sumLen; - char osOutput[SINGLE_SYSCAP_LEN] = {}; errno_t err = EOK; - uint32_t *osCapU32 = nullptr; - char *priOutput = nullptr; char *temp = nullptr; + int retError; + int sumLen = 0; char *allSyscapBuffer = nullptr; - char osCapArray[PCID_MAIN_U32][U32_TO_STR_MAX_LEN] = {}; - char (*priCapArray)[SINGLE_SYSCAP_LEN] = nullptr; - - retBool = EncodeOsSyscap(osOutput, PCID_MAIN_BYTES); - if (!retBool) { - PRINT_ERR("get encoded os syscap failed."); - return nullptr; - } - retBool = EncodePrivateSyscap(&priOutput, &priOutputLen); - if (!retBool) { - PRINT_ERR("get encoded private syscap failed."); - goto FREE_PRIOUTPUT; - } - - osCapU32 = reinterpret_cast(osOutput); - for (size_t i = 0; i < PCID_MAIN_U32; i++) { // 2, header of pcid.sc - retError = sprintf_s(osCapArray[i], U32_TO_STR_MAX_LEN, "%u", osCapU32[i]); - if (retError == -1) { - PRINT_ERR("get uint32_t syscap string failed."); - goto FREE_PRIOUTPUT; - } - } - retBool = DecodePrivateSyscap(priOutput, &priCapArray, &priCapArrayCnt); if (!retBool) { PRINT_ERR("get encoded private syscap failed."); - goto FREE_PRICAP_ARRAY; + return allSyscapBuffer; } - // calculate all string length - sumLen = 0; for (size_t i = 0; i < PCID_MAIN_U32; i++) { sumLen += strlen(osCapArray[i]); } @@ -103,14 +76,13 @@ static char* GetSystemCapability() allSyscapBuffer = (char *)malloc(sumLen); if (allSyscapBuffer == nullptr) { PRINT_ERR("malloc failed!"); - goto FREE_PRICAP_ARRAY; + return allSyscapBuffer; } err = memset_s(allSyscapBuffer, sumLen, 0, sumLen); if (err != EOK) { PRINT_ERR("memset failed!"); free(allSyscapBuffer); - allSyscapBuffer = nullptr; - goto FREE_PRICAP_ARRAY; + return nullptr; } temp = *osCapArray; @@ -119,8 +91,7 @@ static char* GetSystemCapability() if (retError == -1) { PRINT_ERR("splicing os syscap string failed."); free(allSyscapBuffer); - allSyscapBuffer = nullptr; - goto FREE_PRICAP_ARRAY; + return nullptr; } temp = allSyscapBuffer; } @@ -129,14 +100,49 @@ static char* GetSystemCapability() if (retError == -1) { PRINT_ERR("splicing pri syscap string failed."); free(allSyscapBuffer); - allSyscapBuffer = nullptr; - goto FREE_PRICAP_ARRAY; + return nullptr; } temp = allSyscapBuffer; } + return allSyscapBuffer; +} -FREE_PRICAP_ARRAY: +static char* GetSystemCapability() +{ + bool retBool; + int retError, priOutputLen, priCapArrayCnt; + char osOutput[SINGLE_SYSCAP_LEN] = {}; + + uint32_t *osCapU32 = nullptr; + char *priOutput = nullptr; + + char *allSyscapBuffer = nullptr; + char osCapArray[PCID_MAIN_U32][U32_TO_STR_MAX_LEN] = {}; + char (*priCapArray)[SINGLE_SYSCAP_LEN] = nullptr; + + retBool = EncodeOsSyscap(osOutput, PCID_MAIN_BYTES); + if (!retBool) { + PRINT_ERR("get encoded os syscap failed."); + return nullptr; + } + retBool = EncodePrivateSyscap(&priOutput, &priOutputLen); + if (!retBool) { + PRINT_ERR("get encoded private syscap failed."); + goto FREE_PRIOUTPUT; + } + + osCapU32 = reinterpret_cast(osOutput); + for (size_t i = 0; i < PCID_MAIN_U32; i++) { // 2, header of pcid.sc + retError = sprintf_s(osCapArray[i], U32_TO_STR_MAX_LEN, "%u", osCapU32[i]); + if (retError == -1) { + PRINT_ERR("get uint32_t syscap string failed."); + goto FREE_PRIOUTPUT; + } + } + retBool = DecodePrivateSyscap(priOutput, &priCapArray, &priCapArrayCnt); + allSyscapBuffer = CalculateAllStringLength(osCapArray, priCapArray, retBool, priCapArrayCnt); free(priCapArray); + FREE_PRIOUTPUT: free(priOutput); diff --git a/src/create_pcid.c b/src/create_pcid.c index b65391c3686466049a0ccab2f92e9598874d46ea..92cf6bd7d5d734b69ccbbb192d7cf68a9a45a093 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -211,10 +211,26 @@ int32_t GetPriSyscapLen(uint32_t privateCapSize, cJSON *jsonPriSyscapObj, uint16 return 0; } -int32_t FreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, char *contextBuffer, - int32_t type, int32_t ret); +static int32_t CheckConvertedContextSaveAsFile(char *outDirPath, PCIDMain *pcidBuffer, uint16_t pcidLength, int32_t ret) +{ + const char pcidFileName[] = "PCID.sc"; + ret = ConvertedContextSaveAsFile(outDirPath, pcidFileName, (char *)pcidBuffer, pcidLength); + if (ret != 0) { + PRINT_ERR("Save as file failed, outDirPath:%s, filename:%s\n", outDirPath, pcidFileName); + } + return ret; +} -int32_t CheckConvertedContextSaveAsFile(char *outDirPath, PCIDMain *pcidBuffer, uint16_t pcidLength, int32_t ret); +static int32_t FreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, char *contextBuffer, + int32_t type, int32_t ret) +{ + if (type == FREE_CREATE_PCID_BUFFER_OUT) { + free(pcidBuffer); + } + cJSON_Delete(allOsSyscapObj); + FreeContextBuffer(contextBuffer); + return ret; +} int32_t CreatePCID(char *inputFile, char *outDirPath) { @@ -273,27 +289,6 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) return FreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, contextBuffer, FREE_CREATE_PCID_BUFFER_OUT, ret); } -int32_t CheckConvertedContextSaveAsFile(char *outDirPath, PCIDMain *pcidBuffer, uint16_t pcidLength, int32_t ret) -{ - const char pcidFileName[] = "PCID.sc"; - ret = ConvertedContextSaveAsFile(outDirPath, pcidFileName, (char *)pcidBuffer, pcidLength); - if (ret != 0) { - PRINT_ERR("Save as file failed, outDirPath:%s, filename:%s\n", outDirPath, pcidFileName); - } - return ret; -} - -int32_t FreeAfterCreatePCID(PCIDMain *pcidBuffer, cJSON *allOsSyscapObj, char *contextBuffer, - int32_t type, int32_t ret) -{ - if (type == FREE_CREATE_PCID_BUFFER_OUT) { - free(pcidBuffer); - } - cJSON_Delete(allOsSyscapObj); - FreeContextBuffer(contextBuffer); - return ret; -} - int32_t GetOsSyscap(PCIDMain *pcidMain, cJSON *sysCapObject) { uint32_t i, j, countOfSyscap = 0; @@ -386,35 +381,88 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL return 0; } -int32_t CheckJsonRootObj(struct FreeDecodePcidJsonInfo gFreePcidJsonInfo, PCIDMain *pcidMain, char *systemType); +static int32_t CheckSysCapObj(struct FreeDecodePcidJsonInfo freePcidJsonInfo, PCIDMain *pcidMain, + uint32_t contextBufLen, int32_t ret) +{ + freePcidJsonInfo.flag = 0; + if (freePcidJsonInfo.sysCapObj == NULL) { + PRINT_ERR("cJSON_CreateObject failed\n"); + freePcidJsonInfo.flag = -1; + return -1; + } + if (GetOsSyscap(pcidMain, freePcidJsonInfo.sysCapObj) != 0) { + freePcidJsonInfo.flag = -1; + return ret; + } + if (GetPriSyscap(pcidMain, freePcidJsonInfo.sysCapObj, contextBufLen) != 0) { + freePcidJsonInfo.flag = -1; + } + return ret; +} -int32_t FreeAfterDecodePCID(struct FreeDecodePcidJsonInfo gFreePcidJsonInfo, int32_t type, int32_t ret); +static int32_t CheckJsonRootObj(struct FreeDecodePcidJsonInfo freePcidJsonInfo, PCIDMain *pcidMain, char *systemType) +{ + if (!cJSON_AddNumberToObject(freePcidJsonInfo.jsonRootObj, "api_version", NtohsInter(pcidMain->apiVersion))) { + PRINT_ERR("cJSON_AddNumberToObject failed\n"); + return -1; + } + if (!cJSON_AddNumberToObject(freePcidJsonInfo.jsonRootObj, "manufacturer_id", + NtohlInter(pcidMain->manufacturerID))) { + PRINT_ERR("cJSON_AddNumberToObject failed\n"); + return -1; + } + if (!cJSON_AddStringToObject(freePcidJsonInfo.jsonRootObj, "system_type", systemType)) { + PRINT_ERR("cJSON_AddStringToObject failed\n"); + return -1; + } + if (!cJSON_AddItemToObject(freePcidJsonInfo.jsonRootObj, "syscap", freePcidJsonInfo.sysCapObj)) { + PRINT_ERR("cJSON_AddItemToObject failed\n"); + return -1; + } + return 0; +} -int32_t CheckSysCapObj(struct FreeDecodePcidJsonInfo gFreePcidJsonInfo, PCIDMain *pcidMain, - uint32_t contextBufLen, int32_t ret); +static int32_t FreeAfterDecodePCID(struct FreeDecodePcidJsonInfo freePcidJsonInfo, int32_t type, int32_t ret) +{ + switch (type) { + case FREE_DECODE_PCID_CONVERT_OUT: + free(freePcidJsonInfo.strJson); + /* fall-through */ + case FREE_DECODE_PCID_ROOT_OUT: + cJSON_Delete(freePcidJsonInfo.jsonRootObj); + /* fall-through */ + case FREE_DECODE_PCID_SYSCAP_OUT: + cJSON_Delete(freePcidJsonInfo.sysCapObj); + /* fall-through */ + case FREE_DECODE_PCID_CONTEXT_OUT: + default: + FreeContextBuffer(freePcidJsonInfo.contextBuffer); + } + return ret; +} int32_t DecodePCID(char *inputFile, char *outDirPath) { int32_t ret = 0; uint32_t contextBufLen; - struct FreeDecodePcidJsonInfo gFreePcidJsonInfo; - gFreePcidJsonInfo.strJson = NULL; - gFreePcidJsonInfo.contextBuffer = NULL; - gFreePcidJsonInfo.jsonRootObj = NULL; - gFreePcidJsonInfo.sysCapObj = NULL; + struct FreeDecodePcidJsonInfo freePcidJsonInfo; + freePcidJsonInfo.strJson = NULL; + freePcidJsonInfo.contextBuffer = NULL; + freePcidJsonInfo.jsonRootObj = NULL; + freePcidJsonInfo.sysCapObj = NULL; - ret = GetFileContext(inputFile, &gFreePcidJsonInfo.contextBuffer, (uint32_t *)&contextBufLen); + ret = GetFileContext(inputFile, &freePcidJsonInfo.contextBuffer, (uint32_t *)&contextBufLen); if (ret != 0) { PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); return -1; } - PCIDMain *pcidMain = (PCIDMain *)gFreePcidJsonInfo.contextBuffer; + PCIDMain *pcidMain = (PCIDMain *)freePcidJsonInfo.contextBuffer; /* api version */ if (pcidMain->apiVersionType != 0) { PRINT_ERR("Prase file failed, apiVersionType is invaild, input file : %s\n", inputFile); - return FreeAfterDecodePCID(gFreePcidJsonInfo, FREE_DECODE_PCID_CONTEXT_OUT, -1); + return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONTEXT_OUT, -1); } /* system type */ @@ -423,94 +471,37 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) (pcidMain->systemType == 0b100 ? "standard" : NULL)); if (systemType == NULL) { PRINT_ERR("prase file failed, systemType is invaild, %u\n", pcidMain->systemType); - return FreeAfterDecodePCID(gFreePcidJsonInfo, FREE_DECODE_PCID_CONTEXT_OUT, -1); + return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONTEXT_OUT, -1); } /* syscap */ - gFreePcidJsonInfo.sysCapObj = cJSON_CreateObject(); - ret = CheckSysCapObj(gFreePcidJsonInfo, pcidMain, contextBufLen, ret); - if (gFreePcidJsonInfo.flag == -1) { - return FreeAfterDecodePCID(gFreePcidJsonInfo, FREE_DECODE_PCID_CONTEXT_OUT, ret); + freePcidJsonInfo.sysCapObj = cJSON_CreateObject(); + ret = CheckSysCapObj(freePcidJsonInfo, pcidMain, contextBufLen, ret); + if (freePcidJsonInfo.flag == -1) { + return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONTEXT_OUT, ret); } // create json root - gFreePcidJsonInfo.jsonRootObj = cJSON_CreateObject(); - if (gFreePcidJsonInfo.jsonRootObj == NULL) { + freePcidJsonInfo.jsonRootObj = cJSON_CreateObject(); + if (freePcidJsonInfo.jsonRootObj == NULL) { PRINT_ERR("cJSON_CreateObject failed\n"); - return FreeAfterDecodePCID(gFreePcidJsonInfo, FREE_DECODE_PCID_SYSCAP_OUT, -1); + return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_SYSCAP_OUT, -1); } - ret = CheckJsonRootObj(gFreePcidJsonInfo, pcidMain, systemType); + ret = CheckJsonRootObj(freePcidJsonInfo, pcidMain, systemType); if (ret == -1) { - return FreeAfterDecodePCID(gFreePcidJsonInfo, FREE_DECODE_PCID_ROOT_OUT, ret); + return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_ROOT_OUT, ret); } - gFreePcidJsonInfo.sysCapObj = NULL; // avoid being released repeatedly. + freePcidJsonInfo.sysCapObj = NULL; // avoid being released repeatedly. - char *strJson = cJSON_Print(gFreePcidJsonInfo.jsonRootObj); + char *strJson = cJSON_Print(freePcidJsonInfo.jsonRootObj); const char outputFileName[] = "PCID.json"; ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, strJson, strlen(strJson)); if (ret != 0) { PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, outputFileName); } - return FreeAfterDecodePCID(gFreePcidJsonInfo, FREE_DECODE_PCID_CONVERT_OUT, ret); -} - -int32_t CheckSysCapObj(struct FreeDecodePcidJsonInfo gFreePcidJsonInfo, PCIDMain *pcidMain, - uint32_t contextBufLen, int32_t ret) -{ - gFreePcidJsonInfo.flag = 0; - if (gFreePcidJsonInfo.sysCapObj == NULL) { - PRINT_ERR("cJSON_CreateObject failed\n"); - gFreePcidJsonInfo.flag = -1; - return -1; - } - if (GetOsSyscap(pcidMain, gFreePcidJsonInfo.sysCapObj) != 0) { - gFreePcidJsonInfo.flag = -1; - return ret; - } - if (GetPriSyscap(pcidMain, gFreePcidJsonInfo.sysCapObj, contextBufLen) != 0) { - gFreePcidJsonInfo.flag = -1; - } - return ret; -} - -int32_t CheckJsonRootObj(struct FreeDecodePcidJsonInfo gFreePcidJsonInfo, PCIDMain *pcidMain, char *systemType) -{ - if (!cJSON_AddNumberToObject(gFreePcidJsonInfo.jsonRootObj, "api_version", NtohsInter(pcidMain->apiVersion))) { - PRINT_ERR("cJSON_AddNumberToObject failed\n"); - return -1; - } - if (!cJSON_AddNumberToObject(gFreePcidJsonInfo.jsonRootObj, "manufacturer_id", - NtohlInter(pcidMain->manufacturerID))) { - PRINT_ERR("cJSON_AddNumberToObject failed\n"); - return -1; - } - if (!cJSON_AddStringToObject(gFreePcidJsonInfo.jsonRootObj, "system_type", systemType)) { - PRINT_ERR("cJSON_AddStringToObject failed\n"); - return -1; - } - if (!cJSON_AddItemToObject(gFreePcidJsonInfo.jsonRootObj, "syscap", gFreePcidJsonInfo.sysCapObj)) { - PRINT_ERR("cJSON_AddItemToObject failed\n"); - return -1; - } - return 0; -} - -int32_t FreeAfterDecodePCID(struct FreeDecodePcidJsonInfo gFreePcidJsonInfo, int32_t type, int32_t ret) -{ - switch (type) { - case FREE_DECODE_PCID_CONVERT_OUT: - free(gFreePcidJsonInfo.strJson); - case FREE_DECODE_PCID_ROOT_OUT: - cJSON_Delete(gFreePcidJsonInfo.jsonRootObj); - case FREE_DECODE_PCID_SYSCAP_OUT: - cJSON_Delete(gFreePcidJsonInfo.sysCapObj); - case FREE_DECODE_PCID_CONTEXT_OUT: - default: - FreeContextBuffer(gFreePcidJsonInfo.contextBuffer); - } - return ret; + return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONVERT_OUT, ret); } #define U32_TO_STR_MAX_LEN 11 @@ -726,28 +717,106 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath) return ret; } -int32_t FreeAfterEncodePCID(struct FreeEncodePcidInfo gFreePcidInfo, int32_t type, int32_t ret); +static int32_t FreeAfterEncodePCID(struct FreeEncodePcidInfo freePcidInfo, int32_t type, int32_t ret) +{ + switch (type) { + case FREE_ENCODE_PCID_OUT: + free(freePcidInfo.output); + /* fall-through */ + case FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT: + free(freePcidInfo.priSyscapFull); + /* fall-through */ + case FREE_ENCODE_PCID_CONTEXT_OUT: + default: + free(freePcidInfo.contextBuffer); + } + return ret; +} + +static int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapLen, uint32_t *mainSyscap, + struct FreeEncodePcidInfo freePcidInfo, int32_t ret) +{ + // 17, size of "SystemCapability." + uint32_t outputLen = U32_TO_STR_MAX_LEN * PCID_OUT_BUFFER + 17 * priSyscapCount + privateSyscapLen + 1; + char *output = NULL; + uint32_t i; + output = (char *)malloc(outputLen); + if (output == NULL) { + PRINT_ERR("malloc failed\n"); + return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT, ret); + } + (void)memset_s(output, outputLen, 0, outputLen); + ret = sprintf_s(output, outputLen, "%u", mainSyscap[0]); + if (ret == -1) { + PRINT_ERR("sprintf_s failed\n"); + return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret); + } + for (i = 1; i < PCID_OUT_BUFFER; i++) { + ret = sprintf_s(output, outputLen, "%s,%u", output, mainSyscap[i]); + if (ret == -1) { + PRINT_ERR("sprintf_s failed\n"); + return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret); + } + } + for (i = 0; i < priSyscapCount; i++) { + ret = sprintf_s(output, outputLen, "%s,%s", output, freePcidInfo.priSyscapFull + i * SINGLE_SYSCAP_LEN); + if (ret == -1) { + PRINT_ERR("sprintf_s failed\n"); + return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret); + } + } + // save as file + const char outputFileName[] = "PCID.txt"; + ret = ConvertedContextSaveAsFile(freePcidInfo.outDirPathFinal, outputFileName, output, strlen(output)); + if (ret != 0) { + PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", + freePcidInfo.outDirPathFinal, outputFileName); + } + return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret); +} -int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapLen, uint32_t *mainSyscap, - struct FreeEncodePcidInfo gFreePcidInfo, int32_t ret); +static int32_t CheckPrivateSyCap(struct FreeEncodePcidInfo freePcidInfo, uint32_t privateSyscapLen, + char *privateSyscap, int32_t ret) +{ + uint32_t i, j; + char tempSyscap[SINGLE_SYSCAP_LEN] = {0}; + char *temp = tempSyscap; + for (i = 0, j = 0; i < privateSyscapLen; i++) { + if (*privateSyscap == ',') { + *temp = '\0'; + ret = sprintf_s(freePcidInfo.priSyscapFull + j * SINGLE_SYSCAP_LEN, SINGLE_SYSCAP_LEN, + "SystemCapability.%s", tempSyscap); + if (ret == -1) { + PRINT_ERR("sprintf_s failed\n"); + return ret; + } + temp = tempSyscap; + privateSyscap++; + j++; + continue; + } + *temp++ = *privateSyscap++; + } + return ret; +} int32_t EncodePcidscToString(char *inputFile, char *outDirPath) { int32_t ret = 0; uint32_t bufferLen, privateSyscapLen; - uint32_t i, j; + uint32_t i; uint32_t *mainSyscap = NULL; uint16_t priSyscapCount = 0; char *privateSyscap = NULL; - struct FreeEncodePcidInfo gFreePcidInfo; - gFreePcidInfo.contextBuffer = NULL; - gFreePcidInfo.priSyscapFull = NULL; - gFreePcidInfo.output = NULL; - gFreePcidInfo.outDirPathFinal = outDirPath; + struct FreeEncodePcidInfo freePcidInfo; + freePcidInfo.contextBuffer = NULL; + freePcidInfo.priSyscapFull = NULL; + freePcidInfo.output = NULL; + freePcidInfo.outDirPathFinal = outDirPath; PCIDMain *pcidMain = NULL; - ret = GetFileContext(inputFile, &gFreePcidInfo.contextBuffer, (uint32_t *)&bufferLen); + ret = GetFileContext(inputFile, &freePcidInfo.contextBuffer, (uint32_t *)&bufferLen); if (ret != 0) { PRINT_ERR("Get pcid file failed, pcid file path: %s\n", inputFile); return -1; @@ -755,10 +824,10 @@ 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: %u\n", bufferLen); - return FreeAfterEncodePCID(gFreePcidInfo, FREE_ENCODE_PCID_CONTEXT_OUT, ret); + return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_CONTEXT_OUT, ret); } - pcidMain = (PCIDMain *)gFreePcidInfo.contextBuffer; + pcidMain = (PCIDMain *)freePcidInfo.contextBuffer; privateSyscap = (char *)(pcidMain + 1); privateSyscapLen = strlen(privateSyscap); @@ -772,89 +841,21 @@ int32_t EncodePcidscToString(char *inputFile, char *outDirPath) } } if (priSyscapCount == 0) { - return GetEncodePCIDOut(priSyscapCount, privateSyscapLen, mainSyscap, gFreePcidInfo, ret); + return GetEncodePCIDOut(priSyscapCount, privateSyscapLen, mainSyscap, freePcidInfo, ret); } - gFreePcidInfo.priSyscapFull = (char *)malloc(priSyscapCount * SINGLE_SYSCAP_LEN); - if (gFreePcidInfo.priSyscapFull == NULL) { + freePcidInfo.priSyscapFull = (char *)malloc(priSyscapCount * SINGLE_SYSCAP_LEN); + if (freePcidInfo.priSyscapFull == NULL) { PRINT_ERR("malloc failed\n"); - return FreeAfterEncodePCID(gFreePcidInfo, FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT, ret); + return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT, ret); } - (void)memset_s(gFreePcidInfo.priSyscapFull, priSyscapCount * SINGLE_SYSCAP_LEN, + (void)memset_s(freePcidInfo.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(gFreePcidInfo.priSyscapFull + j * SINGLE_SYSCAP_LEN, SINGLE_SYSCAP_LEN, - "SystemCapability.%s", tempSyscap); - if (ret == -1) { - PRINT_ERR("sprintf_s failed\n"); - return FreeAfterEncodePCID(gFreePcidInfo, FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT, ret); - } - temp = tempSyscap; - privateSyscap++; - j++; - continue; - } - *temp++ = *privateSyscap++; - } - // output - return GetEncodePCIDOut(priSyscapCount, privateSyscapLen, mainSyscap, gFreePcidInfo, ret); -} -int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapLen, uint32_t *mainSyscap, - struct FreeEncodePcidInfo gFreePcidInfo, int32_t ret) -{ - // 17, size of "SystemCapability." - uint32_t outputLen = U32_TO_STR_MAX_LEN * PCID_OUT_BUFFER + 17 * priSyscapCount + privateSyscapLen + 1; - char *output = NULL; - uint32_t i; - output = (char *)malloc(outputLen); - if (output == NULL) { - PRINT_ERR("malloc failed\n"); - return FreeAfterEncodePCID(gFreePcidInfo, FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT, ret); - } - (void)memset_s(output, outputLen, 0, outputLen); - ret = sprintf_s(output, outputLen, "%u", mainSyscap[0]); + ret = CheckPrivateSyCap(freePcidInfo, privateSyscapLen, privateSyscap, ret); if (ret == -1) { - PRINT_ERR("sprintf_s failed\n"); - return FreeAfterEncodePCID(gFreePcidInfo, FREE_ENCODE_PCID_OUT, ret); - } - for (i = 1; i < PCID_OUT_BUFFER; i++) { - ret = sprintf_s(output, outputLen, "%s,%u", output, mainSyscap[i]); - if (ret == -1) { - PRINT_ERR("sprintf_s failed\n"); - return FreeAfterEncodePCID(gFreePcidInfo, FREE_ENCODE_PCID_OUT, ret); - } - } - for (i = 0; i < priSyscapCount; i++) { - ret = sprintf_s(output, outputLen, "%s,%s", output, gFreePcidInfo.priSyscapFull + i * SINGLE_SYSCAP_LEN); - if (ret == -1) { - PRINT_ERR("sprintf_s failed\n"); - return FreeAfterEncodePCID(gFreePcidInfo, FREE_ENCODE_PCID_OUT, ret); - } - } - // save as file - const char outputFileName[] = "PCID.txt"; - ret = ConvertedContextSaveAsFile(gFreePcidInfo.outDirPathFinal, outputFileName, output, strlen(output)); - if (ret != 0) { - PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", - gFreePcidInfo.outDirPathFinal, outputFileName); + return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT, ret); } - return FreeAfterEncodePCID(gFreePcidInfo, FREE_ENCODE_PCID_OUT, ret); -} -int32_t FreeAfterEncodePCID(struct FreeEncodePcidInfo gFreePcidInfo, int32_t type, int32_t ret) -{ - switch (type) { - case FREE_ENCODE_PCID_OUT: - free(gFreePcidInfo.output); - case FREE_ENCODE_PCID_PRISYSCAP_FULL_OUT: - free(gFreePcidInfo.priSyscapFull); - case FREE_ENCODE_PCID_CONTEXT_OUT: - default: - free(gFreePcidInfo.contextBuffer); - } - return ret; + // output + return GetEncodePCIDOut(priSyscapCount, privateSyscapLen, mainSyscap, freePcidInfo, ret); } \ No newline at end of file diff --git a/src/syscap_tool.c b/src/syscap_tool.c index a0c3882f15ec0d94c32874b0fcd15c7566ca1c8e..1bc98ea867ecb0f8ccfeaf3a4ddd4876dd6aaa16 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -44,110 +44,141 @@ #define U32_TO_STR_MAX_LEN 11 #define STRING_FORMAT_LEN_MAX 1024 -int32_t RPCIDEncode(char *inputFile, char *outputPath) +#define FREE_CONVERT_OUT_RPCID_ENCODE 1 +#define FREE_CONTEXT_OUT_RPCID_ENCODE 2 + +#define FREE_OUTBUFFER_AFTER_RPCIDSC 1 +#define FREE_MALLOC_RPISYSCAP_AFTER_RPCIDSC 2 +#define FREE_MALLOC_OSSYSCAP_AFTER_RPCIDSC 3 +#define FREE_WHOLE_SYSCAP_AFTER_RPCIDSC 4 +#define FREE_RPCID_ROOT_AFTER_RPCIDSC 5 +#define FREE_CONTEXT_OUT_AFTER_RPCIDSC 6 + +struct JsonObjectSysCap { + cJSON *cjsonObjectRoot; + cJSON *sysCapPtr; +}; + +struct FreeAfterEncodeRpcidscInfo { + char *outBuffer; + char *priSyscapArray; + uint16_t *osSysCapIndex; + cJSON *sysCapDefine; + cJSON *rpcidRoot; + char *contextBuffer; + int32_t type; + int16_t flag; +}; + +static int32_t FillOsCapLength(char *convertedBuffer, char *contextBuffer, struct JsonObjectSysCap gJsonObjectSysCap, + uint32_t sysCapSize, int32_t ret) { - int32_t ret; - char *contextBuffer = NULL; - uint32_t bufferLen, sysCapSize; - char *convertedBuffer = NULL; - uint32_t convertedBufLen = sizeof(RPCIDHead); RPCIDHead *headPtr = NULL; char *fillTmpPtr = NULL; - cJSON *cjsonObjectRoot = NULL; cJSON *apiVerItem = NULL; - cJSON *sysCapPtr = NULL; cJSON *arrayItemPtr = NULL; - ret = GetFileContext(inputFile, &contextBuffer, &bufferLen); - if (ret != 0) { - PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); - return ret; - } - - cjsonObjectRoot = cJSON_ParseWithLength(contextBuffer, bufferLen); - if (cjsonObjectRoot == NULL) { - PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer); - ret = -1; - goto FREE_CONTEXT_OUT; - } - - sysCapPtr = cJSON_GetObjectItem(cjsonObjectRoot, "syscap"); - if (sysCapPtr == NULL || !cJSON_IsArray(sysCapPtr)) { - PRINT_ERR("get \"syscap\" object failed.\n"); - ret = -1; - goto FREE_CONTEXT_OUT; - } - - ret = cJSON_GetArraySize(sysCapPtr); - if (ret < 0) { - PRINT_ERR("get \"syscap\" array size failed\n"); - ret = -1; - goto FREE_CONTEXT_OUT; - } - sysCapSize = (uint32_t)ret; - // 2, to save SysCaptype & SysCapLength - convertedBufLen += (2 * sizeof(uint16_t) + sysCapSize * SINGLE_FEAT_LEN); - - convertedBuffer = (char *)malloc(convertedBufLen); - if (convertedBuffer == NULL) { - PRINT_ERR("malloc failed\n"); - ret = -1; - goto FREE_CONTEXT_OUT; - } - (void)memset_s(convertedBuffer, convertedBufLen, 0, convertedBufLen); - headPtr = (RPCIDHead *)convertedBuffer; - apiVerItem = cJSON_GetObjectItem(cjsonObjectRoot, "api_version"); + apiVerItem = cJSON_GetObjectItem(gJsonObjectSysCap.cjsonObjectRoot, "api_version"); if (apiVerItem == NULL || !cJSON_IsNumber(apiVerItem)) { PRINT_ERR("get \"api_version\" failed\n"); - ret = -1; - goto FREE_CONVERT_OUT; + return -1; } headPtr->apiVersion = HtonsInter((uint16_t)apiVerItem->valueint); headPtr->apiVersionType = 1; fillTmpPtr = convertedBuffer + sizeof(RPCIDHead); - *(uint16_t *)fillTmpPtr = HtonsInter(2); // 2, SysCap Type, 2: request Cap fillTmpPtr += sizeof(uint16_t); // fill osCap Length *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(sysCapSize * SINGLE_FEAT_LEN)); fillTmpPtr += sizeof(uint16_t); for (uint32_t i = 0; i < sysCapSize; i++) { - arrayItemPtr = cJSON_GetArrayItem(sysCapPtr, (int)i); + arrayItemPtr = cJSON_GetArrayItem(gJsonObjectSysCap.sysCapPtr, (int)i); char *pointPos = strchr(arrayItemPtr->valuestring, '.'); if (pointPos == NULL) { PRINT_ERR("context of \"syscap\" array is invalid\n"); - ret = -1; - goto FREE_CONVERT_OUT; + return -1; } ret = strncmp(arrayItemPtr->valuestring, "SystemCapability.", pointPos - arrayItemPtr->valuestring + 1); if (ret != 0) { PRINT_ERR("context of \"syscap\" array is invalid\n"); - ret = -1; - goto FREE_CONVERT_OUT; + return -1; } ret = memcpy_s(fillTmpPtr, SINGLE_FEAT_LEN, pointPos + 1, strlen(pointPos + 1)); if (ret != 0) { PRINT_ERR("context of \"syscap\" array is invalid\n"); - ret = -1; - goto FREE_CONVERT_OUT; + return -1; } fillTmpPtr += SINGLE_FEAT_LEN; } + return ret; +} +static int32_t FreeAfterRPCIDEncode(char *convertedBuffer, char *contextBuffer, int32_t type, int32_t ret) +{ + if (type == FREE_CONVERT_OUT_RPCID_ENCODE) { + free(convertedBuffer); + } + FreeContextBuffer(contextBuffer); + return ret; +} + +int32_t RPCIDEncode(char *inputFile, char *outputPath) +{ + int32_t ret; + char *contextBuffer = NULL; + uint32_t bufferLen, sysCapSize; + char *convertedBuffer = NULL; + uint32_t convertedBufLen = sizeof(RPCIDHead); + struct JsonObjectSysCap gJsonObjectSysCap; + gJsonObjectSysCap.cjsonObjectRoot = NULL; + gJsonObjectSysCap.sysCapPtr = NULL; + + ret = GetFileContext(inputFile, &contextBuffer, &bufferLen); + if (ret != 0) { + PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile); + return ret; + } + + gJsonObjectSysCap.cjsonObjectRoot = cJSON_ParseWithLength(contextBuffer, bufferLen); + if (gJsonObjectSysCap.cjsonObjectRoot == NULL) { + PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer); + return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONTEXT_OUT_RPCID_ENCODE, -1); + } + + gJsonObjectSysCap.sysCapPtr = cJSON_GetObjectItem(gJsonObjectSysCap.cjsonObjectRoot, "syscap"); + if (gJsonObjectSysCap.sysCapPtr == NULL || !cJSON_IsArray(gJsonObjectSysCap.sysCapPtr)) { + PRINT_ERR("get \"syscap\" object failed.\n"); + return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONTEXT_OUT_RPCID_ENCODE, -1); + } + + ret = cJSON_GetArraySize(gJsonObjectSysCap.sysCapPtr); + if (ret < 0) { + PRINT_ERR("get \"syscap\" array size failed\n"); + return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONTEXT_OUT_RPCID_ENCODE, -1); + } + sysCapSize = (uint32_t)ret; + // 2, to save SysCaptype & SysCapLength + convertedBufLen += (2 * sizeof(uint16_t) + sysCapSize * SINGLE_FEAT_LEN); + + convertedBuffer = (char *)malloc(convertedBufLen); + if (convertedBuffer == NULL) { + PRINT_ERR("malloc failed\n"); + return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONTEXT_OUT_RPCID_ENCODE, -1); + } + (void)memset_s(convertedBuffer, convertedBufLen, 0, convertedBufLen); + + ret = FillOsCapLength(convertedBuffer, contextBuffer, gJsonObjectSysCap, sysCapSize, ret); + if (ret == -1) { + return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONVERT_OUT_RPCID_ENCODE, ret); + } ret = ConvertedContextSaveAsFile(outputPath, "RPCID.sc", convertedBuffer, convertedBufLen); if (ret != 0) { PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.sc\n", outputPath); - goto FREE_CONVERT_OUT; } - -FREE_CONVERT_OUT: - free(convertedBuffer); -FREE_CONTEXT_OUT: - FreeContextBuffer(contextBuffer); - return ret; + return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONVERT_OUT_RPCID_ENCODE, ret); } static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson) @@ -256,140 +287,187 @@ static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, const uint16_t *inde return 0; } -int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath) +static int32_t PrintOutputToFile(struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo, uint16_t outBufferLen, + uint32_t outUint[RPCID_OUT_BUFFER], uint16_t indexPri, char *outDirPath) { int32_t ret = 0; - int32_t sysCapArraySize; - uint32_t bufferLen; - uint16_t indexPri = 0; - uint16_t *osSysCapIndex; - char *contextBuffer = NULL; - char *priSyscapArray = NULL; - char *priSyscap = NULL; - cJSON *cJsonTemp = NULL; - cJSON *rpcidRoot = NULL; - cJSON *sysCapDefine = NULL; - cJSON *sysCapArray = NULL; - - // check rpcid.sc - if (CheckRpcidFormat(inputFile, &contextBuffer, &bufferLen) != 0) { - PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile); - goto FREE_CONTEXT_OUT; + freeAfterEncodeRpcidscInfo.type = FREE_OUTBUFFER_AFTER_RPCIDSC; + (void)memset_s(freeAfterEncodeRpcidscInfo.outBuffer, outBufferLen, 0, outBufferLen); + ret = sprintf_s(freeAfterEncodeRpcidscInfo.outBuffer, outBufferLen, "%u", outUint[0]); + if (ret == -1) { + PRINT_ERR("sprintf_s failed.\n"); + freeAfterEncodeRpcidscInfo.flag = 1; + return ret; } - - // parse rpcid to json - rpcidRoot = cJSON_CreateObject(); - if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot) != 0) { - PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile); - goto FREE_RPCID_ROOT; + for (int i = 1; i < RPCID_OUT_BUFFER; i++) { + ret = sprintf_s(freeAfterEncodeRpcidscInfo.outBuffer, outBufferLen, "%s,%u", + freeAfterEncodeRpcidscInfo.outBuffer, outUint[i]); + if (ret == -1) { + PRINT_ERR("sprintf_s failed.\n"); + freeAfterEncodeRpcidscInfo.flag = 1; + return ret; + } } - // 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; + for (uint16_t i = 0; i < indexPri; i++) { + ret = sprintf_s(freeAfterEncodeRpcidscInfo.outBuffer, outBufferLen, "%s,%s", + freeAfterEncodeRpcidscInfo.outBuffer, freeAfterEncodeRpcidscInfo.priSyscapArray + i * SINGLE_SYSCAP_LEN); + if (ret == -1) { + PRINT_ERR("sprintf_s failed.\n"); + freeAfterEncodeRpcidscInfo.flag = 1; + return ret; + } } - // 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; + + const char outputFilename[] = "RPCID.txt"; + ret = ConvertedContextSaveAsFile(outDirPath, outputFilename, freeAfterEncodeRpcidscInfo.outBuffer, + strlen(freeAfterEncodeRpcidscInfo.outBuffer)); + if (ret != 0) { + PRINT_ERR("Save to txt file failed. Output path:%s/%s\n", outDirPath, outputFilename); + freeAfterEncodeRpcidscInfo.flag = 1; } - (void)memset_s(osSysCapIndex, sizeof(uint16_t) * sysCapArraySize, + return ret; +} + +static int32_t OutputSetMemAndPrintToFile(struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo, + int32_t sysCapArraySize, cJSON *sysCapArray, char *outDirPath) +{ + char *priSyscap = NULL; + cJSON *cJsonTemp = NULL; + uint16_t indexPri = 0; + int32_t ret = 0; + freeAfterEncodeRpcidscInfo.type = FREE_MALLOC_OSSYSCAP_AFTER_RPCIDSC; + + (void)memset_s(freeAfterEncodeRpcidscInfo.osSysCapIndex, sizeof(uint16_t) * sysCapArraySize, 0, sizeof(uint16_t) * sysCapArraySize); // malloc for save private syscap string - priSyscapArray = (char *)malloc((uint32_t)sysCapArraySize * SINGLE_SYSCAP_LEN); - if (priSyscapArray == NULL) { + freeAfterEncodeRpcidscInfo.priSyscapArray = (char *)malloc((uint32_t)sysCapArraySize * SINGLE_SYSCAP_LEN); + if (freeAfterEncodeRpcidscInfo.priSyscapArray == NULL) { PRINT_ERR("malloc(%d) failed.\n", sysCapArraySize * SINGLE_SYSCAP_LEN); - goto FREE_MALLOC_OSSYSCAP; + freeAfterEncodeRpcidscInfo.flag = 1; + return ret; } - (void)memset_s(priSyscapArray, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN), + (void)memset_s(freeAfterEncodeRpcidscInfo.priSyscapArray, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN), 0, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN)); - priSyscap = priSyscapArray; + priSyscap = freeAfterEncodeRpcidscInfo.priSyscapArray; // part os syscap and ptivate syscap uint16_t indexOs = 0; for (int i = 0; i < sysCapArraySize; i++) { cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i); - cJsonTemp = cJSON_GetObjectItem(sysCapDefine, cJsonItem->valuestring); + cJsonTemp = cJSON_GetObjectItem(freeAfterEncodeRpcidscInfo.sysCapDefine, cJsonItem->valuestring); if (cJsonTemp != NULL) { - osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint); + freeAfterEncodeRpcidscInfo.osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint); } else { ret = strcpy_s(priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring); if (ret != EOK) { PRINT_ERR("strcpy_s failed.\n"); - goto FREE_MALLOC_PRISYSCAP; + freeAfterEncodeRpcidscInfo.flag = 1; + return ret; } priSyscap += SINGLE_SYSCAP_LEN; indexPri++; } } uint32_t outUint[RPCID_OUT_BUFFER] = {0}; - outUint[0] = *(uint32_t *)contextBuffer; - outUint[1] = *(uint32_t *)(contextBuffer + sizeof(uint32_t)); + outUint[0] = *(uint32_t *)freeAfterEncodeRpcidscInfo.contextBuffer; + outUint[1] = *(uint32_t *)(freeAfterEncodeRpcidscInfo.contextBuffer + sizeof(uint32_t)); uint8_t *osOutUint = (uint8_t *)(outUint + 2); - if (SetOsSysCapBitMap(osOutUint, 120, osSysCapIndex, indexOs) != 0) { // 120, len of osOutUint + // 120, len of osOutUint + if (SetOsSysCapBitMap(osOutUint, 120, freeAfterEncodeRpcidscInfo.osSysCapIndex, indexOs) != 0) { PRINT_ERR("Set os syscap bit map failed.\n"); - goto FREE_MALLOC_PRISYSCAP; + freeAfterEncodeRpcidscInfo.flag = 1; + return ret; } - uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER - + SINGLE_SYSCAP_LEN * indexPri; - char *outBuffer = (char *)malloc(outBufferLen); - if (outBuffer == NULL) { + uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER + SINGLE_SYSCAP_LEN * indexPri; + freeAfterEncodeRpcidscInfo.outBuffer = (char *)malloc(outBufferLen); + if (freeAfterEncodeRpcidscInfo.outBuffer == NULL) { PRINT_ERR("malloc(%u) failed.\n", outBufferLen); - goto FREE_MALLOC_PRISYSCAP; + freeAfterEncodeRpcidscInfo.flag = 1; + return ret; } - (void)memset_s(outBuffer, outBufferLen, 0, outBufferLen); + return PrintOutputToFile(freeAfterEncodeRpcidscInfo, outBufferLen, outUint, indexPri, outDirPath); +} - ret = sprintf_s(outBuffer, outBufferLen, "%u", outUint[0]); - if (ret == -1) { - PRINT_ERR("sprintf_s failed.\n"); - goto FREE_OUTBUFFER; +static int32_t FreeAfterEncodeRpcidsc(struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo, int32_t type, + int32_t ret) +{ + switch (type) { + case FREE_OUTBUFFER_AFTER_RPCIDSC: + free(freeAfterEncodeRpcidscInfo.outBuffer); + /* fall-through */ + case FREE_MALLOC_RPISYSCAP_AFTER_RPCIDSC: + free(freeAfterEncodeRpcidscInfo.priSyscapArray); + /* fall-through */ + case FREE_MALLOC_OSSYSCAP_AFTER_RPCIDSC: + free(freeAfterEncodeRpcidscInfo.osSysCapIndex); + /* fall-through */ + case FREE_WHOLE_SYSCAP_AFTER_RPCIDSC: + cJSON_Delete(freeAfterEncodeRpcidscInfo.sysCapDefine); + /* fall-through */ + case FREE_RPCID_ROOT_AFTER_RPCIDSC: + cJSON_Delete(freeAfterEncodeRpcidscInfo.rpcidRoot); + /* fall-through */ + case FREE_CONTEXT_OUT_AFTER_RPCIDSC: + default: + FreeContextBuffer(freeAfterEncodeRpcidscInfo.contextBuffer); } - for (int 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"); - goto FREE_OUTBUFFER; - } + return ret; +} + +int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath) +{ + int32_t ret = 0; + int32_t sysCapArraySize; + uint32_t bufferLen; + cJSON *sysCapArray = NULL; + struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo; + freeAfterEncodeRpcidscInfo.priSyscapArray = NULL; + freeAfterEncodeRpcidscInfo.osSysCapIndex = NULL; + freeAfterEncodeRpcidscInfo.sysCapDefine = NULL; + freeAfterEncodeRpcidscInfo.rpcidRoot = NULL; + freeAfterEncodeRpcidscInfo.contextBuffer = NULL; + freeAfterEncodeRpcidscInfo.type = 0; + freeAfterEncodeRpcidscInfo.flag = 0; + + // check rpcid.sc + if (CheckRpcidFormat(inputFile, &freeAfterEncodeRpcidscInfo.contextBuffer, &bufferLen) != 0) { + PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile); + return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_CONTEXT_OUT_AFTER_RPCIDSC, ret); } - for (uint16_t i = 0; i < indexPri; i++) { - ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, - priSyscapArray + i * SINGLE_SYSCAP_LEN); - if (ret == -1) { - PRINT_ERR("sprintf_s failed.\n"); - goto FREE_OUTBUFFER; - } + // parse rpcid to json + freeAfterEncodeRpcidscInfo.rpcidRoot = cJSON_CreateObject(); + if (ParseRpcidToJson(freeAfterEncodeRpcidscInfo.contextBuffer, bufferLen, + freeAfterEncodeRpcidscInfo.rpcidRoot) != 0) { + PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile); + return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_RPCID_ROOT_AFTER_RPCIDSC, ret); } - const char outputFilename[] = "RPCID.txt"; - ret = ConvertedContextSaveAsFile(outDirPath, outputFilename, outBuffer, strlen(outBuffer)); - if (ret != 0) { - PRINT_ERR("Save to txt file failed. Output path:%s/%s\n", outDirPath, outputFilename); - goto FREE_OUTBUFFER; + // trans to string format + freeAfterEncodeRpcidscInfo.sysCapDefine = CreateWholeSyscapJsonObj(); + sysCapArray = cJSON_GetObjectItem(freeAfterEncodeRpcidscInfo.rpcidRoot, "syscap"); + if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) { + PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile); + return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_WHOLE_SYSCAP_AFTER_RPCIDSC, ret); + } + sysCapArraySize = cJSON_GetArraySize(sysCapArray); + if (sysCapArraySize < 0) { + PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile); + return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_WHOLE_SYSCAP_AFTER_RPCIDSC, ret); + } + // malloc for save os syscap index + freeAfterEncodeRpcidscInfo.osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) * sysCapArraySize); + if (freeAfterEncodeRpcidscInfo.osSysCapIndex == NULL) { + PRINT_ERR("malloc failed.\n"); + return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_WHOLE_SYSCAP_AFTER_RPCIDSC, ret); } -FREE_OUTBUFFER: - free(outBuffer); -FREE_MALLOC_PRISYSCAP: - free(priSyscapArray); -FREE_MALLOC_OSSYSCAP: - free(osSysCapIndex); -FREE_WHOLE_SYSCAP: - cJSON_Delete(sysCapDefine); -FREE_RPCID_ROOT: - cJSON_Delete(rpcidRoot); -FREE_CONTEXT_OUT: - FreeContextBuffer(contextBuffer); - return ret; + ret = OutputSetMemAndPrintToFile(freeAfterEncodeRpcidscInfo, sysCapArraySize, sysCapArray, outDirPath); + if (freeAfterEncodeRpcidscInfo.flag == 1) { + return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, freeAfterEncodeRpcidscInfo.type, ret); + } + return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_OUTBUFFER_AFTER_RPCIDSC, ret); } char *CopyInputString(const char *inputString) @@ -607,4 +685,4 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ printf("Fail! The pcid does not meet the rpcid\n"); } return 0; -} +} \ No newline at end of file