1 /*
2 ** Copyright (C) 2006-2017 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** This program is free software; you can redistribute it and/or modify
5 ** it under the terms of the GNU General Public License as published by
6 ** the Free Software Foundation; either version 2 of the License, or
7 ** (at your option) any later version.
8 **
9 ** This program is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ** GNU General Public License for more details.
13 **
14 ** You should have received a copy of the GNU General Public License
15 ** along with this program; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 #include "sfconfig.h"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27
28 #if (HAVE_DECL_S_IRGRP == 0)
29 #include <sf_unistd.h>
30 #endif
31
32 #include <string.h>
33 #include <fcntl.h>
34 #ifdef HAVE_DIRECT_H
35 #include <direct.h>
36 #endif
37 #include <sys/types.h>
38
39 #include "utils.h"
40
41 #if (defined (WIN32) || defined (_WIN32) || defined (__CYGWIN__))
42 #define TEST_WIN32 1
43 #else
44 #define TEST_WIN32 0
45 #endif
46
47 #if TEST_WIN32
48 #include <windows.h>
49
50
51 static const char * locations [] =
52 { ".", "../src/", "src/", "../src/.libs/", "src/.libs/",
53 NULL
54 } ; /* locations. */
55
56 static int
test_ordinal(HMODULE hmod,const char * func_name,int ordinal)57 test_ordinal (HMODULE hmod, const char * func_name, int ordinal)
58 { char *lpmsg ;
59 void *name, *ord ;
60
61 print_test_name ("win32_ordinal_test", func_name) ;
62
63 #if SIZEOF_VOIDP == 8
64 #define LPCSTR_OF_ORDINAL(x) ((LPCSTR) ((int64_t) (x)))
65 #else
66 #define LPCSTR_OF_ORDINAL(x) ((LPCSTR) (x))
67 #endif
68
69 ord = GetProcAddress (hmod, LPCSTR_OF_ORDINAL (ordinal)) ;
70 if ((name = GetProcAddress (hmod, func_name)) == NULL)
71 { FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError (),
72 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) &lpmsg, 0, NULL) ;
73 /*-puts (lpmsg) ;-*/
74 } ;
75
76 if (name != NULL && ord != NULL && name == ord)
77 { puts ("ok") ;
78 return 0 ;
79 } ;
80
81 puts ("fail") ;
82 return 1 ;
83 } /* test_ordinal */
84
85 static void
win32_ordinal_test(void)86 win32_ordinal_test (void)
87 { static char buffer [1024] ;
88 static char func_name [1024] ;
89 HMODULE hmod = NULL ;
90 FILE * file = NULL ;
91 int k, ordinal, errors = 0 ;
92
93 for (k = 0 ; locations [k] != NULL ; k++)
94 { snprintf (buffer, sizeof (buffer), "%s/libsndfile-1.def", locations [k]) ;
95 if ((file = fopen (buffer, "r")) != NULL)
96 break ;
97 } ;
98
99 if (file == NULL)
100 { puts ("\n\nError : cannot open DEF file.\n") ;
101 exit (1) ;
102 } ;
103
104 for (k = 0 ; locations [k] != NULL ; k++)
105 { snprintf (buffer, sizeof (buffer), "%s/libsndfile-1.dll", locations [k]) ;
106 if ((hmod = (HMODULE) LoadLibrary (buffer)) != NULL)
107 break ;
108 } ;
109
110 if (hmod == NULL)
111 { printf ("\n\nError : cannot load DLL (cwd is %s).\n", getcwd (buffer, sizeof (buffer))) ;
112 exit (1) ;
113 } ;
114
115 while (fgets (buffer, sizeof (buffer), file) != NULL)
116 { func_name [0] = 0 ;
117 ordinal = 0 ;
118
119 if (sscanf (buffer, "%s @%d", func_name, &ordinal) != 2)
120 continue ;
121
122 errors += test_ordinal (hmod, func_name, ordinal) ;
123 } ;
124
125 FreeLibrary (hmod) ;
126
127 fclose (file) ;
128
129 if (errors > 0)
130 { printf ("\n\nErrors : %d\n\n", errors) ;
131 exit (1) ;
132 } ;
133
134 return ;
135 } /* win32_ordinal_test */
136
137 #endif
138
139 int
main(void)140 main (void)
141 {
142 #if (TEST_WIN32 && WIN32_TARGET_DLL)
143 win32_ordinal_test () ;
144 #endif
145
146 return 0 ;
147 } /* main */
148
149