Willem Hamelink
Willem Hamelink

Reputation: 13

Move Function - Pascal to PHP

Good day,

Trust all is well.

I want to duplicate the Move function from Pascal to PHP.

Here is what I have In Pascal:

function Encode(const S: AnsiString): AnsiString;
const
  Map: array [0 .. 63] of Char = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
    'abcdefghijklmnopqrstuvwxyz0123456789+/';
var
  i: LongInt;
begin
  i := 0;     ;
  Move(S[1], i, Length(S));
  Result := Map[i mod 64] + Map[(i shr 6) mod 64];
end;

Here is what I have in PHP:

private function Encode($pass)
    {
        $map = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/');
        $i = 0;
        $this->MoveFunction($pass[0], $i, mb_strlen($pass));
        $result = $map[$i % 63] . $map[($i >> 6) % 64];
        return $result;
    }

Now I now know that the Move function is used to copy a section of memory from one place to another, just not sure where to begin and how it would be done. I could not replicate the results from Pascal in PHP. I have tried sub strings ens. to no avail.

The "$this->MoveFunction" is the function that I will need to write in order to duplicate the functionality of the Move function from pascal. I need to use the same outcome of the Move from pascal in order to use the same encryption field from a similar DB.

I think this is an easy way to test in Pascal:

    var
A: array[1..4] of Char;
  B: Integer;
begin
A[1] := 'W';
  A[2] := 'H';
  A[3] := 'A';
  A[4] := 'T';
  B := 5;
  Move(A, B, SizeOf(B));
  showmessage(B.ToString()); // 4718679

Any help would be greatly appreciated.

Thank you in advance.

Upvotes: 0

Views: 307

Answers (1)

MBo
MBo

Reputation: 80197

Pascal code moves some AnsiChars into 32-bit Int variable. Note that the first char becomes the least significant byte of integer (due to byte order), and result is just equal to

Result := Map[Byte(S[1]) mod 64];

so Move is not needed at all, all other symbols of string aren't involved in work.

If you can cast the first symbol of $pass as byte/int variable in PHP - work is done.

P.S. I see ord() function, so code might look like this:
(I also changed % to bitwise &)

private function Encode($pass)
    {
        $map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
        $i = ord($pass[0]);
        $result = $map[$i & 63];
        return $result;
    }

For shr:

  Result := Map[i mod 64] + Map[(i shr 6) mod 64]; =>

  $a = ord($pass[0]) & 63;
  $b = ord($pass[0]) >> 6;      //two ms bits      

  $b = $b & 3;      //to clear ms bits

  $c = ord($pass[1]) & 15;  //four ls bits  
  $d = $b | ($c << 2);      //combine them
  $result = $map[$a].$map[$d];

Upvotes: 1

Related Questions