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 <limits.h>
22 #include "securec.h"
23 #include "syscap_tool.h"
24 #include "create_pcid.h"
25 #include "context_tool.h"
26
27 #define SYSCAP_VERSION "2.0.1"
28 #define OUTPUT_VERSION_LEN 200
29 #define ENCODE 0
30 #define DECODE 1
31 #define STRING_DECODE 2
32 #define RPCID 3
33 #define PCID 4
34 #define PCID_STRING 5
35 #define RPCID_STRING 6
36 #define VERSION 7
37 #define INPUT_FILE 8
38 #define OUTPUT_FILE 9
39 #define HELP 10
40 #define INPUT_FILE_NUM 4
41
42 static void PrintHelp(void);
43 static void PrintVersion(void);
44 static void OutputVersion(const char *arg, int opt);
45 static void OutputHelp(void);
46
47 void SetBitMap(char *const *argv, int32_t flag, uint16_t *bitMap, char **outputpath);
48
49 int32_t OperateByBitMap(char *const *argv, uint16_t bitMap, char *outputpath);
50
51 static struct option g_longOptions[] = {
52 {"help", no_argument, 0, 'h' },
53 {"version", no_argument, 0, 'v' },
54 {"rpcid", no_argument, 0, 'R' },
55 {"pcid", no_argument, 0, 'P' },
56 {"compare", required_argument, 0, 'C' },
57 {"encode", no_argument, 0, 'e' },
58 {"decode", no_argument, 0, 'd' },
59 {"string", no_argument, 0, 's' },
60 {"input", required_argument, 0, 'i' },
61 {"output", required_argument, 0, 'o' },
62 {0, 0, 0, 0 }
63 };
64
65 static struct {
66 char *inputfile;
67 char *pcidfile;
68 char *rpcidfile;
69 } g_customerfileinfo;
70
main(int argc,char ** argv)71 int main(int argc, char **argv)
72 {
73 int32_t optIndex;
74 int32_t ret;
75 uint16_t bitMap = 0x0;
76 char curpath[PATH_MAX] = {0};
77 g_customerfileinfo.inputfile = NULL;
78 g_customerfileinfo.pcidfile = NULL;
79 g_customerfileinfo.rpcidfile = NULL;
80 char *outputpath = getcwd(curpath, sizeof(curpath));
81 if (outputpath == NULL) {
82 PRINT_ERR("Get outputpath failed.\n");
83 return -1;
84 }
85
86 while (1) {
87 int32_t flag = getopt_long(argc, argv, "hvRPC:edsi:o:", g_longOptions, &optIndex);
88 if (flag == -1) {
89 break;
90 }
91 if (flag == 'C') {
92 if (argc != INPUT_FILE_NUM || optind < 0 || optind >= argc) { // 4, argc of ./syscap_tool -C f1 f2
93 PRINT_ERR("Input file too few or too many.\n");
94 return -1;
95 }
96 }
97 SetBitMap(argv, flag, &bitMap, &outputpath);
98 }
99 ret = OperateByBitMap(argv, bitMap, outputpath);
100 if (ret != 0) {
101 PRINT_ERR("syscap_tool failed to complete. input: %s\n", g_customerfileinfo.inputfile);
102 }
103 return ret;
104 }
105
OperateByBitMap(char * const * argv,uint16_t bitMap,char * outputpath)106 int32_t OperateByBitMap(char *const *argv, uint16_t bitMap, char *outputpath)
107 {
108 int32_t ret = 0;
109 switch (bitMap) {
110 case 0x109: // 0x109, -Rei inputfile
111 ret = RPCIDEncode(g_customerfileinfo.inputfile, outputpath); break;
112 case 0x10A: // 0x10A, -Rdi inputfile
113 ret = RPCIDDecode(g_customerfileinfo.inputfile, outputpath); break;
114 case 0x10D: // 0x10D, -Resi inputfile
115 ret = EncodeRpcidscToString(g_customerfileinfo.inputfile, outputpath); break;
116 case 0x115: // 0x115, -Pesi inputfile
117 ret = EncodePcidscToString(g_customerfileinfo.inputfile, outputpath); break;
118 case 0x60: // 0x60, -C pcid.txt rpcid.txt
119 ret = ComparePcidWithRpcidString(g_customerfileinfo.pcidfile, g_customerfileinfo.rpcidfile, TYPE_FILE);
120 break;
121 case 0x64: // 0x64, -sC "pcidstring" "rpcidstring"
122 ret = ComparePcidWithRpcidString(g_customerfileinfo.pcidfile, g_customerfileinfo.rpcidfile, TYPE_STRING);
123 break;
124 case 0x111: // 0x111, -Pei inputfile
125 ret = CreatePCID(g_customerfileinfo.inputfile, outputpath); break;
126 case 0x112: // 0x112, -Pdi inputfile
127 ret = DecodePCID(g_customerfileinfo.inputfile, outputpath); break;
128 case 0x116: // 0x116, -Pdsi inputfile
129 ret = DecodeStringPCIDToJson(g_customerfileinfo.inputfile, outputpath); break;
130 case 0x10E: // 0x10E, -Rdsi inputfile
131 printf("-Rdsi is not support currently.\n"); break;
132 case 0x80: // 0x80, -v
133 if (optind < 0 || optind >= INPUT_FILE_NUM) {
134 PRINT_ERR("Input file too few or too many.\n");
135 return -1;
136 } else {
137 (void)OutputVersion(argv[optind], optind);
138 break;
139 }
140 default:
141 (void)OutputHelp();
142 }
143 return ret;
144 }
145
SetBitMap(char * const * argv,int32_t flag,uint16_t * bitMap,char ** outputpath)146 void SetBitMap(char *const *argv, int32_t flag, uint16_t *bitMap, char **outputpath)
147 {
148 switch (flag) {
149 case 'e':
150 (*bitMap) |= 0x1 << ENCODE;
151 break;
152 case 'd':
153 (*bitMap) |= 0x1 << DECODE;
154 break;
155 case 's':
156 (*bitMap) |= 0x1 << STRING_DECODE;
157 break;
158 case 'R':
159 (*bitMap) |= 0x1 << RPCID;
160 break;
161 case 'P':
162 (*bitMap) |= 0x1 << PCID;
163 break;
164 case 'C':
165 (*bitMap) |= 0x1 << PCID_STRING;
166 (*bitMap) |= 0x1 << RPCID_STRING;
167 g_customerfileinfo.pcidfile = optarg;
168 g_customerfileinfo.rpcidfile = argv[optind];
169 break;
170 case 'i':
171 (*bitMap) |= 0x1 << INPUT_FILE;
172 g_customerfileinfo.inputfile = optarg;
173 break;
174 case 'o':
175 (*outputpath) = optarg;
176 break;
177 case 'v':
178 (*bitMap) |= 0x1 << VERSION;
179 break;
180 case 'h':
181 default:
182 (*bitMap) |= 0x1 << HELP;
183 }
184 }
185
PrintVersion(void)186 void PrintVersion(void)
187 {
188 char outputVersion[OUTPUT_VERSION_LEN] = {0};
189 int ret = sprintf_s(outputVersion, OUTPUT_VERSION_LEN, "syscap_tool v%s", SYSCAP_VERSION);
190 if (ret == -1) {
191 PRINT_ERR("sprintf_s failed.\n");
192 exit(-1);
193 }
194 printf("%s\n", outputVersion);
195 }
196
PrintHelp(void)197 void PrintHelp(void)
198 {
199 printf("syscap_tool -R/P -e/d -i filepath [-o outpath]\n");
200 printf("-h, --help\t: how to use\n");
201 printf("-R, --rpcid\t: encode or decode rpcid\n");
202 printf("-P, --pcid\t: encode or decode pcid\n");
203 printf("-C, --compare\t: compare pcid with rpcid string format.\n\t"
204 "-s, --string : input string.\n");
205 printf("-e, --encode\t: encode to sc format.\n\t-s, --string : encode to string format.\n");
206 printf("-d, --decode\t: decode to json format.\n\t-s, --string : decode string format.\n");
207 printf("-i filepath, --input filepath\t: input file\n");
208 printf("-o outpath, --input outpath\t: output path\n");
209 printf("-v, --version\t: print syscap_tool version information.\n");
210 }
211
OutputVersion(const char * arg,int opt)212 void OutputVersion(const char *arg, int opt)
213 {
214 if (arg != NULL && opt > 1) {
215 printf("Try 'syscap_tool --help' for more information.\n");
216 } else {
217 (void)PrintVersion();
218 }
219 }
220
OutputHelp(void)221 void OutputHelp(void)
222 {
223 (void)PrintHelp();
224 printf("\n");
225 (void)PrintVersion();
226 }