From b58b98e86a58fc8a1bd4c50ecdbd1392a9e27828 Mon Sep 17 00:00:00 2001 From: yudechen Date: Mon, 23 May 2022 20:18:35 +0800 Subject: [PATCH 1/2] fix syscap_tool output path failed. Signed-off-by: yudechen Change-Id: Ie9f1c0ebc77c2efaaf9d8cd09b5fe0e6e8a2dffa --- src/create_pcid.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/create_pcid.c b/src/create_pcid.c index ea640b4..a40c15f 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -108,7 +108,6 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename { int32_t ret; FILE *fp = NULL; - int32_t pathLen = strlen(outDirPath); char path[PATH_MAX + 1] = {0x00}; #ifdef _POSIX_ @@ -122,7 +121,7 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename return -1; } #endif - + int32_t pathLen = strlen(path); if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') { path[pathLen] = '/'; } -- Gitee From dc42c1e4223afdbcadb3dfc19c233ec83de349f4 Mon Sep 17 00:00:00 2001 From: yudechen Date: Thu, 26 May 2022 19:10:58 +0800 Subject: [PATCH 2/2] syscap_tool add decode string format pcid funcation. Signed-off-by: yudechen Change-Id: I8319613167ea80c05551f5981dbffd2cc1f2bcf9 --- include/create_pcid.h | 4 +- napi/napi_query_syscap.cpp | 15 +-- src/create_pcid.c | 230 +++++++++++++++++++++++++++++++++++-- src/main.c | 42 ++++--- 4 files changed, 262 insertions(+), 29 deletions(-) diff --git a/include/create_pcid.h b/include/create_pcid.h index 506083b..e3661dd 100644 --- a/include/create_pcid.h +++ b/include/create_pcid.h @@ -19,6 +19,8 @@ #include #define MAX_OS_SYSCAP_NUM 960 +#define TYPE_FILE 1 +#define TYPE_STRING 2 typedef struct ProductCompatibilityID { uint16_t apiVersion : 15; @@ -31,5 +33,5 @@ typedef struct ProductCompatibilityID { int32_t CreatePCID(char *inputFile, char *outDirPath); int32_t DecodePCID(char *inputFile, char *outDirPath); - +int32_t DecodeStringPCID(char *input, char *outDirPath, int type); #endif \ No newline at end of file diff --git a/napi/napi_query_syscap.cpp b/napi/napi_query_syscap.cpp index dec506b..94df7bb 100644 --- a/napi/napi_query_syscap.cpp +++ b/napi/napi_query_syscap.cpp @@ -25,6 +25,7 @@ namespace OHOS { EXTERN_C_START constexpr size_t OS_SYSCAP_U32_NUM = 30; +constexpr size_t PCID_MAIN_U32 = OS_SYSCAP_U32_NUM + 2; constexpr size_t U32_TO_STR_MAX_LEN = 11; constexpr size_t SYSCAP_STR_MAX_LEN = 128; constexpr size_t PCID_MAIN_LEN = 128; @@ -32,7 +33,7 @@ constexpr size_t KEY_BUFFER_SIZE = 32; #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) @@ -68,7 +69,7 @@ static char* getSystemCapability() char *priOutput = nullptr; char *temp = nullptr; char *allSyscapBUffer = nullptr; - char osCapArray[OS_SYSCAP_U32_NUM][U32_TO_STR_MAX_LEN] = {}; + char osCapArray[PCID_MAIN_U32][U32_TO_STR_MAX_LEN] = {}; char (*priCapArray)[SYSCAP_STR_MAX_LEN] = nullptr; retBool = EncodeOsSyscap(osOutput, PCID_MAIN_LEN); @@ -82,8 +83,8 @@ static char* getSystemCapability() goto FREE_PRIOUTPUT; } - osCapU32 = reinterpret_cast(osOutput + 8); // 8, header of pcid.sc - for (i = 0; i < OS_SYSCAP_U32_NUM; i++) { + osCapU32 = reinterpret_cast(osOutput); + for (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."); @@ -99,13 +100,13 @@ static char* getSystemCapability() // calculate all string length sumLen = 0; - for (i = 0; i < OS_SYSCAP_U32_NUM; i++) { + for (i = 0; i < PCID_MAIN_U32; i++) { sumLen += strlen(osCapArray[i]); } for (i = 0; i < priCapArrayCnt; i++) { sumLen += strlen(*(priCapArray + i)); } - sumLen += (OS_SYSCAP_U32_NUM + priCapArrayCnt + 1); // split with ',' + sumLen += (PCID_MAIN_U32 + priCapArrayCnt + 1); // split with ',' // splicing string allSyscapBUffer = (char *)malloc(sumLen); @@ -122,7 +123,7 @@ static char* getSystemCapability() } temp = *osCapArray; - for (i = 1; i < OS_SYSCAP_U32_NUM; i++) { + for (i = 1; i < PCID_MAIN_U32; i++) { retError = sprintf_s(allSyscapBUffer, sumLen, "%s,%s", temp, osCapArray[i]); if (retError == -1) { PRINT_ERR("splicing os syscap string failed."); diff --git a/src/create_pcid.c b/src/create_pcid.c index a40c15f..aceb232 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -33,12 +33,12 @@ #define SINGLE_FEAT_LENGTH (32 * 8) #define PER_SYSCAP_LEN_MAX 128 #define PRIVATE_SYSCAP_SIZE 1000 -#define BITS_OF_ONE_BYTE 8 +#define UINT8_BIT 8 #define BYTES_OF_OS_SYSCAP 120 #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) @@ -260,8 +260,8 @@ int32_t CreatePCID(char *inputFile, char *outDirPath) ret = -1; goto FREE_CONVERT_OUT; } - sectorOfBits = (osCapIndex->valueint) / BITS_OF_ONE_BYTE; - posOfBits = (osCapIndex->valueint) % BITS_OF_ONE_BYTE; + sectorOfBits = (osCapIndex->valueint) / UINT8_BIT; + posOfBits = (osCapIndex->valueint) % UINT8_BIT; if (sectorOfBits >= BYTES_OF_OS_SYSCAP) { PRINT_ERR("num of \"os syscap\" is out of 960\n"); ret = -1; @@ -340,7 +340,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) errno_t nRet = 0; char *contextBuffer = NULL; uint8_t osSyscap[BYTES_OF_OS_SYSCAP] = {0}; - uint16_t indexOfSyscap[BYTES_OF_OS_SYSCAP * BITS_OF_ONE_BYTE] = {0}; + uint16_t indexOfSyscap[BYTES_OF_OS_SYSCAP * UINT8_BIT] = {0}; uint32_t i, j, contextBufLen, countOfSyscap = 0; ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen); @@ -382,9 +382,9 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) goto FREE_VECTOR_OUT; } for (i = 0; i < BYTES_OF_OS_SYSCAP; i++) { - for (j = 0; j < BITS_OF_ONE_BYTE; j++) { + for (j = 0; j < UINT8_BIT; j++) { if (osSyscap[i] & (0x01 << j)) { - indexOfSyscap[countOfSyscap++] = i * BITS_OF_ONE_BYTE + j; + indexOfSyscap[countOfSyscap++] = i * UINT8_BIT + j; } } } @@ -509,4 +509,220 @@ FREE_VECTOR_OUT: FREE_CONTEXT_OUT: FreeContextBuffer(contextBuffer); return ret; +} + +#define U32_TO_STR_MAX_LEN 11 +#define OS_SYSCAP_NUM 30 +#define PCID_HEADER 2 +static int32_t ParseStringSyscap(char *input, uint32_t *osSyscap, uint32_t osSyscapNum, + uint32_t *header, uint32_t headerLen) +{ + uint32_t tempNum; + uint32_t i = 0; + size_t inputLen = strlen(input); + + if (osSyscapNum != OS_SYSCAP_NUM || headerLen != PCID_HEADER) { + PRINT_ERR("Input osSyscapNum(%u) or headerLen(%u) error.\n", osSyscapNum, headerLen); + return -1; + } + + if (sscanf_s(input, "%u,%u,%s", &header[0], &header[1], input, inputLen) != 3) { // 3, return val of "%u,%u,%s" + PRINT_ERR("Get pcid header failed.\n"); + return -1; + } + + while (sscanf_s(input, "%u,%s", &tempNum, input, inputLen) == 2) { // 2, return val of "%u,%s" + if (i >= OS_SYSCAP_NUM) { + PRINT_ERR("Get os syscap numbers(%u) greater than %u.\n", i + 1, osSyscapNum); + return -1; + } + osSyscap[i++] = tempNum; + } + + return 0; +} + +static int32_t AddHeaderToJsonObj(uint32_t *pcidHeader, uint32_t pcidHeaderLen, cJSON *rootObj) +{ + if (pcidHeaderLen != PCID_HEADER) { + PRINT_ERR("input pcidHeader(%u) error.\n", pcidHeaderLen); + return -1; + } + typedef struct pcidHeader { + uint16_t apiVersion : 15; + uint16_t apiVersionType : 1; + uint16_t systemType : 3; + uint16_t reserved : 13; + uint32_t manufacturerID; + } PCIDHeader; + + PCIDHeader *header = (PCIDHeader *)pcidHeader; + // trans system type to string + char *systemType = header->systemType == 0b001 ? "mini" : + (header->systemType == 0b010 ? "small" : + (header->systemType == 0b100 ? "standard" : NULL)); + if (systemType == NULL) { + PRINT_ERR("prase system type failed.\n"); + return -1; + } + + // add to json + if (!cJSON_AddNumberToObject(rootObj, "api_version", NtohsInter(header->apiVersion))) { + PRINT_ERR("add api_version(%u) to json object failed.\n", NtohsInter(header->apiVersion)); + return -1; + } + if (!cJSON_AddNumberToObject(rootObj, "manufacturer_id", NtohlInter(header->manufacturerID))) { + PRINT_ERR("add manufacturer_id(%u) to json object failed\n", NtohlInter(header->manufacturerID)); + return -1; + } + if (!cJSON_AddStringToObject(rootObj, "system_type", systemType)) { + PRINT_ERR("add system_type(%s) to json object failed\n", systemType); + return -1; + } + return 0; +} + +static int32_t AddOsSyscapToJsonObj(uint32_t *osSyscapArray, uint32_t osSyscapArrayLen, cJSON *sysCapObj) +{ + cJSON *sysCapArray = cJSON_CreateArray(); + if (sysCapArray == NULL) { + PRINT_ERR("Create cJSON array failed.\n"); + return -1; + } + + if (osSyscapArrayLen != OS_SYSCAP_NUM) { + PRINT_ERR("Input os syscap array len error.\n"); + free(sysCapArray); + return -1; + } + uint8_t *osSysCapArrayUint8 = (uint8_t *)osSyscapArray; + + uint32_t i, j; + uint32_t osSyscapCount = 0; + int32_t index[BYTES_OF_OS_SYSCAP * UINT8_BIT] = {0}; + for (i = 0; i < BYTES_OF_OS_SYSCAP; i++) { + for (j = 0; j < UINT8_BIT; j++) { + if (osSysCapArrayUint8[i] & (0x01 << j)) { + index[osSyscapCount++] = i * UINT8_BIT + j; + } + } + } + + for (i = 0; i < osSyscapCount; i++) { + for (j = 0; j < sizeof(arraySyscap) / sizeof(SyscapWithNum); j++) { + if (index[i] != arraySyscap[j].num) { + continue; + } + if (!cJSON_AddItemToArray(sysCapArray, cJSON_CreateString(arraySyscap[j].syscapStr))) { + PRINT_ERR("Add os syscap string to json failed.\n"); + free(sysCapArray); + return -1; + } + break; + } + } + + if (!cJSON_AddItemToObject(sysCapObj, "os", sysCapArray)) { + PRINT_ERR("Add os syscap item to json object failed.\n"); + free(sysCapArray); + return -1; + } + return 0; +} + +static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapStringLen, cJSON *sysCapObj) +{ + char *token = NULL; + + cJSON *sysCapArray = cJSON_CreateArray(); + if (sysCapArray == NULL) { + PRINT_ERR("Create cJSON array failed.\n"); + free(sysCapArray); + return -1; + } + + token = strtok(priSyscapString, ","); + while (token != NULL) { + if (!cJSON_AddItemToArray(sysCapArray, cJSON_CreateString(token))) { + PRINT_ERR("Add private syscap string to json failed.\n"); + free(sysCapArray); + return -1; + } + token = strtok(NULL, ","); + } + if (!cJSON_AddItemToObject(sysCapObj, "private", sysCapArray)) { + PRINT_ERR("Add private syscap array to json failed.\n"); + free(sysCapArray); + return -1; + } + return 0; +} + +int32_t DecodeStringPCID(char *input, char *outDirPath, int type) +{ + int32_t ret = -1; + uint32_t osSyscapUintArray[OS_SYSCAP_NUM] = {0}; + uint32_t pcidHeader[PCID_HEADER]; + uint32_t fileContextLen; + char *fileContext = NULL; + char *priSyscapStr = NULL; + + // judge input type + if (type == TYPE_FILE) { + if (GetFileContext(input, &fileContext, &fileContextLen)) { + PRINT_ERR("GetFileContext failed, input file : %s\n", input); + goto PARSE_FAILED; + } + if (ParseStringSyscap(fileContext, osSyscapUintArray, OS_SYSCAP_NUM, pcidHeader, PCID_HEADER)) { + PRINT_ERR("Parse string syscap failed.\n"); + goto PARSE_FAILED; + } + priSyscapStr = fileContext; + } else if (type == TYPE_STRING) { + if (ParseStringSyscap(input, osSyscapUintArray, OS_SYSCAP_NUM, pcidHeader, PCID_HEADER)) { + PRINT_ERR("Parse string syscap failed.\n"); + goto PARSE_FAILED; + } + priSyscapStr = input; + } else { + PRINT_ERR("Input type failed.\n"); + goto PARSE_FAILED; + } + // add to json object + cJSON *sysCapObj = cJSON_CreateObject(); + cJSON *rootObj = cJSON_CreateObject(); + if (!cJSON_AddItemToObject(rootObj, "syscap", sysCapObj)) { + PRINT_ERR("Add syscap to json failed.\n"); + goto ADD_JSON_FAILED; + } + if (AddHeaderToJsonObj(pcidHeader, PCID_HEADER, rootObj)) { + PRINT_ERR("Add header to json object failed.\n"); + goto ADD_JSON_FAILED; + } + if (AddOsSyscapToJsonObj(osSyscapUintArray, OS_SYSCAP_NUM, sysCapObj)) { + PRINT_ERR("Add os syscap json object failed.\n"); + goto ADD_JSON_FAILED; + } + if (AddPriSyscapToJsonObj(priSyscapStr, strlen(priSyscapStr), sysCapObj)) { + PRINT_ERR("Add private syscap json object failed.\n"); + goto ADD_JSON_FAILED; + } + // save as json file + char *jsonBuffer = cJSON_Print(rootObj); + const char outputFileName[] = "syscap.json"; + if (ConvertedContextSaveAsFile(outDirPath, outputFileName, jsonBuffer, strlen(jsonBuffer))) { + PRINT_ERR("Save as json file failed.\n"); + goto SAVE_FAILED; + } + ret = 0; + +SAVE_FAILED: + free(jsonBuffer); +ADD_JSON_FAILED: + cJSON_Delete(rootObj); +PARSE_FAILED: + if (type == TYPE_FILE) { + free(fileContext); + } + return ret; } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 8b4160e..c274bd8 100644 --- a/src/main.c +++ b/src/main.c @@ -22,31 +22,40 @@ #include "syscap_tool.h" #include "create_pcid.h" +#define PRINT_ERR(...) \ + do { \ + printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \ + printf(__VA_ARGS__); \ + } while (0) + int main(int argc, char **argv) { - int32_t ret, optIndex; - char curpath[PATH_MAX] = {0}; + int32_t optIndex; + int32_t ret = 0; int32_t rpcid = 0; int32_t pcid = 0; int32_t encode = 0; int32_t decode = 0; + int32_t stringDecode = 0; int32_t help = 0; + char curpath[PATH_MAX] = {0}; char *inputfile = NULL; char *outputpath = getcwd(curpath, sizeof(curpath)); while (1) { static struct option long_options[] = { - {"help", no_argument, 0, 'h' }, - {"RPCID", no_argument, 0, 'R' }, - {"PCID", no_argument, 0, 'P' }, - {"encode", no_argument, 0, 'e' }, - {"decode", no_argument, 0, 'd' }, - {"input", required_argument, 0, 'i' }, - {"output", required_argument, 0, 'o' }, - {0, 0, 0, 0 } + {"help", no_argument, 0, 'h' }, + {"RPCID", no_argument, 0, 'R' }, + {"PCID", no_argument, 0, 'P' }, + {"encode", no_argument, 0, 'e' }, + {"decode", no_argument, 0, 'd' }, + {"string-decode", no_argument, 0, 's' }, + {"input", required_argument, 0, 'i' }, + {"output", required_argument, 0, 'o' }, + {0, 0, 0, 0 } }; - int32_t flag = getopt_long(argc, argv, "hRPedi:o:", long_options, &optIndex); + int32_t flag = getopt_long(argc, argv, "hRPedsi:o:", long_options, &optIndex); if (flag == -1) { break; } @@ -57,6 +66,9 @@ int main(int argc, char **argv) case 'd': decode = 1; break; + case 's': + stringDecode = 1; + break; case 'R': rpcid = 1; break; @@ -83,6 +95,8 @@ int main(int argc, char **argv) ret = CreatePCID(inputfile, outputpath); } else if (!rpcid && pcid && !encode && decode && inputfile && !help) { ret = DecodePCID(inputfile, outputpath); + } else if (!rpcid && !pcid && !encode && !decode && stringDecode && inputfile && !help) { + ret = DecodeStringPCID(inputfile, outputpath, TYPE_FILE); } else { printf("syscap_tool -R/P -e/d -i filepath [-o outpath]\n"); printf("-h, --help : how to use\n"); @@ -90,15 +104,15 @@ int main(int argc, char **argv) printf("-P, --PCID : encode or decode PCID\n"); printf("-e, --encode : to encode\n"); printf("-d, --encode : to decode\n"); + printf("-s, --string-decode : decode string pcid to json\n"); printf("-i filepath, --input filepath : input file\n"); printf("-o outpath, --input outpath : output path\n"); exit(0); } if (ret != 0) { - printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); - printf("input file(%s) prase failed\n", inputfile); + PRINT_ERR("syscap_tool failed to complete. input: %s\n", inputfile); } - return 0; + return ret; } \ No newline at end of file -- Gitee