]> git.sesse.net Git - vlc/commitdiff
Export VLC's md5 API.
authorAntoine Cellerier <dionoea@videolan.org>
Sat, 20 Feb 2010 20:19:01 +0000 (21:19 +0100)
committerAntoine Cellerier <dionoea@videolan.org>
Sat, 20 Feb 2010 20:19:37 +0000 (21:19 +0100)
modules/misc/lua/Modules.am
modules/misc/lua/demux.c
modules/misc/lua/extension.c
modules/misc/lua/intf.c
modules/misc/lua/libs.h
modules/misc/lua/libs/md5.c [new file with mode: 0644]
modules/misc/lua/meta.c
modules/misc/lua/services_discovery.c
share/lua/README.txt

index c2105c7f1dd4419c594cdb661b5e3b9c794c2585..d715ef979875a3703e50e9c81cbe86f3e789a6af 100644 (file)
@@ -16,6 +16,7 @@ SOURCES_lua = \
        libs/httpd.c \
        libs/input.c \
        libs/input.h \
+       libs/md5.c \
        libs/messages.c \
        libs/misc.c \
        libs/misc.h \
index 1ffc049347a7f4d4f71a478004b63ded3882904f..e201024bb5e3a2032fd8efd1242bda796000627b 100644 (file)
@@ -139,6 +139,7 @@ static int probe_luascript( vlc_object_t *p_this, const char * psz_filename,
     luaopen_msg( L );
     luaopen_strings( L );
     luaopen_xml( L );
+    luaopen_md5( L );
     lua_pushlightuserdata( L, p_demux );
     lua_setfield( L, -2, "private" );
     lua_pushstring( L, p_demux->psz_path );
index 16c363f6e8cce1939d0d611b9e44ab82c92fc35c..a6fa103359977b1bb343dd98eca900f308ba163e 100644 (file)
@@ -736,6 +736,7 @@ static lua_State* GetLuaState( extensions_manager_t *p_mgr,
             luaopen_config( L );
             luaopen_dialog( L, p_ext );
             luaopen_input( L );
+            luaopen_md5( L );
             luaopen_msg( L );
             luaopen_misc( L );
             luaopen_net( L );
index 4a4de3baeaaacfea6c6c797e354afa9199a49089..90bb782005569c37d42f6ff963441f22f70e40d6 100644 (file)
@@ -198,6 +198,7 @@ int Open_LuaIntf( vlc_object_t *p_this )
     luaopen_volume( L );
     luaopen_gettext( L );
     luaopen_xml( L );
+    luaopen_md5( L );
 
     /* clean up */
     lua_pop( L, 1 );
index 18f884ec139e15ea695989247ea6f194903bd96b..8bca89c41d6f6bd8dff382567a14d51c061c10c1 100644 (file)
@@ -45,5 +45,6 @@ void luaopen_volume( lua_State * );
 void luaopen_gettext( lua_State * );
 void luaopen_input_item( lua_State *L, input_item_t *item );
 void luaopen_xml( lua_State *L );
+void luaopen_md5( lua_State *L );
 
 #endif
diff --git a/modules/misc/lua/libs/md5.c b/modules/misc/lua/libs/md5.c
new file mode 100644 (file)
index 0000000..1531a20
--- /dev/null
@@ -0,0 +1,126 @@
+/*****************************************************************************
+ * md5.c: md5 hashing functions
+ *****************************************************************************
+ * Copyright (C) 2010 Antoine Cellerier
+ * $Id$
+ *
+ * Authors: Antoine Cellerier <dionoea at videolan tod org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+#ifndef  _GNU_SOURCE
+#   define  _GNU_SOURCE
+#endif
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_md5.h>
+
+#include <lua.h>        /* Low level lua C API */
+#include <lauxlib.h>    /* Higher level C API */
+
+#include "../vlc.h"
+#include "../libs.h"
+
+/*****************************************************************************
+ *
+ *****************************************************************************/
+static int vlclua_md5_create( lua_State * );
+static int vlclua_md5_add( lua_State * );
+static int vlclua_md5_end( lua_State * );
+static int vlclua_md5_hash( lua_State * );
+
+static const luaL_Reg vlclua_md5_reg[] = {
+    { "add", vlclua_md5_add },
+    { "end", vlclua_md5_end },
+    { "end_", vlclua_md5_end },
+    { "hash", vlclua_md5_hash },
+    { NULL, NULL }
+};
+
+static int vlclua_md5_create( lua_State *L )
+{
+    if( lua_gettop( L ) )
+    {
+        /* If optional first argument is given, and it's a string, just
+         * return the string's hash */
+        const char *psz_str = luaL_checkstring( L, 1 );
+        struct md5_s md5;
+        InitMD5( &md5 );
+        AddMD5( &md5, psz_str, strlen( psz_str ) );
+        EndMD5( &md5 );
+        char *psz_hash = psz_md5_hash( &md5 );
+        lua_pushstring( L, psz_hash );
+        free( psz_hash );
+        return 1;
+    }
+
+    struct md5_s *p_md5 = lua_newuserdata( L, sizeof( struct md5_s ) );
+    InitMD5( p_md5 );
+
+    if( luaL_newmetatable( L, "md5" ) )
+    {
+        lua_newtable( L );
+        luaL_register( L, NULL, vlclua_md5_reg );
+        lua_setfield( L, -2, "__index" );
+    }
+
+    lua_setmetatable( L, -2 );
+    return 1;
+}
+
+static int vlclua_md5_add( lua_State *L )
+{
+    struct md5_s *p_md5 = (struct md5_s *)luaL_checkudata( L, 1, "md5" );
+    if( !lua_isstring( L, 2 ) )
+        luaL_error( L, "2nd argument should be a string" );
+    size_t i_len = 0;
+    const char *psz_str = lua_tolstring( L, 2, &i_len );
+    if( !psz_str )
+        vlclua_error( L );
+
+    AddMD5( p_md5, psz_str, i_len );
+
+    return 0;
+}
+
+static int vlclua_md5_end( lua_State *L )
+{
+    struct md5_s *p_md5 = (struct md5_s *)luaL_checkudata( L, 1, "md5" );
+    EndMD5( p_md5 );
+    return 0;
+}
+
+static int vlclua_md5_hash( lua_State *L )
+{
+    struct md5_s *p_md5 = (struct md5_s *)luaL_checkudata( L, 1, "md5" );
+    char *psz_hash = psz_md5_hash( p_md5 );
+    lua_pushstring( L, psz_hash );
+    free( psz_hash );
+    return 1;
+}
+
+void luaopen_md5( lua_State *L )
+{
+    lua_pushcfunction( L, vlclua_md5_create );
+    lua_setfield( L, -2, "md5" );
+}
index 0cd6e860d4e50befb4d53e9de26e061a82ac715c..200ae2f6cca1fc27e1d536559267e4168e8674aa 100644 (file)
@@ -72,6 +72,7 @@ static lua_State * init( vlc_object_t *p_this, input_item_t * p_item, const char
     luaopen_object( L );
     luaopen_misc( L );
     luaopen_xml( L );
+    luaopen_md5( L );
     luaopen_input_item( L, p_item );
 
     lua_pushlightuserdata( L, p_this );
index 9268303f63218cf1ab39997a36dfbdb5937c5c94..ebe248419113825534413a0b8486ec344c637811 100644 (file)
@@ -110,7 +110,9 @@ int Open_LuaSD( vlc_object_t *p_this )
     luaopen_stream( L );
     luaopen_gettext( L );
     luaopen_xml( L );
+    luaopen_md5( L );
     lua_pop( L, 1 );
+
     if( vlclua_add_modules_path( p_sd, L, p_sys->psz_filename ) )
     {
         msg_Warn( p_sd, "Error while setting the module search path for %s",
index 15d92aade7470898655810735c9f7bffc5048139..f7e6283edb527b78efc7d5758fbcf23e986f57aa 100644 (file)
@@ -130,6 +130,14 @@ input.item(): Get the current input item. Input item methods are:
     .played_abuffers
     .lost_abuffers
 
+MD5
+---
+md5( str ): return the string's hash
+md5(): create an md5 object with the following methods:
+  :add( str ): add a string to the hash
+  :end_(): finish hashing
+  :hash(): return the hash
+
 Messages
 --------
 msg.dbg( [str1, [str2, [...]]] ): Output debug messages (-vv).