Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
quarto-dev
GitHub Repository: quarto-dev/quarto-cli
Path: blob/main/src/resources/pandoc/datadir/_base64.lua
12922 views
1
---@diagnostic disable: undefined-field
2
--[[
3
4
base64 -- v1.5.3 public domain Lua base64 encoder/decoder
5
no warranty implied; use at your own risk
6
7
Needs bit32.extract function. If not present it's implemented using BitOp
8
or Lua 5.3 native bit operators. For Lua 5.1 fallbacks to pure Lua
9
implementation inspired by Rici Lake's post:
10
http://ricilake.blogspot.co.uk/2007/10/iterating-bits-in-lua.html
11
12
author: Ilya Kolbin ([email protected])
13
url: github.com/iskolbin/lbase64
14
15
COMPATIBILITY
16
17
Lua 5.1+, LuaJIT
18
19
LICENSE
20
21
See end of file for license information.
22
23
--]]
24
25
26
local extract = _G.bit32 and _G.bit32.extract -- Lua 5.2/Lua 5.3 in compatibility mode
27
if not extract then
28
if _G.bit then -- LuaJIT
29
local shl, shr, band = _G.bit.lshift, _G.bit.rshift, _G.bit.band
30
extract = function( v, from, width )
31
return band( shr( v, from ), shl( 1, width ) - 1 )
32
end
33
elseif _G._VERSION == "Lua 5.1" then
34
extract = function( v, from, width )
35
local w = 0
36
local flag = 2^from
37
for i = 0, width-1 do
38
local flag2 = flag + flag
39
if v % flag2 >= flag then
40
w = w + 2^i
41
end
42
flag = flag2
43
end
44
return w
45
end
46
else -- Lua 5.3+
47
extract = load[[return function( v, from, width )
48
return ( v >> from ) & ((1 << width) - 1)
49
end]]()
50
end
51
end
52
53
54
local function base64_makeencoder( s62, s63, spad )
55
local encoder = {}
56
for b64code, char in pairs{[0]='A','B','C','D','E','F','G','H','I','J',
57
'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',
58
'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
59
'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2',
60
'3','4','5','6','7','8','9',s62 or '+',s63 or'/',spad or'='} do
61
encoder[b64code] = char:byte()
62
end
63
return encoder
64
end
65
66
local function base64_makedecoder( s62, s63, spad )
67
local decoder = {}
68
for b64code, charcode in pairs( base64_makeencoder( s62, s63, spad )) do
69
decoder[charcode] = b64code
70
end
71
return decoder
72
end
73
74
local DEFAULT_ENCODER = base64_makeencoder()
75
local DEFAULT_DECODER = base64_makedecoder()
76
77
local char, concat = string.char, table.concat
78
79
local function base64_encode( str, encoder, usecaching )
80
encoder = encoder or DEFAULT_ENCODER
81
local t, k, n = {}, 1, #str
82
local lastn = n % 3
83
local cache = {}
84
for i = 1, n-lastn, 3 do
85
local a, b, c = str:byte( i, i+2 )
86
local v = a*0x10000 + b*0x100 + c
87
local s
88
if usecaching then
89
s = cache[v]
90
if not s then
91
s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])
92
cache[v] = s
93
end
94
else
95
s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])
96
end
97
t[k] = s
98
k = k + 1
99
end
100
if lastn == 2 then
101
local a, b = str:byte( n-1, n )
102
local v = a*0x10000 + b*0x100
103
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[64])
104
elseif lastn == 1 then
105
local v = str:byte( n )*0x10000
106
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[64], encoder[64])
107
end
108
return concat( t )
109
end
110
111
local function base64_decode( b64, decoder, usecaching )
112
decoder = decoder or DEFAULT_DECODER
113
local pattern = '[^%w%+%/%=]'
114
if decoder then
115
local s62, s63
116
for charcode, b64code in pairs( decoder ) do
117
if b64code == 62 then s62 = charcode
118
elseif b64code == 63 then s63 = charcode
119
end
120
end
121
pattern = ('[^%%w%%%s%%%s%%=]'):format( char(s62), char(s63) )
122
end
123
b64 = b64:gsub( pattern, '' )
124
local cache = usecaching and {}
125
local t, k = {}, 1
126
local n = #b64
127
local padding = b64:sub(-2) == '==' and 2 or b64:sub(-1) == '=' and 1 or 0
128
for i = 1, padding > 0 and n-4 or n, 4 do
129
local a, b, c, d = b64:byte( i, i+3 )
130
local s
131
if usecaching then
132
local v0 = a*0x1000000 + b*0x10000 + c*0x100 + d
133
s = cache[v0]
134
if not s then
135
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d]
136
s = char( extract(v,16,8), extract(v,8,8), extract(v,0,8))
137
cache[v0] = s
138
end
139
else
140
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d]
141
s = char( extract(v,16,8), extract(v,8,8), extract(v,0,8))
142
end
143
t[k] = s
144
k = k + 1
145
end
146
if padding == 1 then
147
local a, b, c = b64:byte( n-3, n-1 )
148
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40
149
t[k] = char( extract(v,16,8), extract(v,8,8))
150
elseif padding == 2 then
151
local a, b = b64:byte( n-3, n-2 )
152
local v = decoder[a]*0x40000 + decoder[b]*0x1000
153
t[k] = char( extract(v,16,8))
154
end
155
return concat( t )
156
end
157
158
--[[
159
------------------------------------------------------------------------------
160
This software is available under 2 licenses -- choose whichever you prefer.
161
------------------------------------------------------------------------------
162
ALTERNATIVE A - MIT License
163
Copyright (c) 2018 Ilya Kolbin
164
Permission is hereby granted, free of charge, to any person obtaining a copy of
165
this software and associated documentation files (the "Software"), to deal in
166
the Software without restriction, including without limitation the rights to
167
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
168
of the Software, and to permit persons to whom the Software is furnished to do
169
so, subject to the following conditions:
170
The above copyright notice and this permission notice shall be included in all
171
copies or substantial portions of the Software.
172
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
173
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
175
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
176
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
177
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
178
SOFTWARE.
179
------------------------------------------------------------------------------
180
ALTERNATIVE B - Public Domain (www.unlicense.org)
181
This is free and unencumbered software released into the public domain.
182
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
183
software, either in source code form or as a compiled binary, for any purpose,
184
commercial or non-commercial, and by any means.
185
In jurisdictions that recognize copyright laws, the author or authors of this
186
software dedicate any and all copyright interest in the software to the public
187
domain. We make this dedication for the benefit of the public at large and to
188
the detriment of our heirs and successors. We intend this dedication to be an
189
overt act of relinquishment in perpetuity of all present and future rights to
190
this software under copyright law.
191
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
192
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
193
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
194
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
195
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
196
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
197
------------------------------------------------------------------------------
198
--]]
199
200
return {
201
encode = base64_encode,
202
decode = base64_decode
203
}
204