Guest User

unit CustomBase85;

a guest
Jan 3rd, 2013
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 4.26 KB | None | 0 0
  1. unit CustomBase85;
  2.  
  3.  
  4. // uses 85 ascii characters which are better to use in XML ( no &'<"\> chars and no space char = more efficient )
  5. //
  6. // Base85ToBin doesn't support white spaces and line breaks (HexToBin doesn't support it too)
  7.  
  8. {$mode delphi}
  9.  
  10. interface
  11.  
  12. uses
  13.   Classes;
  14.  
  15. procedure BinToBase85(BinValue, outputStringBase85: PChar; BinBufSize: integer);
  16. //
  17. //  example:
  18. //    getmem(outputstring, (BinarySize div 4) * 5 + 5 );
  19. //    BinToBase85(b,outputstring,BinarySize);
  20. //
  21. //  it adds a 0 terminator to outputstring
  22.  
  23. procedure Base85ToBin(inputStringBase85, BinValue: PChar);
  24. //
  25. //  example:
  26. //    size:=length(inputstring);
  27. //    if (size mod 5) > 1 then
  28. //      BinarySize:= (size div 5) * 4 + (size mod 5) - 1
  29. //    else
  30. //      BinarySize:= (size div 5) * 4;
  31. //   getmem(b, BinarySize);
  32. //   Base85ToBin(inputstring, b);
  33. //
  34. //  Base85ToBin doesn't support: white space between the characters, line breaks. (HexToBin doesn't support it too)
  35. //  Base85ToBin doesn't check for data corruption.
  36.  
  37. implementation
  38.  
  39. const
  40.   customBase85='0123456789'+
  41.                 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+
  42.                 'abcdefghijklmnopqrstuvwxyz'+
  43.                 '!#$%()*+,-./:;=?@[]^_{}';
  44.  
  45. procedure BinToBase85(BinValue, outputStringBase85: PChar; BinBufSize: integer);
  46. var
  47.   i : integer;
  48.   a : dword;
  49. begin
  50.  
  51.   i:=0;
  52.   while i<binbufsize do
  53.   begin
  54.  
  55.     //first byte ( from 4-tuple )
  56.     a:=pbyte(BinValue+i)^ shl 24;
  57.     inc(i);
  58.  
  59.     if i<binbufsize then
  60.     begin
  61.       //second byte
  62.       a:=a or pbyte(BinValue+i)^ shl 16;
  63.       inc(i);
  64.     end;
  65.  
  66.     if i<binbufsize then
  67.     begin
  68.       //third byte
  69.       a:=a or pbyte(BinValue+i)^ shl 8;
  70.       inc(i);
  71.     end;
  72.  
  73.     if i<binbufsize then
  74.     begin
  75.       //fourth byte
  76.       a:=a or pbyte(BinValue+i)^;
  77.       inc(i);
  78.     end;
  79.  
  80.     outputStringBase85[4]:= customBase85[a mod 85 + 1]; a:= a div 85;
  81.     outputStringBase85[3]:= customBase85[a mod 85 + 1]; a:= a div 85;
  82.     outputStringBase85[2]:= customBase85[a mod 85 + 1]; a:= a div 85;
  83.     outputStringBase85[1]:= customBase85[a mod 85 + 1]; a:= a div 85;
  84.     outputStringBase85[0]:= customBase85[a mod 85 + 1];
  85.     inc(outputStringBase85,5);
  86.  
  87.   end;
  88.  
  89.   //add zero terminator at right place
  90.   a:= (4 - (BinBufSize mod 4)) mod 4;
  91.   dec(outputStringBase85,a);
  92.   outputStringBase85[0]:=#0;
  93. end;
  94.  
  95. procedure Base85ToBin(inputStringBase85, BinValue: PChar);
  96. var i,j: integer;
  97.     size : integer;
  98.     a : dword;
  99. begin
  100.  
  101.   size:=length(inputStringBase85);
  102.  
  103.   i:=0;
  104.   j:=0;
  105.   while i<size do
  106.   begin
  107.     a:=( pos((inputStringBase85+i)^, customBase85) - 1 )*85*85*85*85;
  108.     inc(i);
  109.  
  110.     if i<size then
  111.     begin
  112.       a:= a + ( pos((inputStringBase85+i)^, customBase85) - 1 )*85*85*85;
  113.       inc(i);
  114.     end;
  115.  
  116.     if i<size then
  117.     begin
  118.       a:= a + ( pos((inputStringBase85+i)^, customBase85) - 1 )*85*85;
  119.       inc(i);
  120.     end;
  121.  
  122.     if i<size then
  123.     begin
  124.       a:= a + ( pos((inputStringBase85+i)^, customBase85) - 1 )*85;
  125.       inc(i);
  126.     end;
  127.  
  128.     if i<size then
  129.     begin
  130.       a:= a + ( pos((inputStringBase85+i)^, customBase85) - 1 );
  131.       inc(i);
  132.  
  133.       // 5-tuple
  134.       binvalue[j+0]:= char(  (a shr 24) and $ff  );
  135.       binvalue[j+1]:= char(  (a shr 16) and $ff  );
  136.       binvalue[j+2]:= char(  (a shr  8) and $ff  );
  137.       binvalue[j+3]:= char(  a and $ff           );
  138.       inc(j,4);
  139.     end;
  140.   end;
  141.  
  142.  
  143.   case (size mod 5) of
  144.     2: begin // must be padded with three digits (last radix85 digit used)
  145.          a:= a + 84*85*85 + 84*85 + 84;
  146.          binvalue[j+0]:= char(  (a shr 24) and $ff  ); // last three bytes of the output are ignored
  147.        end;
  148.  
  149.     3: begin // must be padded with two digits (last radix85 digit used)
  150.          a:= a            + 84*85 + 84;
  151.          binvalue[j+0]:= char(  (a shr 24) and $ff  );
  152.          binvalue[j+1]:= char(  (a shr 16) and $ff  ); // last two bytes of the output are ignored
  153.        end;
  154.  
  155.     4: begin // must be padded with one digit (last radix85 digit used)
  156.          a:= a                    + 84;
  157.          binvalue[j+0]:= char(  (a shr 24) and $ff  );
  158.          binvalue[j+1]:= char(  (a shr 16) and $ff  );
  159.          binvalue[j+2]:= char(  (a shr  8) and $ff  ); // last byte of the output is ignored
  160.        end;
  161.     end;
  162.  
  163. end;
  164.  
  165. end.
Advertisement
Add Comment
Please, Sign In to add comment