3 * Copyright (c) 2007 Tim Kelly/Dialectronics
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to permit
10 * persons to whom the Software is furnished to do so, subject to the
11 * following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 * Copyright (c) 2007 Tim Kelly/Dialectronics
31 * Permission is hereby granted, free of charge, to any person obtaining
32 * a copy of this software and associated documentation files (the
33 * "Software"), to deal in the Software without restriction, including
34 * without limitation the rights to use, copy, modify, merge, publish,
35 * distribute, sublicense, and/or sell copies of the Software, and to permit
36 * persons to whom the Software is furnished to do so, subject to the
37 * following conditions:
39 * The above copyright notice and this permission notice shall be
40 * included in all copies or substantial portions of the Software.
42 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
43 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
47 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
48 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
53 module("luci.bits", package.seeall);
117 -- These functions are big-endian and take up to 32 bits
129 -- s -> hexadecimal string
135 for i in string.gfind(s, ".") do
138 ret = ret..hex2bin[i]
148 -- s -> binary string
160 -- need to prepend zeros to eliminate mod 4
162 s = string.rep("0", 4 - rem)..s
166 b = string.sub(s, i, i+3)
177 -- s -> binary string
180 local ex = string.len(s) - 1
185 b = string.sub(s, i, i)
192 return string.format("%u", num)
198 function Dec2Bin(s, num)
200 -- s -> Base10 string
201 -- num -> string length to extend to
211 s = string.format("%x", s)
215 while string.len(s) < n do
228 -- s -> hexadecimal string
240 -- s -> Base10 string
242 s = string.format("%x", s)
251 -- These functions are big-endian and will extend to 32 bits
262 -- v -> hex string to be masked
263 -- m -> hex string mask
265 -- s -> hex string as masked
267 -- bv -> binary string of v
268 -- bm -> binary string mask
270 local bv = Hex2Bin(v)
271 local bm = Hex2Bin(m)
276 while (string.len(bv) < 32) do
280 while (string.len(bm) < 32) do
286 cv = string.sub(bv, i, i)
287 cm = string.sub(bm, i, i)
305 function BMNAnd(v, m)
307 -- v -> hex string to be masked
308 -- m -> hex string mask
310 -- s -> hex string as masked
312 -- bv -> binary string of v
313 -- bm -> binary string mask
315 local bv = Hex2Bin(v)
316 local bm = Hex2Bin(m)
321 while (string.len(bv) < 32) do
325 while (string.len(bm) < 32) do
331 cv = string.sub(bv, i, i)
332 cm = string.sub(bm, i, i)
353 -- v -> hex string to be masked
354 -- m -> hex string mask
356 -- s -> hex string as masked
358 -- bv -> binary string of v
359 -- bm -> binary string mask
361 local bv = Hex2Bin(v)
362 local bm = Hex2Bin(m)
367 while (string.len(bv) < 32) do
371 while (string.len(bm) < 32) do
377 cv = string.sub(bv, i, i)
378 cm = string.sub(bm, i, i)
381 elseif cm == "1" then
394 -- v -> hex string to be masked
395 -- m -> hex string mask
397 -- s -> hex string as masked
399 -- bv -> binary string of v
400 -- bm -> binary string mask
402 local bv = Hex2Bin(v)
403 local bm = Hex2Bin(m)
408 while (string.len(bv) < 32) do
412 while (string.len(bm) < 32) do
418 cv = string.sub(bv, i, i)
419 cm = string.sub(bm, i, i)
426 elseif cm == "1" then
445 -- v -> hex string to be masked
446 -- m -> hex string mask
448 -- s -> hex string as masked
450 -- bv -> binary string of v
451 -- bm -> binary string mask
453 local bv = Hex2Bin(v)
454 local bm = Hex2Bin(m)
459 while (string.len(bv) < 32) do
463 while (string.len(bm) < 32) do
469 cv = string.sub(bv, i, i)
470 cm = string.sub(bm, i, i)
491 -- these functions shift right and left, adding zeros to lost or gained bits
492 -- returned values are 32 bits long
498 function BShRight(v, nb)
500 -- v -> hexstring value to be shifted
501 -- nb -> number of bits to shift to the right
503 -- s -> binary string of v
507 while (string.len(s) < 32) do
511 s = string.sub(s, 1, 32 - nb)
513 while (string.len(s) < 32) do
521 function BShLeft(v, nb)
523 -- v -> hexstring value to be shifted
524 -- nb -> number of bits to shift to the right
526 -- s -> binary string of v
530 while (string.len(s) < 32) do
534 s = string.sub(s, nb + 1, 32)
536 while (string.len(s) < 32) do