📄 srmpparse.cxx
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
/*++
Module Name:
SrmpParse.cxx
Abstract:
Contains basic parsing related functions.
--*/
#include <windows.h>
#include <msxml2.h>
#include <svsutil.hxx>
#include "sc.hxx"
#include "scdefs.hxx"
#include "SrmpAccept.hxx"
#include "SrmpParse.hxx"
#include "SrmpDefs.hxx"
#include "scutil.hxx"
const WCHAR cszMSMQPrefix[] = L"MSMQ:";
const DWORD ccMSMQPrefix = SVSUTIL_CONSTSTRLEN(cszMSMQPrefix);
BOOL IsUriHttpOrHttps(const WCHAR *szQueue) {
return IsUriHttp(szQueue) || IsUriHttps(szQueue);
}
BOOL IsUriMSMQ(const WCHAR *szQueue) {
return (0 == _wcsnicmp(szQueue,cszMSMQPrefix,ccMSMQPrefix));
}
BOOL UriToQueueFormat(const WCHAR *szQueue, DWORD dwQueueChars, QUEUE_FORMAT *pQueue, WCHAR **ppszQueueBuffer) {
BOOL fMSMQ = FALSE;
WCHAR *szFormatName;
BOOL fConverted = FALSE;
gMem->Lock();
// 1st try to route potential free-form URI to something MSMQ can understand.
if (gMachine->RouteLocal((WCHAR*)szQueue,&szFormatName))
fConverted = TRUE;
else
szFormatName = (WCHAR*)szQueue;
gMem->Unlock();
if (IsUriMSMQ(szQueue)) {
szFormatName += ccMSMQPrefix; // increment past "MSMQ:"
fMSMQ = TRUE;
}
else if (!IsUriHttpOrHttps(szQueue) && !fConverted)
return FALSE;
if (*ppszQueueBuffer) {
g_funcFree(*ppszQueueBuffer,g_pvFreeData);
*ppszQueueBuffer = NULL;
}
// skip past "MSMQ:" prefix if it's present.
if (NULL == (*ppszQueueBuffer = svsutil_wcsdup(szFormatName)))
return FALSE;
if (fMSMQ || fConverted) {
if (! scutil_StringToQF(pQueue,(WCHAR*)(*ppszQueueBuffer)))
return FALSE;
}
else {
pQueue->DirectID(*ppszQueueBuffer);
}
return TRUE;
}
LARGE_INTEGER SecondsToStartOf1970 = {0xb6109100, 0x00000002};
// WinCE doesn't have gmtime, so we have to get fake it with a SYSTEMTIME and this fcn.
VOID SecondsSince1970ToSystemTime(ULONG ElapsedSeconds, SYSTEMTIME *pSystemTime) {
LARGE_INTEGER Seconds;
// Move elapsed seconds to a large integer
Seconds.LowPart = ElapsedSeconds;
Seconds.HighPart = 0;
// Convert number of seconds from 1970 to number of seconds from 1601
Seconds.QuadPart = Seconds.QuadPart + SecondsToStartOf1970.QuadPart;
// Convert seconds to 100ns resolution
Seconds.QuadPart = Seconds.QuadPart * (10*1000*1000);
FileTimeToSystemTime((FILETIME*)&Seconds,pSystemTime);
}
// converts time in time_t format into a string of ISO860 format.
BOOL UtlTimeToIso860Time(time_t tm, WCHAR *szBuffer, DWORD ccBuffer) {
SYSTEMTIME st;
SecondsSince1970ToSystemTime(tm,&st);
return SUCCEEDED(StringCchPrintfW(szBuffer,ccBuffer,
L"%04d%02d%02dT%02d%02d%02d",st.wYear,st.wMonth,
st.wDay,st.wHour,st.wMinute,st.wSecond));
}
// convert Iso860 absolute time format to system time format
BOOL UtlIso8601TimeToSystemTime(const WCHAR *szIso860Time, DWORD cbIso860Time, SYSTEMTIME* pSysTime) {
const WCHAR *szTrav = szIso860Time;
DWORD year;
DWORD month;
DWORD day;
DWORD hour;
if (cbIso860Time < 10)
return FALSE;
memset(pSysTime, 0, sizeof(SYSTEMTIME));
if (4 != swscanf(szTrav, L"%04d%02d%02dT%02d", &year, &month, &day, &hour))
return FALSE;
if ((month > 12) || (day > 31) ||(hour > 24))
return FALSE;
pSysTime->wYear = (WORD)year;
pSysTime->wMonth = (WORD)month;
pSysTime->wDay = (WORD)day;
pSysTime->wHour = (WORD)hour;
szTrav += 11;
DWORD remaningLength = cbIso860Time - 11;
if (remaningLength == 0)
return TRUE;
if (remaningLength < 2)
return FALSE;
if (1 != swscanf(szTrav, L"%02d", &pSysTime->wMinute))
return FALSE;
if (pSysTime->wMinute > 59)
return FALSE;
szTrav += 2;
remaningLength -= 2;
if (remaningLength == 0)
return TRUE;
if (remaningLength != 2)
return FALSE;
if (1 != swscanf(szTrav, L"%02d", &pSysTime->wSecond))
return FALSE;
if (pSysTime->wSecond > 59)
return FALSE;
return TRUE;
}
// convert system time to c runtime time integer
BOOL UtlSystemTimeToCrtTime(const SYSTEMTIME *pSysTime, time_t *pTime) {
FILETIME FileTime;
if (!SystemTimeToFileTime(pSysTime, &FileTime))
return FALSE;
// SystemTimeToFileTime() returns the system time in number
// of 100-nanosecond intervals since January 1, 1601. We
// should return the number of seconds since January 1, 1970.
// So we should subtract the number of 100-nanosecond intervals
// since January 1, 1601 up until January 1, 1970, then divide
// the result by 10**7.
LARGE_INTEGER* pliFileTime = (LARGE_INTEGER*)&FileTime;
pliFileTime->QuadPart -= 0x019db1ded53e8000;
pliFileTime->QuadPart /= 10000000;
SVSUTIL_ASSERT(FileTime.dwHighDateTime == 0);
*pTime = min(FileTime.dwLowDateTime, LONG_MAX);
return TRUE;
}
// convert relative time duration string (Iso8601 5.5.3.2) to integer
BOOL UtlIso8601TimeDuration(const WCHAR *szTimeDuration, DWORD cbTimeDuration, time_t *pTime) {
const WCHAR szTimeDurationPrefix[] = L"P";
const DWORD cbTimeDurationPrefix = SVSUTIL_CONSTSTRLEN(szTimeDurationPrefix);
LPCWSTR pTrav = szTimeDuration + cbTimeDurationPrefix;
LPCWSTR pEnd = szTimeDuration + cbTimeDuration;
if (cbTimeDuration <= cbTimeDurationPrefix)
return FALSE;
if (0 != wcsncmp(szTimeDuration,szTimeDurationPrefix,cbTimeDurationPrefix))
return FALSE;
DWORD years = 0;
DWORD months = 0;
DWORD hours = 0;
DWORD days = 0;
DWORD minutes = 0;
DWORD seconds = 0;
bool fTime = false;
DWORD temp = 0;
while(pTrav++ != pEnd) {
if (iswdigit(*pTrav)) {
temp = temp*10 + (*pTrav -L'0');
continue;
}
switch(*pTrav) {
case L'Y':
case L'y':
years = temp;
break;
case L'M':
case L'm':
if (fTime)
minutes = temp;
else
months = temp;
break;
case L'D':
case L'd':
days = temp;
break;
case L'H':
case L'h':
hours = temp;
break;
case L'S':
case L's':
seconds = temp;
break;
case L'T':
case L't':
fTime = true;
break;
default:
return FALSE;
break;
}
temp = 0;
}
months += (years * 12);
days += (months * 30);
hours += (days * 24);
minutes += (hours * 60);
seconds += (minutes * 60);
*pTime = min(seconds ,LONG_MAX);
return TRUE;
}
const BYTE bAscii2Base64[128] = {
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 62, 255, 255, 255, 63, 52, 53,
54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
255, 0, 255, 255, 255, 0, 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, 255, 255, 255, 255, 255, 255, 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, 255, 255, 255, 255, 255
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -