Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. #include "AdvancedCopier.h"
  2. #include <stdio.h>
  3.  
  4. #define BUFSIZE 512
  5. #define S_LEN 8
  6.  
  7. #define C_INT(p) *((int *) (p))
  8. #define BLOCK_SIZ 64000
  9. #define BUFFER_PIPE 32000
  10.  
  11. #define CATCHCOPY_PROTOCOL 2
  12. #define ID_COPY 0xCAFEBABE
  13.  
  14. #pragma comment(lib, "wsock32")
  15.  
  16.  
  17.  
  18.  
  19.  
  20. /**
  21. * Dump UTF16 (little endian)
  22. */
  23. static char *toHex(const char *str)
  24. {
  25. char *p, *sz;
  26. size_t len;
  27.  
  28. if (str==NULL)
  29. return NULL;
  30.  
  31. len= strlen(str);
  32.  
  33. p = sz = (char *) malloc((len+1)*4);
  34.  
  35. // username goes hexa...
  36. for (size_t i=0; i<len; i++)
  37. {
  38. sprintf(p, "%.2x00", str[i]);
  39. p+=4;
  40. }
  41.  
  42. *p=0;
  43.  
  44. return sz;
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51. //---
  52. //
  53. AdvancedCopierOperation *AdvancedCopierClient::getOperation(int idx)
  54. {
  55. return m_opr[idx];
  56. }
  57.  
  58.  
  59.  
  60.  
  61. //----
  62. //
  63. AdvancedCopierClient::AdvancedCopierClient(const WCHAR *name):AdvancedCopier()
  64. {
  65. const char prefix[]="\\\\.\\pipe\\advanced-copier-";
  66. char uname[1024];
  67. DWORD len=1023;
  68. char *data;
  69.  
  70. MessageBox ( NULL, L"Destructor", L"Destructor", MB_OK);
  71.  
  72. m_nbopr=0;
  73. m_opr = NULL;
  74. //m_connected=false;
  75. m_hpipe=NULL;
  76.  
  77. // false ??
  78. GetUserNameA(uname, &len);
  79.  
  80. // convert into hexa
  81. data = toHex(uname);
  82.  
  83. m_pipename = (char *) malloc(sizeof(prefix)+strlen(data)+2);
  84. strcpy(m_pipename, prefix);
  85. strcat(m_pipename, data);
  86.  
  87. free(data);
  88.  
  89. m_app = _wcsdup( (name==NULL || *name==0) ? L"App": _wcsdup(name));
  90. }
  91.  
  92.  
  93.  
  94. //---
  95. //
  96. AdvancedCopierClient::~AdvancedCopierClient()
  97. {
  98. if (m_pipename!=NULL)
  99. free(m_pipename);
  100.  
  101. if (m_hpipe!=NULL)
  102. CloseHandle(m_hpipe);
  103.  
  104. if (m_app!=NULL)
  105. free(m_app);
  106.  
  107. resetAllOperation(false);
  108. }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. /**
  115. *
  116. */
  117. void AdvancedCopierClient::addOperation(AdvancedCopierOperation *newOperation)
  118. {
  119. // may be too slow...
  120. m_opr = (AdvancedCopierOperation**) realloc(m_opr, sizeof(AdvancedCopierOperation**)*(m_nbopr+1));
  121.  
  122. //... isn't it ?
  123. m_opr[m_nbopr++]=newOperation;
  124. }
  125.  
  126.  
  127.  
  128.  
  129. /**
  130. *
  131. */
  132. static int writePipe(HANDLE hPipe, byte_t *ptr, int len)
  133. {
  134. DWORD cbWritten;
  135.  
  136. if (!WriteFile(hPipe, ptr, len, &cbWritten, NULL))
  137. {
  138. MessageBox ( NULL, L"WritePipe", L"WritePipe", MB_OK);
  139. return -4;
  140. }
  141.  
  142. return 0;
  143. }
  144.  
  145.  
  146.  
  147.  
  148. /**
  149. *
  150. */
  151. static int readPipe(HANDLE hPipe)
  152. {
  153. DWORD cbRead;
  154. char data[2048];
  155.  
  156. memset(data, 0, 2048);
  157.  
  158. if (!ReadFile(hPipe, data, 2048, &cbRead, NULL))
  159. return -4;
  160.  
  161.  
  162.  
  163. return 0;
  164. }
  165.  
  166.  
  167.  
  168. /**
  169. * Send data block to named pipe
  170. */
  171. int AdvancedCopierClient::dataToPipe()
  172. {
  173. byte_t *ptr;
  174. int ret, max;
  175.  
  176.  
  177. if (m_hpipe!=NULL)
  178. {
  179. ptr = m_blk;
  180.  
  181. // more safety, send signature first...
  182. // MaxSize (4)+CopyId (4) + Protocol (4)
  183. /*
  184. writePipe(hPipe, ptr, 12);
  185. ptr+=12;
  186. m_len-=12;
  187. */
  188. ret=0;
  189.  
  190. while (!ret && m_len)
  191. {
  192. max=(m_len>BUFFER_PIPE) ? BUFFER_PIPE:m_len;
  193.  
  194. if (writePipe(m_hpipe, ptr, max))
  195. {
  196. ret=-2;
  197. break;
  198. }
  199.  
  200. m_len-=max;
  201. ptr+=max;
  202. }
  203. }
  204.  
  205.  
  206. return ret;
  207. }
  208.  
  209.  
  210.  
  211.  
  212. //----
  213. //
  214. int AdvancedCopierClient::resetAllOperation(bool deleteOp)
  215. {
  216. int r=0;
  217.  
  218. for (int i=0; i<m_nbopr; i++)
  219. {
  220. AdvancedCopierOperation *pOp = m_opr[i];
  221.  
  222. if (pOp!=NULL)
  223. {
  224. delete pOp;
  225. r++;
  226. }
  227. }
  228.  
  229. m_nbopr=0;
  230. free(m_opr);
  231. m_opr=NULL;
  232.  
  233. clear();
  234.  
  235. return r;
  236. }
  237.  
  238.  
  239.  
  240.  
  241. /**
  242. *
  243. */
  244. bool AdvancedCopierClient::sendAllOperation(bool waiting)
  245. {
  246. byte_t *pBlk;
  247. int nb, id;
  248. AdvancedCopierOperation *pOp;
  249.  
  250.  
  251. if (m_connected)
  252. {
  253. id = (waiting) ? ID_COPY:0;
  254.  
  255. //MessageBox ( NULL, L"SendAll", L"SendAll", MB_OK);
  256.  
  257. for (int i=0; i<m_nbopr; i++)
  258. {
  259. // reset internal buffer
  260. clear();
  261.  
  262. if (m_opr[i]!=NULL)
  263. {
  264. pOp = m_opr[i];
  265. pOp->addSource((WCHAR *) pOp->getDest());
  266.  
  267. // header settings
  268. addInt32(0); // header size
  269. id = 0;
  270. addInt32(id); // id copy, for Java fan's :)
  271. addInt32(CATCHCOPY_PROTOCOL); // magic num
  272.  
  273. //addStr(m_app); // set extra data
  274. addStr(L"client");
  275. //MessageBox ( NULL, m_app, L"SendAll", MB_OK);
  276. addInt32(pOp->nbOperation()); // number of elems
  277.  
  278. wchar_t istr[32];
  279. _itow_s(pOp->nbOperation(), istr, 10);
  280. MessageBox ( NULL, istr, L"nbOperation", MB_OK);
  281.  
  282. // aggregate current block
  283. pBlk = blkGrowing(m_len);
  284.  
  285. wchar_t istr3[32];
  286. _itow_s(m_len, istr3, 10);
  287.  
  288. MessageBox ( NULL, istr3, L"SendAll", MB_OK);
  289.  
  290. memmove(pBlk, pOp->data(), pOp->getSize());
  291.  
  292. m_len+= pOp->getSize();
  293.  
  294. setInt32(0, m_len);
  295.  
  296. // run for it...
  297. nb = dataToPipe();
  298.  
  299. if (waiting)
  300. {
  301. if (nb>=0)
  302. readPipe(m_hpipe);
  303.  
  304. id++;
  305. }
  306. }
  307. }
  308. }
  309.  
  310. return true;
  311. }
  312.  
  313.  
  314.  
  315.  
  316.  
  317. /**
  318. *
  319. bool AdvancedCopierClient::sendAllOperation()
  320. {
  321. byte_t *pBlk;
  322. int nb, tot, nbcmd, off_nbcmd;
  323. AdvancedCopierOperation *pOp;
  324.  
  325. if (m_connected)
  326. {
  327. // header settings
  328. addInt32(0); // header size
  329. addInt32(ID_COPY); // id copy, for Java fan's :)
  330. addInt32(CATCHCOPY_PROTOCOL); // magic num
  331. addStr(m_app); // set extra data
  332. off_nbcmd = addInt32(0XFFFFFFFF); // number of elems
  333.  
  334.  
  335. // 1st entry, collect informations
  336. tot=m_len;
  337. nbcmd=0;
  338.  
  339. for (int i=0; i<m_nbopr; i++)
  340. {
  341. pOp = m_opr[i];
  342.  
  343. if (pOp!=NULL)
  344. {
  345. // everything is done, add destination directory to the current
  346. // operation
  347. pOp->addSource((WCHAR *) pOp->getDest());
  348. tot+= pOp->getSize();
  349. nbcmd+= pOp->nbOperation();
  350. }
  351. }
  352.  
  353. pBlk = blkGrowing(tot);
  354. setInt32(off_nbcmd, nbcmd);
  355.  
  356. // 2nd pass, move data
  357. for (int i=0; i<m_nbopr; i++)
  358. {
  359. pOp = m_opr[i];
  360.  
  361. if (pOp!=NULL)
  362. {
  363. memmove(pBlk, pOp->data(), pOp->getSize());
  364.  
  365. m_len+= pOp->getSize();
  366. pBlk+= pOp->getSize();
  367. }
  368. }
  369.  
  370. setInt32(0, m_len);
  371.  
  372. // run for it...
  373. nb = dataToPipe();
  374.  
  375. return true;
  376. }
  377.  
  378. return false;
  379. }
  380. */
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387. /**
  388. *
  389. */
  390. bool AdvancedCopierClient::connect()
  391. {
  392.  
  393. byte_t *pBlk;
  394. int nb, id;
  395.  
  396. if (m_hpipe==NULL)
  397. {
  398. // create pipe
  399. while (1)
  400. {
  401. m_hpipe = CreateFileA(m_pipename, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
  402.  
  403. if (m_hpipe!=INVALID_HANDLE_VALUE)
  404. break;
  405.  
  406. // trouble ?
  407. if (GetLastError()!= ERROR_PIPE_BUSY)
  408. return false;
  409.  
  410. if (!WaitNamedPipeA(m_pipename, 10000))
  411. {
  412. CloseHandle(m_hpipe);
  413. m_hpipe=NULL;
  414. return false;
  415. }
  416. }
  417. }
  418.  
  419. m_connected=true;
  420.  
  421. return true;
  422. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement