1. ----------------------------------------
  2. -- ASCII85 encode binary string (unoptimized!)
  3. ----------------------------------------
  4. on encode85 (input)
  5.  
  6.   cnt = input.length / 4
  7.   rest = input.length mod 4
  8.   output = ""
  9.   null = numtochar(0)
  10.  
  11.   repeat with i = 1 to cnt
  12.     chunk = input.char[i*4-3..i*4]
  13.    
  14.     if chunk.char[1]=null AND chunk.char[2]=null AND chunk.char[3]=null AND chunk.char[4]=null then
  15.       put "z" after output
  16.     else
  17.       n=0
  18.       repeat with j = 1 to 4
  19.         n = n + power(256,4-j) * chartonum(chunk.char[j])
  20.       end repeat
  21.      
  22.       s=""
  23.       repeat with j = 4 down to 2
  24.         put numtochar((floor(float(n)/power(85,j)) mod 85) + 33) after s
  25.       end repeat
  26.       put numtochar((floor(float(n)/85) mod 85) + 33) after s
  27.       put numtochar(umod(n,85) + 33) after s
  28.       put s after output
  29.     end if
  30.   end repeat
  31.  
  32.   -- REST
  33.   if rest>0 then
  34.     chunk = input.char[input.length-rest+1..input.length]
  35.     n=0
  36.     repeat with j = 1 to rest
  37.       n = n + power(256,4-j) * chartonum(chunk.char[j])
  38.     end repeat
  39.     s=""
  40.     repeat with j = 4 down to 2
  41.       put numtochar((floor(float(n)/power(85,j)) mod 85) + 33) after s
  42.     end repeat
  43.     put numtochar((floor(float(n)/85) mod 85) + 33) after s
  44.     put numtochar(umod(n,85) + 33) after s
  45.     put s.char[1..rest+1] after output
  46.   end if
  47.  
  48.   -- ADD LINE BREAKS
  49.   lineLen = 64 --64
  50.   cnt = output.length / lineLen
  51.   if (output.length mod lineLen)=0 then cnt = cnt - 1
  52.   repeat with i = cnt down to 1
  53.     put numtochar(10) after char i * lineLen of output
  54.   end repeat
  55.  
  56.   return output
  57. end
  58.  
  59. ----------------------------------------
  60. -- mod for a > maxint
  61. ----------------------------------------
  62. on umod (a, b)
  63.   max = power(2,31)-1
  64.  
  65.   if a > max then
  66.     k = floor(a / max)
  67.     n = k*(max mod b) + ((a-max) mod b)
  68.     return (n mod b)
  69.   else
  70.     return (a mod b)
  71.   end if
  72. end
  73.  
  74. ----------------------------------------
  75. --
  76. ----------------------------------------
  77. on floor n
  78.   return bitOr( n, 0 )
  79. end
[raw code]