1 /*****************************************************************************
2 * md5.c: md5 hashing functions
3 *****************************************************************************
4 * Copyright (C) 2010 Antoine Cellerier
7 * Authors: Antoine Cellerier <dionoea at videolan tod org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
35 #include <vlc_common.h>
38 #include <lua.h> /* Low level lua C API */
39 #include <lauxlib.h> /* Higher level C API */
44 /*****************************************************************************
46 *****************************************************************************/
47 static int vlclua_md5_create( lua_State * );
48 static int vlclua_md5_add( lua_State * );
49 static int vlclua_md5_end( lua_State * );
50 static int vlclua_md5_hash( lua_State * );
52 static const luaL_Reg vlclua_md5_reg[] = {
53 { "add", vlclua_md5_add },
54 { "end", vlclua_md5_end },
55 { "end_", vlclua_md5_end },
56 { "hash", vlclua_md5_hash },
60 static int vlclua_md5_create( lua_State *L )
64 /* If optional first argument is given, and it's a string, just
65 * return the string's hash */
66 const char *psz_str = luaL_checkstring( L, 1 );
69 AddMD5( &md5, psz_str, strlen( psz_str ) );
71 char *psz_hash = psz_md5_hash( &md5 );
72 lua_pushstring( L, psz_hash );
77 struct md5_s *p_md5 = lua_newuserdata( L, sizeof( struct md5_s ) );
80 if( luaL_newmetatable( L, "md5" ) )
83 luaL_register( L, NULL, vlclua_md5_reg );
84 lua_setfield( L, -2, "__index" );
87 lua_setmetatable( L, -2 );
91 static int vlclua_md5_add( lua_State *L )
93 struct md5_s *p_md5 = (struct md5_s *)luaL_checkudata( L, 1, "md5" );
94 if( !lua_isstring( L, 2 ) )
95 luaL_error( L, "2nd argument should be a string" );
97 const char *psz_str = lua_tolstring( L, 2, &i_len );
101 AddMD5( p_md5, psz_str, i_len );
106 static int vlclua_md5_end( lua_State *L )
108 struct md5_s *p_md5 = (struct md5_s *)luaL_checkudata( L, 1, "md5" );
113 static int vlclua_md5_hash( lua_State *L )
115 struct md5_s *p_md5 = (struct md5_s *)luaL_checkudata( L, 1, "md5" );
116 char *psz_hash = psz_md5_hash( p_md5 );
117 lua_pushstring( L, psz_hash );
122 void luaopen_md5( lua_State *L )
124 lua_pushcfunction( L, vlclua_md5_create );
125 lua_setfield( L, -2, "md5" );