diff --git a/include/create_pcid.h b/include/create_pcid.h index d8fc150004844f98dcc0a4d38144f6cf04109e7a..8f491deb29bad7d92fe3066f2ad6b12526537b5d 100644 --- a/include/create_pcid.h +++ b/include/create_pcid.h @@ -41,6 +41,6 @@ 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); +int32_t DecodeStringPCIDToJson(char *input, char *outDirPath, int type); +int32_t EncodePcidscToString(char *inputFile, char *outDirPath); #endif \ No newline at end of file diff --git a/include/syscap_tool.h b/include/syscap_tool.h index 1baa22d88632105ea453942e5730489e9fc98b3f..54ac10699e08a7fc1e443c500a52aba5face079b 100644 --- a/include/syscap_tool.h +++ b/include/syscap_tool.h @@ -33,7 +33,7 @@ int32_t RPCIDEncode(char *inputFile, char *outputPath); /* in: inputFile, out: outputPath/rpcid.json */ int32_t RPCIDDecode(char *inputFile, char *outputPath); /* in: inputFile, out: outputPath/rpcid.json */ -int32_t DecodeRpcidToString(char *inputFile, char *outDirPath); +int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath); /* in: pcidFile, rpcidFile */ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile); diff --git a/src/create_pcid.c b/src/create_pcid.c index 6217c42a63c3ab3618af4a54b08966cfa31e59cc..fadf9e103347c949d6cff3fc6a78771dc3bf34d3 100644 --- a/src/create_pcid.c +++ b/src/create_pcid.c @@ -365,7 +365,7 @@ int32_t DecodePCID(char *inputFile, char *outDirPath) (pcidMain->systemType == 0b010 ? "small" : (pcidMain->systemType == 0b100 ? "standard" : NULL)); if (systemType == NULL) { - PRINT_ERR("prase file failed, systemType is invaild, %d\n", pcidMain->systemType); + PRINT_ERR("prase file failed, systemType is invaild, %u\n", pcidMain->systemType); ret = -1; goto FREE_CONTEXT_OUT; } @@ -653,7 +653,7 @@ static int32_t AddPriSyscapToJsonObj(char *priSyscapString, uint32_t priSyscapSt return 0; } -int32_t DecodeStringPCID(char *input, char *outDirPath, int type) +int32_t DecodeStringPCIDToJson(char *input, char *outDirPath, int type) { int32_t ret = -1; uint32_t osSyscapUintArray[OS_SYSCAP_NUM] = {0}; @@ -722,7 +722,7 @@ PARSE_FAILED: return ret; } -int32_t DecodePcidToString(char *inputFile, char *outDirPath) +int32_t EncodePcidscToString(char *inputFile, char *outDirPath) { int32_t ret = 0; uint32_t bufferLen, privateSyscapLen, i, j, outputLen; diff --git a/src/main.c b/src/main.c index 09453b4066d85f49236a4e1b79a62ba1905777fc..af20a3405638ed2a3a2232e17d3168e0627c05a0 100644 --- a/src/main.c +++ b/src/main.c @@ -13,12 +13,14 @@ * limitations under the License. */ +#include #include #include #include #include #include #include +#include "securec.h" #include "syscap_tool.h" #include "create_pcid.h" @@ -27,17 +29,30 @@ printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \ printf(__VA_ARGS__); \ } while (0) +#define SYSCAP_VERSION "1.0.0" +#define OUTPUT_VERSION_LEN 200 +#define ENCODE 0 +#define DECODE 1 +#define STRING_DECODE 2 +#define RPCID 3 +#define PCID 4 +#define PCID_STRING 5 +#define RPCID_STRING 6 +#define VERSION 7 +#define INPUT_FILE 8 +#define OUTPUT_FILE 9 +#define HELP 10 + +void PrintHelp(void); +void PrintVersion(void); +void OutputVersion(char *arg, int opt); +void OutputHelp(void); int main(int argc, char **argv) { 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; + uint16_t bitMap = 0x0; char curpath[PATH_MAX] = {0}; char *inputfile = NULL; char *pcidfile = NULL; @@ -47,6 +62,7 @@ int main(int argc, char **argv) while (1) { static struct option long_options[] = { {"help", no_argument, 0, 'h' }, + {"version", no_argument, 0, 'v' }, {"RPCID", no_argument, 0, 'R' }, {"PCID", no_argument, 0, 'P' }, {"compare", required_argument, 0, 'C' }, @@ -58,78 +74,119 @@ int main(int argc, char **argv) {0, 0, 0, 0 } }; - int32_t flag = getopt_long(argc, argv, "hRPC:edsi:o:", long_options, &optIndex); + int32_t flag = getopt_long(argc, argv, "hvRPC:edsi:o:", long_options, &optIndex); if (flag == -1) { break; } switch (flag) { case 'e': - encode = 1; + bitMap |= 0x1 << ENCODE; break; case 'd': - decode = 1; + bitMap |= 0x1 << DECODE; break; case 's': - stringDecode = 1; + bitMap |= 0x1 << STRING_DECODE; break; case 'R': - rpcid = 1; + bitMap |= 0x1 << RPCID; break; case 'P': - pcid = 1; + bitMap |= 0x1 << PCID; break; case 'C': - pcidfile = optarg; + bitMap |= 0x1 << PCID_STRING; + bitMap |= 0x1 << RPCID_STRING; if (argc != 4 || optind < 0 || optind >= argc) { // 4, argc of ./syscap_tool -C f1 f2 PRINT_ERR("Input file too few or too many.\n"); return -1; } + pcidfile = optarg; rpcidfile = argv[optind]; break; case 'i': + bitMap |= 0x1 << INPUT_FILE; inputfile = optarg; break; case 'o': outputpath = optarg; break; + case 'v': + bitMap |= 0x1 << VERSION; + break; case 'h': default: - help = 1; + bitMap |= 0x1 << HELP;; } } - if (rpcid && !pcid && encode && !decode && inputfile && !stringDecode && !help) { - 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) { - 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) { - ret = CreatePCID(inputfile, outputpath); - } else if (!rpcid && pcid && !encode && decode && inputfile && !stringDecode && !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\t: how to use\n"); - 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: 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); + switch (bitMap) { + case 0x109: // 0x109, -Rei inputfile + ret = RPCIDEncode(inputfile, outputpath); break; + case 0x10A: // 0x10A, -Rdi inputfile + ret = RPCIDDecode(inputfile, outputpath); break; + case 0x10D: + ret = EncodeRpcidscToString(inputfile, outputpath); break; + case 0x115: + ret = EncodePcidscToString(inputfile, outputpath); break; + case 0x60: + ret = ComparePcidWithRpcidString(pcidfile, rpcidfile); break; + case 0x111: + ret = CreatePCID(inputfile, outputpath); break; + case 0x112: + ret = DecodePCID(inputfile, outputpath); break; + case 0x116: + ret = DecodeStringPCIDToJson(inputfile, outputpath, TYPE_FILE); break; + case 0x80: + (void)OutputVersion(argv[optind], optind); break; + default: + (void)OutputHelp(); } if (ret != 0) { PRINT_ERR("syscap_tool failed to complete. input: %s\n", inputfile); } - return ret; +} + +void PrintVersion(void) +{ + char output_version[OUTPUT_VERSION_LEN] = {0}; + int ret = sprintf_s(output_version, OUTPUT_VERSION_LEN, "syscap_tool v%s", SYSCAP_VERSION); + if (ret == -1) { + PRINT_ERR("sprintf_s failed.\n"); + exit(-1); + } + printf("%s\n", output_version); +} + +void PrintHelp(void) +{ + printf("syscap_tool -R/P -e/d -i filepath [-o outpath]\n"); + printf("-h, --help\t: how to use\n"); + 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: 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"); + printf("-v, --version\t: print syscap_tool version information.\n"); +} + +void OutputVersion(char *arg, int opt) +{ + if (arg != NULL && opt > 1) { + printf("syscap_tool: extra operand \"%s\"\n", arg); + printf("Try 'syscap_tool --help' for more information.\n"); + } else { + (void)PrintVersion(); + } +} + +void OutputHelp(void) +{ + (void)PrintHelp(); + printf("\n"); + (void)PrintVersion(); } \ No newline at end of file diff --git a/src/syscap_tool.c b/src/syscap_tool.c index 2879c0c6829e871dbf7f86a0b9262595a3287008..404bf2aeceaba7c0c35f224eb89f7477e1e987f8 100644 --- a/src/syscap_tool.c +++ b/src/syscap_tool.c @@ -44,7 +44,7 @@ typedef struct RequiredProductCompatibilityIDHead { uint16_t apiVersionType : 1; } RPCIDHead; -#define SINGLE_SYSCAP_LEN 128 +#define SINGLE_SYSCAP_LEN 256 #define SYSCAP_PREFIX_LEN 17 #define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN) #define UINT8_BIT 8 @@ -429,7 +429,7 @@ static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, uint16_t *index, uin return 0; } -int32_t DecodeRpcidToString(char *inputFile, char *outDirPath) +int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath) { int32_t ret = 0; int32_t sysCapArraySize; @@ -494,8 +494,7 @@ int32_t DecodeRpcidToString(char *inputFile, char *outDirPath) if (cJsonTemp != NULL) { osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint); } else { - ret = strncpy_s(priSyscap, SINGLE_SYSCAP_LEN, - cJsonItem->valuestring, SINGLE_SYSCAP_LEN - 1); + ret = strcpy_s(priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring); if (ret != EOK) { PRINT_ERR("strcpy_s failed.\n"); goto FREE_MALLOC_PRISYSCAP; @@ -514,7 +513,7 @@ int32_t DecodeRpcidToString(char *inputFile, char *outDirPath) } uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER - + (SINGLE_SYSCAP_LEN + 1) * (uint16_t)sysCapArraySize; + + SINGLE_SYSCAP_LEN * indexPri; char *outBuffer = (char *)malloc(outBufferLen); if (outBuffer == NULL) { PRINT_ERR("malloc(%u) failed.\n", outBufferLen); @@ -536,7 +535,8 @@ int32_t DecodeRpcidToString(char *inputFile, char *outDirPath) } for (i = 0; i < indexPri; i++) { - ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, priSyscap + i * SINGLE_SYSCAP_LEN); + ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, + priSyscapArray + i * SINGLE_SYSCAP_LEN); if (ret == -1) { PRINT_ERR("sprintf_s failed.\n"); goto FREE_OUTBUFFER; @@ -626,7 +626,7 @@ static int32_t SeparateSyscapFromString(char *input, uint32_t *osArray, uint32_t *priSyscapLen = count; SKIP_PRI_SYSCAP: - return ret; + return 0; } int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile) @@ -662,7 +662,7 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile) &rpcidPriSyscap, &rpcidPriSyscapLen); if (ret != 0) { PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret); - return false; + return -1; } // compare version uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion); @@ -688,16 +688,16 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile) } // compare pri syscap priSysFound = false; - for (i = 0; i < pcidPriSyscapLen; i++) { - for (j = 0; j < rpcidPriSyscapLen; j++) { - if (strcmp(pcidPriSyscap + SINGLE_SYSCAP_LEN * i, - rpcidPriSyscap + SINGLE_SYSCAP_LEN * j) == 0) { + 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", pcidPriSyscap + SINGLE_SYSCAP_LEN * i); + printf("Missing: %s\n", rpcidPriSyscap + SINGLE_SYSCAP_LEN * i); prisyscapFlag += 1; } priSysFound = false; @@ -705,8 +705,8 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile) if (!versionFlag && !ossyscapFlag && !prisyscapFlag) { printf("Succeed! The pcid meets the rpcid.\n"); - return 0; } else { - return -1; + printf("Fail! The pcid does not meet the rpcid\n"); } + return 0; } \ No newline at end of file