dom/bluetooth/ObexBase.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
     2 /* vim: set ts=2 et sw=2 tw=80: */
     3 /* This Source Code Form is subject to the terms of the Mozilla Public
     4  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
     5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
     7 #include "ObexBase.h"
     9 BEGIN_BLUETOOTH_NAMESPACE
    11 int
    12 AppendHeaderName(uint8_t* aRetBuf, int aBufferSize, const char* aName,
    13                  int aLength)
    14 {
    15   int headerLength = aLength + 3;
    17   aRetBuf[0] = ObexHeaderId::Name;
    18   aRetBuf[1] = (headerLength & 0xFF00) >> 8;
    19   aRetBuf[2] = headerLength & 0x00FF;
    21   memcpy(&aRetBuf[3], aName, (aLength < aBufferSize - 3)? aLength
    22                                                         : aBufferSize - 3);
    24   return headerLength;
    25 }
    27 int
    28 AppendHeaderBody(uint8_t* aRetBuf, int aBufferSize, const uint8_t* aData,
    29                  int aLength)
    30 {
    31   int headerLength = aLength + 3;
    33   aRetBuf[0] = ObexHeaderId::Body;
    34   aRetBuf[1] = (headerLength & 0xFF00) >> 8;
    35   aRetBuf[2] = headerLength & 0x00FF;
    37   memcpy(&aRetBuf[3], aData, (aLength < aBufferSize - 3)? aLength
    38                                                         : aBufferSize - 3);
    40   return headerLength;
    41 }
    43 int
    44 AppendHeaderEndOfBody(uint8_t* aRetBuf)
    45 {
    46   aRetBuf[0] = ObexHeaderId::EndOfBody;
    47   aRetBuf[1] = 0x00;
    48   aRetBuf[2] = 0x03;
    50   return 3;
    51 }
    53 int
    54 AppendHeaderLength(uint8_t* aRetBuf, int aObjectLength)
    55 {
    56   aRetBuf[0] = ObexHeaderId::Length;
    57   aRetBuf[1] = (aObjectLength & 0xFF000000) >> 24;
    58   aRetBuf[2] = (aObjectLength & 0x00FF0000) >> 16;
    59   aRetBuf[3] = (aObjectLength & 0x0000FF00) >> 8;
    60   aRetBuf[4] = aObjectLength & 0x000000FF;
    62   return 5;
    63 }
    65 int
    66 AppendHeaderConnectionId(uint8_t* aRetBuf, int aConnectionId)
    67 {
    68   aRetBuf[0] = ObexHeaderId::ConnectionId;
    69   aRetBuf[1] = (aConnectionId & 0xFF000000) >> 24;
    70   aRetBuf[2] = (aConnectionId & 0x00FF0000) >> 16;
    71   aRetBuf[3] = (aConnectionId & 0x0000FF00) >> 8;
    72   aRetBuf[4] = aConnectionId & 0x000000FF;
    74   return 5;
    75 }
    77 void
    78 SetObexPacketInfo(uint8_t* aRetBuf, uint8_t aOpcode, int aPacketLength)
    79 {
    80   aRetBuf[0] = aOpcode;
    81   aRetBuf[1] = (aPacketLength & 0xFF00) >> 8;
    82   aRetBuf[2] = aPacketLength & 0x00FF;
    83 }
    85 bool
    86 ParseHeaders(const uint8_t* aHeaderStart,
    87              int aTotalLength,
    88              ObexHeaderSet* aRetHandlerSet)
    89 {
    90   const uint8_t* ptr = aHeaderStart;
    92   while (ptr - aHeaderStart < aTotalLength) {
    93     ObexHeaderId headerId = (ObexHeaderId)*ptr++;
    95     uint16_t contentLength = 0;
    96     uint8_t highByte, lowByte;
    98     // Defined in 2.1 OBEX Headers, IrOBEX 1.2
    99     switch (headerId >> 6)
   100     {
   101       case 0x00:
   102         // Null-terminated Unicode text, length prefixed with 2-byte
   103         // unsigned integer.
   104       case 0x01:
   105         // byte sequence, length prefixed with 2 byte unsigned integer.
   106         highByte = *ptr++;
   107         lowByte = *ptr++;
   108         contentLength = (((uint16_t)highByte << 8) | lowByte) - 3;
   109         break;
   111       case 0x02:
   112         // 1 byte quantity
   113         contentLength = 1;
   114         break;
   116       case 0x03:
   117         // 4 byte quantity
   118         contentLength = 4;
   119         break;
   120     }
   122     // Length check to prevent from memory pollusion.
   123     if (ptr + contentLength > aHeaderStart + aTotalLength) {
   124       // Severe error occurred. We can't even believe the received data, so
   125       // clear all headers.
   126       MOZ_ASSERT(false);
   127       aRetHandlerSet->ClearHeaders();
   128       return false;
   129     }
   131     aRetHandlerSet->AddHeader(new ObexHeader(headerId, contentLength, ptr));
   132     ptr += contentLength;
   133   }
   135   return true;
   136 }
   138 END_BLUETOOTH_NAMESPACE

mercurial