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.

153 lines
3.8 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1999 **/
  4. /**********************************************************************/
  5. /*
  6. string.cxx
  7. This module contains a light weight string class.
  8. FILE HISTORY:
  9. Johnl 15-Aug-1994 Created
  10. MuraliK 27-Feb-1995 Modified to be a standalone module with buffer.
  11. MuraliK 2-June-1995 Made into separate library
  12. */
  13. // Normal includes only for this module to be active
  14. //
  15. #include <nt.h>
  16. #include <ntrtl.h>
  17. #include <nturtl.h>
  18. #include <windows.h>
  19. #include <string.hxx>
  20. //
  21. // Private Definations
  22. //
  23. //
  24. // Converts a value between zero and fifteen to the appropriate hex digit
  25. //
  26. #define HEXDIGIT( nDigit ) \
  27. (CHAR)((nDigit) > 9 ? \
  28. (nDigit) - 10 + 'A' \
  29. : (nDigit) + '0')
  30. // Change a hexadecimal digit to its numerical equivalent
  31. #define TOHEX( ch ) \
  32. ((ch) > '9' ? \
  33. (ch) >= 'a' ? \
  34. (ch) - 'a' + 10 : \
  35. (ch) - 'A' + 10 \
  36. : (ch) - '0')
  37. //
  38. // When appending data, this is the extra amount we request to avoid
  39. // reallocations
  40. //
  41. #define STR_SLOP 128
  42. /***************************************************************************++
  43. Routine Description:
  44. Appends to the string starting at the (byte) offset cbOffset.
  45. Arguments:
  46. pStr - An ANSI string to be appended
  47. cbStr - Length, in bytes, of pStr
  48. cbOffset - Offset, in bytes, at which to begin the append
  49. fAddSlop - If we resize, should we add an extra STR_SLOP bytes.
  50. Return Value:
  51. HRESULT
  52. --***************************************************************************/
  53. HRESULT STRA::AuxAppend(const BYTE * pStr,
  54. ULONG cbStr,
  55. ULONG cbOffset,
  56. BOOL fAddSlop)
  57. {
  58. //
  59. // Only resize when we have to. When we do resize, we tack on
  60. // some extra space to avoid extra reallocations.
  61. //
  62. // Note: QuerySize returns the requested size of the string buffer,
  63. // *not* the strlen of the buffer
  64. //
  65. if ( m_Buff.QuerySize() < cbOffset + cbStr + sizeof(CHAR) )
  66. {
  67. UINT uNewSize = cbOffset + cbStr;
  68. if (fAddSlop) {
  69. uNewSize += STR_SLOP;
  70. } else {
  71. uNewSize += sizeof(CHAR);
  72. }
  73. if ( !m_Buff.Resize(uNewSize) ) {
  74. return HRESULT_FROM_WIN32(GetLastError());
  75. }
  76. }
  77. //
  78. // copy the exact string and append a null character
  79. //
  80. memcpy((BYTE *)m_Buff.QueryPtr() + cbOffset,
  81. pStr,
  82. cbStr);
  83. //
  84. // set the new length
  85. //
  86. m_cchLen = cbStr + cbOffset;
  87. //
  88. // append NULL character
  89. //
  90. *(QueryStr() + m_cchLen) = '\0';
  91. return S_OK;
  92. }
  93. HRESULT
  94. STRA::CopyToBuffer(
  95. CHAR * pszBuffer,
  96. DWORD * pcb
  97. ) const
  98. {
  99. HRESULT hr = S_OK;
  100. if ( pcb == NULL )
  101. {
  102. return HRESULT_FROM_WIN32( ERROR_INVALID_PARAMETER );
  103. }
  104. DWORD cbNeeded = QueryCCH() + 1;
  105. if ( *pcb >= cbNeeded &&
  106. pszBuffer != NULL )
  107. {
  108. //
  109. // Do the copy
  110. //
  111. memcpy(pszBuffer, QueryStr(), cbNeeded);
  112. }
  113. else
  114. {
  115. hr = HRESULT_FROM_WIN32( ERROR_INSUFFICIENT_BUFFER );
  116. }
  117. *pcb = cbNeeded;
  118. return hr;
  119. }