1 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
3 | *
|
---|
4 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
5 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
6 | * the License. You may obtain a copy of the License at
|
---|
7 | * http://www.mozilla.org/MPL/
|
---|
8 | *
|
---|
9 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
11 | * for the specific language governing rights and limitations under the
|
---|
12 | * License.
|
---|
13 | *
|
---|
14 | * The Original Code is Mozilla IPC.
|
---|
15 | *
|
---|
16 | * The Initial Developer of the Original Code is
|
---|
17 | * Netscape Communications Corporation.
|
---|
18 | * Portions created by the Initial Developer are Copyright (C) 2002
|
---|
19 | * the Initial Developer. All Rights Reserved.
|
---|
20 | *
|
---|
21 | * Contributor(s):
|
---|
22 | * Darin Fisher <darin@netscape.com>
|
---|
23 | *
|
---|
24 | * Alternatively, the contents of this file may be used under the terms of
|
---|
25 | * either the GNU General Public License Version 2 or later (the "GPL"), or
|
---|
26 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
27 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
28 | * of those above. If you wish to allow use of your version of this file only
|
---|
29 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
30 | * use your version of this file under the terms of the MPL, indicate your
|
---|
31 | * decision by deleting the provisions above and replace them with the notice
|
---|
32 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
33 | * the provisions above, a recipient may use your version of this file under
|
---|
34 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
35 | *
|
---|
36 | * ***** END LICENSE BLOCK ***** */
|
---|
37 |
|
---|
38 | #include <stdlib.h>
|
---|
39 | #include <string.h>
|
---|
40 | #include "ipcMessage.h"
|
---|
41 |
|
---|
42 | #include <iprt/assert.h>
|
---|
43 | #include <iprt/mem.h>
|
---|
44 |
|
---|
45 |
|
---|
46 | ipcMessage::~ipcMessage()
|
---|
47 | {
|
---|
48 | if (mMsgHdr)
|
---|
49 | RTMemFree(mMsgHdr);
|
---|
50 | }
|
---|
51 |
|
---|
52 | void
|
---|
53 | ipcMessage::Reset()
|
---|
54 | {
|
---|
55 | if (mMsgHdr) {
|
---|
56 | RTMemFree(mMsgHdr);
|
---|
57 | mMsgHdr = NULL;
|
---|
58 | }
|
---|
59 |
|
---|
60 | mMsgOffset = 0;
|
---|
61 | mMsgComplete = PR_FALSE;
|
---|
62 | }
|
---|
63 |
|
---|
64 | ipcMessage *
|
---|
65 | ipcMessage::Clone() const
|
---|
66 | {
|
---|
67 | ipcMessage *clone = new ipcMessage();
|
---|
68 | if (!clone)
|
---|
69 | return NULL;
|
---|
70 |
|
---|
71 | // copy buf if non-null
|
---|
72 | if (mMsgHdr) {
|
---|
73 | clone->mMsgHdr = (ipcMessageHeader *) RTMemDup(mMsgHdr, mMsgHdr->mLen);
|
---|
74 | }
|
---|
75 | else
|
---|
76 | clone->mMsgHdr = NULL;
|
---|
77 |
|
---|
78 | clone->mMsgOffset = mMsgOffset;
|
---|
79 | clone->mMsgComplete = mMsgComplete;
|
---|
80 |
|
---|
81 | return clone;
|
---|
82 | }
|
---|
83 |
|
---|
84 | PRStatus
|
---|
85 | ipcMessage::Init(const nsID &target, const char *data, PRUint32 dataLen)
|
---|
86 | {
|
---|
87 | if (mMsgHdr)
|
---|
88 | RTMemFree(mMsgHdr);
|
---|
89 |
|
---|
90 | mMsgComplete = PR_FALSE;
|
---|
91 |
|
---|
92 | // allocate message data
|
---|
93 | PRUint32 msgLen = IPC_MSG_HEADER_SIZE + dataLen;
|
---|
94 | mMsgHdr = (ipcMessageHeader *) RTMemAlloc(msgLen);
|
---|
95 | if (!mMsgHdr) {
|
---|
96 | mMsgHdr = NULL;
|
---|
97 | return PR_FAILURE;
|
---|
98 | }
|
---|
99 |
|
---|
100 | // fill in message data
|
---|
101 | mMsgHdr->mLen = msgLen;
|
---|
102 | mMsgHdr->mVersion = IPC_MSG_VERSION;
|
---|
103 | mMsgHdr->mFlags = 0;
|
---|
104 | mMsgHdr->mTarget = target;
|
---|
105 |
|
---|
106 | if (data)
|
---|
107 | SetData(0, data, dataLen);
|
---|
108 |
|
---|
109 | mMsgComplete = PR_TRUE;
|
---|
110 | return PR_SUCCESS;
|
---|
111 | }
|
---|
112 |
|
---|
113 | PRStatus
|
---|
114 | ipcMessage::SetData(PRUint32 offset, const char *data, PRUint32 dataLen)
|
---|
115 | {
|
---|
116 | Assert(mMsgHdr != NULL);
|
---|
117 |
|
---|
118 | if (offset + dataLen > DataLen())
|
---|
119 | return PR_FAILURE;
|
---|
120 |
|
---|
121 | memcpy((char *) Data() + offset, data, dataLen);
|
---|
122 | return PR_SUCCESS;
|
---|
123 | }
|
---|
124 |
|
---|
125 | PRBool
|
---|
126 | ipcMessage::Equals(const nsID &target, const char *data, PRUint32 dataLen) const
|
---|
127 | {
|
---|
128 | return mMsgComplete &&
|
---|
129 | mMsgHdr->mTarget.Equals(target) &&
|
---|
130 | DataLen() == dataLen &&
|
---|
131 | memcmp(Data(), data, dataLen) == 0;
|
---|
132 | }
|
---|
133 |
|
---|
134 | PRBool
|
---|
135 | ipcMessage::Equals(const ipcMessage *msg) const
|
---|
136 | {
|
---|
137 | PRUint32 msgLen = MsgLen();
|
---|
138 | return mMsgComplete && msg->mMsgComplete &&
|
---|
139 | msgLen == msg->MsgLen() &&
|
---|
140 | memcmp(MsgBuf(), msg->MsgBuf(), msgLen) == 0;
|
---|
141 | }
|
---|
142 |
|
---|
143 | PRStatus
|
---|
144 | ipcMessage::WriteTo(char *buf,
|
---|
145 | PRUint32 bufLen,
|
---|
146 | PRUint32 *bytesWritten,
|
---|
147 | PRBool *complete)
|
---|
148 | {
|
---|
149 | if (!mMsgComplete)
|
---|
150 | return PR_FAILURE;
|
---|
151 |
|
---|
152 | if (mMsgOffset == MsgLen()) {
|
---|
153 | *bytesWritten = 0;
|
---|
154 | *complete = PR_TRUE;
|
---|
155 | return PR_SUCCESS;
|
---|
156 | }
|
---|
157 |
|
---|
158 | PRUint32 count = MsgLen() - mMsgOffset;
|
---|
159 | if (count > bufLen)
|
---|
160 | count = bufLen;
|
---|
161 |
|
---|
162 | memcpy(buf, MsgBuf() + mMsgOffset, count);
|
---|
163 | mMsgOffset += count;
|
---|
164 |
|
---|
165 | *bytesWritten = count;
|
---|
166 | *complete = (mMsgOffset == MsgLen());
|
---|
167 |
|
---|
168 | return PR_SUCCESS;
|
---|
169 | }
|
---|
170 |
|
---|
171 | PRStatus
|
---|
172 | ipcMessage::ReadFrom(const char *buf,
|
---|
173 | PRUint32 bufLen,
|
---|
174 | PRUint32 *bytesRead,
|
---|
175 | PRBool *complete)
|
---|
176 | {
|
---|
177 | *bytesRead = 0;
|
---|
178 |
|
---|
179 | if (mMsgComplete) {
|
---|
180 | *complete = PR_TRUE;
|
---|
181 | return PR_SUCCESS;
|
---|
182 | }
|
---|
183 |
|
---|
184 | if (mMsgHdr) {
|
---|
185 | // appending data to buffer
|
---|
186 | if (mMsgOffset < sizeof(PRUint32)) {
|
---|
187 | // we haven't learned the message length yet
|
---|
188 | if (mMsgOffset + bufLen < sizeof(PRUint32)) {
|
---|
189 | // we still don't know the length of the message!
|
---|
190 | memcpy((char *) mMsgHdr + mMsgOffset, buf, bufLen);
|
---|
191 | mMsgOffset += bufLen;
|
---|
192 | *bytesRead = bufLen;
|
---|
193 | *complete = PR_FALSE;
|
---|
194 | return PR_SUCCESS;
|
---|
195 | }
|
---|
196 | else {
|
---|
197 | // we now have enough data to determine the message length
|
---|
198 | PRUint32 count = sizeof(PRUint32) - mMsgOffset;
|
---|
199 | memcpy((char *) MsgBuf() + mMsgOffset, buf, count);
|
---|
200 | mMsgOffset += count;
|
---|
201 | buf += count;
|
---|
202 | bufLen -= count;
|
---|
203 | *bytesRead = count;
|
---|
204 |
|
---|
205 | if (MsgLen() > IPC_MSG_GUESSED_SIZE) {
|
---|
206 | // realloc message buffer to the correct size
|
---|
207 | mMsgHdr = (ipcMessageHeader *) RTMemRealloc(mMsgHdr, MsgLen());
|
---|
208 | }
|
---|
209 | }
|
---|
210 | }
|
---|
211 | }
|
---|
212 | else {
|
---|
213 | if (bufLen < sizeof(PRUint32)) {
|
---|
214 | // not enough data available in buffer to determine allocation size
|
---|
215 | // allocate a partial buffer
|
---|
216 | PRUint32 msgLen = IPC_MSG_GUESSED_SIZE;
|
---|
217 | mMsgHdr = (ipcMessageHeader *) RTMemAlloc(msgLen);
|
---|
218 | if (!mMsgHdr)
|
---|
219 | return PR_FAILURE;
|
---|
220 | memcpy(mMsgHdr, buf, bufLen);
|
---|
221 | mMsgOffset = bufLen;
|
---|
222 | *bytesRead = bufLen;
|
---|
223 | *complete = PR_FALSE;
|
---|
224 | return PR_SUCCESS;
|
---|
225 | }
|
---|
226 | else {
|
---|
227 | PRUint32 msgLen = *(PRUint32 *) buf;
|
---|
228 | mMsgHdr = (ipcMessageHeader *) RTMemAlloc(msgLen);
|
---|
229 | if (!mMsgHdr)
|
---|
230 | return PR_FAILURE;
|
---|
231 | mMsgHdr->mLen = msgLen;
|
---|
232 | mMsgOffset = 0;
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | // have mMsgHdr at this point
|
---|
237 |
|
---|
238 | PRUint32 count = MsgLen() - mMsgOffset;
|
---|
239 | if (count > bufLen)
|
---|
240 | count = bufLen;
|
---|
241 |
|
---|
242 | memcpy((char *) mMsgHdr + mMsgOffset, buf, count);
|
---|
243 | mMsgOffset += count;
|
---|
244 | *bytesRead += count;
|
---|
245 |
|
---|
246 | *complete = mMsgComplete = (mMsgOffset == MsgLen());
|
---|
247 | return PR_SUCCESS;
|
---|
248 | }
|
---|