lua/lbitlib.c
author Ryan C. Gordon <icculus@icculus.org>
Tue, 17 Dec 2013 22:16:27 -0500
changeset 0 d7ee4e2ed49d
permissions -rw-r--r--
Initial work.

Goes far enough to decrypt and correctly verify the master key.
icculus@0
     1
/*
icculus@0
     2
** $Id: lbitlib.c,v 1.18.1.2 2013/07/09 18:01:41 roberto Exp $
icculus@0
     3
** Standard library for bitwise operations
icculus@0
     4
** See Copyright Notice in lua.h
icculus@0
     5
*/
icculus@0
     6
icculus@0
     7
#define lbitlib_c
icculus@0
     8
#define LUA_LIB
icculus@0
     9
icculus@0
    10
#include "lua.h"
icculus@0
    11
icculus@0
    12
#include "lauxlib.h"
icculus@0
    13
#include "lualib.h"
icculus@0
    14
icculus@0
    15
icculus@0
    16
/* number of bits to consider in a number */
icculus@0
    17
#if !defined(LUA_NBITS)
icculus@0
    18
#define LUA_NBITS	32
icculus@0
    19
#endif
icculus@0
    20
icculus@0
    21
icculus@0
    22
#define ALLONES		(~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
icculus@0
    23
icculus@0
    24
/* macro to trim extra bits */
icculus@0
    25
#define trim(x)		((x) & ALLONES)
icculus@0
    26
icculus@0
    27
icculus@0
    28
/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
icculus@0
    29
#define mask(n)		(~((ALLONES << 1) << ((n) - 1)))
icculus@0
    30
icculus@0
    31
icculus@0
    32
typedef lua_Unsigned b_uint;
icculus@0
    33
icculus@0
    34
icculus@0
    35
icculus@0
    36
static b_uint andaux (lua_State *L) {
icculus@0
    37
  int i, n = lua_gettop(L);
icculus@0
    38
  b_uint r = ~(b_uint)0;
icculus@0
    39
  for (i = 1; i <= n; i++)
icculus@0
    40
    r &= luaL_checkunsigned(L, i);
icculus@0
    41
  return trim(r);
icculus@0
    42
}
icculus@0
    43
icculus@0
    44
icculus@0
    45
static int b_and (lua_State *L) {
icculus@0
    46
  b_uint r = andaux(L);
icculus@0
    47
  lua_pushunsigned(L, r);
icculus@0
    48
  return 1;
icculus@0
    49
}
icculus@0
    50
icculus@0
    51
icculus@0
    52
static int b_test (lua_State *L) {
icculus@0
    53
  b_uint r = andaux(L);
icculus@0
    54
  lua_pushboolean(L, r != 0);
icculus@0
    55
  return 1;
icculus@0
    56
}
icculus@0
    57
icculus@0
    58
icculus@0
    59
static int b_or (lua_State *L) {
icculus@0
    60
  int i, n = lua_gettop(L);
icculus@0
    61
  b_uint r = 0;
icculus@0
    62
  for (i = 1; i <= n; i++)
icculus@0
    63
    r |= luaL_checkunsigned(L, i);
icculus@0
    64
  lua_pushunsigned(L, trim(r));
icculus@0
    65
  return 1;
icculus@0
    66
}
icculus@0
    67
icculus@0
    68
icculus@0
    69
static int b_xor (lua_State *L) {
icculus@0
    70
  int i, n = lua_gettop(L);
icculus@0
    71
  b_uint r = 0;
icculus@0
    72
  for (i = 1; i <= n; i++)
icculus@0
    73
    r ^= luaL_checkunsigned(L, i);
icculus@0
    74
  lua_pushunsigned(L, trim(r));
icculus@0
    75
  return 1;
icculus@0
    76
}
icculus@0
    77
icculus@0
    78
icculus@0
    79
static int b_not (lua_State *L) {
icculus@0
    80
  b_uint r = ~luaL_checkunsigned(L, 1);
icculus@0
    81
  lua_pushunsigned(L, trim(r));
icculus@0
    82
  return 1;
icculus@0
    83
}
icculus@0
    84
icculus@0
    85
icculus@0
    86
static int b_shift (lua_State *L, b_uint r, int i) {
icculus@0
    87
  if (i < 0) {  /* shift right? */
icculus@0
    88
    i = -i;
icculus@0
    89
    r = trim(r);
icculus@0
    90
    if (i >= LUA_NBITS) r = 0;
icculus@0
    91
    else r >>= i;
icculus@0
    92
  }
icculus@0
    93
  else {  /* shift left */
icculus@0
    94
    if (i >= LUA_NBITS) r = 0;
icculus@0
    95
    else r <<= i;
icculus@0
    96
    r = trim(r);
icculus@0
    97
  }
icculus@0
    98
  lua_pushunsigned(L, r);
icculus@0
    99
  return 1;
icculus@0
   100
}
icculus@0
   101
icculus@0
   102
icculus@0
   103
static int b_lshift (lua_State *L) {
icculus@0
   104
  return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
icculus@0
   105
}
icculus@0
   106
icculus@0
   107
icculus@0
   108
static int b_rshift (lua_State *L) {
icculus@0
   109
  return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
icculus@0
   110
}
icculus@0
   111
icculus@0
   112
icculus@0
   113
static int b_arshift (lua_State *L) {
icculus@0
   114
  b_uint r = luaL_checkunsigned(L, 1);
icculus@0
   115
  int i = luaL_checkint(L, 2);
icculus@0
   116
  if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
icculus@0
   117
    return b_shift(L, r, -i);
icculus@0
   118
  else {  /* arithmetic shift for 'negative' number */
icculus@0
   119
    if (i >= LUA_NBITS) r = ALLONES;
icculus@0
   120
    else
icculus@0
   121
      r = trim((r >> i) | ~(~(b_uint)0 >> i));  /* add signal bit */
icculus@0
   122
    lua_pushunsigned(L, r);
icculus@0
   123
    return 1;
icculus@0
   124
  }
icculus@0
   125
}
icculus@0
   126
icculus@0
   127
icculus@0
   128
static int b_rot (lua_State *L, int i) {
icculus@0
   129
  b_uint r = luaL_checkunsigned(L, 1);
icculus@0
   130
  i &= (LUA_NBITS - 1);  /* i = i % NBITS */
icculus@0
   131
  r = trim(r);
icculus@0
   132
  if (i != 0)  /* avoid undefined shift of LUA_NBITS when i == 0 */
icculus@0
   133
    r = (r << i) | (r >> (LUA_NBITS - i));
icculus@0
   134
  lua_pushunsigned(L, trim(r));
icculus@0
   135
  return 1;
icculus@0
   136
}
icculus@0
   137
icculus@0
   138
icculus@0
   139
static int b_lrot (lua_State *L) {
icculus@0
   140
  return b_rot(L, luaL_checkint(L, 2));
icculus@0
   141
}
icculus@0
   142
icculus@0
   143
icculus@0
   144
static int b_rrot (lua_State *L) {
icculus@0
   145
  return b_rot(L, -luaL_checkint(L, 2));
icculus@0
   146
}
icculus@0
   147
icculus@0
   148
icculus@0
   149
/*
icculus@0
   150
** get field and width arguments for field-manipulation functions,
icculus@0
   151
** checking whether they are valid.
icculus@0
   152
** ('luaL_error' called without 'return' to avoid later warnings about
icculus@0
   153
** 'width' being used uninitialized.)
icculus@0
   154
*/
icculus@0
   155
static int fieldargs (lua_State *L, int farg, int *width) {
icculus@0
   156
  int f = luaL_checkint(L, farg);
icculus@0
   157
  int w = luaL_optint(L, farg + 1, 1);
icculus@0
   158
  luaL_argcheck(L, 0 <= f, farg, "field cannot be negative");
icculus@0
   159
  luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
icculus@0
   160
  if (f + w > LUA_NBITS)
icculus@0
   161
    luaL_error(L, "trying to access non-existent bits");
icculus@0
   162
  *width = w;
icculus@0
   163
  return f;
icculus@0
   164
}
icculus@0
   165
icculus@0
   166
icculus@0
   167
static int b_extract (lua_State *L) {
icculus@0
   168
  int w;
icculus@0
   169
  b_uint r = luaL_checkunsigned(L, 1);
icculus@0
   170
  int f = fieldargs(L, 2, &w);
icculus@0
   171
  r = (r >> f) & mask(w);
icculus@0
   172
  lua_pushunsigned(L, r);
icculus@0
   173
  return 1;
icculus@0
   174
}
icculus@0
   175
icculus@0
   176
icculus@0
   177
static int b_replace (lua_State *L) {
icculus@0
   178
  int w;
icculus@0
   179
  b_uint r = luaL_checkunsigned(L, 1);
icculus@0
   180
  b_uint v = luaL_checkunsigned(L, 2);
icculus@0
   181
  int f = fieldargs(L, 3, &w);
icculus@0
   182
  int m = mask(w);
icculus@0
   183
  v &= m;  /* erase bits outside given width */
icculus@0
   184
  r = (r & ~(m << f)) | (v << f);
icculus@0
   185
  lua_pushunsigned(L, r);
icculus@0
   186
  return 1;
icculus@0
   187
}
icculus@0
   188
icculus@0
   189
icculus@0
   190
static const luaL_Reg bitlib[] = {
icculus@0
   191
  {"arshift", b_arshift},
icculus@0
   192
  {"band", b_and},
icculus@0
   193
  {"bnot", b_not},
icculus@0
   194
  {"bor", b_or},
icculus@0
   195
  {"bxor", b_xor},
icculus@0
   196
  {"btest", b_test},
icculus@0
   197
  {"extract", b_extract},
icculus@0
   198
  {"lrotate", b_lrot},
icculus@0
   199
  {"lshift", b_lshift},
icculus@0
   200
  {"replace", b_replace},
icculus@0
   201
  {"rrotate", b_rrot},
icculus@0
   202
  {"rshift", b_rshift},
icculus@0
   203
  {NULL, NULL}
icculus@0
   204
};
icculus@0
   205
icculus@0
   206
icculus@0
   207
icculus@0
   208
LUAMOD_API int luaopen_bit32 (lua_State *L) {
icculus@0
   209
  luaL_newlib(L, bitlib);
icculus@0
   210
  return 1;
icculus@0
   211
}
icculus@0
   212