static int compile_regex (lua_State *L, const TArgComp *argC, TPosix **pud) { int res; TPosix *ud; ud = (TPosix *)lua_newuserdata (L, sizeof (TPosix)); memset (ud, 0, sizeof (TPosix)); /* initialize all members to 0 */ #ifdef REX_POSIX_EXT if (argC->cflags & REG_PEND) ud->r.re_endp = argC->pattern + argC->patlen; #endif res = regcomp (&ud->r, argC->pattern, argC->cflags); if (res != 0) return generate_error (L, ud, res); if (argC->cflags & REG_NOSUB) ud->r.re_nsub = 0; ud->match = (regmatch_t *) Lmalloc (L, (ALG_NSUB(ud) + 1) * sizeof (regmatch_t)); if (!ud->match) luaL_error (L, "malloc failed"); lua_pushvalue (L, ALG_ENVIRONINDEX); lua_setmetatable (L, -2); if (pud) *pud = ud; return 1; }
char *calloc(unsigned n, unsigned m) { char *s; long size; size = ((long) n) * ((long) m); s = Lmalloc(size); if (s) memset(s, 0, (size_t) size); return(s); }
int rsa_EXTRACTKEYS(RSA_CTX *ctx,unsigned char **N,unsigned char **d, unsigned char **e){ /* we extract as big - endian */ /* unsigned int a=bnBits(&ctx->e); unsigned int b=a/8; b+=1; */ *N=(unsigned char*)Lmalloc((ctx->bits/8)+1); *d=(unsigned char*)Lmalloc((ctx->bits/8)+1); *e=(unsigned char*)Lmalloc((ctx->bits/8)+1); memset(*N,0,(ctx->bits/8)+1); memset(*d,0,(ctx->bits/8)+1); memset(*e,0,(ctx->bits/8)+1); bnExtractBigBytes(&ctx->n,(void*)*N,0,ctx->bits/8); bnExtractBigBytes(&ctx->d,(void*)*d,0,ctx->bits/8); bnExtractBigBytes(&ctx->e,(void*)*e,0,ctx->bits/8); N[(ctx->bits/8)+1]='\0'; d[(ctx->bits/8)+1]='\0'; e[(ctx->bits/8)+1]='\0'; return OK; }
static const unsigned char *gettranslate (lua_State *L, int pos) { unsigned i; const unsigned char *translate; if (lua_isnoneornil (L, pos)) return NULL; translate = (const unsigned char *) Lmalloc (L, ALG_TRANSLATE_SIZE); memset ((unsigned char *) translate, 0, ALG_TRANSLATE_SIZE); /* initialize all members to 0 */ for (i = 0; i <= UCHAR_MAX; i++) { lua_pushinteger (L, i); lua_gettable (L, pos); if (lua_tostring (L, -1)) ((unsigned char *) translate)[i] = *lua_tostring (L, -1); lua_pop (L, 1); } return translate; }
static int compile_regex (lua_State *L, const TArgComp *argC, TPcre **pud) { const char *error; int erroffset; TPcre *ud; const unsigned char *tables = NULL; ud = (TPcre*)lua_newuserdata (L, sizeof (TPcre)); memset (ud, 0, sizeof (TPcre)); /* initialize all members to 0 */ lua_pushvalue (L, LUA_ENVIRONINDEX); lua_setmetatable (L, -2); if (argC->locale) { char old_locale[256]; strcpy (old_locale, setlocale (LC_CTYPE, NULL)); /* store the locale */ if (NULL == setlocale (LC_CTYPE, argC->locale)) /* set new locale */ return luaL_error (L, "cannot set locale"); ud->tables = tables = pcre_maketables (); /* make tables with new locale */ setlocale (LC_CTYPE, old_locale); /* restore the old locale */ } else if (argC->tables) { tables = argC->tables; lua_pushinteger (L, INDEX_CHARTABLES_LINK); lua_rawget (L, LUA_ENVIRONINDEX); lua_pushvalue (L, -2); lua_pushvalue (L, argC->tablespos); lua_rawset (L, -3); lua_pop (L, 1); } ud->pr = pcre_compile (argC->pattern, argC->cflags, &error, &erroffset, tables); if (!ud->pr) return luaL_error (L, "%s (pattern offset: %d)", error, erroffset + 1); ud->extra = pcre_study (ud->pr, 0, &error); if (error) return luaL_error (L, "%s", error); pcre_fullinfo (ud->pr, ud->extra, PCRE_INFO_CAPTURECOUNT, &ud->ncapt); /* need (2 ints per capture, plus one for substring match) * 3/2 */ ud->match = (int *) Lmalloc (L, (ALG_NSUB(ud) + 1) * 3 * sizeof (int)); if (pud) *pud = ud; return 1; }
static int compile_regex (lua_State *L, const TArgComp *argC, TPosix **pud) { int res; TPosix *ud; ud = (TPosix *)lua_newuserdata (L, sizeof (TPosix)); memset (ud, 0, sizeof (TPosix)); /* initialize all members to 0 */ res = tre_regncomp (&ud->r, argC->pattern, argC->patlen, argC->cflags); if (res != 0) return generate_error (L, ud, res); if (argC->cflags & REG_NOSUB) ud->r.re_nsub = 0; ud->match = (regmatch_t *) Lmalloc (L, (ALG_NSUB(ud) + 1) * sizeof (regmatch_t)); lua_pushvalue (L, LUA_ENVIRONINDEX); lua_setmetatable (L, -2); if (pud) *pud = ud; return 1; }
static int Lpcre_dfa_exec (lua_State *L) { TArgExec argE; TPcre *ud; int res; int *buf, *ovector, *wspace; checkarg_dfa_exec (L, &argE, &ud); buf = (int*) Lmalloc (L, (argE.ovecsize + argE.wscount) * sizeof(int)); ovector = buf; wspace = buf + argE.ovecsize; res = pcre_dfa_exec (ud->pr, ud->extra, argE.text, (int)argE.textlen, argE.startoffset, argE.eflags, ovector, argE.ovecsize, wspace, argE.wscount); if (ALG_ISMATCH (res) || res == PCRE_ERROR_PARTIAL) { int i; int max = (res>0) ? res : (res==0) ? (int)argE.ovecsize/2 : 1; lua_pushinteger (L, ovector[0] + 1); /* 1-st return value */ lua_newtable (L); /* 2-nd return value */ for (i=0; i<max; i++) { lua_pushinteger (L, ovector[i+i+1]); lua_rawseti (L, -2, i+1); } lua_pushinteger (L, res); /* 3-rd return value */ free (buf); return 3; } else { free (buf); if (res == ALG_NOMATCH) return lua_pushnil (L), 1; else return generate_error (L, ud, res); } }
char *malloc(unsigned n) { return(Lmalloc(n)); }