Key.c revision 44dda7b2
1/* 2Copyright 1989, 1998 The Open Group 3 4Permission to use, copy, modify, distribute, and sell this software and its 5documentation for any purpose is hereby granted without fee, provided that 6the above copyright notice appear in all copies and that both that 7copyright notice and this permission notice appear in supporting 8documentation. 9 10The above copyright notice and this permission notice shall be included in 11all copies or substantial portions of the Software. 12 13THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 17AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 20Except as contained in this notice, the name of The Open Group shall not be 21used in advertising or otherwise to promote the sale, use or other dealings 22in 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 35static void 36getbits (long data, unsigned char *dst) 37{ 38 dst[0] = (data ) & 0xff; 39 dst[1] = (data >> 8) & 0xff; 40 dst[2] = (data >> 16) & 0xff; 41 dst[3] = (data >> 24) & 0xff; 42} 43 44#define Time_t time_t 45 46#include <stdlib.h> 47 48#if defined(HAVE_LRAND48) && defined(HAVE_SRAND48) 49#define srandom srand48 50#define random lrand48 51#endif 52#ifdef WIN32 53#include <process.h> 54#define srandom srand 55#define random rand 56#define getpid(x) _getpid(x) 57#endif 58 59void 60XdmcpGenerateKey (XdmAuthKeyPtr key) 61{ 62 long lowbits, highbits; 63 64 srandom ((int)getpid() ^ time((Time_t *)0)); 65 lowbits = random (); 66 highbits = random (); 67 getbits (lowbits, key->data); 68 getbits (highbits, key->data + 4); 69} 70 71int 72XdmcpCompareKeys (const XdmAuthKeyPtr a, const XdmAuthKeyPtr b) 73{ 74 int i; 75 76 for (i = 0; i < 8; i++) 77 if (a->data[i] != b->data[i]) 78 return FALSE; 79 return TRUE; 80} 81 82void 83XdmcpIncrementKey (XdmAuthKeyPtr key) 84{ 85 int i; 86 87 i = 7; 88 while (++key->data[i] == 0) 89 if (--i < 0) 90 break; 91} 92 93void 94XdmcpDecrementKey (XdmAuthKeyPtr key) 95{ 96 int i; 97 98 i = 7; 99 while (key->data[i]-- == 0) 100 if (--i < 0) 101 break; 102} 103