[vlc-devel] [PATCH 2/3] lua: Expose OpenSubtitles hash function

Rémi Denis-Courmont remi at remlab.net
Mon Apr 20 19:42:42 CEST 2015


Le lundi 20 avril 2015, 18:59:56 Hugo Beauzée-Luyssen a écrit :
> ---
>  modules/lua/Makefile.am |  1 +
>  modules/lua/extension.c |  1 +
>  modules/lua/intf.c      |  1 +
>  modules/lua/libs.h      |  1 +
>  modules/lua/libs/hash.c | 62
> +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 66
> insertions(+)
>  create mode 100644 modules/lua/libs/hash.c
> 
> diff --git a/modules/lua/Makefile.am b/modules/lua/Makefile.am
> index 38a1b8d..00df429 100644
> --- a/modules/lua/Makefile.am
> +++ b/modules/lua/Makefile.am
> @@ -13,6 +13,7 @@ liblua_plugin_la_SOURCES = \
>  	lua/libs/equalizer.c \
>  	lua/libs/gettext.c \
>  	lua/libs/dialog.c \
> +	lua/libs/hash.c \
>  	lua/libs/httpd.c \
>  	lua/libs/input.c lua/libs/input.h \
>  	lua/libs/messages.c \
> diff --git a/modules/lua/extension.c b/modules/lua/extension.c
> index 5958af1..67c7cef 100644
> --- a/modules/lua/extension.c
> +++ b/modules/lua/extension.c
> @@ -843,6 +843,7 @@ static lua_State* GetLuaState( extensions_manager_t
> *p_mgr, luaopen_vlm( L );
>              luaopen_volume( L );
>              luaopen_xml( L );
> +            luaopen_hash( L );
>  #if defined(_WIN32) && !VLC_WINSTORE_APP
>              luaopen_win( L );
>  #endif
> diff --git a/modules/lua/intf.c b/modules/lua/intf.c
> index 841587f..9d124d2 100644
> --- a/modules/lua/intf.c
> +++ b/modules/lua/intf.c
> @@ -271,6 +271,7 @@ static int Start_LuaIntf( vlc_object_t *p_this, const
> char *name ) luaopen_gettext( L );
>      luaopen_xml( L );
>      luaopen_equalizer( L );
> +    luaopen_hash( L );
>  #if defined(_WIN32) && !VLC_WINSTORE_APP
>      luaopen_win( L );
>  #endif
> diff --git a/modules/lua/libs.h b/modules/lua/libs.h
> index 861cba9..e924eec 100644
> --- a/modules/lua/libs.h
> +++ b/modules/lua/libs.h
> @@ -45,6 +45,7 @@ void luaopen_gettext( lua_State * );
>  void luaopen_input_item( lua_State *L, input_item_t *item );
>  void luaopen_xml( lua_State *L );
>  void luaopen_equalizer( lua_State *L );
> +void luaopen_hash( lua_State* L );
>  #ifdef _WIN32
>  void luaopen_win( lua_State *L );
>  #endif
> diff --git a/modules/lua/libs/hash.c b/modules/lua/libs/hash.c
> new file mode 100644
> index 0000000..c1acc2b
> --- /dev/null
> +++ b/modules/lua/libs/hash.c
> @@ -0,0 +1,62 @@
> +/**************************************************************************
> *** + * hash.c: Expose hash functions to lua extensions
> +
> ***************************************************************************
> ** + * Copyright (C) 2003-2015 VLC authors and VideoLAN
> + *
> + * Authors: Hugo Beauzée-Luyssen <hugo at beauzee.fr>
> + *
> + * This program is free software; you can redistribute it and/or modify it
> + * under the terms of the GNU Lesser General Public License as published by
> + * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser 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. +
> ***************************************************************************
> **/ +
> +#ifdef HAVE_CONFIG_H
> +# include "config.h"
> +#endif
> +
> +#include <vlc_hash.h>
> +
> +#include "../vlc.h"
> +#include "../libs.h"
> +
> +static int vlclua_hash_OpenSubtitles( lua_State* L )
> +{
> +    vlc_object_t * p_this = vlclua_get_this( L );
> +    const char* psz_uri = luaL_checkstring( L, 1 );
> +    if ( p_this == NULL || psz_uri == NULL )
> +    {
> +        lua_pushnil( L );
> +        lua_pushnumber( L, 0 );
> +        return 2;
> +    }
> +    int64_t size = 0;
> +    uint64_t hash = hash_OpenSubtitles( p_this, psz_uri, &size );

Unless I missed the cleverly hidden way to interrupt the function call when 
the Lua interface (thread) is told to quit, this call can lock up for whatever 
long, including infinite, depending on the URL, eventually freezing the VLC 
process.

> +    char res[32];
> +    snprintf( res, 32, "%"PRIx64, hash );

So you did not even bother to count the correct size. You know you are 
supposed to think about the code and review your patches before you post them?

I am not amused.

> +    lua_pushstring( L, res );
> +    lua_pushnumber( L, size );
> +    return 2;
> +}
> +
> +static const luaL_Reg vlclua_hash_reg[] = {
> +    { "open_subtitles", vlclua_hash_OpenSubtitles },
> +
> +    { NULL, NULL }
> +};
> +
> +void luaopen_hash( lua_State* L )
> +{
> +    lua_newtable( L );
> +    luaL_register( L, NULL, vlclua_hash_reg );
> +    lua_setfield( L, -2, "hash" );
> +}

-- 
Rémi Denis-Courmont
http://www.remlab.net/




More information about the vlc-devel mailing list