[BACK]Return to ns_date.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / lib / libresolv

File: [cvs.NetBSD.org] / src / lib / libresolv / ns_date.c (download)

Revision 1.1, Thu Nov 15 18:48:48 2012 UTC (7 years, 6 months ago) by christos
Branch: MAIN
CVS Tags: yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, tls-maxphys-base, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-base, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, matt-nb8-mediatek-base, matt-nb8-mediatek, localcount-20160914, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, agc-symver-base, agc-symver
Branch point for: yamt-pagecache, tls-maxphys, phil-wifi, pgoyette-compat, netbsd-6

Make libresolv handle the part that libc does not handle, update records
and signing.

/*	$NetBSD: ns_date.c,v 1.1 2012/11/15 18:48:48 christos Exp $	*/

/*
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1999 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/cdefs.h>
#if 0
static const char rcsid[] = "Id: ns_date.c,v 1.6 2005/04/27 04:56:39 sra Exp ";
#else
__RCSID("$NetBSD: ns_date.c,v 1.1 2012/11/15 18:48:48 christos Exp $");
#endif

/* Import. */

#include "port_before.h"

#include <arpa/nameser.h>

#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <time.h>

#include "port_after.h"

#ifdef SPRINTF_CHAR
# define SPRINTF(x) strlen(sprintf/**/x)
#else
# define SPRINTF(x) ((size_t)sprintf x)
#endif

/* Forward. */

static int	datepart(const char *, int, int, int, int *);

/* Public. */

/*%
 * Convert a date in ASCII into the number of seconds since
 * 1 January 1970 (GMT assumed).  Format is yyyymmddhhmmss, all
 * digits required, no spaces allowed.
 */

u_int32_t
ns_datetosecs(const char *cp, int *errp) {
	struct tm tim;
	u_int32_t result;
	int mdays, i;
	static const int days_per_month[12] =
		{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

	if (strlen(cp) != 14U) {
		*errp = 1;
		return (0);
	}
	*errp = 0;

	memset(&tim, 0, sizeof tim);
	tim.tm_year  = datepart(cp +  0, 4, 1990, 9999, errp) - 1900;
	tim.tm_mon   = datepart(cp +  4, 2,   01,   12, errp) - 1;
	tim.tm_mday  = datepart(cp +  6, 2,   01,   31, errp);
	tim.tm_hour  = datepart(cp +  8, 2,   00,   23, errp);
	tim.tm_min   = datepart(cp + 10, 2,   00,   59, errp);
	tim.tm_sec   = datepart(cp + 12, 2,   00,   59, errp);
	if (*errp)		/*%< Any parse errors? */
		return (0);

	/* 
	 * OK, now because timegm() is not available in all environments,
	 * we will do it by hand.  Roll up sleeves, curse the gods, begin!
	 */

#define SECS_PER_DAY    ((u_int32_t)24*60*60)
#define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)

	result  = tim.tm_sec;				/*%< Seconds */
	result += tim.tm_min * 60;			/*%< Minutes */
	result += tim.tm_hour * (60*60);		/*%< Hours */
	result += (tim.tm_mday - 1) * SECS_PER_DAY;	/*%< Days */
	/* Months are trickier.  Look without leaping, then leap */
	mdays = 0;
	for (i = 0; i < tim.tm_mon; i++)
		mdays += days_per_month[i];
	result += mdays * SECS_PER_DAY;			/*%< Months */
	if (tim.tm_mon > 1 && isleap(1900+tim.tm_year))
		result += SECS_PER_DAY;		/*%< Add leapday for this year */
	/* First figure years without leapdays, then add them in.  */
	/* The loop is slow, FIXME, but simple and accurate.  */
	result += (tim.tm_year - 70) * (SECS_PER_DAY*365); /*%< Years */
	for (i = 70; i < tim.tm_year; i++)
		if (isleap(1900+i))
			result += SECS_PER_DAY; /*%< Add leapday for prev year */
	return (result);
}

/* Private. */

/*%
 * Parse part of a date.  Set error flag if any error.
 * Don't reset the flag if there is no error.
 */
static int
datepart(const char *buf, int size, int min, int max, int *errp) {
	int result = 0;
	int i;

	for (i = 0; i < size; i++) {
		if (!isdigit((unsigned char)(buf[i])))
			*errp = 1;
		result = (result * 10) + buf[i] - '0';
	}
	if (result < min)
		*errp = 1;
	if (result > max)
		*errp = 1;
	return (result);
}

/*! \file */