diff --git a/include/create_pcid.h b/include/create_pcid.h index 748e7f1c31cb7457254688364fd0e6931f028b14..d8fc150004844f98dcc0a4d38144f6cf04109e7a 100644 --- a/include/create_pcid.h +++ b/include/create_pcid.h @@ -42,4 +42,5 @@ typedef struct pcidHeader { int32_t CreatePCID(char *inputFile, char *outDirPath); int32_t DecodePCID(char *inputFile, char *outDirPath); int32_t DecodeStringPCID(char *input, char *outDirPath, int type); +int32_t DecodePcidToString(char *inputFile, char *outDirPath); #endif \ No newline at end of file diff --git a/src/create_pcid.c b/src/create_pcid.c index 7792911962b3703a25a6a33b0b21c2fc06ddb0d4..0513949c785ee81442910ce70a7eb43133570219 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -32,9 +32,11 @@ #define SINGLE_FEAT_LENGTH (32 * 8) #define PER_SYSCAP_LEN_MAX 128 +#define PCID_OUT_BUFFER 32 #define PRIVATE_SYSCAP_SIZE 1000 #define UINT8_BIT 8 #define BYTES_OF_OS_SYSCAP 120 +#define U32_TO_STR_MAX_LEN 11 #define PRINT_ERR(...) \ do { \ @@ -718,4 +720,114 @@ PARSE_FAILED: free(fileContext); } return ret; +} + +int32_t DecodePcidToString(char *inputFile, char *outDirPath) +{ + int32_t ret = 0; + uint32_t bufferLen, privateSyscapLen, i, j, outputLen; + uint32_t *mainSyscap = NULL; + uint16_t priSyscapCount = 0; + char *contextBuffer = NULL; + char *privateSyscap = NULL; + char *priSyscapFull = NULL; + char *output = NULL; + PCIDMain *pcidMain = NULL; + + ret = GetFileContext(inputFile, &contextBuffer, &bufferLen); + if (ret != 0) { + PRINT_ERR("Get pcid file failed, pcid file path: %s\n", inputFile); + return -1; + } + + if (bufferLen > 1128) { // 1128, max size of pcid.sc + PRINT_ERR("Input pcid file too large, pcid file size: %u\n", bufferLen); + goto FREE_CONTEXT; + } + + pcidMain = (PCIDMain *)contextBuffer; + privateSyscap = (char *)(pcidMain + 1); + privateSyscapLen = strlen(privateSyscap); + + // process os syscap + mainSyscap = (uint32_t *)pcidMain; + + // process private syscap + for (i = 0; i < privateSyscapLen; i++) { + if (privateSyscap[i] == ',') { + priSyscapCount++; + } + } + if (priSyscapCount == 0) { + goto OUT_PUT; + } + priSyscapFull = (char *)malloc(priSyscapCount * SINGLE_FEAT_LENGTH); + if (priSyscapFull == NULL) { + PRINT_ERR("malloc failed\n"); + goto FREE_PRISYSCAP_FULL; + } + (void)memset_s(priSyscapFull, priSyscapCount * SINGLE_FEAT_LENGTH, + 0, priSyscapCount * SINGLE_FEAT_LENGTH); + char tempSyscap[SINGLE_FEAT_LENGTH] = {0}; + char *temp = tempSyscap; + for (i = 0, j = 0; i < privateSyscapLen; i++) { + if (*privateSyscap == ',') { + *temp = '\0'; + ret = sprintf_s(priSyscapFull + j * SINGLE_FEAT_LENGTH, SINGLE_FEAT_LENGTH, + "SystemCapability.%s", tempSyscap); + if (ret == -1) { + PRINT_ERR("sprintf_s failed\n"); + goto FREE_PRISYSCAP_FULL; + } + temp = tempSyscap; + privateSyscap++; + j++; + continue; + } + *temp++ = *privateSyscap++; + } + // output +OUT_PUT: + // 17, size of "SystemCapability." + outputLen = U32_TO_STR_MAX_LEN * PCID_OUT_BUFFER + 17 * priSyscapCount + privateSyscapLen + 1; + output = (char *)malloc(outputLen); + if (output == NULL) { + PRINT_ERR("malloc failed\n"); + goto FREE_PRISYSCAP_FULL; + } + (void)memset_s(output, outputLen, 0, outputLen); + ret = sprintf_s(output, outputLen, "%u", mainSyscap[0]); + if (ret == -1) { + PRINT_ERR("sprintf_s failed\n"); + goto FREE_OUTPUT; + } + 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"); + goto FREE_OUTPUT; + } + } + for (i = 0; i < priSyscapCount; i++) { + ret = sprintf_s(output, outputLen, "%s,%s", output, priSyscapFull + i * SINGLE_FEAT_LENGTH); + if (ret == -1) { + PRINT_ERR("sprintf_s failed\n"); + goto FREE_OUTPUT; + } + } + // save as file + const char outputFileName[] = "PCID.txt"; + ret = ConvertedContextSaveAsFile(outDirPath, outputFileName, output, strlen(output)); + if (ret != 0) { + PRINT_ERR("ConvertedContextSaveAsFile failed, outDirPath:%s, filename:%s\n", outDirPath, outputFileName); + goto FREE_OUTPUT; + } + +FREE_OUTPUT: + free(output); +FREE_PRISYSCAP_FULL: + free(priSyscapFull); +FREE_CONTEXT: + free(contextBuffer); + return ret; } \ No newline at end of file diff --git a/src/main.c b/src/main.c index 8c90f9284d69d626c5f95eda21499ab1dbbf2147..626c6a7282170348f9fdfef2ddf5527933fbe143 100644 --- a/src/main.c +++ b/src/main.c @@ -98,8 +98,10 @@ int main(int argc, char **argv) ret = RPCIDEncode(inputfile, outputpath); } else if (rpcid && !pcid && !encode && decode && !stringDecode && inputfile && !help) { ret = RPCIDDecode(inputfile, outputpath); - } else if (rpcid && !pcid && !encode && decode && stringDecode && inputfile && !help) { + } else if (rpcid && !pcid && encode && !decode && stringDecode && inputfile && !help) { ret = DecodeRpcidToString(inputfile, outputpath); + } else if (!rpcid && pcid && encode && !decode && stringDecode && inputfile && !help) { + ret = DecodePcidToString(inputfile, outputpath); } else if (!rpcid && !pcid && !encode && !decode && !stringDecode && pcidfile && rpcidfile && !inputfile && !help) { ret = ComparePcidWithRpcidString(pcidfile, rpcidfile); } else if (!rpcid && pcid && encode && !decode && inputfile && !stringDecode && !help) { @@ -114,8 +116,8 @@ int main(int argc, char **argv) printf("-R, --RPCID\t: encode or decode RPCID\n"); printf("-P, --PCID\t: encode or decode PCID\n"); printf("-C, --compare\t: compare pcid with rpcid string format.\n"); - printf("-e, --encode\t: decode to sc format.\n"); - printf("-d, --encode\t: decode to json format.\n\t-s, --string : decode (to) string format.\n"); + printf("-e, --encode\t: encode to sc format.\n\t-s, --string : encode to string format.\n"); + printf("-d, --decode\t: decode to json format.\n\t-s, --string : decode string format.\n"); printf("-i filepath, --input filepath\t: input file\n"); printf("-o outpath, --input outpath\t: output path\n"); exit(0); diff --git a/src/syscap_tool.c b/src/syscap_tool.c index a6be5d38a160a9b65861b4832585958ff5ab35b6..dcf437e1bf01007ce508a1446b9a7225c9afb5cc 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -690,5 +690,9 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile) } priSysFound = false; } + + if (ret == 0) { + printf("Succeed! The pcid contains rpcid.\n"); + } return ret; } \ No newline at end of file