mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 1183249: Move PDU helper functions to ipc/hal, r=shuang
This commit is contained in:
parent
f2449f1905
commit
f4f402420d
@ -11,40 +11,13 @@
|
||||
|
||||
BEGIN_BLUETOOTH_NAMESPACE
|
||||
|
||||
using mozilla::ipc::DaemonSocketPDUHelpers::Convert;
|
||||
using mozilla::ipc::DaemonSocketPDUHelpers::PackPDU;
|
||||
|
||||
//
|
||||
// Conversion
|
||||
//
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, uint8_t& aOut)
|
||||
{
|
||||
static const bool sValue[] = {
|
||||
CONVERT(false, 0x00),
|
||||
CONVERT(true, 0x01)
|
||||
};
|
||||
if (NS_WARN_IF(aIn >= MOZ_ARRAY_LENGTH(sValue))) {
|
||||
aOut = 0;
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = sValue[aIn];
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, int32_t& aOut)
|
||||
{
|
||||
static const bool sValue[] = {
|
||||
CONVERT(false, 0x00),
|
||||
CONVERT(true, 0x01)
|
||||
};
|
||||
if (NS_WARN_IF(aIn >= MOZ_ARRAY_LENGTH(sValue))) {
|
||||
aOut = 0;
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = sValue[aIn];
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, BluetoothScanMode& aOut)
|
||||
{
|
||||
@ -60,42 +33,6 @@ Convert(bool aIn, BluetoothScanMode& aOut)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, uint8_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn < std::numeric_limits<uint8_t>::min()) ||
|
||||
NS_WARN_IF(aIn > std::numeric_limits<uint8_t>::max())) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<uint8_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int16_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn < std::numeric_limits<int16_t>::min()) ||
|
||||
NS_WARN_IF(aIn > std::numeric_limits<int16_t>::max())) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<int16_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int32_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn < std::numeric_limits<int32_t>::min()) ||
|
||||
NS_WARN_IF(aIn > std::numeric_limits<int32_t>::max())) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<int32_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(int32_t aIn, BluetoothTypeOfDevice& aOut)
|
||||
{
|
||||
@ -129,41 +66,6 @@ Convert(int32_t aIn, BluetoothScanMode& aOut)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, bool& aOut)
|
||||
{
|
||||
static const bool sBool[] = {
|
||||
CONVERT(0x00, false),
|
||||
CONVERT(0x01, true)
|
||||
};
|
||||
if (NS_WARN_IF(aIn >= MOZ_ARRAY_LENGTH(sBool))) {
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = sBool[aIn];
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, char& aOut)
|
||||
{
|
||||
aOut = static_cast<char>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, int& aOut)
|
||||
{
|
||||
aOut = static_cast<int>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, unsigned long& aOut)
|
||||
{
|
||||
aOut = static_cast<unsigned long>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, BluetoothA2dpAudioState& aOut)
|
||||
{
|
||||
@ -547,36 +449,6 @@ Convert(int32_t aIn, BluetoothGattStatus& aOut)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint32_t aIn, int& aOut)
|
||||
{
|
||||
aOut = static_cast<int>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint32_t aIn, uint8_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn < std::numeric_limits<uint8_t>::min()) ||
|
||||
NS_WARN_IF(aIn > std::numeric_limits<uint8_t>::max())) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<uint8_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(size_t aIn, uint16_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn >= (1ul << 16))) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<uint16_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(const nsAString& aIn, BluetoothAddress& aOut)
|
||||
{
|
||||
@ -1260,12 +1132,6 @@ PackPDU(const BluetoothConfigurationParameter& aIn, DaemonSocketPDU& aPDU)
|
||||
PackArray<uint8_t>(aIn.mValue.get(), aIn.mLength), aPDU);
|
||||
}
|
||||
|
||||
nsresult
|
||||
PackPDU(const DaemonSocketPDUHeader& aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
return PackPDU(aIn.mService, aIn.mOpcode, aIn.mLength, aPDU);
|
||||
}
|
||||
|
||||
nsresult
|
||||
PackPDU(const BluetoothHandsfreeAtResponse& aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
@ -1873,64 +1739,5 @@ UnpackPDU(DaemonSocketPDU& aPDU, BluetoothGattNotifyParam& aOut)
|
||||
/* unpack value */
|
||||
return aPDU.Read(aOut.mValue, aOut.mLength);
|
||||
}
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, nsDependentCString& aOut)
|
||||
{
|
||||
// We get a pointer to the first character in the PDU, a length
|
||||
// of 1 ensures we consume the \0 byte. With 'str' pointing to
|
||||
// the string in the PDU, we can copy the actual bytes.
|
||||
|
||||
const char* str = reinterpret_cast<const char*>(aPDU.Consume(1));
|
||||
if (NS_WARN_IF(!str)) {
|
||||
return NS_ERROR_ILLEGAL_VALUE; // end of PDU
|
||||
}
|
||||
|
||||
const char* end = static_cast<char*>(memchr(str, '\0', aPDU.GetSize() + 1));
|
||||
if (NS_WARN_IF(!end)) {
|
||||
return NS_ERROR_ILLEGAL_VALUE; // no string terminator
|
||||
}
|
||||
|
||||
ptrdiff_t len = end - str;
|
||||
|
||||
const uint8_t* rest = aPDU.Consume(len);
|
||||
if (NS_WARN_IF(!rest)) {
|
||||
// We couldn't consume bytes that should have been there.
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
|
||||
aOut.Rebind(str, len);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, const UnpackCString0& aOut)
|
||||
{
|
||||
nsDependentCString cstring;
|
||||
|
||||
nsresult rv = UnpackPDU(aPDU, cstring);
|
||||
if (NS_FAILED(rv)) {
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
|
||||
aOut.mString->AssignASCII(cstring.get(), cstring.Length());
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, const UnpackString0& aOut)
|
||||
{
|
||||
nsDependentCString cstring;
|
||||
|
||||
nsresult rv = UnpackPDU(aPDU, cstring);
|
||||
if (NS_FAILED(rv)) {
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
|
||||
*aOut.mString = NS_ConvertUTF8toUTF16(cstring);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
END_BLUETOOTH_NAMESPACE
|
||||
|
@ -17,6 +17,11 @@ BEGIN_BLUETOOTH_NAMESPACE
|
||||
|
||||
using mozilla::ipc::DaemonSocketPDU;
|
||||
using mozilla::ipc::DaemonSocketPDUHeader;
|
||||
using mozilla::ipc::DaemonSocketPDUHelpers::Convert;
|
||||
using mozilla::ipc::DaemonSocketPDUHelpers::PackPDU;
|
||||
using mozilla::ipc::DaemonSocketPDUHelpers::UnpackPDU;
|
||||
|
||||
using namespace mozilla::ipc::DaemonSocketPDUHelpers;
|
||||
|
||||
//
|
||||
// Helper structures
|
||||
@ -123,49 +128,16 @@ struct BluetoothServiceName {
|
||||
//
|
||||
// Conversion
|
||||
//
|
||||
// PDUs can only store primitive data types, such as intergers or
|
||||
// strings. Gecko often uses more complex data types, such as
|
||||
// enumators or stuctures. Conversion functions convert between
|
||||
// primitive data and internal Gecko's data types during a PDU's
|
||||
// packing and unpacking.
|
||||
//
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, uint8_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, int32_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, BluetoothScanMode& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, uint8_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int16_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int32_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(int32_t aIn, BluetoothTypeOfDevice& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(int32_t aIn, BluetoothScanMode& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, bool& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, char& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, int& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, unsigned long& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, BluetoothA2dpAudioState& aOut);
|
||||
|
||||
@ -229,15 +201,6 @@ Convert(uint8_t aIn, BluetoothStatus& aOut);
|
||||
nsresult
|
||||
Convert(int32_t aIn, BluetoothGattStatus& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint32_t aIn, int& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint32_t aIn, uint8_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(size_t aIn, uint16_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(const nsAString& aIn, BluetoothAddress& aOut);
|
||||
|
||||
@ -329,38 +292,9 @@ Convert(BluetoothGattWriteType aIn, int32_t& aOut);
|
||||
// Packing
|
||||
//
|
||||
|
||||
// introduce link errors on non-handled data types
|
||||
template <typename T>
|
||||
nsresult
|
||||
PackPDU(T aIn, DaemonSocketPDU& aPDU);
|
||||
|
||||
nsresult
|
||||
PackPDU(bool aIn, DaemonSocketPDU& aPDU);
|
||||
|
||||
inline nsresult
|
||||
PackPDU(uint8_t aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
return aPDU.Write(aIn);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
PackPDU(uint16_t aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
return aPDU.Write(aIn);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
PackPDU(int32_t aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
return aPDU.Write(aIn);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
PackPDU(uint32_t aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
return aPDU.Write(aIn);
|
||||
}
|
||||
|
||||
nsresult
|
||||
PackPDU(const BluetoothAddress& aIn, DaemonSocketPDU& aPDU);
|
||||
|
||||
@ -393,9 +327,6 @@ PackPDU(BluetoothAvrcpStatus aIn, DaemonSocketPDU& aPDU);
|
||||
nsresult
|
||||
PackPDU(const BluetoothConfigurationParameter& aIn, DaemonSocketPDU& aPDU);
|
||||
|
||||
nsresult
|
||||
PackPDU(const DaemonSocketPDUHeader& aIn, DaemonSocketPDU& aPDU);
|
||||
|
||||
nsresult
|
||||
PackPDU(const BluetoothHandsfreeAtResponse& aIn, DaemonSocketPDU& aPDU);
|
||||
|
||||
@ -860,41 +791,6 @@ PackPDU(const T1& aIn1, const T2& aIn2, const T3& aIn3,
|
||||
// Unpacking
|
||||
//
|
||||
|
||||
// introduce link errors on non-handled data types
|
||||
template <typename T>
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, T& aOut);
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, int8_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, uint8_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, uint16_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, int32_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, uint32_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, bool& aOut);
|
||||
|
||||
@ -934,20 +830,6 @@ UnpackPDU(DaemonSocketPDU& aPDU, BluetoothAvrcpRemoteFeature& aOut);
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, BluetoothBondState& aOut);
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, DaemonSocketPDUHeader& aOut)
|
||||
{
|
||||
nsresult rv = UnpackPDU(aPDU, aOut.mService);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = UnpackPDU(aPDU, aOut.mOpcode);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
return UnpackPDU(aPDU, aOut.mLength);
|
||||
}
|
||||
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, BluetoothTypeOfDevice& aOut);
|
||||
|
||||
@ -1021,9 +903,6 @@ UnpackPDU(DaemonSocketPDU& aPDU, BluetoothGattWriteParam& aOut);
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, BluetoothGattNotifyParam& aOut);
|
||||
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, nsDependentCString& aOut);
|
||||
|
||||
/* |UnpackConversion| is a helper for convering unpacked values. Pass
|
||||
* an instance of this structure to |UnpackPDU| to read a value from
|
||||
* the PDU in the input type and convert it to the output type.
|
||||
@ -1128,44 +1007,6 @@ UnpackPDU(DaemonSocketPDU& aPDU, nsTArray<T>& aOut)
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
/* |UnpackCString0| is a helper for unpacking 0-terminated C string,
|
||||
* including the \0 character. Pass an instance of this structure
|
||||
* as the first argument to |UnpackPDU| to unpack a string.
|
||||
*/
|
||||
struct UnpackCString0
|
||||
{
|
||||
UnpackCString0(nsCString& aString)
|
||||
: mString(&aString)
|
||||
{ }
|
||||
|
||||
nsCString* mString; // non-null by construction
|
||||
};
|
||||
|
||||
/* This implementation of |UnpackPDU| unpacks a 0-terminated C string.
|
||||
*/
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, const UnpackCString0& aOut);
|
||||
|
||||
/* |UnpackString0| is a helper for unpacking 0-terminated C string,
|
||||
* including the \0 character. Pass an instance of this structure
|
||||
* as the first argument to |UnpackPDU| to unpack a C string and convert
|
||||
* it to wide-character encoding.
|
||||
*/
|
||||
struct UnpackString0
|
||||
{
|
||||
UnpackString0(nsString& aString)
|
||||
: mString(&aString)
|
||||
{ }
|
||||
|
||||
nsString* mString; // non-null by construction
|
||||
};
|
||||
|
||||
/* This implementation of |UnpackPDU| unpacks a 0-terminated C string
|
||||
* and converts it to wide-character encoding.
|
||||
*/
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, const UnpackString0& aOut);
|
||||
|
||||
/* |UnpackReversed| is a helper for unpacking data in reversed order. Pass an
|
||||
* instance of this structure as the second argument to |UnpackPDU| to unpack
|
||||
* data in reversed order.
|
||||
|
235
ipc/hal/DaemonSocketPDUHelpers.cpp
Normal file
235
ipc/hal/DaemonSocketPDUHelpers.cpp
Normal file
@ -0,0 +1,235 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
||||
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
#include "DaemonSocketPDUHelpers.h"
|
||||
#include <limits>
|
||||
|
||||
namespace mozilla {
|
||||
namespace ipc {
|
||||
namespace DaemonSocketPDUHelpers {
|
||||
|
||||
//
|
||||
// Conversion
|
||||
//
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, uint8_t& aOut)
|
||||
{
|
||||
static const uint8_t sValue[] = {
|
||||
[false] = 0x00,
|
||||
[true] = 0x01
|
||||
};
|
||||
if (NS_WARN_IF(aIn >= MOZ_ARRAY_LENGTH(sValue))) {
|
||||
aOut = 0;
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = sValue[aIn];
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, int32_t& aOut)
|
||||
{
|
||||
uint8_t out;
|
||||
nsresult rv = Convert(aIn, out);
|
||||
if (NS_FAILED(rv)) {
|
||||
out = 0; // silence compiler warning
|
||||
return rv;
|
||||
}
|
||||
aOut = static_cast<int32_t>(out);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, uint8_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn < std::numeric_limits<uint8_t>::min()) ||
|
||||
NS_WARN_IF(aIn > std::numeric_limits<uint8_t>::max())) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<uint8_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int16_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn < std::numeric_limits<int16_t>::min()) ||
|
||||
NS_WARN_IF(aIn > std::numeric_limits<int16_t>::max())) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<int16_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int32_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn < std::numeric_limits<int32_t>::min()) ||
|
||||
NS_WARN_IF(aIn > std::numeric_limits<int32_t>::max())) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<int32_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, bool& aOut)
|
||||
{
|
||||
static const bool sBool[] = {
|
||||
[0x00] = false,
|
||||
[0x01] = true
|
||||
};
|
||||
if (NS_WARN_IF(aIn >= MOZ_ARRAY_LENGTH(sBool))) {
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = sBool[aIn];
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, char& aOut)
|
||||
{
|
||||
aOut = static_cast<char>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, int& aOut)
|
||||
{
|
||||
aOut = static_cast<int>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, unsigned long& aOut)
|
||||
{
|
||||
aOut = static_cast<unsigned long>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint32_t aIn, int& aOut)
|
||||
{
|
||||
aOut = static_cast<int>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(uint32_t aIn, uint8_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn < std::numeric_limits<uint8_t>::min()) ||
|
||||
NS_WARN_IF(aIn > std::numeric_limits<uint8_t>::max())) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<uint8_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
Convert(size_t aIn, uint16_t& aOut)
|
||||
{
|
||||
if (NS_WARN_IF(aIn >= (1ul << 16))) {
|
||||
aOut = 0; // silences compiler warning
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
aOut = static_cast<uint16_t>(aIn);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//
|
||||
// Packing
|
||||
//
|
||||
|
||||
nsresult
|
||||
PackPDU(const DaemonSocketPDUHeader& aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
nsresult rv = PackPDU(aIn.mService, aPDU);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = PackPDU(aIn.mOpcode, aPDU);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = PackPDU(aIn.mLength, aPDU);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
//
|
||||
// Unpacking
|
||||
//
|
||||
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, nsDependentCString& aOut)
|
||||
{
|
||||
// We get a pointer to the first character in the PDU, a length
|
||||
// of 1 ensures we consume the \0 byte. With 'str' pointing to
|
||||
// the string in the PDU, we can copy the actual bytes.
|
||||
|
||||
const char* str = reinterpret_cast<const char*>(aPDU.Consume(1));
|
||||
if (NS_WARN_IF(!str)) {
|
||||
return NS_ERROR_ILLEGAL_VALUE; // end of PDU
|
||||
}
|
||||
|
||||
const char* end = static_cast<char*>(memchr(str, '\0', aPDU.GetSize() + 1));
|
||||
if (NS_WARN_IF(!end)) {
|
||||
return NS_ERROR_ILLEGAL_VALUE; // no string terminator
|
||||
}
|
||||
|
||||
ptrdiff_t len = end - str;
|
||||
|
||||
const uint8_t* rest = aPDU.Consume(len);
|
||||
if (NS_WARN_IF(!rest)) {
|
||||
// We couldn't consume bytes that should have been there.
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
|
||||
aOut.Rebind(str, len);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, const UnpackCString0& aOut)
|
||||
{
|
||||
nsDependentCString cstring;
|
||||
|
||||
nsresult rv = UnpackPDU(aPDU, cstring);
|
||||
if (NS_FAILED(rv)) {
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
|
||||
aOut.mString->AssignASCII(cstring.get(), cstring.Length());
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, const UnpackString0& aOut)
|
||||
{
|
||||
nsDependentCString cstring;
|
||||
|
||||
nsresult rv = UnpackPDU(aPDU, cstring);
|
||||
if (NS_FAILED(rv)) {
|
||||
return NS_ERROR_ILLEGAL_VALUE;
|
||||
}
|
||||
|
||||
*aOut.mString = NS_ConvertUTF8toUTF16(cstring);
|
||||
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
} // namespace DaemonSocketPDUHelpers
|
||||
} // namespace ipc
|
||||
} // namespace mozilla
|
@ -8,6 +8,8 @@
|
||||
#define mozilla_ipc_DaemonSocketPDUHelpers_h
|
||||
|
||||
#include <stdint.h>
|
||||
#include "mozilla/ipc/DaemonSocketPDU.h"
|
||||
#include "nsString.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace ipc {
|
||||
@ -30,6 +32,186 @@ struct DaemonSocketPDUHeader {
|
||||
uint16_t mLength;
|
||||
};
|
||||
|
||||
namespace DaemonSocketPDUHelpers {
|
||||
|
||||
//
|
||||
// Conversion
|
||||
//
|
||||
// PDUs can only store primitive data types, such as integers or
|
||||
// byte arrays. Gecko often uses more complex data types, such as
|
||||
// enumators or stuctures. Conversion functions convert between
|
||||
// primitive data and internal Gecko's data types during a PDU's
|
||||
// packing and unpacking.
|
||||
//
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, uint8_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(bool aIn, int32_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, uint8_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int16_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(int aIn, int32_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, bool& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, char& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, int& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint8_t aIn, unsigned long& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint32_t aIn, int& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(uint32_t aIn, uint8_t& aOut);
|
||||
|
||||
nsresult
|
||||
Convert(size_t aIn, uint16_t& aOut);
|
||||
|
||||
//
|
||||
// Packing
|
||||
//
|
||||
|
||||
// introduce link errors on non-handled data types
|
||||
template <typename T>
|
||||
nsresult
|
||||
PackPDU(T aIn, DaemonSocketPDU& aPDU);
|
||||
|
||||
inline nsresult
|
||||
PackPDU(uint8_t aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
return aPDU.Write(aIn);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
PackPDU(uint16_t aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
return aPDU.Write(aIn);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
PackPDU(int32_t aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
return aPDU.Write(aIn);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
PackPDU(uint32_t aIn, DaemonSocketPDU& aPDU)
|
||||
{
|
||||
return aPDU.Write(aIn);
|
||||
}
|
||||
|
||||
nsresult
|
||||
PackPDU(const DaemonSocketPDUHeader& aIn, DaemonSocketPDU& aPDU);
|
||||
|
||||
//
|
||||
// Unpacking
|
||||
//
|
||||
|
||||
// introduce link errors on non-handled data types
|
||||
template <typename T>
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, T& aOut);
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, int8_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, uint8_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, uint16_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, int32_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, uint32_t& aOut)
|
||||
{
|
||||
return aPDU.Read(aOut);
|
||||
}
|
||||
|
||||
inline nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, DaemonSocketPDUHeader& aOut)
|
||||
{
|
||||
nsresult rv = UnpackPDU(aPDU, aOut.mService);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = UnpackPDU(aPDU, aOut.mOpcode);
|
||||
if (NS_FAILED(rv)) {
|
||||
return rv;
|
||||
}
|
||||
return UnpackPDU(aPDU, aOut.mLength);
|
||||
}
|
||||
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, nsDependentCString& aOut);
|
||||
|
||||
/* |UnpackCString0| is a helper for unpacking 0-terminated C string,
|
||||
* including the \0 character. Pass an instance of this structure
|
||||
* as the first argument to |UnpackPDU| to unpack a string.
|
||||
*/
|
||||
struct UnpackCString0
|
||||
{
|
||||
UnpackCString0(nsCString& aString)
|
||||
: mString(&aString)
|
||||
{ }
|
||||
|
||||
nsCString* mString; // non-null by construction
|
||||
};
|
||||
|
||||
/* This implementation of |UnpackPDU| unpacks a 0-terminated C string.
|
||||
*/
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, const UnpackCString0& aOut);
|
||||
|
||||
/* |UnpackString0| is a helper for unpacking 0-terminated C string,
|
||||
* including the \0 character. Pass an instance of this structure
|
||||
* as the first argument to |UnpackPDU| to unpack a C string and convert
|
||||
* it to wide-character encoding.
|
||||
*/
|
||||
struct UnpackString0
|
||||
{
|
||||
UnpackString0(nsString& aString)
|
||||
: mString(&aString)
|
||||
{ }
|
||||
|
||||
nsString* mString; // non-null by construction
|
||||
};
|
||||
|
||||
/* This implementation of |UnpackPDU| unpacks a 0-terminated C string
|
||||
* and converts it to wide-character encoding.
|
||||
*/
|
||||
nsresult
|
||||
UnpackPDU(DaemonSocketPDU& aPDU, const UnpackString0& aOut);
|
||||
|
||||
} // namespace DaemonSocketPDUHelpers
|
||||
|
||||
} // namespace ipc
|
||||
} // namespace mozilla
|
||||
|
||||
|
@ -15,7 +15,8 @@ EXPORTS.mozilla.ipc += [
|
||||
UNIFIED_SOURCES += [
|
||||
'DaemonSocket.cpp',
|
||||
'DaemonSocketConsumer.cpp',
|
||||
'DaemonSocketPDU.cpp'
|
||||
'DaemonSocketPDU.cpp',
|
||||
'DaemonSocketPDUHelpers.cpp'
|
||||
]
|
||||
|
||||
include('/ipc/chromium/chromium-config.mozbuild')
|
||||
|
Loading…
Reference in New Issue
Block a user