From 10bcd2f170714259984350ce9816a9a6760a53a5 Mon Sep 17 00:00:00 2001 From: Marcos Kirsch Date: Mon, 26 Dec 2016 14:38:12 -0600 Subject: [PATCH] Modified to work on floating point firmware builds using changes by hazarkarabay. Fixes https://github.com/marcoskirsch/nodemcu-httpserver/issues/37 --- httpserver-b64decode.lua | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/httpserver-b64decode.lua b/httpserver-b64decode.lua index e18bea6..a79581e 100755 --- a/httpserver-b64decode.lua +++ b/httpserver-b64decode.lua @@ -1,33 +1,44 @@ #!/usr/local/bin/lua -- httpserver-b64decode.lua -- Part of nodemcu-httpserver, contains b64 decoding used for HTTP Basic Authentication. +-- Modified to use an exponentiation by multiplication method for only applicable for unsigned integers. -- Based on http://lua-users.org/wiki/BaseSixtyFour by Alex Kloss -- compatible with lua 5.1 -- http://www.it-rfc.de -- Author: Marcos Kirsch +local function uipow(a, b) + local ret = 1 + if b >= 0 then + for i = 1, b do + ret = ret * a + end + end + return ret +end + -- bitshift functions (<<, >> equivalent) -- shift left local function lsh(value,shift) - return (value*(2^shift)) % 256 + return (value*(uipow(2, shift))) % 256 end -- shift right local function rsh(value,shift) -- Lua builds with no floating point don't define math. - if math then return math.floor(value/2^shift) % 256 end - return (value/2^shift) % 256 + if math then return math.floor(value/uipow(2, shift)) % 256 end + return (value/uipow(2, shift)) % 256 end -- return single bit (for OR) local function bit(x,b) - return (x % 2^b - x % 2^(b-1) > 0) + return (x % uipow(2, b) - x % uipow(2, (b-1)) > 0) end -- logic OR for number values local function lor(x,y) result = 0 - for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and 2^(p-1) or 0) end + for p=1,8 do result = result + (((bit(x,p) or bit(y,p)) == true) and uipow(2, (p-1)) or 0) end return result end @@ -62,4 +73,3 @@ return function(data) end return result end -