Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / epan / wslua / lua_bitop.c
blob7ade45cd343be82decf4a8acec7e15c71d728c67
1 /*
2 ** Lua BitOp -- a bit operations library for Lua 5.1/5.2.
3 ** http://bitop.luajit.org/
4 **
5 ** Copyright (C) 2008-2012 Mike Pall. All rights reserved.
6 **
7 ** SPDX-License-Identifier: MIT
8 **
9 */
11 #define LUA_BITOP_VERSION "1.0.2"
13 #define LUA_LIB
14 #include <lua.h>
15 #include <lauxlib.h>
17 #include "lua_bitop.h"
18 #include "ws_diag_control.h"
20 #include <stdint.h>
22 typedef int32_t SBits;
23 typedef uint32_t UBits;
25 typedef union {
26 lua_Number n;
27 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
28 uint64_t b;
29 #else
30 UBits b;
31 #endif
32 } BitNum;
34 /* Convert argument to bit type. */
35 static UBits barg(lua_State *L, int idx)
37 BitNum bn;
38 UBits b;
39 bn.n = luaL_checknumber(L, idx);
40 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
41 bn.n += 6755399441055744.0; /* 2^52+2^51 */
42 #ifdef SWAPPED_DOUBLE
43 b = (UBits)(bn.b >> 32);
44 #else
45 b = (UBits)bn.b;
46 #endif
47 #elif defined(LUA_NUMBER_INT) || defined(LUA_INT_INT) || \
48 defined(LUA_NUMBER_LONG) || defined(LUA_INT_LONG) || \
49 defined(LUA_NUMBER_LONGLONG) || defined(LUA_INT_LONGLONG) || \
50 defined(LUA_NUMBER_LONG_LONG) || defined(LUA_NUMBER_LLONG)
51 if (sizeof(UBits) == sizeof(lua_Number))
52 b = bn.b;
53 else
54 b = (UBits)(SBits)bn.n;
55 #elif defined(LUA_NUMBER_FLOAT) || defined(LUA_FLOAT_FLOAT)
56 #error "A 'float' lua_Number type is incompatible with this library"
57 #else
58 #error "Unknown number type, check LUA_NUMBER_*, LUA_FLOAT_*, LUA_INT_* in luaconf.h"
59 #endif
60 return b;
63 /* Return bit type. */
64 #if LUA_VERSION_NUM < 503
65 #define BRET(b) lua_pushnumber(L, (lua_Number)(SBits)(b)); return 1;
66 #else
67 #define BRET(b) lua_pushinteger(L, (lua_Integer)(SBits)(b)); return 1;
68 #endif
70 static int bit_tobit(lua_State *L) { BRET(barg(L, 1)) }
71 static int bit_bnot(lua_State *L) { BRET(~barg(L, 1)) }
73 #define BIT_OP(func, opr) \
74 static int func(lua_State *L) { int i; UBits b = barg(L, 1); \
75 for (i = lua_gettop(L); i > 1; i--) { b opr barg(L, i); } BRET(b) }
76 BIT_OP(bit_band, &=)
77 BIT_OP(bit_bor, |=)
78 BIT_OP(bit_bxor, ^=)
80 #define bshl(b, n) (b << n)
81 #define bshr(b, n) (b >> n)
82 #define bsar(b, n) ((SBits)b >> n)
83 #define brol(b, n) ((b << n) | (b >> (32-n)))
84 #define bror(b, n) ((b << (32-n)) | (b >> n))
85 #define BIT_SH(func, fn) \
86 static int func(lua_State *L) { \
87 UBits b = barg(L, 1); UBits n = barg(L, 2) & 31; BRET(fn(b, n)) }
88 BIT_SH(bit_lshift, bshl)
89 BIT_SH(bit_rshift, bshr)
90 BIT_SH(bit_arshift, bsar)
91 BIT_SH(bit_rol, brol)
92 BIT_SH(bit_ror, bror)
94 static int bit_bswap(lua_State *L)
96 UBits b = barg(L, 1);
97 b = (b >> 24) | ((b >> 8) & 0xff00) | ((b & 0xff00) << 8) | (b << 24);
98 BRET(b)
101 static int bit_tohex(lua_State *L)
103 UBits b = barg(L, 1);
104 SBits n = lua_isnone(L, 2) ? 8 : (SBits)barg(L, 2);
105 const char *hexdigits = "0123456789abcdef";
106 char buf[8];
107 int i;
108 if (n < 0) { n = -n; hexdigits = "0123456789ABCDEF"; }
109 if (n > 8) n = 8;
110 for (i = (int)n; --i >= 0; ) { buf[i] = hexdigits[b & 15]; b >>= 4; }
111 lua_pushlstring(L, buf, (size_t)n);
112 return 1;
115 static const struct luaL_Reg bit_funcs[] = {
116 { "tobit", bit_tobit },
117 { "bnot", bit_bnot },
118 { "band", bit_band },
119 { "bor", bit_bor },
120 { "bxor", bit_bxor },
121 { "lshift", bit_lshift },
122 { "rshift", bit_rshift },
123 { "arshift", bit_arshift },
124 { "rol", bit_rol },
125 { "ror", bit_ror },
126 { "bswap", bit_bswap },
127 { "tohex", bit_tohex },
128 { NULL, NULL }
131 /* Signed right-shifts are implementation-defined per C89/C99.
132 ** But the de facto standard are arithmetic right-shifts on two's
133 ** complement CPUs. This behaviour is required here, so test for it.
135 #define BAD_SAR (bsar(-8, 2) != (SBits)-2)
137 LUALIB_API int luaopen_bit(lua_State *L)
139 UBits b;
140 #if LUA_VERSION_NUM < 503
141 lua_pushnumber(L, (lua_Number)1437217655L);
142 #else
143 lua_pushinteger(L, (lua_Integer)1437217655L);
144 #endif
145 b = barg(L, -1);
146 if (b != (UBits)1437217655L || BAD_SAR) { /* Perform a simple self-test. */
147 const char *msg = "compiled with incompatible luaconf.h";
148 #if defined(LUA_NUMBER_DOUBLE) || defined(LUA_FLOAT_DOUBLE)
149 #ifdef _WIN32
150 if (b == (UBits)1610612736L)
151 msg = "use D3DCREATE_FPU_PRESERVE with DirectX";
152 #endif
153 if (b == (UBits)1127743488L)
154 msg = "not compiled with SWAPPED_DOUBLE";
155 #endif
156 DIAG_OFF(unreachable-code)
157 if (BAD_SAR)
158 msg = "arithmetic right-shift broken";
159 DIAG_ON(unreachable-code)
160 luaL_error(L, "bit library self-test failed (%s)", msg);
163 luaL_newlib(L, bit_funcs);
164 return 1;