⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmcrc32.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 2 页
字号:
             INPUT:  ulToReflect - the value to be reflected
                     chBitCount - the number of bits to move
            OUTPUT:  

           RETURNS:  the new value
*/
ULONG VMCrc32::Reflect( ULONG ulToReflect, char chBitCount )
{
  ULONG ulValue = 0;

  // Swap bit 0 for bit 7 bit 1 for bit 6, etc.
  //
  for( int iLoop = 1; iLoop < ( chBitCount + 1 ); iLoop++ )
  {
    if ( ulToReflect & 1 )
    {
      ulValue |= 1 << ( chBitCount - iLoop );
    }
    ulToReflect >>= 1;
  }
  return( ulValue );
}
/* End of function "VMCrc32::Reflect"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMCrc32::Calculate

       DESCRIPTION:  Calculates the CRC-32 value for the given buffer

             INPUT:  pbBuffer - pointer to the data bytes
                     iBufferSize - the size of the buffer
                     rulOutput - the initial CRC-32 value
            OUTPUT:  value of rulOutput will be modified

           RETURNS:  void  - 
*/
void VMCrc32::Calculate( const LPBYTE pbBuffer, UINT iBufferSize, ULONG& rulOutput )
{
  LPBYTE pByte = pbBuffer;

  while ( iBufferSize-- )
  {
    rulOutput = ( rulOutput >> 8 ) ^ m_aulTable[ (rulOutput & 0xFF ) ^ *pByte++ ];
  }
}
/* End of function "VMCrc32::Calculate"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMCrc32::CalcCRC

       DESCRIPTION:  calculates the CRC-32 value for the given buffer
             Note :  ProgressWnd is passed through the IsWindow() API function.
                     If IsWindow() returns zero, CalcCRC() will calculate the 
                     CRC-32 value directly. if IsWindow() returns nonzero, 
                     CalcCRC() will start a seperate thread. The thread will 
                     send PBM_* progress bar messages to the ProgressWnd. When 
                     the thread is finished, the thread will send a message of
                     type WM_CRC_THREAD_DONE to the parent window of the progress
                     window. the WPARAM parameter will contain the HANDLE of 
                     the thread, and the LPARAM will contain the CRC-32 value 
                     of the buffer.

             INPUT:  pvBuffer - a pointer to the data bytes
                     iBufferSize - the size of the buffer
                     hWndProgress - the HWND of the progress bar
            OUTPUT:  

           RETURNS:  if ProgressWnd is not a window returns the CRC-32 value 
                     of the buffer
                     if ProgressWnd is a window, returns the HANDLE of the 
                     created thread
                     returns NULL if an error occurs
*/
DWORD VMCrc32::CalcCRC( LPVOID pvBuffer, UINT iBufferSize, HWND hWndProgress )
{
  // check the validity of the data
  //
  if ( !pvBuffer || !iBufferSize )
  {
    return( 0 ); 
  }

  if ( !IsWindow( hWndProgress ) )
  {
    // calculate CRC directly
    //
    DWORD dwCRC = 0xFFFFFFFF;
    Calculate( (LPBYTE)pvBuffer, iBufferSize, dwCRC );
    return( dwCRC ^ 0xFFFFFFFF );
  }

  // start the thread
  //
  P_VM_CRC_THREAD_STRUCT pxCtrl = new VM_CRC_THREAD_STRUCT;
  DWORD               dwThreadID;
  HANDLE              hThread;
  hThread = ::CreateThread( NULL, 
                            0, 
                            CRC32ThreadProc, 
                            (LPVOID)pxCtrl,
                            CREATE_SUSPENDED, 
                            &dwThreadID );
  if ( hThread )
  {
    // thread successfully created
    //
    pxCtrl->m_poCRC32        = this;
    pxCtrl->m_achFileName[0] = 0;
    pxCtrl->m_hWndProgress   = hWndProgress;
    pxCtrl->m_hThread        = hThread;
    pxCtrl->m_iSize          = iBufferSize;
    pxCtrl->m_pbData         = new BYTE[ iBufferSize ];
    memcpy( pxCtrl->m_pbData, pvBuffer, iBufferSize );
    ::ResumeThread( hThread );
  }
  else
  {
    // thread creation failed, clean up
    //
    delete pxCtrl;
  }
  return( (DWORD)hThread );
}
/* End of function "VMCrc32::CalcCRC"
/*****************************************************************************/


/*****************************************************************************/
/*

     FUNCTION NAME:  VMCrc32::CalcCRC

       DESCRIPTION:  calculates the CRC-32 value for the given buffer
             Note :  ProgressWnd is passed through the IsWindow() API function.
                     If IsWindow() returns zero, CalcCRC() will calculate the 
                     CRC-32 value directly. if IsWindow() returns nonzero, 
                     CalcCRC() will start a seperate thread. The thread will 
                     send PBM_* progress bar messages to the ProgressWnd. When 
                     the thread is finished, the thread will send a message of
                     type WM_CRC_THREAD_DONE to the parent window of the progress
                     window. the WPARAM parameter will contain the HANDLE of 
                     the thread, and the LPARAM will contain the CRC-32 value 
                     of the buffer.

             INPUT:  pchFileName - the complete path to the file
                     hWndProgress - the HWND of the progress bar
            OUTPUT:  

           RETURNS:  if ProgressWnd is not a window returns the CRC-32 value 
                     of the buffer
                     if ProgressWnd is a window, returns the HANDLE of the 
                     created thread
                     returns NULL if an error occurs
*/
DWORD VMCrc32::CalcCRC( LPCTSTR pchFileName, HWND hWndProgress )
{
  // make sure the file exists and is not a directory
  //
  DWORD dwAttribs = ::GetFileAttributes( pchFileName );
  if ( dwAttribs == 0xFFFFFFFF || dwAttribs & FILE_ATTRIBUTE_DIRECTORY )
  {
    return( 0 );
  }

  // setup the CRCStruct
  //
  P_VM_CRC_THREAD_STRUCT pxCtrl = new VM_CRC_THREAD_STRUCT;
  pxCtrl->m_poCRC32      = this;
  pxCtrl->m_pbData       = NULL;
  pxCtrl->m_iSize        = 0;
  pxCtrl->m_hWndProgress = hWndProgress;
  pxCtrl->m_hThread      = NULL;
  _tcsncpy( pxCtrl->m_achFileName, pchFileName, _MAX_PATH );

  if ( !IsWindow( hWndProgress ) )
  {
    // calculate CRC directly
    //
    return( CRC32ThreadProc( (LPVOID)pxCtrl ) );
  }

  // start the thread
  //
  DWORD  dwThreadID;
  HANDLE hThread;
  hThread = ::CreateThread( NULL, 
                            0, 
                            CRC32ThreadProc, 
                            (LPVOID)pxCtrl, 
                            CREATE_SUSPENDED, 
                            &dwThreadID );
  if ( hThread )
  {
    // thread successfully created
    //
    pxCtrl->m_hThread = hThread;
    ::ResumeThread( hThread );
  }
  else
  {
     // thread creation failed, clean up
     //
     delete pxCtrl;
  }
  return( (DWORD)hThread );
}
/* End of function "VMCrc32::CalcCRC"
/*****************************************************************************/


    
/*****************************************************************************/
/* Check-in history 
   $WorkFile:   $
    $Archive:   $

 *$Log:  $
*/
/*****************************************************************************/


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -