Git init
[framework/uifw/xorg/lib/libice.git] / include / X11 / ICE / ICEmsg.h
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 = _offendingMinorOpcode; \
165     _pMsg->severity = _severity; \
166     _pMsg->offendingSequenceNum = _offendingSequenceNum; \
167     _pMsg->errorClass = _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 #ifndef WORD64
190
191 #define IceWriteData16(_iceConn, _bytes, _data) \
192     IceWriteData (_iceConn, _bytes, (char *) _data)
193
194 #define IceWriteData32(_iceConn, _bytes, _data) \
195     IceWriteData (_iceConn, _bytes, (char *) _data)
196
197 #else /* WORD64 */
198
199 /* IceWriteData16 and IceWriteData32 defined in misc.c for WORD64 */
200
201 #endif /* WORD64 */
202
203
204 /*
205  * The IceSendData macro bypasses copying the data to the
206  * ICE connection buffer and sends the data directly.  If necessary,
207  * the ICE connection buffer is first flushed.
208  */
209
210 #define IceSendData(_iceConn, _bytes, _data) \
211 { \
212     if (_iceConn->outbufptr > _iceConn->outbuf) \
213         IceFlush (_iceConn); \
214     _IceWrite (_iceConn, (unsigned long) (_bytes), _data); \
215 }
216
217
218 /*
219  * Write pad bytes.  Used to force 32 or 64 bit alignment.
220  * A maxium of 7 pad bytes can be specified.
221  */
222
223 #define IceWritePad(_iceConn, _bytes) \
224 { \
225     if ((_iceConn->outbufptr + (_bytes)) > _iceConn->outbufmax) \
226     { \
227         char _dummy[7]; \
228         IceFlush (_iceConn); \
229         _IceWrite (_iceConn, (unsigned long) (_bytes), _dummy); \
230     } \
231     else \
232     { \
233         _iceConn->outbufptr += (_bytes); \
234     } \
235 }
236
237
238 /*
239  * Macros for reading messages.
240  */
241
242 #define IceReadCompleteMessage(_iceConn, _headerSize, _msgType, _pMsg, _pData)\
243 { \
244     unsigned long _bytes; \
245     IceReadMessageHeader (_iceConn, _headerSize, _msgType, _pMsg); \
246     _bytes = (_pMsg->length << 3) - (_headerSize - SIZEOF (iceMsg)); \
247     if ((_iceConn->inbufmax - _iceConn->inbufptr) >= _bytes) \
248     { \
249         _IceRead (_iceConn, _bytes, _iceConn->inbufptr); \
250         _pData = _iceConn->inbufptr; \
251         _iceConn->inbufptr += _bytes; \
252     } \
253     else \
254     { \
255         _pData = (char *) malloc ((unsigned) _bytes); \
256         if (_pData) \
257             _IceRead (_iceConn, _bytes, _pData); \
258         else \
259             _IceReadSkip (_iceConn, _bytes); \
260     } \
261 }
262
263 #define IceDisposeCompleteMessage(_iceConn, _pData) \
264     if ((char *) _pData < _iceConn->inbuf || \
265         (char *) _pData >= _iceConn->inbufmax) \
266         free ((char *) _pData);
267
268
269 #define IceReadSimpleMessage(_iceConn, _msgType, _pMsg) \
270     _pMsg = (_msgType *) (_iceConn->inbuf);
271
272 #define IceReadMessageHeader(_iceConn, _headerSize, _msgType, _pMsg) \
273 { \
274     _IceRead (_iceConn, \
275         (unsigned long) (_headerSize - SIZEOF (iceMsg)), \
276         _iceConn->inbufptr); \
277     _pMsg = (_msgType *) (_iceConn->inbuf); \
278     _iceConn->inbufptr += (_headerSize - SIZEOF (iceMsg)); \
279 }
280
281 #define IceReadData(_iceConn, _bytes, _pData) \
282     _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
283
284 #ifndef WORD64
285
286 #define IceReadData16(_iceConn, _swap, _bytes, _pData) \
287 { \
288     _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
289 }
290
291 #define IceReadData32(_iceConn, _swap, _bytes, _pData) \
292 { \
293     _IceRead (_iceConn, (unsigned long) (_bytes), (char *) _pData); \
294 }
295
296 #else /* WORD64 */
297
298 /* IceReadData16 and IceReadData32 defined in misc.c for WORD64 */
299
300 #endif /* WORD64 */
301
302
303 /*
304  * Read pad bytes (for 32 or 64 bit alignment).
305  * A maxium of 7 pad bytes can be specified.
306  */
307
308 #define IceReadPad(_iceConn, _bytes) \
309 { \
310     char _dummy[7]; \
311     _IceRead (_iceConn, (unsigned long) (_bytes), _dummy); \
312 }
313
314 _XFUNCPROTOEND
315
316 #endif /* _ICEMSG_H_ */