0

The legacy systems use a user control component (UserControl) which in its code has the Decode function, which in turn uses the Move function. I would like to convert pascal code to JavaScript to use the same user control in an API. Is it possible to convert Move function in Pascal to equivalent in JavaScript?

function Decrypt(const S: ansistring; Key: Word): ansistring;
begin
  Result := InternalDecrypt(PreProcess(S), Key);
end;

function InternalDecrypt(const S: ansistring; Key: Word): ansistring;
var
  I:    Word;
  Seed: int64;
begin
  Result := S;
  Seed   := Key;
  for I := 1 to Length(Result) do
  begin
    Result[I] := AnsiChar(byte(Result[I]) xor (Seed shr 8));
    Seed      := (byte(S[I]) + Seed) * word(C1) + word(C2);
  end;
end;

function PreProcess(const S: ansistring): ansistring;
var
  SS: ansistring;
begin
  SS     := S;
  Result := '';
  while SS <> '' do
  begin
    Result := Result + Decode(Copy(SS, 1, 4));
    Delete(SS, 1, 4);
  end;
end;

function Decode(const S: UTF8String): UTF8String;
const
  Map: array[0..255] of Byte = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 0, 0, 0, 63, 52, 53,
    54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2,
    3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
    20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 0, 0, 26, 27, 28, 29, 30,
    31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45,
    46, 47, 48, 49, 50, 51, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0);
var
  I: longint;
begin
  case Length(S) of
    2:
    begin
      I := Map[Ord(S[1])] + (Map[Ord(S[2])] shl 6);
      SetLength(Result, 1);
      Move(I, Result[1], Length(Result));
    end;
    3:
    begin
      I := Map[Ord(S[1])] + (Map[Ord(S[2])] shl 6) + (Map[Ord(S[3])] shl 12);
      SetLength(Result, 2);
      Move(I, Result[1], Length(Result));
    end;
    4:
    begin
      I := Map[Ord(S[1])] + (Map[Ord(S[2])] shl 6) + (Map[Ord(S[3])] shl 12) + (Map[Ord(S[4])] shl 18);
      SetLength(Result, 3);
      Move(I, Result[1], Length(Result));
    end
  end;
end;

procedure Move(const Source; var Dest; count : Integer );
var
    S, D: PChar;
    I: Integer;
begin
    S := PChar(@Source);
    D := PChar(@Dest);
    if S = D then Exit;
    if Cardinal(D) > Cardinal(S) then
        for I := count-1 downto 0 do
            D[I] := S[I]
    else
        for I := 0 to count-1 do
            D[I] := S[I];
end;
fCotrim
  • 1
  • 3
  • is your source and destination strings? – Aritra Chakraborty Jan 16 '20 at 18:30
  • `Move` is rather low-level function essentially equal to `CopyMemory`. Why do you need something similar in JS?? – MBo Jan 16 '20 at 18:53
  • @AritraChakraborty is called for example: Move(I, Result[1], Length(Result)), where I = longint and Result = string, so, Source it's longint and Dest it's char – fCotrim Jan 16 '20 at 20:19
  • @MBo I need that because Move is used in a Crypt/Decrypt code of legacy system... I would like to use the same dabase for authentication... – fCotrim Jan 16 '20 at 20:25
  • 1
    So you want to copy bytes? If you could tell us about the JS data type that would help. Because there really is no direct analogue to Delphi in JS. Context is crucial. – David Heffernan Jan 16 '20 at 21:18
  • 1
    that's a simple base64 decoder. Can you show the code where `Decode` is called? I'd be curious to know how/why you're decoding 2-4 base64 characters at a time. I'd guess that you have not base64 encoded the entire string, but character by character; what a waste of space. – Thomas Jan 17 '20 at 11:30
  • https://stackoverflow.com/questions/8482309/converting-javascript-integer-to-byte-array-and-back has what you ask for, but in reality you'd be better solving the right problem as @Thomas says – David Heffernan Jan 17 '20 at 11:58
  • @DavidHeffernan I put the all source code for better understanding... I'm actually trying to rewrite all of that code... – fCotrim Jan 22 '20 at 17:32
  • @Thomas here is all the code... – fCotrim Jan 22 '20 at 17:33
  • The method `PreProcess` in your code would be [atob](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/atob) in JS; so much to your initial question ;) – Thomas Jan 23 '20 at 07:15
  • @Thomas unfortunately using atob I couldn't achieve the same result of PreProcess function from pascal... in pascal PreProcess('hFqSaLwi') returns 'a¡JÚ‹' [97, 161, 74, 218, 2, 139] in JS returns 'Zh¼"' [132, 90, 146, 104, 188, 34] – fCotrim Jan 23 '20 at 11:12

0 Answers0