webmcp
view framework/env/encode/mime/base64.lua @ 2:72860d232f32
Version 1.0.2
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
| author | jbe/bsw | 
|---|---|
| date | Thu Dec 10 12:00:00 2009 +0100 (2009-12-10) | 
| parents | 9fdfb27f8e67 | 
| children | 
 line source
     1 local alphabet = {
     2   "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
     3   "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
     4   "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
     5   "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
     6   "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"
     7 }
     9 function encode.mime.base64(str)
    10   local parts = {}
    11   local pos = 1
    12   local block_count = 0
    13   while pos <= #str do
    14     local s = string.sub(str, pos, pos + 2)
    15     local n = 0
    16     for i = 1, 3 do
    17       n = n * 256
    18       if i <= #s then
    19         n = n + string.byte(string.sub(s, i, i))
    20       end
    21     end
    22     parts[#parts+1] = alphabet[math.floor(n / 262144) + 1]
    23     parts[#parts+1] = alphabet[math.floor(n / 4096) % 64 + 1]
    24     if #s > 1 then
    25       parts[#parts+1] = alphabet[math.floor(n / 64) % 64 + 1]
    26     else
    27       parts[#parts+1] = "="
    28     end
    29     if #s > 2 then
    30       parts[#parts+1] = alphabet[n % 64 + 1]
    31     else
    32       parts[#parts+1] = "="
    33     end
    34     block_count = block_count + 1
    35     if block_count == 19 then
    36       parts[#parts+1] = "\r\n"
    37       block_count = 0
    38     end
    39     pos = pos + #s
    40   end
    41   if block_count > 0 then
    42     parts[#parts+1] = "\r\n"
    43   end
    44   return table.concat(parts)
    45 end
