upload tizen2.0 source
[framework/uifw/xorg/lib/libxdmcp.git] / Array.c
1 /*
2 Copyright 1989, 1998  The Open Group
3
4 Permission to use, copy, modify, distribute, and sell this software and its
5 documentation for any purpose is hereby granted without fee, provided that
6 the above copyright notice appear in all copies and that both that
7 copyright notice and this permission notice appear in supporting
8 documentation.
9
10 The above copyright notice and this permission notice shall be included in
11 all copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
16 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
17 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
20 Except as contained in this notice, the name of The Open Group shall not be
21 used in advertising or otherwise to promote the sale, use or other dealings
22 in this Software without prior written authorization from The Open Group.
23  *
24  * Author:  Keith Packard, MIT X Consortium
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include <X11/Xos.h>
31 #include <X11/X.h>
32 #include <X11/Xmd.h>
33 #include <X11/Xdmcp.h>
34 #include <stdint.h>
35 #include <stdlib.h>
36
37 /*
38  * This variant of malloc does not return NULL if zero size is passed into.
39  */
40 static void *
41 xmalloc(size_t size)
42 {
43     return malloc(size ? size : 1);
44 }
45
46 /*
47  * This variant of realloc does not return NULL if zero size is passed into
48  */
49 static void *
50 xrealloc(void *ptr, size_t size)
51 {
52     return realloc(ptr, size ? size : 1);
53 }
54
55 int
56 XdmcpAllocARRAY8 (ARRAY8Ptr array, int length)
57 {
58     CARD8Ptr    newData;
59
60     /* length defined in ARRAY8 struct is a CARD16 (not CARD8 like the rest) */
61     if (length > UINT16_MAX)
62         return FALSE;
63
64     newData = (CARD8Ptr) xmalloc(length * sizeof (CARD8));
65     if (!newData)
66         return FALSE;
67     array->length = (CARD16) length;
68     array->data = newData;
69     return TRUE;
70 }
71
72 int
73 XdmcpAllocARRAY16 (ARRAY16Ptr array, int length)
74 {
75     CARD16Ptr   newData;
76
77     /* length defined in ARRAY16 struct is a CARD8 */
78     if (length > UINT8_MAX)
79         return FALSE;
80
81     newData = (CARD16Ptr) xmalloc(length * sizeof (CARD16));
82     if (!newData)
83         return FALSE;
84     array->length = (CARD8) length;
85     array->data = newData;
86     return TRUE;
87 }
88
89 int
90 XdmcpAllocARRAY32 (ARRAY32Ptr array, int length)
91 {
92     CARD32Ptr   newData;
93
94     /* length defined in ARRAY32 struct is a CARD8 */
95     if (length > UINT8_MAX)
96         return FALSE;
97
98     newData = (CARD32Ptr) xmalloc(length * sizeof (CARD32));
99     if (!newData)
100         return FALSE;
101     array->length = (CARD8) length;
102     array->data = newData;
103     return TRUE;
104 }
105
106 int
107 XdmcpAllocARRAYofARRAY8 (ARRAYofARRAY8Ptr array, int length)
108 {
109     ARRAY8Ptr   newData;
110
111     /* length defined in ARRAYofARRAY8 struct is a CARD8 */
112     if (length > UINT8_MAX)
113         return FALSE;
114
115     newData = (ARRAY8Ptr) xmalloc(length * sizeof (ARRAY8));
116     if (!newData)
117         return FALSE;
118     array->length = (CARD8) length;
119     array->data = newData;
120     return TRUE;
121 }
122
123 int
124 XdmcpARRAY8Equal (const ARRAY8Ptr array1, const ARRAY8Ptr array2)
125 {
126     if (array1->length != array2->length)
127         return FALSE;
128     if (memcmp(array1->data, array2->data, array1->length) != 0)
129         return FALSE;
130     return TRUE;
131 }
132
133 int
134 XdmcpCopyARRAY8 (const ARRAY8Ptr src, ARRAY8Ptr dst)
135 {
136     dst->length = src->length;
137     dst->data = (CARD8 *) xmalloc(dst->length * sizeof (CARD8));
138     if (!dst->data)
139         return FALSE;
140     memmove (dst->data, src->data, src->length * sizeof (CARD8));
141     return TRUE;
142 }
143
144 int
145 XdmcpReallocARRAY8 (ARRAY8Ptr array, int length)
146 {
147     CARD8Ptr    newData;
148
149     /* length defined in ARRAY8 struct is a CARD16 (not CARD8 like the rest) */
150     if (length > UINT16_MAX)
151         return FALSE;
152
153     newData = (CARD8Ptr) xrealloc(array->data, length * sizeof (CARD8));
154     if (!newData)
155         return FALSE;
156     array->length = (CARD16) length;
157     array->data = newData;
158     return TRUE;
159 }
160
161 int
162 XdmcpReallocARRAYofARRAY8 (ARRAYofARRAY8Ptr array, int length)
163 {
164     ARRAY8Ptr   newData;
165
166     /* length defined in ARRAYofARRAY8 struct is a CARD8 */
167     if (length > UINT8_MAX)
168         return FALSE;
169
170     newData = (ARRAY8Ptr) xrealloc(array->data, length * sizeof (ARRAY8));
171     if (!newData)
172         return FALSE;
173     array->length = (CARD8) length;
174     array->data = newData;
175     return TRUE;
176 }
177
178 int
179 XdmcpReallocARRAY16 (ARRAY16Ptr array, int length)
180 {
181     CARD16Ptr   newData;
182
183     /* length defined in ARRAY16 struct is a CARD8 */
184     if (length > UINT8_MAX)
185         return FALSE;
186     newData = (CARD16Ptr) xrealloc(array->data, length * sizeof (CARD16));
187     if (!newData)
188         return FALSE;
189     array->length = (CARD8) length;
190     array->data = newData;
191     return TRUE;
192 }
193
194 int
195 XdmcpReallocARRAY32 (ARRAY32Ptr array, int length)
196 {
197     CARD32Ptr   newData;
198
199     /* length defined in ARRAY32 struct is a CARD8 */
200     if (length > UINT8_MAX)
201         return FALSE;
202
203     newData = (CARD32Ptr) xrealloc(array->data, length * sizeof (CARD32));
204     if (!newData)
205         return FALSE;
206     array->length = (CARD8) length;
207     array->data = newData;
208     return TRUE;
209 }
210
211 void
212 XdmcpDisposeARRAY8 (ARRAY8Ptr array)
213 {
214     free(array->data);
215     array->length = 0;
216     array->data = NULL;
217 }
218
219 void
220 XdmcpDisposeARRAY16 (ARRAY16Ptr array)
221 {
222     free(array->data);
223     array->length = 0;
224     array->data = NULL;
225 }
226
227 void
228 XdmcpDisposeARRAY32 (ARRAY32Ptr array)
229 {
230     free(array->data);
231     array->length = 0;
232     array->data = NULL;
233 }
234
235 void
236 XdmcpDisposeARRAYofARRAY8 (ARRAYofARRAY8Ptr array)
237 {
238     int i;
239
240     if (array->data != NULL) {
241         for (i = 0; i < (int)array->length; i++)
242             XdmcpDisposeARRAY8 (&array->data[i]);
243         free(array->data);
244     }
245     array->length = 0;
246     array->data = NULL;
247 }