From f1977dd635dbbea51de4545ec3e4b34d3d8359d5 Mon Sep 17 00:00:00 2001 From: chensi10 Date: Mon, 13 Sep 2021 10:48:55 +0800 Subject: [PATCH] L2 and L3 alignment Signed-off-by: chensi10 --- .../src/ohos/aafwk/content/want_params.cpp | 209 ++++++++++++++++-- .../include/ohos/aafwk/content/want_params.h | 32 ++- 2 files changed, 224 insertions(+), 17 deletions(-) diff --git a/frameworks/kits/content/cpp/src/ohos/aafwk/content/want_params.cpp b/frameworks/kits/content/cpp/src/ohos/aafwk/content/want_params.cpp index bb5cabb7556..51a1b05f636 100755 --- a/frameworks/kits/content/cpp/src/ohos/aafwk/content/want_params.cpp +++ b/frameworks/kits/content/cpp/src/ohos/aafwk/content/want_params.cpp @@ -30,11 +30,81 @@ #include "want_params_wrapper.h" #include "ohos/aafwk/base/zchar_wrapper.h" #include "app_log_wrapper.h" +#include "securec.h" using namespace OHOS::AppExecFwk; namespace OHOS { namespace AAFwk { +UnsupportedData::~UnsupportedData() +{ + if (buffer != nullptr) { + delete[] buffer; + buffer = nullptr; + } +} + +UnsupportedData::UnsupportedData() = default; + +UnsupportedData::UnsupportedData(const UnsupportedData &other) : key(other.key), type(other.type), size(other.size) +{ + buffer = new uint8_t[size]; + if (memcpy_s(buffer, size, other.buffer, size) != EOK) { + APP_LOGI("copy construct fail due to memcpy"); + + key.clear(); + type = 0; + size = 0; + delete[] buffer; + buffer = nullptr; + } +} + +UnsupportedData::UnsupportedData(UnsupportedData &&other) + : key(std::move(other.key)), type(other.type), size(other.size), buffer(other.buffer) +{ + other.type = 0; + other.size = 0; + other.buffer = nullptr; +} + +UnsupportedData &UnsupportedData::operator=(const UnsupportedData &other) +{ + if (this == &other) { + return *this; + } + key = other.key; + type = other.type; + size = other.size; + buffer = new uint8_t[size]; + if (memcpy_s(buffer, size, other.buffer, size) != EOK) { + APP_LOGI("copy assignment fail due to memcpy"); + + key.clear(); + type = 0; + size = 0; + delete[] buffer; + buffer = nullptr; + } + return *this; +} + +UnsupportedData &UnsupportedData::operator=(UnsupportedData &&other) +{ + key = std::move(other.key); + type = other.type; + size = other.size; + std::swap(buffer, other.buffer); + + other.type = 0; + other.size = 0; + if (other.buffer) { + delete[] other.buffer; + other.buffer = nullptr; + } + return *this; +} + std::string WantParams::GetStringByType(const sptr iIt, int typeId) { if (typeId == VALUE_TYPE_BOOLEAN) { @@ -461,8 +531,6 @@ bool WantParams::WriteMarshalling(Parcel &parcel, sptr &o) const return WriteToParcelLong(parcel, o); } else if (IFloat::Query(o) != nullptr) { return WriteToParcelFloat(parcel, o); - } else if (IWantParams::Query(o) != nullptr) { - return WriteToParcelWantParams(parcel, o); } else if (IDouble::Query(o) != nullptr) { return WriteToParcelDouble(parcel, o); } else { @@ -476,14 +544,13 @@ bool WantParams::WriteMarshalling(Parcel &parcel, sptr &o) const } } -/** - * @description: Marshals an IntentParams object into a Parcel. - * @param Key-value pairs in the IntentParams are marshalled separately. - * @return If any key-value pair fails to be marshalled, false is returned. - */ -bool WantParams::Marshalling(Parcel &parcel) const +bool WantParams::DoMarshalling(Parcel &parcel) const { size_t size = params_.size(); + if (!cachedUnsupportedData_.empty()) { + size += cachedUnsupportedData_.size(); + } + if (!parcel.WriteInt32(size)) { return false; } @@ -499,10 +566,61 @@ bool WantParams::Marshalling(Parcel &parcel) const if (!WriteMarshalling(parcel, o)) { return false; } - iter++; } + if (!cachedUnsupportedData_.empty()) { + for (const UnsupportedData &data : cachedUnsupportedData_) { + if (!parcel.WriteString16(data.key)) { + return false; + } + if (!parcel.WriteInt32(data.type)) { + return false; + } + if (!parcel.WriteInt32(data.size)) { + return false; + } + // Corresponding to Parcel#writeByteArray() in Java. + if (!parcel.WriteInt32(data.size)) { + return false; + } + if (!parcel.WriteBuffer(data.buffer, data.size)) { + return false; + } + } + } + return true; +} + +/** + * @description: Marshals an IntentParams object into a Parcel. + * @param Key-value pairs in the IntentParams are marshalled separately. + * @return If any key-value pair fails to be marshalled, false is returned. + */ +bool WantParams::Marshalling(Parcel &parcel) const +{ + Parcel tempParcel; + if (!DoMarshalling(tempParcel)) { + return false; + } + + int size = static_cast(tempParcel.GetDataSize()); + if (!parcel.WriteInt32(size)) { + return false; + } + const uint8_t *buffer = tempParcel.ReadUnpadBuffer(size); + if (buffer == nullptr) { + return false; + } + + // Corresponding to Parcel#writeByteArray() in Java. + if (!parcel.WriteInt32(size)) { + return false; + } + if (!parcel.WriteBuffer(buffer, size)) { + return false; + } + return true; } @@ -813,6 +931,7 @@ bool WantParams::ReadArrayToParcel(Parcel &parcel, int type, sptr &ao) { switch (type) { case VALUE_TYPE_STRINGARRAY: + case VALUE_TYPE_CHARSEQUENCEARRAY: return ReadFromParcelArrayString(parcel, ao); case VALUE_TYPE_BOOLEANARRAY: return ReadFromParcelArrayBool(parcel, ao); @@ -831,8 +950,7 @@ bool WantParams::ReadArrayToParcel(Parcel &parcel, int type, sptr &ao) case VALUE_TYPE_DOUBLEARRAY: return ReadFromParcelArrayDouble(parcel, ao); default: - // ignore - ; + break; } return true; @@ -968,9 +1086,46 @@ bool WantParams::ReadFromParcelDouble(Parcel &parcel, const std::string &key) } } +bool WantParams::ReadUnsupportedData(Parcel &parcel, const std::string &key, int type) +{ + int bufferSize = 0; + if (!parcel.ReadInt32(bufferSize)) { + return false; + } + if (bufferSize < 0) { + return false; + } + + // Corresponding to Parcel#writeByteArray() in Java. + int32_t length = 0; + if (!parcel.ReadInt32(length)) { + return false; + } + const uint8_t *bufferP = parcel.ReadUnpadBuffer(bufferSize); + if (bufferP == nullptr) { + return false; + } + + UnsupportedData data; + data.key = Str8ToStr16(key); + data.type = type; + data.size = bufferSize; + data.buffer = new (std::nothrow) uint8_t[bufferSize]; + if (data.buffer == nullptr) { + return false; + } + + if (memcpy_s(data.buffer, bufferSize, bufferP, bufferSize) != EOK) { + return false; + } + cachedUnsupportedData_.emplace_back(std::move(data)); + return true; +} + bool WantParams::ReadFromParcelParam(Parcel &parcel, const std::string &key, int type) { switch (type) { + case VALUE_TYPE_CHARSEQUENCE: case VALUE_TYPE_STRING: return ReadFromParcelString(parcel, key); case VALUE_TYPE_BOOLEAN: @@ -989,10 +1144,16 @@ bool WantParams::ReadFromParcelParam(Parcel &parcel, const std::string &key, int return ReadFromParcelFloat(parcel, key); case VALUE_TYPE_DOUBLE: return ReadFromParcelDouble(parcel, key); - case VALUE_TYPE_WANTPARAMS: - return ReadFromParcelWantParamWrapper(parcel, key); case VALUE_TYPE_NULL: break; + case VALUE_TYPE_PARCELABLE: + case VALUE_TYPE_PARCELABLEARRAY: + case VALUE_TYPE_SERIALIZABLE: + case VALUE_TYPE_LIST: + if (!ReadUnsupportedData(parcel, key, type)) { + return false; + } + break; default: { // handle array sptr ao = nullptr; @@ -1039,8 +1200,28 @@ bool WantParams::ReadFromParcel(Parcel &parcel) */ WantParams *WantParams::Unmarshalling(Parcel &parcel) { + int32_t bufferSize; + if (!parcel.ReadInt32(bufferSize)) { + return nullptr; + } + + // Corresponding to Parcel#writeByteArray() in Java. + int32_t length; + if (!parcel.ReadInt32(length)) { + return nullptr; + } + const uint8_t *dataInBytes = parcel.ReadUnpadBuffer(bufferSize); + if (dataInBytes == nullptr) { + return nullptr; + } + + Parcel tempParcel; + if (!tempParcel.WriteBuffer(dataInBytes, bufferSize)) { + return nullptr; + } + WantParams *wantParams = new (std::nothrow) WantParams(); - if (wantParams != nullptr && !wantParams->ReadFromParcel(parcel)) { + if (wantParams != nullptr && !wantParams->ReadFromParcel(tempParcel)) { delete wantParams; wantParams = nullptr; } diff --git a/interfaces/innerkits/want/include/ohos/aafwk/content/want_params.h b/interfaces/innerkits/want/include/ohos/aafwk/content/want_params.h index eaea0d69349..dac9a378195 100755 --- a/interfaces/innerkits/want/include/ohos/aafwk/content/want_params.h +++ b/interfaces/innerkits/want/include/ohos/aafwk/content/want_params.h @@ -18,13 +18,29 @@ #include #include #include - +#include #include "ohos/aafwk/base/base_interfaces.h" #include "refbase.h" #include "parcel.h" namespace OHOS { namespace AAFwk { +class UnsupportedData { +public: + std::u16string key; + int type = 0; + int size = 0; + uint8_t *buffer = nullptr; + + ~UnsupportedData(); + + UnsupportedData(); + UnsupportedData(const UnsupportedData &other); + UnsupportedData(UnsupportedData &&other); + + UnsupportedData &operator=(const UnsupportedData &other); + UnsupportedData &operator=(UnsupportedData &&other); +}; class WantParams final : public Parcelable { public: @@ -89,8 +105,14 @@ private: VALUE_TYPE_DOUBLEARRAY = 18, VALUE_TYPE_STRINGARRAY = 19, VALUE_TYPE_CHARSEQUENCEARRAY = 20, - VALUE_TYPE_WANTPARAMS = 21, - VALUE_TYPE_ARRAY = 22, + + VALUE_TYPE_PARCELABLE = 21, + VALUE_TYPE_PARCELABLEARRAY = 22, + VALUE_TYPE_SERIALIZABLE = 23, + VALUE_TYPE_LIST = 50, + + VALUE_TYPE_WANTPARAMS = 101, + VALUE_TYPE_ARRAY = 102, }; bool WriteArrayToParcel(Parcel &parcel, IArray *ao) const; @@ -141,11 +163,15 @@ private: bool WriteToParcelDouble(Parcel &parcel, sptr &o) const; bool WriteToParcelWantParams(Parcel &parcel, sptr &o) const; + bool DoMarshalling(Parcel &parcel) const; + bool ReadUnsupportedData(Parcel &parcel, const std::string &key, int type); + friend class WantParamWrapper; // inner use function bool NewArrayData(IArray *source, sptr &dest); bool NewParams(const WantParams &source, WantParams &dest); std::map> params_; + std::vector cachedUnsupportedData_; }; } // namespace AAFwk } // namespace OHOS -- Gitee