1 /******************************************************************************
2 
3 
4 Copyright 1993, 1998  The Open Group
5 
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
10 documentation.
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 Except as contained in this notice, the name of The Open Group shall not be
23 used in advertising or otherwise to promote the sale, use or other dealings
24 in this Software without prior written authorization from The Open Group.
25 
26 Author: Ralph Mor, X Consortium
27 ******************************************************************************/
28 
29 #ifndef _ICEMSG_H_
30 #define _ICEMSG_H_
31 
32 #include <X11/Xfuncproto.h>
33 
34 #include <X11/ICE/ICEconn.h>
35 
36 _XFUNCPROTOBEGIN
37 
38 /*
39  * Function prototypes for internal ICElib functions
40  */
41 
42 extern Status _IceRead (
43     IceConn		/* iceConn */,
44     unsigned long	/* nbytes */,
45     char *		/* ptr */
46 );
47 
48 extern void _IceReadSkip (
49     IceConn		/* iceConn */,
50     unsigned long	/* nbytes */
51 );
52 
53 extern void _IceWrite (
54     IceConn		/* iceConn */,
55     unsigned long	/* nbytes */,
56     char *		/* ptr */
57 );
58 
59 
60 extern void _IceErrorBadMinor (
61     IceConn		/* iceConn */,
62     int			/* majorOpcode */,
63     int			/* offendingMinor */,
64     int			/* severity */
65 );
66 
67 extern void _IceErrorBadState (
68     IceConn		/* iceConn */,
69     int			/* majorOpcode */,
70     int			/* offendingMinor */,
71     int			/* severity */
72 );
73 
74 extern void _IceErrorBadLength (
75     IceConn		/* iceConn */,
76     int			/* majorOpcode */,
77     int			/* offendingMinor */,
78     int			/* severity */
79 );
80 
81 extern void _IceErrorBadValue (
82     IceConn		/* iceConn */,
83     int			/* majorOpcode */,
84     int			/* offendingMinor */,
85     int			/* offset */,
86     int			/* length */,
87     IcePointer		/* value */
88 );
89 
90 extern IcePoAuthStatus _IcePoMagicCookie1Proc (
91     IceConn		/* iceConn */,
92     IcePointer *	/* authStatePtr */,
93     Bool 		/* cleanUp */,
94     Bool		/* swap */,
95     int     		/* authDataLen */,
96     IcePointer		/* authData */,
97     int *		/* replyDataLenRet */,
98     IcePointer *	/* replyDataRet */,
99     char **		/* errorStringRet */
100 );
101 
102 extern IcePaAuthStatus _IcePaMagicCookie1Proc (
103     IceConn		/* iceConn */,
104     IcePointer *	/* authStatePtr */,
105     Bool		/* swap */,
106     int     		/* authDataLen */,
107     IcePointer		/* authData */,
108     int *		/* replyDataLenRet */,
109     IcePointer *	/* replyDataRet */,
110     char **		/* errorStringRet */
111 );
112 
113 
114 /*
115  * Macro to check if IO operations are valid on an ICE connection.
116  */
117 
118 #define IceValidIO(_iceConn) _iceConn->io_ok
119 
120 
121 /*
122  * Macros for writing messages.
123  */
124 
125 #define IceGetHeader(_iceConn, _major, _minor, _headerSize, _msgType, _pMsg) \
126     if ((_iceConn->outbufptr + _headerSize) > _iceConn->outbufmax) \
127         IceFlush (_iceConn); \
128     _pMsg = (_msgType *) _iceConn->outbufptr; \
129     _pMsg->majorOpcode = _major; \
130     _pMsg->minorOpcode = _minor; \
131     _pMsg->length = (_headerSize - SIZEOF (iceMsg)) >> 3; \
132     _iceConn->outbufptr += _headerSize; \
133     _iceConn->send_sequence++
134 
135 #define IceGetHeaderExtra(_iceConn, _major, _minor, _headerSize, _extra, _msgType, _pMsg, _pData) \
136     if ((_iceConn->outbufptr + \
137 	_headerSize + ((_extra) << 3)) > _iceConn->outbufmax) \
138         IceFlush (_iceConn); \
139     _pMsg = (_msgType *) _iceConn->outbufptr; \
140     if ((_iceConn->outbufptr + \
141 	_headerSize + ((_extra) << 3)) <= _iceConn->outbufmax) \
142         _pData = (char *) _pMsg + _headerSize; \
143     else \
144         _pData = NULL; \
145     _pMsg->majorOpcode = _major; \
146     _pMsg->minorOpcode = _minor; \
147     _pMsg->length = ((_headerSize - SIZEOF (iceMsg)) >> 3) + (_extra); \
148     _iceConn->outbufptr += (_headerSize + ((_extra) << 3)); \
149     _iceConn->send_sequence++
150 
151 #define IceSimpleMessage(_iceConn, _major, _minor) \
152 { \
153     iceMsg *_pMsg; \
154     IceGetHeader (_iceConn, _major, _minor, SIZEOF (iceMsg), iceMsg, _pMsg); \
155 }
156 
157 #define IceErrorHeader(_iceConn, _offendingMajorOpcode, _offendingMinorOpcode, _offendingSequenceNum, _severity, _errorClass, _dataLength) \
158 { \
159     iceErrorMsg	*_pMsg; \
160 \
161     IceGetHeader (_iceConn, _offendingMajorOpcode, ICE_Error, \
162 	SIZEOF (iceErrorMsg), iceErrorMsg, _pMsg); \
163     _pMsg->length += (_dataLength); \
164     _pMsg->offendingMinorOpcode = (CARD8) _offendingMinorOpcode; \
165     _pMsg->severity = (CARD8) _severity; \
166     _pMsg->offendingSequenceNum = (CARD32) _offendingSequenceNum; \
167     _pMsg->errorClass = (CARD16) _errorClass; \
168 }
169 
170 
171 /*
172  * Write data into the ICE output buffer.
173  */
174 
175 #define IceWriteData(_iceConn, _bytes, _data) \
176 { \
177     if ((_iceConn->outbufptr + (_bytes)) > _iceConn->outbufmax) \
178     { \
179 	IceFlush (_iceConn); \
180         _IceWrite (_iceConn, (unsigned long) (_bytes), _data); \
181     } \
182     else \
183     { \
184         memcpy (_iceConn->outbufptr, _data, _bytes); \
185         _iceConn->outbufptr += (_bytes); \
186     } \
187 }
188 
189 #define IceWriteData16(_iceConn, _bytes, _data) \
190     IceWriteData (_iceConn, _bytes, (char *) _data)
191 
192 #define IceWriteData32(_iceConn, _bytes, _data) \
193     IceWriteData (_iceConn, _bytes, (char *) _data)
194 
195 
196 /*
197  * The IceSendData macro bypasses copying the data to the
198  * ICE connection buffer and sends the data directly.  If necessary,
199  * the ICE connection buffer is first flushed.
200  */
201 
202 #define IceSendData(_iceConn, _bytes, _data) \
203 { \
204     if (_iceConn->outbufptr > _iceConn->outbuf) \
205 	IceFlush (_iceConn); \
206     _IceWrite (_iceConn, (unsigned long) (_bytes), _data); \
207 }
208 
209 
210 /*
211  * Write pad bytes.  Used to force 32 or 64 bit alignment.
212  * A maximum of 7 pad bytes can be specified.
213  */
214 
215 #define IceWritePad(_iceConn, _bytes) \
216 { \
217     char _dummy[7] = { 0 }; \
218     IceWriteData (_iceConn, (_bytes), _dummy); \
219 }
220 
221 
222 /*
223  * Macros for reading messages.
224  */
225 
226 #define IceReadCompleteMessage(_iceConn, _headerSize, _msgType, _pMsg, _pData)\
227 { \
228     unsigned long _bytes; \
229     IceReadMessageHeader (_iceConn, _headerSize, _msgType, _pMsg); \
230     _bytes = (_pMsg->length << 3) - (_headerSize - SIZEOF (iceMsg)); \
231     if ((_iceConn->inbufmax - _iceConn->inbufptr) >= _bytes) \
232     { \
233 	_IceRead (_iceConn, _bytes, _iceConn->inbufptr); \
234 	_pData = _iceConn->inbufptr; \
235 	_iceConn->inbufptr += _bytes; \
236     } \
237     else \
238     { \
239 	_pData = malloc (_bytes); \
240         if (_pData) \
241 	    _IceRead (_iceConn, _bytes, _pData); \
242         else \
243 	    _IceReadSkip (_iceConn, _bytes); \
244     } \
245 }
246 
247 #define IceDisposeCompleteMessage(_iceConn, _pData) \
248     if ((char *) _pData < _iceConn->inbuf || \
249 	(char *) _pData >= _iceConn->inbufmax) \
250         free (_pData);
251 
252 
253 #define IceReadSimpleMessage(_iceConn, _msgType, _pMsg) \
254     _pMsg = (_msgType *) (_iceConn->inbuf);
255 
256 #define IceReadMessageHeader(_iceConn, _headerSize, _msgType, _pMsg) \
257 { \
258     _IceRead (_iceConn, \
259 	(unsigned long) (_headerSize - SIZEOF (iceMsg)), \
260 	_iceConn->inbufptr); \
261     _pMsg = (_msgType *) (_iceConn->inbuf); \
262     _iceConn->inbufptr += (_headerSize - SIZEOF (iceMsg)); \
263 }
264 
265 #define IceReadData(_iceConn, _bytes, _pData) \
266     _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
267 
268 #define IceReadData16(_iceConn, _swap, _bytes, _pData) \
269 { \
270     _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
271 }
272 
273 #define IceReadData32(_iceConn, _swap, _bytes, _pData) \
274 { \
275     _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
276 }
277 
278 
279 /*
280  * Read pad bytes (for 32 or 64 bit alignment).
281  * A maxium of 7 pad bytes can be specified.
282  */
283 
284 #define IceReadPad(_iceConn, _bytes) \
285 { \
286     char _dummy[7]; \
287     _IceRead (_iceConn, (unsigned long) (_bytes), _dummy); \
288 }
289 
290 _XFUNCPROTOEND
291 
292 #endif /* _ICEMSG_H_ */
293