Recent

Author Topic: Move Function - Pascal to PHP  (Read 1102 times)

Willem

  • Newbie
  • Posts: 2
Move Function - Pascal to PHP
« on: May 22, 2020, 04:30:07 pm »
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) % 63];
        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.

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.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: Move Function - Pascal to PHP
« Reply #1 on: May 22, 2020, 05:27:46 pm »
You need to look at the PHP class code for the MoveFunction.

Its not very revealing as to what it is really doing..
The only true wisdom is knowing you know nothing

Willem

  • Newbie
  • Posts: 2
Re: Move Function - Pascal to PHP
« Reply #2 on: May 22, 2020, 06:50:22 pm »
Hi @Jamie, you are right sorry about that. The "MoveFunction" is what I need to write that will have the same functionality and behavior as the built in function "Move" from Pascal.

procedure move ( const SourcePointer; var DestinationPointer; CopyCount : Integer ) ;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11455
  • FPC developer.
Re: Move Function - Pascal to PHP
« Reply #3 on: May 22, 2020, 06:59:38 pm »
That procedure just moves "count"  bytes from the first adres to the secon

What the PHP equivalent is, (and IF there is an equivalent in an interpreted language) is better asked on a PHP forum.


Warfley

  • Hero Member
  • *****
  • Posts: 1499
Re: Move Function - Pascal to PHP
« Reply #4 on: May 22, 2020, 07:16:22 pm »
PHP variables are not simply memory objects you can access like in a language like Pascal, so you simply can't do this.

That said, look at what you are doing here. You interpret the string as a continuous byte array and copy it bytewise into a 32 bit integer.
This can be fully expressed using arithmetic:
Code: Pascal  [Select][+][-]
  1. var c: char;
  2.   shift: Integer;
  3. ...
  4. shift := Sizeof(i) * 8 - 8; // little endian
  5. for c in S do
  6. begin
  7.   i += Ord(c) shl shift;
  8.   shift -= 8;
  9. end;
Note, your architecture on which you are running your FPC code is probably little endian, i.e. the smallest address (i.e. first byte) denotes the highest value (i.e. the end written as number is on the smalles address, therefore little end -> little endian)

And reduced to arithmetic, this can be easiely ported to php

Edit: You can simulate direct memory accesses using the pack and unpack function: https://stackoverflow.com/questions/11544821/how-to-convert-integer-to-byte-array-in-php

Edit2: If you are writing an Base64 encoder, you don't need to do that, PHP can do that by itself
« Last Edit: May 22, 2020, 07:23:45 pm by Warfley »

 

TinyPortal © 2005-2018