Util.pm 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #! /usr/bin/env perl
  2. # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. package OpenSSL::Ordinals;
  9. use strict;
  10. use warnings;
  11. use Carp;
  12. use Exporter;
  13. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  14. $VERSION = "0.1";
  15. @ISA = qw(Exporter);
  16. @EXPORT = qw(cmp_versions);
  17. @EXPORT_OK = qw();
  18. =head1 NAME
  19. OpenSSL::Util - small OpenSSL utilities
  20. =head1 SYNOPSIS
  21. use OpenSSL::Util;
  22. $versiondiff = cmp_versions('1.0.2k', '3.0.1');
  23. # $versiondiff should be -1
  24. $versiondiff = cmp_versions('1.1.0', '1.0.2a');
  25. # $versiondiff should be 1
  26. $versiondiff = cmp_versions('1.1.1', '1.1.1');
  27. # $versiondiff should be 0
  28. =head1 DESCRIPTION
  29. =over
  30. =item B<cmp_versions "VERSION1", "VERSION2">
  31. Compares VERSION1 with VERSION2, paying attention to OpenSSL versioning.
  32. Returns 1 if VERSION1 is greater than VERSION2, 0 if they are equal, and
  33. -1 if VERSION1 is less than VERSION2.
  34. =back
  35. =cut
  36. # Until we're rid of everything with the old version scheme,
  37. # we need to be able to handle older style x.y.zl versions.
  38. # In terms of comparison, the x.y.zl and the x.y.z schemes
  39. # are compatible... mostly because the latter starts at a
  40. # new major release with a new major number.
  41. sub _ossl_versionsplit {
  42. my $textversion = shift;
  43. return $textversion if $textversion eq '*';
  44. my ($major,$minor,$edit,$letter) =
  45. $textversion =~ /^(\d+)\.(\d+)\.(\d+)([a-z]{0,2})$/;
  46. return ($major,$minor,$edit,$letter);
  47. }
  48. sub cmp_versions {
  49. my @a_split = _ossl_versionsplit(shift);
  50. my @b_split = _ossl_versionsplit(shift);
  51. my $verdict = 0;
  52. while (@a_split) {
  53. # The last part is a letter sequence (or a '*')
  54. if (scalar @a_split == 1) {
  55. $verdict = $a_split[0] cmp $b_split[0];
  56. } else {
  57. $verdict = $a_split[0] <=> $b_split[0];
  58. }
  59. shift @a_split;
  60. shift @b_split;
  61. last unless $verdict == 0;
  62. }
  63. return $verdict;
  64. }
  65. 1;