• 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 <stdint.h>
17 #include <stdio.h>
18 #include <stdbool.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <sys/stat.h>
24 #include "securec.h"
25 #include "endian_internal.h"
26 #include "cJSON.h"
27 #include "create_pcid.h"
28 #include "syscap_tool.h"
29 #include "context_tool.h"
30 
31 #ifdef SYSCAP_DEFINE_EXTERN_ENABLE
32 #include "syscap_define_custom.h"
33 #else
34 #include "syscap_define.h"
35 #endif
36 
37 #define SYSCAP_PREFIX_LEN 17
38 #define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN)
39 #define UINT8_BIT 8
40 #define INT_BIT 32
41 #define RPCID_OUT_BUFFER 32
42 #define PCID_OUT_BUFFER RPCID_OUT_BUFFER
43 #define BYTES_OF_OS_SYSCAP 120
44 #define U32_TO_STR_MAX_LEN 11
45 #define STRING_FORMAT_LEN_MAX 1024
46 
47 #define FREE_CONVERT_OUT_RPCID_ENCODE 1
48 #define FREE_CONTEXT_OUT_RPCID_ENCODE 2
49 
50 #define FREE_OUTBUFFER_AFTER_RPCIDSC 1
51 #define FREE_MALLOC_RPISYSCAP_AFTER_RPCIDSC 2
52 #define FREE_MALLOC_OSSYSCAP_AFTER_RPCIDSC 3
53 #define FREE_WHOLE_SYSCAP_AFTER_RPCIDSC 4
54 #define FREE_RPCID_ROOT_AFTER_RPCIDSC 5
55 #define FREE_CONTEXT_OUT_AFTER_RPCIDSC 6
56 
57 struct JsonObjectSysCap {
58     cJSON *cjsonObjectRoot;
59     cJSON *sysCapPtr;
60 };
61 
62 struct FreeAfterEncodeRpcidscInfo {
63     char *outBuffer;
64     char *priSyscapArray;
65     uint16_t *osSysCapIndex;
66     cJSON *sysCapDefine;
67     cJSON *rpcidRoot;
68     char *contextBuffer;
69     int32_t type;
70     int16_t flag;
71 };
72 
FillOsCapLength(char * convertedBuffer,char * contextBuffer,struct JsonObjectSysCap gJsonObjectSysCap,uint32_t sysCapSize,int32_t ret)73 static int32_t FillOsCapLength(char *convertedBuffer, char *contextBuffer, struct JsonObjectSysCap gJsonObjectSysCap,
74         uint32_t sysCapSize, int32_t ret)
75 {
76     RPCIDHead *headPtr = NULL;
77     char *fillTmpPtr = NULL;
78     cJSON *apiVerItem = NULL;
79     cJSON *arrayItemPtr = NULL;
80 
81     headPtr = (RPCIDHead *)convertedBuffer;
82     apiVerItem = cJSON_GetObjectItem(gJsonObjectSysCap.cjsonObjectRoot, "api_version");
83     if (apiVerItem == NULL || !cJSON_IsNumber(apiVerItem)) {
84         PRINT_ERR("get \"api_version\" failed\n");
85         return -1;
86     }
87     headPtr->apiVersion = HtonsInter((uint16_t)apiVerItem->valueint);
88     headPtr->apiVersionType = 1;
89 
90     fillTmpPtr = convertedBuffer + sizeof(RPCIDHead);
91     *(uint16_t *)fillTmpPtr = HtonsInter(2); // 2, SysCap Type, 2: request Cap
92     fillTmpPtr += sizeof(uint16_t);
93     // fill osCap Length
94     *(uint16_t *)fillTmpPtr = HtonsInter((uint16_t)(sysCapSize * SINGLE_FEAT_LEN));
95     fillTmpPtr += sizeof(uint16_t);
96     for (uint32_t i = 0; i < sysCapSize; i++) {
97         arrayItemPtr = cJSON_GetArrayItem(gJsonObjectSysCap.sysCapPtr, (int)i);
98         if (arrayItemPtr->valuestring == NULL) {
99             PRINT_ERR("arrayItemPtr->valuestring is NULL\n");
100             return -1;
101         }
102 
103         char *pointPos = strchr(arrayItemPtr->valuestring, '.');
104         if (pointPos == NULL) {
105             PRINT_ERR("context of \"syscap\" array is invalid\n");
106             return -1;
107         }
108         ret = strncmp(arrayItemPtr->valuestring, "SystemCapability.", pointPos - arrayItemPtr->valuestring + 1);
109         if (ret != 0) {
110             PRINT_ERR("context of \"syscap\" array is invalid\n");
111             return -1;
112         }
113 
114         ret = memcpy_s(fillTmpPtr, SINGLE_FEAT_LEN, pointPos + 1, strlen(pointPos + 1));
115         if (ret != 0) {
116             PRINT_ERR("context of \"syscap\" array is invalid\n");
117             return -1;
118         }
119         fillTmpPtr += SINGLE_FEAT_LEN;
120     }
121     return ret;
122 }
123 
FreeAfterRPCIDEncode(char * convertedBuffer,char * contextBuffer,int32_t type,int32_t ret)124 static int32_t FreeAfterRPCIDEncode(char *convertedBuffer, char *contextBuffer, int32_t type, int32_t ret)
125 {
126     if (type == FREE_CONVERT_OUT_RPCID_ENCODE) {
127         free(convertedBuffer);
128     }
129     FreeContextBuffer(contextBuffer);
130     return ret;
131 }
132 
RPCIDEncode(char * inputFile,char * outputPath)133 int32_t RPCIDEncode(char *inputFile, char *outputPath)
134 {
135     int32_t ret;
136     char *contextBuffer = NULL;
137     uint32_t bufferLen, sysCapSize;
138     char *convertedBuffer = NULL;
139     uint32_t convertedBufLen = sizeof(RPCIDHead);
140     struct JsonObjectSysCap gJsonObjectSysCap;
141     gJsonObjectSysCap.cjsonObjectRoot = NULL;
142     gJsonObjectSysCap.sysCapPtr = NULL;
143 
144     ret = GetFileContext(inputFile, &contextBuffer, &bufferLen);
145     if (ret != 0) {
146         PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
147         return ret;
148     }
149 
150     gJsonObjectSysCap.cjsonObjectRoot = cJSON_ParseWithLength(contextBuffer, bufferLen);
151     if (gJsonObjectSysCap.cjsonObjectRoot == NULL) {
152         PRINT_ERR("cJSON_Parse failed, context buffer is:\n%s\n", contextBuffer);
153         return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONTEXT_OUT_RPCID_ENCODE, -1);
154     }
155 
156     gJsonObjectSysCap.sysCapPtr = cJSON_GetObjectItem(gJsonObjectSysCap.cjsonObjectRoot, "syscap");
157     if (gJsonObjectSysCap.sysCapPtr == NULL || !cJSON_IsArray(gJsonObjectSysCap.sysCapPtr)) {
158         PRINT_ERR("get \"syscap\" object failed.\n");
159         return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONTEXT_OUT_RPCID_ENCODE, -1);
160     }
161 
162     ret = cJSON_GetArraySize(gJsonObjectSysCap.sysCapPtr);
163     if (ret < 0) {
164         PRINT_ERR("get \"syscap\" array size failed\n");
165         return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONTEXT_OUT_RPCID_ENCODE, -1);
166     }
167     sysCapSize = (uint32_t)ret;
168     // 2, to save SysCaptype & SysCapLength
169     convertedBufLen += (2 * sizeof(uint16_t) + sysCapSize * SINGLE_FEAT_LEN);
170 
171     convertedBuffer = (char *)malloc(convertedBufLen);
172     if (convertedBuffer == NULL) {
173         PRINT_ERR("malloc failed\n");
174         return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONTEXT_OUT_RPCID_ENCODE, -1);
175     }
176     (void)memset_s(convertedBuffer, convertedBufLen, 0, convertedBufLen);
177 
178     ret = FillOsCapLength(convertedBuffer, contextBuffer, gJsonObjectSysCap,  sysCapSize, ret);
179     if (ret == -1) {
180         return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONVERT_OUT_RPCID_ENCODE, ret);
181     }
182     ret = ConvertedContextSaveAsFile(outputPath, "RPCID.sc", convertedBuffer, convertedBufLen);
183     if (ret != 0) {
184         PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.sc\n", outputPath);
185     }
186     return FreeAfterRPCIDEncode(convertedBuffer, contextBuffer, FREE_CONVERT_OUT_RPCID_ENCODE, ret);
187 }
188 
ParseRpcidToJson(char * input,uint32_t inputLen,cJSON * rpcidJson)189 static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson)
190 {
191     uint32_t i;
192     int32_t ret = 0;
193     uint16_t sysCapLength = NtohsInter(*(uint16_t *)(input + sizeof(uint32_t)));
194     if (sysCapLength > inputLen - sizeof(uint32_t)) {
195         PRINT_ERR("Get sysCapLength(%u) error, inputLen = %u\n", sysCapLength, inputLen);
196         return -1;
197     }
198     uint16_t sysCapCount = sysCapLength / SINGLE_FEAT_LEN;
199     char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t);
200     RPCIDHead *rpcidHeader = (RPCIDHead *)input;
201     cJSON *sysCapJson = cJSON_CreateArray();
202     for (i = 0; i < sysCapCount; i++) {
203         char *temp = sysCapBegin + i * SINGLE_FEAT_LEN;
204         if (strlen(temp) >= SINGLE_FEAT_LEN || strlen(temp) == 0) {
205             PRINT_ERR("Get SysCap failed, string length(%u) error.\n", (uint32_t)strlen(temp));
206             cJSON_Delete(sysCapJson);
207             return -1;
208         }
209         char buffer[SINGLE_SYSCAP_LEN] = "SystemCapability.";
210 
211         ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LEN);
212         if (ret != EOK) {
213             PRINT_ERR("strncat_s failed.\n");
214             cJSON_Delete(sysCapJson);
215             return ret;
216         }
217 
218         if (!cJSON_AddItemToArray(sysCapJson, cJSON_CreateString(buffer))) {
219             PRINT_ERR("Add syscap string to json failed.\n");
220             cJSON_Delete(sysCapJson);
221             return -1;
222         }
223     }
224 
225     if (!cJSON_AddNumberToObject(rpcidJson, "api_version", NtohsInter(rpcidHeader->apiVersion))) {
226         PRINT_ERR("Add api_version to json failed.\n");
227         cJSON_Delete(sysCapJson);
228         return -1;
229     }
230     if (!cJSON_AddItemToObject(rpcidJson, "syscap", sysCapJson)) {
231         PRINT_ERR("Add syscap to json failed.\n");
232         cJSON_Delete(sysCapJson);
233         return -1;
234     }
235 
236     return ret;
237 }
238 
RPCIDDecode(char * inputFile,char * outputPath)239 int32_t RPCIDDecode(char *inputFile, char *outputPath)
240 {
241     int32_t ret = 0;
242     char *contextBuffer = NULL;
243     char *convertedBuffer = NULL;
244     uint32_t bufferLen;
245 
246     // check rpcid.sc
247     if (CheckRpcidFormat(inputFile, &contextBuffer, &bufferLen)) {
248         PRINT_ERR("Check rpcid.sc format failed. Input failed: %s\n", inputFile);
249         goto FREE_CONTEXT_OUT;
250     }
251 
252     // parse rpcid to json
253     cJSON *rpcidRoot = cJSON_CreateObject();
254     if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot) != 0) {
255         PRINT_ERR("Prase rpcid to json failed. Input failed: %s\n", inputFile);
256         goto FREE_RPCID_ROOT;
257     }
258 
259     // save to json file
260     convertedBuffer = cJSON_Print(rpcidRoot);
261     ret = ConvertedContextSaveAsFile(outputPath, "RPCID.json", convertedBuffer, strlen(convertedBuffer));
262     if (ret != 0) {
263         PRINT_ERR("ConvertedContextSaveAsFile failed, outputPath:%s, filename:rpcid.json\n", outputPath);
264         goto FREE_RPCID_ROOT;
265     }
266 
267 FREE_RPCID_ROOT:
268     cJSON_Delete(rpcidRoot);
269 FREE_CONTEXT_OUT:
270     FreeContextBuffer(contextBuffer);
271     return ret;
272 }
273 
SetOsSysCapBitMap(uint8_t * out,uint16_t outLen,const uint16_t * index,uint16_t indexLen)274 static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, const uint16_t *index, uint16_t indexLen)
275 {
276     uint16_t sector, pos;
277 
278     if (outLen != BYTES_OF_OS_SYSCAP) {
279         PRINT_ERR("Input array error.\n");
280         return -1;
281     }
282 
283     for (uint16_t i = 0; i < indexLen; i++) {
284         sector = index[i] / UINT8_BIT;
285         pos = index[i] % UINT8_BIT;
286         if (sector >= BYTES_OF_OS_SYSCAP) {
287             PRINT_ERR("Syscap num(%u) out of range(120).\n", sector);
288             return -1;
289         }
290         out[sector] |=  (1 << pos);
291     }
292     return 0;
293 }
294 
PrintOutputToFile(struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo,uint32_t outUint[RPCID_OUT_BUFFER],uint16_t indexPri,char * outDirPath)295 static int32_t PrintOutputToFile(struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo,
296         uint32_t outUint[RPCID_OUT_BUFFER], uint16_t indexPri, char *outDirPath)
297 {
298     int32_t ret = 0;
299 
300     uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER + SINGLE_SYSCAP_LEN * indexPri;
301     freeAfterEncodeRpcidscInfo.outBuffer = (char *)malloc(outBufferLen);
302     if (freeAfterEncodeRpcidscInfo.outBuffer == NULL) {
303         PRINT_ERR("malloc(%u) failed.\n", outBufferLen);
304         freeAfterEncodeRpcidscInfo.flag = 1;
305         return ret;
306     }
307 
308     freeAfterEncodeRpcidscInfo.type = FREE_OUTBUFFER_AFTER_RPCIDSC;
309     (void)memset_s(freeAfterEncodeRpcidscInfo.outBuffer, outBufferLen, 0, outBufferLen);
310     ret = sprintf_s(freeAfterEncodeRpcidscInfo.outBuffer, outBufferLen, "%u", outUint[0]);
311     if (ret == -1) {
312         PRINT_ERR("sprintf_s failed.\n");
313         freeAfterEncodeRpcidscInfo.flag = 1;
314         return ret;
315     }
316     for (int i = 1; i < RPCID_OUT_BUFFER; i++) {
317         ret = sprintf_s(freeAfterEncodeRpcidscInfo.outBuffer, outBufferLen, "%s,%u",
318                 freeAfterEncodeRpcidscInfo.outBuffer, outUint[i]);
319         if (ret == -1) {
320             PRINT_ERR("sprintf_s failed.\n");
321             freeAfterEncodeRpcidscInfo.flag = 1;
322             return ret;
323         }
324     }
325 
326     for (uint16_t i = 0; i < indexPri; i++) {
327         ret = sprintf_s(freeAfterEncodeRpcidscInfo.outBuffer, outBufferLen, "%s,%s",
328             freeAfterEncodeRpcidscInfo.outBuffer, freeAfterEncodeRpcidscInfo.priSyscapArray + i * SINGLE_SYSCAP_LEN);
329         if (ret == -1) {
330             PRINT_ERR("sprintf_s failed.\n");
331             freeAfterEncodeRpcidscInfo.flag = 1;
332             return ret;
333         }
334     }
335 
336     const char outputFilename[] = "RPCID.txt";
337     ret = ConvertedContextSaveAsFile(outDirPath, outputFilename, freeAfterEncodeRpcidscInfo.outBuffer,
338             strlen(freeAfterEncodeRpcidscInfo.outBuffer));
339     if (ret != 0) {
340         PRINT_ERR("Save to txt file failed. Output path:%s/%s\n", outDirPath, outputFilename);
341         freeAfterEncodeRpcidscInfo.flag = 1;
342     }
343     return ret;
344 }
345 
OutputSetMemAndPrintToFile(struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo,int32_t sysCapArraySize,cJSON * sysCapArray,char * outDirPath)346 static int32_t OutputSetMemAndPrintToFile(struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo,
347     int32_t sysCapArraySize, cJSON *sysCapArray, char *outDirPath)
348 {
349     char *priSyscap = NULL;
350     cJSON *cJsonTemp = NULL;
351     uint16_t indexPri = 0;
352     int32_t ret = 0;
353     freeAfterEncodeRpcidscInfo.type = FREE_MALLOC_OSSYSCAP_AFTER_RPCIDSC;
354 
355     (void)memset_s(freeAfterEncodeRpcidscInfo.osSysCapIndex, sizeof(uint16_t) * sysCapArraySize,
356                    0, sizeof(uint16_t) * sysCapArraySize);
357     // malloc for save private syscap string
358     freeAfterEncodeRpcidscInfo.priSyscapArray = (char *)malloc((uint32_t)sysCapArraySize * SINGLE_SYSCAP_LEN);
359     if (freeAfterEncodeRpcidscInfo.priSyscapArray == NULL) {
360         PRINT_ERR("malloc(%d) failed.\n", sysCapArraySize * SINGLE_SYSCAP_LEN);
361         freeAfterEncodeRpcidscInfo.flag = 1;
362         return ret;
363     }
364     (void)memset_s(freeAfterEncodeRpcidscInfo.priSyscapArray, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN),
365                    0, (size_t)(sysCapArraySize * SINGLE_SYSCAP_LEN));
366     priSyscap = freeAfterEncodeRpcidscInfo.priSyscapArray;
367     // part os syscap and ptivate syscap
368     uint16_t indexOs = 0;
369     for (int i = 0; i < sysCapArraySize; i++) {
370         cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i);
371         if (cJsonItem->valuestring == NULL) {
372             continue;
373         }
374         cJsonTemp = cJSON_GetObjectItem(freeAfterEncodeRpcidscInfo.sysCapDefine, cJsonItem->valuestring);
375         if (cJsonTemp != NULL && cJSON_IsNumber(cJsonTemp)) {
376             freeAfterEncodeRpcidscInfo.osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint);
377         } else {
378             ret = strcpy_s(priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring);
379             if (ret != EOK) {
380                 PRINT_ERR("strcpy_s failed.\n");
381                 freeAfterEncodeRpcidscInfo.flag = 1;
382                 return ret;
383             }
384             priSyscap += SINGLE_SYSCAP_LEN;
385             indexPri++;
386         }
387     }
388     uint32_t outUint[RPCID_OUT_BUFFER] = {0};
389     outUint[0] = *(uint32_t *)freeAfterEncodeRpcidscInfo.contextBuffer;
390     outUint[1] = *(uint32_t *)(freeAfterEncodeRpcidscInfo.contextBuffer + sizeof(uint32_t));
391     uint8_t *osOutUint = (uint8_t *)(outUint + 2);
392     // 120, len of osOutUint
393     if (SetOsSysCapBitMap(osOutUint, 120, freeAfterEncodeRpcidscInfo.osSysCapIndex, indexOs) != 0) {
394         PRINT_ERR("Set os syscap bit map failed.\n");
395         freeAfterEncodeRpcidscInfo.flag = 1;
396         return ret;
397     }
398     return PrintOutputToFile(freeAfterEncodeRpcidscInfo, outUint, indexPri, outDirPath);
399 }
400 
FreeAfterEncodeRpcidsc(struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo,int32_t type,int32_t ret)401 static int32_t FreeAfterEncodeRpcidsc(struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo, int32_t type,
402         int32_t ret)
403 {
404     switch (type) {
405         case FREE_OUTBUFFER_AFTER_RPCIDSC:
406             free(freeAfterEncodeRpcidscInfo.outBuffer);
407             free(freeAfterEncodeRpcidscInfo.priSyscapArray);
408             free(freeAfterEncodeRpcidscInfo.osSysCapIndex);
409             cJSON_Delete(freeAfterEncodeRpcidscInfo.sysCapDefine);
410             cJSON_Delete(freeAfterEncodeRpcidscInfo.rpcidRoot);
411             FreeContextBuffer(freeAfterEncodeRpcidscInfo.contextBuffer);
412             break;
413         case FREE_MALLOC_RPISYSCAP_AFTER_RPCIDSC:
414             free(freeAfterEncodeRpcidscInfo.priSyscapArray);
415             free(freeAfterEncodeRpcidscInfo.osSysCapIndex);
416             cJSON_Delete(freeAfterEncodeRpcidscInfo.sysCapDefine);
417             cJSON_Delete(freeAfterEncodeRpcidscInfo.rpcidRoot);
418             FreeContextBuffer(freeAfterEncodeRpcidscInfo.contextBuffer);
419             break;
420         case FREE_MALLOC_OSSYSCAP_AFTER_RPCIDSC:
421             free(freeAfterEncodeRpcidscInfo.osSysCapIndex);
422             cJSON_Delete(freeAfterEncodeRpcidscInfo.sysCapDefine);
423             cJSON_Delete(freeAfterEncodeRpcidscInfo.rpcidRoot);
424             FreeContextBuffer(freeAfterEncodeRpcidscInfo.contextBuffer);
425             break;
426         case FREE_WHOLE_SYSCAP_AFTER_RPCIDSC:
427             cJSON_Delete(freeAfterEncodeRpcidscInfo.sysCapDefine);
428             cJSON_Delete(freeAfterEncodeRpcidscInfo.rpcidRoot);
429             FreeContextBuffer(freeAfterEncodeRpcidscInfo.contextBuffer);
430             break;
431         case FREE_RPCID_ROOT_AFTER_RPCIDSC:
432             cJSON_Delete(freeAfterEncodeRpcidscInfo.rpcidRoot);
433             FreeContextBuffer(freeAfterEncodeRpcidscInfo.contextBuffer);
434             break;
435         case FREE_CONTEXT_OUT_AFTER_RPCIDSC:
436         default:
437             FreeContextBuffer(freeAfterEncodeRpcidscInfo.contextBuffer);
438     }
439     return ret;
440 }
441 
EncodeRpcidscToString(char * inputFile,char * outDirPath)442 int32_t EncodeRpcidscToString(char *inputFile, char *outDirPath)
443 {
444     int32_t ret = 0;
445     int32_t sysCapArraySize;
446     uint32_t bufferLen;
447     cJSON *sysCapArray = NULL;
448     struct FreeAfterEncodeRpcidscInfo freeAfterEncodeRpcidscInfo;
449     freeAfterEncodeRpcidscInfo.priSyscapArray = NULL;
450     freeAfterEncodeRpcidscInfo.osSysCapIndex = NULL;
451     freeAfterEncodeRpcidscInfo.sysCapDefine = NULL;
452     freeAfterEncodeRpcidscInfo.rpcidRoot = NULL;
453     freeAfterEncodeRpcidscInfo.outBuffer = NULL;
454     freeAfterEncodeRpcidscInfo.contextBuffer = NULL;
455     freeAfterEncodeRpcidscInfo.type = 0;
456     freeAfterEncodeRpcidscInfo.flag = 0;
457 
458     // check rpcid.sc
459     if (CheckRpcidFormat(inputFile, &freeAfterEncodeRpcidscInfo.contextBuffer, &bufferLen) != 0) {
460         PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile);
461         return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_CONTEXT_OUT_AFTER_RPCIDSC, ret);
462     }
463 
464     // parse rpcid to json
465     freeAfterEncodeRpcidscInfo.rpcidRoot = cJSON_CreateObject();
466     if (ParseRpcidToJson(freeAfterEncodeRpcidscInfo.contextBuffer, bufferLen,
467             freeAfterEncodeRpcidscInfo.rpcidRoot) != 0) {
468         PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile);
469         return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_RPCID_ROOT_AFTER_RPCIDSC, ret);
470     }
471 
472     // trans to string format
473     freeAfterEncodeRpcidscInfo.sysCapDefine =  CreateWholeSyscapJsonObj();
474     sysCapArray = cJSON_GetObjectItem(freeAfterEncodeRpcidscInfo.rpcidRoot, "syscap");
475     if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) {
476         PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile);
477         return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_WHOLE_SYSCAP_AFTER_RPCIDSC, ret);
478     }
479     sysCapArraySize = cJSON_GetArraySize(sysCapArray);
480     if (sysCapArraySize < 0) {
481         PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile);
482         return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_WHOLE_SYSCAP_AFTER_RPCIDSC, ret);
483     }
484     // malloc for save os syscap index
485     freeAfterEncodeRpcidscInfo.osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) * sysCapArraySize);
486     if (freeAfterEncodeRpcidscInfo.osSysCapIndex == NULL) {
487         PRINT_ERR("malloc failed.\n");
488         return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_WHOLE_SYSCAP_AFTER_RPCIDSC, ret);
489     }
490 
491     ret = OutputSetMemAndPrintToFile(freeAfterEncodeRpcidscInfo, sysCapArraySize, sysCapArray, outDirPath);
492     if (freeAfterEncodeRpcidscInfo.flag == 1) {
493         return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, freeAfterEncodeRpcidscInfo.type, ret);
494     }
495     return FreeAfterEncodeRpcidsc(freeAfterEncodeRpcidscInfo, FREE_OUTBUFFER_AFTER_RPCIDSC, ret);
496 }
497 
CopyInputString(const char * inputString)498 char *CopyInputString(const char *inputString)
499 {
500     if (inputString == NULL || *inputString == '\0') {
501         PRINT_ERR("inputString is null or empty.\n");
502         return NULL;
503     }
504     size_t inputLen = strlen(inputString);
505     if (inputLen > STRING_FORMAT_LEN_MAX) {
506         PRINT_ERR("input string too long(%zu).\n", inputLen);
507         return NULL;
508     }
509     char *input = (char *)malloc(inputLen + 1);
510     if (input == NULL) {
511         PRINT_ERR("malloc failed.\n");
512         return NULL;
513     }
514     int32_t ret = strcpy_s(input, inputLen + 1, inputString);
515     if (ret != EOK) {
516         PRINT_ERR("strcpy_s failed.\n");
517         free(input);
518         return NULL;
519     }
520     input[inputLen] = '\0';
521     return input;
522 }
523 
GetPriSyscapData(char * input,char ** priSyscap,uint32_t * priSyscapLen)524 int32_t GetPriSyscapData(char *input, char **priSyscap, uint32_t *priSyscapLen)
525 {
526     // count private syscaps
527     uint32_t count = 0;
528     for (uint32_t i = 0; *(input + i) != '\0'; i++) {
529         if (*(input + i) == ',') {
530             count++;
531         }
532     }
533     count++;
534     // get private syscap string
535     char *priSysCapOut = (char *)malloc(SINGLE_SYSCAP_LEN * count);
536     if (priSysCapOut == NULL) {
537         PRINT_ERR("sscanf_s failed.\n");
538         return -1;
539     }
540     (void)memset_s(priSysCapOut, SINGLE_SYSCAP_LEN * count, 0, SINGLE_SYSCAP_LEN * count);
541     char *private = priSysCapOut;
542 
543     char *tok = NULL;
544     char *temp = strtok_r(input, ",", &tok);
545     while (temp) {
546         int ret = strncpy_s(private, SINGLE_SYSCAP_LEN, temp, SINGLE_SYSCAP_LEN - 1);
547         if (ret != EOK) {
548             PRINT_ERR("strncpy_s failed.\n");
549             free(priSysCapOut);
550             return -1;
551         }
552         temp = strtok_r(NULL, ",", &tok);
553         private += SINGLE_SYSCAP_LEN;
554     }
555 
556     *priSyscap = priSysCapOut;
557     *priSyscapLen = count;
558     return 0;
559 }
560 
SeparateSyscapFromString(const char * inputString,uint32_t * osArray,uint32_t osArraySize,char ** priSyscap,uint32_t * priSyscapLen)561 int32_t SeparateSyscapFromString(const char *inputString, uint32_t *osArray, uint32_t osArraySize,
562                                  char **priSyscap, uint32_t *priSyscapLen)
563 {
564     if (osArraySize != PCID_OUT_BUFFER) {
565         return -1;
566     }
567 
568     // copy origin string
569     char *input = CopyInputString(inputString);
570     if (input == NULL) {
571         return -1;
572     }
573 
574     // get os syscap data
575     for (uint32_t i = 0; i < PCID_OUT_BUFFER; i++) {
576         if (sscanf_s(input, "%u,%s", &osArray[i], input, strlen(input)) == -1) {
577             PRINT_ERR("sscanf_s failed.\n");
578             free(input);
579             return -1;
580         }
581     }
582 
583     // get private syscap data
584     if (GetPriSyscapData(input, priSyscap, priSyscapLen) != 0) {
585         free(input);
586         return -1;
587     }
588 
589     free(input);
590     return 0;
591 }
592 
GetSyscapByIndex(uint32_t index)593 int32_t GetSyscapByIndex(uint32_t index)
594 {
595     const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
596     for (uint32_t i = 0; i < allSyscapNum; i++) {
597         if (g_arraySyscap[i].num == index) {
598             return i;
599         }
600     }
601     return -1;
602 }
603 
CompareOsSyscap(const uint32_t pcidOsArray[],const uint32_t rpcidOsAarry[])604 int32_t CompareOsSyscap(const uint32_t pcidOsArray[], const uint32_t rpcidOsAarry[])
605 {
606     int32_t ossyscapFlag = 0;
607     const size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
608 
609     for (uint32_t i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid
610         uint32_t blockBits = (pcidOsArray[i] ^ rpcidOsAarry[i]) & rpcidOsAarry[i];
611         if (!blockBits) {
612             continue;
613         }
614         for (uint8_t k = 0; k < INT_BIT; k++) {
615             if (!(blockBits & (1U << k))) {
616                 continue;
617             }
618             // 2, header of pcid & rpcid
619             size_t pos = (size_t)((i - 2) * INT_BIT + k);
620             if (pos >= allSyscapNum) {
621                 break;
622             }
623             printf("Missing: %s\n", g_arraySyscap[GetSyscapByIndex(pos)].str);
624             ossyscapFlag += 1;
625         }
626     }
627     return ossyscapFlag;
628 }
629 
ComparePriSyscap(char * pcid,char * rpcid,uint32_t pcidLen,uint32_t rpcidLen)630 int32_t ComparePriSyscap(char *pcid, char *rpcid, uint32_t pcidLen, uint32_t rpcidLen)
631 {
632     uint32_t i, j;
633     bool priSysFound = false;
634     int32_t prisyscapFlag = 0;
635 
636     for (i = 0; i < rpcidLen; i++) {
637         for (j = 0; j < pcidLen; j++) {
638             if (strcmp(rpcid + SINGLE_SYSCAP_LEN * i,
639                        pcid + SINGLE_SYSCAP_LEN * j) == 0) {
640                 priSysFound = true;
641                 break;
642             }
643         }
644         if (priSysFound != true) {
645             printf("Missing: %s\n", rpcid + SINGLE_SYSCAP_LEN * i);
646             prisyscapFlag += 1;
647         }
648         priSysFound = false;
649     }
650 
651     return prisyscapFlag;
652 }
653 
CompoareVersion(uint32_t * pcidOsArray,uint32_t * rpcidOsAarry)654 int32_t CompoareVersion(uint32_t *pcidOsArray, uint32_t *rpcidOsAarry)
655 {
656     int32_t versionFlag = 0;
657     uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsArray)->apiVersion);
658     uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion);
659     if (pcidVersion < rpcidVersion) {
660         printf("ERROR: Pcid version(%u) less than rpcid version(%u).\n", pcidVersion, rpcidVersion);
661         versionFlag = 1;
662     }
663     return versionFlag;
664 }
665 
ComparePcidWithRpcidString(char * pcidFile,char * rpcidFile,uint32_t type)666 int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile, uint32_t type)
667 {
668     int32_t ret;
669     char *pcidContent = NULL;
670     char *rpcidContent = NULL;
671     char *pcidPriSyscap = NULL;
672     char *rpcidPriSyscap = NULL;
673     uint32_t pcidContentLen, rpcidContentLen, pcidPriSyscapLen, rpcidPriSyscapLen;
674     uint32_t pcidOsArray[PCID_OUT_BUFFER] = {0};
675     uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0};
676 
677     if (type == TYPE_FILE) {
678         if (GetFileContext(pcidFile, &pcidContent, &pcidContentLen)) {
679             PRINT_ERR("Get pcid file context failed, input file : %s\n", pcidFile);
680             return -1;
681         }
682         if (GetFileContext(rpcidFile, &rpcidContent, &rpcidContentLen)) {
683             PRINT_ERR("Get rpcid file context failed, input file : %s\n", rpcidFile);
684             free(pcidContent);
685             return -1;
686         }
687     } else if (type == TYPE_STRING) {
688         pcidContent = pcidFile;
689         rpcidContent = rpcidFile;
690     } else {
691         PRINT_ERR("Input file type error, type=%u\n", type);
692         return -1;
693     }
694 
695     ret =  SeparateSyscapFromString(pcidContent, pcidOsArray, PCID_OUT_BUFFER,
696                                     &pcidPriSyscap, &pcidPriSyscapLen);
697     ret += SeparateSyscapFromString(rpcidContent, rpcidOsAarry, RPCID_OUT_BUFFER,
698                                     &rpcidPriSyscap, &rpcidPriSyscapLen);
699     if (ret != 0) {
700         PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret);
701         return -1;
702     }
703 
704     int32_t versionFlag = CompoareVersion(pcidOsArray, rpcidOsAarry);
705     int32_t ossyscapFlag = CompareOsSyscap(pcidOsArray, rpcidOsAarry);
706     int32_t prisyscapFlag = ComparePriSyscap(pcidPriSyscap, rpcidPriSyscap, pcidPriSyscapLen, rpcidPriSyscapLen);
707     if (!versionFlag && !ossyscapFlag && !prisyscapFlag) {
708         printf("Succeed! The pcid meets the rpcid.\n");
709     } else {
710         printf("Fail! The pcid does not meet the rpcid\n");
711     }
712     return 0;
713 }