X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/tripe/blobdiff_plain/e6a3a5537973f060bbfbe9b5aecfd9c0312a1a7f..46401b81fe5fab9b0d3d3a5f5764bafa3f9c0b86:/server/servutil.c diff --git a/server/servutil.c b/server/servutil.c index 77cfd2c6..dbca451d 100644 --- a/server/servutil.c +++ b/server/servutil.c @@ -1,13 +1,11 @@ /* -*-c-*- - * - * $Id$ * * Various handy server-only utilities * * (c) 2001 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * * This file is part of Trivial IP Encryption (TrIPE). * @@ -15,12 +13,12 @@ * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * + * * TrIPE 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 General Public License for more details. - * + * * You should have received a copy of the GNU General Public License * along with TrIPE; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -32,7 +30,7 @@ /*----- Global variables --------------------------------------------------*/ -octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ]; +octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ], buf_u[PKBUFSZ]; /*----- Main code ---------------------------------------------------------*/ @@ -43,14 +41,14 @@ octet buf_i[PKBUFSZ], buf_o[PKBUFSZ], buf_t[PKBUFSZ]; * Returns: A pointer to the integer's textual representation. * * Use: Converts a multiprecision integer to a string. Corrupts - * @buf_t@. + * @buf_u@. */ const char *mpstr(mp *m) { - if (mp_writestring(m, (char *)buf_t, sizeof(buf_t), 10)) + if (mp_writestring(m, (char *)buf_u, sizeof(buf_u), 10)) return (""); - return ((const char *)buf_t); + return ((const char *)buf_u); } /* --- @gestr@ --- * @@ -61,14 +59,14 @@ const char *mpstr(mp *m) * Returns: A pointer to the element's textual representation. * * Use: Converts a group element to a string. Corrupts - * @buf_t@. + * @buf_u@. */ const char *gestr(group *g, ge *x) { - if (group_writestring(g, x, (char *)buf_t, sizeof(buf_t))) + if (group_writestring(g, x, (char *)buf_u, sizeof(buf_u))) return (""); - return ((const char *)buf_t); + return ((const char *)buf_u); } /* --- @timestr@ --- * @@ -78,7 +76,7 @@ const char *gestr(group *g, ge *x) * Returns: A pointer to a textual representation of the time. * * Use: Converts a time to a textual representation. Corrupts - * @buf_t@. + * @buf_u@. */ const char *timestr(time_t t) @@ -87,8 +85,25 @@ const char *timestr(time_t t) if (!t) return ("NEVER"); tm = localtime(&t); - strftime((char *)buf_t, sizeof(buf_t), "%Y-%m-%dT%H:%M:%S", tm); - return ((const char *)buf_t); + strftime((char *)buf_u, sizeof(buf_u), "%Y-%m-%dT%H:%M:%S", tm); + return ((const char *)buf_u); +} + +/* --- @mystrieq@ --- * + * + * Arguments: @const char *x, *y@ = two strings + * + * Returns: True if @x@ and @y are equal, up to case. + */ + +int mystrieq(const char *x, const char *y) +{ + for (;;) { + if (!*x && !*y) return (1); + if (tolower((unsigned char)*x) != tolower((unsigned char)*y)) + return (0); + x++; y++; + } } /* --- @seq_reset@ --- * @@ -140,157 +155,4 @@ int seq_check(seqwin *s, uint32 q, const char *service) return (0); } -/* --- @versioncmp@ --- * - * - * Arguments: @const char *va, *vb@ = two version strings - * - * Returns: Less than, equal to, or greater than zero, according to - * whether @va@ is less than, equal to, or greater than @vb@. - * - * Use: Compares version number strings. - * - * The algorithm is an extension of the Debian version - * comparison algorithm. A version number consists of three - * components: - * - * [EPOCH :] MAIN [- SUB] - * - * The MAIN part may contain colons or hyphens if there is an - * EPOCH or SUB, respectively. Version strings are compared - * componentwise: first epochs, then main parts, and finally - * subparts. - * - * The component comparison is done as follows. First, the - * initial subsequence of nondigit characters is extracted from - * each string, and these are compared lexicographically, using - * ASCII ordering, except that letters precede non-letters. If - * both are the same, an initial sequence of digits is extracted - * from the remaining parts of the version strings, and these - * are compared numerically (an empty sequence being considered - * to have the value zero). This process is repeated until we - * have a winner or until both strings are exhausted. - */ - -struct vinfo { - const char *e, *el; - const char *m, *ml; - const char *s, *sl; -}; - -static int vint(const char **vv, const char *vl) -{ - int n = 0; - const char *v = *vv; - int ch; - - while (v < vl) { - ch = *v; - if (!isdigit((unsigned char)ch)) - break; - v++; - n = n * 10 + (ch - '0'); - } - *vv = v; - return (n); -} - -static const char *vchr(const char **vv, const char *vl) -{ - const char *v = *vv; - const char *b = v; - int ch; - - while (v < vl) { - ch = *v; - if (isdigit((unsigned char)ch)) - break; - v++; - } - *vv = v; - return (b); -} - -#define CMP(x, y) ((x) < (y) ? -1 : +1) - -static int vcmp(const char *va, const char *val, - const char *vb, const char *vbl) -{ - const char *pa, *pb; - int ia, ib; - - for (;;) { - - /* --- See if we're done --- */ - - if (va == val && vb == vbl) - return (0); - - /* --- Compare nondigit portions --- */ - - pa = vchr(&va, val); pb = vchr(&vb, vbl); - for (;;) { - if (pa == va && pb == vb) - break; - else if (pa == va) - return (-1); - else if (pb == vb) - return (+1); - else if (*pa == *pb) { - pa++; pb++; - continue; - } else if (isalpha((unsigned char)*pa) == isalpha((unsigned char)*pb)) - return (CMP(*pa, *pb)); - else if (isalpha((unsigned char)*pa)) - return (-1); - else - return (+1); - } - - /* --- Compare digit portions --- */ - - ia = vint(&va, val); ib = vint(&vb, vbl); - if (ia != ib) - return (CMP(ia, ib)); - } -} - -static void vsplit(const char *v, struct vinfo *vi) -{ - const char *p; - size_t n; - - if ((p = strchr(v, ':')) == 0) - vi->e = vi->el = 0; - else { - vi->e = v; - vi->el = p; - v = p + 1; - } - - n = strlen(v); - if ((p = strrchr(v, '-')) == 0) - vi->s = vi->sl = 0; - else { - vi->s = p + 1; - vi->sl = v + n; - n = p - v; - } - - vi->m = v; - vi->ml = v + n; -} - -int versioncmp(const char *va, const char *vb) -{ - struct vinfo via, vib; - int rc; - - vsplit(va, &via); vsplit(vb, &vib); - if ((rc = vcmp(via.e, via.el, vib.e, vib.el)) != 0 || - (rc = vcmp(via.m, via.ml, vib.m, vib.ml)) != 0 || - (rc = vcmp(via.s, via.sl, vib.s, vib.sl)) != 0) - return (rc); - return (0); -} - /*----- That's all, folks -------------------------------------------------*/