1*0a6a1f1dSLionel Sambuc /* $NetBSD: timegm.c,v 1.1.1.2 2014/04/24 12:45:28 pettai Exp $ */
2ebfedea0SLionel Sambuc
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc * Copyright (c) 1997 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc * All rights reserved.
7ebfedea0SLionel Sambuc *
8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc * are met:
11ebfedea0SLionel Sambuc *
12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc *
15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc *
19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc * without specific prior written permission.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc * SUCH DAMAGE.
34ebfedea0SLionel Sambuc */
35ebfedea0SLionel Sambuc
36ebfedea0SLionel Sambuc #include "der_locl.h"
37ebfedea0SLionel Sambuc
38*0a6a1f1dSLionel Sambuc #define ASN1_MAX_YEAR 2000
39ebfedea0SLionel Sambuc
40ebfedea0SLionel Sambuc static int
is_leap(unsigned y)41ebfedea0SLionel Sambuc is_leap(unsigned y)
42ebfedea0SLionel Sambuc {
43ebfedea0SLionel Sambuc y += 1900;
44ebfedea0SLionel Sambuc return (y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0);
45ebfedea0SLionel Sambuc }
46ebfedea0SLionel Sambuc
47ebfedea0SLionel Sambuc static const unsigned ndays[2][12] ={
48ebfedea0SLionel Sambuc {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
49ebfedea0SLionel Sambuc {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}};
50ebfedea0SLionel Sambuc
51ebfedea0SLionel Sambuc /*
52ebfedea0SLionel Sambuc * This is a simplifed version of timegm(3) that doesn't accept out of
53ebfedea0SLionel Sambuc * bound values that timegm(3) normally accepts but those are not
54ebfedea0SLionel Sambuc * valid in asn1 encodings.
55ebfedea0SLionel Sambuc */
56ebfedea0SLionel Sambuc
57ebfedea0SLionel Sambuc time_t
_der_timegm(struct tm * tm)58ebfedea0SLionel Sambuc _der_timegm (struct tm *tm)
59ebfedea0SLionel Sambuc {
60ebfedea0SLionel Sambuc time_t res = 0;
61*0a6a1f1dSLionel Sambuc int i;
62*0a6a1f1dSLionel Sambuc
63*0a6a1f1dSLionel Sambuc /*
64*0a6a1f1dSLionel Sambuc * See comment in _der_gmtime
65*0a6a1f1dSLionel Sambuc */
66*0a6a1f1dSLionel Sambuc if (tm->tm_year > ASN1_MAX_YEAR)
67*0a6a1f1dSLionel Sambuc return 0;
68ebfedea0SLionel Sambuc
69ebfedea0SLionel Sambuc if (tm->tm_year < 0)
70ebfedea0SLionel Sambuc return -1;
71ebfedea0SLionel Sambuc if (tm->tm_mon < 0 || tm->tm_mon > 11)
72ebfedea0SLionel Sambuc return -1;
73*0a6a1f1dSLionel Sambuc if (tm->tm_mday < 1 || tm->tm_mday > (int)ndays[is_leap(tm->tm_year)][tm->tm_mon])
74ebfedea0SLionel Sambuc return -1;
75ebfedea0SLionel Sambuc if (tm->tm_hour < 0 || tm->tm_hour > 23)
76ebfedea0SLionel Sambuc return -1;
77ebfedea0SLionel Sambuc if (tm->tm_min < 0 || tm->tm_min > 59)
78ebfedea0SLionel Sambuc return -1;
79ebfedea0SLionel Sambuc if (tm->tm_sec < 0 || tm->tm_sec > 59)
80ebfedea0SLionel Sambuc return -1;
81ebfedea0SLionel Sambuc
82ebfedea0SLionel Sambuc for (i = 70; i < tm->tm_year; ++i)
83ebfedea0SLionel Sambuc res += is_leap(i) ? 366 : 365;
84ebfedea0SLionel Sambuc
85ebfedea0SLionel Sambuc for (i = 0; i < tm->tm_mon; ++i)
86ebfedea0SLionel Sambuc res += ndays[is_leap(tm->tm_year)][i];
87ebfedea0SLionel Sambuc res += tm->tm_mday - 1;
88ebfedea0SLionel Sambuc res *= 24;
89ebfedea0SLionel Sambuc res += tm->tm_hour;
90ebfedea0SLionel Sambuc res *= 60;
91ebfedea0SLionel Sambuc res += tm->tm_min;
92ebfedea0SLionel Sambuc res *= 60;
93ebfedea0SLionel Sambuc res += tm->tm_sec;
94ebfedea0SLionel Sambuc return res;
95ebfedea0SLionel Sambuc }
96ebfedea0SLionel Sambuc
97ebfedea0SLionel Sambuc struct tm *
_der_gmtime(time_t t,struct tm * tm)98ebfedea0SLionel Sambuc _der_gmtime(time_t t, struct tm *tm)
99ebfedea0SLionel Sambuc {
100ebfedea0SLionel Sambuc time_t secday = t % (3600 * 24);
101ebfedea0SLionel Sambuc time_t days = t / (3600 * 24);
102ebfedea0SLionel Sambuc
103ebfedea0SLionel Sambuc memset(tm, 0, sizeof(*tm));
104ebfedea0SLionel Sambuc
105ebfedea0SLionel Sambuc tm->tm_sec = secday % 60;
106ebfedea0SLionel Sambuc tm->tm_min = (secday % 3600) / 60;
107ebfedea0SLionel Sambuc tm->tm_hour = secday / 3600;
108ebfedea0SLionel Sambuc
109*0a6a1f1dSLionel Sambuc /*
110*0a6a1f1dSLionel Sambuc * Refuse to calculate time ~ 2000 years into the future, this is
111*0a6a1f1dSLionel Sambuc * not possible for systems where time_t is a int32_t, however,
112*0a6a1f1dSLionel Sambuc * when time_t is a int64_t, that can happen, and this becomes a
113*0a6a1f1dSLionel Sambuc * denial of sevice.
114*0a6a1f1dSLionel Sambuc */
115*0a6a1f1dSLionel Sambuc if (days > (ASN1_MAX_YEAR * 365))
116*0a6a1f1dSLionel Sambuc return NULL;
117*0a6a1f1dSLionel Sambuc
118ebfedea0SLionel Sambuc tm->tm_year = 70;
119ebfedea0SLionel Sambuc while(1) {
120ebfedea0SLionel Sambuc unsigned dayinyear = (is_leap(tm->tm_year) ? 366 : 365);
121ebfedea0SLionel Sambuc if (days < dayinyear)
122ebfedea0SLionel Sambuc break;
123ebfedea0SLionel Sambuc tm->tm_year += 1;
124ebfedea0SLionel Sambuc days -= dayinyear;
125ebfedea0SLionel Sambuc }
126ebfedea0SLionel Sambuc tm->tm_mon = 0;
127ebfedea0SLionel Sambuc
128ebfedea0SLionel Sambuc while (1) {
129ebfedea0SLionel Sambuc unsigned daysinmonth = ndays[is_leap(tm->tm_year)][tm->tm_mon];
130ebfedea0SLionel Sambuc if (days < daysinmonth)
131ebfedea0SLionel Sambuc break;
132ebfedea0SLionel Sambuc days -= daysinmonth;
133ebfedea0SLionel Sambuc tm->tm_mon++;
134ebfedea0SLionel Sambuc }
135ebfedea0SLionel Sambuc tm->tm_mday = days + 1;
136ebfedea0SLionel Sambuc
137ebfedea0SLionel Sambuc return tm;
138ebfedea0SLionel Sambuc }
139