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