Select Git revision
aes256-set-encrypt-key.c
Forked from
Nettle / nettle
Source project has a limited visibility.
simulate.lpc 6.45 KiB
inherit "/precompiled/file";
inherit "/precompiled/regexp";
#define error(X) throw( ({ (X), backtrace()[0..sizeof(backtrace())-2] }) )
string current_file, current_mode;
string read_bytes(string filename,void|int start,void|int len)
{
string ret;
if(!open(filename,"r"))
error("Couldn't open file "+filename+".\n");
switch(query_num_arg())
{
case 1:
case 2:
len=0x7fffffff;
case 3:
seek(start);
}
ret=read(len);
close();
return ret;
}
int write_file(string filename,string what)
{
int ret;
if(!open(filename,"awc"))
error("Couldn't open file "+filename+".\n");
ret=write(what);
close();
return ret;
}
mixed *map_array(mixed *arr, mixed fun, mixed ... args)
{
int e;
mixed *ret;
if(intp(fun))
return arr(@args);
if(stringp(fun))
return column(arr, fun)(@args);
if(functionp(fun))
{
ret=allocate(sizeof(arr));
for(e=0;e<sizeof(arr);e++)
ret[e]=fun(arr[e],@args);
return ret;
}
error("Bad argument 2 to map_array().\n");
}
mixed *filter_array(mixed *arr, mixed fun, mixed ... args)
{
int e,d;
mixed *ret;
ret=map_array(arr,fun,@args);
for(e=0;e<sizeof(arr);e++)
if(ret[e])
ret[d++]=arr[e];
return ret[0..d-1];
}
int search_array(mixed *arr, mixed fun, mixed ... args)
{
int e;
if(stringp(fun))
{
for(e=0;e<sizeof(arr);e++)
if(arr[e][fun](@args))
return e;
return -1;
}
else if(functionp(fun))
{
for(e=0;e<sizeof(arr);e++)
if(fun(arr[e],@args))
return e;
return -1;
}
else if(intp(fun))
{
for(e=0;e<sizeof(arr);e++)
if(arr[e](@args))
return e;
return -1;
}
else
{
error("Bad argument 2 to filter_array().\n");
}
}
varargs int exec(string file,string ... foo)
{
string path;
if(search(file,"/"))
return exece(combine_path(getcwd(),file),foo,getenv());
path=getenv("PATH");
foreach(path/":",path)
if(file_stat(path=combine_path(path,file)))
return exece(path, foo,getenv());
return 69;
}
varargs int spawn(string s,object stdin,object stdout,object stderr)
{
object p;
int pid;
string t;
if(pid=fork())
{
return pid;
}else{
program f=(program)"/precompiled/file";
if(stdin)
stdin->dup2(clone(f,"stdin"));
if(stdout)
stdout->dup2(clone(f,"stdout"));
if(stderr)
stderr->dup2(clone(f,"stderr"));
exec("/bin/sh","-c",s);
exit(69);
}
}
string popen(string s)
{
object p;
string t;
current_file=current_mode=0;
p=file::pipe();
if(!p) error("Popen failed. (couldn't create pipe)\n");
spawn(s,0,p,0);
destruct(p);
t=read(0x7fffffff);
if(!t)
{
int e;
e=errno();
close();
error("Popen failed with error "+e+".\n");
}else{
close();
}
return t;
}
void system(string s)
{
object p;
int pid;
string t;
current_file=current_mode=0;
p=file::pipe();
if(!p) error("System() failed.\n");
p->set_close_on_exec(0);
if(pid=fork())
{
destruct(p);
/* Nothing will ever be written here, we are just waiting for it
* to close
*/
file::read(1);
}else{
exec("/bin/sh","-c",s);
exit(69);
}
}
int file_size(string s)
{
int *stat;
stat=file_stat(s);
if(!stat) return -1;
return stat[1];
}
varargs string code_value(mixed s,int a)
{
s=sprintf("%O",s);
if(a) s=replace(s,({"\n","\"","\\"}),({"\\n","\\\"","\\\\"}));
return s;
}
mixed *sum_arrays(function foo, mixed * ... args)
{
mixed *ret;
int e,d;
ret=allocate(sizeof(args[0]));
for(e=0;e<sizeof(args[0]);e++)
ret[e]=foo(@ column(args, e));
return ret;
}
varargs int member_array(mixed needle,mixed *haystack,int start)
{
return search(haystack,needle,start);
}
object previous_object()
{
int e;
mixed **trace;
object o,ret;
trace=backtrace();
o=function_object(trace[-2][2]);
for(e=sizeof(trace)-3;e>=0;e--)
{
if(!trace[1][2]) continue;
ret=function_object(trace[1][2]);
if(o!=ret) return ret;
}
return 0;
}
function this_function()
{
return backtrace()[-2][2];
}
varargs mixed *sort_array(array foo,function cmp, mixed ... args)
{
array bar,tmp;
int len,start;
int length;
int foop, fooend, barp, barend;
if(!cmp || cmp==`>)
{
foo+=({});
sort(foo);
return foo;
}
if(cmp == `<)
{
foo+=({});
sort(foo);
reverse(foo);
return foo;
}
length=sizeof(foo);
foo+=({});
bar=allocate(length);
for(len=1;len<length;len*=2)
{
start=0;
while(start+len < length)
{
foop=start;
barp=start+len;
fooend=barp;
barend=barp+len;
if(barend > length) barend=length;
while(1)
{
if(cmp(foo[foop],foo[barp],@args) <= 0)
{
bar[start++]=foo[foop++];
if(foop == fooend)
{
while(barp < barend) bar[start++]=foo[barp++];
break;
}
}else{
bar[start++]=foo[barp++];
if(barp == barend)
{
while(foop < fooend) bar[start++]=foo[foop++];
break;
}
}
}
}
while(start < length) bar[start]=foo[start++];
tmp=foo;
foo=bar;
bar=tmp;
}
return foo;
}
string capitalize(string s)
{
return upper_case(s[0..0])+s[1..sizeof(s)];
}
function get_function(object o, string a)
{
mixed ret;
ret=o[a];
return functionp(ret) ? ret : 0;
}
string *regexp(string *s, string reg)
{
regexp::create(reg);
s=filter_array(s,match);
regexp::create(); /* Free compiled regexp */
return s;
}
list mklist(mixed *a)
{
return aggregate_list(@a);
}
void create()
{
add_efun("PI",3.1415926535897932384626433832795080);
add_efun("capitalize",capitalize);
add_efun("code_value",code_value);
add_efun("exec","exec");
add_efun("explode",`/);
add_efun("file_size",file_size);
add_efun("filter_array",filter_array);
add_efun("get_function",get_function);
add_efun("implode",`*);
add_efun("l_sizeof",sizeof);
add_efun("m_indices",indices);
add_efun("m_sizeof",sizeof);
add_efun("m_values",values);
add_efun("map_array",map_array);
add_efun("member_array",member_array);
add_efun("mklist",mklist);
add_efun("popen",popen);
add_efun("previous_object",previous_object);
add_efun("read_bytes",read_bytes);
add_efun("regexp",regexp);
add_efun("search_array",search_array);
add_efun("sort_array",sort_array);
add_efun("spawn",spawn);
add_efun("strlen",sizeof);
add_efun("strstr",search);
add_efun("sum",`+);
add_efun("sum_arrays",sum_arrays);
add_efun("system",system);
add_efun("this_function",this_function);
add_efun("version",lambda() { return "uLPC v1.1E-6"; });
add_efun("write_file",write_file);
}