!286 拆分部分函数以降低函数的大小。

Merge pull request !286 from chenyude/split
This commit is contained in:
openharmony_ci
2023-06-27 03:06:24 +00:00
committed by Gitee
4 changed files with 398 additions and 346 deletions
+21 -23
View File
@@ -242,53 +242,52 @@ bool DecodeOsSyscap(const char input[PCID_MAIN_BYTES], char (**output)[SINGLE_SY
return true;
}
bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt)
int32_t GetPriSyscapCount(char *input)
{
char (*outputArray)[SINGLE_SYSCAP_LEN] = NULL;
int32_t syscapCnt = 0;
char *inputPos = input;
int bufferLen, ret;
int syscapCnt = 0;
if (input == NULL) {
*output = outputArray;
*outputCnt = syscapCnt;
return false;
}
while (*inputPos != '\0') {
if (*inputPos == ',') {
syscapCnt++;
}
inputPos++;
}
return syscapCnt;
}
bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt)
{
char *inputPos = input;
char (*outputArray)[SINGLE_SYSCAP_LEN] = NULL;
if (input == NULL) {
return false;
}
int syscapCnt = GetPriSyscapCount(inputPos);
*outputCnt = syscapCnt;
if (syscapCnt == 0) {
*output = outputArray;
*outputCnt = syscapCnt;
return true;
}
inputPos = input;
bufferLen = SINGLE_SYSCAP_LEN * syscapCnt;
int 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;
return false;
}
(void)memset_s(outputArray, bufferLen, 0, bufferLen);
*output = outputArray;
inputPos = input;
char buffer[SINGLE_FEAT_LEN] = {0};
char *bufferPos = buffer;
while (*inputPos != '\0') {
if (*inputPos == ',') {
*bufferPos = '\0';
ret = sprintf_s(*outputArray, SINGLE_SYSCAP_LEN, "SystemCapability.%s", buffer);
if (ret == -1) {
PRINT_ERR("sprintf_s failed\n");
*outputCnt = 0;
if (sprintf_s(*outputArray, SINGLE_SYSCAP_LEN, "SystemCapability.%s", buffer) == -1) {
free(outputArray);
outputArray = NULL;
return false;
}
bufferPos = buffer;
@@ -299,7 +298,6 @@ bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *o
*bufferPos++ = *inputPos++;
}
*outputCnt = syscapCnt;
return true;
}
+262 -227
View File
@@ -167,82 +167,171 @@ static cJSON *CreateWholeSyscapJsonObj(void)
return root;
}
int32_t CreatePCID(char *inputFile, char *outDirPath)
int32_t SetOsSyscap(PCIDMain *pcidBuffer, uint32_t osCapSize,
const cJSON *jsonOsSyscapObj, const cJSON *allOsSyscapObj)
{
int32_t ret, sectorOfBits, posOfBits;
uint32_t i, privateCapSize, osCapSize;
size_t contextBufLen;
errno_t nRet = 0;
char *contextBuffer = NULL;
char *systemType = NULL;
cJSON *jsonRootObj = NULL;
cJSON *jsonSyscapObj = NULL;
cJSON *jsonOsSyscapObj = NULL;
cJSON *jsonPriSyscapObj = NULL;
int32_t sectorOfBits, posOfBits;
cJSON *jsonArrayItem = NULL;
cJSON *osCapIndex = NULL;
cJSON *allOsSyscapObj = CreateWholeSyscapJsonObj();
ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen);
if (ret != 0) {
PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
for (uint32_t i = 0; i < osCapSize; i++) {
jsonArrayItem = cJSON_GetArrayItem(jsonOsSyscapObj, (int)i);
osCapIndex = cJSON_GetObjectItem(allOsSyscapObj, jsonArrayItem->valuestring);
if (osCapIndex == NULL) {
PRINT_ERR("can't find the syscap: %s, please add it in syscap_define.h.\n", jsonArrayItem->valuestring);
return -1;
}
sectorOfBits = (osCapIndex->valueint) / UINT8_BIT;
posOfBits = (osCapIndex->valueint) % UINT8_BIT;
if (sectorOfBits >= OS_SYSCAP_BYTES) {
PRINT_ERR("num of \"os syscap\" is out of 960\n");
return -1;
}
pcidBuffer->osSyscap[sectorOfBits] |= 1 << (posOfBits);
}
return 0;
}
int32_t SetPriSyscap(PCIDMain *pcidBuffer, cJSON *jsonPriSyscapObj,
uint32_t privateCapSize, uint16_t allPriSyscapStrLen)
{
char *priSyscapHead = (char *)(pcidBuffer + 1);
char *priSyscapStr = NULL;
for (uint32_t i = 0; i < privateCapSize; i++) {
cJSON *jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, (int)i);
priSyscapStr = strchr(jsonArrayItem->valuestring, '.') + 1;
errno_t nRet = strcat_s(priSyscapHead, allPriSyscapStrLen + 1, priSyscapStr);
nRet += strcat_s(priSyscapHead, allPriSyscapStrLen + 1, ",");
if (nRet != EOK) {
PRINT_ERR("strcat_s \"pri\" string is failed\n");
return -1;
}
}
return 0;
}
int32_t SetPCIDHeader(PCIDMain *pcidBuffer, const cJSON *jsonRootObj)
{
cJSON *jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "api_version");
if (jsonSyscapObj == NULL || !cJSON_IsNumber(jsonSyscapObj)) {
PRINT_ERR("get \"api_version\" failed\n");
return -1;
}
pcidBuffer->apiVersion = HtonsInter((uint16_t)jsonSyscapObj->valueint);
pcidBuffer->apiVersionType = 0;
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "system_type");
if (jsonSyscapObj == NULL || !cJSON_IsString(jsonSyscapObj)) {
PRINT_ERR("get \"system_type\" failed\n");
return -1;
}
char *systemType = jsonSyscapObj->valuestring;
pcidBuffer->systemType = !strcmp(systemType, "mini") ? 0b001 :
(!strcmp(systemType, "small") ? 0b010 :
(!strcmp(systemType, "standard") ? 0b100 : 0));
if (pcidBuffer->systemType == 0) {
PRINT_ERR("\"system_type\" is invaild, systemType = \"%s\"\n", systemType);
return -1;
}
jsonRootObj = cJSON_ParseWithLength(contextBuffer, contextBufLen);
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "manufacturer_id");
if (jsonSyscapObj == NULL || !cJSON_IsNumber(jsonSyscapObj)) {
PRINT_ERR("get \"manufacturer_id\" failed\n");
return -1;
}
pcidBuffer->manufacturerID = HtonlInter((uint32_t)jsonSyscapObj->valueint);
return 0;
}
int32_t GetOsAndPriSyscapSize(const cJSON *osSyscap, const cJSON *priSyscap,
uint32_t *osCapSize, uint32_t *privateCapSize)
{
*osCapSize = 0;
*privateCapSize = 0;
// get os syscap size
if (osSyscap == NULL || !cJSON_IsArray(osSyscap)) {
PRINT_ERR("get \"os\" array failed\n");
return -1;
}
int32_t ret = cJSON_GetArraySize(osSyscap);
if (ret < 0) {
PRINT_ERR("get \"os\" array size failed\n");
return -1;
}
*osCapSize = (uint32_t)ret;
// get private syscap size
if (priSyscap != NULL && cJSON_IsArray(priSyscap)) {
ret = cJSON_GetArraySize(priSyscap);
if (ret < 0) {
PRINT_ERR("get \"private syscap\" array size failed\n");
return -1;
}
*privateCapSize = (uint32_t)ret;
} else if (priSyscap == NULL) {
*privateCapSize = 0;
} else {
PRINT_ERR("get \"private\" array failed\n");
return -1;
}
return 0;
}
int32_t GetPriSyscapLen(uint32_t privateCapSize, cJSON *jsonPriSyscapObj, uint16_t *len)
{
for (uint32_t i = 0; i < privateCapSize; i++) {
cJSON *jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, (int)i);
*len += (uint16_t)strlen(strchr(jsonArrayItem->valuestring, '.') + 1);
(*len)++; // for separator ','
}
if ((*len + 1) > PRIVATE_SYSCAP_SIZE) {
PRINT_ERR("context of \"pri\" array is too many.\n");
return -1;
}
return 0;
}
int32_t CreatePCID(char *inputFile, char *outDirPath)
{
uint32_t privateCapSize, osCapSize;
size_t contextBufLen;
char *contextBuffer = NULL;
cJSON *allOsSyscapObj = CreateWholeSyscapJsonObj();
int32_t ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen);
if (ret != 0) {
PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
goto FREE_CONVERT_OUT;
}
cJSON *jsonRootObj = cJSON_ParseWithLength(contextBuffer, contextBufLen);
if (jsonRootObj == NULL) {
PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer);
ret = -1;
goto FREE_CONVERT_OUT;
}
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "syscap");
cJSON *jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "syscap");
if (jsonSyscapObj == NULL || !cJSON_IsObject(jsonSyscapObj)) {
PRINT_ERR("get \"syscap\" object failed\n");
ret = -1;
goto FREE_CONVERT_OUT;
}
jsonOsSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "os");
if (jsonOsSyscapObj == NULL || !cJSON_IsArray(jsonOsSyscapObj)) {
PRINT_ERR("get \"os\" array failed\n");
ret = -1;
goto FREE_CONVERT_OUT;
}
ret = cJSON_GetArraySize(jsonOsSyscapObj);
if (ret < 0) {
PRINT_ERR("get \"os\" array size failed\n");
ret = -1;
goto FREE_CONVERT_OUT;
}
osCapSize = (uint32_t)ret;
jsonPriSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "private");
if (jsonPriSyscapObj != NULL && cJSON_IsArray(jsonPriSyscapObj)) {
ret = cJSON_GetArraySize(jsonPriSyscapObj);
if (ret < 0) {
PRINT_ERR("get \"private syscap\" array size failed\n");
ret = -1;
goto FREE_CONVERT_OUT;
}
privateCapSize = (uint32_t)ret;
} else if (jsonPriSyscapObj == NULL) {
privateCapSize = 0;
} else {
PRINT_ERR("get \"private\" array failed\n");
ret = -1;
cJSON *jsonOsSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "os");
cJSON *jsonPriSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "private");
ret = GetOsAndPriSyscapSize(jsonOsSyscapObj, jsonPriSyscapObj, &osCapSize, &privateCapSize);
if (ret != 0) {
goto FREE_CONVERT_OUT;
}
uint16_t allPriSyscapStrLen = 0;
for (i = 0; i < privateCapSize; i++) {
jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, (int)i);
allPriSyscapStrLen += (uint16_t)strlen(strchr(jsonArrayItem->valuestring, '.') + 1);
allPriSyscapStrLen++; // for separator ','
}
if ((allPriSyscapStrLen + 1) > PRIVATE_SYSCAP_SIZE) {
PRINT_ERR("context of \"pri\" array is too many.\n");
ret = -1;
ret = GetPriSyscapLen(privateCapSize, jsonPriSyscapObj, &allPriSyscapStrLen);
if (ret != 0) {
goto FREE_CONVERT_OUT;
}
@@ -254,98 +343,124 @@ int32_t CreatePCID(char *inputFile, char *outDirPath)
goto FREE_CONVERT_OUT;
}
(void)memset_s(pcidBuffer, pcidLength, 0, pcidLength);
// process os syscap
for (i = 0; i < osCapSize; i++) {
jsonArrayItem = cJSON_GetArrayItem(jsonOsSyscapObj, (int)i);
osCapIndex = cJSON_GetObjectItem(allOsSyscapObj, jsonArrayItem->valuestring);
if (osCapIndex == NULL) {
PRINT_ERR("can't find the syscap: %s, please add it in syscap_define.h.\n", jsonArrayItem->valuestring);
ret = -1;
goto FREE_CONVERT_OUT;
}
sectorOfBits = (osCapIndex->valueint) / UINT8_BIT;
posOfBits = (osCapIndex->valueint) % UINT8_BIT;
if (sectorOfBits >= OS_SYSCAP_BYTES) {
PRINT_ERR("num of \"os syscap\" is out of 960\n");
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
pcidBuffer->osSyscap[sectorOfBits] |= 1 << (posOfBits);
}
// process private syscap
char *priSyscapHead = (char *)(pcidBuffer + 1);
char *priSyscapStr = NULL;
for (i = 0; i < privateCapSize; i++) {
jsonArrayItem = cJSON_GetArrayItem(jsonPriSyscapObj, (int)i);
priSyscapStr = strchr(jsonArrayItem->valuestring, '.') + 1;
nRet = strcat_s(priSyscapHead, allPriSyscapStrLen + 1, priSyscapStr);
nRet += strcat_s(priSyscapHead, allPriSyscapStrLen + 1, ",");
if (nRet != EOK) {
PRINT_ERR("strcat_s \"pri\" string is failed\n");
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
}
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "api_version");
if (jsonSyscapObj == NULL || !cJSON_IsNumber(jsonSyscapObj)) {
PRINT_ERR("get \"api_version\" failed\n");
ret = -1;
ret = SetOsSyscap(pcidBuffer, osCapSize, jsonOsSyscapObj, allOsSyscapObj);
ret += SetPriSyscap(pcidBuffer, jsonPriSyscapObj, privateCapSize, allPriSyscapStrLen);
ret += SetPCIDHeader(pcidBuffer, jsonRootObj);
if (ret != 0) {
goto FREE_PCID_BUFFER_OUT;
}
pcidBuffer->apiVersion = HtonsInter((uint16_t)jsonSyscapObj->valueint);
pcidBuffer->apiVersionType = 0;
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "system_type");
if (jsonSyscapObj == NULL || !cJSON_IsString(jsonSyscapObj)) {
PRINT_ERR("get \"system_type\" failed\n");
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
systemType = jsonSyscapObj->valuestring;
pcidBuffer->systemType = !strcmp(systemType, "mini") ? 0b001 :
(!strcmp(systemType, "small") ? 0b010 :
(!strcmp(systemType, "standard") ? 0b100 : 0));
if (pcidBuffer->systemType == 0) {
PRINT_ERR("\"system_type\" is invaild, systemType = \"%s\"\n", systemType);
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "manufacturer_id");
if (jsonSyscapObj == NULL || !cJSON_IsNumber(jsonSyscapObj)) {
PRINT_ERR("get \"manufacturer_id\" failed\n");
ret = -1;
goto FREE_PCID_BUFFER_OUT;
}
pcidBuffer->manufacturerID = HtonlInter((uint32_t)jsonSyscapObj->valueint);
const char pcidFileName[] = "PCID.sc";
ret = ConvertedContextSaveAsFile(outDirPath, pcidFileName, (char *)pcidBuffer, pcidLength);
if (ret != 0) {
PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, pcidFileName);
ret = -1;
PRINT_ERR("Save as file failed, outDirPath:%s, filename:%s\n", outDirPath, pcidFileName);
goto FREE_PCID_BUFFER_OUT;
}
FREE_PCID_BUFFER_OUT:
free(pcidBuffer);
FREE_CONVERT_OUT:
free(allOsSyscapObj);
cJSON_Delete(allOsSyscapObj);
FreeContextBuffer(contextBuffer);
return ret;
}
int32_t DecodePCID(char *inputFile, char *outDirPath)
int32_t GetOsSyscap(PCIDMain *pcidMain, cJSON *sysCapObject)
{
int32_t ret;
errno_t nRet = 0;
char *contextBuffer = NULL;
uint32_t i, j, countOfSyscap = 0;
uint8_t osSyscap[OS_SYSCAP_BYTES] = {0};
uint16_t indexOfSyscap[OS_SYSCAP_BYTES * UINT8_BIT] = {0};
uint32_t i, j, countOfSyscap = 0;
cJSON *capVectorPtr = cJSON_CreateArray();
if (capVectorPtr == NULL) {
PRINT_ERR("cJSON_CreateArray failed\n");
return -1;
}
// 8, bytes of pcid header
errno_t nRet = memcpy_s(osSyscap, OS_SYSCAP_BYTES, (uint8_t *)pcidMain + 8, OS_SYSCAP_BYTES);
if (nRet != EOK) {
PRINT_ERR("memcpy_s failed.");
return -1;
}
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;
}
}
}
for (i = 0; i < countOfSyscap; i++) {
for (j = 0; j < sizeof(g_arraySyscap) / sizeof(SyscapWithNum); j++) {
if (g_arraySyscap[j].num == indexOfSyscap[i]) {
if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(g_arraySyscap[j].str))) {
printf("cJSON_AddItemToArray or cJSON_CreateString failed\n");
return -1;
}
}
}
}
if (!cJSON_AddItemToObject(sysCapObject, "os", capVectorPtr)) {
PRINT_ERR("cJSON_AddItemToObject failed\n");
return -1;
}
return 0;
}
int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufLen)
{
cJSON *capVectorPtr = cJSON_CreateArray();
if (capVectorPtr == NULL) {
PRINT_ERR("cJSON_CreateArray failed\n");
return -1;
}
int32_t privateSyscapLen = (int32_t)(contextBufLen - sizeof(PCIDMain) - 1);
if (privateSyscapLen < 0) {
PRINT_ERR("parse private syscap failed.");
return -1;
} else if (privateSyscapLen == 0) {
return 0;
}
char fullCapStr[SINGLE_SYSCAP_LEN] = {0};
char priSyscapStr[SINGLE_SYSCAP_LEN] = {0};
char *tempPriSyscapStr = priSyscapStr;
char *ptrPrivateSyscap = (char *)(pcidMain + 1);
while (*ptrPrivateSyscap != '\0') {
if (*ptrPrivateSyscap == ',') {
*tempPriSyscapStr = '\0';
int32_t ret = sprintf_s(fullCapStr, SINGLE_SYSCAP_LEN, "SystemCapability.%s", priSyscapStr);
if (ret == -1) {
printf("sprintf_s failed\n");
return -1;
}
if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(fullCapStr))) {
printf("cJSON_AddItemToArray or cJSON_CreateString failed\n");
return -1;
}
tempPriSyscapStr = priSyscapStr;
ptrPrivateSyscap++;
continue;
}
*tempPriSyscapStr++ = *ptrPrivateSyscap++;
}
if (!cJSON_AddItemToObject(sysCapObject, "private", capVectorPtr)) {
PRINT_ERR("cJSON_AddItemToObject failed\n");
return -1;
}
return 0;
}
int32_t DecodePCID(char *inputFile, char *outDirPath)
{
int32_t ret = 0;
char *contextBuffer = NULL;
size_t contextBufLen;
ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen);
@@ -355,7 +470,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath)
}
PCIDMain *pcidMain = (PCIDMain *)contextBuffer;
/* api version */
if (pcidMain->apiVersionType != 0) {
PRINT_ERR("Prase file failed, apiVersionType is invaild, input file : %s\n", inputFile);
@@ -373,97 +488,21 @@ int32_t DecodePCID(char *inputFile, char *outDirPath)
goto FREE_CONTEXT_OUT;
}
cJSON *capVectorPtr = cJSON_CreateArray();
if (capVectorPtr == NULL) {
PRINT_ERR("cJSON_CreateArray failed\n");
ret = -1;
goto FREE_CONTEXT_OUT;
}
nRet = memcpy_s(osSyscap, OS_SYSCAP_BYTES, (uint8_t *)pcidMain + 8, OS_SYSCAP_BYTES); // 8, bytes of pcid header
if (nRet != EOK) {
PRINT_ERR("memcpy_s failed.");
ret = -1;
goto FREE_VECTOR_OUT;
}
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;
}
}
}
for (i = 0; i < countOfSyscap; i++) {
for (j = 0; j < sizeof(g_arraySyscap) / sizeof(SyscapWithNum); j++) {
if (g_arraySyscap[j].num == indexOfSyscap[i]) {
if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(g_arraySyscap[j].str))) {
printf("cJSON_AddItemToArray or cJSON_CreateString failed\n");
ret = -1;
goto FREE_VECTOR_OUT;
}
}
}
}
cJSON *sysCapObjectPtr = cJSON_CreateObject();
if (sysCapObjectPtr == NULL) {
/* syscap */
cJSON *sysCapObj = cJSON_CreateObject();
if (sysCapObj == NULL) {
PRINT_ERR("cJSON_CreateObject failed\n");
ret = -1;
goto FREE_CONTEXT_OUT;
}
if (!cJSON_AddItemToObject(sysCapObjectPtr, "os", capVectorPtr)) {
PRINT_ERR("cJSON_AddItemToObject failed\n");
ret = -1;
goto FREE_VECTOR_OUT;
if (GetOsSyscap(pcidMain, sysCapObj) != 0) {
goto FREE_CONTEXT_OUT;
}
// private syscap
capVectorPtr = cJSON_CreateArray();
if (capVectorPtr == NULL) {
PRINT_ERR("cJSON_CreateArray failed\n");
ret = -1;
goto FREE_SYSCAP_OUT;
if (GetPriSyscap(pcidMain, sysCapObj, contextBufLen) != 0) {
goto FREE_CONTEXT_OUT;
}
char *ptrPrivateSyscap = (char *)(pcidMain + 1);
int privateSyscapLen = (int)(contextBufLen - sizeof(PCIDMain) - 1);
char priSyscapStr[SINGLE_SYSCAP_LEN] = {0};
char *tempPriSyscapStr = priSyscapStr;
char fullPriSyscapStr[SINGLE_SYSCAP_LEN] = {0};
if (privateSyscapLen < 0) {
PRINT_ERR("parse private syscap failed.");
ret = -1;
goto FREE_VECTOR_OUT;
} else if (privateSyscapLen == 0) {
goto SKIP_GET_PRIVATE;
}
while (*ptrPrivateSyscap != '\0') {
if (*ptrPrivateSyscap == ',') {
*tempPriSyscapStr = '\0';
ret = sprintf_s(fullPriSyscapStr, SINGLE_SYSCAP_LEN, "SystemCapability.%s", priSyscapStr);
if (ret == -1) {
printf("sprintf_s failed\n");
goto FREE_VECTOR_OUT;
}
if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(fullPriSyscapStr))) {
printf("cJSON_AddItemToArray or cJSON_CreateString failed\n");
ret = -1;
goto FREE_VECTOR_OUT;
}
tempPriSyscapStr = priSyscapStr;
ptrPrivateSyscap++;
continue;
}
*tempPriSyscapStr++ = *ptrPrivateSyscap++;
}
if (!cJSON_AddItemToObject(sysCapObjectPtr, "private", capVectorPtr)) {
PRINT_ERR("cJSON_AddItemToObject failed\n");
ret = -1;
goto FREE_VECTOR_OUT;
}
SKIP_GET_PRIVATE:
capVectorPtr = NULL;
// create json root
cJSON *jsonRootObj = cJSON_CreateObject();
if (jsonRootObj == NULL) {
@@ -487,30 +526,27 @@ SKIP_GET_PRIVATE:
ret = -1;
goto FREE_ROOT_OUT;
}
if (!cJSON_AddItemToObject(jsonRootObj, "syscap", sysCapObjectPtr)) {
if (!cJSON_AddItemToObject(jsonRootObj, "syscap", sysCapObj)) {
PRINT_ERR("cJSON_AddItemToObject failed\n");
ret = -1;
goto FREE_ROOT_OUT;
}
sysCapObjectPtr = NULL;
char *convertedBuffer = cJSON_Print(jsonRootObj);
sysCapObj = NULL; // avoid being released repeatedly.
char *strJson = cJSON_Print(jsonRootObj);
const char outputFileName[] = "PCID.json";
ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, convertedBuffer, strlen(convertedBuffer));
ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, strJson, strlen(strJson));
if (ret != 0) {
PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, outputFileName);
goto FREE_CONVERT_OUT;
}
FREE_CONVERT_OUT:
free(convertedBuffer);
free(strJson);
FREE_ROOT_OUT:
cJSON_Delete(jsonRootObj);
FREE_SYSCAP_OUT:
cJSON_Delete(sysCapObjectPtr);
FREE_VECTOR_OUT:
cJSON_Delete(capVectorPtr);
cJSON_Delete(sysCapObj);
FREE_CONTEXT_OUT:
FreeContextBuffer(contextBuffer);
return ret;
@@ -675,22 +711,21 @@ static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapSt
int32_t DecodeStringPCIDToJson(char *input, char *outDirPath)
{
int32_t ret = -1;
uint32_t osSyscapUintArray[OS_SYSCAP_NUM] = {0};
uint32_t osSyscap[OS_SYSCAP_NUM] = {0};
uint32_t pcidHeader[PCID_HEADER];
size_t fileContextLen;
char *fileContext = NULL;
char *ctx = NULL;
char *priSyscapStr = NULL;
if (GetFileContext(input, &fileContext, &fileContextLen) != 0) {
if (GetFileContext(input, &ctx, &fileContextLen) != 0) {
PRINT_ERR("GetFileContext failed, input file : %s\n", input);
goto PARSE_FAILED;
}
if (ParseStringSyscap(fileContext, osSyscapUintArray,
OS_SYSCAP_NUM, pcidHeader, PCID_HEADER) != 0) {
if (ParseStringSyscap(ctx, osSyscap, OS_SYSCAP_NUM, pcidHeader, PCID_HEADER) != 0) {
PRINT_ERR("Parse string syscap failed.\n");
goto PARSE_FAILED;
}
priSyscapStr = fileContext;
priSyscapStr = ctx;
// add to json object
cJSON *sysCapObj = cJSON_CreateObject();
@@ -703,7 +738,7 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath)
PRINT_ERR("Add header to json object failed.\n");
goto ADD_JSON_FAILED;
}
if (AddOsSyscapToJsonObj(osSyscapUintArray, OS_SYSCAP_NUM, sysCapObj) != 0) {
if (AddOsSyscapToJsonObj(osSyscap, OS_SYSCAP_NUM, sysCapObj) != 0) {
PRINT_ERR("Add os syscap json object failed.\n");
goto ADD_JSON_FAILED;
}
@@ -726,7 +761,7 @@ SAVE_FAILED:
ADD_JSON_FAILED:
cJSON_Delete(rootObj);
PARSE_FAILED:
free(fileContext);
free(ctx);
return ret;
}
+1 -2
View File
@@ -18,7 +18,6 @@
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <limits.h>
#include "securec.h"
#include "syscap_tool.h"
@@ -29,7 +28,7 @@
printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
printf(__VA_ARGS__); \
} while (0)
#define SYSCAP_VERSION "2.0.0"
#define SYSCAP_VERSION "2.0.1"
#define OUTPUT_VERSION_LEN 200
#define ENCODE 0
#define DECODE 1
+114 -94
View File
@@ -563,61 +563,37 @@ FREE_CONTEXT_OUT:
return ret;
}
int32_t SeparateSyscapFromString(const char *inputString, uint32_t *osArray, uint32_t osArraySize,
char **priSyscap, uint32_t *priSyscapLen)
char *CopyInputString(const char *inputString)
{
int32_t ret = 0;
uint32_t i;
uint32_t count = 0;
char *temp = NULL;
char *tok = NULL;
char *private = NULL;
if (osArraySize != PCID_OUT_BUFFER) {
return -1;
}
// copy to temp string input
if (inputString == NULL) {
PRINT_ERR("inputString is null.\n");
return -1;
if (inputString == NULL || *inputString == '\0') {
PRINT_ERR("inputString is null or empty.\n");
return NULL;
}
size_t inputLen = strlen(inputString);
if (inputLen > STRING_FORMAT_LEN_MAX) {
PRINT_ERR("input string too long(%zu).\n", inputLen);
return -1;
return NULL;
}
char *input = (char *)malloc(inputLen + 1);
if (input == NULL) {
PRINT_ERR("malloc failed.\n");
return -1;
return NULL;
}
ret = strcpy_s(input, inputLen + 1, inputString);
int32_t ret = strcpy_s(input, inputLen + 1, inputString);
if (ret != EOK) {
PRINT_ERR("strcpy_s failed.\n");
free(input);
return -1;
return NULL;
}
input[inputLen] = '\0';
return input;
}
// get os syscap data
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");
free(input);
return -1;
}
}
// count private syscap
if (*input == '\0') {
*priSyscap = 0;
*priSyscapLen = 0;
goto SKIP_PRI_SYSCAP;
}
for (i = 0; *(input + i) != '\0'; i++) {
int32_t GetPriSyscapData(char *input, char **priSyscap, uint32_t *priSyscapLen)
{
// count private syscaps
uint32_t count = 0;
for (uint32_t i = 0; *(input + i) != '\0'; i++) {
if (*(input + i) == ',') {
count++;
}
@@ -627,20 +603,19 @@ int32_t SeparateSyscapFromString(const char *inputString, uint32_t *osArray, uin
char *priSysCapOut = (char *)malloc(SINGLE_SYSCAP_LEN * count);
if (priSysCapOut == NULL) {
PRINT_ERR("sscanf_s failed.\n");
free(input);
return -1;
}
(void)memset_s(priSysCapOut, SINGLE_SYSCAP_LEN * count, 0, SINGLE_SYSCAP_LEN * count);
private = priSysCapOut;
char *private = priSysCapOut;
temp = strtok_r(input, ",", &tok);
char *tok = NULL;
char *temp = strtok_r(input, ",", &tok);
while (temp) {
ret = strncpy_s(private, SINGLE_SYSCAP_LEN,
int ret = strncpy_s(private, SINGLE_SYSCAP_LEN,
temp, SINGLE_SYSCAP_LEN - 1);
if (ret != EOK) {
PRINT_ERR("strncpy_s failed.\n");
free(input);
free(private);
free(priSysCapOut);
return -1;
}
temp = strtok_r(NULL, ",", &tok);
@@ -649,28 +624,112 @@ int32_t SeparateSyscapFromString(const char *inputString, uint32_t *osArray, uin
*priSyscap = priSysCapOut;
*priSyscapLen = count;
return 0;
}
int32_t SeparateSyscapFromString(const char *inputString, uint32_t *osArray, uint32_t osArraySize,
char **priSyscap, uint32_t *priSyscapLen)
{
if (osArraySize != PCID_OUT_BUFFER) {
return -1;
}
// copy origin string
char *input = CopyInputString(inputString);
if (input == NULL) {
return -1;
}
// get os syscap data
for (uint32_t i = 0; i < PCID_OUT_BUFFER; i++) {
if (sscanf_s(input, "%u,%s", &osArray[i], input, strlen(input)) == -1) {
PRINT_ERR("sscanf_s failed.\n");
free(input);
return -1;
}
}
// get private syscap data
if (GetPriSyscapData(input, priSyscap, priSyscapLen) != 0) {
free(input);
return -1;
}
SKIP_PRI_SYSCAP:
free(input);
return 0;
}
int32_t CompareOsSyscap(uint32_t pcidOsAarry[], uint32_t rpcidOsAarry[])
{
int32_t ossyscapFlag = 0;
const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
for (uint32_t 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))) {
continue;
}
// 2, header of pcid & rpcid
size_t pos = (size_t)((i - 2) * INT_BIT + k);
if (pos < allSyscapNum) {
printf("Missing: %s\n", g_arraySyscap[pos].str);
ossyscapFlag += 1;
}
}
}
return ossyscapFlag;
}
int32_t ComparePriSyscap(char *pcid, char *rpcid, uint32_t pcidLen, uint32_t rpcidLen)
{
uint32_t i, j;
bool priSysFound = false;
int32_t prisyscapFlag = 0;
for (i = 0; i < rpcidLen; i++) {
for (j = 0; j < pcidLen; j++) {
if (strcmp(rpcid + SINGLE_SYSCAP_LEN * i,
pcid + SINGLE_SYSCAP_LEN * j) == 0) {
priSysFound = true;
break;
}
}
if (priSysFound != true) {
printf("Missing: %s\n", rpcid + SINGLE_SYSCAP_LEN * i);
prisyscapFlag += 1;
}
priSysFound = false;
}
return prisyscapFlag;
}
int32_t CompoareVersion(uint32_t *pcidOsAarry, uint32_t *rpcidOsAarry)
{
int32_t versionFlag = 0;
uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion);
uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion);
if (pcidVersion < rpcidVersion) {
printf("ERROR: Pcid version(%u) less than rpcid version(%u).\n", pcidVersion, rpcidVersion);
versionFlag = 1;
}
return versionFlag;
}
int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t type)
{
int32_t ret;
int32_t versionFlag = 0;
int32_t ossyscapFlag = 0;
int32_t prisyscapFlag = 0;
char *pcidContent = NULL;
char *rpcidContent = NULL;
char *pcidPriSyscap = NULL;
char *rpcidPriSyscap = NULL;
uint32_t pcidContentLen, rpcidContentLen, pcidPriSyscapLen, rpcidPriSyscapLen;
uint32_t i, j, temp1, temp2;
bool priSysFound;
uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0};
uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0};
const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
if (type == TYPE_FILE) {
if (GetFileContext(pcidFile, &pcidContent, &pcidContentLen)) {
@@ -698,49 +757,10 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t typ
PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret);
return -1;
}
// compare version
uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion);
uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion);
if (pcidVersion < rpcidVersion) {
printf("ERROR: Pcid version(%u) less than rpcid version(%u).\n", pcidVersion, rpcidVersion);
versionFlag = 1;
}
// compare os syscap
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 & (1U << k))) {
continue;
}
// 2, header of pcid & rpcid
size_t pos = (size_t)((i - 2) * INT_BIT + k);
if (pos < allSyscapNum) {
printf("Missing: %s\n", g_arraySyscap[pos].str);
ossyscapFlag += 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) {
priSysFound = true;
break;
}
}
if (priSysFound != true) {
printf("Missing: %s\n", rpcidPriSyscap + SINGLE_SYSCAP_LEN * i);
prisyscapFlag += 1;
}
priSysFound = false;
}
int32_t versionFlag = CompoareVersion(pcidOsAarry, rpcidOsAarry);
int32_t ossyscapFlag = CompareOsSyscap(pcidOsAarry, rpcidOsAarry);
int32_t prisyscapFlag = ComparePriSyscap(pcidPriSyscap, rpcidPriSyscap, pcidPriSyscapLen, rpcidPriSyscapLen);
if (!versionFlag && !ossyscapFlag && !prisyscapFlag) {
printf("Succeed! The pcid meets the rpcid.\n");
} else {