util/perl/OpenSSL/Ordinals.pm: use OpenSSL::Util::cmp_versions
authorRichard Levitte <levitte@openssl.org>
Sun, 2 Dec 2018 12:50:56 +0000 (13:50 +0100)
committerRichard Levitte <levitte@openssl.org>
Fri, 7 Dec 2018 15:02:17 +0000 (16:02 +0100)
OpenSSL::Util::cmp_versions() is introduced to be used everywhere
where versions are compared.

Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/7740)

util/perl/OpenSSL/Ordinals.pm
util/perl/OpenSSL/Util.pm [new file with mode: 0644]

index 35625a32c9056f9dd61a5d175c460f872a33bb11..06bf7b0ac4bfb5d887ed13076bc4c3193db32352 100644 (file)
@@ -845,33 +845,10 @@ OpenSSL::Ordinals::Item objects.
 =cut
 
 sub by_version {
-    # Until we're rid of everything with the old version scheme,
-    # we need to be able to handle older style x.y.zl versions.
-    sub _ossl_versionsplit {
-        my $textversion = shift;
-        return $textversion if $textversion eq '*';
-        my ($major,$minor,$edit,$patch) =
-            $textversion =~ /^(\d+)\.(\d+)\.(\d+)([a-z]{0,2})$/;
-        return ($major,$minor,$edit,$patch);
-    }
-
     return sub {
-        my @a_split = _ossl_versionsplit($_[0]->version());
-        my @b_split = _ossl_versionsplit($_[1]->version());
-        my $verdict = 0;
-        while (@a_split) {
-            # The last part is a letter sequence (or a '*')
-            if (scalar @a_split == 1) {
-                $verdict = $a_split[0] cmp $b_split[0];
-            } else {
-                $verdict = $a_split[0] <=> $b_split[0];
-            }
-            shift @a_split;
-            shift @b_split;
-            last unless $verdict == 0;
-        }
-        $verdict;
-    };
+        # cmp_versions comes from OpenSSL::Util
+        return cmp_versions($_[0]->version(), $_[1]->version());
+    }
 }
 
 =back
diff --git a/util/perl/OpenSSL/Util.pm b/util/perl/OpenSSL/Util.pm
new file mode 100644 (file)
index 0000000..1c8c6af
--- /dev/null
@@ -0,0 +1,88 @@
+#! /usr/bin/env perl
+# Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
+#
+# Licensed under the Apache License 2.0 (the "License").  You may not use
+# this file except in compliance with the License.  You can obtain a copy
+# in the file LICENSE in the source distribution or at
+# https://www.openssl.org/source/license.html
+
+package OpenSSL::Ordinals;
+
+use strict;
+use warnings;
+use Carp;
+
+use Exporter;
+use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
+$VERSION = "0.1";
+@ISA = qw(Exporter);
+@EXPORT = qw(cmp_versions);
+@EXPORT_OK = qw();
+
+=head1 NAME
+
+OpenSSL::Util - small OpenSSL utilities
+
+=head1 SYNOPSIS
+
+  use OpenSSL::Util;
+
+  $versiondiff = cmp_versions('1.0.2k', '3.0.1');
+  # $versiondiff should be -1
+
+  $versiondiff = cmp_versions('1.1.0', '1.0.2a');
+  # $versiondiff should be 1
+
+  $versiondiff = cmp_versions('1.1.1', '1.1.1');
+  # $versiondiff should be 0
+
+=head1 DESCRIPTION
+
+=over
+
+=item B<cmp_versions "VERSION1", "VERSION2">
+
+Compares VERSION1 with VERSION2, paying attention to OpenSSL versioning.
+
+Returns 1 if VERSION1 is greater than VERSION2, 0 if they are equal, and
+-1 if VERSION1 is less than VERSION2.
+
+=back
+
+=cut
+
+# Until we're rid of everything with the old version scheme,
+# we need to be able to handle older style x.y.zl versions.
+# In terms of comparison, the x.y.zl and the x.y.z schemes
+# are compatible...  mostly because the latter starts at a
+# new major release with a new major number.
+sub _ossl_versionsplit {
+    my $textversion = shift;
+    return $textversion if $textversion eq '*';
+    my ($major,$minor,$edit,$letter) =
+        $textversion =~ /^(\d+)\.(\d+)\.(\d+)([a-z]{0,2})$/;
+
+    return ($major,$minor,$edit,$letter);
+}
+
+sub cmp_versions {
+    my @a_split = _ossl_versionsplit(shift);
+    my @b_split = _ossl_versionsplit(shift);
+    my $verdict = 0;
+
+    while (@a_split) {
+        # The last part is a letter sequence (or a '*')
+        if (scalar @a_split == 1) {
+            $verdict = $a_split[0] cmp $b_split[0];
+        } else {
+            $verdict = $a_split[0] <=> $b_split[0];
+        }
+        shift @a_split;
+        shift @b_split;
+        last unless $verdict == 0;
+    }
+
+    return $verdict;
+}
+
+1;