Blame src/version.c

Packit fc043f
/* version.c - Version checking
Packit fc043f
 * Copyright (C) 2001, 2002, 2012, 2013, 2014 g10 Code GmbH
Packit fc043f
 *
Packit fc043f
 * This file is part of libgpg-error.
Packit fc043f
 *
Packit fc043f
 * libgpg-error is free software; you can redistribute it and/or
Packit fc043f
 * modify it under the terms of the GNU Lesser General Public License
Packit fc043f
 * as published by the Free Software Foundation; either version 2.1 of
Packit fc043f
 * the License, or (at your option) any later version.
Packit fc043f
 *
Packit fc043f
 * libgpg-error is distributed in the hope that it will be useful, but
Packit fc043f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit fc043f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit fc043f
 * Lesser General Public License for more details.
Packit fc043f
 *
Packit fc043f
 * You should have received a copy of the GNU Lesser General Public
Packit fc043f
 * License along with this program; if not, see <https://www.gnu.org/licenses/>.
Packit fc043f
 */
Packit fc043f
Packit fc043f
#if HAVE_CONFIG_H
Packit fc043f
#include <config.h>
Packit fc043f
#endif
Packit fc043f
Packit fc043f
#include <stdlib.h>
Packit fc043f
#include <stdio.h>
Packit fc043f
#include <string.h>
Packit fc043f
Packit fc043f
#include <gpg-error.h>
Packit fc043f
Packit fc043f
Packit fc043f
#define digitp(a) ((a) >= '0' && (a) <= '9')
Packit fc043f
Packit fc043f
Packit fc043f
/* This is actually a dummy function to make sure that is module is
Packit fc043f
   not empty.  Some compilers barf on empty modules.  */
Packit fc043f
static const char *
Packit fc043f
cright_blurb (void)
Packit fc043f
{
Packit fc043f
  static const char blurb[] =
Packit fc043f
    "\n\n"
Packit fc043f
    "This is Libgpg-error " PACKAGE_VERSION " - A runtime library\n"
Packit fc043f
    "Copyright 2001-2018 g10 Code GmbH\n"
Packit fc043f
    "\n"
Packit fc043f
    "(" BUILD_REVISION " " BUILD_TIMESTAMP ")\n"
Packit fc043f
    "\n\n";
Packit fc043f
  return blurb;
Packit fc043f
}
Packit fc043f
Packit fc043f
Packit fc043f
static const char*
Packit fc043f
parse_version_number (const char *s, int *number)
Packit fc043f
{
Packit fc043f
  int val = 0;
Packit fc043f
Packit fc043f
  if (*s == '0' && digitp (s[1]))
Packit fc043f
    return NULL;  /* Leading zeros are not allowed.  */
Packit fc043f
  for (; digitp (*s); s++)
Packit fc043f
    {
Packit fc043f
      val *= 10;
Packit fc043f
      val += *s - '0';
Packit fc043f
    }
Packit fc043f
  *number = val;
Packit fc043f
  return val < 0 ? NULL : s;
Packit fc043f
}
Packit fc043f
Packit fc043f
Packit fc043f
static const char *
Packit fc043f
parse_version_string (const char *s, int *major, int *minor)
Packit fc043f
{
Packit fc043f
  s = parse_version_number (s, major);
Packit fc043f
  if (!s || *s != '.')
Packit fc043f
    return NULL;
Packit fc043f
  s++;
Packit fc043f
  s = parse_version_number (s, minor);
Packit fc043f
  if (!s)
Packit fc043f
    return NULL;
Packit fc043f
  return s;  /* Patchlevel.  */
Packit fc043f
}
Packit fc043f
Packit fc043f
Packit fc043f
static const char *
Packit fc043f
compare_versions (const char *my_version, const char *req_version)
Packit fc043f
{
Packit fc043f
  int my_major, my_minor;
Packit fc043f
  int rq_major, rq_minor;
Packit fc043f
  const char *my_plvl, *rq_plvl;
Packit fc043f
Packit fc043f
  if (!req_version)
Packit fc043f
    return my_version;
Packit fc043f
  if (!my_version)
Packit fc043f
    return NULL;
Packit fc043f
Packit fc043f
  my_plvl = parse_version_string (my_version, &my_major, &my_minor);
Packit fc043f
  if (!my_plvl)
Packit fc043f
    return NULL;	/* Very strange: our own version is bogus.  */
Packit fc043f
  rq_plvl = parse_version_string(req_version, &rq_major, &rq_minor);
Packit fc043f
  if (!rq_plvl)
Packit fc043f
    return NULL;	/* Requested version string is invalid.  */
Packit fc043f
Packit fc043f
  if (my_major > rq_major
Packit fc043f
      || (my_major == rq_major && my_minor >= rq_minor))
Packit fc043f
    {
Packit fc043f
      return my_version;
Packit fc043f
    }
Packit fc043f
  return NULL;
Packit fc043f
}
Packit fc043f
Packit fc043f
Packit fc043f
/*
Packit fc043f
 * Check that the the version of the library is at minimum REQ_VERSION
Packit fc043f
 * and return the actual version string; return NULL if the condition
Packit fc043f
 * is not met.  If NULL is passed to this function, no check is done
Packit fc043f
 * and the version string is simply returned.
Packit fc043f
 */
Packit fc043f
const char *
Packit fc043f
_gpg_error_check_version (const char *req_version)
Packit fc043f
{
Packit fc043f
  if (req_version && req_version[0] == 1 && req_version[1] == 1)
Packit fc043f
    return cright_blurb ();
Packit fc043f
  return compare_versions (PACKAGE_VERSION, req_version);
Packit fc043f
}