mp4: check STTS size before allocation
[vlc.git] / src / win32 / plugin.c
blob1a65521fca75be8ae14374308a01ef749e1b0b92
1 /*****************************************************************************
2 * plugin.c : Low-level dynamic library handling
3 *****************************************************************************
4 * Copyright (C) 2001-2011 VLC authors and VideoLAN
6 * Authors: Sam Hocevar <sam@zoy.org>
7 * Ethan C. Baldridge <BaldridgeE@cadmus.com>
8 * Hans-Peter Jansen <hpj@urpla.net>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_charset.h>
32 #include "modules/modules.h"
33 #include <windows.h>
34 #include <wchar.h>
36 extern DWORD LoadLibraryFlags;
38 #if (_WIN32_WINNT < _WIN32_WINNT_WIN7)
39 static BOOL WINAPI SetThreadErrorModeFallback(DWORD mode, DWORD *oldmode)
41 /* TODO: cache the pointer */
42 HANDLE h = GetModuleHandle(_T("kernel32.dll"));
43 if (unlikely(h == NULL))
44 return FALSE;
46 BOOL (WINAPI *SetThreadErrorModeReal)(DWORD, DWORD *);
48 SetThreadErrorModeReal = GetProcAddress(h, "SetThreadErrorMode");
49 if (SetThreadErrorModeReal != NULL)
50 return SetThreadErrorModeReal(mode, oldmode);
52 # if (_WIN32_WINNT < _WIN32_WINNT_VISTA)
53 /* As per libvlc_new() documentation, the calling process is responsible
54 * for setting a proper error mode on Windows 2008 and earlier versions.
55 * This is only a sanity check. */
56 UINT (WINAPI *GetErrorModeReal)(void);
57 DWORD curmode = 0;
59 GetErrorModeReal = (void *)GetProcAddress(h, "GetErrorMode");
60 if (GetErrorModeReal != NULL)
61 curmode = GetErrorModeReal();
62 else
63 curmode = SEM_FAILCRITICALERRORS;
64 # else
65 DWORD curmode = GetErrorMode();
66 # endif
67 /* Extra flags should be OK. Missing flags are NOT OK. */
68 if ((mode & curmode) != mode)
69 return FALSE;
70 if (oldmode != NULL)
71 *oldmode = curmode;
72 return TRUE;
74 # define SetThreadErrorMode SetThreadErrorModeFallback
75 #endif
77 static char *GetWindowsError( void )
79 wchar_t wmsg[256];
80 int i = 0, i_error = GetLastError();
82 FormatMessageW( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
83 NULL, i_error, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT),
84 wmsg, 256, NULL );
86 /* Go to the end of the string */
87 while( !wmemchr( L"\r\n\0", wmsg[i], 3 ) )
88 i++;
90 snwprintf( wmsg + i, 256 - i, L" (error %i)", i_error );
91 return FromWide( wmsg );
94 int module_Load( vlc_object_t *p_this, const char *psz_file,
95 module_handle_t *p_handle, bool lazy )
97 wchar_t *wfile = ToWide (psz_file);
98 if (wfile == NULL)
99 return -1;
101 module_handle_t handle = NULL;
102 #if !VLC_WINSTORE_APP
103 DWORD mode;
104 if (SetThreadErrorMode (SEM_FAILCRITICALERRORS, &mode) != 0)
106 handle = LoadLibraryExW (wfile, NULL, LoadLibraryFlags );
107 SetThreadErrorMode (mode, NULL);
109 #else
110 handle = LoadPackagedLibrary( wfile, 0 );
111 #endif
112 free (wfile);
114 if( handle == NULL )
116 char *psz_err = GetWindowsError();
117 msg_Warn( p_this, "cannot load module `%s' (%s)", psz_file, psz_err );
118 free( psz_err );
119 return -1;
122 *p_handle = handle;
123 (void) lazy;
124 return 0;
127 void module_Unload( module_handle_t handle )
129 FreeLibrary( handle );
132 void *module_Lookup( module_handle_t handle, const char *psz_function )
134 return (void *)GetProcAddress( handle, (char *)psz_function );