----------------------------------------
-- ASCII85 encode binary string (unoptimized!)
----------------------------------------
on encode85 (input)
cnt = input.length / 4
rest = input.length mod 4
output = ""
null = numtochar(0)
repeat with i = 1 to cnt
chunk = input.char[i*4-3..i*4]
if chunk.char[1]=null AND chunk.char[2]=null AND chunk.char[3]=null AND chunk.char[4]=null then
put "z" after output
else
n=0
repeat with j = 1 to 4
n = n + power(256,4-j) * chartonum(chunk.char[j])
end repeat
s=""
repeat with j = 4 down to 2
put numtochar((floor(float(n)/power(85,j)) mod 85) + 33) after s
end repeat
put numtochar((floor(float(n)/85) mod 85) + 33) after s
put numtochar(umod(n,85) + 33) after s
put s after output
end if
end repeat
-- REST
if rest>0 then
chunk = input.char[input.length-rest+1..input.length]
n=0
repeat with j = 1 to rest
n = n + power(256,4-j) * chartonum(chunk.char[j])
end repeat
s=""
repeat with j = 4 down to 2
put numtochar((floor(float(n)/power(85,j)) mod 85) + 33) after s
end repeat
put numtochar((floor(float(n)/85) mod 85) + 33) after s
put numtochar(umod(n,85) + 33) after s
put s.char[1..rest+1] after output
end if
-- ADD LINE BREAKS
lineLen = 64 --64
cnt = output.length / lineLen
if (output.length mod lineLen)=0 then cnt = cnt - 1
repeat with i = cnt down to 1
put numtochar(10) after char i * lineLen of output
end repeat
return output
end
----------------------------------------
-- mod for a > maxint
----------------------------------------
on umod (a, b)
max = power(2,31)-1
if a > max then
k = floor(a / max)
n = k*(max mod b) + ((a-max) mod b)
return (n mod b)
else
return (a mod b)
end if
end
----------------------------------------
--
----------------------------------------
on floor n
return bitOr( n, 0 )
end