base64.lua

main 176 lines · 5.4 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1--[[
2
3 base64 -- v1.5.1 public domain Lua base64 encoder/decoder
4 no warranty implied; use at your own risk
5
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
10
11 author: Ilya Kolbin (iskolbin@gmail.com)
12 url: github.com/iskolbin/lbase64
13
14 COMPATIBILITY
15
16 Lua 5.1, 5.2, 5.3, LuaJIT
17
18 LICENSE
19
20 See end of file for license information.
21
22--]]
23
24
25base64 = {}
26
27local extract = _G.bit32 and _G.bit32.extract
28if not extract then
29 if _G.bit then
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 )
33 end
34 elseif _G._VERSION >= "Lua 5.3" then
35 extract = load[[return function( v, from, width )
36 return ( v >> from ) & ((1 << width) - 1)
37 end]]()
38 else
39 extract = function( v, from, width )
40 local w = 0
41 local flag = 2^from
42 for i = 0, width-1 do
43 local flag2 = flag + flag
44 if v % flag2 >= flag then
45 w = w + 2^i
46 end
47 flag = flag2
48 end
49 return w
50 end
51 end
52end
53
54
55function base64.makeencoder( s62, s63, spad )
56 local encoder = {}
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()
63 end
64 return encoder
65end
66
67function base64.makedecoder( s62, s63, spad )
68 local decoder = {}
69 for b64code, charcode in pairs( base64.makeencoder( s62, s63, spad )) do
70 decoder[charcode] = b64code
71 end
72 return decoder
73end
74
75local DEFAULT_ENCODER = base64.makeencoder()
76local DEFAULT_DECODER = base64.makedecoder()
77
78local char, concat = string.char, table.concat
79
80function base64.encode( str, encoder, usecaching )
81 encoder = encoder or DEFAULT_ENCODER
82 local t, k, n = {}, 1, #str
83 local lastn = n % 3
84 local cache = {}
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
88 local s
89 if usecaching then
90 s = cache[v]
91 if not s then
92 s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])
93 cache[v] = s
94 end
95 else
96 s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])
97 end
98 t[k] = s
99 k = k + 1
100 end
101 if lastn == 2 then
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])
108 end
109 return concat( t )
110end
111
112function base64.decode( b64, decoder, usecaching )
113 decoder = decoder or DEFAULT_DECODER
114 local pattern = '[^%w%+%/%=]'
115 if decoder then
116 local s62, s63
117 for charcode, b64code in pairs( decoder ) do
118 if b64code == 62 then s62 = charcode
119 elseif b64code == 63 then s63 = charcode
120 end
121 end
122 pattern = ('[^%%w%%%s%%%s%%=]'):format( char(s62), char(s63) )
123 end
124 b64 = b64:gsub( pattern, '' )
125 local cache = usecaching and {}
126 local t, k = {}, 1
127 local n = #b64
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 )
131 local s
132 if usecaching then
133 local v0 = a*0x1000000 + b*0x10000 + c*0x100 + d
134 s = cache[v0]
135 if not s then
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))
138 cache[v0] = s
139 end
140 else
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))
143 end
144 t[k] = s
145 k = k + 1
146 end
147 if padding == 1 then
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))
155 end
156 return concat( t )
157end
158
159--[[
160Copyright (c) 2018 Ilya Kolbin
161Permission is hereby granted, free of charge, to any person obtaining a copy of
162this software and associated documentation files (the "Software"), to deal in
163the Software without restriction, including without limitation the rights to
164use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
165of the Software, and to permit persons to whom the Software is furnished to do
166so, subject to the following conditions:
167The above copyright notice and this permission notice shall be included in all
168copies or substantial portions of the Software.
169THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
170IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
171FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
172AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
173LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
174OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
175SOFTWARE.
176--]]
Top