stdafx.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. //---------------------------------------------------------------------------
  2. #ifndef StdAfxH
  3. #define StdAfxH
  4. //---------------------------------------------------------------------------
  5. #define _int64 __int64
  6. //---------------------------------------------------------------------------
  7. #define MPEXT
  8. #define MPEXT_NO_ZLIB
  9. #define MPEXT_NO_GSS
  10. #define MPEXT_NO_SFTP
  11. #define MPEXT_NO_IDENT
  12. #define MPEXT_NO_CACHE
  13. #define MPEXT_NO_SPEED_LIM_RULES
  14. #define MPEXT_NO_SSLDLL
  15. #define _AFX_NOFORCE_LIBS
  16. #define _MPT(T) _T(T)
  17. #define _MPAT(T) T
  18. //---------------------------------------------------------------------------
  19. #define GetOption(OPTION) GetInstanceOption(this->m_pApiLogParent, OPTION)
  20. #define GetOptionVal(OPTION) GetInstanceOptionVal(this->m_pApiLogParent, OPTION)
  21. //---------------------------------------------------------------------------
  22. #include <afx.h>
  23. #include "wtypes.h"
  24. #include <afxmt.h>
  25. //STL includes
  26. #include <list>
  27. #include <map>
  28. #include <vector>
  29. #include <deque>
  30. #include <set>
  31. #include <algorithm>
  32. //---------------------------------------------------------------------------
  33. class CFileFix;
  34. #define CFile CFileFix
  35. //---------------------------------------------------------------------------
  36. #include "MFC64bitFix.h"
  37. #include <ApiLog.h>
  38. #include <FileZillaApi.h>
  39. #include <FileZillaOpt.h>
  40. #include <Options.h>
  41. #include <Crypt.h>
  42. #include <TextsFileZilla.h>
  43. #include <structures.h>
  44. //---------------------------------------------------------------------------
  45. #include <oleauto.h>
  46. #include <afxdisp.h>
  47. #include <afxconv.h>
  48. //---------------------------------------------------------------------------
  49. #define _strlwr strlwr
  50. #define USEDPARAM(p) ((p) == (p))
  51. //---------------------------------------------------------------------------
  52. const int FILEEXISTS_ASK = -1;
  53. const int FILEEXISTS_OVERWRITE = 0;
  54. const int FILEEXISTS_OVERWRITEIFNEWER = 1;
  55. const int FILEEXISTS_RESUME = 2;
  56. const int FILEEXISTS_RENAME = 3;
  57. const int FILEEXISTS_SKIP = 4;
  58. const int FILEEXISTS_RESUME_ASKONFAIL = 5; // Used by queue for automatic resuming. If APPE failes, ask what to do instead.
  59. //---------------------------------------------------------------------------
  60. class t_ffam_statusmessage
  61. {
  62. public:
  63. CString status;
  64. int type;
  65. BOOL post;
  66. };
  67. //---------------------------------------------------------------------------
  68. typedef struct
  69. {
  70. __int64 bytes;
  71. #ifdef MPEXT
  72. __int64 transfersize;
  73. #endif
  74. int percent;
  75. int timeelapsed;
  76. int timeleft;
  77. int transferrate;
  78. BOOL bFileTransfer;
  79. } t_ffam_transferstatus;
  80. //---------------------------------------------------------------------------
  81. #undef CFile
  82. //---------------------------------------------------------------------------
  83. class CFileFix : public CFile
  84. {
  85. public:
  86. // MFC CFile::Read does not include file name into error message
  87. UINT Read(void * lpBuf, UINT nCount)
  88. {
  89. ASSERT_VALID(this);
  90. ASSERT(m_hFile != (UINT)hFileNull);
  91. if (nCount == 0)
  92. {
  93. return 0; // avoid Win32 "null-read"
  94. }
  95. ASSERT(lpBuf != NULL);
  96. ASSERT(AfxIsValidAddress(lpBuf, nCount));
  97. DWORD dwRead;
  98. if (!::ReadFile((HANDLE)m_hFile, lpBuf, nCount, &dwRead, NULL))
  99. {
  100. // The only change from MFC CFile::Read is m_strFileName
  101. CFileException::ThrowOsError((LONG)::GetLastError(), m_strFileName);
  102. }
  103. return (UINT)dwRead;
  104. }
  105. // MFC allocates CObject (ancestor of CFile) with new, but deallocates with free,
  106. // what codeguard dislikes, this is fix, not sure if it is necessary for
  107. // release version, but probably causes no harm
  108. void PASCAL operator delete(void* p)
  109. {
  110. delete p;
  111. }
  112. };
  113. //---------------------------------------------------------------------------
  114. #define CFile CFileFix
  115. //---------------------------------------------------------------------------
  116. struct CStringDataA
  117. {
  118. long nRefs; // reference count
  119. int nDataLength; // length of data (including terminator)
  120. int nAllocLength; // length of allocation
  121. //char data[nAllocLength];
  122. CHAR* data() // CHAR* to managed data
  123. {
  124. return (CHAR*)(this+1);
  125. }
  126. };
  127. //---------------------------------------------------------------------------
  128. extern LPCSTR _afxPchNilA;
  129. extern CStringDataA* _afxDataNilA;
  130. #define afxEmptyStringA ((CStringA&)*(CStringA*)&_afxPchNilA)
  131. //---------------------------------------------------------------------------
  132. class CStringA
  133. {
  134. public:
  135. CStringA()
  136. {
  137. m_pchData = afxEmptyStringA.m_pchData;
  138. }
  139. CStringA(const CStringA& stringSrc)
  140. {
  141. ASSERT(stringSrc.GetData()->nRefs != 0);
  142. if (stringSrc.GetData()->nRefs >= 0)
  143. {
  144. ASSERT(stringSrc.GetData() != _afxDataNilA);
  145. m_pchData = stringSrc.m_pchData;
  146. InterlockedIncrement(&GetData()->nRefs);
  147. }
  148. else
  149. {
  150. Init();
  151. *this = stringSrc.m_pchData;
  152. }
  153. }
  154. CStringA(LPCSTR lpsz)
  155. {
  156. Init();
  157. if (lpsz != NULL && HIWORD(lpsz) == NULL)
  158. {
  159. ASSERT(false);
  160. }
  161. else
  162. {
  163. int nLen = SafeStrlen(lpsz);
  164. if (nLen != 0)
  165. {
  166. AllocBuffer(nLen);
  167. memcpy(m_pchData, lpsz, nLen*sizeof(char));
  168. }
  169. }
  170. }
  171. ~CStringA()
  172. {
  173. if (GetData() != _afxDataNilA)
  174. {
  175. if (InterlockedDecrement(&GetData()->nRefs) <= 0)
  176. {
  177. FreeData(GetData());
  178. }
  179. }
  180. }
  181. int GetLength() const
  182. {
  183. return GetData()->nDataLength;
  184. }
  185. char operator[](int nIndex) const
  186. {
  187. // same as GetAt
  188. ASSERT(nIndex >= 0);
  189. ASSERT(nIndex < GetData()->nDataLength);
  190. return m_pchData[nIndex];
  191. }
  192. // ref-counted copy from another CString
  193. CStringA& operator=(const CStringA& stringSrc)
  194. {
  195. if (m_pchData != stringSrc.m_pchData)
  196. {
  197. if ((GetData()->nRefs < 0 && GetData() != _afxDataNilA) ||
  198. stringSrc.GetData()->nRefs < 0)
  199. {
  200. // actual copy necessary since one of the strings is locked
  201. AssignCopy(stringSrc.GetData()->nDataLength, stringSrc.m_pchData);
  202. }
  203. else
  204. {
  205. // can just copy references around
  206. Release();
  207. ASSERT(stringSrc.GetData() != _afxDataNilA);
  208. m_pchData = stringSrc.m_pchData;
  209. InterlockedIncrement(&GetData()->nRefs);
  210. }
  211. }
  212. return *this;
  213. }
  214. const CStringA& operator=(LPCSTR lpsz)
  215. {
  216. ASSERT(lpsz == NULL || AfxIsValidString(lpsz));
  217. AssignCopy(SafeStrlen(lpsz), lpsz);
  218. return *this;
  219. }
  220. const CStringA& operator+=(char ch)
  221. {
  222. ConcatInPlace(1, &ch);
  223. return *this;
  224. }
  225. friend CStringA AFXAPI operator+(const CStringA& string, char ch);
  226. operator LPCSTR() const
  227. {
  228. return m_pchData;
  229. }
  230. int Compare(LPCSTR lpsz) const
  231. {
  232. ASSERT(AfxIsValidString(lpsz));
  233. return strcmp(m_pchData, lpsz);
  234. }
  235. CStringA Mid(int nFirst, int nCount) const
  236. {
  237. // out-of-bounds requests return sensible things
  238. if (nFirst < 0)
  239. {
  240. nFirst = 0;
  241. }
  242. if (nCount < 0)
  243. {
  244. nCount = 0;
  245. }
  246. if (nFirst + nCount > GetData()->nDataLength)
  247. {
  248. nCount = GetData()->nDataLength - nFirst;
  249. }
  250. if (nFirst > GetData()->nDataLength)
  251. {
  252. nCount = 0;
  253. }
  254. ASSERT(nFirst >= 0);
  255. ASSERT(nFirst + nCount <= GetData()->nDataLength);
  256. // optimize case of returning entire string
  257. if (nFirst == 0 && nFirst + nCount == GetData()->nDataLength)
  258. {
  259. return *this;
  260. }
  261. CStringA dest;
  262. AllocCopy(dest, nCount, nFirst, 0);
  263. return dest;
  264. }
  265. CStringA Left(int nCount) const
  266. {
  267. if (nCount < 0)
  268. {
  269. nCount = 0;
  270. }
  271. if (nCount >= GetData()->nDataLength)
  272. {
  273. return *this;
  274. }
  275. CStringA dest;
  276. AllocCopy(dest, nCount, 0, 0);
  277. return dest;
  278. }
  279. int Find(char ch) const
  280. {
  281. return Find(ch, 0);
  282. }
  283. int Find(char ch, int nStart) const
  284. {
  285. int nLength = GetData()->nDataLength;
  286. if (nStart >= nLength)
  287. {
  288. return -1;
  289. }
  290. // find first single character
  291. LPSTR lpsz = strchr(m_pchData + nStart, (unsigned char)ch);
  292. // return -1 if not found and index otherwise
  293. return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
  294. }
  295. // find a sub-string (like strstr)
  296. int Find(LPCSTR lpszSub) const
  297. {
  298. return Find(lpszSub, 0);
  299. }
  300. int Find(LPCSTR lpszSub, int nStart) const
  301. {
  302. ASSERT(AfxIsValidString(lpszSub));
  303. int nLength = GetData()->nDataLength;
  304. if (nStart > nLength)
  305. {
  306. return -1;
  307. }
  308. // find first matching substring
  309. LPSTR lpsz = strstr(m_pchData + nStart, lpszSub);
  310. // return -1 for not found, distance from beginning otherwise
  311. return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
  312. }
  313. void MakeUpper()
  314. {
  315. CopyBeforeWrite();
  316. strupr(m_pchData);
  317. }
  318. protected:
  319. LPSTR m_pchData; // pointer to ref counted string data
  320. CStringDataA* GetData() const
  321. {
  322. ASSERT(m_pchData != NULL); return ((CStringDataA*)m_pchData)-1;
  323. }
  324. void Init()
  325. {
  326. m_pchData = afxEmptyStringA.m_pchData;
  327. }
  328. void AllocCopy(CStringA& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const
  329. {
  330. // will clone the data attached to this string
  331. // allocating 'nExtraLen' characters
  332. // Places results in uninitialized string 'dest'
  333. // Will copy the part or all of original data to start of new string
  334. int nNewLen = nCopyLen + nExtraLen;
  335. if (nNewLen == 0)
  336. {
  337. dest.Init();
  338. }
  339. else
  340. {
  341. dest.AllocBuffer(nNewLen);
  342. memcpy(dest.m_pchData, m_pchData+nCopyIndex, nCopyLen*sizeof(char));
  343. }
  344. }
  345. void AllocBuffer(int nLen)
  346. // always allocate one extra character for '\0' termination
  347. // assumes [optimistically] that data length will equal allocation length
  348. {
  349. ASSERT(nLen >= 0);
  350. ASSERT(nLen <= INT_MAX-1); // max size (enough room for 1 extra)
  351. if (nLen == 0)
  352. {
  353. Init();
  354. }
  355. else
  356. {
  357. CStringDataA* pData;
  358. {
  359. pData = (CStringDataA*)
  360. new BYTE[sizeof(CStringDataA) + (nLen+1)*sizeof(char)];
  361. pData->nAllocLength = nLen;
  362. }
  363. pData->nRefs = 1;
  364. pData->data()[nLen] = '\0';
  365. pData->nDataLength = nLen;
  366. m_pchData = pData->data();
  367. }
  368. }
  369. void AssignCopy(int nSrcLen, LPCSTR lpszSrcData)
  370. {
  371. AllocBeforeWrite(nSrcLen);
  372. memcpy(m_pchData, lpszSrcData, nSrcLen*sizeof(char));
  373. GetData()->nDataLength = nSrcLen;
  374. m_pchData[nSrcLen] = '\0';
  375. }
  376. void FASTCALL FreeData(CStringDataA* pData)
  377. {
  378. delete[] (BYTE*)pData;
  379. }
  380. void PASCAL Release(CStringDataA* pData)
  381. {
  382. if (pData != _afxDataNilA)
  383. {
  384. ASSERT(pData->nRefs != 0);
  385. if (InterlockedDecrement(&pData->nRefs) <= 0)
  386. {
  387. FreeData(pData);
  388. }
  389. }
  390. }
  391. void Release()
  392. {
  393. if (GetData() != _afxDataNilA)
  394. {
  395. ASSERT(GetData()->nRefs != 0);
  396. if (InterlockedDecrement(&GetData()->nRefs) <= 0)
  397. {
  398. FreeData(GetData());
  399. }
  400. Init();
  401. }
  402. }
  403. void ConcatCopy(int nSrc1Len, LPCSTR lpszSrc1Data, int nSrc2Len, LPCSTR lpszSrc2Data)
  404. {
  405. // -- master concatenation routine
  406. // Concatenate two sources
  407. // -- assume that 'this' is a new CString object
  408. int nNewLen = nSrc1Len + nSrc2Len;
  409. if (nNewLen != 0)
  410. {
  411. AllocBuffer(nNewLen);
  412. memcpy(m_pchData, lpszSrc1Data, nSrc1Len*sizeof(char));
  413. memcpy(m_pchData+nSrc1Len, lpszSrc2Data, nSrc2Len*sizeof(char));
  414. }
  415. }
  416. void ConcatInPlace(int nSrcLen, LPCSTR lpszSrcData)
  417. {
  418. // -- the main routine for += operators
  419. // concatenating an empty string is a no-op!
  420. if (nSrcLen == 0)
  421. {
  422. return;
  423. }
  424. // if the buffer is too small, or we have a width mis-match, just
  425. // allocate a new buffer (slow but sure)
  426. if (GetData()->nRefs > 1 || GetData()->nDataLength + nSrcLen > GetData()->nAllocLength)
  427. {
  428. // we have to grow the buffer, use the ConcatCopy routine
  429. CStringDataA* pOldData = GetData();
  430. ConcatCopy(GetData()->nDataLength, m_pchData, nSrcLen, lpszSrcData);
  431. ASSERT(pOldData != NULL);
  432. CStringA::Release(pOldData);
  433. }
  434. else
  435. {
  436. // fast concatenation when buffer big enough
  437. memcpy(m_pchData+GetData()->nDataLength, lpszSrcData, nSrcLen*sizeof(char));
  438. GetData()->nDataLength += nSrcLen;
  439. ASSERT(GetData()->nDataLength <= GetData()->nAllocLength);
  440. m_pchData[GetData()->nDataLength] = '\0';
  441. }
  442. }
  443. void CopyBeforeWrite()
  444. {
  445. if (GetData()->nRefs > 1)
  446. {
  447. CStringDataA* pData = GetData();
  448. Release();
  449. AllocBuffer(pData->nDataLength);
  450. memcpy(m_pchData, pData->data(), (pData->nDataLength+1)*sizeof(char));
  451. }
  452. ASSERT(GetData()->nRefs <= 1);
  453. }
  454. void AllocBeforeWrite(int nLen)
  455. {
  456. if (GetData()->nRefs > 1 || nLen > GetData()->nAllocLength)
  457. {
  458. Release();
  459. AllocBuffer(nLen);
  460. }
  461. ASSERT(GetData()->nRefs <= 1);
  462. }
  463. static int PASCAL SafeStrlen(LPCSTR lpsz)
  464. {
  465. return (lpsz == NULL) ? 0 : strlen(lpsz);
  466. }
  467. };
  468. //---------------------------------------------------------------------------
  469. inline bool AFXAPI operator==(const CStringA& s1, LPCSTR s2)
  470. {
  471. return s1.Compare(s2) == 0;
  472. }
  473. //---------------------------------------------------------------------------
  474. inline bool AFXAPI operator!=(const CStringA& s1, LPCSTR s2)
  475. {
  476. return s1.Compare(s2) != 0;
  477. }
  478. //---------------------------------------------------------------------------
  479. inline CStringA AFXAPI operator+(const CStringA& string1, char ch)
  480. {
  481. CStringA s;
  482. s.ConcatCopy(string1.GetData()->nDataLength, string1.m_pchData, 1, &ch);
  483. return s;
  484. }
  485. //---------------------------------------------------------------------------
  486. #endif