Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / bluetooth2 / ObexBase.cpp
blobe4de42f35730ab89cc7e3b33029fc046c6db4244
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)
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;
27 int
28 AppendHeaderBody(uint8_t* aRetBuf, int aBufferSize, const uint8_t* aData,
29 int aLength)
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;
43 int
44 AppendHeaderEndOfBody(uint8_t* aRetBuf)
46 aRetBuf[0] = ObexHeaderId::EndOfBody;
47 aRetBuf[1] = 0x00;
48 aRetBuf[2] = 0x03;
50 return 3;
53 int
54 AppendHeaderLength(uint8_t* aRetBuf, int aObjectLength)
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;
65 int
66 AppendHeaderConnectionId(uint8_t* aRetBuf, int aConnectionId)
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;
77 void
78 SetObexPacketInfo(uint8_t* aRetBuf, uint8_t aOpcode, int aPacketLength)
80 aRetBuf[0] = aOpcode;
81 aRetBuf[1] = (aPacketLength & 0xFF00) >> 8;
82 aRetBuf[2] = aPacketLength & 0x00FF;
85 bool
86 ParseHeaders(const uint8_t* aHeaderStart,
87 int aTotalLength,
88 ObexHeaderSet* aRetHandlerSet)
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)
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;
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;
131 aRetHandlerSet->AddHeader(new ObexHeader(headerId, contentLength, ptr));
132 ptr += contentLength;
135 return true;
138 END_BLUETOOTH_NAMESPACE