• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <getopt.h>
17 #include <stdint.h>
18 #include <unistd.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <getopt.h>
22 #include <limits.h>
23 #include "securec.h"
24 #include "syscap_tool.h"
25 #include "create_pcid.h"
26 
27 #define PRINT_ERR(...) \
28     do { \
29         printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
30         printf(__VA_ARGS__); \
31     } while (0)
32 #define SYSCAP_VERSION "2.0.0"
33 #define OUTPUT_VERSION_LEN 200
34 #define ENCODE 0
35 #define DECODE 1
36 #define STRING_DECODE 2
37 #define RPCID 3
38 #define PCID 4
39 #define PCID_STRING 5
40 #define RPCID_STRING 6
41 #define VERSION 7
42 #define INPUT_FILE 8
43 #define OUTPUT_FILE 9
44 #define HELP 10
45 
46 static void PrintHelp(void);
47 static void PrintVersion(void);
48 static void OutputVersion(const char *arg, int opt);
49 static void OutputHelp(void);
50 
main(int argc,char ** argv)51 int main(int argc, char **argv)
52 {
53     int32_t optIndex;
54     int32_t ret = 0;
55     uint16_t bitMap = 0x0;
56     char curpath[PATH_MAX] = {0};
57     char *inputfile = NULL;
58     char *pcidfile = NULL;
59     char *rpcidfile = NULL;
60     char *outputpath = getcwd(curpath, sizeof(curpath));
61 
62     while (1) {
63         static struct option long_options[] = {
64             {"help",           no_argument,       0,  'h' },
65             {"version",        no_argument,       0,  'v' },
66             {"RPCID",          no_argument,       0,  'R' },
67             {"PCID",           no_argument,       0,  'P' },
68             {"compare",        required_argument, 0,  'C' },
69             {"encode",         no_argument,       0,  'e' },
70             {"decode",         no_argument,       0,  'd' },
71             {"string",         no_argument,       0,  's' },
72             {"input",          required_argument, 0,  'i' },
73             {"output",         required_argument, 0,  'o' },
74             {0,                0,                 0,  0   }
75         };
76 
77         int32_t flag = getopt_long(argc, argv, "hvRPC:edsi:o:", long_options, &optIndex);
78         if (flag == -1) {
79             break;
80         }
81         switch (flag) {
82             case 'e':
83                 bitMap |= 0x1 << ENCODE;
84                 break;
85             case 'd':
86                 bitMap |= 0x1 << DECODE;
87                 break;
88             case 's':
89                 bitMap |= 0x1 << STRING_DECODE;
90                 break;
91             case 'R':
92                 bitMap |= 0x1 << RPCID;
93                 break;
94             case 'P':
95                 bitMap |= 0x1 << PCID;
96                 break;
97             case 'C':
98                 bitMap |= 0x1 << PCID_STRING;
99                 bitMap |= 0x1 << RPCID_STRING;
100                 if (argc != 4 || optind < 0 || optind >= argc) {  // 4, argc of ./syscap_tool -C f1 f2
101                     PRINT_ERR("Input file too few or too many.\n");
102                     return -1;
103                 }
104                 pcidfile = optarg;
105                 rpcidfile = argv[optind];
106                 break;
107             case 'i':
108                 bitMap |= 0x1 << INPUT_FILE;
109                 inputfile = optarg;
110                 break;
111             case 'o':
112                 outputpath = optarg;
113                 break;
114             case 'v':
115                 bitMap |= 0x1 << VERSION;
116                 break;
117             case 'h':
118             default:
119                 bitMap |= 0x1 << HELP;;
120         }
121     }
122 
123     switch (bitMap) {
124         case 0x109: // 0x109, -Rei inputfile
125             ret = RPCIDEncode(inputfile, outputpath); break;
126         case 0x10A: // 0x10A, -Rdi inputfile
127             ret = RPCIDDecode(inputfile, outputpath); break;
128         case 0x10D: // 0x10D, -Resi inputfile
129             ret = EncodeRpcidscToString(inputfile, outputpath); break;
130         case 0x115: // 0x115, -Pesi inputfile
131             ret = EncodePcidscToString(inputfile, outputpath); break;
132         case 0x60:  // 0x60,  -C PCID.txt RPCID.txt
133             ret = ComparePcidWithRpcidString(pcidfile, rpcidfile, TYPE_FILE); break;
134         case 0x64:  // 0x64,  -sC "pcidstring" "rpcidstring"
135             ret = ComparePcidWithRpcidString(pcidfile, rpcidfile, TYPE_STRING); break;
136         case 0x111: // 0x111, -Pei inputfile
137             ret = CreatePCID(inputfile, outputpath); break;
138         case 0x112: // 0x112, -Pdi inputfile
139             ret = DecodePCID(inputfile, outputpath); break;
140         case 0x116: // 0x116, -Pdsi inputfile
141             ret = DecodeStringPCIDToJson(inputfile, outputpath); break;
142         case 0x10E: // 0x10E, -Rdsi inputfile
143             printf("-Rdsi is not support currently.\n"); break;
144         case 0x80:  // 0x80,  -v
145             (void)OutputVersion(argv[optind], optind);  break;
146         default:
147             (void)OutputHelp();
148     }
149 
150     if (ret != 0) {
151         PRINT_ERR("syscap_tool failed to complete. input: %s\n", inputfile);
152     }
153     return ret;
154 }
155 
PrintVersion(void)156 void PrintVersion(void)
157 {
158     char outputVersion[OUTPUT_VERSION_LEN] = {0};
159     int ret = sprintf_s(outputVersion, OUTPUT_VERSION_LEN, "syscap_tool v%s", SYSCAP_VERSION);
160     if (ret == -1) {
161         PRINT_ERR("sprintf_s failed.\n");
162         exit(-1);
163     }
164     printf("%s\n", outputVersion);
165 }
166 
PrintHelp(void)167 void PrintHelp(void)
168 {
169     printf("syscap_tool -R/P -e/d -i filepath [-o outpath]\n");
170     printf("-h, --help\t: how to use\n");
171     printf("-R, --RPCID\t: encode or decode RPCID\n");
172     printf("-P, --PCID\t: encode or decode PCID\n");
173     printf("-C, --compare\t: compare pcid with rpcid string format.\n\t"
174            "-s, --string : input string.\n");
175     printf("-e, --encode\t: encode to sc format.\n\t-s, --string : encode to string format.\n");
176     printf("-d, --decode\t: decode to json format.\n\t-s, --string : decode string format.\n");
177     printf("-i filepath, --input filepath\t: input file\n");
178     printf("-o outpath, --input outpath\t: output path\n");
179     printf("-v, --version\t: print syscap_tool version information.\n");
180 }
181 
OutputVersion(const char * arg,int opt)182 void OutputVersion(const char *arg, int opt)
183 {
184     if (arg != NULL && opt > 1) {
185         printf("syscap_tool: extra operand \"%s\"\n", arg);
186         printf("Try 'syscap_tool --help' for more information.\n");
187     } else {
188         (void)PrintVersion();
189     }
190 }
191 
OutputHelp(void)192 void OutputHelp(void)
193 {
194     (void)PrintHelp();
195     printf("\n");
196     (void)PrintVersion();
197 }