Esempio n. 1
0
/**
*  Hash function. Returns a hash for a given string.
*  @param message: arbitrary binary string.
*  @return  A 128-bit hash string.
*/
static int lmd5 (lua_State *L) {
  char buff[16];
  size_t l;
  const char *message = luaL_check_lstr(L, 1, &l);
  md5(message, l, buff);
  lua_pushlstring(L, buff, 16L);
  return 1;
}
Esempio n. 2
0
static int luaB_dostring (lua_State *L) {
  int oldtop = lua_gettop(L);
  size_t l;
  const char *s = luaL_check_lstr(L, 1, &l);
  if (*s == '\33')  /* binary files start with ESC... */
    lua_error(L, "`dostring' cannot run pre-compiled code");
  return passresults(L, lua_dobuffer(L, s, l, luaL_opt_string(L, 2, s)), oldtop);
}
Esempio n. 3
0
static void luaB_dostring (void) {
  long l;
  char *s = luaL_check_lstr(1, &l);
  if (*s == ID_CHUNK)
    lua_error("`dostring' cannot run pre-compiled code");
  if (lua_dobuffer(s, l, luaL_opt_string(2, s)) == 0)
    if (luaA_passresults() == 0)
      lua_pushuserdata(NULL);  /* at least one result to signal no errors */
}
Esempio n. 4
0
static void str_upper (void)
{
  int32 l;
  int32 i;
  const char *s = luaL_check_lstr(1, &l);
  luaL_resetbuffer();
  for (i=0; i<l; i++)
    luaL_addchar(toupper((byte)(s[i])));
  closeandpush();
}
Esempio n. 5
0
static int initblock (lua_State *L, const char *seed, int lseed, char *block) {
  size_t lkey;
  const char *key = luaL_check_lstr(L, 2, &lkey);
  if (lkey > MAXKEY)
    luaL_verror(L, "key too long (> %d)", MAXKEY);
  memset(block, 0, BLOCKSIZE);
  memcpy(block, seed, lseed);
  memcpy(block+BLOCKSIZE, key, lkey);
  return (int)lkey+BLOCKSIZE;
}
Esempio n. 6
0
static void str_rep (void)
{
  int32 l;
  const char *s = luaL_check_lstr(1, &l);
  int32 n = (int32)luaL_check_number(2);
  luaL_resetbuffer();
  while (n-- > 0)
    addnchar(s, l);
  closeandpush();
}
Esempio n. 7
0
static void str_sub (void)
{
  int32 l;
  const char *s = luaL_check_lstr(1, &l);
  int32 start = posrelat((int32)luaL_check_number(2), l);
  int32 end = posrelat((int32)luaL_opt_number(3, -1), l);
  if (1 <= start && start <= end && end <= l)
    lua_pushlstring(s+start-1, end-start+1);
  else lua_pushstring("");
}
Esempio n. 8
0
/**
*  Encrypts a string. Uses the hash function md5 in CFB (Cipher-feedback
*  mode).
*  @param message: arbitrary binary string to be encrypted.
*  @param key: arbitrary binary string to be used as a key.
*  @param [seed]: optional arbitrary binary string to be used as a seed.
*  if no seed is provided, the function uses the result of
*  <code>time()</code> as a seed.  
*  @return  The cyphertext (as a binary string).
*/
static int crypt (lua_State *L) {
  size_t lmsg;
  const char *msg = luaL_check_lstr(L, 1, &lmsg);
  size_t lseed;
  const char *seed;
  int lblock;
  char block[BLOCKSIZE+MAXKEY];
  checkseed(L);
  seed = luaL_check_lstr(L, 3, &lseed);
  if (lseed > BLOCKSIZE)
    luaL_verror(L, "seed too long (> %d)", BLOCKSIZE);
  /* put seed and seed length at the beginning of result */
  block[0] = (char)lseed;
  memcpy(block+1, seed, lseed);
  lua_pushlstring(L, block, lseed+1);  /* to concat with result */
  lblock = initblock(L, seed, lseed, block);
  codestream(L, msg, lmsg, block, lblock);
  lua_concat(L, 2);
  return 1;
}
Esempio n. 9
0
static int str_upper (lua_State *L) {
    size_t l;
    size_t i;
    luaL_Buffer b;
    const char *s = luaL_check_lstr(L, 1, &l);
    luaL_buffinit(L, &b);
    for (i=0; i<l; i++)
        luaL_putchar(&b, toupper((unsigned char)(s[i])));
    luaL_pushresult(&b);
    return 1;
}
Esempio n. 10
0
static int str_rep (lua_State *L) {
    size_t l;
    luaL_Buffer b;
    const char *s = luaL_check_lstr(L, 1, &l);
    int n = luaL_check_int(L, 2);
    luaL_buffinit(L, &b);
    while (n-- > 0)
        luaL_addlstring(&b, s, l);
    luaL_pushresult(&b);
    return 1;
}
Esempio n. 11
0
static int str_sub (lua_State *L) {
    size_t l;
    const char *s = luaL_check_lstr(L, 1, &l);
    long start = posrelat(luaL_check_long(L, 2), l);
    long end = posrelat(luaL_opt_long(L, 3, -1), l);
    if (start < 1) start = 1;
    if (end > (long)l) end = l;
    if (start <= end)
        lua_pushlstring(L, s+start-1, end-start+1);
    else lua_pushstring(L, "");
    return 1;
}
Esempio n. 12
0
static int d_AddToDisTab(lua_State *ls)
	{
	DISZ80		*d;
	const char	*str;
	size_t		len;

	d = GetD(ls);
	str = luaL_check_lstr(ls, 1, &len);

	AddToDisTab(d, (char *)str);
	return 0;
	}
Esempio n. 13
0
/**
*  Decrypts a string. For any message, key, and seed, we have that
*  <code>decrypt(crypt(msg, key, seed), key) == msg</code>.
*  @param cyphertext: message to be decrypted (this must be the result of
   a previous call to <code>crypt</code>.
*  @param key: arbitrary binary string to be used as a key.
*  @return  The plaintext.
*/
static int decrypt (lua_State *L) {
  size_t lcyphertext;
  const char *cyphertext = luaL_check_lstr(L, 1, &lcyphertext);
  size_t lseed = cyphertext[0];
  const char *seed = cyphertext+1;
  int lblock;
  char block[BLOCKSIZE+MAXKEY];
  luaL_arg_check(L, lcyphertext >= lseed+1 && lseed <= BLOCKSIZE, 1,
                 "invalid cyphered string");
  cyphertext += lseed+1;
  lcyphertext -= lseed+1;
  lblock = initblock(L, seed, lseed, block);
  decodestream(L, cyphertext, lcyphertext, block, lblock);
  return 1;
}
Esempio n. 14
0
static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
  size_t l;
  const char *s = luaL_check_lstr(L, arg, &l);
  luaL_putchar(b, '"');
  while (l--) {
    switch (*s) {
      case '"':  case '\\':  case '\n':
        luaL_putchar(b, '\\');
        luaL_putchar(b, *s);
        break;
      case '\0': luaL_addlstring(b, "\\000", 4); break;
      default: luaL_putchar(b, *s);
    }
    s++;
  }
  luaL_putchar(b, '"');
}
Esempio n. 15
0
static int io_write (lua_State *L) {
  int lastarg = lua_gettop(L) - 1;
  IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  int arg = 1;
  int status = 1;
  FILE *f = gethandle(L, ctrl, arg);
  if (f) arg++;
  else f = getfilebyref(L, ctrl, OUTFILE);  /* get _OUTPUT */
  for (; arg <=  lastarg; arg++) {
    if (lua_type(L, arg) == LUA_TNUMBER) {  /* LUA_NUMBER */
      /* optimization: could be done exactly as for strings */
      status = status && fprintf(f, "%ld", lua_tonumber(L, arg)) > 0;
    }
    else {
      size_t l;
      const char *s = luaL_check_lstr(L, arg, &l);
      status = status && (fwrite(s, sizeof(char), l, f) == l);
    }
  }
  pushresult(L, status);
  return 1;
}
Esempio n. 16
0
static int g_write (lua_State *L, FILE *f, int arg) {
  int nargs = lua_gettop(L) - 1;
  int status = 1;
  for (; nargs--; arg++) {
    if (lua_type(L, arg) == LUA_TNUMBER) {
      /* optimization: could be done exactly as for strings */
      status = status &&
          fprintf(f, LUA_NUMBER_FMT, lua_tonumber(L, arg)) > 0;
    }
    else if (lua_type(L, arg) == LUA_TUSTRING) {
      size_t l;
      const wchar_t *s = luaL_check_lustr(L, arg, &l);
      status = status && (fwrite(s, sizeof(wchar_t), l, f) == l);
    }
    else {
      size_t l;
      const char *s = luaL_check_lstr(L, arg, &l);
      status = status && (fwrite(s, sizeof(char), l, f) == l);
    }
  }
  pushresult(L, status);
  return 1;
}
Esempio n. 17
0
char *luaL_opt_lstr (int numArg, char *def, long *len)
{
  return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
                              luaL_check_lstr(numArg, len);
}
Esempio n. 18
0
static void str_len (void)
{
  int32 l;
  luaL_check_lstr(1, &l);
  lua_pushnumber(l);
}
Esempio n. 19
0
static int str_format (lua_State *L) {
    int arg = 1;
    const char *strfrmt = luaL_check_string(L, arg);
    luaL_Buffer b;
    luaL_buffinit(L, &b);
    while (*strfrmt) {
        if (*strfrmt != '%')
            luaL_putchar(&b, *strfrmt++);
        else if (*++strfrmt == '%')
            luaL_putchar(&b, *strfrmt++);  /* %% */
        else { /* format item */
            struct Capture cap;
            char form[MAX_FORMAT];  /* to store the format ('%...') */
            char buff[MAX_ITEM];  /* to store the formatted item */
            const char *initf = strfrmt;
            form[0] = '%';
            if (isdigit((unsigned char)*initf) && *(initf+1) == '$') {
                arg = *initf - '0';
                initf += 2;  /* skip the 'n$' */
            }
            arg++;
            cap.src_end = strfrmt+strlen(strfrmt)+1;
            cap.level = 0;
            strfrmt = match(L, initf, "[-+ #0]*(%d*)%.?(%d*)", &cap);
            if (cap.capture[0].len > 2 || cap.capture[1].len > 2 ||  /* < 100? */
                    strfrmt-initf > MAX_FORMAT-2)
                lua_error(L, "invalid format (width or precision too long)");
            strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include conversion */
            form[strfrmt-initf+2] = 0;
            switch (*strfrmt++) {
            case 'c':
            case 'd':
            case 'i':
                sprintf(buff, form, luaL_check_int(L, arg));
                break;
            case 'o':
            case 'u':
            case 'x':
            case 'X':
                sprintf(buff, form, (unsigned int)luaL_check_number(L, arg));
                break;
            case 'e':
            case 'E':
            case 'f':
            case 'g':
            case 'G':
                sprintf(buff, form, luaL_check_number(L, arg));
                break;
            case 'q':
                luaI_addquoted(L, &b, arg);
                continue;  /* skip the "addsize" at the end */
            case 's': {
                size_t l;
                const char *s = luaL_check_lstr(L, arg, &l);
                if (cap.capture[1].len == 0 && l >= 100) {
                    /* no precision and string is too long to be formatted;
                       keep original string */
                    lua_pushvalue(L, arg);
                    luaL_addvalue(&b);
                    continue;  /* skip the "addsize" at the end */
                }
                else {
                    sprintf(buff, form, s);
                    break;
                }
            }
            default:  /* also treat cases 'pnLlh' */
                lua_error(L, "invalid option in `format'");
            }
            luaL_addlstring(&b, buff, strlen(buff));
        }
    }
    luaL_pushresult(&b);
    return 1;
}
Esempio n. 20
0
static int str_len (lua_State *L) {
    size_t l;
    luaL_check_lstr(L, 1, &l);
    lua_pushnumber(L, l);
    return 1;
}