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

Contents of /libmcrypt/lib/mcrypt_extra.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (show annotations)
Thu May 17 18:58:20 2001 UTC (22 years, 10 months ago) by nmav
Branch: MAIN
CVS Tags: mcrypt_2_4_12
Changes since 1.9: +11 -5 lines
File MIME type: text/plain
fixes to work with the new libltdl

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 /* $Id: mcrypt_extra.c,v 1.9 2001/05/16 12:27:20 nmav Exp $ */
21
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 #endif
33
34 int mcrypt_algorithm_module_ok(const char *file, const char *directory);
35 int mcrypt_mode_module_ok(const char *file, const char *directory);
36 #ifndef USE_LTDL
37 void *mcrypt_dlopen_ext(const char *filename);
38 #endif
39 void *mcrypt_dlopen(const char *a_directory, const char *m_directory,
40 const char *filename);
41
42 #define MAXPATHLEN 256
43 WIN32DLL_DEFINE char *mcrypt_readdir(DIR * dirstream)
44 {
45
46 char *result;
47 struct dirent *ret = NULL;
48 #ifdef HAVE_READDIR_R
49 struct dirent ret2[sizeof(struct dirent)+MAXPATHLEN];
50 #endif
51
52 #ifdef DT_REG
53 do {
54 #endif
55
56 #ifdef HAVE_READDIR_R
57 readdir_r(dirstream, ret2, &ret);
58 if (ret==NULL) return NULL;
59 #else
60 ret = readdir(dirstream);
61 #endif
62
63 result = calloc(1, strlen(ret->d_name) + 1);
64 if (result == NULL) {
65 return NULL;
66 }
67 strcpy(result, ret->d_name);
68 #ifdef DT_REG
69 }
70 while ((ret->d_type != DT_REG) && (ret->d_type != DT_UNKNOWN)
71 && (ret != NULL));
72 #endif
73
74 return result;
75
76 }
77
78 WIN32DLL_DEFINE char **mcrypt_list_algorithms(char *libdir, int *size)
79 {
80 DIR *pdir;
81 char directory[512];
82 char *dirname;
83 char **filename = NULL, *ptr;
84 int tmpsize;
85
86 *size = 0;
87
88 if (libdir == NULL) {
89 strcpy(directory, LIBDIR);
90 } else {
91 strcpy(directory, libdir);
92 }
93
94 pdir = opendir(directory);
95 if (pdir == NULL) {
96 fprintf(stderr, "Unable to open directory %s.\n",
97 directory);
98 return NULL;
99 }
100
101 for (;;) {
102 dirname = mcrypt_readdir(pdir);
103 if (dirname != NULL) {
104 tmpsize = strlen(dirname);
105 if (tmpsize > 3) {
106 if (mcrypt_algorithm_module_ok
107 (dirname, directory) > 0) {
108 filename =
109 realloc(filename,
110 (*size +
111 1) * sizeof(char *));
112 if (filename==NULL) {
113 free(dirname);
114 return NULL;
115 }
116 filename[*size] =
117 calloc(1, tmpsize + 1);
118 if (filename[*size]==NULL) return NULL;
119 strcpy(filename[*size], dirname);
120
121 ptr =
122 strrchr(filename[*size], '.');
123 if (ptr != NULL)
124 *ptr = '\0';
125 (*size)++;
126 }
127 }
128 free(dirname);
129 } else {
130 break;
131 }
132
133 }
134
135
136 closedir(pdir);
137
138 return filename;
139
140 }
141
142 WIN32DLL_DEFINE char **mcrypt_list_modes(char *libdir, int *size)
143 {
144 DIR *pdir;
145 char directory[512];
146 char *dirname;
147 char **filename = NULL, *ptr;
148 int tmpsize;
149
150 if (libdir == NULL) {
151 strcpy(directory, LIBDIR);
152 } else {
153 strcpy(directory, libdir);
154 }
155
156 pdir = opendir(directory);
157 if (pdir == NULL) {
158 fprintf(stderr, "Unable to open directory %s.\n",
159 directory);
160 return NULL;
161 }
162
163 *size = 0;
164 for (;;) {
165
166 dirname = mcrypt_readdir(pdir);
167 if (dirname != NULL) {
168 tmpsize = strlen(dirname);
169 if (tmpsize > 3) {
170 if (mcrypt_mode_module_ok
171 (dirname, directory) > 0) {
172 filename =
173 realloc(filename,
174 (*size +
175 1) * sizeof(char *));
176 if (filename==NULL) {
177 free(dirname);
178 return NULL;
179 }
180 filename[*size] =
181 calloc(1, tmpsize + 1);
182 if (filename[*size]==NULL) return NULL;
183
184 strcpy(filename[*size], dirname);
185 ptr =
186 strrchr(filename[*size], '.');
187 if (ptr != NULL)
188 *ptr = '\0';
189 (*size)++;
190 }
191 }
192 free(dirname);
193 } else {
194 break;
195 }
196
197 }
198
199 closedir(pdir);
200 return filename;
201 }
202
203 WIN32DLL_DEFINE void mcrypt_free_p(char **p, int size)
204 {
205 int i;
206
207 for (i = 0; i < size; i++) {
208 free(p[i]);
209 }
210 free(p);
211 }
212
213 WIN32DLL_DEFINE
214 int mcrypt_algorithm_module_ok(const char *file, const char *directory)
215 {
216 word32 ret;
217 lt_dlhandle _handle;
218 int (*_version) (void);
219
220 if (file == NULL && directory == NULL) {
221 return MCRYPT_UNKNOWN_ERROR;
222 }
223
224 if (lt_dlinit() != 0) {
225 return MCRYPT_UNKNOWN_ERROR;
226 }
227
228
229 /* LTDL_SET_PRELOADED_SYMBOLS(); */
230
231 _handle = mcrypt_dlopen(directory, NULL, file);
232
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 if (_version == NULL) {
245 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 WIN32DLL_DEFINE
260 int mcrypt_mode_module_ok(const char *file, const char *directory)
261 {
262 word32 ret;
263 lt_dlhandle _handle;
264 int (*_version) (void);
265
266 if (file == NULL && directory == NULL) {
267 return MCRYPT_UNKNOWN_ERROR;
268 }
269
270 if (lt_dlinit() != 0) {
271 return MCRYPT_UNKNOWN_ERROR;
272 }
273 /* LTDL_SET_PRELOADED_SYMBOLS(); */
274
275 _handle = mcrypt_dlopen(directory, NULL, file);
276
277 if (!_handle) {
278 lt_dlexit();
279 return MCRYPT_UNKNOWN_ERROR;
280 }
281
282
283 _version = lt_dlsym(_handle, "_mcrypt_mode_version");
284
285 if (_version == NULL) {
286 lt_dlclose(_handle);
287 lt_dlexit();
288 return MCRYPT_UNKNOWN_ERROR;
289 }
290
291 ret = _version();
292
293 lt_dlclose(_handle);
294 lt_dlexit();
295
296 return ret;
297
298 }
299
300 /* Taken from libgcrypt */
301
302 static const char*
303 parse_version_number( const char *s, int *number )
304 {
305 int val = 0;
306
307 if( *s == '0' && isdigit(s[1]) )
308 return NULL; /* leading zeros are not allowed */
309 for ( ; isdigit(*s); s++ ) {
310 val *= 10;
311 val += *s - '0';
312 }
313 *number = val;
314 return val < 0? NULL : s;
315 }
316
317
318 static const char *
319 parse_version_string( const char *s, int *major, int *minor, int *micro )
320 {
321 s = parse_version_number( s, major );
322 if( !s || *s != '.' )
323 return NULL;
324 s++;
325 s = parse_version_number( s, minor );
326 if( !s || *s != '.' )
327 return NULL;
328 s++;
329 s = parse_version_number( s, micro );
330 if( !s )
331 return NULL;
332 return s; /* patchlevel */
333 }
334
335 /****************
336 * Check that the the version of the library is at minimum the requested one
337 * and return the version string; return NULL if the condition is not
338 * satisfied. If a NULL is passed to this function, no check is done,
339 * but the version string is simply returned.
340 */
341 const char *
342 mcrypt_check_version( const char *req_version )
343 {
344 const char *ver = VERSION;
345 int my_major, my_minor, my_micro;
346 int rq_major, rq_minor, rq_micro;
347 const char *my_plvl, *rq_plvl;
348
349 if ( !req_version )
350 return ver;
351
352 my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro );
353 if ( !my_plvl )
354 return NULL; /* very strange our own version is bogus */
355 rq_plvl = parse_version_string( req_version, &rq_major, &rq_minor,
356 &rq_micro );
357 if ( !rq_plvl )
358 return NULL; /* req version string is invalid */
359
360 if ( my_major > rq_major
361 || (my_major == rq_major && my_minor > rq_minor)
362 || (my_major == rq_major && my_minor == rq_minor
363 && my_micro > rq_micro)
364 || (my_major == rq_major && my_minor == rq_minor
365 && my_micro == rq_micro
366 && strcmp( my_plvl, rq_plvl ) >= 0) ) {
367 return ver;
368 }
369 return NULL;
370 }
371

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26