290 |
return ret; |
return ret; |
291 |
|
|
292 |
} |
} |
293 |
|
|
294 |
|
/* Taken from libgcrypt */ |
295 |
|
|
296 |
|
static const char* |
297 |
|
parse_version_number( const char *s, int *number ) |
298 |
|
{ |
299 |
|
int val = 0; |
300 |
|
|
301 |
|
if( *s == '0' && isdigit(s[1]) ) |
302 |
|
return NULL; /* leading zeros are not allowed */ |
303 |
|
for ( ; isdigit(*s); s++ ) { |
304 |
|
val *= 10; |
305 |
|
val += *s - '0'; |
306 |
|
} |
307 |
|
*number = val; |
308 |
|
return val < 0? NULL : s; |
309 |
|
} |
310 |
|
|
311 |
|
|
312 |
|
static const char * |
313 |
|
parse_version_string( const char *s, int *major, int *minor, int *micro ) |
314 |
|
{ |
315 |
|
s = parse_version_number( s, major ); |
316 |
|
if( !s || *s != '.' ) |
317 |
|
return NULL; |
318 |
|
s++; |
319 |
|
s = parse_version_number( s, minor ); |
320 |
|
if( !s || *s != '.' ) |
321 |
|
return NULL; |
322 |
|
s++; |
323 |
|
s = parse_version_number( s, micro ); |
324 |
|
if( !s ) |
325 |
|
return NULL; |
326 |
|
return s; /* patchlevel */ |
327 |
|
} |
328 |
|
|
329 |
|
/**************** |
330 |
|
* Check that the the version of the library is at minimum the requested one |
331 |
|
* and return the version string; return NULL if the condition is not |
332 |
|
* satisfied. If a NULL is passed to this function, no check is done, |
333 |
|
* but the version string is simply returned. |
334 |
|
*/ |
335 |
|
const char * |
336 |
|
mcrypt_check_version( const char *req_version ) |
337 |
|
{ |
338 |
|
const char *ver = VERSION; |
339 |
|
int my_major, my_minor, my_micro; |
340 |
|
int rq_major, rq_minor, rq_micro; |
341 |
|
const char *my_plvl, *rq_plvl; |
342 |
|
|
343 |
|
if ( !req_version ) |
344 |
|
return ver; |
345 |
|
|
346 |
|
my_plvl = parse_version_string( ver, &my_major, &my_minor, &my_micro ); |
347 |
|
if ( !my_plvl ) |
348 |
|
return NULL; /* very strange our own version is bogus */ |
349 |
|
rq_plvl = parse_version_string( req_version, &rq_major, &rq_minor, |
350 |
|
&rq_micro ); |
351 |
|
if ( !rq_plvl ) |
352 |
|
return NULL; /* req version string is invalid */ |
353 |
|
|
354 |
|
if ( my_major > rq_major |
355 |
|
|| (my_major == rq_major && my_minor > rq_minor) |
356 |
|
|| (my_major == rq_major && my_minor == rq_minor |
357 |
|
&& my_micro > rq_micro) |
358 |
|
|| (my_major == rq_major && my_minor == rq_minor |
359 |
|
&& my_micro == rq_micro |
360 |
|
&& strcmp( my_plvl, rq_plvl ) >= 0) ) { |
361 |
|
return ver; |
362 |
|
} |
363 |
|
return NULL; |
364 |
|
} |
365 |
|
|