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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26