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.
25 Modifications and documentation for LuCI made by
26 Steven Barth <steven@midlink.org> and
27 Jo-Philipp Wich <xm@leipzig.freifunk.net>
31 --- LuCI number conversation and bit manipulation functions.
32 module("luci.bits", package.seeall);
72 --- Convert hexadecimal to binary number.
73 -- This function is big endian and can take up to 32 bits.
74 -- @param s String containing hex value
75 -- @return String containing binary value
82 for i in string.gfind(s, ".") do
92 --- Convert binary to hexadecimal number.
93 -- This function is big endian and can take up to 32 bits.
94 -- @param s String containing binary value
95 -- @return String containing hex value
108 -- need to prepend zeros to eliminate mod 4
110 s = string.rep("0", 4 - rem)..s
114 b = string.sub(s, i, i+3)
122 --- Convert binary to decimal number.
123 -- This function is big endian and can take up to 32 bits.
124 -- @param s String containing binary value
125 -- @return String containing decimal value
129 local ex = string.len(s) - 1
134 b = string.sub(s, i, i)
141 return string.format("%u", num)
145 --- Convert decimal to binary number.
146 -- This function is big endian and can take up to 32 bits.
147 -- @param s String or number containing decimal value
148 -- @param num Pad binary number to num bits
149 -- @return String containing binary value
150 function Dec2Bin(s, num)
160 s = string.format("%x", s)
164 while string.len(s) < n do
172 --- Convert hexadecimal to decimal number.
173 -- This function is big endian and can take up to 32 bits.
174 -- @param s String containing hex value
175 -- @return String containing decimal value
184 --- Convert decimal to hexadecimal number.
185 -- This function is big endian and can take up to 32 bits.
186 -- @param s String containing decimal value
187 -- @return String containing hex value
190 s = string.format("%x", s)
197 --- Apply bitmask to value using bitwise And.
198 -- This function is big endian and will extend the values to 32 bits.
199 -- @param v String containing hex value to be masked
200 -- @param m String containing hex value of mask
201 -- @return String containing hex value of masked value
204 local bv = Hex2Bin(v)
205 local bm = Hex2Bin(m)
210 while (string.len(bv) < 32) do
214 while (string.len(bm) < 32) do
220 cv = string.sub(bv, i, i)
221 cm = string.sub(bm, i, i)
238 --- Apply bitmask to value using bitwise Nand.
239 -- This function is big endian and will extend the values to 32 bits.
240 -- @param v String containing hex value to be masked
241 -- @param m String containing hex value of mask
242 -- @return String containing hex value of masked value
243 function BMNAnd(v, m)
245 local bv = Hex2Bin(v)
246 local bm = Hex2Bin(m)
251 while (string.len(bv) < 32) do
255 while (string.len(bm) < 32) do
261 cv = string.sub(bv, i, i)
262 cm = string.sub(bm, i, i)
279 --- Apply bitmask to value using bitwise Or.
280 -- This function is big endian and will extend the values to 32 bits.
281 -- @param v String containing hex value to be masked
282 -- @param m String containing hex value of mask
283 -- @return String containing hex value of masked value
286 local bv = Hex2Bin(v)
287 local bm = Hex2Bin(m)
292 while (string.len(bv) < 32) do
296 while (string.len(bm) < 32) do
302 cv = string.sub(bv, i, i)
303 cm = string.sub(bm, i, i)
306 elseif cm == "1" then
317 --- Apply bitmask to value using bitwise Xor.
318 -- This function is big endian and will extend the values to 32 bits.
319 -- @param v String containing hex value to be masked
320 -- @param m String containing hex value of mask
321 -- @return String containing hex value of masked value
324 local bv = Hex2Bin(v)
325 local bm = Hex2Bin(m)
330 while (string.len(bv) < 32) do
334 while (string.len(bm) < 32) do
340 cv = string.sub(bv, i, i)
341 cm = string.sub(bm, i, i)
348 elseif cm == "1" then
364 --- Apply bitmask to value using bitwise Not.
365 -- This function is big endian and will extend the values to 32 bits.
366 -- @param v String containing hex value to be masked
367 -- @param m String containing hex value of mask
368 -- @return String containing hex value of masked value
371 local bv = Hex2Bin(v)
372 local bm = Hex2Bin(m)
377 while (string.len(bv) < 32) do
381 while (string.len(bm) < 32) do
387 cv = string.sub(bv, i, i)
388 cm = string.sub(bm, i, i)
409 --- Perform righthand bit shifting on value.
410 -- This function pads the shifted value with zeroes and will extend to 32 bits.
411 -- @param v String containing hex value to be shifted
412 -- @param nb Number of bits to shift right
413 -- @return String containing hex value of shifted value
414 function BShRight(v, nb)
418 while (string.len(s) < 32) do
422 s = string.sub(s, 1, 32 - nb)
424 while (string.len(s) < 32) do
432 --- Perform lefthand bit shifting on value.
433 -- This function pads the shifted value with zeroes and extend to 32 bits.
434 -- @param v String containing hex value to be shifted
435 -- @param nb Number of bits to shift left
436 -- @return String containing hex value of shifted value
437 function BShLeft(v, nb)
441 while (string.len(s) < 32) do
445 s = string.sub(s, nb + 1, 32)
447 while (string.len(s) < 32) do