Update lua versions
[ryzomcore.git] / ryzom / client / data / gamedev / interfaces_v3 / base64.lua
blob60aab44bedd7ec2c10d56ec96faa0faf18dfef7a
1 --[[
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
14 COMPATIBILITY
16 Lua 5.1, 5.2, 5.3, LuaJIT
18 LICENSE
20 See end of file for license information.
22 --]]
25 base64 = {}
27 local extract = _G.bit32 and _G.bit32.extract
28 if 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
52 end
55 function 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
65 end
67 function 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
73 end
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
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
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])
109 return concat( t )
112 function 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
122 pattern = ('[^%%w%%%s%%%s%%=]'):format( char(s62), char(s63) )
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
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))
144 t[k] = s
145 k = k + 1
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))
156 return concat( t )
159 return base64
161 --[[
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
181 SOFTWARE.
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 ------------------------------------------------------------------------------
201 --]]
204 -- VERSION --
205 RYZOM_BASE64_VERSION = 10469