summaryrefslogtreecommitdiffstats
path: root/lib/strutil/filevercmp.c
blob: a0e55fc9a5a5f9f75de03561bbc86c74ab04d779 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
   Copyright (C) 1995 Ian Jackson <iwj10@cus.cam.ac.uk>
   Copyright (C) 2001 Anthony Towns <aj@azure.humbug.org.au>
   Copyright (C) 2008-2022 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <stdlib.h>
#include <limits.h>

#include "lib/strutil.h"

/*** global variables ****************************************************************************/

/*** file scope macro definitions ****************************************************************/

/*** file scope type declarations ****************************************************************/

/*** file scope variables ************************************************************************/

/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/* Return the length of a prefix of @s that corresponds to the suffix defined by this extended
 * regular expression in the C locale: (\.[A-Za-z~][A-Za-z0-9~]*)*$
 *
 * Use the longest suffix matching this regular expression, except do not use all of s as a suffix
 * if s is nonempty.
 *
 * If *len is -1, s is a string; set *lem to s's length.
 * Otherwise, *len should be nonnegative, s is a char array, and *len does not change.
 */
static ssize_t
file_prefixlen (const char *s, ssize_t * len)
{
    size_t n = (size_t) (*len); /* SIZE_MAX if N == -1 */
    size_t i = 0;
    size_t prefixlen = 0;

    while (TRUE)
    {
        gboolean done;

        if (*len < 0)
            done = s[i] == '\0';
        else
            done = i == n;

        if (done)
        {
            *len = (ssize_t) i;
            return (ssize_t) prefixlen;
        }

        i++;
        prefixlen = i;

        while (i + 1 < n && s[i] == '.' && (g_ascii_isalpha (s[i + 1]) || s[i + 1] == '~'))
            for (i += 2; i < n && (g_ascii_isalnum (s[i]) || s[i] == '~'); i++)
                ;
    }
}

/* --------------------------------------------------------------------------------------------- */

/* Return a version sort comparison value for @s's byte at position @pos.
 *
 * @param s a string
 * @param pos a position in @s
 * @param len a length of @s. If @pos == @len, sort before all non-'~' bytes.
 */

static int
order (const char *s, size_t pos, size_t len)
{
    unsigned char c;

    if (pos == len)
        return (-1);

    c = s[pos];

    if (g_ascii_isdigit (c))
        return 0;
    if (g_ascii_isalpha (c))
        return c;
    if (c == '~')
        return (-2);

    g_assert (UCHAR_MAX <= (INT_MAX - 1 - 2) / 2);

    return (int) c + UCHAR_MAX + 1;
}

/* --------------------------------------------------------------------------------------------- */

/* Slightly modified verrevcmp function from dpkg
 *
 * This implements the algorithm for comparison of version strings
 * specified by Debian and now widely adopted.  The detailed
 * specification can be found in the Debian Policy Manual in the
 * section on the 'Version' control field.  This version of the code
 * implements that from s5.6.12 of Debian Policy v3.8.0.1
 * https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version
 *
 * @param s1 first char array to compare
 * @param s1_len length of @s1
 * @param s2 second char array to compare
 * @param s2_len length of @s2
 *
 * @return an integer less than, equal to, or greater than zero, if @s1 is <, == or > than @s2.
 */
static int
verrevcmp (const char *s1, ssize_t s1_len, const char *s2, ssize_t s2_len)
{
    ssize_t s1_pos = 0;
    ssize_t s2_pos = 0;

    while (s1_pos < s1_len || s2_pos < s2_len)
    {
        int first_diff = 0;

        while ((s1_pos < s1_len && !g_ascii_isdigit (s1[s1_pos]))
               || (s2_pos < s2_len && !g_ascii_isdigit (s2[s2_pos])))
        {
            int s1_c, s2_c;

            s1_c = order (s1, s1_pos, s1_len);
            s2_c = order (s2, s2_pos, s2_len);

            if (s1_c != s2_c)
                return (s1_c - s2_c);

            s1_pos++;
            s2_pos++;
        }

        while (s1_pos < s1_len && s1[s1_pos] == '0')
            s1_pos++;
        while (s2_pos < s2_len && s2[s2_pos] == '0')
            s2_pos++;

        while (s1_pos < s1_len && s2_pos < s2_len
               && g_ascii_isdigit (s1[s1_pos]) && g_ascii_isdigit (s2[s2_pos]))
        {
            if (first_diff == 0)
                first_diff = s1[s1_pos] - s2[s2_pos];

            s1_pos++;
            s2_pos++;
        }

        if (s1_pos < s1_len && g_ascii_isdigit (s1[s1_pos]))
            return 1;
        if (s2_pos < s2_len && g_ascii_isdigit (s2[s2_pos]))
            return (-1);
        if (first_diff != 0)
            return first_diff;
    }

    return 0;
}

/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */

/* Compare version strings.
 *
 * @param s1 first string to compare
 * @param s2 second string to compare
 *
 * @return an integer less than, equal to, or greater than zero, if @s1 is <, == or > than @s2.
 */
int
filevercmp (const char *s1, const char *s2)
{
    return filenvercmp (s1, -1, s2, -1);
}

/* --------------------------------------------------------------------------------------------- */
/* Compare version strings.
 *
 * @param a first string to compare
 * @param alen length of @a or (-1)
 * @param b second string to compare
 * @param blen length of @b or (-1)
 *
 * @return an integer less than, equal to, or greater than zero, if @s1 is <, == or > than @s2.
 */
int
filenvercmp (const char *a, ssize_t alen, const char *b, ssize_t blen)
{
    gboolean aempty, bempty;
    ssize_t aprefixlen, bprefixlen;
    gboolean one_pass_only;
    int result;

    /* Special case for empty versions. */
    aempty = alen < 0 ? a[0] == '\0' : alen == 0;
    bempty = blen < 0 ? b[0] == '\0' : blen == 0;

    if (aempty)
        return (bempty ? 0 : -1);
    if (bempty)
        return 1;

    /* Special cases for leading ".": "." sorts first, then "..", then other names with leading ".",
       then other names. */
    if (a[0] == '.')
    {
        gboolean adot, bdot;
        gboolean adotdot, bdotdot;

        if (b[0] != '.')
            return (-1);

        adot = alen < 0 ? a[1] == '\0' : alen == 1;
        bdot = blen < 0 ? b[1] == '\0' : blen == 1;

        if (adot)
            return (bdot ? 0 : -1);
        if (bdot)
            return 1;

        adotdot = a[1] == '.' && (alen < 0 ? a[2] == '\0' : alen == 2);
        bdotdot = b[1] == '.' && (blen < 0 ? b[2] == '\0' : blen == 2);
        if (adotdot)
            return (bdotdot ? 0 : -1);
        if (bdotdot)
            return 1;
    }
    else if (b[0] == '.')
        return 1;

    /* Cut file suffixes. */
    aprefixlen = file_prefixlen (a, &alen);
    bprefixlen = file_prefixlen (b, &blen);

    /* If both suffixes are empty, a second pass would return the same thing. */
    one_pass_only = aprefixlen == alen && bprefixlen == blen;

    result = verrevcmp (a, aprefixlen, b, bprefixlen);

    /* Return the initial result if nonzero, or if no second pass is needed.
       Otherwise, restore the suffixes and try again. */
    return (result != 0 || one_pass_only ? result : verrevcmp (a, alen, b, blen));
}

/* --------------------------------------------------------------------------------------------- */