From 5cabcf96e77bcf6d23bfb3ec32c9ea37100578c6 Mon Sep 17 00:00:00 2001
From: Andy Polyakov <appro@openssl.org>
Date: Mon, 29 Dec 2008 12:35:49 +0000
Subject: [PATCH] Fix "possible loss of data" Win64 compiler warnings.

---
 crypto/bn/bn_lib.c        |  2 +-
 crypto/bn/bn_nist.c       | 62 +++++++++++++++++++--------------------
 crypto/dso/dso_win32.c    | 18 ++++++------
 crypto/evp/e_des.c        |  3 +-
 crypto/evp/e_des3.c       |  3 +-
 crypto/md32_common.h      |  4 +--
 crypto/modes/cfb128.c     |  3 +-
 crypto/rc4/rc4_enc.c      |  4 +--
 crypto/sha/sha512.c       |  2 +-
 crypto/whrlpool/wp_dgst.c |  4 +--
 10 files changed, 54 insertions(+), 51 deletions(-)

diff --git a/crypto/bn/bn_lib.c b/crypto/bn/bn_lib.c
index 0c6e7e5964..2564c332b3 100644
--- a/crypto/bn/bn_lib.c
+++ b/crypto/bn/bn_lib.c
@@ -763,7 +763,7 @@ int BN_is_bit_set(const BIGNUM *a, int n)
 	i=n/BN_BITS2;
 	j=n%BN_BITS2;
 	if (a->top <= i) return 0;
-	return(((a->d[i])>>j)&((BN_ULONG)1));
+	return(((a->d[i])>>j)&((BN_ULONG)1)==1);
 	}
 
 int BN_mask_bits(BIGNUM *a, int n)
diff --git a/crypto/bn/bn_nist.c b/crypto/bn/bn_nist.c
index ea991c95b1..2ca5b01391 100644
--- a/crypto/bn/bn_nist.c
+++ b/crypto/bn/bn_nist.c
@@ -388,14 +388,14 @@ int BN_nist_mod_192(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
 	nist_cp_bn_0(buf, a_d + BN_NIST_192_TOP, top - BN_NIST_192_TOP, BN_NIST_192_TOP);
 
 	nist_set_192(t_d, buf, 0, 3, 3);
-	carry = bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
+	carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
 	nist_set_192(t_d, buf, 4, 4, 0);
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
 	nist_set_192(t_d, buf, 5, 5, 5)
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_192_TOP);
 
 	if (carry > 0)
-		carry = bn_sub_words(r_d,r_d,_nist_p_192[carry-1],BN_NIST_192_TOP);
+		carry = (int)bn_sub_words(r_d,r_d,_nist_p_192[carry-1],BN_NIST_192_TOP);
 	else
 		carry = 1;
 
@@ -482,13 +482,13 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
 	nist_cp_bn_0(buf, a_d + BN_NIST_224_TOP, top - BN_NIST_224_TOP, BN_NIST_224_TOP);
 #endif
 	nist_set_224(t_d, buf, 10, 9, 8, 7, 0, 0, 0);
-	carry = bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
+	carry = (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
 	nist_set_224(t_d, buf, 0, 13, 12, 11, 0, 0, 0);
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_224_TOP);
 	nist_set_224(t_d, buf, 13, 12, 11, 10, 9, 8, 7);
-	carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
+	carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
 	nist_set_224(t_d, buf, 0, 0, 0, 0, 13, 12, 11);
-	carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
+	carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_224_TOP);
 
 #if BN_BITS2==64
 	carry = (int)(r_d[BN_NIST_224_TOP-1]>>32);
@@ -496,7 +496,7 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
 	u.f = bn_sub_words;
 	if (carry > 0)
 		{
-		carry = bn_sub_words(r_d,r_d,_nist_p_224[carry-1],BN_NIST_224_TOP);
+		carry = (int)bn_sub_words(r_d,r_d,_nist_p_224[carry-1],BN_NIST_224_TOP);
 #if BN_BITS2==64
 		carry=(int)(~(r_d[BN_NIST_224_TOP-1]>>32))&1;
 #endif
@@ -509,7 +509,7 @@ int BN_nist_mod_224(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
 		 * the modulus. but if it does, then result has
 		 * to be compared to the modulus and conditionally
 		 * adjusted by *subtracting* the latter. */
-		carry = bn_add_words(r_d,r_d,_nist_p_224[-carry-1],BN_NIST_224_TOP);
+		carry = (int)bn_add_words(r_d,r_d,_nist_p_224[-carry-1],BN_NIST_224_TOP);
 		mask = 0-(size_t)carry;
 		u.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);
 		}
@@ -587,7 +587,7 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
 	nist_set_256(t_d, buf, 15, 14, 13, 12, 11, 0, 0, 0);
 	/*S2*/
 	nist_set_256(c_d, buf, 0, 15, 14, 13, 12, 0, 0, 0);
-	carry = bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
+	carry = (int)bn_add_words(t_d, t_d, c_d, BN_NIST_256_TOP);
 	/* left shift */
 		{
 		register BN_ULONG *ap,t,c;
@@ -602,33 +602,33 @@ int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
 		carry <<= 1;
 		carry  |= c;
 		}
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
 	/*S3*/
 	nist_set_256(t_d, buf, 15, 14, 0, 0, 0, 10, 9, 8);
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
 	/*S4*/
 	nist_set_256(t_d, buf, 8, 13, 15, 14, 13, 11, 10, 9);
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_256_TOP);
 	/*D1*/
 	nist_set_256(t_d, buf, 10, 8, 0, 0, 0, 13, 12, 11);
-	carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
+	carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
 	/*D2*/
 	nist_set_256(t_d, buf, 11, 9, 0, 0, 15, 14, 13, 12);
-	carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
+	carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
 	/*D3*/
 	nist_set_256(t_d, buf, 12, 0, 10, 9, 8, 15, 14, 13);
-	carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
+	carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
 	/*D4*/
 	nist_set_256(t_d, buf, 13, 0, 11, 10, 9, 0, 15, 14);
-	carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
+	carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_256_TOP);
 
 	/* see BN_nist_mod_224 for explanation */
 	u.f = bn_sub_words;
 	if (carry > 0)
-		carry = bn_sub_words(r_d,r_d,_nist_p_256[carry-1],BN_NIST_256_TOP);
+		carry = (int)bn_sub_words(r_d,r_d,_nist_p_256[carry-1],BN_NIST_256_TOP);
 	else if (carry < 0)
 		{
-		carry = bn_add_words(r_d,r_d,_nist_p_256[-carry-1],BN_NIST_256_TOP);
+		carry = (int)bn_add_words(r_d,r_d,_nist_p_256[-carry-1],BN_NIST_256_TOP);
 		mask = 0-(size_t)carry;
 		u.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);
 		}
@@ -721,39 +721,39 @@ int BN_nist_mod_384(BIGNUM *r, const BIGNUM *a, const BIGNUM *field,
 			}
 		*ap=c;
 		}
-	carry = bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2), 
+	carry = (int)bn_add_words(r_d+(128/BN_BITS2), r_d+(128/BN_BITS2), 
 		t_d, BN_NIST_256_TOP);
 	/*S2 */
-	carry += bn_add_words(r_d, r_d, buf, BN_NIST_384_TOP);
+	carry += (int)bn_add_words(r_d, r_d, buf, BN_NIST_384_TOP);
 	/*S3*/
 	nist_set_384(t_d,buf,20,19,18,17,16,15,14,13,12,23,22,21);
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
 	/*S4*/
 	nist_set_384(t_d,buf,19,18,17,16,15,14,13,12,20,0,23,0);
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
 	/*S5*/
 	nist_set_384(t_d, buf,0,0,0,0,23,22,21,20,0,0,0,0);
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
 	/*S6*/
 	nist_set_384(t_d,buf,0,0,0,0,0,0,23,22,21,0,0,20);
-	carry += bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
+	carry += (int)bn_add_words(r_d, r_d, t_d, BN_NIST_384_TOP);
 	/*D1*/
 	nist_set_384(t_d,buf,22,21,20,19,18,17,16,15,14,13,12,23);
-	carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
+	carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
 	/*D2*/
 	nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,22,21,20,0);
-	carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
+	carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
 	/*D3*/
 	nist_set_384(t_d,buf,0,0,0,0,0,0,0,23,23,0,0,0);
-	carry -= bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
+	carry -= (int)bn_sub_words(r_d, r_d, t_d, BN_NIST_384_TOP);
 
 	/* see BN_nist_mod_224 for explanation */
 	u.f = bn_sub_words;
 	if (carry > 0)
-		carry = bn_sub_words(r_d,r_d,_nist_p_384[carry-1],BN_NIST_384_TOP);
+		carry = (int)bn_sub_words(r_d,r_d,_nist_p_384[carry-1],BN_NIST_384_TOP);
 	else if (carry < 0)
 		{
-		carry = bn_add_words(r_d,r_d,_nist_p_384[-carry-1],BN_NIST_384_TOP);
+		carry = (int)bn_add_words(r_d,r_d,_nist_p_384[-carry-1],BN_NIST_384_TOP);
 		mask = 0-(size_t)carry;
 		u.p = ((size_t)bn_sub_words&mask) | ((size_t)bn_add_words&~mask);
 		}
diff --git a/crypto/dso/dso_win32.c b/crypto/dso/dso_win32.c
index b8693c13e5..c5b00c0d13 100644
--- a/crypto/dso/dso_win32.c
+++ b/crypto/dso/dso_win32.c
@@ -358,7 +358,7 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
 				return(NULL);
 				}
 			result->device = start;
-			result->devicelen = filename - start;
+			result->devicelen = (int)(filename - start);
 			position = IN_FILE;
 			start = ++filename;
 			result->dir = start;
@@ -367,7 +367,7 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
 		case '/':
 			if(position == IN_NODE)
 				{
-				result->nodelen = filename - start;
+				result->nodelen = (int)(filename - start);
 				position = IN_FILE;
 				start = ++filename;
 				result->dir = start;
@@ -377,20 +377,20 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
 				position = IN_FILE;
 				filename++;
 				result->dir = start;
-				result->dirlen = filename - start;
+				result->dirlen = (int)(filename - start);
 				start = filename;
 				}
 			else
 				{
 				filename++;
-				result->dirlen += filename - start;
+				result->dirlen += (int)(filename - start);
 				start = filename;
 				}
 			break;
 		case '\0':
 			if(position == IN_NODE)
 				{
-				result->nodelen = filename - start;
+				result->nodelen = (int)(filename - start);
 				}
 			else
 				{
@@ -404,13 +404,13 @@ static struct file_st *win32_splitter(DSO *dso, const char *filename,
 							result->dirlen = 0;
 							}
 						result->dirlen +=
-							filename - start;
+							(int)(filename - start);
 						}
 					else
 						{
 						result->file = start;
 						result->filelen =
-							filename - start;
+							(int)(filename - start);
 						}
 					}
 				}
@@ -504,7 +504,7 @@ static char *win32_joiner(DSO *dso, const struct file_st *file_split)
 				+ file_split->predirlen
 				- (start - file_split->predir);
 		strncpy(&result[offset], start,
-			end - start); offset += end - start;
+			end - start); offset += (int)(end - start);
 		result[offset] = '\\'; offset++;
 		start = end + 1;
 		}
@@ -525,7 +525,7 @@ static char *win32_joiner(DSO *dso, const struct file_st *file_split)
 				+ file_split->dirlen
 				- (start - file_split->dir);
 		strncpy(&result[offset], start,
-			end - start); offset += end - start;
+			end - start); offset += (int)(end - start);
 		result[offset] = '\\'; offset++;
 		start = end + 1;
 		}
diff --git a/crypto/evp/e_des.c b/crypto/evp/e_des.c
index 1e4230493b..ca009f2c52 100644
--- a/crypto/evp/e_des.c
+++ b/crypto/evp/e_des.c
@@ -147,7 +147,8 @@ static int des_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
 	    c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;
 	    DES_cfb_encrypt(c,d,1,1,ctx->cipher_data,(DES_cblock *)ctx->iv,
 			ctx->encrypt);
-	    out[n/8]=(out[n/8]&~(0x80 >> (n%8)))|((d[0]&0x80) >> (n%8));
+	    out[n/8]=(out[n/8]&~(0x80 >> (unsigned int)(n%8))) |
+		     ((d[0]&0x80) >> (unsigned int)(n%8));
 	    }
 	inl-=chunk;
 	in +=chunk;
diff --git a/crypto/evp/e_des3.c b/crypto/evp/e_des3.c
index 059ad77118..3232cfe024 100644
--- a/crypto/evp/e_des3.c
+++ b/crypto/evp/e_des3.c
@@ -179,7 +179,8 @@ static int des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
 	DES_ede3_cfb_encrypt(c,d,1,1,
 			     &data(ctx)->ks1,&data(ctx)->ks2,&data(ctx)->ks3,
 			     (DES_cblock *)ctx->iv,ctx->encrypt);
-	out[n/8]=(out[n/8]&~(0x80 >> (n%8)))|((d[0]&0x80) >> (n%8));
+	out[n/8]=(out[n/8]&~(0x80 >> (unsigned int)(n%8))) |
+		 ((d[0]&0x80) >> (unsigned int)(n%8));
 	}
 
     return 1;
diff --git a/crypto/md32_common.h b/crypto/md32_common.h
index 61bcd9786f..279916fa59 100644
--- a/crypto/md32_common.h
+++ b/crypto/md32_common.h
@@ -293,7 +293,7 @@ int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
 	 * Wei Dai <weidai@eskimo.com> for pointing it out. */
 	if (l < c->Nl) /* overflow */
 		c->Nh++;
-	c->Nh+=(len>>29);	/* might cause compiler warning on 16-bit */
+	c->Nh+=(HASH_LONG)(len>>29);	/* might cause compiler warning on 16-bit */
 	c->Nl=l;
 
 	n = c->num;
@@ -331,7 +331,7 @@ int HASH_UPDATE (HASH_CTX *c, const void *data_, size_t len)
 	if (len != 0)
 		{
 		p = (unsigned char *)c->data;
-		c->num = len;
+		c->num = (unsigned int)len;
 		memcpy (p,data,len);
 		}
 	return 1;
diff --git a/crypto/modes/cfb128.c b/crypto/modes/cfb128.c
index 9ba4984718..b6d1e886b9 100644
--- a/crypto/modes/cfb128.c
+++ b/crypto/modes/cfb128.c
@@ -229,7 +229,8 @@ void CRYPTO_cfb128_1_encrypt(const unsigned char *in, unsigned char *out,
 	{
 	c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;
 	cfbr_encrypt_block(c,d,1,key,ivec,enc,block);
-	out[n/8]=(out[n/8]&~(1 << (7-n%8)))|((d[0]&0x80) >> (n%8));
+	out[n/8]=(out[n/8]&~(1 << (unsigned int)(7-n%8))) |
+		 ((d[0]&0x80) >> (unsigned int)(n%8));
 	}
 }
 
diff --git a/crypto/rc4/rc4_enc.c b/crypto/rc4/rc4_enc.c
index 37ef43c16c..8c4fc6c7a3 100644
--- a/crypto/rc4/rc4_enc.c
+++ b/crypto/rc4/rc4_enc.c
@@ -157,7 +157,7 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
 		if (!is_endian.little)
 			{	/* BIG-ENDIAN CASE */
 # define BESHFT(c)	(((sizeof(RC4_CHUNK)-(c)-1)*8)&(sizeof(RC4_CHUNK)*8-1))
-			for (;len&-sizeof(RC4_CHUNK);len-=sizeof(RC4_CHUNK))
+			for (;len&(0-sizeof(RC4_CHUNK));len-=sizeof(RC4_CHUNK))
 				{
 				ichunk  = *(RC4_CHUNK *)indata;
 				otp  = RC4_STEP<<BESHFT(0);
@@ -210,7 +210,7 @@ void RC4(RC4_KEY *key, size_t len, const unsigned char *indata,
 		else
 			{	/* LITTLE-ENDIAN CASE */
 # define LESHFT(c)	(((c)*8)&(sizeof(RC4_CHUNK)*8-1))
-			for (;len&-sizeof(RC4_CHUNK);len-=sizeof(RC4_CHUNK))
+			for (;len&(0-sizeof(RC4_CHUNK));len-=sizeof(RC4_CHUNK))
 				{
 				ichunk  = *(RC4_CHUNK *)indata;
 				otp  = RC4_STEP;
diff --git a/crypto/sha/sha512.c b/crypto/sha/sha512.c
index 832cae24f1..d1aa7614b2 100644
--- a/crypto/sha/sha512.c
+++ b/crypto/sha/sha512.c
@@ -240,7 +240,7 @@ int SHA512_Update (SHA512_CTX *c, const void *_data, size_t len)
 
 		if (len < n)
 			{
-			memcpy (p+c->num,data,len), c->num += len;
+			memcpy (p+c->num,data,len), c->num += (unsigned int)len;
 			return 1;
 			}
 		else	{
diff --git a/crypto/whrlpool/wp_dgst.c b/crypto/whrlpool/wp_dgst.c
index 2919914059..ee5c5c1bf3 100644
--- a/crypto/whrlpool/wp_dgst.c
+++ b/crypto/whrlpool/wp_dgst.c
@@ -130,7 +130,7 @@ void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c,const void *_inp,size_t bits)
 				else
 					{
 					memcpy(c->data+byteoff,inp,bits/8);
-					bitoff += bits;
+					bitoff += (unsigned int)bits;
 					bits = 0;
 					}
 				c->bitoff = bitoff;
@@ -197,7 +197,7 @@ void WHIRLPOOL_BitUpdate(WHIRLPOOL_CTX *c,const void *_inp,size_t bits)
 				b = (inp[0]<<inpgap)&0xff;
 				if (bitrem)	c->data[byteoff++] |= b>>bitrem;
 				else		c->data[byteoff++]  = b;
-				bitoff += bits;
+				bitoff += (unsigned int)bits;
 				if (bitoff==WHIRLPOOL_BBLOCK)
 					{
 					whirlpool_block(c,c->data,1);
-- 
2.25.1