3 base64 -- v1.5.1 public domain Lua base64 encoder/decoder
4 no warranty implied; use at your own risk
6 Needs bit32.extract function. If not present it's implemented using BitOp
7 or Lua 5.3 native bit operators. For Lua 5.1 fallbacks to pure Lua
8 implementation inspired by Rici Lake's post:
9 http://ricilake.blogspot.co.uk/2007/10/iterating-bits-in-lua.html
11 author: Ilya Kolbin (iskolbin@gmail.com)
12 url: github.com/iskolbin/lbase64
16 Lua 5.1, 5.2, 5.3, LuaJIT
20 See end of file for license information.
27 local extract
= _G
.bit32
and _G
.bit32
.extract
30 local shl
, shr
, band
= _G
.bit
.lshift
, _G
.bit
.rshift
, _G
.bit
.band
31 extract
= function( v
, from
, width
)
32 return band( shr( v
, from
), shl( 1, width
) - 1 )
34 elseif _G
._VERSION
>= "Lua 5.3" then
35 extract
= load
[[return function( v, from, width )
36 return ( v >> from ) & ((1 << width) - 1)
39 extract
= function( v
, from
, width
)
43 local flag2
= flag
+ flag
44 if v
% flag2
>= flag
then
55 function base64
.makeencoder( s62
, s63
, spad
)
57 for b64code
, char
in pairs
{[0]='A','B','C','D','E','F','G','H','I','J',
58 'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',
59 'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
60 'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2',
61 '3','4','5','6','7','8','9',s62
or '+',s63
or'/',spad
or'='} do
62 encoder
[b64code
] = char
:byte()
67 function base64
.makedecoder( s62
, s63
, spad
)
69 for b64code
, charcode
in pairs( base64
.makeencoder( s62
, s63
, spad
)) do
70 decoder
[charcode
] = b64code
75 local DEFAULT_ENCODER
= base64
.makeencoder()
76 local DEFAULT_DECODER
= base64
.makedecoder()
78 local char
, concat
= string.char
, table.concat
80 function base64
.encode( str
, encoder
, usecaching
)
81 encoder
= encoder
or DEFAULT_ENCODER
82 local t
, k
, n
= {}, 1, #str
85 for i
= 1, n
-lastn
, 3 do
86 local a
, b
, c
= str
:byte( i
, i
+2 )
87 local v
= a
*0x10000 + b
*0x100 + c
92 s
= char(encoder
[extract(v
,18,6)], encoder
[extract(v
,12,6)], encoder
[extract(v
,6,6)], encoder
[extract(v
,0,6)])
96 s
= char(encoder
[extract(v
,18,6)], encoder
[extract(v
,12,6)], encoder
[extract(v
,6,6)], encoder
[extract(v
,0,6)])
102 local a
, b
= str
:byte( n
-1, n
)
103 local v
= a
*0x10000 + b
*0x100
104 t
[k
] = char(encoder
[extract(v
,18,6)], encoder
[extract(v
,12,6)], encoder
[extract(v
,6,6)], encoder
[64])
105 elseif lastn
== 1 then
106 local v
= str
:byte( n
)*0x10000
107 t
[k
] = char(encoder
[extract(v
,18,6)], encoder
[extract(v
,12,6)], encoder
[64], encoder
[64])
112 function base64
.decode( b64
, decoder
, usecaching
)
113 decoder
= decoder
or DEFAULT_DECODER
114 local pattern
= '[^%w%+%/%=]'
117 for charcode
, b64code
in pairs( decoder
) do
118 if b64code
== 62 then s62
= charcode
119 elseif b64code
== 63 then s63
= charcode
122 pattern
= ('[^%%w%%%s%%%s%%=]'):format( char(s62
), char(s63
) )
124 b64
= b64
:gsub( pattern
, '' )
125 local cache
= usecaching
and {}
128 local padding
= b64
:sub(-2) == '==' and 2 or b64
:sub(-1) == '=' and 1 or 0
129 for i
= 1, padding
> 0 and n
-4 or n
, 4 do
130 local a
, b
, c
, d
= b64
:byte( i
, i
+3 )
133 local v0
= a
*0x1000000 + b
*0x10000 + c
*0x100 + d
136 local v
= decoder
[a
]*0x40000 + decoder
[b
]*0x1000 + decoder
[c
]*0x40 + decoder
[d
]
137 s
= char( extract(v
,16,8), extract(v
,8,8), extract(v
,0,8))
141 local v
= decoder
[a
]*0x40000 + decoder
[b
]*0x1000 + decoder
[c
]*0x40 + decoder
[d
]
142 s
= char( extract(v
,16,8), extract(v
,8,8), extract(v
,0,8))
148 local a
, b
, c
= b64
:byte( n
-3, n
-1 )
149 local v
= decoder
[a
]*0x40000 + decoder
[b
]*0x1000 + decoder
[c
]*0x40
150 t
[k
] = char( extract(v
,16,8), extract(v
,8,8))
151 elseif padding
== 2 then
152 local a
, b
= b64
:byte( n
-3, n
-2 )
153 local v
= decoder
[a
]*0x40000 + decoder
[b
]*0x1000
154 t
[k
] = char( extract(v
,16,8))
162 ------------------------------------------------------------------------------
163 This software is available under 2 licenses -- choose whichever you prefer.
164 ------------------------------------------------------------------------------
165 ALTERNATIVE A - MIT License
166 Copyright (c) 2018 Ilya Kolbin
167 Permission is hereby granted, free of charge, to any person obtaining a copy of
168 this software and associated documentation files (the "Software"), to deal in
169 the Software without restriction, including without limitation the rights to
170 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
171 of the Software, and to permit persons to whom the Software is furnished to do
172 so, subject to the following conditions:
173 The above copyright notice and this permission notice shall be included in all
174 copies or substantial portions of the Software.
175 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
176 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
178 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
179 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
180 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
182 ------------------------------------------------------------------------------
183 ALTERNATIVE B - Public Domain (www.unlicense.org)
184 This is free and unencumbered software released into the public domain.
185 Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
186 software, either in source code form or as a compiled binary, for any purpose,
187 commercial or non-commercial, and by any means.
188 In jurisdictions that recognize copyright laws, the author or authors of this
189 software dedicate any and all copyright interest in the software to the public
190 domain. We make this dedication for the benefit of the public at large and to
191 the detriment of our heirs and successors. We intend this dedication to be an
192 overt act of relinquishment in perpetuity of all present and future rights to
193 this software under copyright law.
194 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
195 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
196 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
197 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
198 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
199 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
200 ------------------------------------------------------------------------------