diff support/casc/bin.lua @ 65:8b8b0bade520

Fixed support for mounts using the new MountJournal and mount IDs (no conversion of old profiles at the moment).
author Jerome Vuarand <jerome.vuarand@gmail.com>
date Thu, 23 Oct 2014 13:44:59 +0100
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/support/casc/bin.lua	Thu Oct 23 13:44:59 2014 +0100
@@ -0,0 +1,55 @@
+local M, sbyte, schar, sgsub, sformat = {}, string.byte, string.char, string.gsub, string.format
+local inf, nan = math.huge, math.huge-math.huge
+
+local hexbin = {} for i=0,255 do
+	local h, b = sformat("%02x", i), schar(i)
+	hexbin[sformat("%02X", i)], hexbin[h], hexbin[b] = b, b, h
+end
+
+function M.uint32_le(s, pos)
+	local d, c, b, a = sbyte(s, (pos or 0)+1, (pos or 0) + 4)
+	return a*256^3 + b*256^2 + c*256 + d
+end
+function M.uint16_le(s, pos)
+	local b,a = sbyte(s, (pos or 0)+1, (pos or 0) + 2)
+	return a*256 + b
+end
+function M.uint32_be(s, pos)
+	local a,b,c,d = sbyte(s, (pos or 0)+1, (pos or 0) + 4)
+	return a*256^3 + b*256^2 + c*256 + d
+end
+function M.uint40_be(s, pos)
+	local a, b, c, d, e = sbyte(s, (pos or 0)+1, (pos or 0) + 5)
+	return a*256^4 + b*256^3 + c*256^2 + d*256 + e
+end
+function M.float32_le(s, pos)
+	local a, b, c, d = sbyte(s, (pos or 0) + 1, (pos or 0) + 4)
+	local s, e, f = d > 127 and -1 or 1, (d % 128)*2 + (c > 127 and 1 or 0), a + b*256 + (c % 128)*256^2
+	if e > 0 and e < 255 then
+		return s * (1+f/2^23) * 2^(e-127)
+	else
+		return e == 0 and (s * f/2^23 * 2^-126) or f == 0 and (s * inf) or nan
+	end
+end
+function M.int32_le(s, pos)
+	local d, c, b, a = sbyte(s, (pos or 0)+1, (pos or 0) + 4)
+	return a*256^3 + b*256^2 + c*256 + d - (a < 128 and 0 or 2^32)
+end
+
+function M.to_le32(n)
+	local n = n % 2^32
+	return schar(n % 256, (n / 256) % 256, (n / 256^2) % 256, (n / 256^3) % 256)
+end
+function M.to_be40(n)
+	local n = n % 2^40
+	return schar((n / 256^4) % 256, (n / 256^3) % 256, (n / 256^2) % 256, (n / 256) % 256, n % 256)
+end
+
+function M.to_bin(hs)
+	return hs and sgsub(hs, "%x%x", hexbin)
+end
+function M.to_hex(bs)
+	return bs and sgsub(bs, ".", hexbin)
+end
+
+return M
\ No newline at end of file