-- Copyright (C) 2015 Tomoyuki Fujimori -- -- This file is part of dromozoa-commons. -- -- dromozoa-commons is free software: you can redistribute it and/or modify -- it under the terms of the GNU General Public License as published by -- the Free Software Foundation, either version 3 of the License, or -- (at your option) any later version. -- -- dromozoa-commons is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with dromozoa-commons. If not, see . local lua_version_num = require "dromozoa.commons.lua_version_num" local function add(a, b, x, ...) local c = (a + b) % 0x100000000 if x == nil then return c else return add(c, x, ...) end end local function sub(a, b) return (a - b) % 0x100000000 end local function mul(a, b, x, ...) local a1 = a % 0x10000 local a2 = (a - a1) / 0x10000 local c1 = a1 * b local c2 = a2 * b % 0x10000 local c = (c1 + c2 * 0x10000) % 0x100000000 if x == nil then return c else return mul(c, x, ...) end end local function div(a, b) local c = a / b return c - c % 1 end local function mod(a, b) return a % b end local function byte(v, endian) local d = v % 0x100 local v = (v - d) / 0x100 local c = v % 0x100 local v = (v - c) / 0x100 local b = v % 0x100 local a = (v - b) / 0x100 if endian == ">" then return a, b, c, d else return d, c, b, a end end local function char(v, endian) return string.char(byte(v, endian)) end local function read(handle, n, endian) local a, b, c, d = handle:read(4):byte(1, 4) local v if endian == ">" then v = a * 0x1000000 + b * 0x10000 + c * 0x100 + d else v = d * 0x1000000 + c * 0x10000 + b * 0x100 + a end if n == nil or n == 1 then return v else return v, read(handle, n - 1, endian) end end if lua_version_num >= 503 then return assert(load([[ local function add(a, b, x, ...) local c = a + b & 0xffffffff if x == nil then return c else return add(c, x, ...) end end local function mul(a, b, x, ...) local c = a * b & 0xffffffff if x == nil then return c else return mul(c, x, ...) end end local function band(a, b, x, ...) local c = a & b if x == nil then return c else return band(c, x, ...) end end local function bor(a, b, x, ...) local c = a | b if x == nil then return c else return bor(c, x, ...) end end local function bxor(a, b, x, ...) local c = a ~ b if x == nil then return c else return bxor(c, x, ...) end end local function read(handle, n, endian) local v if endian == ">" then v = (">I4"):unpack(handle:read(4)) else v = ("> b end; bnot = function (v) return ~v & 0xffffffff end; rotl = function (a, b) return (a << b | a >> 32 - b) & 0xffffffff end; rotr = function (a, b) return (a >> b | a << 32 - b) & 0xffffffff end; byte = function (v, endian) if endian == ">" then local a, b, c, d = ("BBBB"):unpack((">I4"):pack(v)) return a, b, c, d else local a, b, c, d = ("BBBB"):unpack(("" then return (">I4"):pack(v) else return ("