From 2311ac23ae1b9f0fdc90cd1c293f15574a4908be Mon Sep 17 00:00:00 2001 From: Luca Date: Thu, 5 Sep 2024 21:31:52 +0200 Subject: [PATCH] feat: varint encoding --- mqtt.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/mqtt.lua b/mqtt.lua index 1f5277c..e7c7a61 100644 --- a/mqtt.lua +++ b/mqtt.lua @@ -42,6 +42,20 @@ local readVarint = function (conn, first_byte) return n + (b << s), nil end +local encodeVarint = function (n) + if n > 268435455 then + return nil, "number too large" + end + + local data = "" + while n > 127 do + data = data .. string.char(0x80 | n & 0x7F) + n = n >> 7 + end + + return data .. string.char(n), nil +end + local MqttClient = {} function mqtt.open (address, port)