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