1/* $Xorg: xfindproxy.h,v 1.4 2001/02/09 02:05:42 xorgcvs Exp $ */
2
3/*
4Copyright 1996, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included
13in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall
24not be used in advertising or otherwise to promote the sale, use or
25other dealings in this Software without prior written authorization
26from The Open Group.
27*/
28
29
30/*
31 * Pad to a 64 bit boundary
32 */
33
34#define PAD64(_bytes) ((8 - ((size_t) (_bytes) % 8)) % 8)
35
36#define PADDED_BYTES64(_bytes) (_bytes + PAD64 (_bytes))
37
38
39/*
40 * Number of 8 byte units in _bytes.
41 */
42
43#define WORD64COUNT(_bytes) (((size_t) ((_bytes) + 7)) >> 3)
44
45
46/*
47 * Compute the number of bytes for a STRING representation
48 */
49
50static inline size_t
51STRING_BYTES(const char *string) {
52    size_t len = string ? strlen (string) : 0;
53    return (2 + len + PAD64 (2 + len));
54}
55
56
57#define SKIP_STRING(_pBuf, _swap) \
58{ \
59    CARD16 _len; \
60    EXTRACT_CARD16 (_pBuf, _swap, _len); \
61    _pBuf += _len; \
62    if (PAD64 (2 + _len)) \
63        _pBuf += PAD64 (2 + _len); \
64}
65
66/*
67 * STORE macros
68 */
69
70#define STORE_CARD16(_pBuf, _val) \
71{ \
72    *((CARD16 *) _pBuf) = _val; \
73    _pBuf += 2; \
74}
75
76static inline char *
77store_string(char *pBuf, const char *string)
78{
79    size_t len = string ? strlen (string) : 0;
80    STORE_CARD16 (pBuf, (CARD16) len);
81    if (len) {
82        memcpy (pBuf, string, len);
83        pBuf += len;
84    }
85    if (PAD64 (2 + len))
86        pBuf += PAD64 (2 + len);
87    return pBuf;
88}
89
90#define STORE_STRING(_pBuf, _string) _pBuf = store_string(_pBuf, _string)
91
92/*
93 * EXTRACT macros
94 */
95
96#define EXTRACT_CARD16(_pBuf, _swap, _val) \
97{ \
98    _val = *((CARD16 *) _pBuf); \
99    _pBuf += 2; \
100    if (_swap) \
101        _val = lswaps (_val); \
102}
103
104#define EXTRACT_STRING(_pBuf, _swap, _string) \
105{ \
106    CARD16 _len; \
107    EXTRACT_CARD16 (_pBuf, _swap, _len); \
108    _string = malloc (_len + 1); \
109    memcpy (_string, _pBuf, _len); \
110    _string[_len] = '\0'; \
111    _pBuf += _len; \
112    if (PAD64 (2 + _len)) \
113        _pBuf += PAD64 (2 + _len); \
114}
115
116
117/*
118 * Byte swapping
119 */
120
121/* byte swap a long literal */
122#define lswapl(_val) ((((_val) & 0xff) << 24) |\
123		   (((_val) & 0xff00) << 8) |\
124		   (((_val) & 0xff0000) >> 8) |\
125		   (((_val) >> 24) & 0xff))
126
127/* byte swap a short literal */
128#define lswaps(_val) ((((_val) & 0xff) << 8) | (((_val) >> 8) & 0xff))
129
130
131#define CHECK_AT_LEAST_SIZE(_iceConn, _majorOp, _minorOp, _expected_len, _actual_len, _severity) \
132    if ((((_actual_len) - SIZEOF (iceMsg)) >> 3) > _expected_len) \
133    { \
134       _IceErrorBadLength (_iceConn, _majorOp, _minorOp, _severity); \
135       return; \
136    }
137
138
139#define CHECK_COMPLETE_SIZE(_iceConn, _majorOp, _minorOp, _expected_len, _actual_len, _pStart, _severity) \
140    if (((PADDED_BYTES64((_actual_len)) - SIZEOF (iceMsg)) >> 3) \
141        != _expected_len) \
142    { \
143       _IceErrorBadLength (_iceConn, _majorOp, _minorOp, _severity); \
144       IceDisposeCompleteMessage (iceConn, _pStart); \
145       return; \
146    }
147