Bug 458256. Use LoadLibraryW instead of LoadLibrary (patch by DougT). r+sr=vlad
[wine-gecko.git] / ipc / ipcd / shared / src / ipcMessage.cpp
blobe4e097baac90d45deb0c96dab443315ddf273b90
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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.
14 * The Original Code is Mozilla IPC.
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.
21 * Contributor(s):
22 * Darin Fisher <darin@netscape.com>
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.
36 * ***** END LICENSE BLOCK ***** */
38 #include <stdlib.h>
39 #include <string.h>
40 #include "prlog.h"
41 #include "ipcMessage.h"
43 ipcMessage::~ipcMessage()
45 if (mMsgHdr)
46 free(mMsgHdr);
49 void
50 ipcMessage::Reset()
52 if (mMsgHdr) {
53 free(mMsgHdr);
54 mMsgHdr = NULL;
57 mMsgOffset = 0;
58 mMsgComplete = PR_FALSE;
61 ipcMessage *
62 ipcMessage::Clone() const
64 ipcMessage *clone = new ipcMessage();
65 if (!clone)
66 return NULL;
68 // copy buf if non-null
69 if (mMsgHdr) {
70 clone->mMsgHdr = (ipcMessageHeader *) malloc(mMsgHdr->mLen);
71 memcpy(clone->mMsgHdr, mMsgHdr, mMsgHdr->mLen);
73 else
74 clone->mMsgHdr = NULL;
76 clone->mMsgOffset = mMsgOffset;
77 clone->mMsgComplete = mMsgComplete;
79 return clone;
82 PRStatus
83 ipcMessage::Init(const nsID &target, const char *data, PRUint32 dataLen)
85 if (mMsgHdr)
86 free(mMsgHdr);
87 mMsgComplete = PR_FALSE;
89 // allocate message data
90 PRUint32 msgLen = IPC_MSG_HEADER_SIZE + dataLen;
91 mMsgHdr = (ipcMessageHeader *) malloc(msgLen);
92 if (!mMsgHdr) {
93 mMsgHdr = NULL;
94 return PR_FAILURE;
97 // fill in message data
98 mMsgHdr->mLen = msgLen;
99 mMsgHdr->mVersion = IPC_MSG_VERSION;
100 mMsgHdr->mFlags = 0;
101 mMsgHdr->mTarget = target;
103 if (data)
104 SetData(0, data, dataLen);
106 mMsgComplete = PR_TRUE;
107 return PR_SUCCESS;
110 PRStatus
111 ipcMessage::SetData(PRUint32 offset, const char *data, PRUint32 dataLen)
113 PR_ASSERT(mMsgHdr != NULL);
115 if (offset + dataLen > DataLen())
116 return PR_FAILURE;
118 memcpy((char *) Data() + offset, data, dataLen);
119 return PR_SUCCESS;
122 PRBool
123 ipcMessage::Equals(const nsID &target, const char *data, PRUint32 dataLen) const
125 return mMsgComplete &&
126 mMsgHdr->mTarget.Equals(target) &&
127 DataLen() == dataLen &&
128 memcmp(Data(), data, dataLen) == 0;
131 PRBool
132 ipcMessage::Equals(const ipcMessage *msg) const
134 PRUint32 msgLen = MsgLen();
135 return mMsgComplete && msg->mMsgComplete &&
136 msgLen == msg->MsgLen() &&
137 memcmp(MsgBuf(), msg->MsgBuf(), msgLen) == 0;
140 PRStatus
141 ipcMessage::WriteTo(char *buf,
142 PRUint32 bufLen,
143 PRUint32 *bytesWritten,
144 PRBool *complete)
146 if (!mMsgComplete)
147 return PR_FAILURE;
149 if (mMsgOffset == MsgLen()) {
150 *bytesWritten = 0;
151 *complete = PR_TRUE;
152 return PR_SUCCESS;
155 PRUint32 count = MsgLen() - mMsgOffset;
156 if (count > bufLen)
157 count = bufLen;
159 memcpy(buf, MsgBuf() + mMsgOffset, count);
160 mMsgOffset += count;
162 *bytesWritten = count;
163 *complete = (mMsgOffset == MsgLen());
165 return PR_SUCCESS;
168 PRStatus
169 ipcMessage::ReadFrom(const char *buf,
170 PRUint32 bufLen,
171 PRUint32 *bytesRead,
172 PRBool *complete)
174 *bytesRead = 0;
176 if (mMsgComplete) {
177 *complete = PR_TRUE;
178 return PR_SUCCESS;
181 if (mMsgHdr) {
182 // appending data to buffer
183 if (mMsgOffset < sizeof(PRUint32)) {
184 // we haven't learned the message length yet
185 if (mMsgOffset + bufLen < sizeof(PRUint32)) {
186 // we still don't know the length of the message!
187 memcpy((char *) mMsgHdr + mMsgOffset, buf, bufLen);
188 mMsgOffset += bufLen;
189 *bytesRead = bufLen;
190 *complete = PR_FALSE;
191 return PR_SUCCESS;
193 else {
194 // we now have enough data to determine the message length
195 PRUint32 count = sizeof(PRUint32) - mMsgOffset;
196 memcpy((char *) MsgBuf() + mMsgOffset, buf, count);
197 mMsgOffset += count;
198 buf += count;
199 bufLen -= count;
200 *bytesRead = count;
202 if (MsgLen() > IPC_MSG_GUESSED_SIZE) {
203 // realloc message buffer to the correct size
204 mMsgHdr = (ipcMessageHeader *) realloc(mMsgHdr, MsgLen());
209 else {
210 if (bufLen < sizeof(PRUint32)) {
211 // not enough data available in buffer to determine allocation size
212 // allocate a partial buffer
213 PRUint32 msgLen = IPC_MSG_GUESSED_SIZE;
214 mMsgHdr = (ipcMessageHeader *) malloc(msgLen);
215 if (!mMsgHdr)
216 return PR_FAILURE;
217 memcpy(mMsgHdr, buf, bufLen);
218 mMsgOffset = bufLen;
219 *bytesRead = bufLen;
220 *complete = PR_FALSE;
221 return PR_SUCCESS;
223 else {
224 PRUint32 msgLen = *(PRUint32 *) buf;
225 mMsgHdr = (ipcMessageHeader *) malloc(msgLen);
226 if (!mMsgHdr)
227 return PR_FAILURE;
228 mMsgHdr->mLen = msgLen;
229 mMsgOffset = 0;
233 // have mMsgHdr at this point
235 PRUint32 count = MsgLen() - mMsgOffset;
236 if (count > bufLen)
237 count = bufLen;
239 memcpy((char *) mMsgHdr + mMsgOffset, buf, count);
240 mMsgOffset += count;
241 *bytesRead += count;
243 *complete = mMsgComplete = (mMsgOffset == MsgLen());
244 return PR_SUCCESS;