Re: [PHP] Strange Right-Shift Problem

2006-01-03 Thread Jochem Maas

Curt Zirzow wrote:

On Fri, Dec 30, 2005 at 12:34:35PM -0600, Richard Lynch wrote:


On Thu, December 29, 2005 5:37 pm, Michael Gross wrote:



...



If you can determine the number of bits on your system, you could use
a different number from 11 on the two systems to get the answer you
want.

if (is_32_bit_machine()){
 $y = $x  11;
}
else{
 $y = $x  43; //11 + 32 (guess)
}

One hack for detecting 32-bit machine might be this:



Isn't php suppose to handle all this?  This seems odd to me.


I don't see how php can 'handle' people assuming that integers will
always be 32bits - the php code in the encryption algorythm is flawed
because it assumes that all integers are 32bits and doesn't platform
wordsize (terminology?) into account - how can php know that this was
not the intention?




Curt.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Strange Right-Shift Problem

2006-01-02 Thread Curt Zirzow
On Fri, Dec 30, 2005 at 12:34:35PM -0600, Richard Lynch wrote:
 On Thu, December 29, 2005 5:37 pm, Michael Gross wrote:
  Hello
  I have to migrate a PHP-application to a new Linux-Box. Both the old
  and
  the new system are Linux and PHP 5.1.1. (the old one has a Pentium 4,
  the new one two Xeon CPUs). I have a problem using the
  Crypt_Xtea-Extension. I narrowed it down to the following right-shift
  operation:
 
  (-3281063054  11) produces different results:
  Old System: 495070
  New System: -1048576
 
  I understand that both results are wrong, but everything worked with
  the old behavior and I need that behavior back very urgent.
 
  Maybe someone can explain me in which way the bits are shifted so that
  the result is 495070? If I understand it, I implement my own shift
  function.
 
 Assuming the previous hypothesis that it's 32-bit versus 64-bit
 machines at work...
 
 If you can determine the number of bits on your system, you could use
 a different number from 11 on the two systems to get the answer you
 want.
 
 if (is_32_bit_machine()){
   $y = $x  11;
 }
 else{
   $y = $x  43; //11 + 32 (guess)
 }
 
 One hack for detecting 32-bit machine might be this:

Isn't php suppose to handle all this?  This seems odd to me.


Curt.
-- 
cat .signature: No such file or directory

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Strange Right-Shift Problem

2005-12-30 Thread Jochem Maas

Michael Gross wrote:

Hello
I have to migrate a PHP-application to a new Linux-Box. Both the old and 
the new system are Linux and PHP 5.1.1. (the old one has a Pentium 4, 
the new one two Xeon CPUs). I have a problem using the 
Crypt_Xtea-Extension. I narrowed it down to the following right-shift 


looking at the class I draw the conclusion that it reqires the use of
[some] data in 'long' format (i.e. integers) and the code requires and
assumes that such integers are 32bits long ... which they are on 32bit
systems ... but not on 64bit systems . here in lies the problem, you need
someone more knowledgable than I to give you an actual solution though.


operation:

(-3281063054  11) produces different results:
Old System: 495070


this is what you get with a 32bit CPU


New System: -1048576


this is what you get with a 64bit CPU



I understand that both results are wrong, but everything worked with 
the old behavior and I need that behavior back very urgent.


Maybe someone can explain me in which way the bits are shifted so that 
the result is 495070? If I understand it, I implement my own shift 


$a  $b  Shift left  Shift the bits of $a $b steps to the left (each step means 
multiply by two)
$a  $b  Shift right Shift the bits of $a $b steps to the right (each step means 
divide by two)


function.


thanks a lot
  Michael Gross



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Strange Right-Shift Problem

2005-12-30 Thread Richard Lynch
On Thu, December 29, 2005 5:37 pm, Michael Gross wrote:
 Hello
 I have to migrate a PHP-application to a new Linux-Box. Both the old
 and
 the new system are Linux and PHP 5.1.1. (the old one has a Pentium 4,
 the new one two Xeon CPUs). I have a problem using the
 Crypt_Xtea-Extension. I narrowed it down to the following right-shift
 operation:

 (-3281063054  11) produces different results:
 Old System: 495070
 New System: -1048576

 I understand that both results are wrong, but everything worked with
 the old behavior and I need that behavior back very urgent.

 Maybe someone can explain me in which way the bits are shifted so that
 the result is 495070? If I understand it, I implement my own shift
 function.

Assuming the previous hypothesis that it's 32-bit versus 64-bit
machines at work...

If you can determine the number of bits on your system, you could use
a different number from 11 on the two systems to get the answer you
want.

if (is_32_bit_machine()){
  $y = $x  11;
}
else{
  $y = $x  43; //11 + 32 (guess)
}

One hack for detecting 32-bit machine might be this:

function is_32_bit_machine(){
  return mt_getrandmax() == 0x;
}

I suppose if you're going to do this right, what you REALLY should do
is code it to determine the number of bits, no matter how large, and
then bit-shift the correct amount based on that.

So when the 128-bit machines come out in a few years, you aren't
re-coding this same damn problem AGAIN.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Strange Right-Shift Problem

2005-12-30 Thread Michael Gross

Hello
Thanks for your answers. I now implemented everything new, with the 
mcrypt-extension. However, I use the crypt_xtea still for a 
compatibility mode. I had to implement the right-shift and other bit 
operations. If someone is interested:


The problem occurred always when a number exceeded the 32bit and is 
negative. The old behavior was to take 32bits and ignore the other bits 
(therefore the sign could change in the rshift-operation). The following 
simulates the old behavior:

  function getbits($i) {
  $bits = array();
  while (abs($i)  1) {
  $f1 = floor($i / 2.0);
  $f2 = ceil($i / 2.0);
  if ($f1!=$f2) {
  array_push($bits, 1);
  } else {
  array_push($bits, 0);
  }
  $i = $f1;
  }
  return $bits;
  }

  function convertto32($i) {
  $bits = getbits($i);
  $i = 0;
  if ($bits[ 0]) $i|=0x0001;
  if ($bits[ 1]) $i|=0x0002;
  
  if ($bits[31]) $i|=0x8000;
  return $i;
  }

  function rshift($i, $n) {
  if ($i  -2147483648) {
  $i = convertto32($i);
  }
  return $i  $n;
  }


thx
  Michael Gross



Richard Lynch wrote:

On Thu, December 29, 2005 5:37 pm, Michael Gross wrote:

Hello
I have to migrate a PHP-application to a new Linux-Box. Both the old
and
the new system are Linux and PHP 5.1.1. (the old one has a Pentium 4,
the new one two Xeon CPUs). I have a problem using the
Crypt_Xtea-Extension. I narrowed it down to the following right-shift
operation:

(-3281063054  11) produces different results:
Old System: 495070
New System: -1048576

I understand that both results are wrong, but everything worked with
the old behavior and I need that behavior back very urgent.

Maybe someone can explain me in which way the bits are shifted so that
the result is 495070? If I understand it, I implement my own shift
function.


Assuming the previous hypothesis that it's 32-bit versus 64-bit
machines at work...

If you can determine the number of bits on your system, you could use
a different number from 11 on the two systems to get the answer you
want.

if (is_32_bit_machine()){
  $y = $x  11;
}
else{
  $y = $x  43; //11 + 32 (guess)
}

One hack for detecting 32-bit machine might be this:

function is_32_bit_machine(){
  return mt_getrandmax() == 0x;
}

I suppose if you're going to do this right, what you REALLY should do
is code it to determine the number of bits, no matter how large, and
then bit-shift the correct amount based on that.

So when the 128-bit machines come out in a few years, you aren't
re-coding this same damn problem AGAIN.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] Strange Right-Shift Problem

2005-12-29 Thread Michael Gross

Hello
I have to migrate a PHP-application to a new Linux-Box. Both the old and 
the new system are Linux and PHP 5.1.1. (the old one has a Pentium 4, 
the new one two Xeon CPUs). I have a problem using the 
Crypt_Xtea-Extension. I narrowed it down to the following right-shift 
operation:


(-3281063054  11) produces different results:
Old System: 495070
New System: -1048576

I understand that both results are wrong, but everything worked with 
the old behavior and I need that behavior back very urgent.


Maybe someone can explain me in which way the bits are shifted so that 
the result is 495070? If I understand it, I implement my own shift 
function.



thanks a lot
  Michael Gross

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php