1 /*****************************************************************************
2 * rand.c: random number/bytes generation functions
3 *****************************************************************************
4 * Copyright (C) 2007-2018 the VideoLAN team
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19 *****************************************************************************/
25 #include <vlc_common.h>
31 static int vlclua_rand_number( lua_State
*L
)
33 long rand
= vlc_lrand48();
34 lua_pushnumber( L
, rand
);
38 static int vlclua_rand_bytes( lua_State
*L
)
40 lua_Integer i_size
= luaL_checkinteger( L
, 1 );
41 char* p_buff
= malloc( i_size
* sizeof( *p_buff
) );
42 if ( unlikely( p_buff
== NULL
) )
43 return vlclua_error( L
);
44 vlc_rand_bytes( p_buff
, i_size
);
45 lua_pushlstring( L
, p_buff
, i_size
);
50 static const luaL_Reg vlclua_rand_reg
[] = {
51 { "number", vlclua_rand_number
},
52 { "bytes", vlclua_rand_bytes
},
57 void luaopen_rand( lua_State
*L
)
60 luaL_register( L
, NULL
, vlclua_rand_reg
);
61 lua_setfield( L
, -2, "rand" );