changeset 121:e6c8652473fe

srogue: more compatibility improvements. Randomness now uses mdport, and xcrypt.c has been replaced with the rogue5 version. Super-Rogue now builds on MinGW.
author John "Elwin" Edwards
date Sat, 03 May 2014 10:31:30 -0700
parents d6b7c3fb37ea
children 65f3da34578a
files srogue/configure.ac srogue/main.c srogue/mdport.c srogue/rogue.h srogue/save.c srogue/xcrypt.c
diffstat 6 files changed, 104 insertions(+), 66 deletions(-) [+]
line wrap: on
line diff
--- a/srogue/configure.ac	Fri May 02 15:06:23 2014 -0700
+++ b/srogue/configure.ac	Sat May 03 10:31:30 2014 -0700
@@ -24,7 +24,7 @@
 AC_TYPE_SIGNAL
 AC_FUNC_STAT
 AC_FUNC_VPRINTF
-AC_CHECK_FUNCS([erasechar killchar alarm getpass memset setenv strchr nlist _spawnl spawnl getpwuid loadav getloadavg strerror setgid setuid getuid getgid])
+AC_CHECK_FUNCS([erasechar killchar alarm getpass memset setenv strchr nlist _spawnl spawnl getpwuid loadav getloadavg strerror setgid setuid getuid getgid lrand48 random srand48 srandom])
 AC_PROG_INSTALL
 
 AC_ARG_WITH(program-name, AC_HELP_STRING([--with-program-name=NAME],[alternate executable name]),[progname="$withval" ], [progname="srogue"] )
--- a/srogue/main.c	Fri May 02 15:06:23 2014 -0700
+++ b/srogue/main.c	Sat May 03 10:31:30 2014 -0700
@@ -173,7 +173,7 @@
 
 	fflush(stdout);
 	seed = dnum;
-	srandom(seed);			/* init rnd number gen */
+	md_srandom(seed);		/* init rnd number gen */
 
 	md_onsignal_exit();		/* just in case */
 
@@ -354,7 +354,7 @@
 	if (range == 0)
 		wh = 0;
 	else {
-		wh = random() % range;
+		wh = md_random() % range;
 		wh &= 0x7FFFFFFF;
 	}
 	return wh;
--- a/srogue/mdport.c	Fri May 02 15:06:23 2014 -0700
+++ b/srogue/mdport.c	Sat May 03 10:31:30 2014 -0700
@@ -1567,3 +1567,27 @@
 	setgid(playgid);
 #endif
 }
+
+int
+md_random(void)
+{
+#if defined (HAVE_LRAND48)
+	return lrand48();
+#elif defined (HAVE_RANDOM)
+	return random();
+#else
+	return rand();
+#endif
+}
+
+void
+md_srandom(unsigned int seed)
+{
+#if defined (HAVE_SRAND48)
+	srand48(seed);
+#elif defined (HAVE_SRANDOM)
+	srandom(seed);
+#else
+	srand(seed);
+#endif
+}
--- a/srogue/rogue.h	Fri May 02 15:06:23 2014 -0700
+++ b/srogue/rogue.h	Sat May 03 10:31:30 2014 -0700
@@ -71,6 +71,8 @@
 long md_memused(void);
 void md_droppriv(void);
 void md_resetpriv(void);
+int md_random(void);
+void md_srandom(unsigned int seed);
 char *xcrypt(const char *key, const char *setting);
 
 #define reg     register        /* register abbr.       */
--- a/srogue/save.c	Fri May 02 15:06:23 2014 -0700
+++ b/srogue/save.c	Sat May 03 10:31:30 2014 -0700
@@ -287,7 +287,7 @@
 #endif
 	if (!wizard)
 	{
-#ifndef __DJGPP__
+#if defined(HAVE_WORKING_FORK)
 			endwin();
 			if (!use_savedir)
 			{
@@ -344,6 +344,6 @@
 	strcpy(file_name, file);
 	setup();
 	restscr(cw);
-	srandom(getpid());
+	md_srandom(getpid());
 	playit();
 }
--- a/srogue/xcrypt.c	Fri May 02 15:06:23 2014 -0700
+++ b/srogue/xcrypt.c	Sat May 03 10:31:30 2014 -0700
@@ -1,32 +1,32 @@
 /*
- * FreeSec: libcrypt
+ *  FreeSec: libcrypt
  *
- * Copyright (C) 1994 David Burren
- * All rights reserved.
+ *  Copyright (C) 1994 David Burren
+ *  All rights reserved.
  *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name(s) of the author(s) nor the names of other contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
+ *  Redistribution and use in source and binary forms, with or without
+ *  modification, are permitted provided that the following conditions
+ *  are met:
+ *  1. Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *  3. Neither the name(s) of the author(s) nor the names of other contributors
+ *     may be used to endorse or promote products derived from this software
+ *     without specific prior written permission.
  *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) AND CONTRIBUTORS ``AS IS'' AND
+ *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTORS BE LIABLE
+ *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ *  SUCH DAMAGE.
  *
  *
  * This is an original implementation of the DES and the crypt(3) interfaces
@@ -50,12 +50,34 @@
  */
 
 #include <sys/types.h>
-#include <sys/param.h>
 #include <string.h>
 
-#ifdef DEBUG
-# include <stdio.h>
-#endif
+static unsigned int md_endian = 0x01020304;
+
+unsigned int
+xntohl(unsigned int x)
+{
+    if ( *((char *)&md_endian) == 0x01 )
+        return(x);
+    else
+        return( ((x & 0x000000ffU) << 24) |
+                ((x & 0x0000ff00U) <<  8) |
+                ((x & 0x00ff0000U) >>  8) |
+                ((x & 0xff000000U) >> 24) );
+}
+
+unsigned int
+xhtonl(unsigned int x)
+{
+    if ( *((char *)&md_endian) == 0x01 )
+        return(x);
+    else
+        return( ((x & 0x000000ffU) << 24) |
+                ((x & 0x0000ff00U) <<  8) |
+                ((x & 0x00ff0000U) >>  8) |
+                ((x & 0xff000000U) >> 24) );
+}
+
 #define _PASSWORD_EFMT1 '_'
 
 static unsigned char	IP[64] = {
@@ -182,8 +204,7 @@
 /*	  0123456789012345678901234567890123456789012345678901234567890123 */
 
 static __inline int
-ascii_to_bin(ch)
-	char ch;
+ascii_to_bin(int ch)
 {
 	if (ch > 'z')
 		return(0);
@@ -201,7 +222,7 @@
 }
 
 static void
-des_init()
+des_init(void)
 {
 	int	i, j, b, k, inbit, obit;
 	unsigned int	*p, *il, *ir, *fl, *fr;
@@ -236,7 +257,7 @@
 	 * initialise the inverted key permutation.
 	 */
 	for (i = 0; i < 64; i++) {
-		init_perm[final_perm[i] = IP[i] - 1] = i;
+		init_perm[final_perm[i] = IP[i] - 1] = (unsigned char) i;
 		inv_key_perm[i] = 255;
 	}
 
@@ -245,7 +266,7 @@
 	 * compression permutation.
 	 */
 	for (i = 0; i < 56; i++) {
-		inv_key_perm[key_perm[i] - 1] = i;
+		inv_key_perm[key_perm[i] - 1] = (unsigned char) i;
 		inv_comp_perm[i] = 255;
 	}
 
@@ -253,7 +274,7 @@
 	 * Invert the key compression permutation.
 	 */
 	for (i = 0; i < 48; i++) {
-		inv_comp_perm[comp_perm[i] - 1] = i;
+		inv_comp_perm[comp_perm[i] - 1] = (unsigned char) i;
 	}
 
 	/*
@@ -315,7 +336,7 @@
 	 * handling the output of the S-box arrays setup above.
 	 */
 	for (i = 0; i < 32; i++)
-		un_pbox[pbox[i] - 1] = i;
+		un_pbox[pbox[i] - 1] = (unsigned char) i;
 
 	for (b = 0; b < 4; b++)
 		for (i = 0; i < 256; i++) {
@@ -330,8 +351,7 @@
 }
 
 static void
-setup_salt(salt)
-	int salt;
+setup_salt(int salt)
 {
 	unsigned int	obit, saltbit;
 	int	i;
@@ -352,8 +372,7 @@
 }
 
 static int
-des_setkey(key)
-	const char *key;
+des_setkey(const char *key)
 {
 	unsigned int k0, k1, rawkey0, rawkey1;
 	int	shifts, round;
@@ -361,8 +380,8 @@
 	if (!des_initialised)
 		des_init();
 
-	rawkey0 = ntohl(*(unsigned int *) key);
-	rawkey1 = ntohl(*(unsigned int *) (key + 4));
+	rawkey0 = xntohl(*(unsigned int *) key);
+	rawkey1 = xntohl(*(unsigned int *) (key + 4));
 
 	if ((rawkey0 | rawkey1)
 	    && rawkey0 == old_rawkey0
@@ -433,15 +452,14 @@
 }
 
 static int
-do_des(l_in, r_in, l_out, r_out, count)
-	unsigned int l_in, r_in, *l_out, *r_out;
-	int count;
+do_des(unsigned int l_in, unsigned int r_in, unsigned int *l_out, 
+       unsigned int *r_out, int count)
 {
 	/*
 	 *	l_in, r_in, l_out, and r_out are in pseudo-"big-endian" format.
 	 */
 	unsigned int	l, r, *kl, *kr, *kl1, *kr1;
-	unsigned int	f, r48l, r48r;
+	unsigned int	f = 0, r48l, r48r;
 	int		round;
 
 	if (count == 0) {
@@ -551,11 +569,7 @@
 }
 
 static int
-des_cipher(in, out, salt, count)
-	const char *in;
-	char *out;
-	int salt;
-	int count;
+des_cipher(const char *in, char *out, int salt, int count)
 {
 	unsigned int l_out, r_out, rawl, rawr;
 	unsigned int x[2];
@@ -567,20 +581,18 @@
 	setup_salt(salt);
 
 	memcpy(x, in, sizeof x);
-	rawl = ntohl(x[0]);
-	rawr = ntohl(x[1]);
+	rawl = xntohl(x[0]);
+	rawr = xntohl(x[1]);
 	retval = do_des(rawl, rawr, &l_out, &r_out, count);
 
-	x[0] = htonl(l_out);
-	x[1] = htonl(r_out);
+	x[0] = xhtonl(l_out);
+	x[1] = xhtonl(r_out);
 	memcpy(out, x, sizeof x);
 	return(retval);
 }
 
 char *
-xcrypt(key, setting)
-	const char *key;
-	const char *setting;
+xcrypt(const char *key, const char *setting)
 {
 	int		i;
 	unsigned int	count, salt, l, r0, r1, keybuf[2];
@@ -599,7 +611,7 @@
 		if ((*q++ = *key << 1))
 			key++;
 	}
-	if (des_setkey((unsigned char *) keybuf))
+	if (des_setkey((const char *) keybuf))
 		return(NULL);
 
 	if (*setting == _PASSWORD_EFMT1) {
@@ -618,7 +630,7 @@
 			/*
 			 * Encrypt the key with itself.
 			 */
-			if (des_cipher((unsigned char*)keybuf, (unsigned char*)keybuf, 0, 1))
+			if (des_cipher((const char*)keybuf, (char*)keybuf, 0, 1))
 				return(NULL);
 			/*
 			 * And XOR with the next 8 characters of the key.
@@ -628,7 +640,7 @@
 					*key)
 				*q++ ^= *key++ << 1;
 
-			if (des_setkey((unsigned char *) keybuf))
+			if (des_setkey((const char *) keybuf))
 				return(NULL);
 		}
 		strncpy((char *)output, setting, 9);