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