mirror of
https://github.com/openharmony/developtools_syscap_codec.git
synced 2026-08-27 02:11:19 -04:00
@@ -28,6 +28,7 @@ sources_platform_common = [
|
||||
"./src/syscap_tool.c",
|
||||
"./src/create_pcid.c",
|
||||
"./src/endian_internal.c",
|
||||
"./src/context_tool.c",
|
||||
]
|
||||
|
||||
ohos_executable("syscap_tool_bin") {
|
||||
@@ -93,6 +94,7 @@ if (defined(ohos_lite)) {
|
||||
public_configs = [ ":syscap_interface_public_config" ]
|
||||
sources = [
|
||||
"./interfaces/inner_api/syscap_interface.c",
|
||||
"./src/context_tool.c",
|
||||
"./src/endian_internal.c",
|
||||
"./src/syscap_tool.c",
|
||||
]
|
||||
@@ -116,6 +118,7 @@ if (defined(ohos_lite)) {
|
||||
public_configs = [ ":syscap_interface_public_config" ]
|
||||
sources = [
|
||||
"./interfaces/inner_api/syscap_interface.c",
|
||||
"./src/context_tool.c",
|
||||
"./src/endian_internal.c",
|
||||
"./src/syscap_tool.c",
|
||||
]
|
||||
|
||||
+1
-2
@@ -19,8 +19,7 @@
|
||||
"ram": "0",
|
||||
"deps": {
|
||||
"components": [
|
||||
"napi",
|
||||
"hiviewdfx_hilog_native"
|
||||
"napi"
|
||||
],
|
||||
"third_party": [
|
||||
"bounds_checking_function",
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef CONTEXT_TOOL_H
|
||||
#define CONTEXT_TOOL_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "cJSON.h"
|
||||
|
||||
#define PRINT_ERR(...) \
|
||||
do { \
|
||||
printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
#endif /* __cplusplus */
|
||||
|
||||
typedef struct RequiredProductCompatibilityIDHead {
|
||||
uint16_t apiVersion : 15;
|
||||
uint16_t apiVersionType : 1;
|
||||
} RPCIDHead;
|
||||
|
||||
void FreeContextBuffer(char *contextBuffer);
|
||||
int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen);
|
||||
int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, char *convertedBuffer, size_t contextBufLen);
|
||||
int32_t CheckRpcidFormat(const char *inputFile, char **buffer, uint32_t *len);
|
||||
cJSON *CreateWholeSyscapJsonObj(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif /* __cplusplus */
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#endif /* SYSCAP_TOOL_H */
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "syscap_tool.h"
|
||||
#include "endian_internal.h"
|
||||
#include "syscap_interface.h"
|
||||
#include "context_tool.h"
|
||||
|
||||
#ifdef SYSCAP_DEFINE_EXTERN_ENABLE
|
||||
#include "syscap_define_custom.h"
|
||||
@@ -41,18 +42,6 @@
|
||||
#define INT_BIT 32
|
||||
#define U32_TO_STR_MAX_LEN 11
|
||||
|
||||
|
||||
#define PRINT_ERR(...) \
|
||||
do { \
|
||||
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;
|
||||
@@ -64,66 +53,6 @@ typedef struct ProductCompatibilityID {
|
||||
|
||||
static const char *g_pcidPath = "/system/etc/PCID.sc";
|
||||
|
||||
static void FreeContextBuffer(char *contextBuffer)
|
||||
{
|
||||
(void)free(contextBuffer);
|
||||
}
|
||||
|
||||
static int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen)
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
struct stat statBuf;
|
||||
char *contextBuffer = NULL;
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
#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", path, errno);
|
||||
return -1;
|
||||
}
|
||||
if (!(statBuf.st_mode & S_IRUSR)) {
|
||||
PRINT_ERR("don't have permission to read the file(%s)\n", path);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer = (char *)malloc(statBuf.st_size + 1);
|
||||
if (contextBuffer == NULL) {
|
||||
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
fp = fopen(path, "rb");
|
||||
if (fp == NULL) {
|
||||
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", path, errno);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer[statBuf.st_size] = '\0';
|
||||
(void)fclose(fp);
|
||||
|
||||
*contextBufPtr = contextBuffer;
|
||||
*bufferLen = statBuf.st_size + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool EncodeOsSyscap(char *output, int len)
|
||||
{
|
||||
int32_t ret;
|
||||
@@ -322,16 +251,6 @@ static int SetOsSysCapBitMap(uint8_t *out, uint16_t outLen, const uint16_t *inde
|
||||
return 0;
|
||||
}
|
||||
|
||||
static cJSON *CreateWholeSyscapJsonObj(void)
|
||||
{
|
||||
size_t numOfSyscapAll = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
for (size_t i = 0; i < numOfSyscapAll; i++) {
|
||||
cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num));
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson)
|
||||
{
|
||||
uint32_t i;
|
||||
@@ -380,42 +299,6 @@ FREE_SYSCAP_OUT:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int32_t CheckRpcidFormat(const 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(const char *inputFile)
|
||||
{
|
||||
int32_t ret = 0;
|
||||
|
||||
@@ -27,6 +27,7 @@ sources_platform_common = [
|
||||
"../src/syscap_tool.c",
|
||||
"../src/create_pcid.c",
|
||||
"../src/endian_internal.c",
|
||||
"../src/context_tool.c",
|
||||
"../interfaces/inner_api/syscap_interface.c",
|
||||
]
|
||||
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#include "napi/native_api.h"
|
||||
#include "napi/native_node_api.h"
|
||||
#include "syscap_interface.h"
|
||||
#include "context_tool.h"
|
||||
|
||||
namespace OHOS {
|
||||
EXTERN_C_START
|
||||
@@ -28,12 +29,6 @@ constexpr size_t PCID_MAIN_U32 = OS_SYSCAP_U32_NUM + 2;
|
||||
constexpr size_t U32_TO_STR_MAX_LEN = 11;
|
||||
constexpr size_t KEY_BUFFER_SIZE = 32;
|
||||
|
||||
#define PRINT_ERR(...) \
|
||||
do { \
|
||||
printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
#define GET_PARAMS(env, info, num) \
|
||||
size_t argc = num; \
|
||||
napi_value argv[num] = {0}; \
|
||||
|
||||
@@ -0,0 +1,194 @@
|
||||
/*
|
||||
* Copyright (c) 2023-2023 Huawei Device Co., Ltd.
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "context_tool.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include <sys/stat.h>
|
||||
#include "securec.h"
|
||||
|
||||
#include "endian_internal.h"
|
||||
|
||||
#ifdef SYSCAP_DEFINE_EXTERN_ENABLE
|
||||
#include "syscap_define_custom.h"
|
||||
#else
|
||||
#include "syscap_define.h"
|
||||
#endif
|
||||
|
||||
void FreeContextBuffer(char *contextBuffer)
|
||||
{
|
||||
if (contextBuffer != NULL) {
|
||||
(void)free(contextBuffer);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t GetFileContext(const char *inputFile, char **contextBufPtr, uint32_t *bufferLen)
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
struct stat statBuf;
|
||||
char *contextBuffer = NULL;
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
#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", path, errno);
|
||||
return -1;
|
||||
}
|
||||
if (!(statBuf.st_mode & S_IRUSR)) {
|
||||
PRINT_ERR("don't have permission to read the file(%s)\n", path);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer = (char *)malloc(statBuf.st_size + 1);
|
||||
if (contextBuffer == NULL) {
|
||||
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
fp = fopen(path, "rb");
|
||||
if (fp == NULL) {
|
||||
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", path, errno);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer[statBuf.st_size] = '\0';
|
||||
(void)fclose(fp);
|
||||
|
||||
*contextBufPtr = contextBuffer;
|
||||
*bufferLen = statBuf.st_size + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, char *convertedBuffer, size_t contextBufLen)
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
#ifdef _POSIX_
|
||||
if (strlen(outDirPath) >= PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) {
|
||||
PRINT_ERR("get path(%s) failed\n", outDirPath);
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
if (strlen(outDirPath) >= PATH_MAX || realpath(outDirPath, path) == NULL) {
|
||||
PRINT_ERR("get file(%s) real path failed\n", outDirPath);
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
int32_t pathLen = strlen(path);
|
||||
if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') {
|
||||
path[pathLen] = '/';
|
||||
}
|
||||
|
||||
if (strlen(path) + strlen(filename) + 1 > PATH_MAX) {
|
||||
PRINT_ERR("length of path too long.\n");
|
||||
return -1;
|
||||
}
|
||||
ret = strncat_s(path, PATH_MAX, filename, strlen(filename) + 1);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n",
|
||||
path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(path, "wb");
|
||||
if (fp == NULL) {
|
||||
PRINT_ERR("can't create file(%s), errno = %d\n", path, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fwrite(convertedBuffer, contextBufLen, 1, fp) != 1) {
|
||||
PRINT_ERR("can't write file(%s),errno = %d\n", path, errno);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
(void)fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t CheckRpcidFormat(const 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;
|
||||
}
|
||||
|
||||
cJSON *CreateWholeSyscapJsonObj(void)
|
||||
{
|
||||
cJSON *syscapJsonObj = cJSON_CreateObject();
|
||||
if (syscapJsonObj == NULL) {
|
||||
PRINT_ERR("interface-create jsonObj failed.");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_t syscapNums = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
|
||||
for (size_t i = 0; i < syscapNums; i++) {
|
||||
cJSON_AddItemToObject(syscapJsonObj, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num));
|
||||
}
|
||||
return syscapJsonObj;
|
||||
}
|
||||
+10
-137
@@ -24,6 +24,7 @@
|
||||
#include "cJSON.h"
|
||||
#include "endian_internal.h"
|
||||
#include "create_pcid.h"
|
||||
#include "context_tool.h"
|
||||
|
||||
#ifdef SYSCAP_DEFINE_EXTERN_ENABLE
|
||||
#include "syscap_define_custom.h"
|
||||
@@ -39,134 +40,6 @@
|
||||
|
||||
#define U32_TO_STR_MAX_LEN 11
|
||||
|
||||
#define PRINT_ERR(...) \
|
||||
do { \
|
||||
printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
static void FreeContextBuffer(char *contextBuffer)
|
||||
{
|
||||
(void)free(contextBuffer);
|
||||
}
|
||||
|
||||
static int32_t GetFileContext(char *inputFile, char **contextBufPtr, size_t *contextBufLen)
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
struct stat statBuf;
|
||||
char *contextBuffer = NULL;
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
#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", path, errno);
|
||||
return -1;
|
||||
}
|
||||
if (!(statBuf.st_mode & S_IRUSR)) {
|
||||
PRINT_ERR("don't have permission to read the file(%s)\n", path);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer = (char *)malloc(statBuf.st_size + 1);
|
||||
if (contextBuffer == NULL) {
|
||||
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(path, "rb");
|
||||
if (fp == NULL) {
|
||||
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", path, errno);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer[statBuf.st_size] = '\0';
|
||||
(void)fclose(fp);
|
||||
|
||||
*contextBufPtr = contextBuffer;
|
||||
*contextBufLen = statBuf.st_size + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename, \
|
||||
char *convertedBuffer, size_t contextBufLen)
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
#ifdef _POSIX_
|
||||
if (strlen(outDirPath) > PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) {
|
||||
PRINT_ERR("get path(%s) failed\n", outDirPath);
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
if (strlen(outDirPath) > PATH_MAX || realpath(outDirPath, path) == NULL) {
|
||||
PRINT_ERR("get file(%s) real path failed\n", outDirPath);
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
int32_t pathLen = strlen(path);
|
||||
if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') {
|
||||
path[pathLen] = '/';
|
||||
}
|
||||
|
||||
if (strlen(path) + strlen(filename) + 1 > PATH_MAX) {
|
||||
PRINT_ERR("length of path too long.\n");
|
||||
return -1;
|
||||
}
|
||||
ret = strncat_s(path, PATH_MAX, filename, strlen(filename) + 1);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n",
|
||||
path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(path, "wb");
|
||||
if (fp == NULL) {
|
||||
PRINT_ERR("can't create file(%s), errno = %d\n", path, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (fwrite(convertedBuffer, contextBufLen, 1, fp) != 1) {
|
||||
PRINT_ERR("can't write file(%s),errno = %d\n", path, errno);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
(void)fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static cJSON *CreateWholeSyscapJsonObj(void)
|
||||
{
|
||||
size_t numOfSyscapAll = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
for (size_t i = 0; i < numOfSyscapAll; i++) {
|
||||
cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num));
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
int32_t SetOsSyscap(PCIDMain *pcidBuffer, uint32_t osCapSize,
|
||||
const cJSON *jsonOsSyscapObj, const cJSON *allOsSyscapObj)
|
||||
{
|
||||
@@ -310,11 +183,11 @@ int32_t GetPriSyscapLen(uint32_t privateCapSize, cJSON *jsonPriSyscapObj, uint16
|
||||
int32_t CreatePCID(char *inputFile, char *outDirPath)
|
||||
{
|
||||
uint32_t privateCapSize, osCapSize;
|
||||
size_t contextBufLen;
|
||||
uint32_t contextBufLen;
|
||||
char *contextBuffer = NULL;
|
||||
cJSON *allOsSyscapObj = CreateWholeSyscapJsonObj();
|
||||
|
||||
int32_t ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen);
|
||||
int32_t ret = GetFileContext(inputFile, &contextBuffer, (uint32_t *)&contextBufLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
|
||||
goto FREE_CONVERT_OUT;
|
||||
@@ -473,9 +346,9 @@ int32_t DecodePCID(char *inputFile, char *outDirPath)
|
||||
{
|
||||
int32_t ret = 0;
|
||||
char *contextBuffer = NULL;
|
||||
size_t contextBufLen;
|
||||
uint32_t contextBufLen;
|
||||
|
||||
ret = GetFileContext(inputFile, &contextBuffer, &contextBufLen);
|
||||
ret = GetFileContext(inputFile, &contextBuffer, (uint32_t *)&contextBufLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("GetFileContext failed, input file : %s\n", inputFile);
|
||||
return -1;
|
||||
@@ -725,11 +598,11 @@ int32_t DecodeStringPCIDToJson(char *input, char *outDirPath)
|
||||
int32_t ret = -1;
|
||||
uint32_t osSyscap[OS_SYSCAP_NUM] = {0};
|
||||
uint32_t pcidHeader[PCID_HEADER];
|
||||
size_t fileContextLen;
|
||||
uint32_t fileContextLen;
|
||||
char *ctx = NULL;
|
||||
char *priSyscapStr = NULL;
|
||||
|
||||
if (GetFileContext(input, &ctx, &fileContextLen) != 0) {
|
||||
if (GetFileContext(input, &ctx, (uint32_t *)&fileContextLen) != 0) {
|
||||
PRINT_ERR("GetFileContext failed, input file : %s\n", input);
|
||||
goto PARSE_FAILED;
|
||||
}
|
||||
@@ -780,7 +653,7 @@ PARSE_FAILED:
|
||||
int32_t EncodePcidscToString(char *inputFile, char *outDirPath)
|
||||
{
|
||||
int32_t ret = 0;
|
||||
size_t bufferLen, privateSyscapLen, outputLen;
|
||||
uint32_t bufferLen, privateSyscapLen, outputLen;
|
||||
uint32_t i, j;
|
||||
uint32_t *mainSyscap = NULL;
|
||||
uint16_t priSyscapCount = 0;
|
||||
@@ -790,14 +663,14 @@ int32_t EncodePcidscToString(char *inputFile, char *outDirPath)
|
||||
char *output = NULL;
|
||||
PCIDMain *pcidMain = NULL;
|
||||
|
||||
ret = GetFileContext(inputFile, &contextBuffer, &bufferLen);
|
||||
ret = GetFileContext(inputFile, &contextBuffer, (uint32_t *)&bufferLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("Get pcid file failed, pcid file path: %s\n", inputFile);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (bufferLen > 1128) { // 1128, max size of pcid.sc
|
||||
PRINT_ERR("Input pcid file too large, pcid file size: %zu\n", bufferLen);
|
||||
PRINT_ERR("Input pcid file too large, pcid file size: %u\n", bufferLen);
|
||||
goto FREE_CONTEXT;
|
||||
}
|
||||
|
||||
|
||||
+1
-5
@@ -22,12 +22,8 @@
|
||||
#include "securec.h"
|
||||
#include "syscap_tool.h"
|
||||
#include "create_pcid.h"
|
||||
#include "context_tool.h"
|
||||
|
||||
#define PRINT_ERR(...) \
|
||||
do { \
|
||||
printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
} while (0)
|
||||
#define SYSCAP_VERSION "2.0.1"
|
||||
#define OUTPUT_VERSION_LEN 200
|
||||
#define ENCODE 0
|
||||
|
||||
+13
-185
@@ -26,6 +26,7 @@
|
||||
#include "cJSON.h"
|
||||
#include "create_pcid.h"
|
||||
#include "syscap_tool.h"
|
||||
#include "context_tool.h"
|
||||
|
||||
#ifdef SYSCAP_DEFINE_EXTERN_ENABLE
|
||||
#include "syscap_define_custom.h"
|
||||
@@ -33,11 +34,6 @@
|
||||
#include "syscap_define.h"
|
||||
#endif
|
||||
|
||||
typedef struct RequiredProductCompatibilityIDHead {
|
||||
uint16_t apiVersion : 15;
|
||||
uint16_t apiVersionType : 1;
|
||||
} RPCIDHead;
|
||||
|
||||
#define SYSCAP_PREFIX_LEN 17
|
||||
#define SINGLE_FEAT_LEN (SINGLE_SYSCAP_LEN - SYSCAP_PREFIX_LEN)
|
||||
#define UINT8_BIT 8
|
||||
@@ -48,135 +44,6 @@ typedef struct RequiredProductCompatibilityIDHead {
|
||||
#define U32_TO_STR_MAX_LEN 11
|
||||
#define STRING_FORMAT_LEN_MAX 1024
|
||||
|
||||
#define PRINT_ERR(...) \
|
||||
do { \
|
||||
printf("ERROR: [%s: %d] -> ", __FILE__, __LINE__); \
|
||||
printf(__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
static void FreeContextBuffer(char *contextBuffer)
|
||||
{
|
||||
(void)free(contextBuffer);
|
||||
}
|
||||
|
||||
static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *bufferLen)
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
struct stat statBuf;
|
||||
char *contextBuffer = NULL;
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
#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", path, errno);
|
||||
return -1;
|
||||
}
|
||||
if (!(statBuf.st_mode & S_IRUSR)) {
|
||||
PRINT_ERR("don't have permission to read the file(%s)\n", path);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer = (char *)malloc(statBuf.st_size + 1);
|
||||
if (contextBuffer == NULL) {
|
||||
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", (int32_t)statBuf.st_size + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
fp = fopen(path, "rb");
|
||||
if (fp == NULL) {
|
||||
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", path, errno);
|
||||
FreeContextBuffer(contextBuffer);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
contextBuffer[statBuf.st_size] = '\0';
|
||||
(void)fclose(fp);
|
||||
|
||||
*contextBufPtr = contextBuffer;
|
||||
*bufferLen = statBuf.st_size + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename,
|
||||
char *convertedBuffer, size_t bufferLen)
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
#ifdef _POSIX_
|
||||
if (strlen(outDirPath) >= PATH_MAX || strncpy_s(path, PATH_MAX, outDirPath, strlen(outDirPath)) != EOK) {
|
||||
PRINT_ERR("get path(%s) failed\n", outDirPath);
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
if (strlen(outDirPath) >= PATH_MAX || realpath(outDirPath, path) == NULL) {
|
||||
PRINT_ERR("get file(%s) real path failed\n", outDirPath);
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int32_t pathLen = strlen(path);
|
||||
if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') {
|
||||
path[pathLen] = '/';
|
||||
}
|
||||
|
||||
if (strlen(filename) + 1 > PATH_MAX) {
|
||||
PRINT_ERR("filename(%s) too long.\n", filename);
|
||||
return -1;
|
||||
}
|
||||
ret = strncat_s(path, PATH_MAX, filename, strlen(filename));
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("strncat_s failed, (%s, %d, %s, %d), errno = %d\n",
|
||||
path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(path, "wb");
|
||||
if (fp == NULL) {
|
||||
PRINT_ERR("can`t create file(%s), errno = %d\n", path, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t retFwrite = fwrite(convertedBuffer, bufferLen, 1, fp);
|
||||
if (retFwrite != 1) {
|
||||
PRINT_ERR("can`t write file(%s),errno = %d\n", path, errno);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
(void)fclose(fp);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static cJSON *CreateWholeSyscapJsonObj(void)
|
||||
{
|
||||
size_t allSyscapNum = sizeof(g_arraySyscap) / sizeof(SyscapWithNum);
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
for (size_t i = 0; i < allSyscapNum; i++) {
|
||||
cJSON_AddItemToObject(root, g_arraySyscap[i].str, cJSON_CreateNumber(g_arraySyscap[i].num));
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
int32_t RPCIDEncode(char *inputFile, char *outputPath)
|
||||
{
|
||||
int32_t ret;
|
||||
@@ -300,77 +167,39 @@ static int32_t ParseRpcidToJson(char *input, uint32_t inputLen, cJSON *rpcidJson
|
||||
char *temp = sysCapBegin + i * SINGLE_FEAT_LEN;
|
||||
if (strlen(temp) >= SINGLE_FEAT_LEN || strlen(temp) == 0) {
|
||||
PRINT_ERR("Get SysCap failed, string length(%u) error.\n", (uint32_t)strlen(temp));
|
||||
ret = -1;
|
||||
goto FREE_SYSCAP_OUT;
|
||||
cJSON_Delete(sysCapJson);
|
||||
return -1;
|
||||
}
|
||||
char buffer[SINGLE_SYSCAP_LEN] = "SystemCapability.";
|
||||
|
||||
|
||||
ret = strncat_s(buffer, sizeof(buffer), temp, SINGLE_FEAT_LEN);
|
||||
if (ret != EOK) {
|
||||
PRINT_ERR("strncat_s failed.\n");
|
||||
goto FREE_SYSCAP_OUT;
|
||||
cJSON_Delete(sysCapJson);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (!cJSON_AddItemToArray(sysCapJson, cJSON_CreateString(buffer))) {
|
||||
PRINT_ERR("Add syscap string to json failed.\n");
|
||||
ret = -1;
|
||||
goto FREE_SYSCAP_OUT;
|
||||
cJSON_Delete(sysCapJson);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!cJSON_AddNumberToObject(rpcidJson, "api_version", NtohsInter(rpcidHeader->apiVersion))) {
|
||||
PRINT_ERR("Add api_version to json failed.\n");
|
||||
ret = -1;
|
||||
goto FREE_SYSCAP_OUT;
|
||||
cJSON_Delete(sysCapJson);
|
||||
return -1;
|
||||
}
|
||||
if (!cJSON_AddItemToObject(rpcidJson, "syscap", sysCapJson)) {
|
||||
PRINT_ERR("Add syscap to json failed.\n");
|
||||
ret = -1;
|
||||
goto FREE_SYSCAP_OUT;
|
||||
cJSON_Delete(sysCapJson);
|
||||
return -1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int32_t RPCIDDecode(char *inputFile, char *outputPath)
|
||||
{
|
||||
int32_t ret = 0;
|
||||
@@ -611,8 +440,7 @@ int32_t GetPriSyscapData(char *input, char **priSyscap, uint32_t *priSyscapLen)
|
||||
char *tok = NULL;
|
||||
char *temp = strtok_r(input, ",", &tok);
|
||||
while (temp) {
|
||||
int ret = strncpy_s(private, SINGLE_SYSCAP_LEN,
|
||||
temp, SINGLE_SYSCAP_LEN - 1);
|
||||
int ret = strncpy_s(private, SINGLE_SYSCAP_LEN, temp, SINGLE_SYSCAP_LEN - 1);
|
||||
if (ret != EOK) {
|
||||
PRINT_ERR("strncpy_s failed.\n");
|
||||
free(priSysCapOut);
|
||||
|
||||
@@ -37,6 +37,7 @@ if (defined(ohos_lite)) {
|
||||
|
||||
sources = [
|
||||
"../../../interfaces/inner_api/syscap_interface.c",
|
||||
"../../../src/context_tool.c",
|
||||
"../../../src/create_pcid.c",
|
||||
"../../../src/endian_internal.c",
|
||||
"../../../src/syscap_tool.c",
|
||||
@@ -46,7 +47,6 @@ if (defined(ohos_lite)) {
|
||||
defines = [ "NAPI_TEST" ]
|
||||
|
||||
deps = [
|
||||
"//base//hiviewdfx/hilog_lite/frameworks/featured:hilog_shared",
|
||||
"//build/lite/config/component/cJSON:cjson_static",
|
||||
"//test/testfwk/developer_test/third_party/lib/cpp:gtest_main",
|
||||
"//third_party/bounds_checking_function:libsec_static",
|
||||
@@ -78,6 +78,7 @@ if (defined(ohos_lite)) {
|
||||
|
||||
sources = [
|
||||
"../../../interfaces/inner_api/syscap_interface.c",
|
||||
"../../../src/context_tool.c",
|
||||
"../../../src/create_pcid.c",
|
||||
"../../../src/endian_internal.c",
|
||||
"../../../src/syscap_tool.c",
|
||||
|
||||
Reference in New Issue
Block a user