dom/bluetooth/ObexBase.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial