[vlc-devel] [PATCH 1/3] Add an Open Subtitles hash function
Hugo Beauzée-Luyssen
hugo at beauzee.fr
Mon Apr 20 18:59:55 CEST 2015
---
include/vlc_hash.h | 31 +++++++++++++++++++++
src/Makefile.am | 2 ++
src/libvlccore.sym | 1 +
src/misc/hash.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 113 insertions(+)
create mode 100644 include/vlc_hash.h
create mode 100644 src/misc/hash.c
diff --git a/include/vlc_hash.h b/include/vlc_hash.h
new file mode 100644
index 0000000..5cf8b13
--- /dev/null
+++ b/include/vlc_hash.h
@@ -0,0 +1,31 @@
+/*****************************************************************************
+ * vlc_hash.h: Various hash generation functions
+ *****************************************************************************
+ * 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.
+ *****************************************************************************/
+
+#ifndef VLC_HASH_H
+#define VLC_HASH_H
+
+#include <vlc_common.h>
+
+VLC_API uint64_t hash_OpenSubtitles( vlc_object_t* p_obj, const char* psz_uri, int64_t *p_size );
+
+#endif // VLC_HASH_H
+
diff --git a/src/Makefile.am b/src/Makefile.am
index 1f02494..f374f4f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -50,6 +50,7 @@ pluginsinclude_HEADERS = \
../include/vlc_fs.h \
../include/vlc_gcrypt.h \
../include/vlc_opengl.h \
+ ../include/vlc_hash.h \
../include/vlc_http.h \
../include/vlc_httpd.h \
../include/vlc_image.h \
@@ -458,6 +459,7 @@ SOURCES_libvlc_common = \
misc/cpu.c \
misc/epg.c \
misc/exit.c \
+ misc/hash.c \
config/configuration.h \
config/core.c \
config/chain.c \
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index e89cf4e..eeb66f3 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -663,3 +663,4 @@ addons_manager_Remove
addon_entry_New
addon_entry_Hold
addon_entry_Release
+hash_OpenSubtitles
diff --git a/src/misc/hash.c b/src/misc/hash.c
new file mode 100644
index 0000000..cc5370a
--- /dev/null
+++ b/src/misc/hash.c
@@ -0,0 +1,79 @@
+/*****************************************************************************
+ * hash.c: Various hash generation functions
+ *****************************************************************************
+ * 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_fs.h>
+#include <vlc_stream.h>
+#include <vlc_input_item.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+
+uint64_t hash_OpenSubtitles( vlc_object_t* p_obj, const char* psz_uri, int64_t* p_size )
+{
+ uint64_t hash = 0;
+ int64_t size = 0;
+
+ stream_t* p_stream = stream_UrlNew( p_obj, psz_uri );
+ if ( p_stream == NULL )
+ return 0;
+
+ bool b_can_seek;
+ stream_Control( p_stream, STREAM_CAN_SEEK, &b_can_seek );
+ if ( b_can_seek == false )
+ goto bailout;
+
+ size = stream_Size( p_stream );
+ uint64_t tmp = 0;
+ hash = size;
+
+ for( size_t i = 0; i < 65536 / sizeof(tmp); i++)
+ {
+ if ( stream_Read( p_stream, &tmp, sizeof(tmp) ) <= 0 )
+ {
+ goto bailout;
+ }
+ hash += tmp;
+ }
+
+ if ( stream_Seek( p_stream, size - 65536 ) != VLC_SUCCESS )
+ goto bailout;
+
+ for( size_t i = 0; i < 65536 / sizeof(tmp); i++)
+ {
+ if ( stream_Read( p_stream, &tmp, sizeof(tmp) ) <= 0 )
+ {
+ goto bailout;
+ }
+ hash += tmp;
+ }
+
+bailout:
+ stream_Delete( p_stream );
+ *p_size = size;
+ return hash;
+}
--
2.1.0
More information about the vlc-devel
mailing list