Table of Content
HAL Forum
VOID AreaBase64Encode (
area RawData,var area EncodedData)
Description:
Encodes the contents of a specified area using Base64 encoding. NOTE that this function has been reserved for internal use and can not be used through HAL
Parameters:
RawData An area that holds the data that will be encoded
EncodedData An area that will receive the encoded data
// Here is an alternative to achieve AreaBase64Encode functionality.
// We will not use Base64Encode, as it is not bitsafe.

global 
procedure BitSafeAreaBase64Encode(area target,var area encoded) 
begin
  Area chunk,zero;
  LongInt alen,i,j;
  LongInt tmp_long;
  Integer cc;
  string 8 fromArea;
  string 70 b64;
  array string 1 b64_arr;

  // for faster flow
  b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  alen = len(b64);
  for(i=0;i<alen;i=i+1) begin
    b64_arr[i] = Mid(b64,i,1);
  end;

  alen = GetAreaLength(target);
  AddTextToArea(chr(0),zero);
  for(i=0;i<alen;i=i+3) begin
    SetAreaZeroSize(chunk);
    GetAreaFromArea(target,i,3,chunk);
    InsertAreaBeforeArea(zero,chunk); // we want to treat this chunk as unsigned long
    tmp_long = GetLongFromArea(chunk,0);
    for(j=0;j<4;j=j+1) begin
      cc = Mod(tmp_long,64);
      tmp_long = (tmp_long-cc)/64;
      fromArea = b64_arr[cc] & fromArea;
    end;
    AddTextToArea(fromArea,encoded);
    fromArea = "";
  end;

  switch(Mod(alen,3)) begin
    case 1: // 8 bit final
      fromArea = "==";
    case 2: // 16 bit final
      fromArea = "=";
  end;
  
  AddTextToArea(fromArea,encoded);
  return
end;
Related topics:
««