Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- do
- local char = string.char
- local floor = math.floor
- -- Bit manipulation
- -----------------------------
- local band32 = bit32.band32
- local lshift32 = bit32.lshift
- local rshift32 = bit32.rshift
- local function lshift64 (num, shifts)
- local result = num
- while ( shifts > 0 ) do
- result = result + result
- shifts = shifts - 1
- end
- return result
- end
- local function rshift64 (num, shifts)
- local result = num
- while ( shifts > 0 ) do
- result = result / 2
- shifts = shifts - 1
- end
- return floor(result)
- end
- local function multilshift (num, shifts)
- if ( num > 0x7FFFFFFF ) then
- return lshift64(num, shifts)
- end
- return lshift32(num, shifts)
- end
- local function multirshift (num, shifts)
- if ( num > 0x7FFFFFFF ) then
- return rshift64(num, shifts)
- end
- return rshift32(num, shifts)
- end
- --- Internal facilities
- ---------------------------------
- --[=[
- * A stack stores numbers, and numbers store octet bytes.
- * Octet bytes are accessed by a location holding their respective
- * number index and initial bit index.
- ]=]
- local MAX_BYTES =
- (rshift64(0xFFFFFFFF + 1, 1) == 0x80000000) and 6 or 3
- local function nextlocation (locNum, locBit)
- if ( locBit >= 40 ) then
- return locNum + 1, 0
- end
- return locNum, locBit + 8
- end
- local function reachlocation
- ( targetPos
- , locNum
- , locBit
- , curPos )
- while ( curPos < targetPos ) do
- locNum, locBit =
- nextlocation(locNum, locBit)
- end
- return locNum, locBit
- end
- local function resize (stack, len, curlen)
- if ( len > curlen ) then
- for i = curlen + 1, len do
- stack[i] = 0
- end
- else
- for i = len + 1, curlen do
- stack[i] = nil
- end
- end
- end
- local function updnummid (stack, index, ahead, rest)
- local num = stack[index]
- local left = rshift64(num, ahead)
- stack[locNum] = left + value +
- band32(locNum, rest)
- end
- local function setbyte (stack, locNum, locBit, value)
- -- Intentionally repeated conditions
- -- may decrease some operations.
- if ( locBit >= 40 ) then
- -- Assign sixth byte
- stack[locNum] =
- value + rshift64(num, 40)
- elseif ( locBit >= 32 ) then
- -- Assign fifth byte
- local left = rshift64(num, 40)
- stack[locNum] = left + value +
- band32(locNum, 0xFFFFFFFF)
- updnummid(stack, locNum, 40, )
- elseif ( locBit >= 24 ) then
- -- Assign fourth byte
- local left = rshift64(num, 32)
- stack[locNum] = left + value +
- band32(locNum, 0xFFFFFF)
- elseif ( locBit >= 16 ) then
- -- Assign third byte
- local left = rshift64(num, 24)
- stack[locNum] = left + value +
- band32(locNum, 0xFFFF)
- elseif ( locBit >= 8 ) then
- -- Assign second byte
- local left = rshift64(num, 24)
- stack[locNum] = left + value +
- band32(locNum, 0xFFFF)
- else
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement