base64.lua 6.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
if minetest.request_insecure_environment then
   ie = minetest.request_insecure_environment()
else
   ie = _G
end
--[[--------------------------------------------------------------------------

The MIT License (MIT)

Copyright (c) 2014 Mark Rogaski

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

----------------------------------------------------------------------------

This software includes portions of base64.lua, available at:

    https://gist.github.com/paulmoore/2563975

Copyright (C) 2012 by Paul Moore

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

--]]--------------------------------------------------------------------------

----------------------------------------------------------------------------
-- Package Definition
----------------------------------------------------------------------------

local Base64 = {}


----------------------------------------------------------------------------
-- Imports
----------------------------------------------------------------------------

local bit
local status,err = pcall(function() bit = ie.require 'bit' end)
if not status then
	bit = ie.dofile(minetest.get_modpath(minetest.get_current_modname()) .. '/slowbit32.lua')
end
local band, bor, lshift, rshift = bit.band, bit.bor, bit.lshift, bit.rshift

----------------------------------------------------------------------------
-- Constants and tables
----------------------------------------------------------------------------

local ENCODE = {
    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
    'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
    'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
    'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
    'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
    'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
    'w', 'x', 'y', 'z', '0', '1', '2', '3',
    '4', '5', '6', '7', '8', '9', '+', '/'  --- ARP: changed to Mime version
}

local DECODE = {}
for i, c in ipairs(ENCODE) do
    DECODE[c] = i - 1
end
 
----------------------------------------------------------------------------
-- Utility functions
----------------------------------------------------------------------------

--- Converts a 6-bit octet into the associated Base64 character.
--
-- @param octet A 6-bit integer.
-- @return The Base64 representation of the character
local function toChar(octet)
    return assert(ENCODE[octet + 1], "No Base64 character for octet: " .. tostring(octet))
end

--- Converts a Base64 character into the associated octet.
--
-- @param char The single Base64 character.
-- @return The 6-bit integer representing the Base64 character.
local function toOctet(char)
        return assert(DECODE[char], "Not a valid Base64 character: " .. tostring(char))
end


----------------------------------------------------------------------------
-- User functions
----------------------------------------------------------------------------

--- Encodes a string into a Base64 string.
-- The input can be any string of arbitrary bytes.
-- If the input is not a string, or the string is empty, an error will be thrown.
--
-- @param input The input string.
-- @return The Base64 representation of the input string.
function Base64.encode(sInput)
    assert(type(sInput) == "string", "Invalid input, expected type string but got: " .. tostring(sInput) .. " as a: " .. type(sInput))
    assert(#sInput > 0, "Invalid input, cannot encode an empty string.")    

    local b = { sInput:byte(1, #sInput) }
    local tOutput = {}
    
    for i = 1, #b, 3 do
        local c = {}
        c[1] = rshift(b[i], 2)
        if b[i + 1] ~= nil then
            c[2] = band(lshift(b[i], 4) + rshift(b[i + 1], 4), 0x3F)
            if b[i + 2] ~= nil then
                c[3] = band(lshift(b[i + 1], 2) + rshift(b[i + 2], 6), 0x3F)
                c[4] = band(b[i + 2], 0x3F)
            else
                c[3] = band(lshift(b[i + 1], 2), 0x3F)
            end
        else
            c[2] = band(lshift(b[i], 4), 0x3F)
        end
        for j = 1, 4 do
            if c[j] ~= nil then
                tOutput[#tOutput + 1] = toChar(c[j])
            else
                tOutput[#tOutput + 1] = '='
            end
        end
    end

    return table.concat(tOutput)
end

--- Decodes a Base64 string into an output string of arbitrary bytes.
-- If the input is not a string, or the string is empty, or the string is not well-formed Base64, an error will be thrown.
--
-- @param input The Base64 input to decode.
-- @return The decoded Base64 string, as a string of bytes.
function Base64.decode (sInput)
    assert(type(sInput) == "string", "Invalid input, expected type string but got: " .. tostring(sInput) .. " as a: " .. type(sInput))
    assert(#sInput > 0, "Invalid input, cannot decode an empty string.")  

    local tOutput = {}
    for i = 1, #sInput, 4 do
        local block, pad = string.gsub(string.sub(sInput, i, i + 3), '=', '')
        local buffer, b = 0, 0
        for j = 1, 4 - pad do
            b = toOctet(string.sub(block, j, j))
            b = lshift(b, (4 - j) * 6)
            buffer = bor(buffer, b)
        end
        for j = 1, 3 - pad do
            b = rshift(buffer, (3 - j) * 8) % 256
            tOutput[#tOutput + 1] = string.char(b)
        end
    end
    
    return table.concat(tOutput)
end


----------------------------------------------------------------------------
-- Package Registration
----------------------------------------------------------------------------

--Apollo.RegisterPackage(Base64, "Encoding:Base64-1.0", 1, {})

186
return Base64