• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-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 <errno.h>
17 #include <stdbool.h>
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/stat.h>
22 #include <securec.h>
23 #include <limits.h>
24 #include "cJSON.h"
25 #include "syscap_define.h"
26 #include "syscap_tool.h"
27 #include "endian_internal.h"
28 #include "syscap_interface.h"
29 
30 #define OS_SYSCAP_BYTES 120
31 #define SYSCAP_PREFIX_LEN 17
32 #define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN)
33 #define RPCID_OUT_BUFFER 32
34 #define PCID_OUT_BUFFER RPCID_OUT_BUFFER
35 #define UINT8_BIT 8
36 #define INT_BIT 32
37 #define U32_TO_STR_MAX_LEN 11
38 
39 
40 #define PRINT_ERR(...) \
41     do { \
42         printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
43         printf(__VA_ARGS__); \
44     } while (0)
45 
46 typedef struct RequiredProductCompatibilityIDHead {
47     uint16_t apiVersion : 15;
48     uint16_t apiVersionType : 1;
49 } RPCIDHead;
50 
51 typedef struct ProductCompatibilityID {
52     uint16_t apiVersion : 15;
53     uint16_t apiVersionType : 1;
54     uint16_t systemType : 3;
55     uint16_t reserved : 13;
56     uint32_t manufacturerID;
57     uint8_t osSyscap[OS_SYSCAP_BYTES];
58 } PCIDMain;
59 
60 static const char *pcidPath = "/system/etc/PCID.sc";
61 
FreeContextBuffer(char * contextBuffer)62 static void FreeContextBuffer(char *contextBuffer)
63 {
64     (void)free(contextBuffer);
65 }
66 
GetFileContext(const char * inputFile,char ** contextBufPtr,uint32_t * bufferLen)67 static int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen)
68 {
69     int32_t ret;
70     FILE *fp = NULL;
71     struct stat statBuf;
72     char *contextBuffer = NULL;
73     char path[PATH_MAX + 1] = {0x00};
74 
75 #ifdef _POSIX_
76     if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) {
77         PRINT_ERR("get path(%s) failed\n", inputFile);
78         return -1;
79     }
80 #else
81     if (strlen(inputFile) > PATH_MAX || realpath(inputFile, path) == NULL) {
82         PRINT_ERR("get file(%s) real path failed\n", inputFile);
83         return -1;
84     }
85 #endif
86 
87     ret = stat(path, &statBuf);
88     if (ret != 0) {
89         PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno);
90         return -1;
91     }
92     if (!(statBuf.st_mode & S_IRUSR)) {
93         PRINT_ERR("don't have permission to read the file(%s)\n", path);
94         return -1;
95     }
96     contextBuffer = (char *)malloc(statBuf.st_size + 1);
97     if (contextBuffer == NULL) {
98         PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno);
99         return -1;
100     }
101     fp = fopen(path, "rb");
102     if (fp == NULL) {
103         PRINT_ERR("open file(%s) failed, errno = %d\n", path, errno);
104         FreeContextBuffer(contextBuffer);
105         return -1;
106     }
107     size_t retFread = fread(contextBuffer, statBuf.st_size, 1, fp);
108     if (retFread != 1) {
109         PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno);
110         FreeContextBuffer(contextBuffer);
111         (void)fclose(fp);
112         return -1;
113     }
114     contextBuffer[statBuf.st_size] = '\0';
115     (void)fclose(fp);
116 
117     *contextBufPtr = contextBuffer;
118     *bufferLen = statBuf.st_size + 1;
119     return 0;
120 }
121 
EncodeOsSyscap(char * output,int len)122 bool EncodeOsSyscap(char *output, int len)
123 {
124     int32_t ret;
125     int32_t res;
126     char *contextBuffer = NULL;
127     uint32_t bufferLen;
128 
129     if (len != PCID_MAIN_BYTES) {
130         PRINT_ERR("Os Syscap input len(%d) must be equal to 128.\n", len);
131         return false;
132     }
133 
134     ret = GetFileContext(pcidPath, &contextBuffer, &bufferLen);
135     if (ret != 0) {
136         PRINT_ERR("GetFileContext failed, input file : /system/etc/PCID.sc\n");
137         return false;
138     }
139 
140     res = memcpy_s(output, PCID_MAIN_BYTES, contextBuffer, PCID_MAIN_BYTES);
141     if (res != 0) {
142         PRINT_ERR("memcpy_s failed.");
143         FreeContextBuffer(contextBuffer);
144         return false;
145     }
146 
147     FreeContextBuffer(contextBuffer);
148     return true;
149 }
150 
EncodePrivateSyscap(char ** output,int * outputLen)151 bool EncodePrivateSyscap(char **output, int *outputLen)
152 {
153     int32_t ret;
154     char *contextBuffer = NULL;
155     char *outputStr = NULL;
156     uint32_t bufferLen;
157 
158     ret = GetFileContext(pcidPath, &contextBuffer, &bufferLen);
159     if (ret != 0) {
160         PRINT_ERR("GetFileContext failed, input file : /system/etc/PCID.sc\n");
161         return false;
162     }
163 
164     uint32_t priLen = bufferLen - PCID_MAIN_BYTES - 1;
165     if ((int)priLen <= 0) {
166         *outputLen = 0;
167         return false;
168     }
169     outputStr = (char *)calloc(priLen, sizeof(char));
170     if (outputStr == NULL) {
171         PRINT_ERR("malloc buffer failed, size = %u, errno = %d\n", priLen, errno);
172         *outputLen = 0;
173         return false;
174     }
175 
176     ret = strncpy_s(outputStr, priLen, contextBuffer + PCID_MAIN_BYTES, priLen - 1);
177     if (ret != 0) {
178         PRINT_ERR("strcpy_s failed.");
179         FreeContextBuffer(contextBuffer);
180         free(outputStr);
181         *outputLen = 0;
182         return false;
183     }
184 
185     FreeContextBuffer(contextBuffer);
186     *outputLen = (int)strlen(outputStr);
187     *output = outputStr;
188     return true;
189 }
190 
DecodeOsSyscap(const char input[PCID_MAIN_BYTES],char (** output)[SINGLE_SYSCAP_LEN],int * outputCnt)191 bool DecodeOsSyscap(const char input[PCID_MAIN_BYTES], char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt)
192 {
193     errno_t nRet = 0;
194     uint16_t indexOfSyscap[CHAR_BIT * OS_SYSCAP_BYTES] = {0};
195     uint16_t countOfSyscap = 0;
196     uint16_t i, j;
197 
198     uint8_t *osSyscap = (uint8_t *)(input + 8); // 8, int[2] of pcid header
199 
200     for (i = 0; i < OS_SYSCAP_BYTES; i++) {
201         for (j = 0; j < CHAR_BIT; j++) {
202             if (osSyscap[i] & (0x01 << j)) {
203                 indexOfSyscap[countOfSyscap++] = i * CHAR_BIT + j;
204             }
205         }
206     }
207 
208     *outputCnt = countOfSyscap;
209     char (*strSyscap)[SINGLE_SYSCAP_LEN] = NULL;
210     strSyscap = (char (*)[SINGLE_SYSCAP_LEN])malloc(countOfSyscap * SINGLE_SYSCAP_LEN);
211     if (strSyscap == NULL) {
212         PRINT_ERR("malloc failed.");
213         *outputCnt = 0;
214         return false;
215     }
216     (void)memset_s(strSyscap, countOfSyscap * SINGLE_SYSCAP_LEN, \
217                    0, countOfSyscap * SINGLE_SYSCAP_LEN);
218     *output = strSyscap;
219 
220     for (i = 0; i < countOfSyscap; i++) {
221         for (j = 0; j < sizeof(g_arraySyscap) / sizeof(SyscapWithNum); j++) {
222             if (g_arraySyscap[j].num == indexOfSyscap[i]) {
223                 nRet = strcpy_s(*strSyscap, SINGLE_SYSCAP_LEN, g_arraySyscap[j].str);
224                 if (nRet != EOK) {
225                     printf("strcpy_s failed. error = %d\n", nRet);
226                     *outputCnt = 0;
227                     free(strSyscap);
228                     strSyscap = NULL;
229                     return false;
230                 }
231                 strSyscap++;
232                 break;
233             }
234         }
235     }
236 
237     return true;
238 }
239 
DecodePrivateSyscap(char * input,char (** output)[SINGLE_SYSCAP_LEN],int * outputCnt)240 bool DecodePrivateSyscap(char *input, char (**output)[SINGLE_SYSCAP_LEN], int *outputCnt)
241 {
242     char (*outputArray)[SINGLE_SYSCAP_LEN] = NULL;
243     char *inputPos = input;
244     int bufferLen, ret;
245     int syscapCnt = 0;
246 
247     if (input == NULL) {
248         *output = outputArray;
249         *outputCnt = syscapCnt;
250         return false;
251     }
252 
253     while (*inputPos != '\0') {
254         if (*inputPos == ',') {
255             syscapCnt++;
256         }
257         inputPos++;
258     }
259     if (syscapCnt == 0) {
260         *output = outputArray;
261         *outputCnt = syscapCnt;
262         return true;
263     }
264 
265     inputPos = input;
266     bufferLen = SINGLE_SYSCAP_LEN * syscapCnt;
267     outputArray = (char (*)[SINGLE_SYSCAP_LEN])malloc(bufferLen);
268     if (outputArray == NULL) {
269         PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", bufferLen, errno);
270         *outputCnt = 0;
271         return false;
272     }
273     (void)memset_s(outputArray, bufferLen, 0, bufferLen);
274 
275     *output = outputArray;
276     char buffer[SINGLE_FEAT_LEN] = {0};
277     char *bufferPos = buffer;
278     while (*inputPos != '\0') {
279         if (*inputPos == ',') {
280             *bufferPos = '\0';
281             ret = sprintf_s(*outputArray, SINGLE_SYSCAP_LEN, "SystemCapability.%s", buffer);
282             if (ret == -1) {
283                 PRINT_ERR("sprintf_s failed\n");
284                 *outputCnt = 0;
285                 free(outputArray);
286                 outputArray = NULL;
287                 return false;
288             }
289             bufferPos = buffer;
290             outputArray++;
291             inputPos++;
292             continue;
293         }
294         *bufferPos++ = *inputPos++;
295     }
296 
297     *outputCnt = syscapCnt;
298     return true;
299 }
300 
SetOsSysCapBitMap(uint8_t * out,uint16_t outLen,const uint16_t * index,uint16_t indexLen)301 static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, const uint16_t *index, uint16_t indexLen)
302 {
303     uint16_t sector, pos;
304 
305     if (outLen != OS_SYSCAP_BYTES) {
306         PRINT_ERR("Input array error.\n");
307         return -1;
308     }
309 
310     for (uint16_t i = 0; i < indexLen; i++) {
311         sector = index[i] / UINT8_BIT;
312         pos = index[i] % UINT8_BIT;
313         if (sector >= OS_SYSCAP_BYTES) {
314             PRINT_ERR("Syscap num(%u) out of range(120).\n", sector);
315             return -1;
316         }
317         out[sector] |=  (1 << pos);
318     }
319     return 0;
320 }
321 
CreateWholeSyscapJsonObj(void)322 static cJSON *CreateWholeSyscapJsonObj(void)
323 {
324     size_t numOfSyscapAll = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
325     cJSON *root =  cJSON_CreateObject();
326     for (size_t i = 0; i < numOfSyscapAll; i++) {
327         cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num));
328     }
329     return root;
330 }
331 
ParseRpcidToJson(char * input,uint32_t inputLen,cJSON * rpcidJson)332 static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson)
333 {
334     uint32_t i;
335     int32_t ret = 0;
336     uint16_t sysCapLength = NtohsInter(*(uint16_t *)(input + sizeof(uint32_t)));
337     uint16_t sysCapCount = sysCapLength / SINGLE_FEAT_LEN;
338     char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t);
339     RPCIDHead *rpcidHeader = (RPCIDHead *)input;
340     cJSON *sysCapJson = cJSON_CreateArray();
341     for (i = 0; i < sysCapCount; i++) {
342         char *temp = sysCapBegin + i * SINGLE_FEAT_LEN;
343         if (strlen(temp) >= SINGLE_FEAT_LEN) {
344             PRINT_ERR("Get SysCap failed, string length too long.\n");
345             ret = -1;
346             goto FREE_SYSCAP_OUT;
347         }
348         char buffer[SINGLE_SYSCAP_LEN] = "SystemCapability.";
349 
350         ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LEN);
351         if (ret != EOK) {
352             PRINT_ERR("strncat_s failed.\n");
353             goto FREE_SYSCAP_OUT;
354         }
355 
356         if (!cJSON_AddItemToArray(sysCapJson, cJSON_CreateString(buffer))) {
357             PRINT_ERR("Add syscap string to json failed.\n");
358             ret = -1;
359             goto FREE_SYSCAP_OUT;
360         }
361     }
362 
363     if (!cJSON_AddNumberToObject(rpcidJson, "api_version", NtohsInter(rpcidHeader->apiVersion))) {
364         PRINT_ERR("Add api_version to json failed.\n");
365         ret = -1;
366         goto FREE_SYSCAP_OUT;
367     }
368     if (!cJSON_AddItemToObject(rpcidJson, "syscap", sysCapJson)) {
369         PRINT_ERR("Add syscap to json failed.\n");
370         ret = -1;
371         goto FREE_SYSCAP_OUT;
372     }
373 
374     return 0;
375 FREE_SYSCAP_OUT:
376     cJSON_Delete(sysCapJson);
377     return ret;
378 }
379 
CheckRpcidFormat(const char * inputFile,char ** buffer,uint32_t * len)380 static int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len)
381 {
382     uint32_t bufferLen;
383     uint16_t sysCaptype, sysCapLength;
384     char *contextBuffer = NULL;
385     RPCIDHead *rpcidHeader = NULL;
386 
387     if (GetFileContext(inputFile, &contextBuffer, &bufferLen)) {
388         PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
389         return -1;
390     }
391     if (bufferLen < (2 * sizeof(uint32_t))) { // 2, header of rpcid.sc
392         PRINT_ERR("Parse file failed(format is invalid), input file : %s\n", inputFile);
393         return -1;
394     }
395     rpcidHeader = (RPCIDHead *)contextBuffer;
396     if (rpcidHeader->apiVersionType != 1) {
397         PRINT_ERR("Parse file failed(apiVersionType != 1), input file : %s\n", inputFile);
398         return -1;
399     }
400     sysCaptype = NtohsInter(*(uint16_t *)(rpcidHeader + 1));
401     if (sysCaptype != 2) { // 2, app syscap type
402         PRINT_ERR("Parse file failed(sysCaptype != 2), input file : %s\n", inputFile);
403         return -1;
404     }
405     sysCapLength = NtohsInter(*(uint16_t *)((char *)(rpcidHeader + 1) + sizeof(uint16_t)));
406     if (bufferLen < sizeof(RPCIDHead) + sizeof(uint32_t) + sysCapLength) {
407         PRINT_ERR("Parse file failed(SysCap length exceeded), input file : %s\n", inputFile);
408         return -1;
409     }
410 
411     *buffer = contextBuffer;
412     *len = bufferLen;
413     return 0;
414 }
415 
DecodeRpcidToStringFormat(const char * inputFile)416 char *DecodeRpcidToStringFormat(const char *inputFile)
417 {
418     int32_t ret = 0;
419     int32_t sysCapArraySize;
420     uint32_t bufferLen, i;
421     uint16_t indexOs = 0;
422     uint16_t indexPri = 0;
423     uint16_t *osSysCapIndex;
424     char *contextBuffer = NULL;
425     char *priSyscapArray = NULL;
426     char *priSyscap = NULL;
427     char *outBuffer = NULL;
428     cJSON *cJsonTemp = NULL;
429     cJSON *rpcidRoot = NULL;
430     cJSON *sysCapDefine = NULL;
431     cJSON *sysCapArray = NULL;
432 
433     // check rpcid.sc
434     if (CheckRpcidFormat(inputFile, &contextBuffer, &bufferLen)) {
435         PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile);
436         goto FREE_CONTEXT_OUT;
437     }
438 
439     // parse rpcid to json
440     rpcidRoot = cJSON_CreateObject();
441     if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot)) {
442         PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile);
443         goto FREE_RPCID_ROOT;
444     }
445 
446     // trans to string format
447     sysCapDefine =  CreateWholeSyscapJsonObj();
448     sysCapArray = cJSON_GetObjectItem(rpcidRoot, "syscap");
449     if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) {
450         PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile);
451         goto FREE_WHOLE_SYSCAP;
452     }
453     sysCapArraySize = cJSON_GetArraySize(sysCapArray);
454     if (sysCapArraySize < 0) {
455         PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile);
456         goto FREE_WHOLE_SYSCAP;
457     }
458     // malloc for save os syscap index
459     osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) * sysCapArraySize);
460     if (osSysCapIndex == NULL) {
461         PRINT_ERR("malloc failed.\n");
462         goto FREE_WHOLE_SYSCAP;
463     }
464     (void)memset_s(osSysCapIndex, sizeof(uint16_t) * sysCapArraySize,
465                    0, sizeof(uint16_t) * sysCapArraySize);
466     // malloc for save private syscap string
467     priSyscapArray = (char *)malloc(sysCapArraySize * SINGLE_SYSCAP_LEN);
468     if (priSyscapArray == NULL) {
469         PRINT_ERR("malloc(%u) failed.\n", (uint32_t)sysCapArraySize * SINGLE_SYSCAP_LEN);
470         goto FREE_MALLOC_OSSYSCAP;
471     }
472     (void)memset_s(priSyscapArray, sysCapArraySize * SINGLE_SYSCAP_LEN,
473                    0, sysCapArraySize * SINGLE_SYSCAP_LEN);
474     priSyscap = priSyscapArray;
475     // part os syscap and ptivate syscap
476     for (i = 0; i < (uint32_t)sysCapArraySize; i++) {
477         cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i);
478         cJsonTemp = cJSON_GetObjectItem(sysCapDefine, cJsonItem->valuestring);
479         if (cJsonTemp != NULL) {
480             osSysCapIndex[indexOs++] = (uint16_t)(cJsonTemp->valueint);
481         } else {
482             ret = strcpy_s(priSyscap, SINGLE_SYSCAP_LEN, cJsonItem->valuestring);
483             if (ret != EOK) {
484                 PRINT_ERR("strcpy_s failed.\n");
485                 goto FREE_MALLOC_PRISYSCAP;
486             }
487             priSyscapArray += SINGLE_SYSCAP_LEN;
488             indexPri++;
489         }
490     }
491     uint32_t outUint[RPCID_OUT_BUFFER] = {0};
492     outUint[0] = *(uint32_t *)contextBuffer;
493     outUint[1] = *(uint32_t *)(contextBuffer + sizeof(uint32_t));
494     uint8_t *osOutUint = (uint8_t *)(outUint + 2);
495     if (SetOsSysCapBitMap(osOutUint, 120, osSysCapIndex, indexOs)) {  // 120, len of osOutUint
496         PRINT_ERR("Set os syscap bit map failed.\n");
497         goto FREE_MALLOC_PRISYSCAP;
498     }
499 
500     uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER +
501                             SINGLE_SYSCAP_LEN * indexPri;
502     outBuffer = (char *)malloc(outBufferLen);
503     if (outBuffer == NULL) {
504         PRINT_ERR("malloc(%u) failed.\n", outBufferLen);
505         goto FREE_MALLOC_PRISYSCAP;
506     }
507     (void)memset_s(outBuffer, outBufferLen, 0, outBufferLen);
508 
509     ret = sprintf_s(outBuffer, outBufferLen, "%u", outUint[0]);
510     if (ret == -1) {
511         PRINT_ERR("sprintf_s failed.\n");
512         outBuffer = NULL;
513         goto FREE_MALLOC_PRISYSCAP;
514     }
515     for (i = 1; i < RPCID_OUT_BUFFER; i++) {
516         ret = sprintf_s(outBuffer, outBufferLen, "%s,%u", outBuffer, outUint[i]);
517         if (ret == -1) {
518             PRINT_ERR("sprintf_s failed.\n");
519             outBuffer = NULL;
520             goto FREE_MALLOC_PRISYSCAP;
521         }
522     }
523 
524     for (i = 0; i < indexPri; i++) {
525         ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer,
526                         priSyscapArray + i * SINGLE_SYSCAP_LEN);
527         if (ret == -1) {
528             PRINT_ERR("sprintf_s failed.\n");
529             outBuffer = NULL;
530             goto FREE_MALLOC_PRISYSCAP;
531         }
532     }
533 
534 FREE_MALLOC_PRISYSCAP:
535     free(priSyscap);
536 FREE_MALLOC_OSSYSCAP:
537     free(osSysCapIndex);
538 FREE_WHOLE_SYSCAP:
539     cJSON_Delete(sysCapDefine);
540 FREE_RPCID_ROOT:
541     cJSON_Delete(rpcidRoot);
542 FREE_CONTEXT_OUT:
543     FreeContextBuffer(contextBuffer);
544     return outBuffer;
545 }
546 
ComparePcidString(const char * pcidString,const char * rpcidString,CompareError * result)547 int32_t ComparePcidString(const char *pcidString, const char *rpcidString, CompareError *result)
548 {
549     int32_t ret;
550     uint16_t versionFlag = 0;
551     uint16_t ossyscapFlag = 0;
552     uint16_t prisyscapFlag = 0;
553     char *pcidPriSyscap = NULL;
554     char *rpcidPriSyscap = NULL;
555     bool priSysFound;
556     uint32_t pcidPriSyscapLen, rpcidPriSyscapLen;
557     uint32_t i, j, temp1, temp2;
558     uint32_t retFlag = 0;
559     uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0};
560     uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0};
561 
562     ret =  SeparateSyscapFromString(pcidString, pcidOsAarry, PCID_OUT_BUFFER,
563                                     &pcidPriSyscap, &pcidPriSyscapLen);
564     ret += SeparateSyscapFromString(rpcidString, rpcidOsAarry, RPCID_OUT_BUFFER,
565                                     &rpcidPriSyscap, &rpcidPriSyscapLen);
566     if (ret != 0) {
567         PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret);
568         return -1;
569     }
570     result->missSyscapNum = 0;
571     // compare version
572     uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion);
573     uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion);
574     if (pcidVersion < rpcidVersion) {
575         result->targetApiVersion = rpcidVersion;
576         versionFlag = 1;
577     }
578     // compare os sysscap
579     for (i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid
580         temp1 = pcidOsAarry[i] ^ rpcidOsAarry[i];
581         temp2 = temp1 & rpcidOsAarry[i];
582         if (!temp2) {
583             continue;
584         }
585         for (uint8_t k = 0; k < INT_BIT; k++) {
586             if (temp2 & (1U << k)) {
587                 char *temp = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN);
588                 if (temp == NULL) {
589                     PRINT_ERR("malloc failed.\n");
590                     FreeCompareError(result);
591                     return -1;
592                 }
593                 ret = strcpy_s(temp, sizeof(char) * SINGLE_SYSCAP_LEN,
594                                g_arraySyscap[(i - 2) * INT_BIT + k].str); // 2, header of pcid & rpcid
595                 if (ret != EOK) {
596                     PRINT_ERR("strcpy_s failed.\n");
597                     FreeCompareError(result);
598                     return -1;
599                 }
600                 result->syscap[ossyscapFlag++] = temp;
601             }
602         }
603     }
604     // compare pri syscap
605     priSysFound = false;
606     for (i = 0; i < rpcidPriSyscapLen; i++) {
607         for (j = 0; j < pcidPriSyscapLen; j++) {
608             if (strcmp(rpcidPriSyscap + SINGLE_SYSCAP_LEN * i,
609                        pcidPriSyscap + SINGLE_SYSCAP_LEN * j) == 0) {
610                 priSysFound = true;
611                 break;
612             }
613         }
614         if (priSysFound != true) {
615             char *temp = (char *)malloc(sizeof(char) * SINGLE_SYSCAP_LEN);
616             if (temp == NULL) {
617                 PRINT_ERR("malloc failed.\n");
618                 FreeCompareError(result);
619                 return -1;
620             }
621             ret = strcpy_s(temp, sizeof(char) * SINGLE_SYSCAP_LEN,
622                            rpcidPriSyscap + SINGLE_SYSCAP_LEN * i);
623             if (ret != EOK) {
624                 FreeCompareError(result);
625                 PRINT_ERR("strcpy_s failed.\n");
626                 return -1;
627             }
628             result->syscap[ossyscapFlag + prisyscapFlag] = temp;
629             ++prisyscapFlag;
630         }
631         priSysFound = false;
632     }
633 
634     if (versionFlag > 0) {
635         retFlag |= 1U << 0;
636     }
637     if (ossyscapFlag > 0 || prisyscapFlag > 0) {
638         retFlag |= 1U << 1;
639         result->missSyscapNum = ossyscapFlag + prisyscapFlag;
640     }
641     return (int32_t)retFlag;
642 }
643 
FreeCompareError(CompareError * result)644 int32_t FreeCompareError(CompareError *result)
645 {
646     if (result == NULL) {
647         return 0;
648     }
649     for (int i = 0; i < result->missSyscapNum; i++) {
650         free(result->syscap[i]);
651         result->syscap[i] = NULL;
652     }
653     result->missSyscapNum = 0;
654     result->targetApiVersion = 0;
655     return 0;
656 }