lnn support data compress

Signed-off-by: fushuchang <fushuchang2@huawei.com>
This commit is contained in:
fushuchang 2024-05-16 21:42:23 +08:00
parent cc0b34eb09
commit 1238d4fc97
7 changed files with 170 additions and 11 deletions

View File

@ -82,7 +82,8 @@
"bounds_checking_function",
"cJSON",
"mbedtls",
"libcoap"
"libcoap",
"zlib"
],
"third_party": [
"cJSON",
@ -90,7 +91,8 @@
"mbedtls",
"openssl",
"bounds_checking_function",
"sqlite"
"sqlite",
"zlib"
]
},
"build": {

View File

@ -2078,11 +2078,11 @@ static void DfxRecordLnnPostDeviceInfoStart(int64_t authSeq, const AuthSessionIn
}
static void SetCompressFlagByAuthInfo(const AuthSessionInfo *info, char *msg, int32_t *compressFlag,
uint8_t *compressData, uint32_t *compressLen)
uint8_t **compressData, uint32_t *compressLen)
{
if ((info->connInfo.type != AUTH_LINK_TYPE_WIFI) && info->isSupportCompress) {
AUTH_LOGI(AUTH_FSM, "before compress, datalen=%{public}zu", strlen(msg) + 1);
if (DataCompress((uint8_t *)msg, strlen(msg) + 1, &compressData, compressLen) != SOFTBUS_OK) {
if (DataCompress((uint8_t *)msg, strlen(msg) + 1, compressData, compressLen) != SOFTBUS_OK) {
*compressFlag = FLAG_UNCOMPRESS_DEVICE_INFO;
} else {
*compressFlag = FLAG_COMPRESS_DEVICE_INFO;
@ -2115,7 +2115,7 @@ int32_t PostDeviceInfoMessage(int64_t authSeq, const AuthSessionInfo *info)
int32_t compressFlag = FLAG_UNCOMPRESS_DEVICE_INFO;
uint8_t *compressData = NULL;
uint32_t compressLen = 0;
SetCompressFlagByAuthInfo(info, msg, &compressFlag, compressData, &compressLen);
SetCompressFlagByAuthInfo(info, msg, &compressFlag, &compressData, &compressLen);
InDataInfo inDataInfo = { 0 };
uint8_t *data = NULL;
uint32_t dataLen = 0;

View File

@ -12,6 +12,7 @@
# limitations under the License.
import("../../../dsoftbus.gni")
import("../../authentication/authentication.gni")
import("../../common/dfx/dsoftbus_dfx.gni")
core_bus_center_utils_path = "$dsoftbus_root_path/core/bus_center/utils"
@ -19,7 +20,6 @@ core_bus_center_utils_path = "$dsoftbus_root_path/core/bus_center/utils"
bus_center_utils_src = [
"$core_bus_center_utils_path/src/lnn_async_callback_utils.c",
"$core_bus_center_utils_path/src/lnn_common_utils.c",
"$core_bus_center_utils_path/src/lnn_compress.c",
"$core_bus_center_utils_path/src/lnn_connection_addr_utils.c",
"$core_bus_center_utils_path/src/lnn_map.c",
]
@ -39,3 +39,9 @@ bus_center_utils_inc = [
]
bus_center_utils_deps =
[ "$dsoftbus_root_path/core/common/dfx/log:softbus_dfx_log" ]
if (enhanced) {
bus_center_utils_src += [ "$core_bus_center_utils_path/src/lnn_compress.c" ]
} else {
bus_center_utils_src +=
[ "$core_bus_center_utils_path/src/lnn_compress_virtual.c" ]
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Copyright (c) 2023 - 2024 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
@ -16,19 +16,137 @@
#include "lnn_compress.h"
#include <securec.h>
#include <zlib.h>
#include "softbus_adapter_file.h"
#include "softbus_errcode.h"
#include "softbus_feature_config.h"
#include "softbus_adapter_mem.h"
#include "lnn_log.h"
#define CHUNK 4096
#define GZIP_ENCODING 16
#define MAX_WBITS 15
#define Z_MEM_LEVEL 8
/* compress data by GZIP */
int32_t DataCompress(uint8_t *in, uint32_t inLen, uint8_t **out, uint32_t *outLen)
{
if ((in == NULL) || (inLen == 0) || (out == NULL) || (outLen == NULL)) {
LNN_LOGE(LNN_STATE, "param invalid");
return SOFTBUS_INVALID_PARAM;
}
z_stream strm;
int32_t ret = SOFTBUS_OK;
uint32_t tmpLen = compressBound(inLen);
*out = SoftBusCalloc(tmpLen);
if (*out == NULL) {
LNN_LOGE(LNN_STATE, "malloc fail.");
return SOFTBUS_MALLOC_ERR;
}
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS | GZIP_ENCODING, Z_MEM_LEVEL,
Z_DEFAULT_STRATEGY);
if (ret != Z_OK) {
SoftBusFree(*out);
*out = NULL;
LNN_LOGE(LNN_STATE, "deflateInit2 fail, ret=%{public}d", ret);
return SOFTBUS_ERR;
}
strm.avail_in = inLen;
strm.next_in = in;
strm.avail_out = tmpLen;
strm.next_out = *out;
ret = deflate(&strm, Z_FINISH);
if (ret != Z_STREAM_END) {
deflateEnd(&strm);
SoftBusFree(*out);
*out = NULL;
LNN_LOGE(LNN_STATE, "deflate fail, ret=%{public}d", ret);
return SOFTBUS_ERR;
}
*outLen = strm.total_out;
deflateEnd(&strm);
return SOFTBUS_OK;
}
static int32_t PerformInflate(z_stream *strm, uint8_t *in, uint32_t inLen, uint8_t **out, uint32_t *outLen)
{
int32_t ret = SOFTBUS_OK;
uint32_t chunk = CHUNK;
uint32_t bufferSize = chunk;
unsigned char *buffer = SoftBusCalloc(bufferSize);
if (buffer == NULL) {
LNN_LOGE(LNN_STATE, "malloc fail.");
return SOFTBUS_MALLOC_ERR;
}
strm->avail_in = inLen;
strm->next_in = in;
*outLen = 0;
do {
strm->avail_out = chunk;
if (*outLen + chunk > bufferSize) {
uint32_t newBufferSize = bufferSize * 2;
unsigned char *newBuffer = SoftBusCalloc(newBufferSize);
if (newBuffer == NULL) {
LNN_LOGE(LNN_STATE, "malloc fail.");
SoftBusFree(buffer);
return SOFTBUS_MALLOC_ERR;
}
if (memcpy_s(newBuffer, newBufferSize, buffer, *outLen) != EOK) {
LNN_LOGE(LNN_STATE, "memcpy fail.");
SoftBusFree(buffer);
SoftBusFree(newBuffer);
return SOFTBUS_MEM_ERR;
}
SoftBusFree(buffer);
buffer = newBuffer;
bufferSize = newBufferSize;
}
strm->next_out = buffer + *outLen;
ret = inflate(strm, Z_NO_FLUSH);
if (ret != Z_OK && ret != Z_STREAM_END) {
SoftBusFree(buffer);
LNN_LOGE(LNN_STATE, "inflate fail, ret=%{public}d", ret);
return SOFTBUS_ERR;
}
*outLen += chunk - strm->avail_out;
} while (strm->avail_out == 0);
if (ret != Z_STREAM_END) {
SoftBusFree(buffer);
LNN_LOGE(LNN_STATE, "performInflate fail, ret=%{public}d", ret);
return SOFTBUS_ERR;
}
*out = buffer;
return SOFTBUS_OK;
}
/* decompress data by GZIP */
int32_t DataDecompress(uint8_t *in, uint32_t inLen, uint8_t **out, uint32_t *outLen)
{
return SOFTBUS_OK;
if ((in == NULL) || (inLen == 0) || (out == NULL) || (outLen == NULL)) {
LNN_LOGE(LNN_STATE, "param invalid");
return SOFTBUS_INVALID_PARAM;
}
z_stream strm;
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
int32_t ret = inflateInit2(&strm, MAX_WBITS | GZIP_ENCODING);
if (ret != Z_OK) {
LNN_LOGE(LNN_STATE, "inflateInit2 fail, ret=%{public}d", ret);
return SOFTBUS_ERR;
}
ret = PerformInflate(&strm, in, inLen, out, outLen);
if (ret != SOFTBUS_OK) {
LNN_LOGE(LNN_STATE, "performInflate fail, ret=%{public}d", ret);
}
inflateEnd(&strm);
return ret;
}

View File

@ -0,0 +1,30 @@
/*
* Copyright (c) 2023 - 2024 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 "lnn_compress.h"
#include "softbus_errcode.h"
/* compress data by GZIP */
int32_t DataCompress(uint8_t *in, uint32_t inLen, uint8_t **out, uint32_t *outLen)
{
return SOFTBUS_OK;
}
/* decompress data by GZIP */
int32_t DataDecompress(uint8_t *in, uint32_t inLen, uint8_t **out, uint32_t *outLen)
{
return SOFTBUS_OK;
}

View File

@ -275,7 +275,10 @@ if (defined(ohos_lite)) {
}
if (enhanced) {
cflags += [ "-DENHANCED_FLAG" ]
external_deps += [ "device_certificate_manager:device_cert_mgr_sdk" ]
external_deps += [
"device_certificate_manager:device_cert_mgr_sdk",
"zlib:libz",
]
}
}

View File

@ -109,7 +109,7 @@ ohos_unittest("AuthEnhanceMockTest") {
"$dsoftbus_root_path/core/bus_center/lnn/net_builder/src/lnn_device_info_recovery_virtual.c",
"$dsoftbus_root_path/core/bus_center/lnn/net_builder/src/lnn_ptk_info_virtual.c",
"$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/src/lnn_feature_capability.c",
"$dsoftbus_root_path/core/bus_center/utils/src/lnn_compress.c",
"$dsoftbus_root_path/core/bus_center/utils/src/lnn_compress_virtual.c",
"$dsoftbus_root_path/core/bus_center/utils/src/lnn_state_machine.c",
"$dsoftbus_root_path/tests/core/bus_center/mock_common/src/lnn_connection_mock.cpp",
"$dsoftbus_root_path/tests/core/bus_center/mock_common/src/lnn_hichain_mock.cpp",
@ -214,7 +214,7 @@ ohos_unittest("AuthTestCallBackTest") {
"$dsoftbus_root_path/core/bus_center/lnn/net_builder/src/lnn_cipherkey_manager_virtual.c",
"$dsoftbus_root_path/core/bus_center/lnn/net_builder/src/lnn_device_info_recovery_virtual.c",
"$dsoftbus_root_path/core/bus_center/lnn/net_ledger/common/src/lnn_feature_capability.c",
"$dsoftbus_root_path/core/bus_center/utils/src/lnn_compress.c",
"$dsoftbus_root_path/core/bus_center/utils/src/lnn_compress_virtual.c",
"$dsoftbus_root_path/core/bus_center/utils/src/lnn_state_machine.c",
"$dsoftbus_root_path/tests/core/bus_center/mock_common/src/lnn_connection_mock.cpp",
"$dsoftbus_root_path/tests/core/bus_center/mock_common/src/lnn_hichain_mock.cpp",