webmcp
annotate framework/env/encode/mime/base64.lua @ 28:ea2e8f3a2776
allow webmcp path to be set in cgi script
this allows another script to include the webmcp script when the cwd is not the cgi-bin. the script needs to set the WEBMCP_PATH variable.
this allows another script to include the webmcp script when the cwd is not the cgi-bin. the script needs to set the WEBMCP_PATH variable.
author | Daniel Poelzleithner <poelzi@poelzi.org> |
---|---|
date | Sun Sep 19 01:36:08 2010 +0200 (2010-09-19) |
parents | 9fdfb27f8e67 |
children |
rev | line source |
---|---|
jbe/bsw@0 | 1 local alphabet = { |
jbe/bsw@0 | 2 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", |
jbe/bsw@0 | 3 "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", |
jbe/bsw@0 | 4 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", |
jbe/bsw@0 | 5 "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", |
jbe/bsw@0 | 6 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/" |
jbe/bsw@0 | 7 } |
jbe/bsw@0 | 8 |
jbe/bsw@0 | 9 function encode.mime.base64(str) |
jbe/bsw@0 | 10 local parts = {} |
jbe/bsw@0 | 11 local pos = 1 |
jbe/bsw@0 | 12 local block_count = 0 |
jbe/bsw@0 | 13 while pos <= #str do |
jbe/bsw@0 | 14 local s = string.sub(str, pos, pos + 2) |
jbe/bsw@0 | 15 local n = 0 |
jbe/bsw@0 | 16 for i = 1, 3 do |
jbe/bsw@0 | 17 n = n * 256 |
jbe/bsw@0 | 18 if i <= #s then |
jbe/bsw@0 | 19 n = n + string.byte(string.sub(s, i, i)) |
jbe/bsw@0 | 20 end |
jbe/bsw@0 | 21 end |
jbe/bsw@0 | 22 parts[#parts+1] = alphabet[math.floor(n / 262144) + 1] |
jbe/bsw@0 | 23 parts[#parts+1] = alphabet[math.floor(n / 4096) % 64 + 1] |
jbe/bsw@0 | 24 if #s > 1 then |
jbe/bsw@0 | 25 parts[#parts+1] = alphabet[math.floor(n / 64) % 64 + 1] |
jbe/bsw@0 | 26 else |
jbe/bsw@0 | 27 parts[#parts+1] = "=" |
jbe/bsw@0 | 28 end |
jbe/bsw@0 | 29 if #s > 2 then |
jbe/bsw@0 | 30 parts[#parts+1] = alphabet[n % 64 + 1] |
jbe/bsw@0 | 31 else |
jbe/bsw@0 | 32 parts[#parts+1] = "=" |
jbe/bsw@0 | 33 end |
jbe/bsw@0 | 34 block_count = block_count + 1 |
jbe/bsw@0 | 35 if block_count == 19 then |
jbe/bsw@0 | 36 parts[#parts+1] = "\r\n" |
jbe/bsw@0 | 37 block_count = 0 |
jbe/bsw@0 | 38 end |
jbe/bsw@0 | 39 pos = pos + #s |
jbe/bsw@0 | 40 end |
jbe/bsw@0 | 41 if block_count > 0 then |
jbe/bsw@0 | 42 parts[#parts+1] = "\r\n" |
jbe/bsw@0 | 43 end |
jbe/bsw@0 | 44 return table.concat(parts) |
jbe/bsw@0 | 45 end |