summaryrefslogtreecommitdiff
path: root/lib/libXdmcp/Array.c
blob: 6b9b617f7bc6c32f20e299452770d5d7f1625101 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/*
Copyright 1989, 1998  The Open Group

Permission to use, copy, modify, distribute, and sell this software and its
documentation for any purpose is hereby granted without fee, provided that
the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation.

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of The Open Group shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from The Open Group.
 *
 * Author:  Keith Packard, MIT X Consortium
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <X11/Xos.h>
#include <X11/X.h>
#include <X11/Xmd.h>
#include <X11/Xdmcp.h>
#include <stdint.h>
#include <stdlib.h>

/*
 * This variant of malloc does not return NULL if zero size is passed into.
 */
static void *
xmalloc(size_t size)
{
    return malloc(size ? size : 1);
}

/*
 * This variant of calloc does not return NULL if zero count is passed into.
 */
static void *
xcalloc(size_t n, size_t size)
{
    return calloc(n ? n : 1, size);
}

/*
 * This variant of realloc does not return NULL if zero size is passed into
 */
static void *
xrealloc(void *ptr, size_t size)
{
    return realloc(ptr, size ? size : 1);
}

int
XdmcpAllocARRAY8 (ARRAY8Ptr array, int length)
{
    /* length defined in ARRAY8 struct is a CARD16 (not CARD8 like the rest) */
    if ((length > UINT16_MAX) || (length < 0))
        array->data = NULL;
    else
        array->data = xmalloc(length * sizeof (CARD8));

    if (array->data == NULL) {
	array->length = 0;
	return FALSE;
    }
    array->length = (CARD16) length;
    return TRUE;
}

int
XdmcpAllocARRAY16 (ARRAY16Ptr array, int length)
{
    /* length defined in ARRAY16 struct is a CARD8 */
    if ((length > UINT8_MAX) || (length < 0))
        array->data = NULL;
    else
        array->data = xmalloc(length * sizeof (CARD16));

    if (array->data == NULL) {
	array->length = 0;
	return FALSE;
    }
    array->length = (CARD8) length;
    return TRUE;
}

int
XdmcpAllocARRAY32 (ARRAY32Ptr array, int length)
{
    /* length defined in ARRAY32 struct is a CARD8 */
    if ((length > UINT8_MAX) || (length < 0))
        array->data = NULL;
    else
        array->data = xmalloc(length * sizeof (CARD32));

    if (array->data == NULL) {
	array->length = 0;
	return FALSE;
    }
    array->length = (CARD8) length;
    return TRUE;
}

int
XdmcpAllocARRAYofARRAY8 (ARRAYofARRAY8Ptr array, int length)
{
    /* length defined in ARRAYofARRAY8 struct is a CARD8 */
    if ((length > UINT8_MAX) || (length < 0))
        array->data = NULL;
    else
        /*
         * Use calloc to ensure the pointers are cleared out so we
         * don't try to free garbage if XdmcpDisposeARRAYofARRAY8()
         * is called before the caller sets them to valid pointers.
         */
        array->data = xcalloc(length, sizeof (ARRAY8));

    if (array->data == NULL) {
	array->length = 0;
	return FALSE;
    }
    array->length = (CARD8) length;
    return TRUE;
}

int
XdmcpARRAY8Equal (const ARRAY8Ptr array1, const ARRAY8Ptr array2)
{
    if (array1->length != array2->length)
	return FALSE;
    if (memcmp(array1->data, array2->data, array1->length) != 0)
	return FALSE;
    return TRUE;
}

int
XdmcpCopyARRAY8 (const ARRAY8Ptr src, ARRAY8Ptr dst)
{
    if (!XdmcpAllocARRAY8(dst, src->length))
	return FALSE;
    memmove (dst->data, src->data, src->length * sizeof (CARD8));
    return TRUE;
}

int
XdmcpReallocARRAY8 (ARRAY8Ptr array, int length)
{
    CARD8Ptr	newData;

    /* length defined in ARRAY8 struct is a CARD16 (not CARD8 like the rest) */
    if ((length > UINT16_MAX) || (length < 0))
	return FALSE;

    newData = (CARD8Ptr) xrealloc(array->data, length * sizeof (CARD8));
    if (!newData)
	return FALSE;
    array->length = (CARD16) length;
    array->data = newData;
    return TRUE;
}

int
XdmcpReallocARRAYofARRAY8 (ARRAYofARRAY8Ptr array, int length)
{
    ARRAY8Ptr	newData;

    /* length defined in ARRAYofARRAY8 struct is a CARD8 */
    if ((length > UINT8_MAX) || (length < 0))
	return FALSE;

    newData = (ARRAY8Ptr) xrealloc(array->data, length * sizeof (ARRAY8));
    if (!newData)
	return FALSE;
    if (length > array->length)
        memset(newData + array->length, 0,
               (length - array->length) * sizeof (ARRAY8));
    array->length = (CARD8) length;
    array->data = newData;
    return TRUE;
}

int
XdmcpReallocARRAY16 (ARRAY16Ptr array, int length)
{
    CARD16Ptr	newData;

    /* length defined in ARRAY16 struct is a CARD8 */
    if ((length > UINT8_MAX) || (length < 0))
	return FALSE;
    newData = (CARD16Ptr) xrealloc(array->data, length * sizeof (CARD16));
    if (!newData)
	return FALSE;
    array->length = (CARD8) length;
    array->data = newData;
    return TRUE;
}

int
XdmcpReallocARRAY32 (ARRAY32Ptr array, int length)
{
    CARD32Ptr	newData;

    /* length defined in ARRAY32 struct is a CARD8 */
    if ((length > UINT8_MAX) || (length < 0))
	return FALSE;

    newData = (CARD32Ptr) xrealloc(array->data, length * sizeof (CARD32));
    if (!newData)
	return FALSE;
    array->length = (CARD8) length;
    array->data = newData;
    return TRUE;
}

void
XdmcpDisposeARRAY8 (ARRAY8Ptr array)
{
    free(array->data);
    array->length = 0;
    array->data = NULL;
}

void
XdmcpDisposeARRAY16 (ARRAY16Ptr array)
{
    free(array->data);
    array->length = 0;
    array->data = NULL;
}

void
XdmcpDisposeARRAY32 (ARRAY32Ptr array)
{
    free(array->data);
    array->length = 0;
    array->data = NULL;
}

void
XdmcpDisposeARRAYofARRAY8 (ARRAYofARRAY8Ptr array)
{
    int	i;

    if (array->data != NULL) {
	for (i = 0; i < (int)array->length; i++)
	    XdmcpDisposeARRAY8 (&array->data[i]);
	free(array->data);
    }
    array->length = 0;
    array->data = NULL;
}