misc fixes in Libs

This commit is contained in:
Pinya
2018-07-22 21:20:20 +03:00
parent 5a9c2e9ade
commit 7621e0f4af
4 changed files with 41 additions and 51 deletions
+25 -34
View File
@@ -14,14 +14,13 @@ end
local error = error local error = error
local type = type local type = type
local mod = math.mod local fmod = math.fmod
local byte, char, format, len, sub = string.byte, string.char, string.format, string.len, string.sub local byte, char, format, len, sub = string.byte, string.char, string.format, string.len, string.sub
local concat = table.concat local concat, getn, tinsert, wipe = table.concat, table.getn, table.insert, table.wipe
local _chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' local _chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
local byteToNum = {} local byteToNum = {}
local numToChar = {} local numToChar = {}
for i = 1, len(_chars) do for i = 1, len(_chars) do
numToChar[i - 1] = sub(_chars, i, i) numToChar[i - 1] = sub(_chars, i, i)
byteToNum[byte(_chars, i)] = i - 1 byteToNum[byte(_chars, i)] = i - 1
@@ -60,7 +59,7 @@ function LibBase64:Encode(text, maxLineLength, lineEnding)
-- do nothing -- do nothing
elseif type(maxLineLength) ~= "number" then elseif type(maxLineLength) ~= "number" then
error(format("Bad argument #2 to `Encode'. Expected %q or %q, got %q", "number", "nil", type(maxLineLength)), 2) error(format("Bad argument #2 to `Encode'. Expected %q or %q, got %q", "number", "nil", type(maxLineLength)), 2)
elseif mod(maxLineLength, 4) ~= 0 then elseif fmod(maxLineLength, 4) ~= 0 then
error(format("Bad argument #2 to `Encode'. Expected a multiple of 4, got %s", maxLineLength), 2) error(format("Bad argument #2 to `Encode'. Expected a multiple of 4, got %s", maxLineLength), 2)
elseif maxLineLength <= 0 then elseif maxLineLength <= 0 then
error(format("Bad argument #2 to `Encode'. Expected a number > 0, got %s", maxLineLength), 2) error(format("Bad argument #2 to `Encode'. Expected a number > 0, got %s", maxLineLength), 2)
@@ -89,35 +88,33 @@ function LibBase64:Encode(text, maxLineLength, lineEnding)
end end
local num = a * 2^16 + b * 2^8 + c local num = a * 2^16 + b * 2^8 + c
local d = mod(num, 2^6) local d = fmod(num, 2^6)
num = (num - d) / 2^6 num = (num - d) / 2^6
local c = mod(num, 2^6) local c = fmod(num, 2^6)
num = (num - c) / 2^6 num = (num - c) / 2^6
local b = mod(num, 2^6) local b = fmod(num, 2^6)
num = (num - b) / 2^6 num = (num - b) / 2^6
local a = mod(num, 2^6) local a = fmod(num, 2^6)
t[getn(t)+1] = numToChar[a] tinsert(t, numToChar[a])
t[getn(t)+1] = numToChar[b] tinsert(t, numToChar[b])
t[getn(t)+1] = (nilNum >= 2) and "=" or numToChar[c] tinsert(t, ((nilNum >= 2) and "=" or numToChar[c]))
t[getn(t)+1] = (nilNum >= 1) and "=" or numToChar[d] tinsert(t, ((nilNum >= 1) and "=" or numToChar[d]))
currentLength = currentLength + 4 currentLength = currentLength + 4
if maxLineLength and mod(currentLength, maxLineLength) == 0 then if maxLineLength and fmod(currentLength, maxLineLength) == 0 then
t[getn(t)+1] = lineEnding tinsert(t, lineEnding)
end end
end end
local s = concat(t) local s = concat(t)
for i = 1, getn(t) do wipe(t)
t[i] = nil
end
return s return s
end end
@@ -141,13 +138,11 @@ function LibBase64:Decode(text)
else else
local num = byteToNum[byte] local num = byteToNum[byte]
if not num then if not num then
for i = 1, getn(t2) do wipe(t2)
t2[k] = nil
end
error(format("Bad argument #1 to `Decode'. Received an invalid char: %q", sub(text, i, i)), 2) error(format("Bad argument #1 to `Decode'. Received an invalid char: %q", sub(text, i, i)), 2)
end end
t2[getn(t2)+1] = num tinsert(t2, num)
end end
end end
@@ -166,32 +161,28 @@ function LibBase64:Decode(text)
local num = a * 2^18 + b * 2^12 + c * 2^6 + d local num = a * 2^18 + b * 2^12 + c * 2^6 + d
local c = mod(num, 2^8) local c = fmod(num, 2^8)
num = (num - c) / 2^8 num = (num - c) / 2^8
local b = mod(num, 2^8) local b = fmod(num, 2^8)
num = (num - b) / 2^8 num = (num - b) / 2^8
local a = mod(num, 2^8) local a = fmod(num, 2^8)
t[getn(t)+1] = char(a) tinsert(t, char(a))
if nilNum < 2 then if nilNum < 2 then
t[getn(t)+1] = char(b) tinsert(t, char(b))
end end
if nilNum < 1 then if nilNum < 1 then
t[getn(t)+1] = char(c) tinsert(t, char(c))
end end
end end
for i = 1, getn(t2) do wipe(t2)
t2[i] = nil
end
local s = concat(t) local s = concat(t)
for i = 1, getn(t) do wipe(t)
t[i] = nil
end
return s return s
end end
@@ -201,7 +192,7 @@ function LibBase64:IsBase64(text)
error(format("Bad argument #1 to `IsBase64'. Expected %q, got %q", "string", type(text)), 2) error(format("Bad argument #1 to `IsBase64'. Expected %q, got %q", "string", type(text)), 2)
end end
if mod(len(text), 4) ~= 0 then if fmod(len(text), 4) ~= 0 then
return false return false
end end
+13 -14
View File
@@ -34,6 +34,7 @@ local assert = assert
local table_insert = table.insert local table_insert = table.insert
local table_remove = table.remove local table_remove = table.remove
local table_concat = table.concat local table_concat = table.concat
local table_wipe = table.wipe
local string_char = string.char local string_char = string.char
local string_byte = string.byte local string_byte = string.byte
local string_len = string.len local string_len = string.len
@@ -41,7 +42,7 @@ local string_gsub = string.gsub
local string_sub = string.sub local string_sub = string.sub
local unpack = unpack local unpack = unpack
local pairs = pairs local pairs = pairs
local math_mod = math.mod local math_fmod = math.fmod
local bit_band = bit.band local bit_band = bit.band
local bit_bor = bit.bor local bit_bor = bit.bor
local bit_bxor = bit.bxor local bit_bxor = bit.bxor
@@ -99,15 +100,13 @@ end
-- the bytes returned by this do not contain "\000" -- the bytes returned by this do not contain "\000"
local bytes = {} local bytes = {}
local function encode(x) local function encode(x)
for k = 1, getn(bytes) do table_wipe(bytes)
bytes[k] = nil
end
bytes[getn(bytes) + 1] = math_mod(x, 255) table_insert(bytes, math_fmod(x, 255))
x=math.floor(x/255) x=math.floor(x/255)
while x > 0 do while x > 0 do
bytes[getn(bytes) + 1] = math_mod(x, 255) table_insert(bytes, math_fmod(x, 255))
x=math.floor(x/255) x=math.floor(x/255)
end end
if getn(bytes) == 1 and bytes[1] > 0 and bytes[1] < 250 then if getn(bytes) == 1 and bytes[1] > 0 and bytes[1] < 250 then
@@ -168,7 +167,7 @@ function LibCompress:CompressLZW(uncompressed)
dict_size = dict_size + 1 dict_size = dict_size + 1
local r = encode(dict[w]) local r = encode(dict[w])
ressize = ressize + string_len(r) ressize = ressize + string_len(r)
result[getn(result) + 1] = r table_insert(result, r)
w = c w = c
end end
end end
@@ -176,7 +175,7 @@ function LibCompress:CompressLZW(uncompressed)
if w then if w then
local r = encode(dict[w]) local r = encode(dict[w])
ressize = ressize + string_len(r) ressize = ressize + string_len(r)
result[getn(result) + 1] = r table_insert(result, r)
end end
if (string_len(uncompressed) + 1) > ressize then if (string_len(uncompressed) + 1) > ressize then
@@ -214,15 +213,15 @@ function LibCompress:DecompressLZW(compressed)
local delta, k local delta, k
k, delta = decode(compressed, t) k, delta = decode(compressed, t)
t = t + delta t = t + delta
result[getn(result) + 1] = dict[k] table_insert(result, dict[k])
local w = dict[k] local w = dict[k]
local entry local entry
while t <= getn(compressed) do while t <= string_len(compressed) do
k, delta = decode(compressed, t) k, delta = decode(compressed, t)
t = t + delta t = t + delta
entry = dict[k] or (w..string_sub(w, 1, 1)) entry = dict[k] or (w..string_sub(w, 1, 1))
result[getn(result) + 1] = entry table_insert(result, entry)
dict[dict_size] = w..string_sub(entry, 1, 1) dict[dict_size] = w..string_sub(entry, 1, 1)
dict_size = dict_size + 1 dict_size = dict_size + 1
w = entry w = entry
@@ -881,7 +880,7 @@ function LibCompress:GetEncodeTable(reservedChars, escapeChars, mapChars)
return nil, "No escape characters supplied" return nil, "No escape characters supplied"
end end
if getn(reservedChars) < getn(mapChars) then if string_len(reservedChars) < string_len(mapChars) then
return nil, "Number of reserved characters must be at least as many as the number of mapped chars" return nil, "Number of reserved characters must be at least as many as the number of mapped chars"
end end
@@ -914,8 +913,8 @@ function LibCompress:GetEncodeTable(reservedChars, escapeChars, mapChars)
local escapeCharIndex, escapeChar = 0 local escapeCharIndex, escapeChar = 0
-- map single byte to single byte -- map single byte to single byte
if getn(mapChars) > 0 then if string_len(mapChars) > 0 then
for i = 1, getn(mapChars) do for i = 1, string_len(mapChars) do
from = string_sub(reservedChars, i, i) from = string_sub(reservedChars, i, i)
to = string_sub(mapChars, i, i) to = string_sub(mapChars, i, i)
encode_translate[from] = to encode_translate[from] = to
@@ -152,7 +152,7 @@ function lib:VersionCheck()
local E = ElvUI[1] local E = ElvUI[1]
if event == "CHAT_MSG_ADDON" then if event == "CHAT_MSG_ADDON" then
if not (arg1 == lib.prefix and arg4 and arg1 and not strmatch(arg1, "^%s-$")) then return end if not (arg1 == lib.prefix and arg4 and arg2 and not strmatch(arg2, "^%s-$")) then return end
if arg4 == E.myname then return end if arg4 == E.myname then return end
if not E["pluginRecievedOutOfDateMessage"] then if not E["pluginRecievedOutOfDateMessage"] then
@@ -173,7 +173,7 @@ function lib:Register(mediatype, key, data, langmask)
mediatype = lower(mediatype) mediatype = lower(mediatype)
if mediatype == lib.MediaType.FONT and ((langmask and band(langmask, LOCALE_MASK) == 0) or not (langmask or locale_is_western)) then return false end if mediatype == lib.MediaType.FONT and ((langmask and band(langmask, LOCALE_MASK) == 0) or not (langmask or locale_is_western)) then return false end
if mediatype == lib.MediaType.SOUND and type(data) == "string" then if mediatype == lib.MediaType.SOUND and type(data) == "string" then
local path = data local path = lower(data)
-- Only wav, ogg and mp3 are valid sounds. -- Only wav, ogg and mp3 are valid sounds.
if not find(path, ".ogg", nil, true) and not find(path, ".mp3", nil, true) and not find(path, ".wav", nil, true) then if not find(path, ".ogg", nil, true) and not find(path, ".mp3", nil, true) and not find(path, ".wav", nil, true) then
return false return false