mirror of
https://github.com/minetest-mods/MoreMesecons.git
synced 2024-09-12 08:08:37 +02:00
Made md5.lua use BitOp or Bit32 libraries where available
This commit is contained in:
parent
191b026807
commit
e7df9d53cf
20
md5.lua
20
md5.lua
@ -33,7 +33,14 @@ local md5 = {
|
||||
local floor, abs, max = math.floor, math.abs, math.max
|
||||
local char, byte, format, rep, sub =
|
||||
string.char, string.byte, string.format, string.rep, string.sub
|
||||
local bit_or, bit_and, bit_not, bit_xor, bit_rshift, bit_lshift
|
||||
|
||||
if require "bit" or require "bit32" then
|
||||
local bit = require "bit" or require "bit32"
|
||||
|
||||
bit_or, bit_and, bit_not, bit_xor = bit.bor, bit.band, bit.bnot, bit.bxor
|
||||
bit_rshift, bit_lshift = bit.rshift, bit.lshift
|
||||
else
|
||||
local function check_int(n)
|
||||
-- checking not float
|
||||
if(n - floor(n) > 0) then
|
||||
@ -73,7 +80,7 @@ end
|
||||
|
||||
local to_bits -- needs to be declared before bit_not
|
||||
|
||||
local function bit_not(n)
|
||||
function bit_not(n)
|
||||
local tbl = to_bits(n)
|
||||
local size = max(#tbl, 32)
|
||||
for i = 1, size do
|
||||
@ -110,7 +117,7 @@ to_bits = function (n)
|
||||
return tbl
|
||||
end
|
||||
|
||||
local function bit_or(m, n)
|
||||
function bit_or(m, n)
|
||||
local tbl_m = to_bits(m)
|
||||
local tbl_n = to_bits(n)
|
||||
expand(tbl_m, tbl_n)
|
||||
@ -128,7 +135,7 @@ local function bit_or(m, n)
|
||||
return tbl2number(tbl)
|
||||
end
|
||||
|
||||
local function bit_and(m, n)
|
||||
function bit_and(m, n)
|
||||
local tbl_m = to_bits(m)
|
||||
local tbl_n = to_bits(n)
|
||||
expand(tbl_m, tbl_n)
|
||||
@ -146,7 +153,7 @@ local function bit_and(m, n)
|
||||
return tbl2number(tbl)
|
||||
end
|
||||
|
||||
local function bit_xor(m, n)
|
||||
function bit_xor(m, n)
|
||||
local tbl_m = to_bits(m)
|
||||
local tbl_n = to_bits(n)
|
||||
expand(tbl_m, tbl_n)
|
||||
@ -164,7 +171,7 @@ local function bit_xor(m, n)
|
||||
return tbl2number(tbl)
|
||||
end
|
||||
|
||||
local function bit_rshift(n, bits)
|
||||
function bit_rshift(n, bits)
|
||||
check_int(n)
|
||||
|
||||
local high_bit = 0
|
||||
@ -181,7 +188,7 @@ local function bit_rshift(n, bits)
|
||||
return floor(n)
|
||||
end
|
||||
|
||||
local function bit_lshift(n, bits)
|
||||
function bit_lshift(n, bits)
|
||||
check_int(n)
|
||||
|
||||
if(n < 0) then
|
||||
@ -194,6 +201,7 @@ local function bit_lshift(n, bits)
|
||||
end
|
||||
return bit_and(n, 4294967295) -- 0xFFFFFFFF
|
||||
end
|
||||
end
|
||||
|
||||
-- convert little-endian 32-bit int to a 4-char string
|
||||
local function lei2str(i)
|
||||
|
Loading…
Reference in New Issue
Block a user