/** \file ClientCatchcopy.cpp \brief Define the catchcopy client \author alpha_one_x86 \version 0002 \date 2010 */ #include #include "ClientCatchcopy.h" #include //#pragma comment(lib, "Ws2_32.lib") #undef NONBLOCK_FLAG ClientCatchcopy::ClientCatchcopy() { m_hpipe=NULL; idNextOrder=0; const char prefix[]="\\\\.\\pipe\\advanced-copier-"; char uname[1024]; DWORD len=1023; char *data; // false ?? if(GetUserNameA(uname, &len)!=FALSE) { // convert into hexa data = toHex(uname); m_pipename = (char *) malloc(sizeof(prefix)+strlen(data)+2); #if defined(_MFC_VER) strcpy_s(m_pipename, _countof(prefix) ,prefix); strcat_s(m_pipename, sizeof(prefix)+strlen(data)+2,data); #else strcpy(m_pipename, prefix); strcat(m_pipename, data); #endif free(data); m_blk=NULL; m_len=0; m_tot=0; canConnect=true; } else { #ifdef CATCHCOPY_EXPLORER_PLUGIN_DEBUG void* lpBuffer; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, ::GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpBuffer, 0, NULL ); MessageBox(NULL,(LPCTSTR)lpBuffer, L"GetUserName Failed", MB_OK); LocalFree( lpBuffer ); #endif // CATCHCOPY_EXPLORER_PLUGIN_DEBUG canConnect=false; } } ClientCatchcopy::~ClientCatchcopy() { disconnectFromServer(); } // Dump UTF16 (little endian) char * ClientCatchcopy::toHex(const char *str) { char *p, *sz; size_t len; if (str==NULL) return NULL; len= strlen(str); p = sz = (char *) malloc((len+1)*4); // username goes hexa... for (size_t i=0; iBUFFER_PIPE) ? BUFFER_PIPE:m_len; #ifdef NONBLOCK_FLAG writePipe_nonBlock(m_hpipe, ptr, max); break; #else if(writePipe(m_hpipe, ptr, max)!=0) { ret=-2; break; } m_len-=max; ptr+=max; #endif } } return ret; } int ClientCatchcopy::writePipe(HANDLE hPipe, byte_t *ptr, int len) { DWORD cbWritten; if (!WriteFile(hPipe, ptr, len, &cbWritten, NULL)) return -4; return 0; } #ifdef NONBLOCK_FLAG int ClientCatchcopy::writePipe_nonBlock(HANDLE hPipe, byte_t *ptr, int len) { DWORD cbWritten; WriteFile(hPipe, ptr, len, &cbWritten, NULL); return 0; } #endif // Add int32 (big-endian) into binary block int ClientCatchcopy::addInt32(int value) { blkGrowing(sizeof(int)); // add value setInt32(m_len, value); m_len+=sizeof(int); return m_len-sizeof(int); } void ClientCatchcopy::setInt32(int offset, int value) { C_INT(m_blk+offset)=htonl(value); } // Add unicode string into binary block from ASCIIZ int ClientCatchcopy::addStr(WCHAR *data) { int ret=-1, len; WCHAR *x; if (data!=NULL && *data) { // le => be x = _wcsdup(data); len = toBigEndian(x); // set size of string ret = addInt32(len); // and add it to block blkGrowing(len); memmove(m_blk+m_len, x, len); m_len+=len; free(x); } return ret; } // resize binary block (if needed) byte_t *ClientCatchcopy::blkGrowing(int added) { if (m_len+added>m_tot) { // check if added isn't bigger than buffer itself... m_tot+= (added>BLOCK_SIZ) ? added:BLOCK_SIZ; m_blk = (byte_t *) realloc(m_blk, m_tot); } return m_blk+m_len; } int ClientCatchcopy::toBigEndian(WCHAR *p) { WCHAR tmp; int ret=0; while(*p) { tmp = htons(*p); *p++=tmp; ret+=2; } return ret; } void ClientCatchcopy::clear() { m_tot=0; m_len=0; idNextOrder=0; if (m_blk!=NULL) { free(m_blk); m_blk=NULL; } } bool ClientCatchcopy::isConnected() { if(m_hpipe==NULL) return false; bool fSuccess = PeekNamedPipe( m_hpipe, NULL, 0, 0, 0, 0 ); if(!fSuccess && GetLastError() != ERROR_MORE_DATA) { #ifdef CATCHCOPY_EXPLORER_PLUGIN_DEBUG void* lpBuffer; FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, ::GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) &lpBuffer, 0, NULL ); MessageBox(NULL,(LPCTSTR)lpBuffer, L"Error detected with the connexion", MB_OK); LocalFree( lpBuffer ); #endif // CATCHCOPY_EXPLORER_PLUGIN_DEBUG disconnectFromServer(); return false; } else return true; }