Skip to Content
It is currently May 27th, 2019, 2:05 am

All times are UTC - 6 hours [ DST ]




Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: February 9th, 2019, 6:15 am 
Offline
Senior User
Senior User

Joined: September 7th, 2018, 9:00 am
Posts: 99
I would need to know if PAL can encode a string in base64

If yes, how is the correct sintaxis?

Thanks a million


Top
 Profile  
Reply with quote  
PostPosted: February 9th, 2019, 9:25 am 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 7836
Location: Aachen (Germany)
There's no readymade function for encoding base64 if that's what you want to know.
Obviously it's possible to calculate base64 by hand. PAL has all the neccessary math functions built-in to do that by hand:
Code:
(**
@author Benedikt Bauer <benedikt.bauer@bbauer.eu>
@license GNU AGPL - http://www.gnu.org/licenses/agpl-3.0.txt
*)

FUNCTION dec2bin(dec : Integer) : String;
VAR remainder : Integer;
temp : String;
BEGIN
  remainder := dec;
  WHILE remainder > 0 DO
  BEGIN
    temp := IntToStr(remainder mod 2) + temp;
//    WriteLn('Bin: ' + temp);
    remainder := remainder div 2;
//    WriteLn('Remainder: ' + IntToStr(remainder));
  END;
  WHILE Length(temp) mod 4 > 0 DO
    temp := '0' + temp;

  result := temp;
END;

FUNCTION str2bin(str : String) : String;
VAR temp : String;
i : Integer;
BEGIN
  FOR i := 1 TO Length(str) DO
    temp := temp + dec2bin(Ord(str[i]));

  result := temp;
END;

FUNCTION base64encode(str : String) : String;
VAR binary, sextbin, resulttemp, lookup : String;
multiplier : Float;
i, j, k, len, pow, exp, sextet : Integer;
BEGIN
  // Base64 Lookup Table
  lookup := 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  binary := str2bin(str);
  WHILE i < Length(binary) DO
  BEGIN
//    WriteLn('i: ' + IntToStr(i));
    // 6 digit binary substring
    sextbin := Copy(binary, i+1, 6);

    // Add padding
    WHILE Length(sextbin) < 6 DO
      sextbin := sextbin + '0';
//    WriteLn('Binary: ' + sextbin);
    // j runs from LSB to MSB
    j := 6;
    // k runs the exponent
    FOR k := 0 TO 5 DO
    BEGIN
//    WriteLn('j: ' + IntToStr(j));
      multiplier := StrToFloat(sextbin[j]);
      exp := k;
      // digit * 2^pos
      pow := StrToInt(FloatToStr(multiplier * Power(2.0, exp)));
//      WriteLn(FloatToStr(multiplier) + ' * 2^' + IntToStr(exp) + '=' + IntToStr(pow));
      sextet := sextet + pow;
      j := j-1;
    END;
//    WriteLn('Number: ' + IntToStr(sextet));
//    WriteLn('Base64: ' + lookup[sextet+1]);
    resulttemp := resulttemp + lookup[sextet+1];
    sextet := 0;
    sextbin := '';
    i := i+6;
  END;
 
  // Add = padding to the base64 string
  WHILE Length(resulttemp) mod 4 > 0 DO
    resulttemp := resulttemp + '=';

  result := resulttemp;
END;

// Example encodings, taken from Wikipedia (https://en.wikipedia.org/wiki/Base64#Examples) for verifying the correctness of my algorithm
//WriteLn(resulttemp);
WriteLn(base64encode('Man'));
WriteLn(base64encode('Ma'));
WriteLn(base64encode('M'));
WriteLn(base64encode('any carnal pleasure.'));

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

We're offering custom PAL / PHP code and general SAM assistance at palscripts.com

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
Reply with quote  
PostPosted: April 18th, 2019, 2:41 am 
Offline
Senior User
Senior User

Joined: September 7th, 2018, 9:00 am
Posts: 99
It's arrived the moment to work with. But to write each time the function is really not practical.
I'm wondering if is there any possibility to write it into a file and import it time by time.

base64 is really practical when you need to send several arguments using GET and sometimes also POST, if ever strange characers are used etc ...

when sent in base64 is much "lighter" and it can be decoded from the other side quite easily

I'm find also strange that in 20 years no one yet implemented a readymade function in PAL


Top
 Profile  
Reply with quote  
PostPosted: April 18th, 2019, 1:34 pm 
Offline
SVS Member
SVS Member

Joined: December 6th, 2004, 9:00 am
Posts: 7836
Location: Aachen (Germany)
Tormy wrote:
It's arrived the moment to work with. But to write each time the function is really not practical.
I'm wondering if is there any possibility to write it into a file and import it time by time.

In theory, yes. In practice, no.
Pascal has a feature called Include directive to import code from external files within the search path:
Code:
{$INCLUDE 'base64encode.pal'}

Only problem is since PAL is an embedded ObjectPascal inside another ObjectPascal application, you can't modify the search path and can't even print it.
I'm not even sure if it's possible to have it search in the filesystem or if it's limited to resources embedded in the SAMBC.exe binary.
The default case for include would be to search in the same directory as the file you compile or in the full path given to the include directive, but neither seems to work in PAL.
Feel free to investigate further, the engine used to build PAL is called DwScript.


Tormy wrote:
base64 is really practical when you need to send several arguments using GET and sometimes also POST, if ever strange characers are used etc ...

Base64 is great for exchanging binary data.
If you have any kind of structured, text-based/numeric data, base64 is NOT the way to go.

Tormy wrote:
when sent in base64 is much "lighter" and it can be decoded from the other side quite easily

When sent in JSON it's much easier to read and diagnose problems and it can be decoded by all kinds of software and if there's no native support it's easy to decode using basic string manipulation.

Tormy wrote:
I'm find also strange that in 20 years no one yet implemented a readymade function in PAL

Have you ever had a look at the PAL forums and what people try to achieve with it or want from PAL?
There are like 30 people in the history of this forum that went beyond Playlist automation with PAL. I doubt there are more than 2-3 people that would ever find a use for base64 in PAL, even if you told them about it.

Feel free to ask the Spacial support for it, though. After all, they also exposed the Delphi THttp API to PAL after being asked about TLS support and more complex API support.

_________________
Benedikt Bauer - SVS (Spacial Volunteer Support)

We're offering custom PAL / PHP code and general SAM assistance at palscripts.com

My Project:
Send "Now Playing" from SAM to Twitter and/or Facebook | Sourcecode


Top
 Profile  
Reply with quote  
PostPosted: April 19th, 2019, 12:54 am 
Offline
Senior User
Senior User

Joined: September 7th, 2018, 9:00 am
Posts: 99
The THttp API I have to investigate it deeply

yes ... JSon ... it's also a super valid possibility (I aks myself why i din't think about :-) it was made on purpose :-D )


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 6 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group