diff --git a/interfaces/kits/cj/src/stream_ffi.cpp b/interfaces/kits/cj/src/stream_ffi.cpp index 16ab8c8fdcb559a34627d9549d18d3df9eadb4ab..4875fc0ef9e1c492669d73d37a3c908fe323b711 100644 --- a/interfaces/kits/cj/src/stream_ffi.cpp +++ b/interfaces/kits/cj/src/stream_ffi.cpp @@ -51,7 +51,8 @@ RetDataI64 FfiOHOSStreamWriteCur(int64_t id, const char* buffer, int64_t length, LOGE("Stream instance not exist %{public}" PRId64, id); return ret; } - auto [state, writeLen] = instance->WriteCur(buffer, length, encode); + auto [state, writeLen] = + instance->WriteCur(reinterpret_cast(const_cast(buffer)), length, encode); ret.code = state; if (state != SUCCESS_CODE) { ret.data = 0; @@ -70,7 +71,8 @@ RetDataI64 FfiOHOSStreamWrite(int64_t id, const char* buffer, int64_t length, in LOGE("Stream instance not exist %{public}" PRId64, id); return ret; } - auto [state, writeLen] = instance->Write(buffer, length, offset, encode); + auto [state, writeLen] = + instance->Write(reinterpret_cast(const_cast(buffer)), length, offset, encode); ret.code = state; if (state != SUCCESS_CODE) { ret.data = 0; diff --git a/interfaces/kits/cj/src/stream_impl.cpp b/interfaces/kits/cj/src/stream_impl.cpp index a26b4a2fc33cf833d94ed41c1ca3c1f7abd6cfbf..4b91b19b3881a10e96a1434a0dad2acc4b50aea0 100644 --- a/interfaces/kits/cj/src/stream_impl.cpp +++ b/interfaces/kits/cj/src/stream_impl.cpp @@ -14,10 +14,10 @@ */ #include "stream_impl.h" -#include "securec.h" - #include #include +#include "securec.h" +#include "utils.h" using namespace std; @@ -25,24 +25,6 @@ namespace OHOS { namespace CJSystemapi { namespace FileFs { -std::tuple, size_t> DecodeString(const std::string& buffer, const std::string& encode) -{ - std::unique_ptr buf = std::make_unique(buffer.length() + 1); - if (!buf) { - return { ENOMEM, nullptr, 0}; - } - - for (size_t i = 0; i < buffer.length(); i++) { - buf[i] = buffer[i]; - } - - if (encode == "utf-8") { - return make_tuple(SUCCESS_CODE, move(buf), buffer.length()); - } else { - return { EINVAL, nullptr, 0}; - } -} - std::tuple GetActualLen(size_t bufLen, size_t bufOff, int64_t offset, int64_t length) { size_t retLen = bufLen - bufOff; @@ -59,37 +41,6 @@ std::tuple GetActualLen(size_t bufLen, size_t bufOff, int64_t offse return { SUCCESS_CODE, retLen }; } -tuple, void *, size_t, int64_t> GetWriteArg(const std::string& buffer, int64_t length, - int64_t offset, const std::string& encode) -{ - void *buf = nullptr; - - auto [decodeState, bufferGuard, bufLen] = DecodeString(buffer, encode); - if (decodeState != SUCCESS_CODE) { - LOGE("Illegal write buffer or encoding"); - return { decodeState, nullptr, nullptr, 0, 0 }; - } else { - buf = bufferGuard.get(); - } - if (bufLen > UINT_MAX) { - LOGE("The Size of buffer is too large"); - return { false, nullptr, nullptr, 0, 0 }; - } - - auto [lenState, retLen] = GetActualLen(bufLen, 0, offset, length); - if (lenState != SUCCESS_CODE) { - LOGE("Failed to get actual length"); - return { lenState, nullptr, nullptr, 0, 0 }; - } - - if (offset < 0) { - LOGE("option.offset shall be positive number"); - return { EINVAL, nullptr, nullptr, 0, 0 }; - } - - return { SUCCESS_CODE, move(bufferGuard), buf, retLen, offset }; -} - tuple, size_t, int64_t> GetReadArg(size_t bufLen, int64_t length, int64_t offset) { std::unique_ptr buf = std::make_unique(bufLen); @@ -193,13 +144,13 @@ tuple StreamImpl::Read(uint8_t* buffer, size_t buLen, int64_t leng return ReadImpl(buf, len, filp, buffer); } -tuple StreamImpl::WriteCur(const std::string& buffer, int64_t length, const std::string& encode) +tuple StreamImpl::WriteCur(void* buffer, int64_t length, const std::string& encode) { FILE *filp = nullptr; filp = fp_.get(); - auto [state, bufGuard, buf, len, offsetResult] = - GetWriteArg(buffer, length, 0.0, encode); + auto [state, buf, len, offsetResult] = + CommonFunc::GetWriteArg(buffer, length, 0, encode); if (state != SUCCESS_CODE) { LOGE("Failed to resolve buf and options"); return {GetErrorCode(state), 0}; @@ -213,14 +164,13 @@ tuple StreamImpl::WriteCur(const std::string& buffer, int64_t leng return {SUCCESS_CODE, static_cast(writeLen)}; } -tuple StreamImpl::Write(const std::string& buffer, int64_t length, int64_t offset, - const std::string& encode) +tuple StreamImpl::Write(void* buffer, int64_t length, int64_t offset, const std::string& encode) { FILE *filp = nullptr; filp = fp_.get(); - auto [state, bufGuard, buf, len, offsetResult] = - GetWriteArg(buffer, length, offset, encode); + auto [state, buf, len, offsetResult] = + CommonFunc::GetWriteArg(buffer, length, offset, encode); if (state != SUCCESS_CODE) { LOGE("Failed to resolve buf and options"); return {GetErrorCode(state), 0}; diff --git a/interfaces/kits/cj/src/stream_impl.h b/interfaces/kits/cj/src/stream_impl.h index 3cdfd967be73c1e5149f77c89138d6107e1b13a1..4840fcc13401a8c35dfffe994846be423e75d0a9 100644 --- a/interfaces/kits/cj/src/stream_impl.h +++ b/interfaces/kits/cj/src/stream_impl.h @@ -25,8 +25,6 @@ namespace OHOS { namespace CJSystemapi { namespace FileFs { -std::tuple, void *, size_t, int64_t> GetWriteArg(const std::string& buffer, int64_t length, - int64_t offset, const std::string& encode); std::tuple, size_t, int64_t> GetReadArg(size_t bufLen, int64_t length, int64_t offset); class StreamImpl : public OHOS::FFI::FFIData { @@ -41,10 +39,9 @@ public: std::tuple Read(uint8_t* buffer, size_t buLen, int64_t length, int64_t offset); - std::tuple WriteCur(const std::string& buffer, int64_t length, const std::string& encode); + std::tuple WriteCur(void* buffer, int64_t length, const std::string& encode); - std::tuple Write(const std::string& buffer, int64_t length, int64_t offset, - const std::string& encode); + std::tuple Write(void* buffer, int64_t length, int64_t offset, const std::string& encode); OHOS::FFI::RuntimeType* GetRuntimeType() override { return GetClassType(); }