File: lua_bytes.c

package info (click to toggle)
cardpeek 0.8.3-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,184 kB
  • ctags: 1,275
  • sloc: ansic: 10,467; sh: 4,241; makefile: 32; xml: 19
file content (598 lines) | stat: -rw-r--r-- 13,649 bytes parent folder | download | duplicates (2)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/********************************************************************** 
*
* This file is part of Cardpeek, the smart card reader utility.
*
* Copyright 2009-2013 by Alain Pannetrat <L1L1@gmx.com>
*
* Cardpeek is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Cardpeek is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Cardpeek.  If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <lauxlib.h>
#include "lua_bytes.h"
#include "bytestring.h"
#include "misc.h"
#include <string.h>
#include <stdlib.h>

/*** LUA_BYTES: lua encapsulation of bytestring.c ***/

bytestring_t* luaL_check_bytestring(lua_State *L, int p)
{
  void *ud = luaL_checkudata(L,p,"bytes.type");
  luaL_argcheck(L, ud != NULL, p, "`bytes' expected");
  return *(bytestring_t **)ud;
}

int luaL_is_bytestring(lua_State *L, int idx)
{
	int retval;
	void *p;
	
	if (idx<0) idx=lua_gettop(L)+idx+1;

	if ((p = lua_touserdata(L, idx)) == NULL)
	{	
		return 0;
	}

	luaL_getmetatable(L, "bytes.type");  /* pushes one on stack */

	if (!lua_getmetatable(L, idx))
	{
		lua_pop(L,1);
		return 0;
	}

	if (!lua_rawequal(L, -1, -2))
		retval = 0;
	else
		retval = 1;

	lua_pop(L, 2);  
 
	return retval;
}

void lua_push_bytestring(lua_State *L, bytestring_t* bs)
{
  bytestring_t **dst;

  if (bs==NULL)
    lua_pushnil(L);
  else
  {
    dst = (bytestring_t **)lua_newuserdata(L, sizeof(bytestring_t *)); /* -2 */
    luaL_getmetatable(L, "bytes.type"); /* -1 */
    lua_setmetatable(L, -2);
    *dst = bs;
  }
}

static bytestring_t* x_bytes_create(lua_State *L, unsigned width, int start, int end)
{
  bytestring_t *bs=bytestring_new(width);
  int p;
  bytestring_t *tmp;

  for (p=start;p<=end;p++)
  {
    switch (lua_type(L,p)) {
      case LUA_TSTRING:
	tmp = bytestring_new(width);
	bytestring_assign_digit_string(tmp,lua_tostring(L,p));
	bytestring_append(bs,tmp);
	bytestring_free(tmp);
	break;
      case LUA_TNUMBER:
	bytestring_pushback(bs,lua_tointeger(L,p));
	break;
      case LUA_TUSERDATA:
	tmp = luaL_check_bytestring(L,p);
	bytestring_append(bs,tmp);
	break;
      default:
	luaL_error(L,"wrong type for parameter %d (%s)", p, lua_typename(L,lua_type(L,p)));
	return NULL;
    }
  }
  return bs;
}

/*******************************************************
 *
 * CONSTRUCTORS
 */

static int subr_bytes_new(lua_State *L) 
{
  int width = luaL_checkint(L, 1);
  int n = lua_gettop(L);
  bytestring_t *bs;

  bs=x_bytes_create(L,width,2,n);
  lua_push_bytestring(L,bs);
  return 1;
}

static int subr_bytes_new_from_chars(lua_State *L)
{
  const char *str = luaL_checkstring(L, 1);
  bytestring_t *bs = bytestring_new(8);
  
  bytestring_assign_data(bs,strlen(str),(const unsigned char *)str);

  lua_push_bytestring(L,bs);
  return 1;
}

static int subr_bytes_concat(lua_State *L)
{
  int i;
  bytestring_t *bs;
  int width = 8;

  /* find the first bytestring and get its width */
  for (i=1;i<=lua_gettop(L);i++)
  {
    if (lua_isuserdata(L,i))
    {
      bs = luaL_check_bytestring(L, i);
      width = bs->width;
      break;
    }
  }

  /* create the result and push it on the stack */
  bs = x_bytes_create(L,width,1,lua_gettop(L)); 
  lua_push_bytestring(L,bs);
  return 1;
}

/*******************************************************
 *
 * DESTRUCTOR
 */

static int subr_bytes_gc(lua_State *L)
{
  bytestring_t **bs=(bytestring_t **)lua_touserdata(L,1);
	
  if (*bs)
    bytestring_free(*bs);
  return 0;
}

/*******************************************************
 *
 * META-METHODS
 */ 

static int subr_bytes_tostring(lua_State *L) 
{
  bytestring_t *bs = luaL_check_bytestring(L, 1);
  char *s=bytestring_to_format("%D",bs);
  lua_pushstring(L,s);
  free(s);
  return 1;
}

static int subr_bytes_len(lua_State *L)
{
  bytestring_t *bs = luaL_check_bytestring(L, 1);
  lua_pushnumber(L,bytestring_get_size(bs));
  return 1;
}


static int subr_bytes_eq(lua_State *L)
{
  bytestring_t *a = luaL_check_bytestring(L, 1);
  bytestring_t *b = luaL_check_bytestring(L, 2);

  lua_pushboolean(L,bytestring_is_equal(a,b));
  return 1;
}

/******************************************************
 *
 * ITERATOR
 */

typedef struct {
	bytestring_t* bs;
	unsigned      index;
} bytes_iter_data_t;

static int iter_bytes(lua_State *L)
{
	unsigned char c;

	bytes_iter_data_t *iter = (bytes_iter_data_t *)lua_touserdata(L,lua_upvalueindex(1));

	if (iter->index<bytestring_get_size(iter->bs))
	{
		lua_pushnumber(L,iter->index);
		bytestring_get_element(&c,iter->bs,iter->index++);
		lua_pushnumber(L,c);
		return 2;
	}
	return 0;
}

static int subr_bytes_ipairs(lua_State *L)
{
	bytestring_t *bs = luaL_check_bytestring(L, 1);
	bytes_iter_data_t *iter = (bytes_iter_data_t *)lua_newuserdata(L,sizeof(bytestring_t));

	iter->bs = bs;
	iter->index = 0;

	lua_pushcclosure(L, iter_bytes, 1);
        return 1;
}

/********************************************************
 * 
 * ORDINARY METHODS
 */

static int subr_bytes_get(lua_State *L)
{
  bytestring_t *bs = luaL_check_bytestring(L, 1);
  int b_index;
  int e_index;
  int i;
  unsigned char c;

  if (lua_isnoneornil(L,2))
	b_index = 0;
  else
  {
	b_index=luaL_checkint(L,2);
	if (b_index<0) b_index = 0;
  }

  if (lua_isnoneornil(L,3))
	e_index = b_index;
  else
  {
	e_index=luaL_checkint(L,2);
	if (e_index>=(int)bytestring_get_size(bs)) e_index = bytestring_get_size(bs)-1;
  }

  if (e_index<b_index)
  {
	lua_pushnil(L);
        return 1;
  }

  for (i=b_index;i<=e_index;i++)
  {
	bytestring_get_element(&c,bs,i);
	lua_pushnumber(L,c);
  }
  return e_index-b_index+1;
}

static int subr_bytes_set(lua_State *L)
{
  bytestring_t *bs = luaL_check_bytestring(L, 1);
  unsigned b_index;
  unsigned e_index;
  unsigned i;
  unsigned char c;

  b_index=(unsigned)luaL_checkint(L,2);

  e_index = b_index + lua_gettop(L)-3;

  for (i=b_index;i<=e_index;i++)
  {
	  c = (unsigned char)luaL_checkint(L,i-b_index+3);
	  if (i<bytestring_get_size(bs))
	  {
		  bytestring_set_element(bs,i,c);
	  }
	  else if (i==bytestring_get_size(bs))
	  {
		  bytestring_pushback(bs,c);
	  }
      else
      {
          char error_msg[100];
          sprintf(error_msg,"Index %i is out of bounds in 'bytes': acceptable range is 0 to %i.", i, bytestring_get_size(bs));
          return luaL_error(L,error_msg);
      }
  }
  lua_settop(L,1);
  return 1;
}



static int subr_bytes_pad_left(lua_State *L)
{
  bytestring_t *bs = bytestring_duplicate(luaL_check_bytestring(L, 1));
  unsigned pad_length = luaL_checkint(L,2);
  unsigned pad_value =  luaL_checkint(L,3);
  if (bytestring_pad_left(bs,pad_length,pad_value)!=BYTESTRING_OK)
  {
	bytestring_free(bs);
	lua_pushnil(L);
	return 1;
  }
  lua_push_bytestring(L,bs);
  return 1;
}  


static int subr_bytes_pad_right(lua_State *L)
{
  bytestring_t *bs = bytestring_duplicate(luaL_check_bytestring(L, 1));
  unsigned pad_length = luaL_checkint(L,2);
  unsigned pad_value =  luaL_checkint(L,3);
  if (bytestring_pad_right(bs,pad_length,pad_value)!=BYTESTRING_OK)
  {
	bytestring_free(bs);
	lua_pushnil(L);
	return 1;
  }
  lua_push_bytestring(L,bs);
  return 1;
}  

static int subr_bytes_reverse(lua_State *L)
{
  bytestring_t *bs = bytestring_duplicate(luaL_check_bytestring(L, 1));
  if (bytestring_invert(bs)!=BYTESTRING_OK)
  {
	bytestring_free(bs);
	lua_pushnil(L);
	return 1;
  }
  lua_push_bytestring(L,bs);
  return 1;
}  

static int subr_bytes_clone(lua_State *L)
{
  bytestring_t *bs = luaL_check_bytestring(L, 1);
  bytestring_t *res = bytestring_duplicate(bs);
  lua_push_bytestring(L,res);
  return 1;
}

static int subr_bytes_sub(lua_State *L)
{
  bytestring_t *bs = luaL_check_bytestring(L, 1);
  int start=luaL_checkint(L,2);
  int end=-1;
  bytestring_t *ret;

  if (lua_gettop(L)>2)
    end=luaL_checkint(L,3);
  if (start<0)
    start=bytestring_get_size(bs)+start;
  if (end<0)
    end=bytestring_get_size(bs)+end;

  ret = bytestring_new(bs->width);

  if (start>end || bytestring_substr(ret,start,end-start+1,bs)!=BYTESTRING_OK)
  {
    bytestring_free(ret);
    lua_pushnil(L);
    return 1;
  }
  
  lua_push_bytestring(L,ret);
  return 1;
}

static int subr_bytes_width(lua_State *L)
{
  bytestring_t *bs = luaL_check_bytestring(L, 1);
  lua_pushnumber(L,bs->width);
  return 1;
}

static int subr_bytes_is_printable(lua_State *L)
{
  bytestring_t *bs = luaL_check_bytestring(L, 1);

  lua_pushboolean(L,bytestring_is_printable(bs)==BYTESTRING_OK);
  return 1;
}

static int subr_bytes_convert(lua_State *L)
{
  static int warn_obsolete=0; 
  bytestring_t *bs;
  int width;
  bytestring_t *ret; 

  if (lua_type(L,2)==LUA_TUSERDATA && lua_type(L,1)==LUA_TNUMBER)
  {
      if (!warn_obsolete)
      {
          warn_obsolete = 1;
          log_printf(LOG_WARNING,"The calling convention of bytes.convert() has changed in cardpeek 0.8, please update your scripts\n"
                                 "\tchange bytes.convert(A,B) to bytes.convert(B,A)\n"
                                 "\tThis warning will only appear once.");
      }
      width=luaL_checkint(L,1);
      bs = luaL_check_bytestring(L, 2);
  } 
  else
  {
      bs = luaL_check_bytestring(L, 1);
      width=luaL_checkint(L,2);
  }

  if (width!=8 && width!=4 && width!=1)
  	return luaL_error(L,"bytes.convert() only recognizes 8, 4 or 1 as parameter value.");

  ret = bytestring_new(width);

  if (bytestring_convert(ret,bs)!=BYTESTRING_OK)
  {
    bytestring_free(ret);
    lua_pushnil(L);
    return 1;
  }

  lua_push_bytestring(L,ret);
  return 1;
}

static int subr_bytes_to_number(lua_State *L)
{
  bytestring_t *bs = luaL_check_bytestring(L, 1);
  double ret=bytestring_to_number(bs);
  
  lua_pushnumber(L,ret);
  return 1;
}

static int subr_bytes_format(lua_State *L)
{
  static int warn_obsolete=0; 
    bytestring_t *bs;
    const char *format;
    char *s;

    if (lua_type(L,2)==LUA_TUSERDATA && lua_type(L,1)==LUA_TSTRING)
    {
        if (!warn_obsolete)
        {
            warn_obsolete = 1;
            log_printf(LOG_WARNING,"The calling convention of bytes.format() has changed in version 0.8, please update your scripts\n"
                                   "\tchange bytes.format(A,B) to bytes.format(B,A)\n"
                                   "\tThis warning will only appear once.");
        }
        format = lua_tostring(L,1);
        bs = luaL_check_bytestring(L,2);
    } 
    else
    { 
        bs = luaL_check_bytestring(L,1);
        format = lua_tostring(L,2);
    }

    s=bytestring_to_format(format,bs);
    lua_pushstring(L,s);
    free(s);
    return 1;
}

static int subr_bytes_index(lua_State *L)
{
  if (lua_gettop(L)<2)
    return luaL_error(L,"missing argument to bytes.__index() function."); 

  switch (lua_type(L,2)) 
  {
    case LUA_TNUMBER:
        return subr_bytes_get(L);
    case LUA_TSTRING:
        luaL_getmetatable(L, "bytes.type"); /* st: mt */ 
        lua_pushvalue(L,2);                 /* st: mt, key */
        lua_rawget(L,-2);                   /* st: mt, val */
        lua_remove(L,-2);                   /* st: val */
        return 1;
  } 
  return luaL_error(L,"Invalid index type for bytes.");
}

static int subr_bytes_newindex(lua_State *L)
{
  if (lua_gettop(L)<2)
    return luaL_error(L,"missing argument to bytes.__newindex() function."); 

  switch (lua_type(L,2))
  {
    case LUA_TNUMBER:
        return subr_bytes_set(L);
    case LUA_TSTRING:
        return luaL_error(L,"The 'bytes' type does not accept new indices.");
  } 
  return luaL_error(L,"Invalid index type for bytes.");
}

/**********************************************
 *
 * LUA REGISTRATION
 */


static const struct luaL_Reg byteslib_m [] = {
  {"__gc",subr_bytes_gc},
  {"__tostring",subr_bytes_tostring},
  {"__len",subr_bytes_len},
  {"__concat",subr_bytes_concat},
  {"__eq", subr_bytes_eq},
  {"__index", subr_bytes_index },
  {"__newindex", subr_bytes_newindex },

  {"ipairs",subr_bytes_ipairs},
  {"get",subr_bytes_get},
  {"set",subr_bytes_set},
  {"pad_left",subr_bytes_pad_left},
  {"pad_right",subr_bytes_pad_right},
  {"reverse",subr_bytes_reverse},
  {"clone",subr_bytes_clone},
  {"sub",subr_bytes_sub},
  {"width",subr_bytes_width},
  {"is_printable",subr_bytes_is_printable},
  {"convert",subr_bytes_convert},
  {"tonumber",subr_bytes_to_number},
  {"format",subr_bytes_format},
  {NULL,NULL}
};

static const struct luaL_Reg byteslib_f [] = {
  {"new", subr_bytes_new},
  {"new_from_chars",subr_bytes_new_from_chars},
  {"concat", subr_bytes_concat},

  {"ipairs",subr_bytes_ipairs},
  {"get",subr_bytes_get},
  {"set",subr_bytes_set},
  {"pad_left",subr_bytes_pad_left},
  {"pad_right",subr_bytes_pad_right},
  {"reverse",subr_bytes_reverse},
  {"clone",subr_bytes_clone},
  {"sub",subr_bytes_sub},
  {"width",subr_bytes_width},
  {"is_printable",subr_bytes_is_printable},
  {"convert",subr_bytes_convert},
  {"tonumber",subr_bytes_to_number},
  {"format",subr_bytes_format},
  {NULL, NULL}
};

int luaopen_bytes(lua_State* L) 
{
  luaL_newmetatable(L, "bytes.type");
  lua_pushstring(L, "__index");
  lua_pushvalue(L, -2);  /* pushes the metatable */
  lua_settable(L, -3);  /* metatable.__index = metatable */
  luaL_setfuncs(L, byteslib_m, 0);
  luaL_newlib(L, byteslib_f);
  lua_setglobal(L,"bytes");
  lua_pop(L,1); /* pop the metatable */ 
  return 1;
}