mirror of
https://github.com/openharmony/developtools_syscap_codec.git
synced 2026-08-27 10:21:25 -04:00
add length argument of funcation EncodeOsSyscap.
Signed-off-by: yudechen <chenyude@huawei.com> Change-Id: If65b898671e0d035a578e34ba52c442b1a48de80
This commit is contained in:
@@ -21,14 +21,13 @@
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <securec.h>
|
||||
#include <limits.h>
|
||||
#include "syscap_define.h"
|
||||
#include "syscap_interface.h"
|
||||
|
||||
#define PCID_OUT_BUFFER 32
|
||||
#define OS_SYSCAP_BYTES 120
|
||||
#define BITS_OF_BYTE 8
|
||||
#define PCID_MAIN_LEN 128
|
||||
#define PATH_MAX 4096
|
||||
#define PCID_MAIN_BYTES 128
|
||||
#define SYSCAP_STR_LEN_MAX 128
|
||||
|
||||
#define PRINT_ERR(...) \
|
||||
do { \
|
||||
@@ -85,20 +84,25 @@ static int32_t GetFileContext(char **contextBufPtr, uint32_t *bufferLen)
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN])
|
||||
bool EncodeOsSyscap(char *output, int len)
|
||||
{
|
||||
int32_t ret;
|
||||
int32_t res;
|
||||
char *contextBuffer = NULL;
|
||||
uint32_t bufferLen;
|
||||
|
||||
if (len != PCID_MAIN_BYTES) {
|
||||
PRINT_ERR("Os Syscap input len must be equal to 128.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
ret = GetFileContext(&contextBuffer, &bufferLen);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("GetFileContext failed, input file : /system/etc/PCID.sc\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
res = memcpy_s(output, PCID_MAIN_LEN, contextBuffer, PCID_MAIN_LEN);
|
||||
res = memcpy_s(output, PCID_MAIN_BYTES, contextBuffer, PCID_MAIN_BYTES);
|
||||
if (res != 0) {
|
||||
PRINT_ERR("memcpy_s failed.");
|
||||
FreeContextBuffer(contextBuffer);
|
||||
@@ -122,7 +126,7 @@ bool EncodePrivateSyscap(char **output, int *outputLen)
|
||||
return false;
|
||||
}
|
||||
|
||||
*outputLen = bufferLen - PCID_MAIN_LEN - 1;
|
||||
*outputLen = bufferLen - PCID_MAIN_BYTES - 1;
|
||||
outputStr = (char *)malloc(*outputLen);
|
||||
if (outputStr == NULL) {
|
||||
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", *outputLen, errno);
|
||||
@@ -131,7 +135,7 @@ bool EncodePrivateSyscap(char **output, int *outputLen)
|
||||
}
|
||||
(void)memset_s(outputStr, *outputLen, 0, *outputLen);
|
||||
|
||||
ret = strncpy_s(outputStr, *outputLen, contextBuffer + PCID_MAIN_LEN, *outputLen - 1);
|
||||
ret = strncpy_s(outputStr, *outputLen, contextBuffer + PCID_MAIN_BYTES, *outputLen - 1);
|
||||
if (ret != 0) {
|
||||
PRINT_ERR("strcpy_s failed.");
|
||||
FreeContextBuffer(contextBuffer);
|
||||
@@ -148,36 +152,36 @@ bool EncodePrivateSyscap(char **output, int *outputLen)
|
||||
bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt)
|
||||
{
|
||||
errno_t nRet = 0;
|
||||
uint16_t indexOfSyscap[BITS_OF_BYTE * OS_SYSCAP_BYTES] = {0};
|
||||
uint16_t indexOfSyscap[CHAR_BIT * OS_SYSCAP_BYTES] = {0};
|
||||
uint16_t countOfSyscap = 0;
|
||||
uint16_t i, j;
|
||||
|
||||
uint8_t *osSyscap = (uint8_t *)(input + 8); // 8, int[2] of pcid header
|
||||
|
||||
for (i = 0; i < OS_SYSCAP_BYTES; i++) {
|
||||
for (j = 0; j < BITS_OF_BYTE; j++) {
|
||||
for (j = 0; j < CHAR_BIT; j++) {
|
||||
if (osSyscap[i] & (0x01 << j)) {
|
||||
indexOfSyscap[countOfSyscap++] = i * BITS_OF_BYTE + j;
|
||||
indexOfSyscap[countOfSyscap++] = i * CHAR_BIT + j;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*outputCnt = countOfSyscap;
|
||||
char (*strSyscap)[MAX_SYSCAP_STR_LEN] = NULL;
|
||||
strSyscap = (char (*)[MAX_SYSCAP_STR_LEN])malloc(countOfSyscap * MAX_SYSCAP_STR_LEN);
|
||||
char (*strSyscap)[SYSCAP_STR_LEN_MAX] = NULL;
|
||||
strSyscap = (char (*)[SYSCAP_STR_LEN_MAX])malloc(countOfSyscap * SYSCAP_STR_LEN_MAX);
|
||||
if (strSyscap == NULL) {
|
||||
PRINT_ERR("malloc failed.");
|
||||
*outputCnt = 0;
|
||||
return false;
|
||||
}
|
||||
(void)memset_s(strSyscap, countOfSyscap * MAX_SYSCAP_STR_LEN, \
|
||||
0, countOfSyscap * MAX_SYSCAP_STR_LEN);
|
||||
(void)memset_s(strSyscap, countOfSyscap * SYSCAP_STR_LEN_MAX, \
|
||||
0, countOfSyscap * SYSCAP_STR_LEN_MAX);
|
||||
*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, MAX_SYSCAP_STR_LEN, arraySyscap[j].syscapStr);
|
||||
nRet = strcpy_s(*strSyscap, SYSCAP_STR_LEN_MAX, arraySyscap[j].syscapStr);
|
||||
if (nRet != EOK) {
|
||||
printf("strcpy_s failed. error = %d\n", nRet);
|
||||
*outputCnt = 0;
|
||||
@@ -196,7 +200,7 @@ bool DecodeOsSyscap(char input[128], char (**output)[128], int *outputCnt)
|
||||
|
||||
bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt)
|
||||
{
|
||||
char (*outputArray)[MAX_SYSCAP_STR_LEN] = NULL;
|
||||
char (*outputArray)[SYSCAP_STR_LEN_MAX] = NULL;
|
||||
char *inputPos = input;
|
||||
int bufferLen, ret;
|
||||
int syscapCnt = 0;
|
||||
@@ -209,8 +213,8 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt)
|
||||
}
|
||||
inputPos = input;
|
||||
|
||||
bufferLen = MAX_SYSCAP_STR_LEN * syscapCnt;
|
||||
outputArray = (char (*)[MAX_SYSCAP_STR_LEN])malloc(bufferLen);
|
||||
bufferLen = SYSCAP_STR_LEN_MAX * syscapCnt;
|
||||
outputArray = (char (*)[SYSCAP_STR_LEN_MAX])malloc(bufferLen);
|
||||
if (outputArray == NULL) {
|
||||
PRINT_ERR("malloc buffer failed, size = %d, errno = %d\n", bufferLen, errno);
|
||||
*outputCnt = 0;
|
||||
@@ -219,12 +223,12 @@ bool DecodePrivateSyscap(char *input, char (**output)[128], int *outputCnt)
|
||||
(void)memset_s(outputArray, bufferLen, 0, bufferLen);
|
||||
|
||||
*output = outputArray;
|
||||
char buffer[MAX_SYSCAP_STR_LEN - 17] = {0}; // 17. size of "SystemCapability."
|
||||
char buffer[SYSCAP_STR_LEN_MAX - 17] = {0}; // 17. size of "SystemCapability."
|
||||
char *bufferPos = buffer;
|
||||
while (*inputPos != '\0') {
|
||||
if (*inputPos == ',') {
|
||||
*bufferPos = '\0';
|
||||
ret = sprintf_s(*outputArray, MAX_SYSCAP_STR_LEN, "SystemCapability.%s", buffer);
|
||||
ret = sprintf_s(*outputArray, SYSCAP_STR_LEN_MAX, "SystemCapability.%s", buffer);
|
||||
if (ret == -1) {
|
||||
PRINT_ERR("sprintf_s failed\n");
|
||||
*outputCnt = 0;
|
||||
|
||||
@@ -25,9 +25,7 @@ extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
#endif /* __cplusplus */
|
||||
|
||||
#define MAX_SYSCAP_STR_LEN 128
|
||||
|
||||
bool EncodeOsSyscap(char output[MAX_SYSCAP_STR_LEN]);
|
||||
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);
|
||||
|
||||
@@ -27,6 +27,7 @@ EXTERN_C_START
|
||||
constexpr size_t OS_SYSCAP_U32_NUM = 30;
|
||||
constexpr size_t U32_TO_STR_MAX_LEN = 11;
|
||||
constexpr size_t SYSCAP_STR_MAX_LEN = 128;
|
||||
constexpr size_t PCID_MAIN_LEN = 128;
|
||||
constexpr size_t KEY_BUFFER_SIZE = 32;
|
||||
|
||||
#define PRINT_ERR(...) \
|
||||
@@ -70,7 +71,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);
|
||||
retBool = EncodeOsSyscap(osOutput, PCID_MAIN_LEN);
|
||||
if (!retBool) {
|
||||
PRINT_ERR("get encoded os syscap failed.");
|
||||
return nullptr;
|
||||
|
||||
@@ -14,11 +14,13 @@
|
||||
*/
|
||||
|
||||
#include"syscap_codec_test.h"
|
||||
#include <cstddef>
|
||||
|
||||
using namespace testing::ext;
|
||||
using namespace std;
|
||||
|
||||
namespace Syscap {
|
||||
constexpr size_t SYSCAP_STR_LEN_MAX = 128;
|
||||
void SyscapCodecTest::SetUpTestCase() {}
|
||||
|
||||
void SyscapCodecTest::TearDownTestCase() {}
|
||||
@@ -34,8 +36,9 @@ void SyscapCodecTest::TearDown() {}
|
||||
*/
|
||||
HWTEST_F(SyscapCodecTest, EncodeOsSyscap, TestSize.Level1)
|
||||
{
|
||||
char OsInput[MAX_SYSCAP_STR_LEN] = {0};
|
||||
EXPECT_TRUE(EncodeOsSyscap(OsInput));
|
||||
int pcidLen = SYSCAP_STR_LEN_MAX;
|
||||
char OsInput[SYSCAP_STR_LEN_MAX] = {0};
|
||||
EXPECT_TRUE(EncodeOsSyscap(OsInput, pcidLen));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -59,12 +62,12 @@ HWTEST_F(SyscapCodecTest, EncodePrivateSyscap, TestSize.Level1)
|
||||
HWTEST_F(SyscapCodecTest, DecodeOsSyscap, TestSize.Level1)
|
||||
{
|
||||
int osSyscap[32] = {1, 3, 3};
|
||||
char (*osOutput)[MAX_SYSCAP_STR_LEN] = NULL;
|
||||
char (*osOutput)[SYSCAP_STR_LEN_MAX] = NULL;
|
||||
int decodeOsCnt;
|
||||
char expectOsOutput001[] = "SystemCapability.Account.AppAccount";
|
||||
char expectOsOutput002[] = "SystemCapability.Account.OsAccount";
|
||||
EXPECT_TRUE(DecodeOsSyscap((char *)osSyscap, &osOutput, &decodeOsCnt));
|
||||
char (*tmpOsOutput)[MAX_SYSCAP_STR_LEN] = osOutput;
|
||||
char (*tmpOsOutput)[SYSCAP_STR_LEN_MAX] = osOutput;
|
||||
EXPECT_STREQ(*tmpOsOutput, expectOsOutput001);
|
||||
EXPECT_STREQ(*(tmpOsOutput + 1), expectOsOutput002);
|
||||
EXPECT_EQ(decodeOsCnt, 2);
|
||||
@@ -78,7 +81,7 @@ HWTEST_F(SyscapCodecTest, DecodeOsSyscap, TestSize.Level1)
|
||||
*/
|
||||
HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1)
|
||||
{
|
||||
char (*priOutput)[MAX_SYSCAP_STR_LEN] = NULL;
|
||||
char (*priOutput)[SYSCAP_STR_LEN_MAX] = NULL;
|
||||
char priSyscap[] = "Device.syscap1GEDR,Device.syscap2WREGW,Vendor.syscap3RGD,Vendor.syscap4RWEG,Vendor.syscap5REWGWE,";
|
||||
int decodePriCnt;
|
||||
char expectPriOutput001[] = "SystemCapability.Device.syscap1GEDR";
|
||||
@@ -87,7 +90,7 @@ HWTEST_F(SyscapCodecTest, DecodePrivateSyscap, TestSize.Level1)
|
||||
char expectPriOutput004[] = "SystemCapability.Vendor.syscap4RWEG";
|
||||
char expectPriOutput005[] = "SystemCapability.Vendor.syscap5REWGWE";
|
||||
EXPECT_TRUE(DecodePrivateSyscap(priSyscap, &priOutput, &decodePriCnt));
|
||||
char (*tmpPtiOutput)[MAX_SYSCAP_STR_LEN] = priOutput;
|
||||
char (*tmpPtiOutput)[SYSCAP_STR_LEN_MAX] = priOutput;
|
||||
EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput001);
|
||||
EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput002);
|
||||
EXPECT_STREQ(*tmpPtiOutput++, expectPriOutput003);
|
||||
|
||||
Reference in New Issue
Block a user