mirror of
https://github.com/openharmony/developtools_syscap_codec.git
synced 2026-08-27 02:11:19 -04:00
add decode rpcid.sc to string format.
add compare pcid with rpcid string. Signed-off-by: yudechen <chenyude@huawei.com> Change-Id: Ia030b8e25a03d042c1180f9942a6e36c6798e11c
This commit is contained in:
@@ -99,9 +99,15 @@ config("syscap_interface_public_config") {
|
||||
|
||||
if (defined(ohos_lite)) {
|
||||
shared_library("syscap_interface_shared") {
|
||||
include_dirs = [ "include" ]
|
||||
include_dirs = [
|
||||
"include",
|
||||
"src",
|
||||
]
|
||||
public_configs = [ ":syscap_interface_public_config" ]
|
||||
sources = [ "./interfaces/inner_api/syscap_interface.c" ]
|
||||
sources = [
|
||||
"./interfaces/inner_api/syscap_interface.c",
|
||||
"./src/endian_internal.c",
|
||||
]
|
||||
|
||||
deps = [
|
||||
"//build/lite/config/component/cJSON:cjson_static",
|
||||
@@ -110,10 +116,15 @@ if (defined(ohos_lite)) {
|
||||
}
|
||||
} else {
|
||||
ohos_shared_library("syscap_interface_shared") {
|
||||
include_dirs = [ "include" ]
|
||||
include_dirs = [
|
||||
"include",
|
||||
"src",
|
||||
]
|
||||
public_configs = [ ":syscap_interface_public_config" ]
|
||||
sources = [ "./interfaces/inner_api/syscap_interface.c" ]
|
||||
|
||||
sources = [
|
||||
"./interfaces/inner_api/syscap_interface.c",
|
||||
"./src/endian_internal.c",
|
||||
]
|
||||
deps = [
|
||||
"//third_party/bounds_checking_function:libsec_static",
|
||||
"//third_party/cJSON:cjson_static",
|
||||
|
||||
@@ -22,39 +22,75 @@
|
||||
#include <sys/stat.h>
|
||||
#include <securec.h>
|
||||
#include <limits.h>
|
||||
#include "cJSON.h"
|
||||
#include "syscap_define.h"
|
||||
#include "endian_internal.h"
|
||||
#include "syscap_interface.h"
|
||||
|
||||
#define OS_SYSCAP_BYTES 120
|
||||
#define PCID_MAIN_BYTES 128
|
||||
#define SYSCAP_STR_LEN_MAX 128
|
||||
#define SINGLE_FEAT_LENGTH 128
|
||||
#define RPCID_OUT_BUFFER 32
|
||||
#define PCID_OUT_BUFFER RPCID_OUT_BUFFER
|
||||
#define UINT8_BIT 8
|
||||
#define INT_BIT 32
|
||||
#define U32_TO_STR_MAX_LEN 11
|
||||
|
||||
|
||||
#define PRINT_ERR(...) \
|
||||
do { \
|
||||
printf("ERROR: in file %s at line %d -> ", __FILE__, __LINE__); \
|
||||
printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
typedef struct RequiredProductCompatibilityIDHead {
|
||||
uint16_t apiVersion : 15;
|
||||
uint16_t apiVersionType : 1;
|
||||
} RPCIDHead;
|
||||
|
||||
typedef struct ProductCompatibilityID {
|
||||
uint16_t apiVersion : 15;
|
||||
uint16_t apiVersionType : 1;
|
||||
uint16_t systemType : 3;
|
||||
uint16_t reserved : 13;
|
||||
uint32_t manufacturerID;
|
||||
uint8_t osSyscap[OS_SYSCAP_BYTES];
|
||||
} PCIDMain;
|
||||
|
||||
static const char *pcidPath = "/system/etc/PCID.sc";
|
||||
|
||||
static void FreeContextBuffer(char *contextBuffer)
|
||||
{
|
||||
(void)free(contextBuffer);
|
||||
}
|
||||
|
||||
static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen)
|
||||
static int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen)
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
struct stat statBuf;
|
||||
char *contextBuffer = NULL;
|
||||
const char *inputFile = "/system/etc/PCID.sc";
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
ret = stat(inputFile, &statBuf);
|
||||
#ifdef _POSIX_
|
||||
if (strlen(inputFile) > PATH_MAX || strncpy_s(path, PATH_MAX, inputFile, strlen(inputFile)) != EOK) {
|
||||
PRINT_ERR("get path(%s) failed\n", inputFile);
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
if (strlen(inputFile) > PATH_MAX || realpath(inputFile, path) == NULL) {
|
||||
PRINT_ERR("get file(%s) real path failed\n", inputFile);
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = stat(path, &statBuf);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", inputFile, errno);
|
||||
PRINT_ERR("get file(%s) st_mode failed, errno = %d\n", path, errno);
|
||||
return -1;
|
||||
}
|
||||
if (!(statBuf.st_mode & S_IRUSR)) {
|
||||
PRINT_ERR("don't have permission to read the file(%s)\n", inputFile);
|
||||
PRINT_ERR("don't have permission to read the file(%s)\n", path);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer = (char *)malloc(statBuf.st_size + 1);
|
||||
@@ -62,16 +98,15 @@ static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen)
|
||||
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(inputFile, "rb");
|
||||
fp = fopen(path, "rb");
|
||||
if (fp == NULL) {
|
||||
PRINT_ERR("open file(%s) failed, errno = %d\n", inputFile, errno);
|
||||
PRINT_ERR("open file(%s) failed, errno = %d\n", path, errno);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
return -1;
|
||||
}
|
||||
size_t retFread = fread(contextBuffer, statBuf.st_size, 1, fp);
|
||||
if (retFread != 1) {
|
||||
PRINT_ERR("read file(%s) failed, errno = %d\n", inputFile, errno);
|
||||
PRINT_ERR("read file(%s) failed, errno = %d\n", path, errno);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
@@ -96,7 +131,7 @@ bool EncodeOsSyscap(char *output, int len)
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = GetFileContext(&contextBuffer, &bufferLen);
|
||||
ret = GetFileContext(pcidPath, &contextBuffer, &bufferLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("GetFileContext failed, input file : /system/etc/PCID.sc\n");
|
||||
return false;
|
||||
@@ -120,7 +155,7 @@ bool EncodePrivateSyscap(char **output, int *outputLen)
|
||||
char *outputStr = NULL;
|
||||
uint32_t bufferLen;
|
||||
|
||||
ret = GetFileContext(&contextBuffer, &bufferLen);
|
||||
ret = GetFileContext(pcidPath, &contextBuffer, &bufferLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("GetFileContext failed, input file : /system/etc/PCID.sc\n");
|
||||
return false;
|
||||
@@ -167,21 +202,21 @@ bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt)
|
||||
}
|
||||
|
||||
*outputCnt = countOfSyscap;
|
||||
char (*strSyscap)[SYSCAP_STR_LEN_MAX] = NULL;
|
||||
strSyscap = (char (*)[SYSCAP_STR_LEN_MAX])malloc(countOfSyscap * SYSCAP_STR_LEN_MAX);
|
||||
char (*strSyscap)[SINGLE_FEAT_LENGTH] = NULL;
|
||||
strSyscap = (char (*)[SINGLE_FEAT_LENGTH])malloc(countOfSyscap * SINGLE_FEAT_LENGTH);
|
||||
if (strSyscap == NULL) {
|
||||
PRINT_ERR("malloc failed.");
|
||||
*outputCnt = 0;
|
||||
return false;
|
||||
}
|
||||
(void)memset_s(strSyscap, countOfSyscap * SYSCAP_STR_LEN_MAX, \
|
||||
0, countOfSyscap * SYSCAP_STR_LEN_MAX);
|
||||
(void)memset_s(strSyscap, countOfSyscap * SINGLE_FEAT_LENGTH, \
|
||||
0, countOfSyscap * SINGLE_FEAT_LENGTH);
|
||||
*output = strSyscap;
|
||||
|
||||
for (i = 0; i < countOfSyscap; i++) {
|
||||
for (j = 0; j < sizeof(arraySyscap) / sizeof(SyscapWithNum); j++) {
|
||||
if (arraySyscap[j].num == indexOfSyscap[i]) {
|
||||
nRet = strcpy_s(*strSyscap, SYSCAP_STR_LEN_MAX, arraySyscap[j].syscapStr);
|
||||
nRet = strcpy_s(*strSyscap, SINGLE_FEAT_LENGTH, arraySyscap[j].syscapStr);
|
||||
if (nRet != EOK) {
|
||||
printf("strcpy_s failed. error = %d\n", nRet);
|
||||
*outputCnt = 0;
|
||||
@@ -200,7 +235,7 @@ bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt)
|
||||
|
||||
bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt)
|
||||
{
|
||||
char (*outputArray)[SYSCAP_STR_LEN_MAX] = NULL;
|
||||
char (*outputArray)[SINGLE_FEAT_LENGTH] = NULL;
|
||||
char *inputPos = input;
|
||||
int bufferLen, ret;
|
||||
int syscapCnt = 0;
|
||||
@@ -213,8 +248,8 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt)
|
||||
}
|
||||
inputPos = input;
|
||||
|
||||
bufferLen = SYSCAP_STR_LEN_MAX * syscapCnt;
|
||||
outputArray = (char (*)[SYSCAP_STR_LEN_MAX])malloc(bufferLen);
|
||||
bufferLen = SINGLE_FEAT_LENGTH * syscapCnt;
|
||||
outputArray = (char (*)[SINGLE_FEAT_LENGTH])malloc(bufferLen);
|
||||
if (outputArray == NULL) {
|
||||
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", bufferLen, errno);
|
||||
*outputCnt = 0;
|
||||
@@ -223,12 +258,12 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt)
|
||||
(void)memset_s(outputArray, bufferLen, 0, bufferLen);
|
||||
|
||||
*output = outputArray;
|
||||
char buffer[SYSCAP_STR_LEN_MAX - 17] = {0}; // 17. size of "SystemCapability."
|
||||
char buffer[SINGLE_FEAT_LENGTH - 17] = {0}; // 17. size of "SystemCapability."
|
||||
char *bufferPos = buffer;
|
||||
while (*inputPos != '\0') {
|
||||
if (*inputPos == ',') {
|
||||
*bufferPos = '\0';
|
||||
ret = sprintf_s(*outputArray, SYSCAP_STR_LEN_MAX, "SystemCapability.%s", buffer);
|
||||
ret = sprintf_s(*outputArray, SINGLE_FEAT_LENGTH, "SystemCapability.%s", buffer);
|
||||
if (ret == -1) {
|
||||
PRINT_ERR("sprintf_s failed\n");
|
||||
*outputCnt = 0;
|
||||
@@ -246,4 +281,381 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt)
|
||||
|
||||
*outputCnt = syscapCnt;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, uint16_t *index, uint16_t indexLen)
|
||||
{
|
||||
uint16_t sector, pos;
|
||||
|
||||
if (outLen != OS_SYSCAP_BYTES) {
|
||||
PRINT_ERR("Input array error.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
for (uint16_t i = 0; i < indexLen; i++) {
|
||||
sector = index[i] / UINT8_BIT;
|
||||
pos = index[i] % UINT8_BIT;
|
||||
if (sector >= OS_SYSCAP_BYTES) {
|
||||
PRINT_ERR("Syscap num(%u) out of range(120).\n", sector);
|
||||
return -1;
|
||||
}
|
||||
out[sector] |= (1 << pos);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static cJSON *CreateWholeSyscapJsonObj(void)
|
||||
{
|
||||
size_t numOfSyscapAll = sizeof(arraySyscap) / sizeof(SyscapWithNum);
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
for (size_t i = 0; i < numOfSyscapAll; i++) {
|
||||
cJSON_AddItemToObject(root, arraySyscap[i].syscapStr, cJSON_CreateNumber(arraySyscap[i].num));
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson)
|
||||
{
|
||||
uint32_t i;
|
||||
int32_t ret = 0;
|
||||
uint16_t sysCapLength = NtohsInter(*(uint16_t *)(input + sizeof(uint32_t)));
|
||||
uint16_t sysCapCount = sysCapLength / SINGLE_FEAT_LENGTH;
|
||||
char *sysCapBegin = input + sizeof(RPCIDHead) + sizeof(uint32_t);
|
||||
RPCIDHead *rpcidHeader = (RPCIDHead *)input;
|
||||
cJSON *sysCapJson = cJSON_CreateArray();
|
||||
for (i = 0; i < sysCapCount; i++) {
|
||||
char *temp = sysCapBegin + i * SINGLE_FEAT_LENGTH;
|
||||
if (strlen(temp) >= SINGLE_FEAT_LENGTH) {
|
||||
PRINT_ERR("Get SysCap failed, string length too long.\n");
|
||||
ret = -1;
|
||||
goto FREE_SYSCAP_OUT;
|
||||
}
|
||||
char buffer[SINGLE_FEAT_LENGTH + 17] = "SystemCapability."; // 17, sizeof "SystemCapability."
|
||||
|
||||
ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LENGTH);
|
||||
if (ret != EOK) {
|
||||
PRINT_ERR("strncat_s failed.\n");
|
||||
goto FREE_SYSCAP_OUT;
|
||||
}
|
||||
|
||||
if (!cJSON_AddItemToArray(sysCapJson, cJSON_CreateString(buffer))) {
|
||||
PRINT_ERR("Add syscap string to json failed.\n");
|
||||
ret = -1;
|
||||
goto FREE_SYSCAP_OUT;
|
||||
}
|
||||
}
|
||||
|
||||
if (!cJSON_AddNumberToObject(rpcidJson, "api_version", NtohsInter(rpcidHeader->apiVersion))) {
|
||||
PRINT_ERR("Add api_version to json failed.\n");
|
||||
ret = -1;
|
||||
goto FREE_SYSCAP_OUT;
|
||||
}
|
||||
if (!cJSON_AddItemToObject(rpcidJson, "syscap", sysCapJson)) {
|
||||
PRINT_ERR("Add syscap to json failed.\n");
|
||||
ret = -1;
|
||||
goto FREE_SYSCAP_OUT;
|
||||
}
|
||||
|
||||
return 0;
|
||||
FREE_SYSCAP_OUT:
|
||||
cJSON_Delete(sysCapJson);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int32_t CheckRpcidFormat(char *inputFile, char **Buffer, uint32_t *Len)
|
||||
{
|
||||
uint32_t bufferLen;
|
||||
uint16_t sysCaptype, sysCapLength;
|
||||
char *contextBuffer = NULL;
|
||||
RPCIDHead *rpcidHeader = NULL;
|
||||
|
||||
if (GetFileContext(inputFile, &contextBuffer, &bufferLen)) {
|
||||
PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
|
||||
return -1;
|
||||
}
|
||||
if (bufferLen < (2 * sizeof(uint32_t))) { // 2, header of rpcid.sc
|
||||
PRINT_ERR("Parse file failed(format is invalid), input file : %s\n", inputFile);
|
||||
return -1;
|
||||
}
|
||||
rpcidHeader = (RPCIDHead *)contextBuffer;
|
||||
if (rpcidHeader->apiVersionType != 1) {
|
||||
PRINT_ERR("Parse file failed(apiVersionType != 1), input file : %s\n", inputFile);
|
||||
return -1;
|
||||
}
|
||||
sysCaptype = NtohsInter(*(uint16_t *)(rpcidHeader + 1));
|
||||
if (sysCaptype != 2) { // 2, app syscap type
|
||||
PRINT_ERR("Parse file failed(sysCaptype != 2), input file : %s\n", inputFile);
|
||||
return -1;
|
||||
}
|
||||
sysCapLength = NtohsInter(*(uint16_t *)((char *)(rpcidHeader + 1) + sizeof(uint16_t)));
|
||||
if (bufferLen < sizeof(RPCIDHead) + sizeof(uint32_t) + sysCapLength) {
|
||||
PRINT_ERR("Parse file failed(SysCap length exceeded), input file : %s\n", inputFile);
|
||||
return -1;
|
||||
}
|
||||
|
||||
*Buffer = contextBuffer;
|
||||
*Len = bufferLen;
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *DecodeRpcidToStringFormat(char *inputFile)
|
||||
{
|
||||
int32_t ret = 0;
|
||||
int32_t sysCapArraySize;
|
||||
uint32_t bufferLen, i;
|
||||
uint16_t indexOs = 0;
|
||||
uint16_t indexPri = 0;
|
||||
uint16_t *osSysCapIndex;
|
||||
char *contextBuffer = NULL;
|
||||
char *priSyscapArray = NULL;
|
||||
char *priSyscap = NULL;
|
||||
char *outBuffer = NULL;
|
||||
cJSON *cJsonTemp = NULL;
|
||||
cJSON *rpcidRoot = NULL;
|
||||
cJSON *sysCapDefine = NULL;
|
||||
cJSON *sysCapArray = NULL;
|
||||
|
||||
// check rpcid.sc
|
||||
if (CheckRpcidFormat(inputFile, &contextBuffer, &bufferLen)) {
|
||||
PRINT_ERR("Check rpcid.sc format failed. Input file: %s\n", inputFile);
|
||||
goto FREE_CONTEXT_OUT;
|
||||
}
|
||||
|
||||
// parse rpcid to json
|
||||
rpcidRoot = cJSON_CreateObject();
|
||||
if (ParseRpcidToJson(contextBuffer, bufferLen, rpcidRoot)) {
|
||||
PRINT_ERR("Prase rpcid to json failed. Input file: %s\n", inputFile);
|
||||
goto FREE_RPCID_ROOT;
|
||||
}
|
||||
|
||||
// trans to string format
|
||||
sysCapDefine = CreateWholeSyscapJsonObj();
|
||||
sysCapArray = cJSON_GetObjectItem(rpcidRoot, "syscap");
|
||||
if (sysCapArray == NULL || !cJSON_IsArray(sysCapArray)) {
|
||||
PRINT_ERR("Get syscap failed. Input file: %s\n", inputFile);
|
||||
goto FREE_WHOLE_SYSCAP;
|
||||
}
|
||||
sysCapArraySize = cJSON_GetArraySize(sysCapArray);
|
||||
if (sysCapArraySize < 0) {
|
||||
PRINT_ERR("Get syscap size failed. Input file: %s\n", inputFile);
|
||||
goto FREE_WHOLE_SYSCAP;
|
||||
}
|
||||
// malloc for save os syscap index
|
||||
osSysCapIndex = (uint16_t *)malloc(sizeof(uint16_t) * sysCapArraySize);
|
||||
if (osSysCapIndex == NULL) {
|
||||
PRINT_ERR("malloc failed.\n");
|
||||
goto FREE_WHOLE_SYSCAP;
|
||||
}
|
||||
(void)memset_s(osSysCapIndex, sizeof(uint16_t) * sysCapArraySize,
|
||||
0, sizeof(uint16_t) * sysCapArraySize);
|
||||
// malloc for save private syscap string
|
||||
priSyscapArray = (char *)malloc(sysCapArraySize * SINGLE_FEAT_LENGTH);
|
||||
if (priSyscapArray == NULL) {
|
||||
PRINT_ERR("malloc(%u) failed.\n", sysCapArraySize * SINGLE_FEAT_LENGTH);
|
||||
goto FREE_MALLOC_OSSYSCAP;
|
||||
}
|
||||
(void)memset_s(priSyscapArray, sysCapArraySize * SINGLE_FEAT_LENGTH,
|
||||
0, sysCapArraySize * SINGLE_FEAT_LENGTH);
|
||||
priSyscap = priSyscapArray;
|
||||
// part os syscap and ptivate syscap
|
||||
for (i = 0; i < (uint32_t)sysCapArraySize; i++) {
|
||||
cJSON *cJsonItem = cJSON_GetArrayItem(sysCapArray, i);
|
||||
cJsonTemp = cJSON_GetObjectItem(sysCapDefine, cJsonItem->valuestring);
|
||||
if (cJsonTemp != NULL) {
|
||||
osSysCapIndex[indexOs++] = cJsonTemp->valueint;
|
||||
} else {
|
||||
ret = strncpy_s(priSyscapArray, sysCapArraySize * SINGLE_FEAT_LENGTH,
|
||||
cJsonItem->valuestring, SINGLE_FEAT_LENGTH - 1);
|
||||
if (ret != EOK) {
|
||||
PRINT_ERR("strcpy_s failed.\n");
|
||||
goto FREE_MALLOC_PRISYSCAP;
|
||||
}
|
||||
priSyscapArray += SINGLE_FEAT_LENGTH;
|
||||
indexPri++;
|
||||
}
|
||||
}
|
||||
uint32_t outUint[RPCID_OUT_BUFFER] = {0};
|
||||
outUint[0] = *(uint32_t *)contextBuffer;
|
||||
outUint[1] = *(uint32_t *)(contextBuffer + sizeof(uint32_t));
|
||||
uint8_t *osOutUint = (uint8_t *)(outUint + 2);
|
||||
if (SetOsSysCapBitMap(osOutUint, 120, osSysCapIndex, indexOs)) { // 120, len of osOutUint
|
||||
PRINT_ERR("Set os syscap bit map failed.\n");
|
||||
goto FREE_MALLOC_PRISYSCAP;
|
||||
}
|
||||
|
||||
uint16_t outBufferLen = U32_TO_STR_MAX_LEN * RPCID_OUT_BUFFER
|
||||
+ (SINGLE_FEAT_LENGTH + 1) * sysCapArraySize;
|
||||
outBuffer = (char *)malloc(outBufferLen);
|
||||
if (outBuffer == NULL) {
|
||||
PRINT_ERR("malloc(%u) failed.\n", outBufferLen);
|
||||
goto FREE_MALLOC_PRISYSCAP;
|
||||
}
|
||||
(void)memset_s(outBuffer, outBufferLen, 0, outBufferLen);
|
||||
|
||||
ret = sprintf_s(outBuffer, outBufferLen, "%u", outUint[0]);
|
||||
if (ret == -1) {
|
||||
PRINT_ERR("sprintf_s failed.\n");
|
||||
outBuffer = NULL;
|
||||
goto FREE_MALLOC_PRISYSCAP;
|
||||
}
|
||||
for (i = 1; i < RPCID_OUT_BUFFER; i++) {
|
||||
ret = sprintf_s(outBuffer, outBufferLen, "%s,%u", outBuffer, outUint[i]);
|
||||
if (ret == -1) {
|
||||
PRINT_ERR("sprintf_s failed.\n");
|
||||
outBuffer = NULL;
|
||||
goto FREE_MALLOC_PRISYSCAP;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < indexPri; i++) {
|
||||
ret = sprintf_s(outBuffer, outBufferLen, "%s,%s", outBuffer, priSyscap + i * SINGLE_FEAT_LENGTH);
|
||||
if (ret == -1) {
|
||||
PRINT_ERR("sprintf_s failed.\n");
|
||||
outBuffer = NULL;
|
||||
goto FREE_MALLOC_PRISYSCAP;
|
||||
}
|
||||
}
|
||||
|
||||
FREE_MALLOC_PRISYSCAP:
|
||||
free(priSyscap);
|
||||
FREE_MALLOC_OSSYSCAP:
|
||||
free(osSysCapIndex);
|
||||
FREE_WHOLE_SYSCAP:
|
||||
cJSON_Delete(sysCapDefine);
|
||||
FREE_RPCID_ROOT:
|
||||
cJSON_Delete(rpcidRoot);
|
||||
FREE_CONTEXT_OUT:
|
||||
FreeContextBuffer(contextBuffer);
|
||||
return outBuffer;
|
||||
}
|
||||
|
||||
static int32_t SeparateSyscapFromString(char *input, uint32_t *osArray, uint32_t osArraySize,
|
||||
char **priSyscap, uint32_t *priSyscapLen)
|
||||
{
|
||||
int32_t ret = 0;
|
||||
uint32_t i, inputLen;
|
||||
uint32_t count = 0;
|
||||
char *temp = NULL;
|
||||
char *tok = NULL;
|
||||
char *private = NULL;
|
||||
|
||||
if (osArraySize != PCID_OUT_BUFFER) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
inputLen = strlen(input);
|
||||
for (i = 0; i < PCID_OUT_BUFFER; i++) {
|
||||
ret = sscanf_s(input, "%u,%s", &osArray[i], input, inputLen);
|
||||
if (ret == -1) {
|
||||
PRINT_ERR("sscanf_s failed.\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// count private syscap
|
||||
if (*input == '\0') {
|
||||
*priSyscap = 0;
|
||||
*priSyscapLen = 0;
|
||||
goto SKIP_PRI_SYSCAP;
|
||||
}
|
||||
for (i = 0; *(input + i) != '\0'; i++) {
|
||||
if (*(input + i) == ',') {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
count++;
|
||||
// get private syscap string
|
||||
char *priSysCapOut = (char *)malloc(SINGLE_FEAT_LENGTH * count);
|
||||
if (priSysCapOut == NULL) {
|
||||
PRINT_ERR("sscanf_s failed.\n");
|
||||
return -1;
|
||||
}
|
||||
(void)memset_s(priSysCapOut, SINGLE_FEAT_LENGTH * count, 0, SINGLE_FEAT_LENGTH * count);
|
||||
private = priSysCapOut;
|
||||
|
||||
temp = strtok_r(input, ",", &tok);
|
||||
while (temp) {
|
||||
ret = strncpy_s(priSysCapOut, SINGLE_FEAT_LENGTH * count,
|
||||
temp, SINGLE_FEAT_LENGTH - 1);
|
||||
if (ret != EOK) {
|
||||
PRINT_ERR("strncpy_s failed.\n");
|
||||
free(priSysCapOut);
|
||||
return -1;
|
||||
}
|
||||
temp = strtok_r(NULL, ",", &tok);
|
||||
priSysCapOut += SINGLE_FEAT_LENGTH;
|
||||
}
|
||||
|
||||
*priSyscap = private;
|
||||
*priSyscapLen = count;
|
||||
|
||||
SKIP_PRI_SYSCAP:
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool ComparePcidString(char *pcidString, char *rpcidString)
|
||||
{
|
||||
int32_t ret;
|
||||
int32_t versionFlag = 0;
|
||||
int32_t ossyscapFlag = 0;
|
||||
int32_t prisyscapFlag = 0;
|
||||
char *pcidPriSyscap = NULL;
|
||||
char *rpcidPriSyscap = NULL;
|
||||
uint32_t pcidPriSyscapLen, rpcidPriSyscapLen;
|
||||
uint32_t flag, i, j, temp1, temp2;
|
||||
uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0};
|
||||
uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0};
|
||||
|
||||
ret = SeparateSyscapFromString(pcidString, pcidOsAarry, PCID_OUT_BUFFER,
|
||||
&pcidPriSyscap, &pcidPriSyscapLen);
|
||||
ret += SeparateSyscapFromString(rpcidString, rpcidOsAarry, RPCID_OUT_BUFFER,
|
||||
&rpcidPriSyscap, &rpcidPriSyscapLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("Separate syscap from string failed. ret = %d\n", ret);
|
||||
return false;
|
||||
}
|
||||
// compare version
|
||||
uint16_t pcidVersion = NtohsInter(((PCIDMain *)pcidOsAarry)->apiVersion);
|
||||
uint16_t rpcidVersion = NtohsInter(((RPCIDHead *)rpcidOsAarry)->apiVersion);
|
||||
if (pcidVersion < rpcidVersion) {
|
||||
PRINT_ERR("Pcid version(%u) less than rpcid version(%u).\n", pcidVersion, rpcidVersion);
|
||||
versionFlag = 1;
|
||||
}
|
||||
// compare os sysscap
|
||||
for (i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid
|
||||
temp1 = pcidOsAarry[i] ^ rpcidOsAarry[i];
|
||||
temp2 = temp1 & rpcidOsAarry[i];
|
||||
if (!temp2) {
|
||||
continue;
|
||||
}
|
||||
for (uint8_t k = 0; k < INT_BIT; k++) {
|
||||
if (temp2 & (0x1 << k)) {
|
||||
// 2, header of pcid & rpcid
|
||||
printf("Miss: %s\n", arraySyscap[(i - 2) * INT_BIT + k].syscapStr);
|
||||
ossyscapFlag += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
// compare pri syscap
|
||||
flag = 1;
|
||||
for (i = 0; i < pcidPriSyscapLen; i++) {
|
||||
for (j = 0; j < rpcidPriSyscapLen; j++) {
|
||||
if (strcmp(pcidPriSyscap + SINGLE_FEAT_LENGTH * i,
|
||||
rpcidPriSyscap + SINGLE_FEAT_LENGTH * j) == 0) {
|
||||
flag = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag != 0) {
|
||||
printf("Miss: %s\n", pcidPriSyscap + SINGLE_FEAT_LENGTH * i);
|
||||
prisyscapFlag += 1;
|
||||
}
|
||||
flag = 1;
|
||||
}
|
||||
|
||||
if (!versionFlag && !ossyscapFlag && !prisyscapFlag) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -29,6 +29,8 @@ bool EncodeOsSyscap(char *output, int len);
|
||||
bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt);
|
||||
bool EncodePrivateSyscap(char **output, int *outputLen);
|
||||
bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt);
|
||||
char *DecodeRpcidToStringFormat(char *inputFile);
|
||||
bool ComparePcidString(char *pcidString, char *rpcidString);
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
|
||||
@@ -33,6 +33,7 @@ ohos_shared_library("systemcapability") {
|
||||
include_dirs = [
|
||||
"//third_party/node/src",
|
||||
"//foundation/arkui/napi/interfaces/kits",
|
||||
"//developtools/syscap_codec/src",
|
||||
"//developtools/syscap_codec/napi",
|
||||
"//developtools/syscap_codec/interfaces/inner_api",
|
||||
"//developtools/syscap_codec/include",
|
||||
|
||||
+20
-4
@@ -502,8 +502,8 @@ int32_t DecodeRpcidToString(char *inputFile, char *outDirPath)
|
||||
}
|
||||
}
|
||||
uint32_t outUint[RPCID_OUT_BUFFER] = {0};
|
||||
outUint[0] = NtohsInter(((RPCIDHead *)contextBuffer)->apiVersion);
|
||||
outUint[1] = NtohsInter(*(uint16_t *)(contextBuffer + sizeof(uint32_t)));
|
||||
outUint[0] = *(uint32_t *)contextBuffer;
|
||||
outUint[1] = *(uint32_t *)(contextBuffer + sizeof(uint32_t));
|
||||
uint8_t *osOutUint = (uint8_t *)(outUint + 2);
|
||||
if (SetOsSysCapBitMap(osOutUint, 120, osSysCapIndex, indexOs)) { // 120, len of osOutUint
|
||||
PRINT_ERR("Set os syscap bit map failed.\n");
|
||||
@@ -633,7 +633,8 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile)
|
||||
char *rpcidContent = NULL;
|
||||
char *pcidPriSyscap = NULL;
|
||||
char *rpcidPriSyscap = NULL;
|
||||
uint32_t pcidContentLen, rpcidContentLen, pcidPriSyscapLen, rpcidPriSyscapLen, i;
|
||||
uint32_t pcidContentLen, rpcidContentLen, pcidPriSyscapLen, rpcidPriSyscapLen;
|
||||
uint32_t flag, i, j;
|
||||
uint32_t pcidOsAarry[PCID_OUT_BUFFER] = {0};
|
||||
uint32_t rpcidOsAarry[PCID_OUT_BUFFER] = {0};
|
||||
|
||||
@@ -658,7 +659,7 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile)
|
||||
if (pcidVersion < rpcidVersion) {
|
||||
PRINT_ERR("Pcid version(%u) less than rpcid version(%u).\n", pcidVersion, rpcidVersion);
|
||||
}
|
||||
// compare os sysscap
|
||||
// compare os syscap
|
||||
for (i = 2; i < PCID_OUT_BUFFER; i++) { // 2, header of pcid & rpcid
|
||||
uint32_t temp1 = pcidOsAarry[i] ^ rpcidOsAarry[i];
|
||||
uint32_t temp2 = temp1 & rpcidOsAarry[i];
|
||||
@@ -672,5 +673,20 @@ int32_t ComparePcidWithRpcidString(char *pcidFile, char *rpcidFile)
|
||||
}
|
||||
}
|
||||
}
|
||||
// compare pri syscap
|
||||
flag = 1;
|
||||
for (i = 0; i < pcidPriSyscapLen; i++) {
|
||||
for (j = 0; j < rpcidPriSyscapLen; j++) {
|
||||
if (strcmp(pcidPriSyscap + SINGLE_FEAT_LENGTH * i,
|
||||
rpcidPriSyscap + SINGLE_FEAT_LENGTH * j) == 0) {
|
||||
flag = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flag != 0) {
|
||||
printf("Miss: %s\n", pcidPriSyscap + SINGLE_FEAT_LENGTH * i);
|
||||
}
|
||||
flag = 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -35,6 +35,7 @@ if (defined(ohos_lite)) {
|
||||
sources = [
|
||||
"//developtools/syscap_codec/interfaces/inner_api/syscap_interface.c",
|
||||
"//developtools/syscap_codec/src/create_pcid.c",
|
||||
"//developtools/syscap_codec/src/endian_internal.c",
|
||||
"syscap_codec_test.cpp",
|
||||
]
|
||||
|
||||
@@ -72,6 +73,7 @@ if (defined(ohos_lite)) {
|
||||
sources = [
|
||||
"//developtools/syscap_codec/interfaces/inner_api/syscap_interface.c",
|
||||
"//developtools/syscap_codec/src/create_pcid.c",
|
||||
"//developtools/syscap_codec/src/endian_internal.c",
|
||||
"syscap_codec_test.cpp",
|
||||
]
|
||||
|
||||
|
||||
@@ -99,4 +99,40 @@ HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1)
|
||||
EXPECT_EQ(decodePriCnt, 5);
|
||||
free(priOutput);
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: DecodeRpcidToStringFormat
|
||||
* @tc.desc: Check the DecodeRpcidToStringFormat Decoding.
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(SyscapCodecTest, DecodeRpcidToStringFormat, TestSize.Level1)
|
||||
{
|
||||
char inputfile[] = "/system/etc/rpcid.sc";
|
||||
char *out = DecodeRpcidToStringFormat(inputfile);
|
||||
EXPECT_TRUE(out);
|
||||
if (out != NULL) {
|
||||
printf("%s\n", out);
|
||||
free(out);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* @tc.name: ComparePcidString
|
||||
* @tc.desc: Check the DecodeRpcidToStringFormat Decoding.
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(SyscapCodecTest, ComparePcidString, TestSize.Level1)
|
||||
{
|
||||
char pcidString[] = "132096,385875968,12,17268736,2,344130,4,0,0,0,0,\
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
|
||||
SystemCapability.Device.xxx1,SystemCapability.Device.xxx2,\
|
||||
SystemCapability.Vendor.xxx1,SystemCapability.Vendor.xxx2";
|
||||
char rpcidString[] = "33588224,1665236995,4,262144,0,0,4,0,0,0,0,0,\
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,\
|
||||
SystemCapability.Device.xxx1,\
|
||||
SystemCapability.Device.xxx2,\
|
||||
SystemCapability.Vendor.xxx1,\
|
||||
SystemCapability.Vendor.xxx2";
|
||||
EXPECT_TRUE(ComparePcidString(pcidString, rpcidString));
|
||||
}
|
||||
} // namespace Syscap
|
||||
|
||||
Reference in New Issue
Block a user