155682Smarkm/*
255682Smarkm * Copyright (c) 1997 Kungliga Tekniska H�gskolan
355682Smarkm * (Royal Institute of Technology, Stockholm, Sweden).
455682Smarkm * All rights reserved.
555682Smarkm *
655682Smarkm * Redistribution and use in source and binary forms, with or without
755682Smarkm * modification, are permitted provided that the following conditions
855682Smarkm * are met:
955682Smarkm *
1055682Smarkm * 1. Redistributions of source code must retain the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer.
1255682Smarkm *
1355682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1455682Smarkm *    notice, this list of conditions and the following disclaimer in the
1555682Smarkm *    documentation and/or other materials provided with the distribution.
1655682Smarkm *
1755682Smarkm * 3. Neither the name of the Institute nor the names of its contributors
1855682Smarkm *    may be used to endorse or promote products derived from this software
1955682Smarkm *    without specific prior written permission.
2055682Smarkm *
2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155682Smarkm * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#include "der_locl.h"
3555682Smarkm
36178825SdfrRCSID("$Id: timegm.c 21366 2007-06-27 10:06:22Z lha $");
3755682Smarkm
3855682Smarkmstatic int
3955682Smarkmis_leap(unsigned y)
4055682Smarkm{
4155682Smarkm    y += 1900;
4255682Smarkm    return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
4355682Smarkm}
4455682Smarkm
45178825Sdfr/*
46178825Sdfr * This is a simplifed version of timegm(3) that doesn't accept out of
47178825Sdfr * bound values that timegm(3) normally accepts but those are not
48178825Sdfr * valid in asn1 encodings.
49178825Sdfr */
50178825Sdfr
5155682Smarkmtime_t
52178825Sdfr_der_timegm (struct tm *tm)
5355682Smarkm{
5455682Smarkm  static const unsigned ndays[2][12] ={
5555682Smarkm    {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
5655682Smarkm    {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
5755682Smarkm  time_t res = 0;
5855682Smarkm  unsigned i;
5955682Smarkm
60178825Sdfr  if (tm->tm_year < 0)
61178825Sdfr      return -1;
62178825Sdfr  if (tm->tm_mon < 0 || tm->tm_mon > 11)
63178825Sdfr      return -1;
64178825Sdfr  if (tm->tm_mday < 1 || tm->tm_mday > ndays[is_leap(tm->tm_year)][tm->tm_mon])
65178825Sdfr      return -1;
66178825Sdfr  if (tm->tm_hour < 0 || tm->tm_hour > 23)
67178825Sdfr      return -1;
68178825Sdfr  if (tm->tm_min < 0 || tm->tm_min > 59)
69178825Sdfr      return -1;
70178825Sdfr  if (tm->tm_sec < 0 || tm->tm_sec > 59)
71178825Sdfr      return -1;
72178825Sdfr
7355682Smarkm  for (i = 70; i < tm->tm_year; ++i)
7455682Smarkm    res += is_leap(i) ? 366 : 365;
7555682Smarkm
7655682Smarkm  for (i = 0; i < tm->tm_mon; ++i)
7755682Smarkm    res += ndays[is_leap(tm->tm_year)][i];
7855682Smarkm  res += tm->tm_mday - 1;
7955682Smarkm  res *= 24;
8055682Smarkm  res += tm->tm_hour;
8155682Smarkm  res *= 60;
8255682Smarkm  res += tm->tm_min;
8355682Smarkm  res *= 60;
8455682Smarkm  res += tm->tm_sec;
8555682Smarkm  return res;
8655682Smarkm}
87