Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

368 lines
11 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1994 **/
  4. /**********************************************************************/
  5. /*
  6. string.hxx
  7. This module contains a light weight string class
  8. FILE HISTORY:
  9. Johnl 15-Aug-1994 Created
  10. MuraliK 09-July-1996 Rewrote for efficiency with no unicode support
  11. */
  12. #ifndef _STRING_HXX_
  13. #define _STRING_HXX_
  14. # include <buffer.hxx>
  15. //
  16. // Maximum number of characters a loadable string resource can be
  17. //
  18. # define STR_MAX_RES_SIZE ( 320)
  19. class IRTL_DLLEXP STR;
  20. //
  21. // If an application defines STR_MODULE_NAME, it will be used
  22. // as the default module name on string loads
  23. //
  24. #ifndef STR_MODULE_NAME
  25. #define STR_MODULE_NAME NULL
  26. #endif
  27. //
  28. // These are the characters that are considered to be white space
  29. //
  30. #define ISWHITE( ch ) ((ch) == L'\t' || (ch) == L' ' || (ch) == L'\r')
  31. #define ISWHITEA( ch ) ((ch) == '\t' || (ch) == ' ' || (ch) == '\r')
  32. //
  33. // Removes useless segments from the URL and makes sure it doesn't go
  34. // past the root of the tree (i.e., "/foo/../..")
  35. //
  36. INT CanonURL( CHAR * pszPath, BOOL fIsDBCS = FALSE );
  37. //
  38. // Map these string functions to appropriate RT call depending on the code
  39. // page of the system. Thus we only use DBCS operations when necessary.
  40. //
  41. DWORD InitializeStringFunctions( VOID );
  42. UCHAR * IISstrupr( UCHAR * pszString );
  43. UCHAR * IISstrlwr( UCHAR * pszString );
  44. INT IISstrnicmp( UCHAR * pszString1, UCHAR * pszString2, size_t iSize );
  45. size_t IISstrlen( UCHAR * pszString1 );
  46. char * IISstrncpy (char * dest, const char * source, size_t count);
  47. INT IISstricmp( UCHAR * pszString1, UCHAR * pszString2);
  48. UCHAR *IISstrrchr(const UCHAR * pszString, UINT c);
  49. /*++
  50. class STR:
  51. Intention:
  52. A light-weight string class supporting encapsulated string class.
  53. This object is derived from BUFFER class.
  54. It maintains following state:
  55. m_fValid - whether this object is valid -
  56. used only by STR() init functions
  57. * NYI: I need to kill this someday *
  58. m_cchLen - string length cached when we update the string.
  59. m_fNoRealloc - does not allow resize of the buffer. if overflow - return error / truncate
  60. Member Functions:
  61. There are two categories of functions:
  62. 1) Safe Functions - which do integrity checking of state
  63. 2) UnSafe Functions - which do not do integrity checking, but
  64. enable writing to the data stream freely.
  65. (someday this will be enabled as Safe versions without
  66. problem for users)
  67. --*/
  68. class IRTL_DLLEXP STR : public BUFFER
  69. {
  70. public:
  71. STR()
  72. : BUFFER (),
  73. m_cchLen ( 0),
  74. m_fNoRealloc (FALSE)
  75. {}
  76. // creates a stack version of the STR object - uses passed in stack buffer
  77. // STR does not free this pbInit on its own.
  78. STR( CHAR * pbInit, DWORD cbInit, BOOL fNoRealloc = FALSE)
  79. : BUFFER( (BYTE *) pbInit, cbInit),
  80. m_cchLen (0),
  81. m_fNoRealloc (fNoRealloc)
  82. {}
  83. STR( DWORD cbInit)
  84. : BUFFER( cbInit),
  85. m_cchLen (0),
  86. m_fNoRealloc (FALSE)
  87. {}
  88. STR( const CHAR * pchInit )
  89. : BUFFER (),
  90. m_cchLen ( 0),
  91. m_fNoRealloc (FALSE)
  92. { AuxInit( (const BYTE * ) pchInit); }
  93. STR( const STR & str )
  94. : BUFFER (),
  95. m_cchLen ( 0),
  96. m_fNoRealloc (FALSE)
  97. { AuxInit( (const BYTE * ) str.QueryStr()); }
  98. BOOL SetLen( IN DWORD cchLen)
  99. {
  100. return ( ( cchLen >= QuerySize())? FALSE :
  101. ( *((CHAR *) QueryPtr() + cchLen) = '\0', // null terminate
  102. m_cchLen = cchLen, // set the length
  103. TRUE
  104. )
  105. );
  106. }
  107. BOOL IsValid(VOID) const { return ( BUFFER::IsValid()) ; }
  108. //
  109. // Checks and returns TRUE if this string has no valid data else FALSE
  110. //
  111. BOOL IsEmpty( VOID) const { return ( *QueryStr() == '\0'); }
  112. BOOL Append( const CHAR * pchInit ) {
  113. return ((pchInit != NULL) ? (AuxAppend( (const BYTE * ) pchInit,
  114. (UINT) ::strlen(pchInit))) :
  115. TRUE);
  116. }
  117. BOOL Append( const CHAR * pchInit, DWORD cchLen ) {
  118. return ((pchInit != NULL) ? (AuxAppend( (const BYTE * ) pchInit,
  119. cchLen)) :
  120. TRUE);
  121. }
  122. BOOL Append( const STR & str )
  123. { return AuxAppend( (const BYTE * ) str.QueryStr(), str.QueryCCH()); }
  124. // Resets the internal string to be NULL string. Buffer remains cached.
  125. VOID Reset( VOID)
  126. { DBG_ASSERT( QueryPtr() != NULL);
  127. *(QueryStr()) = '\0'; m_cchLen = 0;
  128. }
  129. // erase the internal string. Buffer remains cached.
  130. VOID Clear( VOID)
  131. { DBG_ASSERT( QueryPtr() != NULL);
  132. ZeroMemory( QueryPtr(), QueryCB());
  133. Reset();
  134. }
  135. BOOL Copy( const CHAR * pchInit ) {
  136. if ( QueryPtr() ) { *(QueryStr()) = '\0'; m_cchLen = 0; }
  137. return ( (pchInit != NULL) ?
  138. AuxAppend( (const BYTE *) pchInit, (UINT) ::strlen( pchInit ), FALSE ):
  139. TRUE);
  140. }
  141. BOOL SafeCopy( const CHAR * pchInit );
  142. BOOL Copy( const CHAR * pchInit, IN DWORD cchLen ) {
  143. if ( QueryPtr() ) { *(QueryStr()) = '\0'; m_cchLen = 0; }
  144. return ( (pchInit != NULL) ?
  145. AuxAppend( (const BYTE *) pchInit, cchLen, FALSE ):
  146. TRUE);
  147. }
  148. BOOL Copy( const STR & str )
  149. { return ( Copy(str.QueryStr(), str.QueryCCH())); }
  150. //
  151. // Loads a string from this module's string resource table
  152. //
  153. BOOL LoadString( IN DWORD dwResID,
  154. IN LPCTSTR lpszModuleName = STR_MODULE_NAME,
  155. IN DWORD dwLangID = 0);
  156. BOOL LoadString( IN DWORD dwResID,
  157. IN HMODULE hModule);
  158. //
  159. // Loads a string with insert params from this module's .mc resource
  160. // table. Pass zero for the resource ID to use *this.
  161. //
  162. BOOL
  163. FormatString( IN DWORD dwResID,
  164. IN LPCTSTR apszInsertParams[],
  165. IN LPCTSTR lpszModuleName = STR_MODULE_NAME OPTIONAL,
  166. IN DWORD cbMaxMsg = 1024 OPTIONAL );
  167. //
  168. // Inserts and removes any odd ranged Latin-1 characters with the
  169. // escaped hexadecimal equivalent (%xx)
  170. //
  171. BOOL Escape();
  172. BOOL EscapeSpaces();
  173. BOOL Unescape();
  174. //
  175. // support for hashing the string for privacy purposes
  176. //
  177. VOID Hash();
  178. VOID Unhash();
  179. //
  180. // Returns the number of bytes in the string excluding the terminating
  181. // NULL
  182. //
  183. UINT QueryCB( VOID ) const
  184. { return ( m_cchLen * sizeof(CHAR)); }
  185. //
  186. // Returns # of characters in the string excluding the terminating NULL
  187. //
  188. UINT QueryCCH( VOID ) const { return (m_cchLen); }
  189. //
  190. // Makes a copy of the stored string in given buffer
  191. //
  192. BOOL CopyToBuffer( WCHAR * lpszBuffer, LPDWORD lpcch) const;
  193. BOOL CopyToBuffer( CHAR * lpszBuffer, LPDWORD lpcch) const;
  194. //
  195. // Return the string buffer
  196. //
  197. CHAR * QueryStrA( VOID ) const { return ( QueryStr()); }
  198. CHAR * QueryStr( VOID ) const { return ((CHAR *) QueryPtr()); }
  199. CHAR QueryFirstChar( VOID ) const
  200. { return (m_cchLen > 0) ? *QueryStr() : '\0'; }
  201. CHAR QueryLastChar( VOID ) const
  202. { return (m_cchLen > 0) ? *(QueryStr() + m_cchLen - 1) : '\0'; }
  203. //
  204. // Makes a clone of the current string in the string pointer passed in.
  205. //
  206. BOOL Clone( OUT STR * pstrClone) const
  207. {
  208. return ((pstrClone == NULL) ?
  209. (SetLastError(ERROR_INVALID_PARAMETER), FALSE) :
  210. (pstrClone->Copy( *this))
  211. );
  212. } // STR::Clone()
  213. BOOL Equ( const STR & strA) const {
  214. return (QueryCB() != strA.QueryCB()) ? FALSE :
  215. ( QueryCB() ? !IISstricmp((UCHAR *)QueryStr(), (UCHAR *)strA.QueryStr()) : TRUE );
  216. }
  217. BOOL Equ( CHAR *pszA) const {
  218. return QueryCB() ? !IISstricmp((UCHAR *)QueryStr(), (UCHAR *)pszA) : *pszA == '\0';
  219. }
  220. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  221. // UNSAFE FUNCTIONS - make sure you have enough space here
  222. // Use these only if you have been dumping bytes to string
  223. // object by considering it as a buffer object
  224. // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  225. // Append a single character
  226. VOID Append( CHAR ch)
  227. {
  228. register CHAR * pch = ((CHAR *) QueryPtr()) + (m_cchLen++);
  229. *pch = ch; *(pch+1) = '\0';
  230. }
  231. // Append two characters
  232. VOID Append( CHAR ch1, CHAR ch2)
  233. {
  234. register CHAR * pch = ((CHAR *) QueryPtr()) + (m_cchLen += 2);
  235. *(pch-2) = ch1; *(pch-1) = ch2; *pch = '\0';
  236. }
  237. // Append CRLF pattern \r\n --> use two character append function
  238. VOID AppendCRLF(VOID) { Append( '\r', '\n'); }
  239. private:
  240. DWORD m_cchLen;
  241. BOOL m_fNoRealloc;
  242. VOID AuxInit( const BYTE * pInit );
  243. BOOL AuxAppend( const BYTE * pInit,
  244. UINT cbStr, BOOL fAddSlop = TRUE );
  245. };
  246. //
  247. // Quick macro for declaring a STR that will use stack memory of <size>
  248. // bytes. If the buffer overflows then a heap buffer will be allocated
  249. //
  250. #define STACK_STR( name, size ) \
  251. CHAR __ach##name[size]; \
  252. STR name( __ach##name, sizeof( __ach##name ))
  253. //
  254. // Similar to STACK_STR, but suing the static buffer constructor, so no
  255. // realloc can occur
  256. //
  257. #define STACK_STATSTR( name, size ) \
  258. CHAR __ach##name[size]; \
  259. STR name( __ach##name, sizeof( __ach##name ), TRUE)
  260. //
  261. // Unlike the STACK_STR macro, this template can be used for member
  262. // variables in classes.
  263. //
  264. template <size_t N>
  265. class IRTL_DLLEXP Str : public STR
  266. {
  267. private:
  268. CHAR m_achData[N]; // Actual data
  269. public:
  270. Str()
  271. : STR(m_achData, N)
  272. {}
  273. }; // class Str<N>
  274. //
  275. // this template is similar to Str, but initializes STR to static buffer usage (no realloc)
  276. //
  277. template <size_t N>
  278. class StatStr : public STR
  279. {
  280. private:
  281. CHAR m_achData[N]; // Actual data
  282. public:
  283. StatStr()
  284. : STR(m_achData, N, TRUE)
  285. {}
  286. }; // class Str<N>
  287. #endif // !_STRING_HXX_