Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "AdvancedCopier.h"
- #include <stdio.h>
- #define BUFSIZE 512
- #define S_LEN 8
- #define C_INT(p) *((int *) (p))
- #define BLOCK_SIZ 64000
- #define BUFFER_PIPE 32000
- #define CATCHCOPY_PROTOCOL 2
- #define ID_COPY 0xCAFEBABE
- #pragma comment(lib, "wsock32")
- /**
- * Dump UTF16 (little endian)
- */
- static char *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; i<len; i++)
- {
- sprintf(p, "%.2x00", str[i]);
- p+=4;
- }
- *p=0;
- return sz;
- }
- //---
- //
- AdvancedCopierOperation *AdvancedCopierClient::getOperation(int idx)
- {
- return m_opr[idx];
- }
- //----
- //
- AdvancedCopierClient::AdvancedCopierClient(const WCHAR *name):AdvancedCopier()
- {
- const char prefix[]="\\\\.\\pipe\\advanced-copier-";
- char uname[1024];
- DWORD len=1023;
- char *data;
- MessageBox ( NULL, L"Destructor", L"Destructor", MB_OK);
- m_nbopr=0;
- m_opr = NULL;
- //m_connected=false;
- m_hpipe=NULL;
- // false ??
- GetUserNameA(uname, &len);
- // convert into hexa
- data = toHex(uname);
- m_pipename = (char *) malloc(sizeof(prefix)+strlen(data)+2);
- strcpy(m_pipename, prefix);
- strcat(m_pipename, data);
- free(data);
- m_app = _wcsdup( (name==NULL || *name==0) ? L"App": _wcsdup(name));
- }
- //---
- //
- AdvancedCopierClient::~AdvancedCopierClient()
- {
- if (m_pipename!=NULL)
- free(m_pipename);
- if (m_hpipe!=NULL)
- CloseHandle(m_hpipe);
- if (m_app!=NULL)
- free(m_app);
- resetAllOperation(false);
- }
- /**
- *
- */
- void AdvancedCopierClient::addOperation(AdvancedCopierOperation *newOperation)
- {
- // may be too slow...
- m_opr = (AdvancedCopierOperation**) realloc(m_opr, sizeof(AdvancedCopierOperation**)*(m_nbopr+1));
- //... isn't it ?
- m_opr[m_nbopr++]=newOperation;
- }
- /**
- *
- */
- static int writePipe(HANDLE hPipe, byte_t *ptr, int len)
- {
- DWORD cbWritten;
- if (!WriteFile(hPipe, ptr, len, &cbWritten, NULL))
- {
- MessageBox ( NULL, L"WritePipe", L"WritePipe", MB_OK);
- return -4;
- }
- return 0;
- }
- /**
- *
- */
- static int readPipe(HANDLE hPipe)
- {
- DWORD cbRead;
- char data[2048];
- memset(data, 0, 2048);
- if (!ReadFile(hPipe, data, 2048, &cbRead, NULL))
- return -4;
- return 0;
- }
- /**
- * Send data block to named pipe
- */
- int AdvancedCopierClient::dataToPipe()
- {
- byte_t *ptr;
- int ret, max;
- if (m_hpipe!=NULL)
- {
- ptr = m_blk;
- // more safety, send signature first...
- // MaxSize (4)+CopyId (4) + Protocol (4)
- /*
- writePipe(hPipe, ptr, 12);
- ptr+=12;
- m_len-=12;
- */
- ret=0;
- while (!ret && m_len)
- {
- max=(m_len>BUFFER_PIPE) ? BUFFER_PIPE:m_len;
- if (writePipe(m_hpipe, ptr, max))
- {
- ret=-2;
- break;
- }
- m_len-=max;
- ptr+=max;
- }
- }
- return ret;
- }
- //----
- //
- int AdvancedCopierClient::resetAllOperation(bool deleteOp)
- {
- int r=0;
- for (int i=0; i<m_nbopr; i++)
- {
- AdvancedCopierOperation *pOp = m_opr[i];
- if (pOp!=NULL)
- {
- delete pOp;
- r++;
- }
- }
- m_nbopr=0;
- free(m_opr);
- m_opr=NULL;
- clear();
- return r;
- }
- /**
- *
- */
- bool AdvancedCopierClient::sendAllOperation(bool waiting)
- {
- byte_t *pBlk;
- int nb, id;
- AdvancedCopierOperation *pOp;
- if (m_connected)
- {
- id = (waiting) ? ID_COPY:0;
- //MessageBox ( NULL, L"SendAll", L"SendAll", MB_OK);
- for (int i=0; i<m_nbopr; i++)
- {
- // reset internal buffer
- clear();
- if (m_opr[i]!=NULL)
- {
- pOp = m_opr[i];
- pOp->addSource((WCHAR *) pOp->getDest());
- // header settings
- addInt32(0); // header size
- id = 0;
- addInt32(id); // id copy, for Java fan's :)
- addInt32(CATCHCOPY_PROTOCOL); // magic num
- //addStr(m_app); // set extra data
- addStr(L"client");
- //MessageBox ( NULL, m_app, L"SendAll", MB_OK);
- addInt32(pOp->nbOperation()); // number of elems
- wchar_t istr[32];
- _itow_s(pOp->nbOperation(), istr, 10);
- MessageBox ( NULL, istr, L"nbOperation", MB_OK);
- // aggregate current block
- pBlk = blkGrowing(m_len);
- wchar_t istr3[32];
- _itow_s(m_len, istr3, 10);
- MessageBox ( NULL, istr3, L"SendAll", MB_OK);
- memmove(pBlk, pOp->data(), pOp->getSize());
- m_len+= pOp->getSize();
- setInt32(0, m_len);
- // run for it...
- nb = dataToPipe();
- if (waiting)
- {
- if (nb>=0)
- readPipe(m_hpipe);
- id++;
- }
- }
- }
- }
- return true;
- }
- /**
- *
- bool AdvancedCopierClient::sendAllOperation()
- {
- byte_t *pBlk;
- int nb, tot, nbcmd, off_nbcmd;
- AdvancedCopierOperation *pOp;
- if (m_connected)
- {
- // header settings
- addInt32(0); // header size
- addInt32(ID_COPY); // id copy, for Java fan's :)
- addInt32(CATCHCOPY_PROTOCOL); // magic num
- addStr(m_app); // set extra data
- off_nbcmd = addInt32(0XFFFFFFFF); // number of elems
- // 1st entry, collect informations
- tot=m_len;
- nbcmd=0;
- for (int i=0; i<m_nbopr; i++)
- {
- pOp = m_opr[i];
- if (pOp!=NULL)
- {
- // everything is done, add destination directory to the current
- // operation
- pOp->addSource((WCHAR *) pOp->getDest());
- tot+= pOp->getSize();
- nbcmd+= pOp->nbOperation();
- }
- }
- pBlk = blkGrowing(tot);
- setInt32(off_nbcmd, nbcmd);
- // 2nd pass, move data
- for (int i=0; i<m_nbopr; i++)
- {
- pOp = m_opr[i];
- if (pOp!=NULL)
- {
- memmove(pBlk, pOp->data(), pOp->getSize());
- m_len+= pOp->getSize();
- pBlk+= pOp->getSize();
- }
- }
- setInt32(0, m_len);
- // run for it...
- nb = dataToPipe();
- return true;
- }
- return false;
- }
- */
- /**
- *
- */
- bool AdvancedCopierClient::connect()
- {
- byte_t *pBlk;
- int nb, id;
- if (m_hpipe==NULL)
- {
- // create pipe
- while (1)
- {
- m_hpipe = CreateFileA(m_pipename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
- if (m_hpipe!=INVALID_HANDLE_VALUE)
- break;
- // trouble ?
- if (GetLastError()!= ERROR_PIPE_BUSY)
- return false;
- if (!WaitNamedPipeA(m_pipename, 10000))
- {
- CloseHandle(m_hpipe);
- m_hpipe=NULL;
- return false;
- }
- }
- }
- m_connected=true;
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement