diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp index f763d20d34a47f6b3353ddac5bfa727bc3f798eb..2158affa107552558929185f2bf40245ad98b358 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.cpp @@ -22,6 +22,7 @@ #include "log_print.h" #include "securec.h" #include "upgrade_manager.h" +#include "utils/endian_converter.h" namespace OHOS::DistributedData { using namespace OHOS::DistributedKv; @@ -125,10 +126,10 @@ bool RouteHeadHandlerImpl::PackDataHead(uint8_t *data, uint32_t totalLen) return false; } RouteHead *head = reinterpret_cast(ptr); - head->magic = RouteHead::MAGIC_NUMBER; - head->version = RouteHead::VERSION; - head->checkSum = 0; - head->dataLen = static_cast(totalLen - sizeof(RouteHead)); + head->magic = HostToNet(RouteHead::MAGIC_NUMBER); + head->version = HostToNet(RouteHead::VERSION); + head->checkSum = HostToNet(uint64_t(0)); + head->dataLen = HostToNet(uint32_t(totalLen - sizeof(RouteHead))); return true; } @@ -149,16 +150,17 @@ bool RouteHeadHandlerImpl::PackDataBody(uint8_t *data, uint32_t totalLen) ptr += sizeof(SessionDevicePair); SessionUserPair *userPair = reinterpret_cast(ptr); - userPair->sourceUserId = session_.sourceUserId; + userPair->sourceUserId = HostToNet(session_.sourceUserId); userPair->targetUserCount = session_.targetUserIds.size(); for (size_t i = 0; i < session_.targetUserIds.size(); ++i) { - *(userPair->targetUserIds + i) = session_.targetUserIds[i]; + *(userPair->targetUserIds + i) = HostToNet(session_.targetUserIds[i]); } ptr += (sizeof(SessionUserPair) + session_.targetUserIds.size() * sizeof(int)); SessionAppId *appPair = reinterpret_cast(ptr); - appPair->len = data + totalLen - ptr; // left size - ret = strcpy_s(appPair->appId, data + totalLen - ptr, session_.appId.c_str()); + uint32_t appLen = data + totalLen - ptr; + appPair->len = HostToNet(appLen); // left size + ret = strcpy_s(appPair->appId, appLen, session_.appId.c_str()); if (ret != 0) { ZLOGE("strcpy for app id failed"); return false; @@ -196,33 +198,37 @@ bool RouteHeadHandlerImpl::UnPackData(const uint8_t *data, uint32_t totalLen, ui return false; } unpackedSize = 0; - const RouteHead *head = UnPackHeadHead(data, totalLen); - if (head != nullptr && head->version == RouteHead::VERSION) { - auto isOk = UnPackHeadBody(data + sizeof(RouteHead), totalLen - sizeof(RouteHead)); + RouteHead head = { 0 }; + bool result = UnPackDataHead(data, totalLen, head); + if (result && head.version == RouteHead::VERSION) { + auto isOk = UnPackDataBody(data + sizeof(RouteHead), totalLen - sizeof(RouteHead)); if (isOk) { - unpackedSize = sizeof(RouteHead) + head->dataLen; + unpackedSize = sizeof(RouteHead) + head.dataLen; } return isOk; } return false; } -const RouteHead *RouteHeadHandlerImpl::UnPackHeadHead(const uint8_t *data, uint32_t totalLen) +bool RouteHeadHandlerImpl::UnPackDataHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead) { - const uint8_t *ptr = data; - const RouteHead *head = reinterpret_cast(ptr); - if (head->magic != RouteHead::MAGIC_NUMBER) { + const RouteHead *head = reinterpret_cast(data); + routeHead.magic = NetToHost(head->magic); + routeHead.version = NetToHost(head->version); + routeHead.checkSum = NetToHost(head->checkSum); + routeHead.dataLen = NetToHost(head->dataLen); + if (routeHead.magic != RouteHead::MAGIC_NUMBER) { ZLOGW("not route head data"); - return nullptr; + return false; } - if (head->dataLen + sizeof(RouteHead) > totalLen) { + if (routeHead.dataLen + sizeof(RouteHead) > totalLen) { ZLOGE("invalid route head len"); - return nullptr; + return false; } - return head; + return true; } -bool RouteHeadHandlerImpl::UnPackHeadBody(const uint8_t *data, uint32_t totalLen) +bool RouteHeadHandlerImpl::UnPackDataBody(const uint8_t *data, uint32_t totalLen) { const uint8_t *ptr = data; uint32_t leftSize = totalLen; @@ -242,14 +248,14 @@ bool RouteHeadHandlerImpl::UnPackHeadBody(const uint8_t *data, uint32_t totalLen return false; } const SessionUserPair *userPair = reinterpret_cast(ptr); - session_.sourceUserId = userPair->sourceUserId; + session_.sourceUserId = NetToHost(userPair->sourceUserId); if (leftSize < sizeof(SessionUserPair) + userPair->targetUserCount * sizeof(int)) { ZLOGE("failed to parse user pair, target user"); return false; } for (int i = 0; i < userPair->targetUserCount; ++i) { - session_.targetUserIds.push_back(*(userPair->targetUserIds + i)); + session_.targetUserIds.push_back(NetToHost(*(userPair->targetUserIds + i))); } ptr += sizeof(SessionUserPair) + userPair->targetUserCount * sizeof(int); @@ -258,12 +264,12 @@ bool RouteHeadHandlerImpl::UnPackHeadBody(const uint8_t *data, uint32_t totalLen return false; } const SessionAppId *appId = reinterpret_cast(ptr); - - if (leftSize < sizeof(SessionAppId) + appId->len) { + auto appIdLen = NetToHost(appId->len); + if (leftSize < sizeof(SessionAppId) + appIdLen) { ZLOGE("failed to parse app id"); return false; } - session_.appId.append(appId->appId, appId->len); + session_.appId.append(appId->appId, appIdLen); return true; } } // namespace OHOS::DistributedData \ No newline at end of file diff --git a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h index 395e4d3361a2adf6963ff5305cc281bc8aaedd02..3deb9ed9eab6e87caed3ba32832afa1213e15369 100644 --- a/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h +++ b/services/distributeddataservice/app/src/session_manager/route_head_handler_impl.h @@ -44,9 +44,9 @@ struct SessionDevicePair { }; struct SessionUserPair { - int sourceUserId; + uint32_t sourceUserId; uint8_t targetUserCount; - int targetUserIds[0]; + uint32_t targetUserIds[0]; }; struct SessionAppId { @@ -69,8 +69,8 @@ private: bool PackDataHead(uint8_t *data, uint32_t totalLen); bool PackDataBody(uint8_t *data, uint32_t totalLen); bool UnPackData(const uint8_t *data, uint32_t totalLen, uint32_t &unpackedSize); - const RouteHead *UnPackHeadHead(const uint8_t *data, uint32_t totalLen); - bool UnPackHeadBody(const uint8_t *data, uint32_t totalLen); + bool UnPackDataHead(const uint8_t *data, uint32_t totalLen, RouteHead &routeHead); + bool UnPackDataBody(const uint8_t *data, uint32_t totalLen); std::string userId_; std::string appId_; diff --git a/services/distributeddataservice/app/src/session_manager/session_manager.h b/services/distributeddataservice/app/src/session_manager/session_manager.h index 35553973825be8bb6434d8068199a2adafb2c718..17909f7de43c92a4cc5a8bd0ca7f56e80fb4bdf3 100644 --- a/services/distributeddataservice/app/src/session_manager/session_manager.h +++ b/services/distributeddataservice/app/src/session_manager/session_manager.h @@ -24,7 +24,7 @@ namespace OHOS::DistributedData { struct SessionPoint { std::string deviceId; - int userId; + uint32_t userId; std::string appId; }; @@ -32,8 +32,8 @@ class Session : public Serializable { public: std::string sourceDeviceId; std::string targetDeviceId; - int32_t sourceUserId; - std::vector targetUserIds; + uint32_t sourceUserId; + std::vector targetUserIds; std::string appId; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; diff --git a/services/distributeddataservice/framework/include/metadata/user_meta_data.h b/services/distributeddataservice/framework/include/metadata/user_meta_data.h index 7ed2b61a2568ee2bcd9f076d85988bbcd42c7977..09353c657848351762f61905a74c828e90200f11 100644 --- a/services/distributeddataservice/framework/include/metadata/user_meta_data.h +++ b/services/distributeddataservice/framework/include/metadata/user_meta_data.h @@ -22,7 +22,7 @@ namespace OHOS::DistributedData { class API_EXPORT UserStatus final : public Serializable { public: - int id; + int32_t id; bool isActive; API_EXPORT UserStatus() = default; API_EXPORT ~UserStatus() = default; diff --git a/services/distributeddataservice/framework/include/utils/endian_converter.h b/services/distributeddataservice/framework/include/utils/endian_converter.h new file mode 100644 index 0000000000000000000000000000000000000000..faf4c0a587321a50ebda1a315b6956614f1f87f4 --- /dev/null +++ b/services/distributeddataservice/framework/include/utils/endian_converter.h @@ -0,0 +1,53 @@ +/* +* Copyright (c) 2022 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 DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H +#define DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H + +#include + +namespace OHOS::DistributedData { +// use little endian byteorder by default +static inline uint16_t HostToNet(uint16_t value) +{ + return htole16(value); +} + +static inline uint16_t NetToHost(uint16_t value) +{ + return le16toh(value); +} + +static inline uint32_t HostToNet(uint32_t value) +{ + return htole32(value); +} + +static inline uint32_t NetToHost(uint32_t value) +{ + return le32toh(value); +} + +static inline uint64_t HostToNet(uint64_t value) +{ + return htole64(value); +} + +static inline uint64_t NetToHost(uint64_t value) +{ + return le64toh(value); +} +} // namespace OHOS::DistributedData +#endif // DISTRIBUTEDDATAMGR_ENDIAN_CONVERTER_H