]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/md5.c
lua: remove "bla ? true : false"
[vlc] / modules / misc / lua / libs / md5.c
1 /*****************************************************************************
2  * md5.c: md5 hashing functions
3  *****************************************************************************
4  * Copyright (C) 2010 Antoine Cellerier
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #ifndef  _GNU_SOURCE
28 #   define  _GNU_SOURCE
29 #endif
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
35 #include <vlc_common.h>
36 #include <vlc_md5.h>
37
38 #include <lua.h>        /* Low level lua C API */
39 #include <lauxlib.h>    /* Higher level C API */
40
41 #include "../vlc.h"
42 #include "../libs.h"
43
44 /*****************************************************************************
45  *
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 * );
51
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 },
57     { NULL, NULL }
58 };
59
60 static int vlclua_md5_create( lua_State *L )
61 {
62     if( lua_gettop( L ) )
63     {
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 );
67         struct md5_s md5;
68         InitMD5( &md5 );
69         AddMD5( &md5, psz_str, strlen( psz_str ) );
70         EndMD5( &md5 );
71         char *psz_hash = psz_md5_hash( &md5 );
72         lua_pushstring( L, psz_hash );
73         free( psz_hash );
74         return 1;
75     }
76
77     struct md5_s *p_md5 = lua_newuserdata( L, sizeof( struct md5_s ) );
78     InitMD5( p_md5 );
79
80     if( luaL_newmetatable( L, "md5" ) )
81     {
82         lua_newtable( L );
83         luaL_register( L, NULL, vlclua_md5_reg );
84         lua_setfield( L, -2, "__index" );
85     }
86
87     lua_setmetatable( L, -2 );
88     return 1;
89 }
90
91 static int vlclua_md5_add( lua_State *L )
92 {
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" );
96     size_t i_len = 0;
97     const char *psz_str = lua_tolstring( L, 2, &i_len );
98     if( !psz_str )
99         vlclua_error( L );
100
101     AddMD5( p_md5, psz_str, i_len );
102
103     return 0;
104 }
105
106 static int vlclua_md5_end( lua_State *L )
107 {
108     struct md5_s *p_md5 = (struct md5_s *)luaL_checkudata( L, 1, "md5" );
109     EndMD5( p_md5 );
110     return 0;
111 }
112
113 static int vlclua_md5_hash( lua_State *L )
114 {
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 );
118     free( psz_hash );
119     return 1;
120 }
121
122 void luaopen_md5( lua_State *L )
123 {
124     lua_pushcfunction( L, vlclua_md5_create );
125     lua_setfield( L, -2, "md5" );
126 }