/[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.15 - (show annotations)
Fri Sep 7 12:34:47 2001 UTC (22 years, 7 months ago) by nmav
Branch: MAIN
Changes since 1.14: +4 -25 lines
File MIME type: text/plain
Finished option which allows for static linking modules into the library

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26