9bce6aca3c7ef469c82028cf861efda3f519d1f1
[oweals/cde.git] / cde / lib / tt / lib / util / tt_xdr_utils.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 //%%  (c) Copyright 1993, 1994 Hewlett-Packard Company                  
24 //%%  (c) Copyright 1993, 1994 International Business Machines Corp.    
25 //%%  (c) Copyright 1993, 1994 Sun Microsystems, Inc.                   
26 //%%  (c) Copyright 1993, 1994 Novell, Inc.                             
27 //%%  $XConsortium: tt_xdr_utils.C /main/6 1996/08/29 18:08:20 drk $                                                    
28 /*
29  *
30  * tt_xdr_utils.cc
31  *
32  * Copyright (c) 1990 by Sun Microsystems, Inc.
33  */
34 #include <stdint.h>
35 #if defined(ultrix)
36 #include <rpc/types.h>
37 #define bool_t int
38 #endif
39 #include <rpc/rpc.h>
40 #include <util/tt_xdr_utils.h>
41 #include <memory.h>
42 #include "tt_options.h"
43
44 #if defined(__OpenBSD__)
45 #define XDR __rpc_xdr
46 #endif
47
48 typedef bool_t (*local_xdrproc_t)(XDR *, caddr_t *);
49
50 #ifndef OPT_XDR_LONG_TYPE
51 #if defined(OPT_CONST_CORRECT)
52 # define OPT_XDR_LONG_TYPE      const long
53 #else
54 # define OPT_XDR_LONG_TYPE      long
55 #endif
56 #endif
57 static bool_t
58 tt_x_putlong(XDR *xp, OPT_XDR_LONG_TYPE *)
59 {
60     xp->x_handy += 4;
61     return TRUE;
62 }
63
64 static bool_t
65 /* The second argument is caddr_t and not void * in both SUN, DEC headers
66  * files.
67  * The third agrument is also wrong. Both SUN and DEC system header files
68  * expect int instead of u_int.
69  */
70 #if defined(CSRG_BASED)
71 #if defined(OPT_CONST_CORRECT)
72 tt_x_putbytes(XDR *xp, const char *, unsigned int len)
73 #else
74 tt_x_putbytes(XDR *xp, caddr_t, unsigned int len)
75 #endif
76 #else
77 tt_x_putbytes(XDR *xp, caddr_t, int len)
78 #endif
79 {
80     xp->x_handy += RNDUP (len);
81     return TRUE;
82 }
83
84 #if defined(ultrix) || defined(__osf__)
85 static int*
86 #elif defined(CSRG_BASED)
87 static int32_t*
88 #else
89 static long *
90 #endif
91 #if defined(CSRG_BASED)
92 tt_x_inline(XDR *xp, unsigned int len)
93 #else
94 tt_x_inline(XDR *xp, int len)
95 #endif
96 {
97         /* Be paranoid -- some code really expects inline to
98          * always succeed, so we keep a small buffer around
99          * just in case.  Not too paranoid, though -- it's
100          * legal to not support inline!
101          */
102     /* It is better to promote len to caddr_t than demote x_base to
103        int for 64 bit arch.
104     */
105     if (len > 0 && (caddr_t) (intptr_t) len < xp->x_base) {
106         xp->x_handy += RNDUP (len);
107 #if defined(ultrix) || defined(__osf__)
108         return (int *) xp->x_private;
109 #elif defined(CSRG_BASED)
110         return (int32_t *) xp->x_private;
111 #else
112         return (long *) xp->x_private;
113 #endif
114     } else
115         return 0;
116 }
117
118
119 unsigned long
120 _tt_xdr_sizeof(xdrproc_t f, void *data)
121 {
122         _Tt_xdr_size_stream x;
123
124         if ((*(local_xdrproc_t)f) ((XDR *)x, (caddr_t *)data) == TRUE) {
125                 return x.getsize();
126         } else {
127                 return 0;
128         }
129 }
130
131 _Tt_xdr_size_stream::
132 _Tt_xdr_size_stream() {
133         memset (&ops, 0, sizeof ops);
134 #if defined(OPT_BUG_SUNOS_4) || defined(OPT_BUG_HPUX)
135         ops.x_putlong = (int (*)(...))tt_x_putlong;
136         ops.x_putbytes = (int (*)(...))tt_x_putbytes;
137         ops.x_inline = (long *(*)(...))tt_x_inline;
138 #elif defined(OPT_BUG_AIX)
139         ops.x_putlong = (int (*)(XDR *, long *))tt_x_putlong;
140         ops.x_putbytes = (int (*)(XDR *, caddr_t, u_int))tt_x_putbytes;
141         ops.x_inline = (long *(*)(XDR *, u_int))tt_x_inline;
142 #elif defined(OPT_BUG_SUNOS_5)
143         ops.x_putlong = tt_x_putlong;
144         ops.x_putbytes = (bool_t (*)(XDR *, caddr_t, int))tt_x_putbytes;
145         ops.x_inline = tt_x_inline;
146 #elif defined(CRAY)
147         ops.x_putlong = tt_x_putlong;
148         ops.x_putbytes = tt_x_putbytes;
149         ops.x_inline = (inline_t *(*)(...))tt_x_inline;
150 #elif defined(OPT_BUG_USL) || defined(OPT_BUG_UXP)
151         ops.x_putlong = tt_x_putlong;
152         ops.x_putbytes = (bool_t (*)(XDR *, caddr_t, u_int))  tt_x_putbytes;
153 #if defined(OPT_BUG_UW_1)
154         ops.x_inline = (long *(*)(struct __XDR *, const int)) tt_x_inline;
155 #else
156         ops.x_inline = (long *(*)(struct XDR *, int)) tt_x_inline;
157 #endif /* OPT_BUG_UW1 */
158 #else
159         ops.x_putlong = tt_x_putlong;
160         ops.x_putbytes = tt_x_putbytes;
161         ops.x_inline = tt_x_inline;
162 #endif  
163         xdrstream.x_op = XDR_ENCODE;
164         xdrstream.x_ops = &ops;
165         xdrstream.x_handy = 0;
166         xdrstream.x_private = (caddr_t) buf;
167         xdrstream.x_base = (caddr_t) sizeof buf;
168 }
169
170 _Tt_xdr_size_stream::
171 operator XDR *() {
172         return(&xdrstream);
173 }