+++ /dev/null
-/*
- * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
- *
- * 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 <cstring>
-
-#include "parcel-internal.hh"
-
-namespace rpc_port {
-
-Parcel::Parcel(const unsigned char* buf, unsigned int size)
- : data_(buf, buf + size) {}
-
-void Parcel::WriteByte(char b) {
- Write<char>(b);
-}
-
-void Parcel::WriteInt16(short i) {
- Write<short>(i);
-}
-
-void Parcel::WriteInt32(int i) {
- Write<int>(i);
-}
-
-void Parcel::WriteInt64(long long i) {
- Write<long long>(i);
-}
-
-void Parcel::WriteFloat(float f) {
- Write<float>(f);
-}
-
-void Parcel::WriteDouble(double d) {
- Write<double>(d);
-}
-
-void Parcel::WriteString(const char* str) {
- int l = std::strlen(str) + 1;
-
- Write<int>(l);
- std::copy(str, str + l, std::back_inserter(data_));
-}
-
-void Parcel::WriteBool(bool b) {
- Write<bool>(b);
-}
-
-void Parcel::WriteBundle(bundle* b) {
- bundle_raw *r;
- int len;
- bundle_encode(b, &r, &len);
- unsigned char* c = reinterpret_cast<unsigned char*>(r);
-
- Write<int>(len + 1);
- std::copy(c, c + len + 1, std::back_inserter(data_));
- free(r);
-}
-
-void Parcel::WriteArrayCount(int count) {
- Write<int>(count);
-}
-
-char Parcel::ReadByte() {
- return Read<char>();
-}
-
-short Parcel::ReadInt16() {
- return Read<short>();
-}
-
-int Parcel::ReadInt32() {
- return Read<int>();
-}
-
-long long Parcel::ReadInt64() {
- return Read<long long>();
-}
-
-float Parcel::ReadFloat() {
- return Read<float>();
-}
-
-double Parcel::ReadDouble() {
- return Read<double>();
-}
-
-const char* Parcel::ReadString() {
- int l = Read<int>();
-
- if (l <= 0)
- return nullptr;
- if (reader_ + l > data_.size())
- return nullptr;
- if (data_[reader_ + l - 1] != 0)
- return nullptr;
- const char* str = reinterpret_cast<const char*>(&data_[reader_]);
- reader_ += l;
-
- return str;
-}
-
-bool Parcel::ReadBool() {
- return Read<bool>();
-}
-
-bundle* Parcel::ReadBundle() {
- int l = Read<int>();
-
- if (l <= 0)
- return nullptr;
- if (reader_ + l > data_.size())
- return nullptr;
- if (data_[reader_ + l - 1] != 0)
- return nullptr;
- const bundle_raw* str = reinterpret_cast<const bundle_raw*>(&data_[reader_]);
- reader_ += l;
-
- bundle* b = bundle_decode(str, l - 1);
-
- return b;
-}
-
-int Parcel::ReadArrayCount() {
- return Read<int>();
-}
-
-const std::vector<unsigned char>& Parcel::GetRaw() {
- return data_;
-}
-
-void Parcel::Write(const unsigned char* buf, unsigned int size) {
- std::copy(buf, buf + size, std::back_inserter(data_));
-}
-
-void Parcel::Read(unsigned char* buf, unsigned int size) {
- if (reader_ + size > data_.size())
- return;
- std::copy(&data_[reader_], &data_[reader_] + size, buf);
- reader_ += size;
-}
-
-// LCOV_EXCL_START
-void Parcel::ResetReader() {
- reader_ = 0;
-}
-// LCOV_EXCL_STOP
-
-// LCOV_EXCL_START
-void Parcel::Clear() {
- data_.clear();
- reader_ = 0;
-}
-// LCOV_EXCL_STOP
-
-// LCOV_EXCL_START
-void Parcel::Reset(const unsigned char* buf, unsigned int size) {
- Clear();
- Write(buf, size);
-}
-// LCOV_EXCL_STOP
-
-template<typename T>
-void Parcel::Write(T d) {
- unsigned char* p = reinterpret_cast<unsigned char*>(&d);
-
- std::copy(p, p + sizeof(T), std::back_inserter(data_));
-}
-
-template<typename T>
-T Parcel::Read() {
- T d = 0;
- unsigned char* p = reinterpret_cast<unsigned char*>(&d);
-
- if (reader_ + sizeof(T) > data_.size())
- return d;
- std::copy(&data_[reader_], &data_[reader_] + sizeof(T), p);
- reader_ += sizeof(T);
- return d;
-}
-
-
-} // namespace rpc_port
/*
- * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2018 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* limitations under the License.
*/
+#include <parcel.h>
+#include <stdint.h>
#include <string.h>
-#include "rpc-port-parcel.h"
-
-#include "parcel-internal.hh"
+#include "log-private.hh"
#include "port-internal.hh"
+#include "rpc-port-parcel.h"
#define MAX_PARCEL_SIZE (1024 * 1024 * 10)
using namespace rpc_port;
+static int __convert_parcel_error(int error) {
+ switch (error) {
+ case PARCEL_ERROR_ILLEGAL_BYTE_SEQ:
+ case PARCEL_ERROR_NO_DATA:
+ return RPC_PORT_ERROR_IO_ERROR;
+ case PARCEL_ERROR_OUT_OF_MEMORY:
+ return RPC_PORT_ERROR_OUT_OF_MEMORY;
+ default:
+ return RPC_PORT_ERROR_INVALID_PARAMETER;
+ }
+}
+
RPC_API int rpc_port_parcel_create(rpc_port_parcel_h* h) {
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = new Parcel();
-
- *h = p;
+ parcel_h parcel = nullptr;
+ int ret = parcel_create(&parcel);
+ if (ret != PARCEL_ERROR_NONE)
+ return __convert_parcel_error(ret);
+ *h = static_cast<rpc_port_parcel_h>(parcel);
return RPC_PORT_ERROR_NONE;
}
if (len <= 0 || len > MAX_PARCEL_SIZE)
return RPC_PORT_ERROR_IO_ERROR;
+
buf = new unsigned char[len];
ret = rpc_port_read(port, buf, len);
if (ret != 0) {
- delete[] buf; // LCOV_EXCL_LINE
- return ret; // LCOV_EXCL_LINE
+ delete[] buf;
+ return ret;
}
}
- Parcel* p = new Parcel(buf, len);
+ parcel_h parcel = nullptr;
+ int ret = parcel_create(&parcel);
+ if (ret != PARCEL_ERROR_NONE)
+ return RPC_PORT_ERROR_IO_ERROR;
+ parcel_burst_write(parcel, buf, len);
delete[] buf;
- *h = p;
-
+ *h = static_cast<rpc_port_parcel_h>(parcel);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || port == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
- int len = p->GetRaw().size();
+ parcel_h parcel = static_cast<parcel_h>(h);
+ void* raw = nullptr;
+ uint32_t len = 0;
+ parcel_get_raw(parcel, &raw, &len);
if (len <= 0)
return RPC_PORT_ERROR_INVALID_PARAMETER;
internal::Port* pt = static_cast<internal::Port*>(port);
{
std::lock_guard<std::recursive_mutex> lock(pt->GetMutex());
- int ret = rpc_port_write(port, &len, 4);
+ int ret = rpc_port_write(port, &len, sizeof(len));
if (ret != 0)
return ret;
- ret = rpc_port_write(port, &*(p->GetRaw().cbegin()), len);
+ ret = rpc_port_write(port, raw, len);
if (ret != 0)
return ret;
}
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- delete static_cast<Parcel*>(h);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_destroy(parcel);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteByte(b);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_byte(parcel, b);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteInt16(i);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_int16(parcel, i);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteInt32(i);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_int32(parcel, i);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteInt64(i);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_int64(parcel, i);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteFloat(f);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_float(parcel, f);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteDouble(d);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_double(parcel, d);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || str == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteString(str);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_string(parcel, str);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteBool(b);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_bool(parcel, b);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || b == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteBundle(b);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_bundle(parcel, b);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->WriteArrayCount(count);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_write_int32(parcel, count);
return RPC_PORT_ERROR_NONE;
}
return RPC_PORT_ERROR_INVALID_PARAMETER;
parcelable->to(h, data);
-
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || b == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- *b = p->ReadByte();
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_byte(parcel, b);
+ if (ret != PARCEL_ERROR_NONE)
+ _E("parcel_read_byte() is failed. error(%d)", ret);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || i == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- *i = p->ReadInt16();
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_int16(parcel, i);
+ if (ret != PARCEL_ERROR_NONE)
+ _E("parcel_read_int16() is failed. error(%d)", ret);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || i == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- *i = p->ReadInt32();
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_int32(parcel, i);
+ if (ret != PARCEL_ERROR_NONE)
+ _E("parcel_read_int32() is failed. error(%d)", ret);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || i == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- *i = p->ReadInt64();
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_int64(parcel, i);
+ if (ret != PARCEL_ERROR_NONE)
+ _E("parcel_read_int64() is failed. error(%d)", ret);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || f == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- *f = p->ReadFloat();
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_float(parcel, f);
+ if (ret != PARCEL_ERROR_NONE)
+ _E("parcel_read_float() is failed. error(%d)", ret);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || d == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- *d = p->ReadDouble();
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_double(parcel, d);
+ if (ret != PARCEL_ERROR_NONE)
+ _E("parcel_read_double() is failed. error(%d)", ret);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || str == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
- const char* read = p->ReadString();
- *str = read == nullptr ? strdup("") : strdup(read);
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_string(parcel, str);
+ if (ret != PARCEL_ERROR_NONE) {
+ _E("parcel_read_string() is failed. error(%d)", ret);
+ *str = strdup("");
+ }
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || b == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- *b = p->ReadBool();
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_bool(parcel, b);
+ if (ret != PARCEL_ERROR_NONE)
+ _E("parcel_read_bool() is failed. error(%d)", ret);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || b == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
- bundle* read = p->ReadBundle();
-
- *b = read == nullptr ? bundle_create() : read;
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_bundle(parcel, b);
+ if (ret != PARCEL_ERROR_NONE) {
+ _E("parcel_read_bundle() is failed. error(%d)", ret);
+ *b = bundle_create();
+ }
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || count == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- *count = p->ReadArrayCount();
+ parcel_h parcel = static_cast<parcel_h>(h);
+ int ret = parcel_read_int32(parcel, count);
+ if (ret != PARCEL_ERROR_NONE)
+ _E("parcel_read_int32() is failed. error(%d)", ret);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || buf == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->Read(buf, size);
+ parcel_h parcel = static_cast<parcel_h>(h);
+ uint32_t valid_size = size & UINT32_MAX;
+ int ret = parcel_burst_read(parcel, static_cast<void*>(buf), valid_size);
+ if (ret != PARCEL_ERROR_NONE)
+ _E("parcel_burst_read() is failed. error(%d)", ret);
return RPC_PORT_ERROR_NONE;
}
if (h == nullptr || buf == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->Write(buf, size);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ uint32_t valid_size = size & UINT32_MAX;
+ parcel_burst_write(parcel, static_cast<const void*>(buf), valid_size);
return RPC_PORT_ERROR_NONE;
}
-// LCOV_EXCL_START
RPC_API int rpc_port_parcel_reset_reader(rpc_port_parcel_h h) {
if (h == nullptr)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->ResetReader();
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ parcel_reset_reader(parcel);
return RPC_PORT_ERROR_NONE;
}
-// LCOV_EXCL_STOP
-// LCOV_EXCL_START
RPC_API int rpc_port_parcel_to_array(rpc_port_parcel_h h, void** array,
unsigned int* size) {
if (h == nullptr || !array || !size)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
+ parcel_h parcel = static_cast<parcel_h>(h);
+ void* raw = nullptr;
+ uint32_t raw_size = 0;
+ parcel_get_raw(parcel, &raw, &raw_size);
- const auto& ptr = p->GetRaw();
- void* array_ptr = malloc(ptr.size());
- if (!array_ptr)
+ void* array_ptr = malloc(raw_size);
+ if (array_ptr == nullptr)
return RPC_PORT_ERROR_OUT_OF_MEMORY;
- memcpy(array_ptr, ptr.data(), ptr.size());
+ memcpy(array_ptr, raw, raw_size);
*array = array_ptr;
- *size = ptr.size();
-
+ *size = static_cast<unsigned int>(raw_size);
return RPC_PORT_ERROR_NONE;
}
-// LCOV_EXCL_STOP
-// LCOV_EXCL_START
RPC_API int rpc_port_parcel_from_array(rpc_port_parcel_h h, const void* array,
unsigned int size) {
if (h == nullptr || !array || size == 0)
return RPC_PORT_ERROR_INVALID_PARAMETER;
- Parcel* p = static_cast<Parcel*>(h);
-
- p->Reset(reinterpret_cast<const unsigned char*>(array), size);
-
+ parcel_h parcel = static_cast<parcel_h>(h);
+ uint32_t valid_size = size & UINT32_MAX;
+ parcel_reset(parcel, array, valid_size);
return RPC_PORT_ERROR_NONE;
}
-// LCOV_EXCL_STOP