summaryrefslogtreecommitdiff
path: root/protocols/MRA/src/Sdk/InternetTime.h
blob: dae0ee0ce39a060cb68a38984617f8573ec89e77 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/*
 * Copyright (c) 2003 Rozhuk Ivan <rozhuk.im@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */



#if !defined(AFX_INTERNET_TIME_H__F58D13FF_F6F2_476C_B8F0_7B9E9357CF48__INCLUDED_)
#define AFX_INTERNET_TIME_H__F58D13FF_F6F2_476C_B8F0_7B9E9357CF48__INCLUDED_


#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include "Sdk/StrToNum.h"
#include "Sdk/MemoryFind.h"
#include "Sdk/MemoryFindByte.h"

typedef struct
{
	LONG		lTimeZone;
	SYSTEMTIME	stTime;
} INTERNET_TIME;


static LPCSTR lpcszenmMonthEnum[13]=	{"---","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
static LPCSTR lpcszenmDayOfWeakEnum[7]=	{"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};

#define CRLF "\r\n"
#define LF "\n"
//WSP++
#define LWSHT "\r\n\t"
#define LWSSP "\r\n "
//WSP--

//FWS++
#define FWSHT LWSHT
#define FWSSP LWSSP
//FWS--

#define SEPARATOR "\r\n\r\n"


//DWORD	InternetTimeGetTime	(LPCSTR lpszTime,size_t dwTimeSize,INTERNET_TIME *pitTime);
//BOOL	SkeepSPWSP			(LPCSTR lpszBuff,size_t dwBuffSize,LPSTR *plpszRetBuff,size_t *pdwRetBuffSize);
//BOOL	WSP2SP				(LPCSTR lpszBuff,size_t dwBuffSize,LPSTR lpszRetBuff,size_t *pdwRetBuffSize);





__inline BOOL SkeepSPWSP(LPCSTR lpszBuff,size_t dwBuffSize,LPSTR *plpszRetBuff,size_t *pdwRetBuffSize)
{
	BOOL bRet=TRUE;

	if (lpszBuff && dwBuffSize && (plpszRetBuff || pdwRetBuffSize))
	{
		while ((*lpszBuff)<33 && dwBuffSize)
		{
			dwBuffSize--;
			lpszBuff++;
		}

		if (plpszRetBuff)	(*plpszRetBuff)=(LPSTR)lpszBuff;
		if (pdwRetBuffSize)	(*pdwRetBuffSize)=dwBuffSize;
	}else{
		bRet=FALSE;
	}
return(bRet);
}


__inline BOOL WSP2SP(LPCSTR lpszBuff,size_t dwBuffSize,LPSTR lpszRetBuff,size_t *pdwRetBuffSize)
{//	WSP->SP
	BOOL bRet=TRUE;

	if (lpszBuff && dwBuffSize && lpszRetBuff)
	{
		LPSTR lpszCurReadPos,pCRLF,lpszCurWritePos;
		size_t dwToCopy,dwRetBuffSize;

		pCRLF=(LPSTR)lpszBuff;
		lpszCurReadPos=(LPSTR)lpszBuff;
		lpszCurWritePos=lpszRetBuff;
		dwRetBuffSize=0;

		while(pCRLF)
		{
			pCRLF=(LPSTR)MemoryFind((pCRLF-lpszBuff),lpszBuff,(dwBuffSize-1),CRLF,2);
			if (pCRLF)
			{
				pCRLF+=2;
				if ((*pCRLF)==9 || (*pCRLF)==32)// LWS: <US-ASCII HT, horizontal-tab (9)> || <US-ASCII SP, space (32)>
				{
					dwToCopy=((pCRLF-2)-lpszCurReadPos);
					pCRLF++;

					memmove((LPVOID)lpszCurWritePos,(CONST VOID*)lpszCurReadPos,dwToCopy);
					dwRetBuffSize+=(dwToCopy+1);
					lpszCurWritePos+=dwToCopy;
					lpszCurWritePos[0]=32;
					lpszCurWritePos++;
					lpszCurReadPos=pCRLF;
				}
			}else{
				dwToCopy=((lpszBuff+dwBuffSize)-lpszCurReadPos);
				dwRetBuffSize+=dwToCopy;
				memmove((LPVOID)lpszCurWritePos,(CONST VOID*)lpszCurReadPos,dwToCopy);
			}
		}

		if (pdwRetBuffSize) (*pdwRetBuffSize)=dwRetBuffSize;
	}else{
		bRet=FALSE;
	}
return(bRet);
}



__inline BOOL HT2SP(LPCSTR lpszBuff,size_t dwBuffSize,LPSTR lpszRetBuff,size_t *pdwRetBuffSize)
{//	HT->SP
	BOOL bRet=TRUE;

	if (lpszBuff && dwBuffSize && lpszRetBuff)
	{
		LPSTR lpszCurReadPos,pHT,lpszCurWritePos;
		size_t dwToCopy,dwRetBuffSize;

		pHT=(LPSTR)lpszBuff;
		lpszCurReadPos=(LPSTR)lpszBuff;
		lpszCurWritePos=lpszRetBuff;
		dwRetBuffSize=0;

		while(pHT)
		{
			pHT=(LPSTR)MemoryFind((pHT-lpszBuff),lpszBuff,dwBuffSize,"\t",1);
			if (pHT)
			{
				dwToCopy=(pHT-lpszCurReadPos);
				pHT++;

				dwRetBuffSize+=(dwToCopy+1);
				memmove((LPVOID)lpszCurWritePos,(CONST VOID*)lpszCurReadPos,dwToCopy);
				lpszCurWritePos+=dwToCopy;
				lpszCurWritePos[0]=32;
				lpszCurWritePos++;
				lpszCurReadPos=pHT;
			}else{
				dwToCopy=((lpszBuff+dwBuffSize)-lpszCurReadPos);
				dwRetBuffSize+=dwToCopy;
				memmove((LPVOID)lpszCurWritePos,(CONST VOID*)lpszCurReadPos,dwToCopy);
			}
		}

		if (pdwRetBuffSize) (*pdwRetBuffSize)=dwRetBuffSize;
	}else{
		bRet=FALSE;
	}
return(bRet);
}




__inline BOOL CleanUnneededSP(LPCSTR lpszBuff,size_t dwBuffSize,LPSTR lpszRetBuff,size_t *pdwRetBuffSize)
{//	nSP->SP, SPCRLF->CRLF, CRLFSP->CRLF
	BOOL bRet=TRUE;

	if (lpszBuff && dwBuffSize && lpszRetBuff)
	{
		LPSTR lpszCurReadPos,pSP,pSPStart,pSPEnd,lpszCurWritePos,pEnd;
		size_t dwToCopy,dwRetBuffSize;

		pSP=(LPSTR)lpszBuff;
		lpszCurReadPos=(LPSTR)lpszBuff;
		lpszCurWritePos=lpszRetBuff;
		pEnd=((LPSTR)lpszBuff+dwBuffSize);
		dwRetBuffSize=0;

		while(pSP)
		{
			pSP=(LPSTR)MemoryFind((pSP-lpszBuff),lpszBuff,dwBuffSize," ",1);
			if (pSP)
			{
				dwToCopy=(pSP-lpszCurReadPos);
				pSPStart=pSP;
				pSPEnd=pSP;
				while((*pSPEnd)==32 && pSPEnd<pEnd) pSPEnd++;

				// check SP on line start
				if ((pSPStart-1)>lpszBuff)
				{
					if ((*((WORD*)(pSPStart-2)))!=(*((WORD*)CRLF))) dwToCopy++;
				}else{// buff start
					if (pSPStart>lpszBuff) dwToCopy++;
				}

				// check SP on line end
				if ((pSPEnd+1)<=pEnd)
				{
					if ((*((WORD*)pSPEnd))!=(*((WORD*)CRLF))) dwToCopy++;
				}else{// buff start
					if (pSPEnd>lpszBuff) dwToCopy++;
				}

				memmove((LPVOID)lpszCurWritePos,(CONST VOID*)lpszCurReadPos,dwToCopy);
				lpszCurWritePos+=dwToCopy;
				dwRetBuffSize+=dwToCopy;
				lpszCurReadPos=pSPEnd;
				pSP=pSPEnd;
			}else{
				dwToCopy=((lpszBuff+dwBuffSize)-lpszCurReadPos);
				dwRetBuffSize+=dwToCopy;
				memmove((LPVOID)lpszCurWritePos,(CONST VOID*)lpszCurReadPos,dwToCopy);
			}
		}

		if (pdwRetBuffSize) (*pdwRetBuffSize)=dwRetBuffSize;
	}else{
		bRet=FALSE;
	}
return(bRet);
}




__inline size_t CopyText(LPVOID lpOutBuff,LPCVOID lpcBuff,size_t dwLen)
{
	size_t dwRet=0;

	if (lpOutBuff && lpcBuff && dwLen)
	{
		BYTE bt;
		LPBYTE lpbtIn=(LPBYTE)lpcBuff,lpbtOut=(LPBYTE)lpOutBuff;

		for(size_t i=dwLen;i;i--)
		{
			bt=(*(lpbtIn++));
			if (bt<127 && (bt>31 || bt==9 || bt==10 || bt==13))
			{
				(*(lpbtOut++))=bt;
				dwRet++;
			}
		}
	}
return(dwRet);
}


__inline void InternetTimeGetCurrentTime(INTERNET_TIME *pitTime)
{
	TIME_ZONE_INFORMATION tzi={0};
	GetTimeZoneInformation(&tzi);
	pitTime->lTimeZone=tzi.Bias;
	GetSystemTime(&pitTime->stTime);
}


__inline DWORD InternetTimeGetString(INTERNET_TIME *pitTime,LPSTR lpszBuff,size_t dwBuffSize,size_t *pdwBuffSizeRet)
{//	Ïåðåâîäèò âðåìÿ èç MAILTIME â ñòðîêîâîå
	DWORD dwRet=NO_ERROR;

	if (dwBuffSize>31)
	{
		LPSTR lpszCurPos=lpszBuff;
		size_t dwTimeLen=0,dwtm;

		// day of weak// date of mounth// mounth name// year// hours // minutes// seconds
		dwtm=wsprintfA(lpszCurPos,"%s, %02lu %s %04lu %02lu:%02lu:%02lu ",lpcszenmDayOfWeakEnum[pitTime->stTime.wDayOfWeek],pitTime->stTime.wDay,lpcszenmMonthEnum[pitTime->stTime.wMonth],pitTime->stTime.wYear,pitTime->stTime.wHour,pitTime->stTime.wMinute,pitTime->stTime.wSecond);
		lpszCurPos+=dwtm;
		dwTimeLen+=dwtm;

		// time zone
		if (pitTime->lTimeZone)
		{
			if (pitTime->lTimeZone < 0)
			{// íóæíî äîáàâèòü ïëþñèê, ìèíóñ äîáàâëÿåòñÿ àâòîìàòîì
				(*((BYTE*)lpszCurPos))='+';
				lpszCurPos++;
				dwTimeLen++;
			}

			dwtm=wsprintfA(lpszCurPos,"%04ld",-(((pitTime->lTimeZone/60)*100)+pitTime->lTimeZone%60));
			lpszCurPos+=dwtm;
			dwTimeLen+=dwtm;
		}else{
			dwtm=wsprintfA(lpszCurPos,"GMT");
			lpszCurPos+=dwtm;
			dwTimeLen+=dwtm;
		}

		if (pdwBuffSizeRet) (*pdwBuffSizeRet)=dwTimeLen;
	}else{// ïåðåäàííûé áóôôåð ñëèøêîì ìàë
		if (pdwBuffSizeRet) (*pdwBuffSizeRet)=32;
		dwRet=ERROR_INSUFFICIENT_BUFFER;
	}
return(dwRet);
}



__inline DWORD InternetTimeGetTime(LPCSTR lpszTime,size_t dwTimeSize,INTERNET_TIME *pitTime)
{//	Ïåðåâîäèò âðåìÿ èç ñòðîêîâîãî â INTERNET_TIME
	DWORD dwRet=NO_ERROR;

	if (lpszTime && dwTimeSize && dwTimeSize<4097 && pitTime)
	{// = Thu, 21 May 1998 05:33:29 -0700 =
		char sztmBuff[4096];
		LPSTR lpszCurPos=sztmBuff,lpszTemp;
		size_t i,dwCurSize=4096,dwTemp;

		memset(pitTime, 0, sizeof(INTERNET_TIME));
		WSP2SP((LPSTR)lpszTime,dwTimeSize,lpszCurPos,&dwCurSize);

		if (dwCurSize>3)
		{// äåíü íåäåëè
			if (lpszCurPos[3]==',')
			{
				for (i=0;i<8;i++)
				{
					if (CompareStringA(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NORM_IGNORECASE,lpcszenmDayOfWeakEnum[i],3,lpszCurPos,3)==CSTR_EQUAL) 
					{
						pitTime->stTime.wDayOfWeek=(unsigned short)i;
						break;
					}
				}

				lpszCurPos+=4;
				dwCurSize-=4;
			}

			if (dwCurSize>2)
			{// äåíü ìåñÿöà
				SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);
				if ((lpszTemp=(LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,' ')))
				{
					dwTemp=(lpszTemp-lpszCurPos);
					pitTime->stTime.wDay=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

					lpszCurPos=(lpszTemp+1);
					dwCurSize-=(dwTemp+1);

					if (dwCurSize>3)
					{// ìåñÿö
						SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);

						for (i=1;i<14;i++)
						{
							if (CompareStringA(MAKELANGID(LANG_ENGLISH,SUBLANG_ENGLISH_US),NORM_IGNORECASE,lpcszenmMonthEnum[i],3,lpszCurPos,3)==CSTR_EQUAL)
							{
								pitTime->stTime.wMonth=(unsigned short)i;
								break;
							}
						}

						lpszCurPos+=3;
						dwCurSize-=3;


						if (dwCurSize>3)
						{// ãîä
							SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);
							if ((lpszTemp=(LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,' ')))
							{
								dwTemp=(lpszTemp-lpszCurPos);
								pitTime->stTime.wYear=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

								lpszCurPos=(lpszTemp+1);
								dwCurSize-=(dwTemp+1);

								if (dwCurSize>2)
								{ // ÷àñû
									SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);
									if ((lpszTemp=(LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,':')))
									{
										dwTemp=(lpszTemp-lpszCurPos);
										pitTime->stTime.wHour=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

										lpszCurPos=(lpszTemp+1);
										dwCurSize-=(dwTemp+1);

										if (dwCurSize>2)
										{// ìèíóòû
											SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);
											if ((lpszTemp=(LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,':')))
											{
												dwTemp=(lpszTemp-lpszCurPos);
												pitTime->stTime.wMinute=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

												lpszCurPos=(lpszTemp+1);
												dwCurSize-=(dwTemp+1);

												if (dwCurSize>2)
												{// ñåêóíäû, îíè åñòü
													if ((lpszTemp=(LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,' ')))
													{
														dwTemp=(lpszTemp-lpszCurPos);
														pitTime->stTime.wSecond=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

														lpszCurPos=(lpszTemp+1);
														dwCurSize-=(dwTemp+1);
													}
												}else{// çîíû íåò
													if (dwCurSize)
													{
														pitTime->stTime.wSecond=(unsigned short)StrToUNum32(lpszCurPos,dwCurSize);
														lpszCurPos+=dwCurSize;
														dwCurSize=0;
													}
												}
											}else{
												if ((lpszTemp=(LPSTR)MemoryFindByte(0,lpszCurPos,dwCurSize,' ')))
												{
													dwTemp=(lpszTemp-lpszCurPos);
													pitTime->stTime.wMinute=(unsigned short)StrToUNum32(lpszCurPos,dwTemp);

													lpszCurPos=(lpszTemp+1);
													dwCurSize-=(dwTemp+1);
												}
											}

											if (dwCurSize)
											{// ÷àñîâîé ïîÿñ
												SkeepSPWSP(lpszCurPos,dwCurSize,&lpszCurPos,&dwCurSize);
												pitTime->lTimeZone=(LONG)StrToNum(lpszCurPos,dwCurSize);
												if (pitTime->lTimeZone>1300 || pitTime->lTimeZone<-1200) pitTime->lTimeZone=2400;
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}else{
		dwRet=ERROR_INVALID_HANDLE;
	}
return(dwRet);
}



#endif // !defined(AFX_INTERNET_TIME_H__F58D13FF_F6F2_476C_B8F0_7B9E9357CF48__INCLUDED_)