ReactOS  0.4.15-dev-1171-gab82533
xdr_array.c
Go to the documentation of this file.
1 
2 /*
3  * Copyright (c) 2009, Sun Microsystems, Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  * - Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * - Neither the name of Sun Microsystems, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 //#include <sys/cdefs.h>
31 
32 /*
33  * xdr_array.c, Generic XDR routines impelmentation.
34  *
35  * Copyright (C) 1984, Sun Microsystems, Inc.
36  *
37  * These are the "non-trivial" xdr primitives used to serialize and de-serialize
38  * arrays. See xdr.h for more info on the interface to xdr.
39  */
40 
41 #include <wintirpc.h>
42 #include "namespace.h"
43 //#include <err.h>
44 //#include <limits.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 
49 #include <rpc/types.h>
50 #include <rpc/xdr.h>
51 #include "un-namespace.h"
52 
53 /*
54  * XDR an array of arbitrary elements
55  * *addrp is a pointer to the array, *sizep is the number of elements.
56  * If addrp is NULL (*sizep * elsize) bytes are allocated.
57  * elsize is the size (in bytes) of each element, and elproc is the
58  * xdr procedure to call to handle each element of the array.
59  */
60 bool_t
61 xdr_array(xdrs, addrp, sizep, maxsize, elsize, elproc)
62  XDR *xdrs;
63  caddr_t *addrp; /* array pointer */
64  u_int *sizep; /* number of elements */
65  u_int maxsize; /* max numberof elements */
66  u_int elsize; /* size in bytes of each element */
67  xdrproc_t elproc; /* xdr routine to handle each element */
68 {
69  u_int i;
70  caddr_t target = *addrp;
71  u_int c; /* the actual element count */
72  bool_t stat = TRUE;
73  u_int nodesize;
74 
75  /* like strings, arrays are really counted arrays */
76  if (!xdr_u_int(xdrs, sizep)) {
77  return (FALSE);
78  }
79  c = *sizep;
80  if ((c > maxsize || UINT_MAX/elsize < c) &&
81  (xdrs->x_op != XDR_FREE)) {
82  return (FALSE);
83  }
84  nodesize = c * elsize;
85 
86  /*
87  * if we are deserializing, we may need to allocate an array.
88  * We also save time by checking for a null array if we are freeing.
89  */
90  if (target == NULL)
91  switch (xdrs->x_op) {
92  case XDR_DECODE:
93  if (c == 0)
94  return (TRUE);
95  *addrp = target = mem_alloc(nodesize);
96  if (target == NULL) {
97  //warnx("xdr_array: out of memory");
98  return (FALSE);
99  }
100  memset(target, 0, nodesize);
101  break;
102 
103  case XDR_FREE:
104  return (TRUE);
105 
106  case XDR_ENCODE:
107  break;
108  }
109 
110  /*
111  * now we xdr each element of array
112  */
113  for (i = 0; (i < c) && stat; i++) {
114  stat = (*elproc)(xdrs, target);
115  target += elsize;
116  }
117 
118  /*
119  * the array may need freeing
120  */
121  if (xdrs->x_op == XDR_FREE) {
122  mem_free(*addrp, nodesize);
123  *addrp = NULL;
124  }
125  return (stat);
126 }
127 
128 /*
129  * xdr_vector():
130  *
131  * XDR a fixed length array. Unlike variable-length arrays,
132  * the storage of fixed length arrays is static and unfreeable.
133  * > basep: base of the array
134  * > size: size of the array
135  * > elemsize: size of each element
136  * > xdr_elem: routine to XDR each element
137  */
138 bool_t
139 xdr_vector(xdrs, basep, nelem, elemsize, xdr_elem)
140  XDR *xdrs;
141  char *basep;
142  u_int nelem;
143  u_int elemsize;
144  xdrproc_t xdr_elem;
145 {
146  u_int i;
147  char *elptr;
148 
149  elptr = basep;
150  for (i = 0; i < nelem; i++) {
151  if (!(*xdr_elem)(xdrs, elptr)) {
152  return(FALSE);
153  }
154  elptr += elemsize;
155  }
156  return(TRUE);
157 }
bool_t xdr_array(XDR *xdrs, caddr_t *addrp, u_int *sizep, u_int maxsize, u_int elsize, xdrproc_t elproc)
Definition: xdr_array.c:61
int32_t bool_t
Definition: types.h:101
#define TRUE
Definition: types.h:120
char * caddr_t
Definition: rosdhcp.h:36
Definition: xdr.h:103
bool_t(* xdrproc_t)(XDR *,...)
Definition: xdr.h:144
#define nelem(x)
Definition: shaptest.c:19
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define mem_alloc(bsize)
Definition: types.h:123
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:416
bool_t xdr_u_int(XDR *xdrs, u_int *up)
Definition: xdr.c:133
const GLubyte * c
Definition: glext.h:8905
enum xdr_op x_op
Definition: xdr.h:104
Definition: xdr.h:85
Definition: stat.h:55
#define UINT_MAX
Definition: limits.h:41
UINT elemsize
Definition: safearray.c:332
UINT32 u_int
Definition: types.h:82
Definition: xdr.h:87
bool_t xdr_vector(XDR *xdrs, char *basep, u_int nelem, u_int elemsize, xdrproc_t xdr_elem)
Definition: xdr_array.c:139
#define c
Definition: ke_i.h:80
Definition: xdr.h:86
GLenum target
Definition: glext.h:7315
#define mem_free(ptr, bsize)
Definition: types.h:124
#define memset(x, y, z)
Definition: compat.h:39