Software /
code /
prosody
Comparison
net/websocket/frames.lua @ 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 |
parent | 6897:8972b1a96526 |
child | 6899:5f3da8b00b9b |
comparison
equal
deleted
inserted
replaced
6897:8972b1a96526 | 6898:d01254d5a825 |
---|---|
28 return l1*256 + l2; | 28 return l1*256 + l2; |
29 end | 29 end |
30 -- FIXME: this may lose precision | 30 -- FIXME: this may lose precision |
31 local function read_uint64be(str, pos) | 31 local function read_uint64be(str, pos) |
32 local l1, l2, l3, l4, l5, l6, l7, l8 = s_byte(str, pos, pos+7); | 32 local l1, l2, l3, l4, l5, l6, l7, l8 = s_byte(str, pos, pos+7); |
33 return lshift(l1, 56) + lshift(l2, 48) + lshift(l3, 40) + lshift(l4, 32) | 33 local h = lshift(l1, 24) + lshift(l2, 16) + lshift(l3, 8) + l4; |
34 + lshift(l5, 24) + lshift(l6, 16) + lshift(l7, 8) + l8; | 34 local l = lshift(l5, 24) + lshift(l6, 16) + lshift(l7, 8) + l8; |
35 return h * 2^32 + l; | |
35 end | 36 end |
36 local function pack_uint16be(x) | 37 local function pack_uint16be(x) |
37 return s_char(rshift(x, 8), band(x, 0xFF)); | 38 return s_char(rshift(x, 8), band(x, 0xFF)); |
38 end | 39 end |
39 local function get_byte(x, n) | 40 local function get_byte(x, n) |
40 return band(rshift(x, n), 0xFF); | 41 return band(rshift(x, n), 0xFF); |
41 end | 42 end |
42 local function pack_uint64be(x) | 43 local function pack_uint64be(x) |
43 return s_char(rshift(x, 56), get_byte(x, 48), get_byte(x, 40), get_byte(x, 32), | 44 local h = band(x / 2^32, 2^32-1); |
44 get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF)); | 45 return s_char(get_byte(h, 24), get_byte(h, 16), get_byte(h, 8), band(h, 0xFF)); |
46 get_byte(x, 24), get_byte(x, 16), get_byte(x, 8), band(x, 0xFF); | |
45 end | 47 end |
46 | 48 |
47 local function parse_frame_header(frame) | 49 local function parse_frame_header(frame) |
48 if #frame < 2 then return; end | 50 if #frame < 2 then return; end |
49 | 51 |