/[mcrypt]/libmcrypt/lib/mcrypt_extra.c
ViewVC logotype

Annotation of /libmcrypt/lib/mcrypt_extra.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Thu Oct 12 21:06:31 2000 UTC (23 years, 6 months ago) by nmav
Branch: MAIN
Changes since 1.4: +7 -2 lines
File MIME type: text/plain
Added defines and some function emulation needed in WIN32. Thanks goes
to Dimitris Souflis (dsouflis@altera.gr)

1 nmav 1.1 /*
2     * Copyright (C) 1998,1999 Nikos Mavroyanopoulos
3     *
4     * This library is free software; you can redistribute it and/or modify it under the terms of the
5     * GNU Library General Public License as published by the Free Software
6     * Foundation; either version 2 of the License, or (at your option) any
7     * later version.
8     *
9     * This library 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 GNU
12     * Library General Public License for more details.
13     *
14     * You should have received a copy of the GNU Library General Public
15     * License along with this library; if not, write to the
16     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17     * Boston, MA 02111-1307, USA.
18     */
19    
20 nmav 1.5 /* $Id: mcrypt_extra.c,v 1.4 2000/09/18 09:42:54 nmav Exp $ */
21 nmav 1.1
22     #ifndef LIBDEFS_H
23     #define LIBDEFS_H
24     #include <libdefs.h>
25     #endif
26     #include <bzero.h>
27     #include <xmemory.h>
28     #include <mcrypt_internal.h>
29    
30     #if 0
31     extern int preloaded_symbols;
32 nmav 1.3 #endif
33 nmav 1.1
34 nmav 1.3 int mcrypt_algorithm_module_ok(const char *file, const char *directory);
35     int mcrypt_mode_module_ok(const char *file, const char *directory);
36 nmav 1.2 #ifndef USE_LTDL
37 nmav 1.3 void *mcrypt_dlopen_ext(const char *filename);
38 nmav 1.2 #endif
39 nmav 1.3 void *mcrypt_dlopen ( const char* a_directory, const char *m_directory, const char *filename);
40    
41 nmav 1.5 WIN32DLL_DEFINE
42 nmav 1.3 struct dirent *mcrypt_readdir(DIR * dirstream)
43     {
44 nmav 1.2
45 nmav 1.3 struct dirent *result;
46     struct dirent *ret = NULL;
47 nmav 1.2
48     #ifdef HAVE_READDIR_R
49 nmav 1.3 struct dirent ret2[sizeof(struct dirent)];
50 nmav 1.2
51 nmav 1.3 readdir_r(dirstream, ret2, &ret);
52 nmav 1.2 #else
53 nmav 1.3 ret = readdir(dirstream);
54 nmav 1.2 #endif
55 nmav 1.3 if (ret != NULL) {
56     result = malloc(sizeof(struct dirent));
57     memmove(result, ret, sizeof(struct dirent));
58     } else {
59     result = NULL;
60     }
61    
62     return result;
63    
64 nmav 1.2 }
65 nmav 1.1
66 nmav 1.5 WIN32DLL_DEFINE
67 nmav 1.3 char **mcrypt_list_algorithms(char *libdir, int *size)
68 nmav 1.1 {
69     DIR *pdir;
70 nmav 1.2 char directory[512];
71 nmav 1.1 struct dirent *dirname;
72     char **filename = NULL, *ptr;
73     int tmpsize;
74    
75     *size = 0;
76    
77     if (libdir == NULL) {
78     strcpy(directory, LIBDIR);
79     } else {
80     strcpy(directory, libdir);
81     }
82    
83     pdir = opendir(directory);
84     if (pdir == NULL) {
85     fprintf(stderr, "Unable to open directory %s.\n",
86     directory);
87     return NULL;
88     }
89    
90     for (;;) {
91 nmav 1.2 dirname = mcrypt_readdir(pdir);
92 nmav 1.1 if (dirname != NULL) {
93     #ifdef DT_REG
94     if ((dirname->d_type == DT_REG)
95     || (dirname->d_type == DT_UNKNOWN)) {
96     #endif
97 nmav 1.3 tmpsize = strlen(dirname->d_name); /*(unsigned int)dirname->d_namlen;*/
98 nmav 1.1
99 nmav 1.3 if (tmpsize > 3) {
100     if (mcrypt_algorithm_module_ok(dirname->d_name, directory) > 0) {
101     filename = realloc(filename, (*size + 1) * sizeof(char *));
102     filename[*size] = calloc(1, tmpsize + 1);
103     strcpy(filename[*size], dirname->d_name);
104    
105     ptr = strrchr(filename[*size], '.');
106     if (ptr != NULL)
107     *ptr = '\0';
108     (*size)++;
109 nmav 1.1 }
110 nmav 1.3 }
111 nmav 1.1 #ifdef DT_REG
112     }
113     #endif
114 nmav 1.3 if (dirname != NULL)
115     free(dirname);
116 nmav 1.1 } else {
117 nmav 1.3 if (dirname != NULL)
118     free(dirname);
119 nmav 1.1 break;
120     }
121    
122     }
123    
124    
125 nmav 1.3 closedir(pdir);
126 nmav 1.1
127     return filename;
128    
129     }
130    
131 nmav 1.5 WIN32DLL_DEFINE
132 nmav 1.3 char **mcrypt_list_modes(char *libdir, int *size)
133 nmav 1.1 {
134     DIR *pdir;
135 nmav 1.2 char directory[512];
136 nmav 1.1 struct dirent *dirname;
137     char **filename = NULL, *ptr;
138     int tmpsize;
139    
140     if (libdir == NULL) {
141     strcpy(directory, LIBDIR);
142     } else {
143     strcpy(directory, libdir);
144     }
145    
146     pdir = opendir(directory);
147     if (pdir == NULL) {
148     fprintf(stderr, "Unable to open directory %s.\n",
149     directory);
150     return NULL;
151     }
152    
153     *size = 0;
154     for (;;) {
155    
156 nmav 1.2 dirname = mcrypt_readdir(pdir);
157 nmav 1.1 if (dirname != NULL) {
158     #ifdef DT_REG
159     if ((dirname->d_type == DT_REG)
160     || (dirname->d_type == DT_UNKNOWN)) {
161     #endif
162 nmav 1.2 tmpsize = strlen(dirname->d_name);
163     if (tmpsize > 3) {
164 nmav 1.3 if (mcrypt_mode_module_ok
165     (dirname->d_name,
166     directory) > 0) {
167 nmav 1.1 filename =
168     realloc(filename,
169     (*size +
170     1) *
171     sizeof(char
172     *));
173     filename[*size] =
174     calloc(1, tmpsize + 1);
175     strcpy(filename[*size],
176     dirname->d_name);
177 nmav 1.3 ptr =
178     strrchr(filename
179     [*size], '.');
180     if (ptr != NULL)
181     *ptr = '\0';
182 nmav 1.1 (*size)++;
183     }
184     }
185     #ifdef DT_REG
186     }
187     #endif
188 nmav 1.3 if (dirname != NULL)
189     free(dirname);
190 nmav 1.1 } else {
191 nmav 1.3 if (dirname != NULL)
192     free(dirname);
193 nmav 1.1 break;
194     }
195    
196     }
197    
198     closedir(pdir);
199     return filename;
200     }
201    
202 nmav 1.5 WIN32DLL_DEFINE
203 nmav 1.3 void mcrypt_free_p(char **p, int size)
204     {
205     int i;
206 nmav 1.1
207 nmav 1.3 for (i = 0; i < size; i++) {
208 nmav 1.1 free(p[i]);
209     }
210     free(p);
211     }
212    
213 nmav 1.5 WIN32DLL_DEFINE
214 nmav 1.3 int mcrypt_algorithm_module_ok(const char *file, const char *directory)
215 nmav 1.1 {
216     word32 ret;
217     lt_dlhandle *_handle;
218     int (*_version) (void);
219    
220 nmav 1.3 if (file == NULL && directory == NULL) {
221 nmav 1.1 return MCRYPT_UNKNOWN_ERROR;
222     }
223 nmav 1.4
224 nmav 1.3 if (lt_dlinit() != 0) {
225 nmav 1.1 return MCRYPT_UNKNOWN_ERROR;
226     }
227 nmav 1.4
228 nmav 1.3
229 nmav 1.2 /* LTDL_SET_PRELOADED_SYMBOLS(); */
230 nmav 1.3
231 nmav 1.2 _handle = mcrypt_dlopen(directory, NULL, file);
232 nmav 1.1
233     if (!_handle) {
234     /* fputs(lt_dlerror(), stderr);
235     * fputs("\n", stderr);
236     */
237     lt_dlexit();
238     return MCRYPT_UNKNOWN_ERROR;
239     }
240    
241    
242     _version = lt_dlsym(_handle, "_mcrypt_algorithm_version");
243    
244 nmav 1.3 if (_version == NULL) {
245 nmav 1.1 lt_dlclose(_handle);
246     lt_dlexit();
247     return MCRYPT_UNKNOWN_ERROR;
248     }
249    
250     ret = _version();
251    
252     lt_dlclose(_handle);
253     lt_dlexit();
254    
255     return ret;
256    
257     }
258    
259 nmav 1.5 WIN32DLL_DEFINE
260 nmav 1.3 int mcrypt_mode_module_ok(const char *file, const char *directory)
261 nmav 1.1 {
262     word32 ret;
263     lt_dlhandle *_handle;
264     int (*_version) (void);
265    
266 nmav 1.3 if (file == NULL && directory == NULL) {
267 nmav 1.1 return MCRYPT_UNKNOWN_ERROR;
268     }
269    
270 nmav 1.3 if (lt_dlinit() != 0) {
271 nmav 1.1 return MCRYPT_UNKNOWN_ERROR;
272 nmav 1.3 }
273 nmav 1.1 /* LTDL_SET_PRELOADED_SYMBOLS(); */
274 nmav 1.3
275 nmav 1.2 _handle = mcrypt_dlopen(directory, NULL, file);
276 nmav 1.3
277 nmav 1.1 if (!_handle) {
278     lt_dlexit();
279     return MCRYPT_UNKNOWN_ERROR;
280     }
281 nmav 1.3
282 nmav 1.1
283     _version = lt_dlsym(_handle, "_mcrypt_mode_version");
284    
285 nmav 1.3 if (_version == NULL) {
286 nmav 1.1 lt_dlclose(_handle);
287     lt_dlexit();
288     return MCRYPT_UNKNOWN_ERROR;
289     }
290 nmav 1.3
291 nmav 1.1 ret = _version();
292    
293     lt_dlclose(_handle);
294     lt_dlexit();
295    
296     return ret;
297    
298     }
299    

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26