From 6f57df02befa3b6837186a56dc1168ce2de0bd0a Mon Sep 17 00:00:00 2001 From: dmiller Date: Mon, 30 Nov 2020 23:22:07 +0000 Subject: [PATCH] Don't strip leading 0s in stdnse.tobinary Every place this function is used, the result is padded with 0s anyway, so may as well not strip them here. Didn't remove the padding code since this could return only 4 chars, and most padding is to 8-char width. --- nselib/formulas.lua | 10 ++++++---- nselib/stdnse.lua | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/nselib/formulas.lua b/nselib/formulas.lua index 916a05f1b..6f19000fa 100644 --- a/nselib/formulas.lua +++ b/nselib/formulas.lua @@ -70,14 +70,16 @@ local function chi2(data, num_cats) return x2 end +local function c_to_bin (c) + local n = stdnse.tobinary(c:byte()) + return ("0"):rep(8-#n)..n +end + -- Split a string into a sequence of bit strings of the given length. -- splitbits("abc", 5) --> {"01100", "00101", "10001", "00110"} -- Any short final group is omitted. local function splitbits(s, n) - local bits = s:gsub(".", function (c) - local n = stdnse.tobinary(c:byte()) - return ("0"):rep(8-#n)..n - end) + local bits = s:gsub(".", c_to_bin) local seq = {} for i = 1, #bits - n, n do diff --git a/nselib/stdnse.lua b/nselib/stdnse.lua index a800bee4f..c18013250 100644 --- a/nselib/stdnse.lua +++ b/nselib/stdnse.lua @@ -260,12 +260,12 @@ do }; --- Converts the given number, n, to a string in a binary number format (12 --- becomes "1100"). +-- becomes "1100"). Leading 0s not stripped. -- @param n Number to convert. -- @return String in binary format. function tobinary(n) assert(tonumber(n), "number expected"); - return (("%x"):format(n):gsub("%w", t):gsub("^0*", "")); + return (("%x"):format(n):gsub("%w", t)) end end