summaryrefslogtreecommitdiff
path: root/lcrypt/lcrypt_bits.c
blob: 50daa759beec4d5cdff349f4eb2c04754bf8ade1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#include <stdint.h>

static void copy_bits(uint8_t *out, unsigned int out_pos, const uint8_t *in, unsigned int in_pos, unsigned int length)
{
  unsigned int offset, chunk_size;
  uint8_t temp, mask;

  // skip untouched out bytes
  offset = out_pos / 8;
  out += offset;
  out_pos -= offset * 8;

  // skip untouched in bytes
  offset = in_pos / 8;
  in += offset;
  in_pos -= offset * 8;

  while(length > 0)
  {
    chunk_size = (in_pos < out_pos) ? 8 - out_pos : 8 - in_pos;
    if(chunk_size == 0) chunk_size = 8;
    if(chunk_size > length) chunk_size = length;

    temp = ((*in) << in_pos);
    temp >>= out_pos;
    mask = 0xff << (8 - chunk_size);
    mask = /*0xff ^*/ (mask >> out_pos);
    *out |= mask & temp;

    in_pos += chunk_size;
    if(in_pos >= 8)
    {
      in++;
      in_pos -= 8;
    }
    out_pos += chunk_size;
    if(out_pos >= 8)
    {
      out++;
      out_pos -= 8;
    }
    length -= chunk_size;
  }
}

static void reverse_data(uint8_t *a, int len)
{
  int i;
  uint8_t temp;
  for(i = len / 2 - 1; i >= 0; i--)
  {
    temp = a[i];
    a[i] = a[len-1-i];
    a[len-1-i] = temp;
  }
}

static uint64_t reverse_bits(uint64_t a, int bits)
{
  int i;
  uint64_t b = 0;
  for(i = 0; i < bits; i++)
  {
    b = (b << 1) | (a & 1);
    a >>= 1;
  }
  return b;
}

static int64_t sign_extend(uint64_t a, int bits)
{
  int64_t sret;;
  if((a & (1ULL << (bits - 1))) != 0)
  {
    a = ((uint64_t)0xffffffffffffffffLL << bits) | a;
    memcpy(&sret, &a, 8);
    return sret;
  }
  return a;
}

#define B_SKIP  0
#define B_STR   1
#define B_INT   2
#define B_LSB   4
#define B_LE    8
#define B_SIGN  16

#define BSKIP   B_SKIP
#define BSTR    B_STR
#define BMSB    B_INT
#define BLSB    (B_INT|B_LSB)
#define BLE     (B_INT|B_LE)
#define BSMSB   (B_INT|B_SIGN)
#define BSLSB   (B_INT|B_LSB|B_SIGN)
#define BSLE    (B_INT|B_LE|B_SIGN)

#if BYTE_ORDER == LITTLE_ENDIAN
  #define BMO   BLE
  #define BSMO  BSLE
#else
  #define BMO   BMSB
  #define BSMO  BSMSB
#endif

static int lcrypt_bget(lua_State *L)
{
  int count = 0, argc = lua_gettop(L);
  size_t length;
  const unsigned char *in = (const unsigned char*)luaL_checklstring(L, 1, &length);
  int i, type, bits, offset = luaL_checkint(L, 2);
  length *= 8;
  for(i = 3; i <= argc && offset < (int)length; i += 2)
  {
    type = luaL_checkint(L, i);
    if(i+1 > argc)
      bits = length;
    else
      bits = luaL_checkint(L, i+1);

    if(offset + bits > (int)length) bits = length - offset;
    if(type == B_SKIP)
    {
      count--;
    }
    else if(type == B_STR)
    {
      int len = (bits + 7) / 8;
      uint8_t * data = (uint8_t *)alloca(len);
      memset(data, 0, len);
      copy_bits(data, 0, in, offset, bits);
      lua_pushlstring(L, (char*)data, len);
    }
    else // integer
    {
      uint64_t ret = 0;
      copy_bits((uint8_t*)&ret, sizeof(ret) * 8 - bits, in, offset, bits);
      #if BYTE_ORDER == LITTLE_ENDIAN
        reverse_data((uint8_t*)&ret, sizeof(ret));
      #endif
      if((type & B_LSB) == B_LSB) ret = reverse_bits(ret, bits);
      #if BYTE_ORDER == LITTLE_ENDIAN
        if((type & B_LE) == B_LE) reverse_data((uint8_t*)&ret, (bits+7)/8);
      #else
        if((type & B_LE) == B_LE) reverse_data((uint8_t*)&ret + 8-(bits+7)/8, (bits+7)/8);
      #endif
      if((type & B_SIGN) == B_SIGN)
        lua_pushnumber(L, sign_extend(ret, bits));
      else
        lua_pushnumber(L, ret);
    }
    count++;
    offset += bits;
  }
  return count;
}

static int lcrypt_bput(lua_State *L)
{
  int argc = lua_gettop(L);
  int i, type, bits = 0, offset = 0, len = 0;
  size_t length;
  for(i = 1; i <= argc; i += 3)
  {
    type = luaL_checkint(L, i+1);
    if(type == B_STR && lua_isnil(L, i+2) == 1)
    {
      (void)luaL_checklstring(L, i, &length);
      bits = (lua_isnil(L, i+2) == 1) ? length * 8 : luaL_checkint(L, i+2);
      if(bits > (int)length * 8) bits = length * 8;
    }
    else
    {
      bits = luaL_checkint(L, i+2);
    }
    len += bits;
  }
  len = (len + 7) / 8;
  uint8_t * ret = (uint8_t *)alloca(len);
  memset(ret, 0, len);

  for(i = 1; i <= argc; i += 3)
  {
    type = luaL_checkint(L, i+1);
    if(type == B_STR)
    {
      const uint8_t *in = (const uint8_t *)luaL_checklstring(L, i, &length);
      bits = (lua_isnil(L, i+2) == 1) ? length * 8 : luaL_checkint(L, i+2);
      if(bits > (int)length * 8) bits = length * 8;
      copy_bits(ret, offset, in, 0, bits);
    }
    else if(type != B_SKIP) // integer
    {
      uint64_t in;
      bits = luaL_checkint(L, i+2);
      if((type & B_SIGN) == B_SIGN)
      {
        int64_t sin = luaL_checknumber(L, i);
        memcpy(&in, &sin, 8);
      }
      else
      {
        in = luaL_checknumber(L, i);
      }
      #if BYTE_ORDER == LITTLE_ENDIAN
        reverse_data((uint8_t*)&in, sizeof(in));
      #endif

      if((type & B_LSB) == B_LSB)
      {
        in = reverse_bits(in, 64);
        copy_bits(ret, offset, (uint8_t*)&in, 0, bits);
      }
      else if((type & B_LE) == B_LE)
      {
        reverse_data((uint8_t*)&in, sizeof(in));
        copy_bits(ret, offset, (uint8_t*)&in, 0, bits);
      }
      else
        copy_bits(ret, offset, (uint8_t*)&in, sizeof(in) * 8 - bits, bits);
    }
    offset += bits;
  }
  lua_pushlstring(L, (char*)ret, len);
  return 1;
}

static void lcrypt_start_bits(lua_State *L)
{
  ADD_FUNCTION(L, bget);   ADD_FUNCTION(L, bput);
  ADD_CONSTANT(L, BSKIP);  ADD_CONSTANT(L, BSTR);   ADD_CONSTANT(L, BMSB);   ADD_CONSTANT(L, BLSB);
  ADD_CONSTANT(L, BLE);    ADD_CONSTANT(L, BSMSB);  ADD_CONSTANT(L, BSLSB);  ADD_CONSTANT(L, BSLE);
  ADD_CONSTANT(L, BMO);    ADD_CONSTANT(L, BSMO);
}