Changeset

6898:d01254d5a825

net.websocket.frames: Pack and unpack 64bit ints without overflows (lua-bitop/bit32 are 32bit)
author Kim Alvefur <zash@zash.se>
date Tue, 06 Oct 2015 18:03:04 +0200
parents 6897:8972b1a96526
children 6899:5f3da8b00b9b
files net/websocket/frames.lua
diffstat 1 files changed, 6 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/net/websocket/frames.lua	Tue Oct 06 16:49:01 2015 +0200
+++ b/net/websocket/frames.lua	Tue Oct 06 18:03:04 2015 +0200
@@ -30,8 +30,9 @@
 -- FIXME: this may lose precision
 local function read_uint64be(str, pos)
 	local l1, l2, l3, l4, l5, l6, l7, l8 = s_byte(str, pos, pos+7);
-	return lshift(l1, 56) + lshift(l2, 48) + lshift(l3, 40) + lshift(l4, 32)
-		+ lshift(l5, 24) + lshift(l6, 16) + lshift(l7, 8) + l8;
+	local h = lshift(l1, 24) + lshift(l2, 16) + lshift(l3, 8) + l4;
+	local l = lshift(l5, 24) + lshift(l6, 16) + lshift(l7, 8) + l8;
+	return h * 2^32 + l;
 end
 local function pack_uint16be(x)
 	return s_char(rshift(x, 8), band(x, 0xFF));
@@ -40,8 +41,9 @@
 	return band(rshift(x, n), 0xFF);
 end
 local function pack_uint64be(x)
-	return s_char(rshift(x, 56), get_byte(x, 48), get_byte(x, 40), get_byte(x, 32),
-		get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF));
+	local h = band(x / 2^32, 2^32-1);
+	return s_char(get_byte(h, 24), get_byte(h, 16), get_byte(h, 8), band(h, 0xFF));
+		get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF);
 end
 
 local function parse_frame_header(frame)