summaryrefslogtreecommitdiffstats
path: root/net/khttpd/rfc_time.c
blob: 8672cce7c4b9338a5136dc9b854a1966c3f98422 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*

Functions related to time:

1) rfc (string) time to unix-time
2) unix-time to rfc (string) time
3) current time to rfc (string) time for the "Date:" header

*/

/****************************************************************
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation; either version 2, or (at your option)
 *	any later version.
 *
 *	This program 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 this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 ****************************************************************/

#include <linux/time.h>
#include <linux/kernel.h>
#include <linux/malloc.h>
#include <linux/ctype.h>


#include "times.h"
#include "prototypes.h"
static char *dayName[7] = {
	"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};

static char *monthName[12] = {
	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};


char CurrentTime[64];
int  CurrentTime_i;


static char itoa_h[60]={'0','0','0','0','0','0','0','0','0','0',
		 '1','1','1','1','1','1','1','1','1','1',
		 '2','2','2','2','2','2','2','2','2','2',
		 '3','3','3','3','3','3','3','3','3','3',
		 '4','4','4','4','4','4','4','4','4','4',
		 '5','5','5','5','5','5','5','5','5','5'};
		
static char itoa_l[60]={'0','1','2','3','4','5','6','7','8','9',
		 '0','1','2','3','4','5','6','7','8','9',
		 '0','1','2','3','4','5','6','7','8','9',
		 '0','1','2','3','4','5','6','7','8','9',
		 '0','1','2','3','4','5','6','7','8','9',
		 '0','1','2','3','4','5','6','7','8','9'};
void time_Unix2RFC(const time_t Zulu,char *Buffer)
{
	int Y=0,M=0,D=0;
	int H=0,Min=0,S=0,WD=0;
	int I,I2;
	time_t rest;
	
	
		
	I=0;
	while (I<KHTTPD_NUMYEARS)
	{
		if (TimeDays[I][0]>Zulu) 
		   break;
		I++;
	}
	
	Y=--I;
	if (I<0) 
	{
		Y=0;
		goto BuildYear;
	}
	I2=0;
	while (I2<=12)
	{
		if (TimeDays[I][I2]>Zulu) 
		   break;
		I2++;
	}			   
	
	M=I2-1;
	
	rest=Zulu - TimeDays[Y][M];
	WD=WeekDays[Y][M];
	D=rest/86400;
	rest=rest%86400;
	WD+=D;
	WD=WD%7;
	H=rest/3600;
	rest=rest%3600;
	Min=rest/60;
	rest=rest%60;
	S=rest;
	
BuildYear:
	Y+=KHTTPD_YEAROFFSET;
	
	
	/* Format:  Day, 01 Mon 1999 01:01:01 GMT */
	
/*	
	We want to do 
	
	sprintf( Buffer, "%s, %02i %s %04i %02i:%02i:%02i GMT",
		dayName[ WD ], D+1, monthName[ M ], Y,
		H, Min, S
	);
	
	but this is very expensive. Since the string is fixed length,
	it is filled manually.
*/
	Buffer[0]=dayName[WD][0];
	Buffer[1]=dayName[WD][1];
	Buffer[2]=dayName[WD][2];
	Buffer[3]=',';
	Buffer[4]=' ';
	Buffer[5]=itoa_h[D+1];
	Buffer[6]=itoa_l[D+1];
	Buffer[7]=' ';
	Buffer[8]=monthName[M][0];
	Buffer[9]=monthName[M][1];
	Buffer[10]=monthName[M][2];
	Buffer[11]=' ';
	Buffer[12]=itoa_l[Y/1000];
	Buffer[13]=itoa_l[(Y/100)%10];
	Buffer[14]=itoa_l[(Y/10)%10];
	Buffer[15]=itoa_l[Y%10];
	Buffer[16]=' ';
	Buffer[17]=itoa_h[H];
	Buffer[18]=itoa_l[H];
	Buffer[19]=':';
	Buffer[20]=itoa_h[Min];
	Buffer[21]=itoa_l[Min];
	Buffer[22]=':';
	Buffer[23]=itoa_h[S];
	Buffer[24]=itoa_l[S];
	Buffer[25]=' ';
	Buffer[26]='G';
	Buffer[27]='M';
	Buffer[28]='T';
	Buffer[29]=0;
	
	
		
	
}

void UpdateCurrentDate(void)
{
   struct timeval tv;
   
   do_gettimeofday(&tv);
   if (CurrentTime_i!=tv.tv_sec)
	   time_Unix2RFC(tv.tv_sec,CurrentTime);
   
   CurrentTime_i = tv.tv_sec;
}

static int MonthHash[32] = {0,0,7,0,0,0,0,0,0,0,0,3,0,0,0,2,6,0,5,0,9,8,4,0,0,11,1,10,0,0,0,0};

#define is_digit(c)	((c) >= '0' && (c) <= '9')

__inline static int skip_atoi(char **s)
{
	int i=0;

	while (is_digit(**s))
		i = i*10 + *((*s)++) - '0';
	return i;
}

time_t mimeTime_to_UnixTime(char *Q)
{
	int Y,M,D,H,Min,S;
	unsigned int Hash;
	time_t Temp;
	char *s,**s2;
	
	s=Q;
	s2=&s;
	
	if (strlen(s)<30) return 0;
	if (s[3]!=',') return 0;
	if (s[19]!=':') return 0;
	
	s+=5; /* Skip day of week */
	D = skip_atoi(s2);  /*  Day of month */
	s++;
	Hash = (unsigned char)s[0]+(unsigned char)s[2];
	Hash = (Hash<<1) + (unsigned char)s[1];
	Hash = (Hash&63)>>1;
	M = MonthHash[Hash];
	s+=4;
	Y = skip_atoi(s2); /* Year */
	s++;
	H = skip_atoi(s2); /* Hour */
	s++;
	Min = skip_atoi(s2); /* Minutes */
	s++;
	S = skip_atoi(s2); /* Seconds */
	s++;
	if ((s[0]!='G')||(s[1]!='M')||(s[2]!='T')) 
	{	
  		return 0; /* No GMT */
  	}

	if (Y<KHTTPD_YEAROFFSET) Y = KHTTPD_YEAROFFSET;
	if (Y>KHTTPD_YEAROFFSET+9) Y = KHTTPD_YEAROFFSET+9;
	
	Temp = 	TimeDays[Y-KHTTPD_YEAROFFSET][M];
	Temp += D*86400+H*3600+Min*60+S;
	
	return Temp;  
}