]> err.no Git - yubikey-personalization/blob - ykpers-version.c
Merge tag 'v1.15.1'
[yubikey-personalization] / ykpers-version.c
1 /* -*- mode:C; c-file-style: "bsd" -*- */
2 /*
3  * Copyright (c) 2012-2013 Yubico AB
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *
13  *     * Redistributions in binary form must reproduce the above
14  *       copyright notice, this list of conditions and the following
15  *       disclaimer in the documentation and/or other materials provided
16  *       with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <ykpers-version.h>
32 #include <stddef.h>
33
34 #define _GNU_SOURCE
35 #include <string.h>
36
37 /* From http://article.gmane.org/gmane.os.freebsd.devel.hackers/23606 */
38 int
39 my_strverscmp(const char *s1, const char *s2)
40 {
41   static const char *digits = "0123456789";
42   int ret, lz1, lz2;
43   size_t p1, p2;
44
45   p1 = strcspn(s1, digits);
46   p2 = strcspn(s2, digits);
47   while (p1 == p2 && s1[p1] != '\0' && s2[p2] != '\0') {
48     /* Different prefix */
49     if ((ret = strncmp(s1, s2, p1)) != 0)
50       return ret;
51
52     s1 += p1;
53     s2 += p2;
54
55     lz1 = lz2 = 0;
56     if (*s1 == '0')
57       lz1 = 1;
58     if (*s2 == '0')
59       lz2 = 1;
60
61     if (lz1 > lz2)
62       return -1;
63     else if (lz1 < lz2)
64       return 1;
65     else if (lz1 == 1) {
66       /*
67        * If the common prefix for s1 and s2 consists only of zeros, then the
68        * "longer" number has to compare less. Otherwise the comparison needs
69        * to be numerical (just fallthrough). See
70        * http://refspecs.freestandards.org/LSB_2.0.1/LSB-generic/
71        *                                 LSB-generic/baselib-strverscmp.html
72        */
73       while (*s1 == '0' && *s2 == '0') {
74         ++s1;
75         ++s2;
76       }
77
78       p1 = strspn(s1, digits);
79       p2 = strspn(s2, digits);
80
81       /* Catch empty strings */
82       if (p1 == 0 && p2 > 0)
83         return 1;
84       else if (p2 == 0 && p1 > 0)
85         return -1;
86
87       /* Prefixes are not same */
88       if (*s1 != *s2 && *s1 != '0' && *s2 != '0') {
89         if (p1 < p2)
90           return 1;
91         else if (p1 > p2)
92           return -1;
93       } else {
94         if (p1 < p2)
95           ret = strncmp(s1, s2, p1);
96         else if (p1 > p2)
97           ret = strncmp(s1, s2, p2);
98         if (ret != 0)
99           return ret;
100       }
101     }
102
103     p1 = strspn(s1, digits);
104     p2 = strspn(s2, digits);
105
106     if (p1 < p2)
107       return -1;
108     else if (p1 > p2)
109       return 1;
110     else if ((ret = strncmp(s1, s2, p1)) != 0)
111       return ret;
112
113     /* Numbers are equal or not present, try with next ones. */
114     s1 += p1;
115     s2 += p2;
116     p1 = strcspn(s1, digits);
117     p2 = strcspn(s2, digits);
118   }
119
120   return strcmp(s1, s2);
121 }
122
123 const char *
124 ykpers_check_version (const char *req_version)
125 {
126   if (!req_version || my_strverscmp (req_version, YKPERS_VERSION_STRING) <= 0)
127     return YKPERS_VERSION_STRING;
128
129   return NULL;
130 }