add trans pcid.sc to pcid string function.

Signed-off-by: yudechen <chenyude@huawei.com>
Change-Id: I50a6f9f48060df615f5b74e85726f687a0a68183
This commit is contained in:
yudechen
2022-06-09 12:29:32 +08:00
parent c792eb9a73
commit eab7f4ba7e
4 changed files with 122 additions and 3 deletions
+1
View File
@@ -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
+112
View File
@@ -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;
}
+5 -3
View File
@@ -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);
+4
View File
@@ -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;
}