mirror of
https://github.com/openharmony/developtools_syscap_codec.git
synced 2026-08-27 02:11:19 -04:00
modfiy for codex.
Signed-off-by: yudechen <chenyude@huawei.com> Change-Id: I1af469670be0dfef45a02ee259cc22c9dcc305e8
This commit is contained in:
@@ -34,7 +34,6 @@ ohos_executable("syscap_tool_bin") {
|
||||
if (is_mingw) {
|
||||
defines += [ "_POSIX_" ]
|
||||
}
|
||||
|
||||
sources = [ "./src/main.c" ]
|
||||
sources += sources_platform_common
|
||||
|
||||
|
||||
@@ -36,8 +36,6 @@
|
||||
printf(__VA_ARGS__); \
|
||||
} while (0)
|
||||
|
||||
static char *inputFile = "/system/etc/PCID.sc";
|
||||
|
||||
static void FreeContextBuffer(char *contextBuffer)
|
||||
{
|
||||
(void)free(contextBuffer);
|
||||
@@ -49,6 +47,7 @@ static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen)
|
||||
FILE *fp = NULL;
|
||||
struct stat statBuf;
|
||||
char *contextBuffer = NULL;
|
||||
const char *inputFile = "/system/etc/PCID.sc";
|
||||
|
||||
ret = stat(inputFile, &statBuf);
|
||||
if (ret != 0) {
|
||||
@@ -86,7 +85,7 @@ static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen)
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN], int length)
|
||||
bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN])
|
||||
{
|
||||
int32_t ret;
|
||||
int32_t res;
|
||||
|
||||
@@ -27,7 +27,7 @@ extern "C" {
|
||||
|
||||
#define MAX_SYSCAP_STR_LEN 128
|
||||
|
||||
bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN], int length);
|
||||
bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_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);
|
||||
|
||||
@@ -70,7 +70,7 @@ static char* getSystemCapability()
|
||||
char osCapArray[OS_SYSCAP_U32_NUM][U32_TO_STR_MAX_LEN] = {};
|
||||
char (*priCapArray)[SYSCAP_STR_MAX_LEN] = nullptr;
|
||||
|
||||
retBool = EncodeOsSyscap(osOutput, SYSCAP_STR_MAX_LEN);
|
||||
retBool = EncodeOsSyscap(osOutput);
|
||||
if (!retBool) {
|
||||
PRINT_ERR("get encoded os syscap failed.");
|
||||
return nullptr;
|
||||
|
||||
+39
-20
@@ -53,14 +53,27 @@ static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *c
|
||||
FILE *fp = NULL;
|
||||
struct stat statBuf;
|
||||
char *contextBuffer = NULL;
|
||||
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);
|
||||
@@ -69,15 +82,15 @@ static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *c
|
||||
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;
|
||||
}
|
||||
ret = fread(contextBuffer, statBuf.st_size, 1, fp);
|
||||
if (ret != 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;
|
||||
@@ -95,38 +108,44 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, const char *filename
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
char fileFullPath[PATH_MAX] = {0};
|
||||
int32_t pathLen = strlen(outDirPath);
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
|
||||
ret = strncpy_s(fileFullPath, PATH_MAX, outDirPath, pathLen + 1);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("strncpy_s failed, source string:%s, len = %d, errno = %d\n", outDirPath, pathLen + 1, errno);
|
||||
#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
|
||||
|
||||
if (fileFullPath[pathLen - 1] != '/' && fileFullPath[pathLen - 1] != '\\') {
|
||||
fileFullPath[pathLen] = '/';
|
||||
if (path[pathLen - 1] != '/' && path[pathLen - 1] != '\\') {
|
||||
path[pathLen] = '/';
|
||||
}
|
||||
|
||||
if (strlen(fileFullPath) + strlen(filename) + 1 > PATH_MAX) {
|
||||
if (strlen(path) + strlen(filename) + 1 > PATH_MAX) {
|
||||
PRINT_ERR("length of path too long.\n");
|
||||
return -1;
|
||||
}
|
||||
ret = strncat_s(fileFullPath, PATH_MAX, filename, strlen(filename) + 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",
|
||||
fileFullPath, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno);
|
||||
path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(fileFullPath, "wb");
|
||||
fp = fopen(path, "wb");
|
||||
if (fp == NULL) {
|
||||
PRINT_ERR("can't create file(%s), errno = %d\n", fileFullPath, errno);
|
||||
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", fileFullPath, errno);
|
||||
PRINT_ERR("can't write file(%s),errno = %d\n", path, errno);
|
||||
(void)fclose(fp);
|
||||
return -1;
|
||||
}
|
||||
@@ -148,8 +167,8 @@ static cJSON *CreateWholeSyscapJsonObj(void)
|
||||
|
||||
int32_t CreatePCID(char *inputFile, char *outDirPath)
|
||||
{
|
||||
int32_t ret, osCapSize, sectorOfBits, posOfBits;
|
||||
uint32_t i, contextBufLen, privateCapSize;
|
||||
int32_t ret, sectorOfBits, posOfBits;
|
||||
uint32_t i, contextBufLen, privateCapSize, osCapSize;
|
||||
errno_t nRet = 0;
|
||||
char *contextBuffer = NULL;
|
||||
char *systemType = NULL;
|
||||
|
||||
+46
-22
@@ -60,14 +60,27 @@ static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *b
|
||||
FILE *fp = NULL;
|
||||
struct stat statBuf;
|
||||
char *contextBuffer = NULL;
|
||||
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);
|
||||
@@ -75,15 +88,15 @@ static int32_t GetFileContext(char *inputFile, char **contextBufPtr, uint32_t *b
|
||||
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;
|
||||
}
|
||||
ret = fread(contextBuffer, statBuf.st_size, 1, fp);
|
||||
if (ret != 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;
|
||||
@@ -100,39 +113,45 @@ static int32_t ConvertedContextSaveAsFile(char *outDirPath, char *filename, char
|
||||
{
|
||||
int32_t ret;
|
||||
FILE *fp = NULL;
|
||||
char fileFullPath[PATH_MAX] = {0};
|
||||
char path[PATH_MAX + 1] = {0x00};
|
||||
int32_t pathLen = strlen(outDirPath);
|
||||
|
||||
ret = strncpy_s(fileFullPath, PATH_MAX, outDirPath, pathLen + 1);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("strncpy_s failed, source string:%s, len = %d, errno = %d\n", outDirPath, pathLen + 1, errno);
|
||||
#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
|
||||
|
||||
if (fileFullPath[pathLen - 1] != '/' && fileFullPath[pathLen - 1] != '\\') {
|
||||
fileFullPath[pathLen] = '/';
|
||||
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(fileFullPath, PATH_MAX, filename, strlen(filename) + 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",
|
||||
fileFullPath, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno);
|
||||
path, PATH_MAX, filename, (int32_t)strlen(filename) + 1, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
fp = fopen(fileFullPath, "wb");
|
||||
fp = fopen(path, "wb");
|
||||
if (fp == NULL) {
|
||||
PRINT_ERR("can`t create file(%s), errno = %d\n", fileFullPath, errno);
|
||||
PRINT_ERR("can`t create file(%s), errno = %d\n", path, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = fwrite(convertedBuffer, bufferLen, 1, fp);
|
||||
if (ret != 1) {
|
||||
PRINT_ERR("can`t write file(%s),errno = %d\n", fileFullPath, errno);
|
||||
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;
|
||||
}
|
||||
@@ -229,7 +248,7 @@ int32_t PCIDEncode(char *inputFile, char *outDirPath)
|
||||
|
||||
cjsonObjectPtr = cJSON_GetObjectItem(cjsonObjectRoot, "system_type");
|
||||
if (cjsonObjectPtr == NULL || !cJSON_IsString(cjsonObjectPtr)) {
|
||||
PRINT_ERR("get \"system_type\" failed, cjsonObjectPtr = %p\n", cjsonObjectPtr);
|
||||
PRINT_ERR("get \"system_type\" failed.\n");
|
||||
ret = -1;
|
||||
goto FREE_CONVERT_OUT;
|
||||
}
|
||||
@@ -482,7 +501,7 @@ int32_t PCIDDecode(char *inputFile, char *outDirPath)
|
||||
goto FREE_SYSCAP_OUT;
|
||||
}
|
||||
|
||||
for (int32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) {
|
||||
for (uint32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) {
|
||||
if (*(privateCapArrayPtr + (i + 1) * SINGLE_FEAT_LENGTH - 1) != '\0') {
|
||||
PRINT_ERR("prase file failed, format is invalid, input file : %s\n", inputFile);
|
||||
ret = -1;
|
||||
@@ -617,6 +636,11 @@ int32_t RPCIDEncode(char *inputFile, char *outDirPath)
|
||||
convertedBufLen += (2 * sizeof(uint16_t) + sysCapSize * SINGLE_FEAT_LENGTH);
|
||||
|
||||
convertedBuffer = (char *)malloc(convertedBufLen);
|
||||
if (convertedBuffer == NULL) {
|
||||
PRINT_ERR("malloc failed\n");
|
||||
ret = -1;
|
||||
goto FREE_CONTEXT_OUT;
|
||||
}
|
||||
(void)memset_s(convertedBuffer, convertedBufLen, 0, convertedBufLen);
|
||||
|
||||
headPtr = (RPCIDHead *)convertedBuffer;
|
||||
@@ -727,7 +751,7 @@ int32_t RPCIDDecode(char *inputFile, char *outDirPath)
|
||||
ret = -1;
|
||||
goto FREE_CONTEXT_OUT;
|
||||
}
|
||||
for (int32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) {
|
||||
for (uint32_t i = 0; i < (sysCapLength / SINGLE_FEAT_LENGTH); i++) {
|
||||
if (*(sysCapArrayPtr + (i + 1) * SINGLE_FEAT_LENGTH - 1) != '\0') {
|
||||
PRINT_ERR("prase file failed, format is invalid, input file : %s\n", inputFile);
|
||||
ret = -1;
|
||||
|
||||
@@ -35,7 +35,7 @@ void SyscapCodecTest::TearDown() {}
|
||||
HWTEST_F(SyscapCodecTest, EncodeOsSyscap, TestSize.Level1)
|
||||
{
|
||||
char OsInput[MAX_SYSCAP_STR_LEN] = {0};
|
||||
EXPECT_TRUE(EncodeOsSyscap(OsInput, MAX_SYSCAP_STR_LEN));
|
||||
EXPECT_TRUE(EncodeOsSyscap(OsInput));
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user