X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;f=libs%2Fcore%2Fluasrc%2Fip.lua;h=bba701ae24c3f1114f01c2021e9878535f6a9b2b;hb=1a4c192c65c166304014075857d7568cb4e7ae5b;hp=75311bfc74685a1cce492cfa38daf8d1bc7a9e97;hpb=a83de5a3b4e0fb23643030e6d0fd5938670be6bd;p=project%2Fluci.git diff --git a/libs/core/luasrc/ip.lua b/libs/core/luasrc/ip.lua index 75311bfc7..bba701ae2 100644 --- a/libs/core/luasrc/ip.lua +++ b/libs/core/luasrc/ip.lua @@ -414,7 +414,8 @@ function cidr.equal( self, addr ) end --- Return the prefix length of this CIDR instance. --- @return Prefix length in bit +-- @param mask Override instance prefix with given netmask (optional) +-- @return Prefix length in bit function cidr.prefix( self, mask ) local prefix = self[3] @@ -550,19 +551,17 @@ function cidr.add( self, amount, inplace ) local data = { unpack(self[2]) } local shorts = __array16( amount, self[1] ) - if shorts then - for pos = #data, 1, -1 do - local add = ( #shorts > 0 ) and table.remove( shorts, #shorts ) or 0 - if ( data[pos] + add ) > 0xFFFF then - data[pos] = ( data[pos] + add ) % 0xFFFF - if pos > 1 then - data[pos-1] = data[pos-1] + ( add - data[pos] ) - else - return nil - end + for pos = #data, 1, -1 do + local add = ( #shorts > 0 ) and table.remove( shorts, #shorts ) or 0 + if ( data[pos] + add ) > 0xFFFF then + data[pos] = ( data[pos] + add ) % 0xFFFF + if pos > 1 then + data[pos-1] = data[pos-1] + ( add - data[pos] ) else - data[pos] = data[pos] + add + return nil end + else + data[pos] = data[pos] + add end end @@ -583,19 +582,17 @@ function cidr.sub( self, amount, inplace ) local data = { unpack(self[2]) } local shorts = __array16( amount, self[1] ) - if shorts then - for pos = #data, 1, -1 do - local sub = ( #shorts > 0 ) and table.remove( shorts, #shorts ) or 0 - if ( data[pos] - sub ) < 0 then - data[pos] = ( sub - data[pos] ) % 0xFFFF - if pos > 1 then - data[pos-1] = data[pos-1] - ( sub + data[pos] ) - else - return nil - end + for pos = #data, 1, -1 do + local sub = ( #shorts > 0 ) and table.remove( shorts, #shorts ) or 0 + if ( data[pos] - sub ) < 0 then + data[pos] = ( sub - data[pos] ) % 0xFFFF + if pos > 1 then + data[pos-1] = data[pos-1] - ( sub + data[pos] ) else - data[pos] = data[pos] - sub + return nil end + else + data[pos] = data[pos] - sub end end