Path: blob/main/src/resources/pandoc/datadir/_base64.lua
12922 views
---@diagnostic disable: undefined-field1--[[23base64 -- v1.5.3 public domain Lua base64 encoder/decoder4no warranty implied; use at your own risk56Needs bit32.extract function. If not present it's implemented using BitOp7or Lua 5.3 native bit operators. For Lua 5.1 fallbacks to pure Lua8implementation inspired by Rici Lake's post:9http://ricilake.blogspot.co.uk/2007/10/iterating-bits-in-lua.html1011author: Ilya Kolbin ([email protected])12url: github.com/iskolbin/lbase641314COMPATIBILITY1516Lua 5.1+, LuaJIT1718LICENSE1920See end of file for license information.2122--]]232425local extract = _G.bit32 and _G.bit32.extract -- Lua 5.2/Lua 5.3 in compatibility mode26if not extract then27if _G.bit then -- LuaJIT28local shl, shr, band = _G.bit.lshift, _G.bit.rshift, _G.bit.band29extract = function( v, from, width )30return band( shr( v, from ), shl( 1, width ) - 1 )31end32elseif _G._VERSION == "Lua 5.1" then33extract = function( v, from, width )34local w = 035local flag = 2^from36for i = 0, width-1 do37local flag2 = flag + flag38if v % flag2 >= flag then39w = w + 2^i40end41flag = flag242end43return w44end45else -- Lua 5.3+46extract = load[[return function( v, from, width )47return ( v >> from ) & ((1 << width) - 1)48end]]()49end50end515253local function base64_makeencoder( s62, s63, spad )54local encoder = {}55for b64code, char in pairs{[0]='A','B','C','D','E','F','G','H','I','J',56'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',57'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',58'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2',59'3','4','5','6','7','8','9',s62 or '+',s63 or'/',spad or'='} do60encoder[b64code] = char:byte()61end62return encoder63end6465local function base64_makedecoder( s62, s63, spad )66local decoder = {}67for b64code, charcode in pairs( base64_makeencoder( s62, s63, spad )) do68decoder[charcode] = b64code69end70return decoder71end7273local DEFAULT_ENCODER = base64_makeencoder()74local DEFAULT_DECODER = base64_makedecoder()7576local char, concat = string.char, table.concat7778local function base64_encode( str, encoder, usecaching )79encoder = encoder or DEFAULT_ENCODER80local t, k, n = {}, 1, #str81local lastn = n % 382local cache = {}83for i = 1, n-lastn, 3 do84local a, b, c = str:byte( i, i+2 )85local v = a*0x10000 + b*0x100 + c86local s87if usecaching then88s = cache[v]89if not s then90s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])91cache[v] = s92end93else94s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])95end96t[k] = s97k = k + 198end99if lastn == 2 then100local a, b = str:byte( n-1, n )101local v = a*0x10000 + b*0x100102t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[64])103elseif lastn == 1 then104local v = str:byte( n )*0x10000105t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[64], encoder[64])106end107return concat( t )108end109110local function base64_decode( b64, decoder, usecaching )111decoder = decoder or DEFAULT_DECODER112local pattern = '[^%w%+%/%=]'113if decoder then114local s62, s63115for charcode, b64code in pairs( decoder ) do116if b64code == 62 then s62 = charcode117elseif b64code == 63 then s63 = charcode118end119end120pattern = ('[^%%w%%%s%%%s%%=]'):format( char(s62), char(s63) )121end122b64 = b64:gsub( pattern, '' )123local cache = usecaching and {}124local t, k = {}, 1125local n = #b64126local padding = b64:sub(-2) == '==' and 2 or b64:sub(-1) == '=' and 1 or 0127for i = 1, padding > 0 and n-4 or n, 4 do128local a, b, c, d = b64:byte( i, i+3 )129local s130if usecaching then131local v0 = a*0x1000000 + b*0x10000 + c*0x100 + d132s = cache[v0]133if not s then134local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d]135s = char( extract(v,16,8), extract(v,8,8), extract(v,0,8))136cache[v0] = s137end138else139local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d]140s = char( extract(v,16,8), extract(v,8,8), extract(v,0,8))141end142t[k] = s143k = k + 1144end145if padding == 1 then146local a, b, c = b64:byte( n-3, n-1 )147local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40148t[k] = char( extract(v,16,8), extract(v,8,8))149elseif padding == 2 then150local a, b = b64:byte( n-3, n-2 )151local v = decoder[a]*0x40000 + decoder[b]*0x1000152t[k] = char( extract(v,16,8))153end154return concat( t )155end156157--[[158------------------------------------------------------------------------------159This software is available under 2 licenses -- choose whichever you prefer.160------------------------------------------------------------------------------161ALTERNATIVE A - MIT License162Copyright (c) 2018 Ilya Kolbin163Permission is hereby granted, free of charge, to any person obtaining a copy of164this software and associated documentation files (the "Software"), to deal in165the Software without restriction, including without limitation the rights to166use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies167of the Software, and to permit persons to whom the Software is furnished to do168so, subject to the following conditions:169The above copyright notice and this permission notice shall be included in all170copies or substantial portions of the Software.171THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR172IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,173FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE174AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER175LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,176OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE177SOFTWARE.178------------------------------------------------------------------------------179ALTERNATIVE B - Public Domain (www.unlicense.org)180This is free and unencumbered software released into the public domain.181Anyone is free to copy, modify, publish, use, compile, sell, or distribute this182software, either in source code form or as a compiled binary, for any purpose,183commercial or non-commercial, and by any means.184In jurisdictions that recognize copyright laws, the author or authors of this185software dedicate any and all copyright interest in the software to the public186domain. We make this dedication for the benefit of the public at large and to187the detriment of our heirs and successors. We intend this dedication to be an188overt act of relinquishment in perpetuity of all present and future rights to189this software under copyright law.190THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR191IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,192FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE193AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN194ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION195WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.196------------------------------------------------------------------------------197--]]198199return {200encode = base64_encode,201decode = base64_decode202}203204