Browse Source

Modified to work on floating point firmware builds using changes by hazarkarabay. Fixes https://github.com/marcoskirsch/nodemcu-httpserver/issues/37

Marcos Kirsch 7 years ago
parent
commit
10bcd2f170
1 changed files with 16 additions and 6 deletions
  1. 16 6
      httpserver-b64decode.lua

+ 16 - 6
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
-