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.

202 lines
4.2 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corp., 1994 **/
  4. /**********************************************************************/
  5. /*
  6. cbin.cxx
  7. This module contains a light weight binary class
  8. FILE HISTORY:
  9. MichTh 17-May-1996 Created, based on string.cxx
  10. */
  11. #include "precomp.hxx"
  12. //
  13. // Private Definations
  14. //
  15. /*******************************************************************
  16. NAME: CBIN::CBIN
  17. SYNOPSIS: Construct a string object
  18. ENTRY: Optional object initializer
  19. NOTES: If the object is not valid (i.e. !IsValid()) then GetLastError
  20. should be called.
  21. The object is guaranteed to construct successfully if nothing
  22. or NULL is passed as the initializer.
  23. HISTORY:
  24. Johnl 17-Aug-1994 Created
  25. ********************************************************************/
  26. CBIN::CBIN( DWORD cbLen, const PVOID pbInit )
  27. {
  28. AuxInit(cbLen, pbInit);
  29. }
  30. CBIN::CBIN( const CBIN & cbin )
  31. {
  32. AuxInit(cbin.QueryCB(), cbin.QueryPtr());
  33. }
  34. VOID CBIN::AuxInit( DWORD cbLen, PVOID pbInit)
  35. {
  36. BOOL fRet;
  37. _fValid = TRUE;
  38. if ( pbInit )
  39. {
  40. fRet = Resize( cbLen );
  41. if ( !fRet )
  42. {
  43. _fValid = FALSE;
  44. return;
  45. }
  46. SetCB(cbLen);
  47. ::memcpy( QueryPtr(), pbInit, cbLen );
  48. }
  49. else {
  50. SetCB(0);
  51. }
  52. }
  53. /*******************************************************************
  54. NAME: CBIN::Append
  55. SYNOPSIS: Appends the buffer onto this one.
  56. ENTRY: Object to append
  57. NOTES:
  58. HISTORY:
  59. Johnl 17-Aug-1994 Created
  60. ********************************************************************/
  61. BOOL CBIN::Append( DWORD cbLen, const PVOID pbBuf )
  62. {
  63. if ( pbBuf )
  64. {
  65. return AuxAppend(pbBuf, cbLen);
  66. }
  67. return TRUE;
  68. }
  69. BOOL CBIN::Append( const CBIN & cbin )
  70. {
  71. return Append(cbin.QueryCB(), cbin.QueryPtr());
  72. }
  73. BOOL CBIN::AuxAppend( PVOID pbBuf, UINT cbLen, BOOL )
  74. {
  75. DBG_ASSERT( pbBuf != NULL );
  76. UINT cbThis = QueryCB();
  77. //
  78. // Only resize when we have to. When we do resize, we tack on
  79. // some extra space to avoid extra reallocations.
  80. //
  81. // Note: QuerySize returns the requested size of the string buffer,
  82. // *not* the strlen of the buffer
  83. //
  84. if ( QuerySize() < cbThis + cbLen)
  85. {
  86. if ( !Resize( cbThis + cbLen) )
  87. return FALSE;
  88. }
  89. SetCB(cbThis + cbLen);
  90. memcpy( (BYTE *) QueryPtr() + cbThis,
  91. pbBuf,
  92. cbLen);
  93. return TRUE;
  94. }
  95. /*******************************************************************
  96. NAME: CBIN::Copy
  97. SYNOPSIS: Copies the string into this one.
  98. ENTRY: Object to Copy
  99. NOTES: A copy is a special case of Append so we just zero terminate
  100. *this and append the string.
  101. HISTORY:
  102. Johnl 17-Aug-1994 Created
  103. ********************************************************************/
  104. BOOL CBIN::Copy( DWORD cbLen, const PVOID pbBuf )
  105. {
  106. SetCB(0);
  107. if ( pbBuf )
  108. {
  109. return AuxAppend( pbBuf, cbLen, FALSE );
  110. }
  111. return TRUE;
  112. }
  113. BOOL CBIN::Copy( const CBIN & cbin )
  114. {
  115. if ( cbin.IsEmpty()) {
  116. // To avoid pathological allocation of small chunk of memory
  117. SetCB(0);
  118. return ( TRUE);
  119. }
  120. return Copy( cbin.QueryCB(), cbin.QueryPtr() );
  121. }
  122. /*******************************************************************
  123. NAME: CBIN::Resize
  124. SYNOPSIS: Resizes or allocates string memory, NULL terminating
  125. if necessary
  126. ENTRY: cbNewRequestedSize - New string size
  127. NOTES:
  128. HISTORY:
  129. Johnl 12-Sep-1994 Created
  130. ********************************************************************/
  131. BOOL CBIN::Resize( UINT cbNewRequestedSize )
  132. {
  133. if ( !BUFFER::Resize( cbNewRequestedSize ))
  134. return FALSE;
  135. return TRUE;
  136. }
  137.