From 555ffcc8b54b6f3fc2b2daf15d7bd80a6ff2a161 Mon Sep 17 00:00:00 2001 From: Nathan Froyd Date: Mon, 2 Nov 2015 15:13:10 -0500 Subject: [PATCH] Bug 1221326 - use Endian.h more widely in bluetooth code; r=btian --- .../bluedroid/BluetoothMapSmsManager.cpp | 3 +-- .../bluedroid/BluetoothOppManager.cpp | 5 ++--- .../bluedroid/BluetoothPbapManager.cpp | 2 +- dom/bluetooth/bluez/BluetoothOppManager.cpp | 5 ++--- dom/bluetooth/common/BluetoothCommon.h | 20 ++++++------------- dom/bluetooth/common/ObexBase.cpp | 17 +++++----------- dom/bluetooth/common/ObexBase.h | 8 +++----- 7 files changed, 20 insertions(+), 40 deletions(-) diff --git a/dom/bluetooth/bluedroid/BluetoothMapSmsManager.cpp b/dom/bluetooth/bluedroid/BluetoothMapSmsManager.cpp index 25dca3a16ba5..f14601bcdadd 100644 --- a/dom/bluetooth/bluedroid/BluetoothMapSmsManager.cpp +++ b/dom/bluetooth/bluedroid/BluetoothMapSmsManager.cpp @@ -502,8 +502,7 @@ BluetoothMapSmsManager::ReplyToConnect() req[3] = 0x10; // version=1.0 req[4] = 0x00; // flag=0x00 - req[5] = BluetoothMapSmsManager::MAX_PACKET_LENGTH >> 8; - req[6] = (uint8_t)BluetoothMapSmsManager::MAX_PACKET_LENGTH; + BigEndian::writeUint16(&req[5], BluetoothMapSmsManager::MAX_PACKET_LENGTH); // Section 6.4 "Establishing an OBEX Session", MapSms 1.2 // Headers: [Who:16][Connection ID] diff --git a/dom/bluetooth/bluedroid/BluetoothOppManager.cpp b/dom/bluetooth/bluedroid/BluetoothOppManager.cpp index 7bc74a2a1a52..0bd579e4e711 100644 --- a/dom/bluetooth/bluedroid/BluetoothOppManager.cpp +++ b/dom/bluetooth/bluedroid/BluetoothOppManager.cpp @@ -866,8 +866,7 @@ BluetoothOppManager::ComposePacket(uint8_t aOpCode, UnixSocketBuffer* aMessage) // [opcode:1][length:2][Headers:var] frameHeaderLength = 3; - mPacketLength = ((static_cast(data[1]) << 8) | data[2]) - - frameHeaderLength; + mPacketLength = BigEndian::readUint16(&data[1]) - frameHeaderLength; /** * A PUT request from remote devices may be divided into multiple parts. @@ -1119,7 +1118,7 @@ BluetoothOppManager::ClientDataHandler(UnixSocketBuffer* aMessage) // Keep remote information mRemoteObexVersion = data[3]; mRemoteConnectionFlags = data[4]; - mRemoteMaxPacketLength = ((static_cast(data[5]) << 8) | data[6]); + mRemoteMaxPacketLength = BigEndian::readUint16(&data[5]); // The length of file name exceeds maximum length. int fileNameByteLen = (mFileName.Length() + 1) * 2; diff --git a/dom/bluetooth/bluedroid/BluetoothPbapManager.cpp b/dom/bluetooth/bluedroid/BluetoothPbapManager.cpp index 31adf4e4b1a8..8188792ef651 100644 --- a/dom/bluetooth/bluedroid/BluetoothPbapManager.cpp +++ b/dom/bluetooth/bluedroid/BluetoothPbapManager.cpp @@ -242,7 +242,7 @@ BluetoothPbapManager::ReceiveSocketData(BluetoothSocket* aSocket, } // Save the max packet length from remote information - mRemoteMaxPacketLength = ((static_cast(data[5]) << 8) | data[6]); + mRemoteMaxPacketLength = BigEndian::readUint16(&data[5]); if (mRemoteMaxPacketLength < kObexLeastMaxSize) { BT_LOGR("Remote maximum packet length %d is smaller than %d bytes", diff --git a/dom/bluetooth/bluez/BluetoothOppManager.cpp b/dom/bluetooth/bluez/BluetoothOppManager.cpp index b12425baf405..c470987a832a 100644 --- a/dom/bluetooth/bluez/BluetoothOppManager.cpp +++ b/dom/bluetooth/bluez/BluetoothOppManager.cpp @@ -842,8 +842,7 @@ BluetoothOppManager::ComposePacket(uint8_t aOpCode, UnixSocketBuffer* aMessage) // [opcode:1][length:2][Headers:var] frameHeaderLength = 3; - mPacketLength = ((static_cast(data[1]) << 8) | data[2]) - - frameHeaderLength; + mPacketLength = BigEndian::readUint16(&data[1]) - frameHeaderLength; /** * A PUT request from remote devices may be divided into multiple parts. * In other words, one request may need to be received multiple times, @@ -1094,7 +1093,7 @@ BluetoothOppManager::ClientDataHandler(UnixSocketBuffer* aMessage) // Keep remote information mRemoteObexVersion = data[3]; mRemoteConnectionFlags = data[4]; - mRemoteMaxPacketLength = (static_cast(data[5]) << 8) | data[6]; + mRemoteMaxPacketLength = BigEndian::readUint16(&data[5]); // The length of file name exceeds maximum length. int fileNameByteLen = (mFileName.Length() + 1) * 2; diff --git a/dom/bluetooth/common/BluetoothCommon.h b/dom/bluetooth/common/BluetoothCommon.h index 8835afa6d2f8..635dce8d156f 100644 --- a/dom/bluetooth/common/BluetoothCommon.h +++ b/dom/bluetooth/common/BluetoothCommon.h @@ -9,6 +9,7 @@ #include #include "mozilla/Compiler.h" +#include "mozilla/Endian.h" #include "mozilla/Observer.h" #include "nsAutoPtr.h" #include "nsPrintfCString.h" @@ -496,14 +497,12 @@ struct BluetoothAddress { uint16_t GetNAP() const { - return (static_cast(mAddr[4])) | - (static_cast(mAddr[5]) << 8); + return LittleEndian::readUint16(&mAddr[4]); } void SetNAP(uint16_t aNAP) { - mAddr[4] = aNAP; - mAddr[5] = aNAP >> 8; + LittleEndian::writeUint16(&mAddr[4], aNAP); } }; @@ -635,10 +634,7 @@ struct BluetoothUuid { void SetUuid32(uint32_t aUuid32) { - mUuid[0] = static_cast(0xff & (aUuid32 >> 24)); - mUuid[1] = static_cast(0xff & (aUuid32 >> 16)); - mUuid[2] = static_cast(0xff & (aUuid32 >> 8)); - mUuid[3] = static_cast(0xff & (aUuid32)); + BigEndian::writeUint32(&mUuid[0], aUuid32); mUuid[4] = 0x00; mUuid[5] = 0x00; mUuid[6] = 0x10; @@ -655,10 +651,7 @@ struct BluetoothUuid { uint32_t GetUuid32() const { - return (static_cast(mUuid[0]) << 24) | - (static_cast(mUuid[1]) << 16) | - (static_cast(mUuid[2]) << 8) | - (static_cast(mUuid[3])); + return BigEndian::readUint32(&mUuid[0]); } void SetUuid16(uint16_t aUuid16) @@ -668,8 +661,7 @@ struct BluetoothUuid { uint16_t GetUuid16() const { - return (static_cast(mUuid[2]) << 8) | - (static_cast(mUuid[3])); + return BigEndian::readUint16(&mUuid[2]); } }; diff --git a/dom/bluetooth/common/ObexBase.cpp b/dom/bluetooth/common/ObexBase.cpp index b81012591fc0..40efc44f5f8f 100644 --- a/dom/bluetooth/common/ObexBase.cpp +++ b/dom/bluetooth/common/ObexBase.cpp @@ -23,8 +23,7 @@ AppendHeader(uint8_t aHeaderId, uint8_t* aRetBuf, int aBufferSize, int writtenLength = (headerLength < aBufferSize) ? headerLength : aBufferSize; aRetBuf[0] = aHeaderId; - aRetBuf[1] = (headerLength & 0xFF00) >> 8; - aRetBuf[2] = headerLength & 0x00FF; + BigEndian::writeUint16(&aRetBuf[1], headerLength); memcpy(&aRetBuf[3], aData, writtenLength - 3); return writtenLength; @@ -37,10 +36,7 @@ int AppendHeader(uint8_t aHeaderId, uint8_t* aRetBuf, int aValue) { aRetBuf[0] = aHeaderId; - aRetBuf[1] = (aValue & 0xFF000000) >> 24; - aRetBuf[2] = (aValue & 0x00FF0000) >> 16; - aRetBuf[3] = (aValue & 0x0000FF00) >> 8; - aRetBuf[4] = aValue & 0x000000FF; + BigEndian::writeInt32(&aRetBuf[1], aValue); return 5; } @@ -135,8 +131,7 @@ void SetObexPacketInfo(uint8_t* aRetBuf, uint8_t aOpcode, int aPacketLength) { aRetBuf[0] = aOpcode; - aRetBuf[1] = (aPacketLength & 0xFF00) >> 8; - aRetBuf[2] = aPacketLength & 0x00FF; + BigEndian::writeUint16(&aRetBuf[1], aPacketLength); } bool @@ -150,7 +145,6 @@ ParseHeaders(const uint8_t* aHeaderStart, ObexHeaderId headerId = (ObexHeaderId)*ptr++; uint16_t contentLength = 0; - uint8_t highByte, lowByte; // Defined in 2.1 OBEX Headers, IrOBEX 1.2 switch (headerId >> 6) @@ -160,9 +154,8 @@ ParseHeaders(const uint8_t* aHeaderStart, // unsigned integer. case 0x01: // byte sequence, length prefixed with 2 byte unsigned integer. - highByte = *ptr++; - lowByte = *ptr++; - contentLength = (((uint16_t)highByte << 8) | lowByte) - 3; + contentLength = BigEndian::readUint16(ptr) - 3; + ptr += 2; break; case 0x02: diff --git a/dom/bluetooth/common/ObexBase.h b/dom/bluetooth/common/ObexBase.h index 7937f5df6e06..81970237eba5 100644 --- a/dom/bluetooth/common/ObexBase.h +++ b/dom/bluetooth/common/ObexBase.h @@ -8,6 +8,7 @@ #define mozilla_dom_bluetooth_ObexBase_h #include "BluetoothCommon.h" +#include "mozilla/Endian.h" #include "nsAutoPtr.h" #include "nsTArray.h" @@ -178,7 +179,7 @@ public: uint8_t* ptr = mHeaders[i]->mData.get(); for (int j = 0; j < nameLength; ++j) { - char16_t c = ((((uint32_t)ptr[j * 2]) << 8) | ptr[j * 2 + 1]); + char16_t c = BigEndian::readUint16(&ptr[j * 2]); aRetName += c; } @@ -211,10 +212,7 @@ public: for (int i = 0; i < length; ++i) { if (mHeaders[i]->mId == ObexHeaderId::Length) { uint8_t* ptr = mHeaders[i]->mData.get(); - *aRetLength = ((uint32_t)ptr[0] << 24) | - ((uint32_t)ptr[1] << 16) | - ((uint32_t)ptr[2] << 8) | - ((uint32_t)ptr[3]); + *aRetLength = BigEndian::readUint32(&ptr[0]); return; } }