144dda7b2Smrg/* 244dda7b2SmrgCopyright 1989, 1998 The Open Group 344dda7b2Smrg 444dda7b2SmrgPermission to use, copy, modify, distribute, and sell this software and its 544dda7b2Smrgdocumentation for any purpose is hereby granted without fee, provided that 644dda7b2Smrgthe above copyright notice appear in all copies and that both that 744dda7b2Smrgcopyright notice and this permission notice appear in supporting 844dda7b2Smrgdocumentation. 944dda7b2Smrg 1044dda7b2SmrgThe above copyright notice and this permission notice shall be included in 1144dda7b2Smrgall copies or substantial portions of the Software. 1244dda7b2Smrg 1344dda7b2SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1444dda7b2SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1544dda7b2SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1644dda7b2SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 1744dda7b2SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 1844dda7b2SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1944dda7b2Smrg 2044dda7b2SmrgExcept as contained in this notice, the name of The Open Group shall not be 2144dda7b2Smrgused in advertising or otherwise to promote the sale, use or other dealings 2244dda7b2Smrgin this Software without prior written authorization from The Open Group. 2344dda7b2Smrg * 2444dda7b2Smrg * Author: Keith Packard, MIT X Consortium 2544dda7b2Smrg */ 2644dda7b2Smrg 2744dda7b2Smrg#ifdef HAVE_CONFIG_H 2844dda7b2Smrg#include <config.h> 2944dda7b2Smrg#endif 3044dda7b2Smrg#include <X11/Xos.h> 3144dda7b2Smrg#include <X11/X.h> 3244dda7b2Smrg#include <X11/Xmd.h> 3344dda7b2Smrg#include <X11/Xdmcp.h> 3444dda7b2Smrg#include <stdint.h> 3544dda7b2Smrg#include <stdlib.h> 3644dda7b2Smrg 3744dda7b2Smrg/* 3844dda7b2Smrg * This variant of malloc does not return NULL if zero size is passed into. 3944dda7b2Smrg */ 4044dda7b2Smrgstatic void * 4144dda7b2Smrgxmalloc(size_t size) 4244dda7b2Smrg{ 4344dda7b2Smrg return malloc(size ? size : 1); 4444dda7b2Smrg} 4544dda7b2Smrg 46fc544a13Smrg/* 47fc544a13Smrg * This variant of calloc does not return NULL if zero count is passed into. 48fc544a13Smrg */ 49fc544a13Smrgstatic void * 50fc544a13Smrgxcalloc(size_t n, size_t size) 51fc544a13Smrg{ 52fc544a13Smrg return calloc(n ? n : 1, size); 53fc544a13Smrg} 54fc544a13Smrg 5544dda7b2Smrg/* 5644dda7b2Smrg * This variant of realloc does not return NULL if zero size is passed into 5744dda7b2Smrg */ 5844dda7b2Smrgstatic void * 5944dda7b2Smrgxrealloc(void *ptr, size_t size) 6044dda7b2Smrg{ 6144dda7b2Smrg return realloc(ptr, size ? size : 1); 6244dda7b2Smrg} 6344dda7b2Smrg 6444dda7b2Smrgint 6544dda7b2SmrgXdmcpAllocARRAY8 (ARRAY8Ptr array, int length) 6644dda7b2Smrg{ 6744dda7b2Smrg /* length defined in ARRAY8 struct is a CARD16 (not CARD8 like the rest) */ 68fc544a13Smrg if ((length > UINT16_MAX) || (length < 0)) 69fc544a13Smrg array->data = NULL; 70fc544a13Smrg else 71fc544a13Smrg array->data = xmalloc(length * sizeof (CARD8)); 7244dda7b2Smrg 73fc544a13Smrg if (array->data == NULL) { 74fc544a13Smrg array->length = 0; 7544dda7b2Smrg return FALSE; 76fc544a13Smrg } 7744dda7b2Smrg array->length = (CARD16) length; 7844dda7b2Smrg return TRUE; 7944dda7b2Smrg} 8044dda7b2Smrg 8144dda7b2Smrgint 8244dda7b2SmrgXdmcpAllocARRAY16 (ARRAY16Ptr array, int length) 8344dda7b2Smrg{ 8444dda7b2Smrg /* length defined in ARRAY16 struct is a CARD8 */ 85fc544a13Smrg if ((length > UINT8_MAX) || (length < 0)) 86fc544a13Smrg array->data = NULL; 87fc544a13Smrg else 88fc544a13Smrg array->data = xmalloc(length * sizeof (CARD16)); 8944dda7b2Smrg 90fc544a13Smrg if (array->data == NULL) { 91fc544a13Smrg array->length = 0; 9244dda7b2Smrg return FALSE; 93fc544a13Smrg } 9444dda7b2Smrg array->length = (CARD8) length; 9544dda7b2Smrg return TRUE; 9644dda7b2Smrg} 9744dda7b2Smrg 9844dda7b2Smrgint 9944dda7b2SmrgXdmcpAllocARRAY32 (ARRAY32Ptr array, int length) 10044dda7b2Smrg{ 10144dda7b2Smrg /* length defined in ARRAY32 struct is a CARD8 */ 102fc544a13Smrg if ((length > UINT8_MAX) || (length < 0)) 103fc544a13Smrg array->data = NULL; 104fc544a13Smrg else 105fc544a13Smrg array->data = xmalloc(length * sizeof (CARD32)); 10644dda7b2Smrg 107fc544a13Smrg if (array->data == NULL) { 108fc544a13Smrg array->length = 0; 10944dda7b2Smrg return FALSE; 110fc544a13Smrg } 11144dda7b2Smrg array->length = (CARD8) length; 11244dda7b2Smrg return TRUE; 11344dda7b2Smrg} 11444dda7b2Smrg 11544dda7b2Smrgint 11644dda7b2SmrgXdmcpAllocARRAYofARRAY8 (ARRAYofARRAY8Ptr array, int length) 11744dda7b2Smrg{ 11844dda7b2Smrg /* length defined in ARRAYofARRAY8 struct is a CARD8 */ 119fc544a13Smrg if ((length > UINT8_MAX) || (length < 0)) 120fc544a13Smrg array->data = NULL; 121fc544a13Smrg else 122fc544a13Smrg /* 123fc544a13Smrg * Use calloc to ensure the pointers are cleared out so we 124fc544a13Smrg * don't try to free garbage if XdmcpDisposeARRAYofARRAY8() 125fc544a13Smrg * is called before the caller sets them to valid pointers. 126fc544a13Smrg */ 127fc544a13Smrg array->data = xcalloc(length, sizeof (ARRAY8)); 128fc544a13Smrg 129fc544a13Smrg if (array->data == NULL) { 130fc544a13Smrg array->length = 0; 13144dda7b2Smrg return FALSE; 132fc544a13Smrg } 13344dda7b2Smrg array->length = (CARD8) length; 13444dda7b2Smrg return TRUE; 13544dda7b2Smrg} 13644dda7b2Smrg 13744dda7b2Smrgint 13844dda7b2SmrgXdmcpARRAY8Equal (const ARRAY8Ptr array1, const ARRAY8Ptr array2) 13944dda7b2Smrg{ 14044dda7b2Smrg if (array1->length != array2->length) 14144dda7b2Smrg return FALSE; 14244dda7b2Smrg if (memcmp(array1->data, array2->data, array1->length) != 0) 14344dda7b2Smrg return FALSE; 14444dda7b2Smrg return TRUE; 14544dda7b2Smrg} 14644dda7b2Smrg 14744dda7b2Smrgint 14844dda7b2SmrgXdmcpCopyARRAY8 (const ARRAY8Ptr src, ARRAY8Ptr dst) 14944dda7b2Smrg{ 150fc544a13Smrg if (!XdmcpAllocARRAY8(dst, src->length)) 15144dda7b2Smrg return FALSE; 152515ec619Smrg memcpy(dst->data, src->data, src->length * sizeof (CARD8)); 15344dda7b2Smrg return TRUE; 15444dda7b2Smrg} 15544dda7b2Smrg 15644dda7b2Smrgint 15744dda7b2SmrgXdmcpReallocARRAY8 (ARRAY8Ptr array, int length) 15844dda7b2Smrg{ 15944dda7b2Smrg CARD8Ptr newData; 16044dda7b2Smrg 16144dda7b2Smrg /* length defined in ARRAY8 struct is a CARD16 (not CARD8 like the rest) */ 162fc544a13Smrg if ((length > UINT16_MAX) || (length < 0)) 16344dda7b2Smrg return FALSE; 16444dda7b2Smrg 16544dda7b2Smrg newData = (CARD8Ptr) xrealloc(array->data, length * sizeof (CARD8)); 16644dda7b2Smrg if (!newData) 16744dda7b2Smrg return FALSE; 16844dda7b2Smrg array->length = (CARD16) length; 16944dda7b2Smrg array->data = newData; 17044dda7b2Smrg return TRUE; 17144dda7b2Smrg} 17244dda7b2Smrg 17344dda7b2Smrgint 17444dda7b2SmrgXdmcpReallocARRAYofARRAY8 (ARRAYofARRAY8Ptr array, int length) 17544dda7b2Smrg{ 17644dda7b2Smrg ARRAY8Ptr newData; 17744dda7b2Smrg 17844dda7b2Smrg /* length defined in ARRAYofARRAY8 struct is a CARD8 */ 179fc544a13Smrg if ((length > UINT8_MAX) || (length < 0)) 18044dda7b2Smrg return FALSE; 18144dda7b2Smrg 18244dda7b2Smrg newData = (ARRAY8Ptr) xrealloc(array->data, length * sizeof (ARRAY8)); 18344dda7b2Smrg if (!newData) 18444dda7b2Smrg return FALSE; 185fc544a13Smrg if (length > array->length) 186fc544a13Smrg memset(newData + array->length, 0, 187fc544a13Smrg (length - array->length) * sizeof (ARRAY8)); 18844dda7b2Smrg array->length = (CARD8) length; 18944dda7b2Smrg array->data = newData; 19044dda7b2Smrg return TRUE; 19144dda7b2Smrg} 19244dda7b2Smrg 19344dda7b2Smrgint 19444dda7b2SmrgXdmcpReallocARRAY16 (ARRAY16Ptr array, int length) 19544dda7b2Smrg{ 19644dda7b2Smrg CARD16Ptr newData; 19744dda7b2Smrg 19844dda7b2Smrg /* length defined in ARRAY16 struct is a CARD8 */ 199fc544a13Smrg if ((length > UINT8_MAX) || (length < 0)) 20044dda7b2Smrg return FALSE; 20144dda7b2Smrg newData = (CARD16Ptr) xrealloc(array->data, length * sizeof (CARD16)); 20244dda7b2Smrg if (!newData) 20344dda7b2Smrg return FALSE; 20444dda7b2Smrg array->length = (CARD8) length; 20544dda7b2Smrg array->data = newData; 20644dda7b2Smrg return TRUE; 20744dda7b2Smrg} 20844dda7b2Smrg 20944dda7b2Smrgint 21044dda7b2SmrgXdmcpReallocARRAY32 (ARRAY32Ptr array, int length) 21144dda7b2Smrg{ 21244dda7b2Smrg CARD32Ptr newData; 21344dda7b2Smrg 21444dda7b2Smrg /* length defined in ARRAY32 struct is a CARD8 */ 215fc544a13Smrg if ((length > UINT8_MAX) || (length < 0)) 21644dda7b2Smrg return FALSE; 21744dda7b2Smrg 21844dda7b2Smrg newData = (CARD32Ptr) xrealloc(array->data, length * sizeof (CARD32)); 21944dda7b2Smrg if (!newData) 22044dda7b2Smrg return FALSE; 22144dda7b2Smrg array->length = (CARD8) length; 22244dda7b2Smrg array->data = newData; 22344dda7b2Smrg return TRUE; 22444dda7b2Smrg} 22544dda7b2Smrg 22644dda7b2Smrgvoid 22744dda7b2SmrgXdmcpDisposeARRAY8 (ARRAY8Ptr array) 22844dda7b2Smrg{ 22944dda7b2Smrg free(array->data); 23044dda7b2Smrg array->length = 0; 23144dda7b2Smrg array->data = NULL; 23244dda7b2Smrg} 23344dda7b2Smrg 23444dda7b2Smrgvoid 23544dda7b2SmrgXdmcpDisposeARRAY16 (ARRAY16Ptr array) 23644dda7b2Smrg{ 23744dda7b2Smrg free(array->data); 23844dda7b2Smrg array->length = 0; 23944dda7b2Smrg array->data = NULL; 24044dda7b2Smrg} 24144dda7b2Smrg 24244dda7b2Smrgvoid 24344dda7b2SmrgXdmcpDisposeARRAY32 (ARRAY32Ptr array) 24444dda7b2Smrg{ 24544dda7b2Smrg free(array->data); 24644dda7b2Smrg array->length = 0; 24744dda7b2Smrg array->data = NULL; 24844dda7b2Smrg} 24944dda7b2Smrg 25044dda7b2Smrgvoid 25144dda7b2SmrgXdmcpDisposeARRAYofARRAY8 (ARRAYofARRAY8Ptr array) 25244dda7b2Smrg{ 25344dda7b2Smrg if (array->data != NULL) { 254515ec619Smrg for (unsigned int i = 0; i < (unsigned int) array->length; i++) 25544dda7b2Smrg XdmcpDisposeARRAY8 (&array->data[i]); 25644dda7b2Smrg free(array->data); 25744dda7b2Smrg } 25844dda7b2Smrg array->length = 0; 25944dda7b2Smrg array->data = NULL; 26044dda7b2Smrg} 261