mirror of
https://github.com/openharmony/developtools_syscap_codec.git
synced 2026-08-27 02:11:19 -04:00
@@ -57,7 +57,7 @@ typedef struct ProductCompatibilityID {
|
||||
uint8_t osSyscap[OS_SYSCAP_BYTES];
|
||||
} PCIDMain;
|
||||
|
||||
static const char *g_pcidPath = "/system/etc/PCID.sc";
|
||||
static const char *PCID_PATH = "/system/etc/PCID.sc";
|
||||
|
||||
struct FreeAfterDecodeRpcidInfo {
|
||||
char *priSyscap;
|
||||
@@ -89,7 +89,7 @@ bool EncodeOsSyscap(char *output, int len)
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = GetFileContext(g_pcidPath, &contextBuffer, &bufferLen);
|
||||
ret = GetFileContext(PCID_PATH, &contextBuffer, &bufferLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("GetFileContext failed, input file : /system/etc/PCID.sc\n");
|
||||
return false;
|
||||
@@ -113,12 +113,16 @@ bool EncodePrivateSyscap(char **output, int *outputLen)
|
||||
char *outputStr = NULL;
|
||||
uint32_t bufferLen;
|
||||
|
||||
ret = GetFileContext(g_pcidPath, &contextBuffer, &bufferLen);
|
||||
ret = GetFileContext(PCID_PATH, &contextBuffer, &bufferLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("GetFileContext failed, input file : /system/etc/PCID.sc\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (bufferLen < (PCID_MAIN_BYTES + 1) || bufferLen > INT32_MAX) {
|
||||
PRINT_ERR("Parameter bufferLen out of range.");
|
||||
return false;
|
||||
}
|
||||
uint32_t priLen = bufferLen - PCID_MAIN_BYTES - 1;
|
||||
if ((int)priLen <= 0) {
|
||||
*outputLen = 0;
|
||||
@@ -177,18 +181,19 @@ bool DecodeOsSyscap(const char input[PCID_MAIN_BYTES], char (**output)[SINGLE_SY
|
||||
|
||||
for (i = 0; i < countOfSyscap; i++) {
|
||||
for (j = 0; j < sizeof(g_arraySyscap) / sizeof(SyscapWithNum); j++) {
|
||||
if (g_arraySyscap[j].num == indexOfSyscap[i]) {
|
||||
nRet = strcpy_s(*strSyscap, SINGLE_SYSCAP_LEN, g_arraySyscap[j].str);
|
||||
if (nRet != EOK) {
|
||||
printf("strcpy_s failed. error = %d\n", nRet);
|
||||
*outputCnt = 0;
|
||||
free(strSyscap);
|
||||
strSyscap = NULL;
|
||||
return false;
|
||||
}
|
||||
strSyscap++;
|
||||
break;
|
||||
if (g_arraySyscap[j].num != indexOfSyscap[i]) {
|
||||
continue;
|
||||
}
|
||||
nRet = strcpy_s(*strSyscap, SINGLE_SYSCAP_LEN, g_arraySyscap[j].str);
|
||||
if (nRet != EOK) {
|
||||
printf("strcpy_s failed. error = %d\n", nRet);
|
||||
*outputCnt = 0;
|
||||
free(strSyscap);
|
||||
strSyscap = NULL;
|
||||
return false;
|
||||
}
|
||||
strSyscap++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,6 +246,7 @@ bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *o
|
||||
*bufferPos = '\0';
|
||||
if (sprintf_s(*outputArray, SINGLE_SYSCAP_LEN, "SystemCapability.%s", buffer) == -1) {
|
||||
free(outputArray);
|
||||
free(output);
|
||||
return false;
|
||||
}
|
||||
bufferPos = buffer;
|
||||
@@ -316,8 +322,6 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson
|
||||
ret = -1;
|
||||
goto FREE_SYSCAP_OUT;
|
||||
}
|
||||
|
||||
return 0;
|
||||
FREE_SYSCAP_OUT:
|
||||
cJSON_Delete(sysCapJson);
|
||||
return ret;
|
||||
@@ -326,24 +330,25 @@ FREE_SYSCAP_OUT:
|
||||
static int32_t TransStringFormatAndSaveSyscap(struct FreeAfterDecodeRpcidInfo freeAfterDecodeRpcidInfo,
|
||||
cJSON *sysCapArray, const char *inputFile)
|
||||
{
|
||||
int32_t ret = -1;
|
||||
// trans to string format
|
||||
sysCapArray = cJSON_GetObjectItem(freeAfterDecodeRpcidInfo.rpcidRoot, "syscap");
|
||||
if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) {
|
||||
PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile);
|
||||
return -1;
|
||||
return ret;
|
||||
}
|
||||
freeAfterDecodeRpcidInfo.sysCapArraySize = cJSON_GetArraySize(sysCapArray);
|
||||
if (freeAfterDecodeRpcidInfo.sysCapArraySize < 0) {
|
||||
PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile);
|
||||
return -1;
|
||||
return ret;
|
||||
}
|
||||
// malloc for save os syscap index
|
||||
freeAfterDecodeRpcidInfo.osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t)
|
||||
* freeAfterDecodeRpcidInfo.sysCapArraySize);
|
||||
if (freeAfterDecodeRpcidInfo.osSysCapIndex == NULL) {
|
||||
PRINT_ERR("malloc failed.\n");
|
||||
return -1;
|
||||
}
|
||||
free(freeAfterDecodeRpcidInfo.osSysCapIndex);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
+34
-7
@@ -254,6 +254,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath)
|
||||
cJSON *jsonSyscapObj = cJSON_GetObjectItem(jsonRootObj, "syscap");
|
||||
if (jsonSyscapObj == NULL || !cJSON_IsObject(jsonSyscapObj)) {
|
||||
PRINT_ERR("get \"syscap\" object failed\n");
|
||||
cJSON_Delete(jsonRootObj);
|
||||
return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, -1);
|
||||
}
|
||||
|
||||
@@ -261,12 +262,14 @@ int32_t CreatePCID(char *inputFile, char *outDirPath)
|
||||
cJSON *jsonPriSyscapObj = cJSON_GetObjectItem(jsonSyscapObj, "private");
|
||||
ret = GetOsAndPriSyscapSize(jsonOsSyscapObj, jsonPriSyscapObj, &osCapSize, &privateCapSize);
|
||||
if (ret != 0) {
|
||||
cJSON_Delete(jsonRootObj);
|
||||
return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, ret);
|
||||
}
|
||||
|
||||
uint16_t allPriSyscapStrLen = 0;
|
||||
ret = GetPriSyscapLen(privateCapSize, jsonPriSyscapObj, &allPriSyscapStrLen);
|
||||
if (ret != 0) {
|
||||
cJSON_Delete(jsonRootObj);
|
||||
return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, ret);
|
||||
}
|
||||
|
||||
@@ -274,6 +277,7 @@ int32_t CreatePCID(char *inputFile, char *outDirPath)
|
||||
PCIDMain *pcidBuffer = (PCIDMain *)malloc(pcidLength);
|
||||
if (pcidBuffer == NULL) {
|
||||
PRINT_ERR("malloc for pcid buffer failed\n");
|
||||
cJSON_Delete(jsonRootObj);
|
||||
return FreeAfterCreatePCID(NULL, allOsSyscapObj, contextBuffer, 0, -1);
|
||||
}
|
||||
(void)memset_s(pcidBuffer, pcidLength, 0, pcidLength);
|
||||
@@ -282,10 +286,12 @@ int32_t CreatePCID(char *inputFile, char *outDirPath)
|
||||
ret += SetPriSyscap(pcidBuffer, jsonPriSyscapObj, privateCapSize, allPriSyscapStrLen);
|
||||
ret += SetPCIDHeader(pcidBuffer, jsonRootObj);
|
||||
if (ret != 0) {
|
||||
cJSON_Delete(jsonRootObj);
|
||||
return FreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, contextBuffer, FREE_CREATE_PCID_BUFFER_OUT, ret);
|
||||
}
|
||||
|
||||
ret = CheckConvertedContextSaveAsFile(outDirPath, pcidBuffer, pcidLength, ret);
|
||||
cJSON_Delete(jsonRootObj);
|
||||
return FreeAfterCreatePCID(pcidBuffer, allOsSyscapObj, contextBuffer, FREE_CREATE_PCID_BUFFER_OUT, ret);
|
||||
}
|
||||
|
||||
@@ -305,6 +311,7 @@ int32_t GetOsSyscap(PCIDMain *pcidMain, cJSON *sysCapObject)
|
||||
errno_t nRet = memcpy_s(osSyscap, OS_SYSCAP_BYTES, (uint8_t *)pcidMain + 8, OS_SYSCAP_BYTES);
|
||||
if (nRet != EOK) {
|
||||
PRINT_ERR("memcpy_s failed.");
|
||||
cJSON_Delete(capVectorPtr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@@ -343,11 +350,17 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (contextBufLen < 0 || contextBufLen > UINT32_MAX) {
|
||||
PRINT_ERR("the data privateSyscapLen is out of scope.");
|
||||
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) {
|
||||
cJSON_Delete(capVectorPtr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -361,10 +374,12 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL
|
||||
int32_t ret = sprintf_s(fullCapStr, SINGLE_SYSCAP_LEN, "SystemCapability.%s", priSyscapStr);
|
||||
if (ret == -1) {
|
||||
printf("sprintf_s failed\n");
|
||||
cJSON_Delete(capVectorPtr);
|
||||
return -1;
|
||||
}
|
||||
if (!cJSON_AddItemToArray(capVectorPtr, cJSON_CreateString(fullCapStr))) {
|
||||
printf("cJSON_AddItemToArray or cJSON_CreateString failed\n");
|
||||
cJSON_Delete(capVectorPtr);
|
||||
return -1;
|
||||
}
|
||||
tempPriSyscapStr = priSyscapStr;
|
||||
@@ -375,9 +390,9 @@ int32_t GetPriSyscap(PCIDMain *pcidMain, cJSON *sysCapObject, size_t contextBufL
|
||||
}
|
||||
if (!cJSON_AddItemToObject(sysCapObject, "private", capVectorPtr)) {
|
||||
PRINT_ERR("cJSON_AddItemToObject failed\n");
|
||||
cJSON_Delete(capVectorPtr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -507,6 +522,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath)
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, outputFileName);
|
||||
}
|
||||
free(strJson);
|
||||
return FreeAfterDecodePCID(freePcidJsonInfo, FREE_DECODE_PCID_CONVERT_OUT, ret);
|
||||
}
|
||||
|
||||
@@ -643,9 +659,10 @@ static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapSt
|
||||
if (priSyscapStringLen == 0) {
|
||||
if (!cJSON_AddItemToObject(sysCapObj, "private", sysCapArray)) {
|
||||
PRINT_ERR("Add private syscap array to json failed.\n");
|
||||
free(sysCapArray);
|
||||
cJSON_Delete(sysCapArray);
|
||||
return -1;
|
||||
}
|
||||
cJSON_Delete(sysCapArray);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -653,16 +670,17 @@ static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapSt
|
||||
while (token != NULL) {
|
||||
if (!cJSON_AddItemToArray(sysCapArray, cJSON_CreateString(token))) {
|
||||
PRINT_ERR("Add private syscap string to json failed.\n");
|
||||
free(sysCapArray);
|
||||
cJSON_Delete(sysCapArray);
|
||||
return -1;
|
||||
}
|
||||
token = strtok(NULL, ",");
|
||||
}
|
||||
if (!cJSON_AddItemToObject(sysCapObj, "private", sysCapArray)) {
|
||||
PRINT_ERR("Add private syscap array to json failed.\n");
|
||||
free(sysCapArray);
|
||||
cJSON_Delete(sysCapArray);
|
||||
return -1;
|
||||
}
|
||||
cJSON_Delete(sysCapArray);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -690,18 +708,22 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath)
|
||||
cJSON *rootObj = cJSON_CreateObject();
|
||||
if (!cJSON_AddItemToObject(rootObj, "syscap", sysCapObj)) {
|
||||
PRINT_ERR("Add syscap to json failed.\n");
|
||||
cJSON_Delete(sysCapObj);
|
||||
goto ADD_JSON_FAILED;
|
||||
}
|
||||
if (AddHeaderToJsonObj(pcidHeader, PCID_HEADER, rootObj) != 0) {
|
||||
PRINT_ERR("Add header to json object failed.\n");
|
||||
cJSON_Delete(sysCapObj);
|
||||
goto ADD_JSON_FAILED;
|
||||
}
|
||||
if (AddOsSyscapToJsonObj(osSyscap, OS_SYSCAP_NUM, sysCapObj) != 0) {
|
||||
PRINT_ERR("Add os syscap json object failed.\n");
|
||||
cJSON_Delete(sysCapObj);
|
||||
goto ADD_JSON_FAILED;
|
||||
}
|
||||
if (AddPriSyscapToJsonObj(priSyscapStr, (uint32_t)strlen(priSyscapStr), sysCapObj) != 0) {
|
||||
PRINT_ERR("Add private syscap json object failed.\n");
|
||||
cJSON_Delete(sysCapObj);
|
||||
goto ADD_JSON_FAILED;
|
||||
}
|
||||
// save as json file
|
||||
@@ -714,11 +736,11 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath)
|
||||
}
|
||||
ret = 0;
|
||||
|
||||
SAVE_FAILED:
|
||||
SAVE_FAILED:
|
||||
free(jsonBuffer);
|
||||
ADD_JSON_FAILED:
|
||||
ADD_JSON_FAILED:
|
||||
cJSON_Delete(rootObj);
|
||||
PARSE_FAILED:
|
||||
PARSE_FAILED:
|
||||
free(ctx);
|
||||
return ret;
|
||||
}
|
||||
@@ -752,18 +774,21 @@ static int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapL
|
||||
output = (char *)malloc(outputLen);
|
||||
if (output == NULL) {
|
||||
PRINT_ERR("malloc failed\n");
|
||||
free(output);
|
||||
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");
|
||||
free(output);
|
||||
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");
|
||||
free(output);
|
||||
return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret);
|
||||
}
|
||||
}
|
||||
@@ -771,6 +796,7 @@ static int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapL
|
||||
ret = sprintf_s(output, outputLen, "%s,%s", output, freePcidInfo.priSyscapFull + i * SINGLE_SYSCAP_LEN);
|
||||
if (ret == -1) {
|
||||
PRINT_ERR("sprintf_s failed\n");
|
||||
free(output);
|
||||
return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret);
|
||||
}
|
||||
}
|
||||
@@ -781,6 +807,7 @@ static int32_t GetEncodePCIDOut(uint16_t priSyscapCount, uint32_t privateSyscapL
|
||||
PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n",
|
||||
freePcidInfo.outDirPathFinal, outputFileName);
|
||||
}
|
||||
free(output);
|
||||
return FreeAfterEncodePCID(freePcidInfo, FREE_ENCODE_PCID_OUT, ret);
|
||||
}
|
||||
|
||||
|
||||
+8
-2
@@ -37,6 +37,7 @@
|
||||
#define INPUT_FILE 8
|
||||
#define OUTPUT_FILE 9
|
||||
#define HELP 10
|
||||
#define INPUT_FILE_NUM 4
|
||||
|
||||
static void PrintHelp(void);
|
||||
static void PrintVersion(void);
|
||||
@@ -85,7 +86,7 @@ int main(int argc, char **argv)
|
||||
break;
|
||||
}
|
||||
if (flag == 'C') {
|
||||
if (argc != 4 || optind < 0 || optind >= argc) { // 4, argc of ./syscap_tool -C f1 f2
|
||||
if (argc != INPUT_FILE_NUM || optind < 0 || optind >= argc) { // 4, argc of ./syscap_tool -C f1 f2
|
||||
PRINT_ERR("Input file too few or too many.\n");
|
||||
return -1;
|
||||
}
|
||||
@@ -126,7 +127,12 @@ int32_t OperateByBitMap(char *const *argv, uint16_t bitMap, char *outputpath)
|
||||
case 0x10E: // 0x10E, -Rdsi inputfile
|
||||
printf("-Rdsi is not support currently.\n"); break;
|
||||
case 0x80: // 0x80, -v
|
||||
(void)OutputVersion(argv[optind], optind); break;
|
||||
if (optind < 0 || optind >= INPUT_FILE_NUM) {
|
||||
PRINT_ERR("Input file too few or too many.\n");
|
||||
return -1;
|
||||
} else {
|
||||
(void)OutputVersion(argv[optind], optind); break;
|
||||
}
|
||||
default:
|
||||
(void)OutputHelp();
|
||||
}
|
||||
|
||||
@@ -266,6 +266,7 @@ int32_t RPCIDDecode(char *inputFile, char *outputPath)
|
||||
|
||||
FREE_RPCID_ROOT:
|
||||
cJSON_Delete(rpcidRoot);
|
||||
free(convertedBuffer);
|
||||
FREE_CONTEXT_OUT:
|
||||
FreeContextBuffer(contextBuffer);
|
||||
return ret;
|
||||
|
||||
Reference in New Issue
Block a user