Esempio n. 1
0
static int tconcat (lua_State *L) {
  luaL_Buffer b;
  size_t lsep;
  int i, last;
  const char *sep = luaL_optlstring(L, 2, "", &lsep);
  luaL_checktype(L, 1, LUA_TTABLE);
  i = luaL_optint(L, 3, 1);
  last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
  luaL_buffinit(L, &b);
  for (; i <= last; i++) {
    lua_rawgeti(L, 1, i);
    luaL_argcheck(L, lua_isstring(L, -1), 1, "table contains non-strings");
    luaL_addvalue(&b);
    if (i != last)
      luaL_addlstring(&b, sep, lsep);
  }
  luaL_pushresult(&b);
  return 1;
}
Esempio n. 2
0
// Lua: s = net.dns.setdnsserver(ip_addr, [index])
static int net_setdnsserver( lua_State* L )
{
  size_t l;
  u32_t ip32;

  const char *server = luaL_checklstring( L, 1, &l );
  if (l>16 || server == NULL || (ip32 = ipaddr_addr(server)) == IPADDR_NONE || ip32 == IPADDR_ANY)
    return luaL_error( L, "invalid dns server ip" );

  int numdns = luaL_optint(L, 2, 0);
  if (numdns >= DNS_MAX_SERVERS)
    return luaL_error( L, "server index out of range [0-%d]", DNS_MAX_SERVERS - 1);

  ip_addr_t ipaddr;
  ip4_addr_set_u32(&ipaddr, ip32);
  dns_setserver(numdns,&ipaddr);

  return 0;
}
Esempio n. 3
0
// Lua: ucg.setColor( self, [idx], r, g, b )
static int lucg_setColor( lua_State *L )
{
    lucg_userdata_t *lud;

    if ((lud = get_lud( L )) == NULL)
        return 0;

    ucg_int_t args[3];
    lucg_get_int_args( L, 2, 3, args );

    ucg_int_t opt = luaL_optint( L, (1+3) + 1, -1 );

    if (opt < 0)
        ucg_SetColor( LUCG, 0, args[0], args[1], args[2] );
    else
        ucg_SetColor( LUCG, args[0], args[1], args[2], opt );

    return 0;
}
Esempio n. 4
0
static LUA_FUNCTION(openssl_pkcs7_decrypt)
{
  PKCS7 *p7 = CHECK_OBJECT(1, PKCS7, "openssl.pkcs7");
  X509 *cert = CHECK_OBJECT(2, X509, "openssl.x509");
  EVP_PKEY *key = CHECK_OBJECT(3, EVP_PKEY, "openssl.evp_pkey");
  long flags = luaL_optint(L, 4, 0);
  BIO *out = BIO_new(BIO_s_mem());

  if (PKCS7_decrypt(p7, key, cert, out, flags))
  {
    BUF_MEM* mem;
    BIO_get_mem_ptr(out, &mem);
    lua_pushlstring(L, mem->data, mem->length);
  }
  else
    lua_pushnil(L);
  BIO_free(out);
  return 1;
}
Esempio n. 5
0
WSLUA_METHOD TvbRange_strsize(lua_State* L) {
        /* Find the size of a zero terminated string from a TvbRange.  The size of the string includes the terminating zero. */
#define WSLUA_OPTARG_TvbRange_strsize_ENCODING 2 /* The encoding to use. Defaults to ENC_ASCII. */
    TvbRange tvbr = checkTvbRange(L,1);
    guint encoding = (guint)luaL_optint(L,WSLUA_OPTARG_TvbRange_strsize_ENCODING, ENC_ASCII|ENC_NA);
    gint offset;
    gunichar2 uchar;

    if ( !(tvbr && tvbr->tvb)) return 0;
    if (tvbr->tvb->expired) {
        luaL_error(L,"expired tvb");
        return 0;
    }

    switch (encoding & ENC_CHARENCODING_MASK) {

    case ENC_UTF_16:
    case ENC_UCS_2:
        offset = tvbr->offset;
        do {
            if (!tvb_bytes_exist (tvbr->tvb->ws_tvb, offset, 2)) {
                luaL_error(L,"out of bounds");
                return 0;
            }
            /* Endianness doesn't matter when looking for null */
            uchar = tvb_get_ntohs (tvbr->tvb->ws_tvb, offset);
            offset += 2;
        } while (uchar != 0);
        lua_pushinteger(L, tvb_unicode_strsize(tvbr->tvb->ws_tvb, tvbr->offset));
        break;

    default:
        if (tvb_find_guint8 (tvbr->tvb->ws_tvb, tvbr->offset, -1, 0) == -1) {
            luaL_error(L,"out of bounds");
            return 0;
        }
        lua_pushinteger(L, tvb_strsize(tvbr->tvb->ws_tvb, tvbr->offset));
        break;
    }

    WSLUA_RETURN(1); /* Length of the zero terminated string */
}
Esempio n. 6
0
WSLUA_METHOD TvbRange_stringz(lua_State* L) {
	/* Obtain a zero terminated string from a TvbRange */
#define WSLUA_OPTARG_TvbRange_stringz_ENCODING 2 /* The encoding to use. Defaults to ENC_ASCII. */
    TvbRange tvbr = checkTvbRange(L,1);
    guint encoding = (guint)luaL_optint(L,WSLUA_OPTARG_TvbRange_stringz_ENCODING, ENC_ASCII|ENC_NA);
    gint offset;
    gunichar2 uchar;

    if ( !(tvbr && tvbr->tvb)) return 0;
    if (tvbr->tvb->expired) {
        luaL_error(L,"expired tvb");
        return 0;
    }

    switch (encoding & ENC_CHARENCODING_MASK) {

    case ENC_UTF_16:
    case ENC_UCS_2:
        offset = tvbr->offset;
        do {
            if (!tvb_bytes_exist (tvbr->tvb->ws_tvb, offset, 2)) {
                luaL_error(L,"out of bounds");
                return 0;
            }
            /* Endianness doesn't matter when looking for null */
            uchar = tvb_get_ntohs (tvbr->tvb->ws_tvb, offset);
            offset += 2;
        } while(uchar != 0);
        break;

    default:
        if (tvb_find_guint8 (tvbr->tvb->ws_tvb, tvbr->offset, -1, 0) == -1) {
            luaL_error(L,"out of bounds");
            return 0;
        }
        break;
    }

    lua_pushstring(L, (gchar*)tvb_get_stringz_enc(wmem_packet_scope(),tvbr->tvb->ws_tvb,tvbr->offset,NULL,encoding));

    WSLUA_RETURN(1); /* The zero terminated string */
}
Esempio n. 7
0
// Lua: s = net.dns.getdnsserver([index])
static int net_getdnsserver( lua_State* L ) {
  int numdns = luaL_optint(L, 1, 0);
  if (numdns >= DNS_MAX_SERVERS)
    return luaL_error( L, "server index out of range [0-%d]", DNS_MAX_SERVERS - 1);

  // ip_addr_t ipaddr;
  // dns_getserver(numdns,&ipaddr);
  // Bug fix by @md5crypt https://github.com/nodemcu/nodemcu-firmware/pull/500
  ip_addr_t ipaddr = dns_getserver(numdns);

  if ( ip_addr_isany(&ipaddr) ) {
    lua_pushnil( L );
  } else {
    char temp[20] = {0};
    c_sprintf(temp, IPSTR, IP2STR( &ipaddr.addr ) );
    lua_pushstring( L, temp );
  }

  return 1;
}
Esempio n. 8
0
static int luazmq_skt_send (lua_State *L) {
  zsocket *skt = luazmq_getsocket(L);
  size_t len;
  const char *data = luaL_checklstring(L, 2, &len);
  int ret, flags = luaL_optint(L,3,0);

#ifdef LUAZMQ_USE_SEND_AS_BUF
  ret = zmq_send(skt->skt, data, len, flags);
#else
  zmq_msg_t msg;
  ret = zmq_msg_init_size(&msg, len);
  if(-1 == ret) return luazmq_fail(L, skt);
  memcpy(zmq_msg_data(&msg), data, len);
  ret = zmq_msg_send(&msg, skt->skt, flags);
  zmq_msg_close(&msg);
#endif

  if(-1 == ret) return luazmq_fail(L, skt);
  return luazmq_pass(L);
}
Esempio n. 9
0
File: rsa.c Progetto: world100/11111
static LUA_FUNCTION(openssl_rsa_sign)
{
  RSA* rsa = CHECK_OBJECT(1, RSA, "openssl.rsa");
  size_t l;
  const unsigned char* msg = (const unsigned char *)luaL_checklstring(L, 2, &l);
  int type = luaL_optint(L, 3, NID_md5_sha1);
  unsigned char* sig = OPENSSL_malloc(RSA_size(rsa));
  int flen = l;
  unsigned int slen = RSA_size(rsa);

  int ret = RSA_sign(type, msg, flen, sig, &slen, rsa);
  if (ret == 1)
  {
    lua_pushlstring(L, (const char*)sig, slen);
    OPENSSL_free(sig);
    return 1;
  }
  OPENSSL_free(sig);
  return openssl_pushresult(L, ret);
};
Esempio n. 10
0
static int alien_memcpy(lua_State *L) {
  void* dst;
  void* src;
  size_t size;
  dst = lua_touserdata(L, 1);
  if(!dst)
    luaL_typerror(L, 1, "userdata, or light userdata");
  if (!(lua_isuserdata(L, 2) || lua_isstring(L, 2)))
    luaL_typerror(L, 2, "string, userdata, or light userdata");
  if (lua_isuserdata(L, 2)) {
    src = lua_touserdata(L, 2);
    size = luaL_checkint(L, 3);
  } else {
    src = (void*)lua_tolstring(L, 2, &size);
    size = luaL_optint(L, 3, size);
  }
  if (size > 0)
    memcpy(dst, src, size);
  return 0;
}
Esempio n. 11
0
//tpt. api methods
int luatpt_getelement(lua_State *l)
{
	int t;
	if (lua_isnumber(l, 1))
	{
		t = luaL_optint(l, 1, 1);
		if (t<0 || t>=PT_NUM)
			return luaL_error(l, "Unrecognised element number '%d'", t);
		lua_pushstring(l, luacon_sim->elements[t].Name.ToUtf8().c_str());
	}
	else
	{
		luaL_checktype(l, 1, LUA_TSTRING);
		const char* name = luaL_optstring(l, 1, "");
		if ((t = luacon_sim->GetParticleType(name))==-1)
			return luaL_error(l, "Unrecognised element '%s'", name);
		lua_pushinteger(l, t);
	}
	return 1;
}
Esempio n. 12
0
static int libE_concat (lua_State *L)
{
    luaL_Buffer b;
    size_t lsep;
    int i, last;
    const char *sep = luaL_optlstring(L, 2, "", &lsep);
    luaL_checktype(L, 1, LUA_TTABLE);
    i = luaL_optint(L, 3, 1);
    last = luaL_opt(L, luaL_checkint, 4, luaL_getn(L, 1));
    luaL_buffinit(L, &b);
    for (; i < last; i++)
    {
        addfield(L, &b, i);
        luaL_addlstring(&b, sep, lsep);
    }
    if (i == last)  /* add last value (if interval was not empty) */
        addfield(L, &b, i);
    luaL_pushresult(&b);
    return 1;
}
Esempio n. 13
0
static int lua_connect_unix (lua_State *L) {
  const char *path = luaL_checkstring(L, 1);
  int timeout = luaL_optint(L, 3, -1);
  int nonblock = lua_toboolean(L, 4);
  if (timeout == -1) {
    if (nonblock == 1) {
      redisContext *con = redisConnectUnixNonBlock(path);
      return pushConnection(L, con);
    } else {
      redisContext *con = redisConnectUnix(path);
      return pushConnection(L, con);
    }
  } else {
    struct timeval to;
    to.tv_sec = timeout;
    to.tv_usec = 0;
    redisContext *con = redisConnectUnixWithTimeout(path, to);
    return pushConnection(L, con);
  }
};
Esempio n. 14
0
static int tremove(lua_State *L)
{
    int size = aux_getn(L, 1);
    int pos  = luaL_optint(L, 2, size);

    if (pos != size) /* validate 'pos' if given */
        luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds");

    lua_rawgeti(L, 1, pos); /* result = t[pos] */

    for (; pos < size; pos++)
    {
        lua_rawgeti(L, 1, pos + 1);
        lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */
    }

    lua_pushnil(L);
    lua_rawseti(L, 1, pos); /* t[pos] = nil */
    return 1;
}
Esempio n. 15
0
static int openssl_xname_get_text(lua_State*L)
{
  X509_NAME* xn = CHECK_OBJECT(1, X509_NAME, "openssl.x509_name");
  int nid = openssl_get_nid(L, 2);
  int lastpos = luaL_optint(L, 3, -1);
  X509_NAME_ENTRY *e;
  ASN1_STRING *s;
  if (nid == NID_undef)
    return 0;

  lastpos = X509_NAME_get_index_by_NID(xn, nid, lastpos);
  if (lastpos == -1)
    return 0;

  e = X509_NAME_get_entry(xn, lastpos);
  s = X509_NAME_ENTRY_get_data(e);
  lua_pushlstring(L, (const char *)ASN1_STRING_data(s), ASN1_STRING_length(s));
  lua_pushinteger(L, lastpos);
  return 2;
};
Esempio n. 16
0
int LuaGlobalFunctions_SetDBCSpellVar(lua_State * L)
{
	uint32 entry = luaL_checkinteger(L,1);
	const char* var = luaL_checkstring(L,2);
	int subindex = 0;
	if (lua_gettop(L) == 4)
		subindex = luaL_optint(L,3,0);

	int valindex = 3;
	if (subindex)
		valindex++;

	SpellEntry * proto = dbcSpell.LookupEntryForced(entry);
	if (!entry || !var || subindex < 0 || !proto)
		RET_BOOL(false);

	LuaSpellEntry l = GetLuaSpellEntryByName(var);
	if (!l.name)
		RET_BOOL(false);

	switch (l.typeId) //0: int, 1: char*, 2: bool, 3: float
	{
	case 0:
		GET_SPELLVAR_INT(proto,l.offset,subindex) = luaL_checkinteger(L, valindex);
		lua_pushboolean(L, 1);
		break;
	case 1:
		strcpy(GET_SPELLVAR_CHAR(proto,l.offset,subindex), luaL_checkstring(L, valindex));
		lua_pushboolean(L, 1);
		break;
	case 2:
		GET_SPELLVAR_BOOL(proto,l.offset,subindex) = CHECK_BOOL(L, valindex);
		lua_pushboolean(L, 1);
		break;
	case 3:
		GET_SPELLVAR_FLOAT(proto,l.offset,subindex) = (float)luaL_checknumber(L, valindex);
		lua_pushboolean(L, 1);
		break;
	}
	return 1;
}
Esempio n. 17
0
int LuaSpell_GetVar(lua_State * L, Spell * sp)
{
    if(sp == NULL)
        RET_NIL(true);

    const char* var = luaL_checkstring(L,1);
    if (var == NULL)
        RET_NIL(true);

    int subindex = luaL_optint(L,2,0);
    if (subindex < 0)
        RET_NIL(true);

    SpellEntry * proto = sp->GetSpellProto();
    if (proto == NULL)
        RET_NIL(true);

    LuaSpellEntry l = GetLuaSpellEntryByName(var);
    if (!l.name)
        RET_NIL(true);

    switch (l.typeId) //0: int, 1: char*, 2: bool, 3: float
    {
    case 0:
        lua_pushinteger(L, GET_SPELLVAR_INT(proto,l.offset,subindex));
        break;
    case 1:
        lua_pushstring(L, GET_SPELLVAR_CHAR(proto,l.offset,subindex));
        break;
    case 2:
        lua_pushboolean(L, (GET_SPELLVAR_BOOL(proto,l.offset,subindex)) ? 1 : 0);
        break;
    case 3:
        lua_pushnumber(L, GET_SPELLVAR_FLOAT(proto,l.offset,subindex));
        break;
    default:
        RET_NIL(false);
        break;
    }
    return 1;
}
Esempio n. 18
0
	static bool to_message(lua_State * L, Event::Message & msg)
	{
		const char * str = luaL_checkstring(L, 1);

		if(!Event::getConstant(str, msg.type))
			return false;

		switch(msg.type)
		{
		case Event::TYPE_KEY_PRESSED:
			if(!Event::getConstant(luaL_checkstring(L, 2), msg.keyboard.k))
				return false;
			msg.keyboard.u = (unsigned short)luaL_optint(L, 3, 0);
			return true;
		case Event::TYPE_KEY_RELEASED:
			if(!Event::getConstant(luaL_checkstring(L, 2), msg.keyboard.k))
				return false;
			return true;
		case Event::TYPE_MOUSE_PRESSED:
		case Event::TYPE_MOUSE_RELEASED:
			if(!Event::getConstant(luaL_checkstring(L, 2), msg.mouse.b))
				return false;
			msg.mouse.x = luaL_checkint(L, 3);
			msg.mouse.y = luaL_checkint(L, 4);
			return true;
		case Event::TYPE_JOYSTICK_PRESSED:
		case Event::TYPE_JOYSTICK_RELEASED:
			msg.joystick.index = luaL_checkint(L, 2);
			msg.joystick.button = luaL_checkint(L, 3);
			return true;
		case Event::TYPE_FOCUS:
			msg.focus.f = luax_toboolean(L, 2);
			return true;
		case Event::TYPE_QUIT:
			return true;
		default:
			return false;
		}

		return false;
	}
Esempio n. 19
0
File: ldblib.c Progetto: korman/Temp
static int db_sethook (lua_State *L) {
  int arg;
  lua_State *L1 = getthread(L, &arg);
  if (lua_isnoneornil(L, arg+1)) {
    lua_settop(L, arg+1);
    lua_sethook(L1, NULL, 0, 0);  /* turn off hooks */
  }
  else {
    const char *smask = luaL_checkstring(L, arg+2);
    int count = luaL_optint(L, arg+3, 0);
    luaL_checktype(L, arg+1, LUA_TFUNCTION);
    lua_sethook(L1, hookf, makemask(smask, count), count);
  }
  gethooktable(L1);
  lua_pushlightuserdata(L1, L1);
  lua_pushvalue(L, arg+1);
  lua_xmove(L, L1, 1);
  lua_rawset(L1, -3);  /* set new hook */
  lua_pop(L1, 1);  /* remove hook table */
  return 0;
}
Esempio n. 20
0
static int db_sethook (lua_State *L) {
  int arg, mask, count;
  lua_Hook func;
  lua_State *L1 = getthread(L, &arg);
  if (lua_isnoneornil(L, arg+1)) {
    lua_settop(L, arg+1);
    func = NULL; mask = 0; count = 0;  /* turn off hooks */
  }
  else {
    const char *smask = luaL_checkstring(L, arg+2);
    luaL_checktype(L, arg+1, LUA_TFUNCTION);
    count = luaL_optint(L, arg+3, 0);
    func = hookf; mask = makemask(smask, count);
  }
  gethooktable(L);
  lua_pushvalue(L, arg+1);
  lua_rawsetp(L, -2, L1);  /* set new hook */
  lua_pop(L, 1);  /* remove hook table */
  lua_sethook(L1, func, mask, count);  /* set hooks */
  return 0;
}
Esempio n. 21
0
static int openssl_ssl_peek(lua_State*L)
{
  SSL* s = CHECK_OBJECT(1, SSL, "openssl.ssl");
  int num = luaL_optint(L, 2, SSL_pending(s));
  void* buf;
  int ret;

  num = num ? num : 4096;
  buf = malloc(num);
  ret = SSL_peek(s, buf, num);
  if (ret > 0)
  {
    lua_pushlstring(L, buf, ret);
    ret = 1;
  } else
  {
    ret = openssl_ssl_pushresult(L, s, ret);
  }
  free(buf);
  return ret;
}
Esempio n. 22
0
static int unpack(lua_State *L)
{
    int i, e, n;

    luaL_checktype(L, 1, LUA_TTABLE);
    i = luaL_optint(L, 2, 1);
    e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
    if (i > e)
        return 0;       /* empty range */

    n = e - i + 1; /* number of elements */
    if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
        return luaL_error(L, "too many results to unpack");

    lua_rawgeti(L, 1, i); /* push arg[i] (avoiding overflow problems) */

    while (i++ < e) /* push arg[i + 1...e] */
        lua_rawgeti(L, 1, i);

    return n;
}
Esempio n. 23
0
static int openssl_cms_sign_receipt(lua_State*L)
{
  CMS_ContentInfo *cms = CHECK_OBJECT(1, CMS_ContentInfo, "openssl.cms");
  X509 *signcert = CHECK_OBJECT(2, X509, "openssl.x509");
  EVP_PKEY* pkey = CHECK_OBJECT(3, EVP_PKEY, "openssl.evp_pkey");
  STACK_OF(X509) *other = CHECK_OBJECT(4, STACK_OF(X509), "openssl.stack_of_x509");
  unsigned int flags = luaL_optint(L, 5, 0);

  STACK_OF(CMS_SignerInfo) *sis = CMS_get0_SignerInfos(cms);
  if (sis)
  {
    CMS_SignerInfo *si = sk_CMS_SignerInfo_value(sis, 0);
    CMS_ContentInfo *srcms = CMS_sign_receipt(si, signcert, pkey, other, flags);
    if (srcms)
    {
      PUSH_OBJECT(srcms, "openssl.cms");
      return 1;
    }
  }
  return openssl_pushresult(L, 0);
}
Esempio n. 24
0
File: osd.c Progetto: CSRedRat/vlc
static int vlclua_osd_icon( lua_State *L )
{
    const char *psz_icon = luaL_checkstring( L, 1 );
    int i_icon = vlc_osd_icon_from_string( psz_icon );
    int i_chan = luaL_optint( L, 2, SPU_DEFAULT_CHANNEL );
    if( !i_icon )
        return luaL_error( L, "\"%s\" is not a valid osd icon.", psz_icon );

    input_thread_t *p_input = vlclua_get_input_internal( L );
    if( p_input )
    {
        vout_thread_t *p_vout = input_GetVout( p_input );
        if( p_vout )
        {
            vout_OSDIcon( p_vout, i_chan, i_icon );
            vlc_object_release( p_vout );
        }
        vlc_object_release( p_input );
    }
    return 0;
}
Esempio n. 25
0
/*
** self = buffer:putreader(rd, offset=0, length=all)
*/
static int lbuffer_putreader(lua_State *L) 
{
	Buffer *buffer = buffer_lcheck(L, 1);
	Reader *reader = reader_lcheck(L, 2);
	size_t offset = (size_t)luaL_optint(L, 3, 0);
	size_t length = reader->datasiz;
	
	if (lua_gettop(L) >= 4) 
		length = (size_t)luaL_checkint(L, 4);
	
	if (offset >= reader->datasiz)
		length = 0;
	else if ((offset + length) > reader->datasiz)
		length = (reader->datasiz - offset);
		
	if (length > 0)
		buffer_push(buffer, reader->data + offset, length);
		
	lua_pushvalue(L, 1);
	return 1;
}
Esempio n. 26
0
/*
** line1, ... = buffer:getline(num)
** 
** content will be shifted
*/
static int lbuffer_getline(lua_State *L)
{
	Buffer *buffer = buffer_lcheck(L, 1);
	size_t num = (size_t)luaL_optint(L, 2, 1);
	Reader rd;

	reader_init(&rd, buffer->data, buffer->datasiz);

	for (size_t i = 0; i < num ; i++) {
		size_t len;
		const char *str = reader_getline(&rd, &len);
		if (str != NULL)
			lua_pushlstring(L, str, len);
		else
			lua_pushnil(L);
	}	
	
	buffer_shift(buffer, (size_t)(rd.data - rd.mem));
	
	return (int)num;
}
int CBaseLuaRadioButton::LuaAdd(lua_State *L)
{
    CBaseLuaRadioButton *rad = CheckLuaWidgetClass<CBaseLuaRadioButton>("radiobutton", 1);
    const char *label = luaL_checkstring(L, 2);
    int pos = luaL_optint(L, 3, -1);
    
    TSTLVecSize n;
    if (pos < 0)
        n = rad->m_Options.size();
    else
        n = SafeConvert<TSTLVecSize>(pos) - 1;
    
    if (n >= rad->m_Options.size())
        rad->m_Options.push_back(label);
    else
        rad->m_Options.insert(rad->m_Options.begin() + n, label);
    
    rad->AddButton(label, n);
    
    return 0;
}
/*
**test upvalue in c function
*/
static int t_tuple(lua_State *L)
{
	int default_value = 0;
	int op = luaL_optint(L,1,default_value);
	if(op == 0) /*no parameters*/
	{
		int i;
		for(i = 1; !lua_isnone(L,lua_upvalueindex(i)); i++)
			lua_pushvalue(L,lua_upvalueindex(i));
		return (i - 1);
	}
	else
	{
		luaL_argcheck(L,0 < op,1,"index out of range");
		if(lua_isnone(L,lua_upvalueindex(op)))
			return 0;

		lua_pushvalue(L,lua_upvalueindex(op));
		return 1;
	}
}
Esempio n. 29
0
static int luaB_tonumber (lua_State *L) {
  int base = luaL_optint(L, 2, 10);
  if (base == 10) {  /* standard conversion */
    luaL_checkany(L, 1);
    if (lua_isnumber(L, 1)) {
      lua_pushnumber(L, lua_tonumber(L, 1));
      return 1;
    }
  }
  else if (lua_type(L, 1) == LUA_TWSTRING) {
    const lua_WChar *s1 = luaL_checkwstring(L, 1);
    lua_WChar *s2;
    unsigned long n;
    luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
    n = wcstoul(s1, &s2, base);
    if (s1 != s2) {  /* at least one valid digit? */
      while (iswspace(*s2)) s2++;  /* skip trailing spaces */
      if (*s2 == '\0') {  /* no invalid trailing characters? */
        lua_pushnumber(L, n);
        return 1;
      }
    }
  }
  else {
    const char *s1 = luaL_checkstring(L, 1);
    char *s2;
    unsigned long n;
    luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
    n = strtoul(s1, &s2, base);
    if (s1 != s2) {  /* at least one valid digit? */
      while (isspace((unsigned char)(*s2))) s2++;  /* skip trailing spaces */
      if (*s2 == '\0') {  /* no invalid trailing characters? */
        lua_pushnumber(L, (lua_Number)n);
        return 1;
      }
    }
  }
  lua_pushnil(L);  /* else not a number */
  return 1;
}
Esempio n. 30
0
static int openssl_cms_write(lua_State *L)
{
  CMS_ContentInfo *cms = CHECK_OBJECT(1, CMS_ContentInfo, "openssl.cms");
  BIO *out = load_bio_object(L, 2);
  BIO *in = load_bio_object(L, 3);
  int flags = luaL_optint(L, 4, 0);
  int fmt = luaL_checkoption(L, 5, "smime", format);
  int ret = 0;

  if (fmt == FORMAT_SMIME)
    ret = SMIME_write_CMS(out, cms, in, flags);
  else if (fmt == FORMAT_PEM)
    ret = PEM_write_bio_CMS_stream(out, cms, in, flags);
  else if (fmt == FORMAT_DER)
  {
    ret = i2d_CMS_bio_stream(out, cms, in, flags);
    //i2d_CMS_bio
  }
  else
    luaL_argerror(L, 5, "only accept smime, pem or der");
  return openssl_pushresult(L, ret);
}