📄 urlutil.h
字号:
//
// 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.
//
//+---------------------------------------------------------------------------------
//
//
// File:
// UrlUtil.h
//
// Contents:
//
// Url escaping utility functions
//
//----------------------------------------------------------------------------------
#ifndef __URLUTIL_H_INCLUDED__
#define __URLUTIL_H_INCLUDED__
#define CHAR_PERCENT 0x25
#define ESCAPE_MASK 0xFFFFFF80
extern CHAR s_ToHex[];
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: template<class F> inline bool IsEscapeCharacter(F ch)
//
// parameters:
//
// description:
// Decides whether character needs escaping or not. Character is escaped iff its value is
// greater than 0x7F (that is if (ch & ESCAPE_MASK) != 0) or it is '%' character
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class F> inline bool IsEscapeCharacter(F ch)
{
return (((F)ESCAPE_MASK & ch) != 0) || (ch == (F)CHAR_PERCENT);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: template<class F, class T> T HexDigitToValue(F digit)
//
// parameters:
//
// description:
// Converts Hexadecimal digit character into corresponding value
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class F, class T> T HexDigitToValue(F digit)
{
if (digit >= (F)'0' && digit <= (F)'9')
return (T)(digit - (F)'0');
else if (digit >= (F)'A' && digit <= (F)'F')
return (T)(digit - (F)'A' + 10);
else if (digit >= (F)'a' && digit <= (F)'f')
return (T)(digit - (F)'a' + 10);
else return (T)(-1);
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: template<class F> DWORD SizeToEscape(const F *unescaped)
//
// parameters:
//
// description:
// Returns number of characters (including terminating zero) needed to escape given URL.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class F> DWORD SizeToEscape(const F *unescaped)
{
if (unescaped)
{
DWORD dwSize = 1; // null terminator
for ( ; *unescaped; unescaped ++)
{
static DWORD s_addens[] = { 1, 3 };
dwSize += s_addens[IsEscapeCharacter(*unescaped)];
}
return dwSize;
}
else
{
return 0;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: template<class F> DWORD SizeToUnescape(const F *escaped)
//
// parameters:
//
// description:
// Returns number of characters (including terminating zero) needed to unescape given URL.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class F> DWORD SizeToUnescape(const F *escaped)
{
if (escaped)
{
DWORD dwSize = 1; // null terminator;
for ( ; *escaped; )
{
if ( *escaped ++ == (F)CHAR_PERCENT)
{
// 2 following characters are the encoding
if (*escaped) escaped ++;
if (*escaped) escaped ++;
}
dwSize ++;
}
return dwSize;
}
else
{
return 0;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: template<class F, class T> HRESULT DoEscapeUrl(const F *unescaped, T *escaped)
//
// parameters:
//
// description:
// Performs URL escape work (assumes buffers are of correct sizes)
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class F, class T> HRESULT DoEscapeUrl(const F *unescaped, T *escaped)
{
while (*unescaped)
{
F unesc = (F) *unescaped ++;
if (IsEscapeCharacter(unesc))
{
*escaped ++ = (T)'%';
*escaped ++ = (T)s_ToHex[((unesc) >> 4) & 0xF];
*escaped ++ = (T)s_ToHex[(unesc) & 0xF];
}
else
{
*escaped ++ = (T) unesc;
}
}
ASSERT(*unescaped == 0);
*escaped = (T)0; // null terminator
return S_OK;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: template<class F, class T> HRESULT EscapeUrl(const F *unescaped, T *escaped, DWORD *size)
//
// parameters:
//
// description:
// Escapes URL. If escaped is null, returns only the buffer size (in characters) needed to
// escape the url. This size includes the null string terminator.
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class F, class T> HRESULT EscapeUrl(const F *unescaped, T *escaped, DWORD *size)
{
HRESULT hr = S_OK;
DWORD dwSize = SizeToEscape<F>(unescaped);
if (unescaped && dwSize && escaped && size && *size >= dwSize)
{
CHK((DoEscapeUrl<F, T>(unescaped, escaped)));
}
if (size)
{
*size = dwSize;
}
Cleanup:
return hr;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
// function: template<class F, class T> HRESULT DoUnescapeUrl(const F *escaped, T*unescaped)
//
// parameters:
//
// description:
// Performs URL unescape work (assumes buffers are of correct sizes)
// returns:
//
////////////////////////////////////////////////////////////////////////////////////////////////////
template<class F, class T> HRESULT DoUnescapeUrl(const F *escaped, T*unescaped)
{
while (*escaped)
{
F esc = *escaped ++;
if ( esc == (F)CHAR_PERCENT )
{
// 2 following characters are the encoding
T unesc = 0;
T u = 0;
if (*escaped && (u = HexDigitToValue<F, T>(*escaped ++)) != (T)(-1))
{
unesc = u << 4;
}
else
return E_INVALIDARG;
if (*escaped && (u = HexDigitToValue<F, T>(*escaped ++)) != (T)(-1))
{
unesc |= u;
}
else
return E_INVALIDARG;
*unescaped++ = unesc;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -