FtpListResult.cpp 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087
  1. // FileZilla - a Windows ftp client
  2. // Copyright (C) 2002-2004 - Tim Kosse <[email protected]>
  3. // This program is free software; you can redistribute it and/or
  4. // modify it under the terms of the GNU General Public License
  5. // as published by the Free Software Foundation; either version 2
  6. // of the License, or (at your option) any later version.
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU General Public License for more details.
  11. // You should have received a copy of the GNU General Public License
  12. // along with this program; if not, write to the Free Software
  13. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. // FtpListResult.cpp: Implementierung der Klasse CFtpListResult.
  15. //
  16. //////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "FtpListResult.h"
  19. #include "FileZillaApi.h"
  20. #include "Options.h"
  21. #include "misc/utf8.h"
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char THIS_FILE[]=__FILE__;
  25. #define new DEBUG_NEW
  26. #endif
  27. //////////////////////////////////////////////////////////////////////
  28. // Konstruktion/Destruktion
  29. //////////////////////////////////////////////////////////////////////
  30. //#define LISTDEBUG
  31. #ifdef LISTDEBUG
  32. //It's the normal UNIX format (or even another nonstandard format)
  33. //Some samples are from http://cr.yp.to/ftpparse/ftpparse.c
  34. /* UNIX-style listing, without inum and without blocks */
  35. static char data[][110]={
  36. "-rw-r--r-- 1 root other 531 Jan 29 03:26 01-unix-std file",
  37. "dr-xr-xr-x 2 root other 512 Apr 8 1994 02-unix-std dir",
  38. "dr-xr-xr-x 2 root 512 Apr 8 1994 03-unix-nogroup dir",
  39. "lrwxrwxrwx 1 root other 7 Jan 25 00:17 04-unix-std link -> usr/bin",
  40. /* Some listings with uncommon date/time format: */
  41. "-rw-r--r-- 1 root other 531 09-26 2000 05-unix-date file",
  42. "-rw-r--r-- 1 root other 531 09-26 13:45 06-unix-date file",
  43. "-rw-r--r-- 1 root other 531 2005-06-07 21:22 07-unix-date file",
  44. /* Unix style with size information in kilobytes */
  45. "-rw-r--r-- 1 root other 34.5k Oct 5 21:22 08-unix-namedsize file",
  46. /* Also NetWare: */
  47. "d [R----F--] supervisor 512 Jan 16 18:53 09-netware dir",
  48. "- [R----F--] rhesus 214059 Oct 20 15:27 10-netware file",
  49. /* Also NetPresenz for the Mac: */
  50. "-------r-- 326 1391972 1392298 Nov 22 1995 11-netpresenz file",
  51. "drwxrwxr-x folder 2 May 10 1996 12-netpresenz dir",
  52. /* A format with domain field some windows servers send */
  53. "-rw-r--r-- 1 group domain user 531 Jan 29 03:26 13-unix-domain file",
  54. /* EPLF directory listings */
  55. "+i8388621.48594,m825718503,r,s280,\t14-eplf file",
  56. "+i8388621.50690,m824255907,/,\t15-eplf dir",
  57. /* MSDOS type listing used by IIS */
  58. "04-27-00 12:09PM <DIR> 16-dos-dateambigious dir",
  59. "04-14-00 03:47PM 589 17-dos-dateambigious file",
  60. /* Another type of MSDOS style listings */
  61. "2002-09-02 18:48 <DIR> 18-dos-longyear dir",
  62. "2002-09-02 19:06 9,730 19-dos-longyear file",
  63. /* Numerical Unix style format */
  64. "0100644 500 101 12345 123456789 20-unix-numerical file",
  65. /* This one is used by SSH-2.0-VShell_2_1_2_143, this is the old VShell format */
  66. "206876 Apr 04, 2000 21:06 21-vshell-old file",
  67. "0 Dec 12, 2002 02:13 22-vshell-old dir/",
  68. /* This type of directory listings is sent by some newer versions of VShell
  69. * both year and time in one line is uncommon.
  70. */
  71. "-rwxr-xr-x 1 user group 9 Oct 08, 2002 09:47 23-vshell-new file",
  72. /* Next ones come from an OS/2 server. The server obviously isn't Y2K aware */
  73. "36611 A 04-23-103 10:57 24-os2 file",
  74. " 1123 A 07-14-99 12:37 25-os2 file",
  75. " 0 DIR 02-11-103 16:15 26-os2 dir",
  76. " 1123 DIR A 10-05-100 23:38 27-os2 dir",
  77. /* Some servers send localized date formats, here the German one: */
  78. "dr-xr-xr-x 2 root other 2235 26. Juli, 20:10 28-datetest-ger dir",
  79. "-r-xr-xr-x 2 root other 2235 2. Okt. 2003 29-datetest-ger file",
  80. "-r-xr-xr-x 2 root other 2235 1999/10/12 17:12 30-datetest file",
  81. "-r-xr-xr-x 2 root other 2235 24-04-2003 17:12 31-datetest file",
  82. /* Here a Japanese one: */
  83. "-rw-r--r-- 1 root sys 8473 4\x8c\x8e 18\x93\xfa 2003\x94\x4e 32-datatest-japanese file",
  84. /* VMS style listings */
  85. "33-vms-dir.DIR;1 1 19-NOV-2001 21:41 [root,root] (RWE,RWE,RE,RE)",
  86. "34-vms-file;1 155 2-JUL-2003 10:30:13.64",
  87. /* VMS style listings without time */
  88. "35-vms-notime-file;1 2/8 15-JAN-2000 [IV2_XXX] (RWED,RWED,RE,)",
  89. "36-vms-notime-file;1 6/8 15-JUI-2002 PRONAS (RWED,RWED,RE,)",
  90. /* VMS multiline */
  91. "37-vms-multiline-file;1\r\n170774/170775 24-APR-2003 08:16:15 [FTP_CLIENT,SCOT] (RWED,RWED,RE,)",
  92. "38-vms-multiline-file;1\r\n10 2-JUL-2003 10:30:08.59 [FTP_CLIENT,SCOT] (RWED,RWED,RE,)",
  93. /* IBM AS/400 style listing */
  94. "QSYS 77824 02/23/00 15:09:55 *DIR 39-ibm-as400 dir/",
  95. "QSYS 77824 23/02/00 15:09:55 *FILE 40-ibm-as400-date file",
  96. /* aligned directory listing with too long size */
  97. "-r-xr-xr-x longowner longgroup123456 Feb 12 17:20 41-unix-concatsize file",
  98. /* short directory listing with month name */
  99. "-r-xr-xr-x 2 owner group 4512 01-jun-99 42_unix_shortdatemonth file",
  100. /* the following format is sent by the Connect:Enterprise server by Sterling Commerce */
  101. "-C--E-----FTP B BCC3I1 7670 1294495 Jan 13 07:42 43-conent file",
  102. "-C--E-----FTS B BCC3I1 7670 1294495 Jan 13 07:42 44-conent-file",
  103. "-AR--M----TCP B ceunix 17570 2313708 Mar 29 08:56 45-conent-file",
  104. /* Nortel wfFtp router */
  105. "46-nortel-wfftp-file 1014196 06/03/04 Thur. 10:20:03",
  106. /* VxWorks based server used in Nortel routers */
  107. "2048 Feb-28-1998 05:23:30 47-nortel-vxworks dir <DIR>",
  108. /* IBM MVS listings */
  109. // Volume Unit Referred Ext Used Recfm Lrecl BlkSz Dsorg Dsname
  110. " WYOSPT 3420 2003/05/21 1 200 FB 80 8053 PS 48-MVS.FILE",
  111. " WPTA01 3290 2004/03/04 1 3 FB 80 3125 PO 49-MVS.DATASET",
  112. " TSO004 3390 VSAM 50-mvs-file",
  113. " TSO005 3390 2005/06/06 213000 U 0 27998 PO 51-mvs-dir",
  114. /* Dataset members */
  115. // Name VV.MM Created Changed Size Init Mod Id
  116. // ADATAB /* filenames without data, only check for those on MVS servers */
  117. " 52-MVSPDSMEMBER 01.01 2004/06/22 2004/06/22 16:32 128 128 0 BOBY12",
  118. "53-MVSPDSMEMBER2 00B308 000411 00 FO RU 31 ANY",
  119. "54-MVSPDSMEMBER3 00B308 000411 00 FO RU ANY 24",
  120. // Some asian listing format. Those >127 chars are just examples
  121. "-rwxrwxrwx 1 root staff 0 2003 3\xed\xef 20 55-asian date file",
  122. "-r--r--r-- 1 root root 2096 8\xed 17 08:52 56-asian date file",
  123. // VMS style listing with a different field order
  124. "57-vms-alternate-field-order-file;1 [SUMMARY] 1/3 2-AUG-2006 13:05 (RWE,RWE,RE,)",
  125. ""};
  126. #endif
  127. CFtpListResult::CFtpListResult(t_server server, bool *bUTF8)
  128. {
  129. listhead=curpos=0;
  130. m_server = server;
  131. m_bUTF8 = bUTF8;
  132. pos=0;
  133. m_prevline=0;
  134. m_curline=0;
  135. m_curlistaddpos=0;
  136. //Fill the month names map
  137. //English month names
  138. m_MonthNamesMap[_T("jan")] = 1;
  139. m_MonthNamesMap[_T("feb")] = 2;
  140. m_MonthNamesMap[_T("mar")] = 3;
  141. m_MonthNamesMap[_T("apr")] = 4;
  142. m_MonthNamesMap[_T("may")] = 5;
  143. m_MonthNamesMap[_T("jun")] = 6;
  144. m_MonthNamesMap[_T("june")] = 6;
  145. m_MonthNamesMap[_T("jul")] = 7;
  146. m_MonthNamesMap[_T("july")] = 7;
  147. m_MonthNamesMap[_T("aug")] = 8;
  148. m_MonthNamesMap[_T("sep")] = 9;
  149. m_MonthNamesMap[_T("sept")] = 9;
  150. m_MonthNamesMap[_T("oct")] = 10;
  151. m_MonthNamesMap[_T("nov")] = 11;
  152. m_MonthNamesMap[_T("dec")] = 12;
  153. //Numerical values for the month
  154. m_MonthNamesMap[_T("1")] = 1;
  155. m_MonthNamesMap[_T("01")] = 1;
  156. m_MonthNamesMap[_T("2")] = 2;
  157. m_MonthNamesMap[_T("02")] = 2;
  158. m_MonthNamesMap[_T("3")] = 3;
  159. m_MonthNamesMap[_T("03")] = 3;
  160. m_MonthNamesMap[_T("4")] = 4;
  161. m_MonthNamesMap[_T("04")] = 4;
  162. m_MonthNamesMap[_T("5")] = 5;
  163. m_MonthNamesMap[_T("05")] = 5;
  164. m_MonthNamesMap[_T("6")] = 6;
  165. m_MonthNamesMap[_T("06")] = 6;
  166. m_MonthNamesMap[_T("7")] = 7;
  167. m_MonthNamesMap[_T("07")] = 7;
  168. m_MonthNamesMap[_T("8")] = 8;
  169. m_MonthNamesMap[_T("08")] = 8;
  170. m_MonthNamesMap[_T("9")] = 9;
  171. m_MonthNamesMap[_T("09")] = 9;
  172. m_MonthNamesMap[_T("10")] = 10;
  173. m_MonthNamesMap[_T("11")] = 11;
  174. m_MonthNamesMap[_T("12")] = 12;
  175. //German month names
  176. m_MonthNamesMap[_T("mrz")] = 3;
  177. m_MonthNamesMap[_T("m\xE4r")] = 3;
  178. m_MonthNamesMap[_T("m\xE4rz")] = 3;
  179. m_MonthNamesMap[L"m\xC3\xA4r"] = 3; // UTF-8
  180. m_MonthNamesMap[L"m\xC3\xA4rz"] = 3; // UTF-8
  181. m_MonthNamesMap[_T("mai")] = 5;
  182. m_MonthNamesMap[_T("juni")] = 6;
  183. m_MonthNamesMap[_T("juli")] = 7;
  184. m_MonthNamesMap[_T("okt")] = 10;
  185. m_MonthNamesMap[_T("dez")] = 12;
  186. //Austrian month names
  187. m_MonthNamesMap[_T("j\xE4n")] = 1;
  188. m_MonthNamesMap[L"j\xC3\xA4n"] = 1; // UTF-8
  189. //French month names
  190. m_MonthNamesMap[_T("janv")] = 1;
  191. m_MonthNamesMap[_T("f\xE9")_T("b")] = 1;
  192. m_MonthNamesMap[_T("f\xE9v")] = 2;
  193. m_MonthNamesMap[_T("fev")] = 2;
  194. m_MonthNamesMap[_T("f\xE9vr")] = 2;
  195. m_MonthNamesMap[_T("fevr")] = 2;
  196. m_MonthNamesMap[_T("mars")] = 3;
  197. m_MonthNamesMap[_T("mrs")] = 3;
  198. m_MonthNamesMap[_T("avr")] = 4;
  199. m_MonthNamesMap[_T("juin")] = 6;
  200. m_MonthNamesMap[_T("juil")] = 7;
  201. m_MonthNamesMap[_T("jui")] = 7;
  202. m_MonthNamesMap[_T("ao\xFB")] = 8;
  203. m_MonthNamesMap[_T("ao\xFBt")] = 8;
  204. m_MonthNamesMap[_T("aout")] = 8;
  205. m_MonthNamesMap[_T("d\xE9")_T("c")] = 12;
  206. m_MonthNamesMap[_T("dec")] = 12;
  207. //Italian month names
  208. m_MonthNamesMap[_T("gen")] = 1;
  209. m_MonthNamesMap[_T("mag")] = 5;
  210. m_MonthNamesMap[_T("giu")] = 6;
  211. m_MonthNamesMap[_T("lug")] = 7;
  212. m_MonthNamesMap[_T("ago")] = 8;
  213. m_MonthNamesMap[_T("set")] = 9;
  214. m_MonthNamesMap[_T("ott")] = 10;
  215. m_MonthNamesMap[_T("dic")] = 12;
  216. //Spanish month names
  217. m_MonthNamesMap[_T("ene")] = 1;
  218. m_MonthNamesMap[_T("fbro")] = 2;
  219. m_MonthNamesMap[_T("mzo")] = 3;
  220. m_MonthNamesMap[_T("ab")] = 4;
  221. m_MonthNamesMap[_T("abr")] = 4;
  222. m_MonthNamesMap[_T("agto")] = 8;
  223. m_MonthNamesMap[_T("sbre")] = 9;
  224. m_MonthNamesMap[_T("obre")] = 9;
  225. m_MonthNamesMap[_T("nbre")] = 9;
  226. m_MonthNamesMap[_T("dbre")] = 9;
  227. //Polish month names
  228. m_MonthNamesMap[_T("sty")] = 1;
  229. m_MonthNamesMap[_T("lut")] = 2;
  230. m_MonthNamesMap[_T("kwi")] = 4;
  231. m_MonthNamesMap[_T("maj")] = 5;
  232. m_MonthNamesMap[_T("cze")] = 6;
  233. m_MonthNamesMap[_T("lip")] = 7;
  234. m_MonthNamesMap[_T("sie")] = 8;
  235. m_MonthNamesMap[_T("wrz")] = 9;
  236. m_MonthNamesMap[_T("pa")] = 10;
  237. m_MonthNamesMap[_T("lis")] = 11;
  238. m_MonthNamesMap[_T("gru")] = 12;
  239. //Russian month names
  240. m_MonthNamesMap[_T("\xFF\xED\xE2")] = 1;
  241. m_MonthNamesMap[_T("\xF4\xE5\xE2")] = 2;
  242. m_MonthNamesMap[_T("\xEC\xE0\xF0")] = 3;
  243. m_MonthNamesMap[_T("\xE0\xEF\xF0")] = 4;
  244. m_MonthNamesMap[_T("\xEC\xE0\xE9")] = 5;
  245. m_MonthNamesMap[_T("\xE8\xFE\xED")] = 6;
  246. m_MonthNamesMap[_T("\xE8\xFE\xEB")] = 7;
  247. m_MonthNamesMap[_T("\xE0\xE2\xE3")] = 8;
  248. m_MonthNamesMap[_T("\xF1\xE5\xED")] = 9;
  249. m_MonthNamesMap[_T("\xEE\xEA\xF2")] = 10;
  250. m_MonthNamesMap[_T("\xED\xEE\xFF")] = 11;
  251. m_MonthNamesMap[_T("\xE4\xE5\xEA")] = 12;
  252. //Dutch month names
  253. m_MonthNamesMap[_T("mrt")] = 3;
  254. m_MonthNamesMap[_T("mei")] = 5;
  255. //Portuguese month names
  256. m_MonthNamesMap[_T("out")] = 10;
  257. //Japanese month names
  258. m_MonthNamesMap[_T("1\x8c\x8e")] = 1;
  259. m_MonthNamesMap[_T("2\x8c\x8e")] = 2;
  260. m_MonthNamesMap[_T("3\x8c\x8e")] = 3;
  261. m_MonthNamesMap[_T("4\x8c\x8e")] = 4;
  262. m_MonthNamesMap[_T("5\x8c\x8e")] = 5;
  263. m_MonthNamesMap[_T("6\x8c\x8e")] = 6;
  264. m_MonthNamesMap[_T("7\x8c\x8e")] = 7;
  265. m_MonthNamesMap[_T("8\x8c\x8e")] = 8;
  266. m_MonthNamesMap[_T("9\x8c\x8e")] = 9;
  267. m_MonthNamesMap[_T("10\x8c\x8e")] = 10;
  268. m_MonthNamesMap[_T("11\x8c\x8e")] = 11;
  269. m_MonthNamesMap[_T("12\x8c\x8e")] = 12;
  270. //Korean (Unicode) month names
  271. m_MonthNamesMap[_T("1\xC6\xD4")] = 1;
  272. m_MonthNamesMap[_T("2\xC6\xD4")] = 2;
  273. m_MonthNamesMap[_T("3\xC6\xD4")] = 3;
  274. m_MonthNamesMap[_T("4\xC6\xD4")] = 4;
  275. m_MonthNamesMap[_T("5\xC6\xD4")] = 5;
  276. m_MonthNamesMap[_T("6\xC6\xD4")] = 6;
  277. m_MonthNamesMap[_T("7\xC6\xD4")] = 7;
  278. m_MonthNamesMap[_T("8\xC6\xD4")] = 8;
  279. m_MonthNamesMap[_T("9\xC6\xD4")] = 9;
  280. m_MonthNamesMap[_T("10\xC6\xD4")] = 10;
  281. m_MonthNamesMap[_T("11\xC6\xD4")] = 11;
  282. m_MonthNamesMap[_T("12\xC6\xD4")] = 12;
  283. //Korean (EUC-KR) month names
  284. m_MonthNamesMap[_T("1\xBF\xF9")] = 1;
  285. m_MonthNamesMap[_T("2\xBF\xF9")] = 2;
  286. m_MonthNamesMap[_T("3\xBF\xF9")] = 3;
  287. m_MonthNamesMap[_T("4\xBF\xF9")] = 4;
  288. m_MonthNamesMap[_T("5\xBF\xF9")] = 5;
  289. m_MonthNamesMap[_T("6\xBF\xF9")] = 6;
  290. m_MonthNamesMap[_T("7\xBF\xF9")] = 7;
  291. m_MonthNamesMap[_T("8\xBF\xF9")] = 8;
  292. m_MonthNamesMap[_T("9\xBF\xF9")] = 9;
  293. m_MonthNamesMap[_T("10\xBF\xF9")] = 10;
  294. m_MonthNamesMap[_T("11\xBF\xF9")] = 11;
  295. m_MonthNamesMap[_T("12\xBF\xF9")] = 12;
  296. //Finnish month names
  297. m_MonthNamesMap[_T("tammi")] = 1;
  298. m_MonthNamesMap[_T("helmi")] = 2;
  299. m_MonthNamesMap[_T("maalis")] = 3;
  300. m_MonthNamesMap[_T("huhti")] = 4;
  301. m_MonthNamesMap[_T("touko")] = 5;
  302. m_MonthNamesMap[_T("kes\xE4")] = 6;
  303. m_MonthNamesMap[_T("hein\xE4")] = 7;
  304. m_MonthNamesMap[_T("elo")] = 8;
  305. m_MonthNamesMap[_T("syys")] = 9;
  306. m_MonthNamesMap[_T("loka")] = 10;
  307. m_MonthNamesMap[_T("marras")] = 11;
  308. m_MonthNamesMap[_T("joulu")] = 12;
  309. //There are more languages and thus month
  310. //names, but as long nobody reports a
  311. //problem, I won't add them, there are way
  312. //too much languages
  313. //Some very strange combinations of names and numbers I've seen.
  314. //The developers of those ftp servers must have been dumb.
  315. m_MonthNamesMap[_T("jan1")] = 1;
  316. m_MonthNamesMap[_T("feb2")] = 2;
  317. m_MonthNamesMap[_T("mar3")] = 3;
  318. m_MonthNamesMap[_T("apr4")] = 4;
  319. m_MonthNamesMap[_T("may5")] = 5;
  320. m_MonthNamesMap[_T("jun6")] = 6;
  321. m_MonthNamesMap[_T("jul7")] = 7;
  322. m_MonthNamesMap[_T("aug8")] = 8;
  323. m_MonthNamesMap[_T("sep9")] = 9;
  324. m_MonthNamesMap[_T("sept9")] = 9;
  325. m_MonthNamesMap[_T("oct0")] = 10;
  326. m_MonthNamesMap[_T("nov1")] = 11;
  327. m_MonthNamesMap[_T("dec2")] = 12;
  328. // Slovenian month names
  329. m_MonthNamesMap[_T("avg")] = 8;
  330. #ifdef LISTDEBUG
  331. int i=-1;
  332. while (*data[++i])
  333. {
  334. char *pData=new char[strlen(data[i])+3];
  335. sprintf(pData, "%s\r\n", data[i]);
  336. AddData(pData, strlen(pData));
  337. }
  338. TRACE1("%d lines added\n", i);
  339. #endif
  340. }
  341. CFtpListResult::~CFtpListResult()
  342. {
  343. t_list *ptr=listhead;
  344. t_list *ptr2;
  345. while (ptr)
  346. {
  347. delete [] ptr->buffer;
  348. ptr2=ptr;
  349. ptr=ptr->next;
  350. delete ptr2;
  351. }
  352. if (m_prevline)
  353. delete [] m_prevline;
  354. if (m_curline)
  355. delete [] m_curline;
  356. }
  357. t_directory::t_direntry *CFtpListResult::getList(int &num, CTime EntryTime, bool mlst)
  358. {
  359. #ifdef _DEBUG
  360. USES_CONVERSION;
  361. #endif
  362. char *line=GetLine();
  363. m_curline=line;
  364. while (line)
  365. {
  366. int tmp;
  367. char *tmpline = new char[strlen(line) + 1];
  368. strcpy(tmpline, line);
  369. t_directory::t_direntry direntry;
  370. if (parseLine(tmpline, strlen(tmpline), direntry, tmp, mlst))
  371. {
  372. delete [] tmpline;
  373. if (tmp)
  374. m_server.nServerType |= tmp;
  375. if (direntry.name!=_MPT(".") && direntry.name!=_MPT(".."))
  376. {
  377. AddLine(direntry);
  378. }
  379. if (m_prevline)
  380. {
  381. delete [] m_prevline;
  382. m_prevline=0;
  383. }
  384. if (m_curline!=line)
  385. delete [] m_curline;
  386. delete [] line;
  387. line=GetLine();
  388. m_curline=line;
  389. }
  390. else
  391. {
  392. delete [] tmpline;
  393. if (m_prevline)
  394. {
  395. if (m_curline!=line)
  396. {
  397. delete [] m_prevline;
  398. m_prevline=m_curline;
  399. delete [] line;
  400. line=GetLine();
  401. m_curline=line;
  402. }
  403. else
  404. {
  405. line=new char[strlen(m_prevline)+strlen(m_curline)+2];
  406. sprintf(line, "%s %s", m_prevline, m_curline);
  407. }
  408. }
  409. else
  410. {
  411. m_prevline=line;
  412. line=GetLine();
  413. m_curline=line;
  414. }
  415. }
  416. }
  417. if (m_prevline)
  418. {
  419. delete [] m_prevline;
  420. m_prevline=0;
  421. }
  422. if (m_curline!=line)
  423. delete [] m_curline;
  424. delete [] line;
  425. m_curline=0;
  426. num=m_EntryList.size();
  427. if (!num)
  428. return 0;
  429. t_directory::t_direntry *res=new t_directory::t_direntry[num];
  430. int i=0;
  431. for (tEntryList::iterator iter=m_EntryList.begin();iter!=m_EntryList.end();iter++, i++)
  432. {
  433. res[i]=*iter;
  434. res[i].EntryTime=EntryTime;
  435. }
  436. m_EntryList.clear();
  437. return res;
  438. }
  439. BOOL CFtpListResult::parseLine(const char *lineToParse, const int linelen, t_directory::t_direntry &direntry, int &nFTPServerType, bool mlst)
  440. {
  441. USES_CONVERSION;
  442. nFTPServerType = 0;
  443. direntry.ownergroup = _T("");
  444. if (parseAsMlsd(lineToParse, linelen, direntry, mlst))
  445. return TRUE;
  446. if (parseAsUnix(lineToParse, linelen, direntry))
  447. return TRUE;
  448. if (parseAsDos(lineToParse, linelen, direntry))
  449. return TRUE;
  450. if (parseAsEPLF(lineToParse, linelen, direntry))
  451. return TRUE;
  452. if (parseAsVMS(lineToParse, linelen, direntry))
  453. {
  454. #ifndef LISTDEBUG
  455. m_server.nServerType |= FZ_SERVERTYPE_SUB_FTP_VMS;
  456. #endif // LISTDEBUG
  457. return TRUE;
  458. }
  459. if (parseAsOther(lineToParse, linelen, direntry))
  460. return TRUE;
  461. if (parseAsIBMMVS(lineToParse, linelen, direntry))
  462. return TRUE;
  463. if (parseAsIBMMVSPDS(lineToParse, linelen, direntry))
  464. return TRUE;
  465. if (parseAsIBM(lineToParse, linelen, direntry))
  466. return TRUE;
  467. if (parseAsWfFtp(lineToParse, linelen, direntry))
  468. return TRUE;
  469. // Should be last
  470. if (parseAsIBMMVSPDS2(lineToParse, linelen, direntry))
  471. return TRUE;
  472. return FALSE;
  473. }
  474. // Used only with LISTDEBUG
  475. void CFtpListResult::AddData(char *data, int size)
  476. {
  477. #ifdef _DEBUG
  478. USES_CONVERSION;
  479. #endif
  480. if (!size)
  481. return;
  482. if (!m_curlistaddpos)
  483. m_curlistaddpos = new t_list;
  484. else
  485. {
  486. m_curlistaddpos->next = new t_list;
  487. m_curlistaddpos = m_curlistaddpos->next;
  488. }
  489. if (!listhead)
  490. {
  491. curpos = m_curlistaddpos;
  492. listhead = m_curlistaddpos;
  493. }
  494. m_curlistaddpos->buffer = data;
  495. m_curlistaddpos->len = size;
  496. m_curlistaddpos->next = 0;
  497. t_list *pOldListPos = curpos;
  498. int nOldListBufferPos = pos;
  499. //Try if there are already some complete lines
  500. t_directory::t_direntry direntry;
  501. char *line = GetLine();
  502. m_curline = line;
  503. while (line)
  504. {
  505. if (curpos)
  506. {
  507. pOldListPos = curpos;
  508. nOldListBufferPos = pos;
  509. }
  510. else
  511. {
  512. delete [] line;
  513. if (m_curline != line)
  514. delete [] m_curline;
  515. m_curline = 0;
  516. break;
  517. }
  518. int tmp;
  519. char *tmpline = new char[strlen(line) + 1];
  520. strcpy(tmpline, line);
  521. if (parseLine(tmpline, strlen(tmpline), direntry, tmp, false))
  522. {
  523. delete [] tmpline;
  524. if (tmp)
  525. m_server.nServerType |= tmp;
  526. if (direntry.name!=_MPT(".") && direntry.name!=_MPT(".."))
  527. {
  528. AddLine(direntry);
  529. }
  530. if (m_prevline)
  531. {
  532. delete [] m_prevline;
  533. m_prevline=0;
  534. }
  535. if (m_curline!=line)
  536. delete [] m_curline;
  537. delete [] line;
  538. line = GetLine();
  539. m_curline = line;
  540. }
  541. else
  542. {
  543. delete [] tmpline;
  544. if (m_prevline)
  545. {
  546. if (m_curline != line)
  547. {
  548. delete [] m_prevline;
  549. m_prevline = m_curline;
  550. delete [] line;
  551. line = GetLine();
  552. m_curline = line;
  553. }
  554. else
  555. {
  556. line=new char[strlen(m_prevline)+strlen(m_curline)+2];
  557. sprintf(line, "%s %s", m_prevline, m_curline);
  558. }
  559. }
  560. else
  561. {
  562. m_prevline=line;
  563. line=GetLine();
  564. m_curline=line;
  565. }
  566. }
  567. }
  568. curpos=pOldListPos;
  569. pos=nOldListBufferPos;
  570. }
  571. void CFtpListResult::SendToMessageLog(HWND hWnd, UINT nMsg)
  572. {
  573. t_list *oldlistpos = curpos;
  574. int oldbufferpos = pos;
  575. curpos = listhead;
  576. pos=0;
  577. char *line = GetLine();
  578. // Note that FZ_LOG_INFO here is not checked against debug level, as the direct
  579. // call to PostMessage bypasses check in LogMessage.
  580. // So we get the listing on any logging level, what is actually what we want
  581. if (!line)
  582. {
  583. //Displays a message in the message log
  584. t_ffam_statusmessage *pStatus = new t_ffam_statusmessage;
  585. pStatus->post = TRUE;
  586. pStatus->status = _T("<Empty directory listing>");
  587. pStatus->type = FZ_LOG_INFO;
  588. PostMessage(hWnd, nMsg, FZ_MSG_MAKEMSG(FZ_MSG_STATUS, 0), (LPARAM)pStatus);
  589. }
  590. while (line)
  591. {
  592. CString status = line;
  593. delete [] line;
  594. //Displays a message in the message log
  595. t_ffam_statusmessage *pStatus = new t_ffam_statusmessage;
  596. pStatus->post = TRUE;
  597. pStatus->status = status;
  598. pStatus->type = FZ_LOG_INFO;
  599. if (!PostMessage(hWnd, nMsg, FZ_MSG_MAKEMSG(FZ_MSG_STATUS, 0), (LPARAM)pStatus))
  600. delete pStatus;
  601. line = GetLine();
  602. }
  603. curpos = oldlistpos;
  604. pos = oldbufferpos;
  605. }
  606. char * CFtpListResult::GetLine()
  607. {
  608. if (!curpos)
  609. return 0;
  610. int len=curpos->len;
  611. while (curpos->buffer[pos]=='\r' || curpos->buffer[pos]=='\n' || curpos->buffer[pos]==' ' || curpos->buffer[pos]=='\t')
  612. {
  613. pos++;
  614. if (pos>=len)
  615. {
  616. curpos=curpos->next;
  617. if (!curpos)
  618. return 0;
  619. len=curpos->len;
  620. pos=0;
  621. }
  622. }
  623. t_list *startptr=curpos;
  624. int startpos=pos;
  625. int reslen=0;
  626. int emptylen=0;
  627. while ((curpos->buffer[pos]!='\n')&&(curpos->buffer[pos]!='\r'))
  628. {
  629. if (curpos->buffer[pos]!=' ' && curpos->buffer[pos]!='\t')
  630. {
  631. reslen+=emptylen+1;
  632. emptylen=0;
  633. }
  634. else
  635. emptylen++;
  636. pos++;
  637. if (pos>=len)
  638. {
  639. curpos=curpos->next;
  640. if (!curpos)
  641. break;
  642. len=curpos->len;
  643. pos=0;
  644. }
  645. }
  646. char *res = new char[reslen+1];
  647. res[reslen]=0;
  648. int respos=0;
  649. while (startptr!=curpos && reslen)
  650. {
  651. int copylen=startptr->len-startpos;
  652. if (copylen>reslen)
  653. copylen=reslen;
  654. memcpy(&res[respos],&startptr->buffer[startpos], copylen);
  655. reslen-=copylen;
  656. respos+=startptr->len-startpos;
  657. startpos=0;
  658. startptr=startptr->next;
  659. }
  660. if (curpos && reslen)
  661. {
  662. int copylen=pos-startpos;
  663. if (copylen>reslen)
  664. copylen=reslen;
  665. memcpy(&res[respos], &curpos->buffer[startpos], copylen);
  666. }
  667. return res;
  668. }
  669. void CFtpListResult::AddLine(t_directory::t_direntry &direntry)
  670. {
  671. if (m_server.nTimeZoneOffset &&
  672. direntry.date.hasdate && direntry.date.hastime && !direntry.date.utc)
  673. {
  674. SYSTEMTIME st = {0};
  675. st.wYear = direntry.date.year;
  676. st.wMonth = direntry.date.month;
  677. st.wDay = direntry.date.day;
  678. st.wHour = direntry.date.hour;
  679. st.wMinute = direntry.date.minute;
  680. st.wSecond = direntry.date.second;
  681. FILETIME ft;
  682. SystemTimeToFileTime(&st, &ft);
  683. _int64 nFt = ((_int64)ft.dwHighDateTime << 32) + ft.dwLowDateTime;
  684. _int64 nFt2 = nFt;
  685. nFt += ((_int64)m_server.nTimeZoneOffset) * 10000000 * 60;
  686. ft.dwHighDateTime = static_cast<unsigned long>(nFt >> 32);
  687. ft.dwLowDateTime = static_cast<unsigned long>(nFt & 0xFFFFFFFF);
  688. FileTimeToSystemTime(&ft, &st);
  689. direntry.date.year = st.wYear;
  690. direntry.date.month = st.wMonth;
  691. direntry.date.day = st.wDay;
  692. direntry.date.hour = st.wHour;
  693. direntry.date.minute = st.wMinute;
  694. direntry.date.second = st.wSecond;
  695. }
  696. direntry.lName = direntry.name;
  697. direntry.lName.MakeLower();
  698. if (m_server.nServerType&FZ_SERVERTYPE_SUB_FTP_VMS &&
  699. (!COptions::GetOptionVal(OPTION_VMSALLREVISIONS) || direntry.dir))
  700. { //Remove version information, only keep the latest file
  701. int pos=direntry.name.ReverseFind(_MPT(';'));
  702. if (pos<=0 || pos>=(direntry.name.GetLength()-1))
  703. return;;
  704. int version=_ttoi(direntry.name.Mid(pos+1));
  705. direntry.name=direntry.name.Left(pos);
  706. tEntryList::iterator entryiter=m_EntryList.begin();
  707. tTempData::iterator dataiter=m_TempData.begin();
  708. BOOL bContinue=FALSE;
  709. while (entryiter!=m_EntryList.end())
  710. {
  711. ASSERT(dataiter!=m_TempData.end());
  712. t_directory::t_direntry dir=*entryiter;
  713. int oldversion=*dataiter;
  714. if (direntry.name==dir.name)
  715. {
  716. bContinue=TRUE;
  717. if (version>oldversion)
  718. {
  719. *entryiter=direntry;
  720. *dataiter=version;
  721. }
  722. break;
  723. }
  724. entryiter++;
  725. dataiter++;
  726. }
  727. if (bContinue)
  728. return;
  729. m_EntryList.push_back(direntry);
  730. m_TempData.push_back(version);
  731. }
  732. else
  733. {
  734. m_EntryList.push_back(direntry);
  735. m_TempData.push_back(0);
  736. }
  737. }
  738. bool CFtpListResult::IsNumeric(const char *str, int len) const
  739. {
  740. if (!str)
  741. return false;
  742. if (!*str)
  743. return false;
  744. const char *p=str;
  745. while(*p)
  746. {
  747. if (len != -1)
  748. if ((p - str) >= len)
  749. return true;
  750. if (*p<'0' || *p>'9')
  751. {
  752. return false;
  753. }
  754. p++;
  755. }
  756. return true;
  757. }
  758. bool CFtpListResult::ParseShortDate(const char *str, int len, t_directory::t_direntry::t_date &date) const
  759. {
  760. if (!str)
  761. return false;
  762. if (len <= 0)
  763. return false;
  764. int i=0;
  765. //Extract the date
  766. BOOL bGotYear = FALSE;
  767. BOOL bGotMonth = FALSE;
  768. BOOL bGotDay = FALSE;
  769. int value = 0;
  770. bool numeric = true;
  771. while (str[i] != '-' && str[i] != '.' && str[i] != '/')
  772. {
  773. if (!str[i])
  774. return false;
  775. // Left half of token not numeric, check if it's a month name
  776. if (str[i] < '0' || str[i] > '9')
  777. numeric = false;
  778. else
  779. {
  780. value *= 10;
  781. value += str[i] - '0';
  782. }
  783. i++;
  784. if (i == len)
  785. return false;
  786. }
  787. if (!i)
  788. return false;
  789. if (!numeric)
  790. {
  791. std::map<CString, int>::const_iterator iter;
  792. char *tmpstr = new char[i + 1];
  793. strncpy(tmpstr, str, i);
  794. tmpstr[i] = 0;
  795. strlwr(tmpstr);
  796. USES_CONVERSION;
  797. iter = m_MonthNamesMap.find(A2T(tmpstr));
  798. delete [] tmpstr;
  799. if (iter == m_MonthNamesMap.end())
  800. return false;
  801. date.month = iter->second;
  802. bGotMonth = true;
  803. }
  804. else if (i == 4)
  805. { //Seems to be yyyy-mm-dd
  806. if (value < 1900)
  807. return false;
  808. date.year = value;
  809. bGotYear = TRUE;
  810. }
  811. else if (i <= 2)
  812. {
  813. if (str[i] == '.')
  814. {
  815. // Maybe dd.mm.yyyy
  816. if (!value || value > 31)
  817. return false;
  818. date.day = value;
  819. bGotDay = TRUE;
  820. }
  821. else
  822. {
  823. // Seems to be mm-dd-yyyy or mm/dd/yyyy (stupid format, though)
  824. if (!value)
  825. return false;
  826. else if (value > 12) // sigh, guess dd/mm/yyyy instead
  827. {
  828. date.day = value;
  829. bGotDay = true;
  830. }
  831. else
  832. {
  833. date.month = value;
  834. bGotMonth = TRUE;
  835. }
  836. }
  837. }
  838. else
  839. return false;
  840. //Extract the second date field
  841. const char *p = str + i + 1;
  842. len -= i + 1;
  843. i=0;
  844. value=0;
  845. if (i >= len)
  846. return false;
  847. while (p[i]!='-' && p[i]!='.' && p[i]!='/')
  848. {
  849. value *= 10;
  850. value += p[i]-'0';
  851. i++;
  852. if (i >= len)
  853. return false;
  854. }
  855. if (bGotYear || bGotDay)
  856. {
  857. // Month field in yyyy-mm-dd or dd-mm-yyyy
  858. if (!value || value > 12)
  859. return false;
  860. date.month = value;
  861. bGotMonth = TRUE;
  862. }
  863. else
  864. {
  865. // Day field in mm-dd-yyyy
  866. if (!value || value > 31)
  867. return false;
  868. date.day = value;
  869. bGotDay = TRUE;
  870. }
  871. //Extract the last date field
  872. p += i+1;
  873. len -= i + 1;
  874. i=0;
  875. value=0;
  876. if (i >= len)
  877. return false;
  878. while (p[i]!='-' && p[i]!='.' && p[i]!='/')
  879. {
  880. value *= 10;
  881. value += p[i]-'0';
  882. i++;
  883. if (i >= len)
  884. break;
  885. }
  886. if (bGotYear)
  887. {
  888. // Day field in yyyy-mm-dd
  889. if (!value || value > 31)
  890. return false;
  891. date.day = value;
  892. }
  893. else
  894. {
  895. //Year in dd.mm.yyyy or mm-dd-yyyy
  896. date.year = value;
  897. if (date.year<50)
  898. date.year+=2000;
  899. else if (date.year<1000)
  900. date.year += 1900;
  901. }
  902. date.hasdate = TRUE;
  903. return true;
  904. }
  905. BOOL CFtpListResult::parseAsVMS(const char *line, const int linelen, t_directory::t_direntry &direntry)
  906. {
  907. int tokenlen = 0;
  908. int pos = 0;
  909. USES_CONVERSION;
  910. std::map<CString, int>::const_iterator iter;
  911. t_directory::t_direntry dir;
  912. dir.bUnsure = FALSE;
  913. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  914. if (!str)
  915. return FALSE;
  916. if (!strnchr(str, tokenlen, ';'))
  917. return FALSE;
  918. dir.size = -2;
  919. const char *separator = strnchr(str, tokenlen, ';');
  920. if (!separator)
  921. return FALSE;
  922. dir.dir = FALSE;
  923. if ((separator - str) > 4)
  924. if (*(separator - 4) == '.')
  925. if (*(separator - 3) == 'D')
  926. if (*(separator - 2) == 'I')
  927. if (*(separator - 1) == 'R')
  928. {
  929. dir.dir = TRUE;
  930. }
  931. if (dir.dir)
  932. {
  933. int i;
  934. LPTSTR pBuffer = dir.name.GetBuffer(tokenlen - 4);
  935. for (i = 0; i < (separator - str - 4); i++)
  936. pBuffer[i] = str[i];
  937. for (i = 0; i < (tokenlen - (separator - str)); i++)
  938. pBuffer[i + (separator - str) - 4] = separator[i];
  939. dir.name.ReleaseBuffer(tokenlen - 4);
  940. }
  941. else
  942. copyStr(dir.name, 0, str, tokenlen);
  943. // This field is either the size or a username (???) enclosed in [].
  944. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  945. if (!str)
  946. return FALSE;
  947. bool gotSize = false;
  948. const char *p = strnchr(str, tokenlen, '/');
  949. if (!p && IsNumeric(str, tokenlen))
  950. {
  951. gotSize = true;
  952. dir.size = strntoi64(str, tokenlen) * 512;
  953. }
  954. else if (p && p > str && IsNumeric(str, p - str))
  955. {
  956. gotSize = true;
  957. dir.size = strntoi64(str, p - str) * 512;
  958. }
  959. else
  960. {
  961. if (tokenlen < 3 || str[0] != '[' || str[tokenlen - 1] != ']')
  962. return false;
  963. copyStr(dir.ownergroup, 0, str + 1, tokenlen - 2);
  964. }
  965. if (!gotSize)
  966. {
  967. //Size
  968. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  969. if (!str)
  970. return FALSE;
  971. const char *p = strnchr(str, tokenlen, '/');
  972. int len;
  973. if (p)
  974. len = p - str;
  975. else
  976. len = tokenlen;
  977. if (!IsNumeric(str, len))
  978. return FALSE;
  979. dir.size = strntoi64(str, len) * 512;
  980. }
  981. //Get date
  982. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  983. if (!str)
  984. return FALSE;
  985. dir.date.hasdate = TRUE;
  986. //Day
  987. p = str;
  988. while (*p != '-')
  989. if ((++p - str) == tokenlen)
  990. return 0;
  991. dir.date.day = static_cast<int>(strntoi64(str, p-str));
  992. if (!dir.date.day || dir.date.day > 31)
  993. return FALSE;
  994. p++;
  995. const char *pMonth = p;
  996. //Month
  997. while (*p != '-')
  998. if ((++p - str) == tokenlen)
  999. return FALSE;
  1000. if ((p - pMonth) >= 15)
  1001. return FALSE;
  1002. char buffer[15] = {0};
  1003. memcpy(buffer, pMonth, p-pMonth);
  1004. strlwr(buffer);
  1005. iter = m_MonthNamesMap.find(A2T(buffer));
  1006. if (iter == m_MonthNamesMap.end())
  1007. return FALSE;
  1008. dir.date.month = iter->second;
  1009. p++;
  1010. dir.date.year = static_cast<int>(strntoi64(p, tokenlen - (p - str)));
  1011. //Get time
  1012. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1013. if (str && strnchr(str + 1, tokenlen - 2, ':'))
  1014. {
  1015. dir.date.hastime = TRUE;
  1016. p = str;
  1017. //Hours
  1018. while (*p != ':')
  1019. if ((++p - str) == tokenlen)
  1020. return 0;
  1021. dir.date.hour = static_cast<int>(strntoi64(str, p - str));
  1022. if (dir.date.hour < 0 || dir.date.hour > 23)
  1023. return FALSE;
  1024. p++;
  1025. const char *pMinute = p;
  1026. //Minutes
  1027. while (*p && *p != ':' )
  1028. p++;
  1029. dir.date.minute = static_cast<int>(strntoi64(pMinute, p - pMinute));
  1030. if (dir.date.minute < 0 || dir.date.minute > 59)
  1031. return FALSE;
  1032. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1033. }
  1034. else
  1035. {
  1036. dir.date.hastime = FALSE;
  1037. }
  1038. while (str)
  1039. {
  1040. if (tokenlen > 2 && str[0] == '(' && str[tokenlen - 1] == ')')
  1041. {
  1042. if (dir.permissionstr != _T(""))
  1043. dir.permissionstr += _T(" ");
  1044. CString tmp;
  1045. copyStr(tmp, 0, str + 1, tokenlen - 2);
  1046. dir.permissionstr += tmp;
  1047. }
  1048. else if (tokenlen > 2 && str[0] == '[' && str[tokenlen - 1] == ']')
  1049. {
  1050. if (dir.ownergroup != _T(""))
  1051. dir.ownergroup += _T(" ");
  1052. CString tmp;
  1053. copyStr(tmp, 0, str + 1, tokenlen - 2);
  1054. dir.ownergroup += tmp;
  1055. }
  1056. else
  1057. {
  1058. if (dir.permissionstr != _T(""))
  1059. dir.permissionstr += _T(" ");
  1060. CString tmp;
  1061. copyStr(tmp, 0, str, tokenlen);
  1062. dir.permissionstr += tmp;
  1063. }
  1064. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1065. }
  1066. direntry = dir;
  1067. return TRUE;
  1068. }
  1069. BOOL CFtpListResult::parseAsEPLF(const char *line, const int linelen, t_directory::t_direntry &direntry)
  1070. {
  1071. t_directory::t_direntry dir;
  1072. const char *str = strstr(line, "\t");
  1073. //Check if directory listing is an EPLF one
  1074. if (*line=='+' && str)
  1075. {
  1076. str++;
  1077. if (!*str)
  1078. return FALSE;
  1079. dir.bLink = FALSE;
  1080. dir.bUnsure = FALSE;
  1081. dir.date.hasdate = dir.date.hastime = FALSE;
  1082. dir.dir = FALSE;
  1083. dir.size = -2;
  1084. dir.name = str;
  1085. const char *fact = line + 1;
  1086. const char *nextfact = fact;
  1087. nextfact = strstr(nextfact, ",");
  1088. //if (nextfact && nextfact < str)
  1089. // *nextfact=0;
  1090. while (fact<=(str-2))
  1091. {
  1092. int len;
  1093. if (!nextfact)
  1094. len = str - fact - 1;
  1095. else
  1096. len = nextfact - fact;
  1097. if (len == 1 && fact[0] == '/')
  1098. dir.dir = TRUE;
  1099. else if (*fact=='s')
  1100. dir.size = strntoi64(fact+1, len-1);
  1101. else if (*fact=='m')
  1102. {
  1103. __int64 rawtime = strntoi64(fact+1, len-1);
  1104. COleDateTime time((time_t)rawtime);
  1105. dir.date.hasdate = TRUE;
  1106. dir.date.hastime = TRUE;
  1107. dir.date.year = time.GetYear();
  1108. dir.date.month = time.GetMonth();
  1109. dir.date.day = time.GetDay();
  1110. dir.date.hour = time.GetHour();
  1111. dir.date.minute = time.GetMinute();
  1112. }
  1113. else if (len == 5 && *fact=='u' && *(fact+1)=='p')
  1114. {
  1115. char buffer[4] = {0};
  1116. memcpy(buffer, fact+2, len-2);
  1117. direntry.permissionstr = buffer;
  1118. }
  1119. if (!nextfact || nextfact>=(str-2))
  1120. break;
  1121. fact = nextfact+1;
  1122. nextfact = strstr(nextfact+1, ",");
  1123. // if (nextfact && nextfact<str)
  1124. // *nextfact=0;
  1125. }
  1126. direntry = dir;
  1127. return TRUE;
  1128. }
  1129. return FALSE;
  1130. }
  1131. BOOL CFtpListResult::parseAsMlsd(const char *line, const int linelen, t_directory::t_direntry &direntry, bool mlst)
  1132. {
  1133. #ifdef _DEBUG
  1134. USES_CONVERSION;
  1135. #endif
  1136. // MLSD format as described here: http://www.ietf.org/internet-drafts/draft-ietf-ftpext-mlst-16.txt
  1137. // Parsing is done strict, abort on slightest error.
  1138. // If we ever add some detection that entry is symlink,
  1139. // make sure to add support for resolving the symlink
  1140. // using MLST to TFTPFileSystem::ReadSymlink
  1141. int pos = 0;
  1142. int tokenlen = 0;
  1143. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1144. if (!str)
  1145. return FALSE;
  1146. CString facts(str, tokenlen);
  1147. if (facts.IsEmpty())
  1148. return FALSE;
  1149. direntry.bUnsure = FALSE;
  1150. direntry.dir = FALSE;
  1151. direntry.bLink = FALSE;
  1152. direntry.ownergroup = _T("");
  1153. direntry.permissionstr = _T("");
  1154. CString owner, group, uid, gid;
  1155. while (!facts.IsEmpty())
  1156. {
  1157. int delim = facts.Find(';');
  1158. if (delim < 3)
  1159. {
  1160. if (delim != -1)
  1161. return 0;
  1162. else
  1163. delim = facts.GetLength();
  1164. }
  1165. int pos = facts.Find('=');
  1166. if (pos < 1 || pos > delim)
  1167. {
  1168. return FALSE;
  1169. }
  1170. CString factname = facts.Left(pos);
  1171. factname.MakeLower();
  1172. CString value = facts.Mid(pos + 1, delim - pos - 1);
  1173. // When adding new facts, update filter in CFtpControlSocket::LogOnToServer
  1174. // (CONNECT_FEAT state)
  1175. if (factname == _T("type"))
  1176. {
  1177. if (!value.CompareNoCase(_T("dir")))
  1178. {
  1179. direntry.dir = TRUE;
  1180. }
  1181. // This is syntax used by proftpd by default
  1182. // http://www.proftpd.org/docs/modules/mod_facts.html
  1183. // They claim it's the correct one.
  1184. // See also
  1185. // http://www.rfc-editor.org/errata_search.php?rfc=3659&eid=1500
  1186. else if (!value.Left(15).CompareNoCase(_T("OS.unix=symlink")))
  1187. {
  1188. direntry.dir = TRUE;
  1189. direntry.bLink = TRUE;
  1190. // actually symlink target should not be included in this syntax,
  1191. // but just in case some servers do.
  1192. if ((value.GetLength() > 16) && (value[15] == ':'))
  1193. direntry.linkTarget = value.Mid(16);
  1194. }
  1195. // This is syntax shown in RFC 3659 section 7.7.4 "A More Complex Example"
  1196. // Type=OS.unix=slink:/foobar;Perm=;Unique=keVO1+4G4; foobar
  1197. // https://tools.ietf.org/html/rfc3659
  1198. else if (!value.Left(13).CompareNoCase(_T("OS.unix=slink")))
  1199. {
  1200. direntry.dir = TRUE;
  1201. direntry.bLink = TRUE;
  1202. if ((value.GetLength() > 14) && (value[13] == ':'))
  1203. direntry.linkTarget = value.Mid(14);
  1204. }
  1205. else if (!value.CompareNoCase(L"cdir"))
  1206. {
  1207. // ProFTPD up to 1.3.6rc1 and 1.3.5a incorrectly uses "cdir" for the current working directory.
  1208. // So at least in MLST, where this would be the only entry, we treat it like "dir".
  1209. if (mlst)
  1210. {
  1211. direntry.dir = TRUE;
  1212. }
  1213. else
  1214. {
  1215. return FALSE;
  1216. }
  1217. }
  1218. else if (!value.CompareNoCase(L"pdir"))
  1219. {
  1220. return FALSE;
  1221. }
  1222. }
  1223. else if (factname == _T("size"))
  1224. {
  1225. direntry.size = 0;
  1226. for (unsigned int i = 0; i < value.GetLength(); ++i)
  1227. {
  1228. if (value[i] < '0' || value[i] > '9')
  1229. return FALSE;
  1230. direntry.size *= 10;
  1231. direntry.size += value[i] - '0';
  1232. }
  1233. }
  1234. else if (factname == _T("modify") ||
  1235. (!direntry.date.hasdate && factname == _T("create")))
  1236. {
  1237. if (!parseMlsdDateTime(value, direntry.date))
  1238. return FALSE;
  1239. }
  1240. else if (factname == _T("perm"))
  1241. {
  1242. // there's no way we can convert Perm fact to unix-style permissions,
  1243. // so we at least present Perm as-is to a user
  1244. direntry.humanpermstr = value;
  1245. }
  1246. else if (factname == _T("unix.mode"))
  1247. {
  1248. direntry.permissionstr = value;
  1249. }
  1250. else if (factname == _T("unix.owner") || factname == _T("unix.user"))
  1251. {
  1252. owner = value;
  1253. }
  1254. else if (factname == _T("unix.group"))
  1255. {
  1256. group = value;
  1257. }
  1258. else if (factname == _T("unix.uid"))
  1259. {
  1260. uid = value;
  1261. }
  1262. else if (factname == _T("unix.gid"))
  1263. {
  1264. gid = value;
  1265. }
  1266. facts = facts.Mid(delim + 1);
  1267. }
  1268. // The order of the facts is undefined, so assemble ownerGroup in correct
  1269. // order
  1270. if (!owner.IsEmpty())
  1271. direntry.ownergroup += owner;
  1272. else if (!uid.IsEmpty())
  1273. direntry.ownergroup += uid;
  1274. if (!group.IsEmpty())
  1275. direntry.ownergroup += _T(" ") + group;
  1276. else if (!gid.IsEmpty())
  1277. direntry.ownergroup += _T(" ") + gid;
  1278. if (!(str = GetNextToken(line, linelen, tokenlen, pos, 1)))
  1279. return FALSE;
  1280. CString fileName;
  1281. copyStr(fileName, 0, str, tokenlen, true);
  1282. CServerPath path(fileName);
  1283. direntry.name = path.GetLastSegment();
  1284. if (direntry.name.IsEmpty())
  1285. {
  1286. direntry.name = fileName;
  1287. }
  1288. return TRUE;
  1289. }
  1290. bool CFtpListResult::parseMlsdDateTime(const CString value, t_directory::t_direntry::t_date &date) const
  1291. {
  1292. if (value.IsEmpty())
  1293. return FALSE;
  1294. bool result = FALSE;
  1295. int Year, Month, Day, Hours, Minutes, Seconds;
  1296. Year=Month=Day=Hours=Minutes=Seconds=0;
  1297. if (swscanf((LPCWSTR)value, L"%4d%2d%2d%2d%2d%2d", &Year, &Month, &Day, &Hours, &Minutes, &Seconds) == 6)
  1298. {
  1299. date.hasdate = TRUE;
  1300. date.hastime = TRUE;
  1301. date.hasseconds = TRUE;
  1302. result = TRUE;
  1303. }
  1304. else if (swscanf((LPCWSTR)value, L"%4d%2d%2d", &Year, &Month, &Day) == 3)
  1305. {
  1306. date.hasdate = TRUE;
  1307. date.hastime = FALSE;
  1308. result = TRUE;
  1309. }
  1310. if (result)
  1311. {
  1312. date.year = Year;
  1313. date.month = Month;
  1314. date.day = Day;
  1315. date.hour = Hours;
  1316. date.minute = Minutes;
  1317. date.second = Seconds;
  1318. date.utc = TRUE;
  1319. }
  1320. return result;
  1321. }
  1322. BOOL CFtpListResult::parseAsUnix(const char *line, const int linelen, t_directory::t_direntry &direntry)
  1323. {
  1324. int pos = 0;
  1325. int tokenlen = 0;
  1326. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1327. if (!str)
  1328. return FALSE;
  1329. //Check the first token
  1330. if (str[0] != 'b' &&
  1331. str[0] != 'c' &&
  1332. str[0] != 'd' &&
  1333. str[0] != 'l' &&
  1334. str[0] != 'p' &&
  1335. str[0] != 's' &&
  1336. str[0] != '-')
  1337. return FALSE;
  1338. //First check if it is a netware server
  1339. bool bNetWare = false;
  1340. copyStr(direntry.permissionstr, 0, str, tokenlen);
  1341. if (tokenlen == 1)
  1342. {
  1343. //Yes, it's most likely a netware server
  1344. //Now get the full permission string
  1345. bNetWare = true;
  1346. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1347. if (!str)
  1348. return FALSE;
  1349. direntry.permissionstr += _MPT(" ");
  1350. copyStr(direntry.permissionstr, direntry.permissionstr.GetLength(), str, tokenlen);
  1351. }
  1352. //Set directory and link flags
  1353. //Always assume links point directories
  1354. //GUI frontend should try to figure out
  1355. //to where the link really points
  1356. if (direntry.permissionstr[0]==_MPT('d') || direntry.permissionstr[0]==_MPT('l'))
  1357. direntry.dir = true;
  1358. else
  1359. direntry.dir = false;
  1360. if (direntry.permissionstr[0]==_MPT('l'))
  1361. direntry.bLink = true;
  1362. else
  1363. direntry.bLink = false;
  1364. bool bNetPresenz = false;
  1365. if (!bNetWare) //On non-netware servers, expect at least two unused tokens
  1366. {
  1367. bool groupid = false;
  1368. // Unused param
  1369. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1370. if (!str)
  1371. return FALSE;
  1372. if (tokenlen == 6 && !strncmp(str, "folder", tokenlen) && direntry.dir)
  1373. bNetPresenz = true; //Assume NetPresenz server
  1374. //However, it's possible that we mark a non-NetPresenz
  1375. //server if the fileowner is "folder"
  1376. else if (!IsNumeric(str, tokenlen))
  1377. {
  1378. // Check for Connect:Enterprise server
  1379. if (direntry.permissionstr.GetLength() > 3 &&
  1380. (direntry.permissionstr.Right(3) == _MPT("FTP") ||
  1381. direntry.permissionstr.Right(3) == _MPT("FTS") ||
  1382. direntry.permissionstr.Right(3) == _MPT("TCP") ||
  1383. direntry.permissionstr.Right(3) == _MPT("SSH")))
  1384. groupid = TRUE;
  1385. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), str, tokenlen);
  1386. }
  1387. else
  1388. groupid = TRUE;
  1389. if (!bNetPresenz && groupid)
  1390. {
  1391. //Unused param
  1392. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1393. if (!str)
  1394. return FALSE;
  1395. if (direntry.ownergroup != _T(""))
  1396. direntry.ownergroup += _T(" ");
  1397. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), str, tokenlen);
  1398. }
  1399. }
  1400. //Skip param, may be used for size
  1401. int skippedlen = 0;
  1402. const char *skipped = GetNextToken(line, linelen,skippedlen, pos, 0);
  1403. if (!skipped)
  1404. return FALSE;
  1405. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1406. if (!str)
  1407. return FALSE;
  1408. // Keep parsing the information until we get a numerical string,
  1409. // because some broken servers may send 3 tags: domain/network, group and user
  1410. const char *prevstr = 0;
  1411. int prevstrlen = 0;
  1412. __int64 tmp = 0;
  1413. std::map<CString, int>::const_iterator iter;
  1414. while (str && !ParseSize(str, tokenlen, tmp) && !IsNumeric(skipped, skippedlen))
  1415. {
  1416. //Maybe the server has left no space between the group and the size
  1417. //because of stupid alignment
  1418. char *tmpstr = new char[tokenlen + 1];
  1419. strncpy(tmpstr, str, tokenlen);
  1420. tmpstr[tokenlen] = 0;
  1421. strlwr(tmpstr);
  1422. USES_CONVERSION;
  1423. iter = m_MonthNamesMap.find(A2T(tmpstr));
  1424. delete [] tmpstr;
  1425. if (iter != m_MonthNamesMap.end())
  1426. {
  1427. BOOL bRightNumeric = true;
  1428. if (skipped[skippedlen-1]<'0' || skipped[skippedlen-1]>'9')
  1429. bRightNumeric = false;
  1430. if (bRightNumeric)
  1431. break;
  1432. bRightNumeric = true;
  1433. if (prevstr && prevstrlen)
  1434. {
  1435. if (prevstr[prevstrlen-1]<'0' || prevstr[prevstrlen-1]>'9')
  1436. bRightNumeric = false;
  1437. if (bRightNumeric)
  1438. {
  1439. if (direntry.ownergroup != _T(""))
  1440. direntry.ownergroup += _T(" ");
  1441. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), str, tokenlen);
  1442. skipped = prevstr;
  1443. skippedlen = prevstrlen;
  1444. break;
  1445. }
  1446. }
  1447. }
  1448. if (prevstr)
  1449. {
  1450. if (direntry.ownergroup != _T(""))
  1451. direntry.ownergroup += _T(" ");
  1452. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), prevstr, prevstrlen);
  1453. }
  1454. prevstr = str;
  1455. prevstrlen = tokenlen;
  1456. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1457. }
  1458. if (!str)
  1459. return FALSE;
  1460. const char *size = str;
  1461. int sizelen = tokenlen;
  1462. if (!ParseSize(str, tokenlen, direntry.size))
  1463. {
  1464. //Maybe we've skipped too much tokens
  1465. if (!ParseSize(skipped, skippedlen, direntry.size))
  1466. {
  1467. //Maybe the server has left no space between the group and the size
  1468. //because of stupid alignment
  1469. bool bRightNumeric = true;
  1470. const char *pos;
  1471. for (pos=(str+tokenlen-1); pos > str; pos--)
  1472. {
  1473. if (*pos<'0' || *pos>'9')
  1474. {
  1475. if (pos==(str+tokenlen-1))
  1476. bRightNumeric=false;
  1477. break;
  1478. }
  1479. }
  1480. if (bRightNumeric && pos>str)
  1481. {
  1482. size = pos + 1;
  1483. sizelen = pos - str;
  1484. direntry.ownergroup += _T(" ");
  1485. if (direntry.ownergroup != _T(""))
  1486. direntry.ownergroup += _T(" ");
  1487. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), str, pos-str);
  1488. }
  1489. else
  1490. {
  1491. for (pos=(skipped+skippedlen-1); pos > skipped; pos--)
  1492. {
  1493. if (*pos<'0' || *pos>'9')
  1494. {
  1495. if (pos==(skipped+skippedlen-1))
  1496. return false;
  1497. break;
  1498. }
  1499. }
  1500. size = pos + 1;
  1501. sizelen = skippedlen + skipped - size;
  1502. if (direntry.ownergroup != _T(""))
  1503. direntry.ownergroup += _T(" ");
  1504. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), skipped, skippedlen - sizelen);
  1505. }
  1506. direntry.size = strntoi64(size, sizelen);
  1507. }
  1508. else
  1509. {
  1510. //We should've not skipped the last token
  1511. //This also fixes the problem with the NetPresenz detection
  1512. if (bNetPresenz && direntry.dir && direntry.ownergroup != _MPT(""))
  1513. {
  1514. direntry.ownergroup = _MPT("folder ") + direntry.ownergroup;
  1515. }
  1516. }
  1517. }
  1518. else
  1519. {
  1520. if (direntry.ownergroup != _T(""))
  1521. direntry.ownergroup += _T(" ");
  1522. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), skipped, skippedlen);
  1523. if (prevstr)
  1524. {
  1525. direntry.ownergroup += _T(" ");
  1526. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), prevstr, prevstrlen);
  1527. }
  1528. str = 0;
  1529. }
  1530. //Month
  1531. if (!str)
  1532. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1533. if (!str)
  1534. return FALSE;
  1535. const char *smonth = str;
  1536. int smonthlen = tokenlen;
  1537. direntry.date.year = 0;
  1538. //Day
  1539. const char *sday = 0;
  1540. int sdaylen = 0;
  1541. // Some VShell server send both the year and the time, try to detect them
  1542. BOOL bCouldBeVShell = FALSE;
  1543. //Some servers use the following date formats:
  1544. // 26-09 2002, 2002-10-14, 01-jun-99
  1545. const char *p = strnchr(smonth, smonthlen, '-');
  1546. if (p)
  1547. {
  1548. int plen = smonthlen - (p - smonth);
  1549. const char *pos2 = strnchr(p+1, plen - 1, '-');
  1550. if (!pos2) //26-09 2002
  1551. {
  1552. sday = p + 1;
  1553. sdaylen = plen - 1;
  1554. smonthlen = p-smonth;
  1555. }
  1556. else if (p-smonth == 4) //2002-10-14
  1557. {
  1558. direntry.date.year = static_cast<int>(strntoi64(smonth, p-smonth));
  1559. sday = pos2 + 1;
  1560. sdaylen = smonthlen - (pos2 - smonth) - 1;
  1561. smonthlen = pos2-smonth - (p-smonth) - 1;
  1562. smonth = p + 1;
  1563. /* Try to detect difference between yyyy/dd/mm and yyyy/mm/dd
  1564. * Unfortunately we have to guess which one is the right if
  1565. * the month is < 12
  1566. */
  1567. if (strntoi64(smonth, smonthlen) > 12)
  1568. {
  1569. const char *tmp = smonth;
  1570. smonth = sday;
  1571. sday = tmp;
  1572. int tmplen = smonthlen;
  1573. smonthlen = sdaylen;
  1574. sdaylen = tmplen;
  1575. }
  1576. }
  1577. else if (p-smonth) //14-10-2002 or 01-jun-99
  1578. {
  1579. direntry.date.year = static_cast<int>(strntoi64(pos2+1, tokenlen - (pos2-smonth) - 1));
  1580. sday = smonth;
  1581. sdaylen = p - smonth;
  1582. smonthlen = pos2-smonth - (p-smonth) - 1;
  1583. smonth = p + 1;
  1584. /* Try to detect difference between yyyy/dd/mm and yyyy/mm/dd
  1585. * Unfortunately we have to guess which one is the right if
  1586. * the month is < 12
  1587. */
  1588. if (strntoi64(smonth, smonthlen) > 12)
  1589. {
  1590. const char *tmp = smonth;
  1591. smonth = sday;
  1592. sday = tmp;
  1593. int tmplen = smonthlen;
  1594. smonthlen = sdaylen;
  1595. sdaylen = tmplen;
  1596. }
  1597. }
  1598. else
  1599. return FALSE;
  1600. }
  1601. /* Some servers use the following date formats:
  1602. * yyyy/dd/mm, yyyy/mm/dd, dd/mm/yyyy, mm/dd/yyyy
  1603. * try to detect them.
  1604. */
  1605. else if (strnchr(smonth, smonthlen, '/'))
  1606. {
  1607. const char *p = strnchr(smonth, smonthlen, '/');
  1608. int plen = smonthlen - (p - smonth);
  1609. const char *pos2 = strnchr(p+1, plen - 1, '/');
  1610. if (!pos2) //Assume 26/09 2002
  1611. {
  1612. sday = p + 1;
  1613. sdaylen = plen - 1;
  1614. smonthlen = p-smonth;
  1615. }
  1616. else if (p-smonth==4)
  1617. {
  1618. direntry.date.year = static_cast<int>(strntoi64(smonth, p-smonth));
  1619. sday = pos2 + 1;
  1620. sdaylen = smonthlen - (pos2 - smonth) - 1;
  1621. smonthlen = pos2-smonth - (p-smonth) - 1;
  1622. smonth = p + 1;
  1623. /* Try to detect difference between yyyy/dd/mm and yyyy/mm/dd
  1624. * Unfortunately we have to guess which one is the right if
  1625. * the month is < 12
  1626. */
  1627. if (strntoi64(smonth, smonthlen) > 12)
  1628. {
  1629. const char *tmp = smonth;
  1630. smonth = sday;
  1631. sday = tmp;
  1632. int tmplen = smonthlen;
  1633. smonthlen = sdaylen;
  1634. sdaylen = tmplen;
  1635. }
  1636. }
  1637. else if (p-smonth==2)
  1638. {
  1639. direntry.date.year = static_cast<int>(strntoi64(pos2+1, tokenlen - (pos2-smonth) - 1));
  1640. sday = smonth;
  1641. sdaylen = p - smonth;
  1642. smonthlen = pos2-smonth - (p-smonth) - 1;
  1643. smonth = p + 1;
  1644. /* Try to detect difference between yyyy/dd/mm and yyyy/mm/dd
  1645. * Unfortunately we have to guess which one is the right if
  1646. * the month is < 12
  1647. */
  1648. if (strntoi64(smonth, smonthlen) > 12)
  1649. {
  1650. const char *tmp = smonth;
  1651. smonth = sday;
  1652. sday = tmp;
  1653. int tmplen = smonthlen;
  1654. smonthlen = sdaylen;
  1655. sdaylen = tmplen;
  1656. }
  1657. }
  1658. else
  1659. return FALSE;
  1660. }
  1661. else
  1662. {
  1663. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1664. if (!str)
  1665. return FALSE;
  1666. sday = str;
  1667. sdaylen = tokenlen;
  1668. if (sdaylen && sday[sdaylen-1] == ',')
  1669. {
  1670. sdaylen--;
  1671. bCouldBeVShell = TRUE;
  1672. }
  1673. //Trim trailing characters
  1674. while (sdaylen && (sday[sdaylen-1]=='.' || sday[sdaylen-1]==','))
  1675. {
  1676. bCouldBeVShell = FALSE;
  1677. sdaylen--;
  1678. }
  1679. int i;
  1680. for (i = 0; i < sdaylen; i++)
  1681. if (sday[i] < '0' || sday[i] > '9')
  1682. break;
  1683. if (i && i < sdaylen)
  1684. {
  1685. if ((unsigned char)sday[i] > 127)
  1686. sdaylen = i;
  1687. }
  1688. if (!sdaylen)
  1689. return FALSE;
  1690. }
  1691. if (!strntoi64(sday, sdaylen)) //Day field invalid
  1692. { //Maybe the server is sending a directory listing with localized date format.
  1693. //Try to fix this really bad behavior
  1694. bCouldBeVShell = FALSE;
  1695. const char *tmp = smonth;
  1696. smonth = sday;
  1697. sday = tmp;
  1698. int tmplen = smonthlen;
  1699. smonthlen = sdaylen;
  1700. sdaylen = tmplen;
  1701. }
  1702. //Time/Year
  1703. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1704. if (!str)
  1705. return FALSE;
  1706. //Trim trailing characters
  1707. while (smonthlen && (smonth[smonthlen-1]=='.' || smonth[smonthlen-1]==','))
  1708. smonthlen--;
  1709. if (!smonthlen)
  1710. return FALSE;
  1711. char *lwr = new char[smonthlen + 1];
  1712. memcpy(lwr, smonth, smonthlen);
  1713. lwr[smonthlen] = 0;
  1714. _strlwr(lwr);
  1715. bool gotYear = false;
  1716. int year = (int)strntoi64(smonth, smonthlen);
  1717. if (year > 1000)
  1718. {
  1719. gotYear = true;
  1720. direntry.date.year = year;
  1721. }
  1722. else
  1723. {
  1724. //Try if we can recognize the month name
  1725. USES_CONVERSION;
  1726. iter = m_MonthNamesMap.find(A2T(lwr));
  1727. delete [] lwr;
  1728. if (iter == m_MonthNamesMap.end())
  1729. {
  1730. int i;
  1731. for (i = 0; i < smonthlen; i++)
  1732. if (smonth[i] < '0' || smonth[i] > '9')
  1733. break;
  1734. if (!i || i == smonthlen)
  1735. return false;
  1736. if ((unsigned char)smonth[i] < 128)
  1737. return false;
  1738. smonthlen = i;
  1739. direntry.date.month = (int)strntoi64(smonth, smonthlen);
  1740. if (!direntry.date.month || direntry.date.month > 12)
  1741. return false;
  1742. }
  1743. else
  1744. direntry.date.month = iter->second;
  1745. }
  1746. if (!gotYear)
  1747. {
  1748. direntry.date.day = static_cast<int>(strntoi64(sday, sdaylen));
  1749. if (direntry.date.day < 1 || direntry.date.day > 31)
  1750. return false;
  1751. }
  1752. else
  1753. {
  1754. direntry.date.month = static_cast<int>(strntoi64(sday, sdaylen));
  1755. if (direntry.date.month < 1 || direntry.date.month > 12)
  1756. return false;
  1757. }
  1758. const char *stimeyear = str;
  1759. int stimeyearlen = tokenlen;
  1760. //Parse the time/year token
  1761. const char *strpos = strnchr(stimeyear, stimeyearlen, ':');
  1762. if (!strpos)
  1763. strpos = strnchr(stimeyear, stimeyearlen, '.');
  1764. if (!strpos)
  1765. strpos = strnchr(stimeyear, stimeyearlen, '-');
  1766. if (strpos)
  1767. {
  1768. //stimeyear has delimiter, so it's a time
  1769. direntry.date.hour = static_cast<int>(strntoi64(stimeyear, strpos - stimeyear));
  1770. direntry.date.minute = static_cast<int>(strntoi64(strpos + 1, stimeyearlen - (strpos - stimeyear) - 1));
  1771. direntry.date.hastime = TRUE;
  1772. //Problem: Some servers use times only for files newer than 6 months,
  1773. //others use one year as limit. So there is no support for files with time
  1774. //dated in the near future. Under normal conditions there should not be such files
  1775. if (!direntry.date.year)
  1776. {
  1777. CTime curtime = CTime::GetCurrentTime();
  1778. int curday = curtime.GetDay();
  1779. int curmonth = curtime.GetMonth();
  1780. int curyear = curtime.GetYear();
  1781. int now = curmonth*31+curday;
  1782. int file = direntry.date.month*31+direntry.date.day;
  1783. if ((now+1)>=file)
  1784. direntry.date.year = curyear;
  1785. else
  1786. direntry.date.year = curyear-1;
  1787. }
  1788. bCouldBeVShell = FALSE;
  1789. }
  1790. else
  1791. {
  1792. if (gotYear)
  1793. {
  1794. direntry.date.day = static_cast<int>(strntoi64(stimeyear, stimeyearlen));
  1795. if (direntry.date.day < 1 || direntry.date.day > 31)
  1796. return false;
  1797. }
  1798. else if (!direntry.date.year)
  1799. {
  1800. //No delimiters -> year
  1801. direntry.date.hastime = FALSE;
  1802. direntry.date.year = static_cast<int>(strntoi64(stimeyear, stimeyearlen));
  1803. }
  1804. else
  1805. {
  1806. // File has no time token and short date format
  1807. pos -= stimeyearlen;
  1808. }
  1809. }
  1810. if (!direntry.date.year) //Year 0? Really ancient file, this is invalid!
  1811. return FALSE;
  1812. // Check if server could still be one of the newer VShell servers
  1813. if (bCouldBeVShell)
  1814. {
  1815. int oldpos = pos;
  1816. //Get time
  1817. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1818. if (!str)
  1819. return FALSE;
  1820. const char *p = strnchr(str, tokenlen, ':');
  1821. if (pos && (p - str) == 2 && IsNumeric(str, 2) && IsNumeric(str + 3, 2))
  1822. {
  1823. //stimeyear has delimiter, so it's a time
  1824. int hour = static_cast<int>(strntoi64(str, 2));
  1825. int minute = static_cast<int>(strntoi64(str + 3, 2));
  1826. if (hour >= 0 && hour < 24 && minute >= 0 && minute < 60)
  1827. {
  1828. direntry.date.hour = hour;
  1829. direntry.date.minute = minute;
  1830. direntry.date.hastime = TRUE;
  1831. }
  1832. else
  1833. pos = oldpos;
  1834. }
  1835. else
  1836. pos = oldpos;
  1837. }
  1838. //Get filename
  1839. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  1840. if (!str)
  1841. return FALSE;
  1842. //Trim link data from filename
  1843. if (direntry.bLink)
  1844. {
  1845. const char *pos = strnstr(str, tokenlen, " -> ");
  1846. if (pos)
  1847. {
  1848. copyStr(direntry.linkTarget, 0, pos + 4, tokenlen - (pos - str) - 4);
  1849. tokenlen = pos - str;
  1850. }
  1851. if (!tokenlen)
  1852. return FALSE;
  1853. }
  1854. //Trim indicators, some server add those to mark special files
  1855. if (str[tokenlen - 1] == '*' ||
  1856. str[tokenlen - 1] == '/' ||
  1857. // str[tokenlen - 1] == '=' || //Don't trim this char, it would cause problems on certain servers
  1858. //This char just marks sockets, so it will never appear as indicator
  1859. //However it is valid as character for filenames on some systems
  1860. str[tokenlen - 1] == '|')
  1861. tokenlen--;
  1862. copyStr(direntry.name, 0, str, tokenlen, true);
  1863. direntry.bUnsure = FALSE;
  1864. direntry.date.hasdate = TRUE;
  1865. return TRUE;
  1866. }
  1867. BOOL CFtpListResult::parseAsDos(const char *line, const int linelen, t_directory::t_direntry &direntry)
  1868. {
  1869. int pos = 0;
  1870. int tokenlen = 0;
  1871. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1872. if (!str)
  1873. return FALSE;
  1874. //Check the first token
  1875. if (str[0]=='b' ||
  1876. str[0]=='c' ||
  1877. str[0]=='d' ||
  1878. str[0]=='l' ||
  1879. str[0]=='p' ||
  1880. str[0]=='s' ||
  1881. str[0]=='-')
  1882. return FALSE;
  1883. if (IsNumeric(str, tokenlen))
  1884. return FALSE;
  1885. //It's a NT server with MSDOS directory format
  1886. if (!ParseShortDate(str, tokenlen, direntry.date))
  1887. return FALSE;
  1888. //Extract time
  1889. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1890. if (!str)
  1891. return FALSE;
  1892. if (!parseTime(str, tokenlen, direntry.date))
  1893. return FALSE;
  1894. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1895. if (!str)
  1896. return FALSE;
  1897. if (tokenlen == 5 && !memcmp(str, "<DIR>", 5))
  1898. {
  1899. direntry.dir = TRUE;
  1900. }
  1901. else
  1902. {
  1903. char * buffer = new char[tokenlen];
  1904. int i, j;
  1905. for (i = 0, j = 0; i < tokenlen; i++)
  1906. {
  1907. if (str[i] != ',')
  1908. buffer[j++] = str[i];
  1909. }
  1910. direntry.dir = FALSE;
  1911. direntry.size = strntoi64(buffer, j);
  1912. delete [] buffer;
  1913. }
  1914. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  1915. if (!str)
  1916. return FALSE;
  1917. copyStr(direntry.name, 0, str, tokenlen, true);
  1918. direntry.bUnsure = FALSE;
  1919. return TRUE;
  1920. }
  1921. BOOL CFtpListResult::parseAsOther(const char *line, const int linelen, t_directory::t_direntry &direntry)
  1922. {
  1923. int pos = 0;
  1924. int tokenlen = 0;
  1925. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1926. if (!str)
  1927. return FALSE;
  1928. //Check the first token
  1929. if (str[0]=='b' ||
  1930. str[0]=='c' ||
  1931. str[0]=='d' ||
  1932. str[0]=='l' ||
  1933. str[0]=='p' ||
  1934. str[0]=='s' ||
  1935. str[0]=='-')
  1936. return FALSE;
  1937. if (!IsNumeric(str, tokenlen))
  1938. return FALSE;
  1939. //Could be numerical Unix style format or VShell format
  1940. //or even an OS2 server
  1941. const char *skipped = str;
  1942. int skippedtokenlen = tokenlen;
  1943. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1944. if (!str)
  1945. return FALSE;
  1946. //If next token is numerical, than it's the numerical Unix style format,
  1947. //else it's the VShell or OS/2 format
  1948. if (IsNumeric(str, tokenlen))
  1949. {
  1950. copyStr(direntry.permissionstr, 0, skipped, skippedtokenlen);
  1951. if (skippedtokenlen >= 2 && skipped[1] == '4')
  1952. direntry.dir = TRUE;
  1953. else
  1954. direntry.dir = FALSE;
  1955. //Unused token
  1956. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1957. if (!str)
  1958. return FALSE;
  1959. //Size
  1960. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1961. if (!str)
  1962. return FALSE;
  1963. direntry.size = strntoi64(str, tokenlen);
  1964. //Date/Time
  1965. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1966. if (!str)
  1967. return FALSE;
  1968. time_t secsSince1970 = static_cast<long>(strntoi64(str, tokenlen));
  1969. tm *sTime = gmtime(&secsSince1970);
  1970. direntry.date.year = sTime->tm_year + 1900;
  1971. direntry.date.month = sTime->tm_mon+1;
  1972. direntry.date.day = sTime->tm_mday;
  1973. direntry.date.hour = sTime->tm_hour;
  1974. direntry.date.minute = sTime->tm_min;
  1975. direntry.date.hasdate = direntry.date.hastime = TRUE;
  1976. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  1977. if (!str)
  1978. return FALSE;
  1979. copyStr(direntry.name, 0, str, tokenlen, true);
  1980. }
  1981. else
  1982. {
  1983. std::map<CString, int>::const_iterator iter;
  1984. //Get size
  1985. direntry.size = strntoi64(skipped, skippedtokenlen);
  1986. //Get date, month first
  1987. if (tokenlen >= 15)
  1988. return FALSE;
  1989. char buffer[15] = {0};
  1990. memcpy(buffer, str, tokenlen);
  1991. strlwr(buffer);
  1992. USES_CONVERSION;
  1993. iter = m_MonthNamesMap.find(A2T(buffer));
  1994. if (iter == m_MonthNamesMap.end())
  1995. {
  1996. direntry.dir = FALSE;
  1997. while (str)
  1998. {
  1999. //Could be an OS/2 server
  2000. if (tokenlen == 3 && !memcmp(str, "dir", 3))
  2001. direntry.dir = TRUE;
  2002. else if (tokenlen == 3 && !memcmp(str, "DIR", 3))
  2003. direntry.dir = TRUE;
  2004. else if (strnchr(str, tokenlen, '-'))
  2005. break;
  2006. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2007. }
  2008. if (!str)
  2009. return FALSE;
  2010. if (!ParseShortDate(str, tokenlen, direntry.date))
  2011. return FALSE;
  2012. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2013. if (!str)
  2014. return FALSE;
  2015. //Parse the time token
  2016. const char *strpos = strnchr(str, tokenlen, ':');
  2017. if (!strpos)
  2018. return FALSE;
  2019. if (strpos)
  2020. {
  2021. //stimeyear has delimiter, so it's a time
  2022. direntry.date.hour = static_cast<int>(strntoi64(str, strpos - str));
  2023. direntry.date.minute = static_cast<int>(strntoi64(strpos+1, tokenlen - (strpos - str) - 1));
  2024. direntry.date.hastime = TRUE;
  2025. //Problem: Some servers use times only for files newer than 6 months,
  2026. //others use one year as limit. So there is no support for files with time
  2027. //dated in the near future. Under normal conditions there should not be such files
  2028. if (!direntry.date.year)
  2029. {
  2030. CTime curtime = CTime::GetCurrentTime();
  2031. int curday = curtime.GetDay();
  2032. int curmonth = curtime.GetMonth();
  2033. int curyear = curtime.GetYear();
  2034. int now = curmonth*31+curday;
  2035. int file = direntry.date.month*31+direntry.date.day;
  2036. if ((now+1)>=file)
  2037. direntry.date.year = curyear;
  2038. else
  2039. direntry.date.year = curyear-1;
  2040. }
  2041. }
  2042. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  2043. if (!str)
  2044. return FALSE;
  2045. if (tokenlen > 6)
  2046. {
  2047. if (!strnicmp(str + tokenlen - 5, "<DIR>", 5))
  2048. {
  2049. direntry.dir = TRUE;
  2050. tokenlen -= 5;
  2051. while (tokenlen && str[tokenlen - 1] == ' ' || str[tokenlen - 1] == '\t')
  2052. tokenlen--;
  2053. }
  2054. if (!tokenlen)
  2055. return FALSE;
  2056. }
  2057. copyStr(direntry.name, 0, str, tokenlen, true);
  2058. }
  2059. else
  2060. {
  2061. direntry.date.month = iter->second;
  2062. //Day
  2063. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2064. if (!str)
  2065. return FALSE;
  2066. if (str[tokenlen-1]==',')
  2067. tokenlen--;
  2068. if (!tokenlen)
  2069. return FALSE;
  2070. if (!IsNumeric(str, tokenlen))
  2071. return FALSE;
  2072. direntry.date.day = static_cast<int>(strntoi64(str, tokenlen));
  2073. if (direntry.date.day < 1 || direntry.date.day > 31)
  2074. return FALSE;
  2075. //Year
  2076. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2077. if (!str)
  2078. return FALSE;
  2079. if (!IsNumeric(str, tokenlen))
  2080. return FALSE;
  2081. direntry.date.year = static_cast<int>(strntoi64(str, tokenlen));
  2082. if (direntry.date.year < 50)
  2083. direntry.date.year += 2000;
  2084. else if (direntry.date.year < 1000)
  2085. direntry.date.year += 1900;
  2086. direntry.date.hasdate = TRUE;
  2087. //Now get the time
  2088. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2089. const char *p = strnchr(str, tokenlen, ':');
  2090. if (!p)
  2091. return FALSE;
  2092. if (p==str || !IsNumeric(str, p-str) || (p-str + 1) >= tokenlen || !IsNumeric(p+1, tokenlen - (p-str) - 1))
  2093. return FALSE;
  2094. direntry.date.hour = static_cast<int>(strntoi64(str, p-str));
  2095. direntry.date.minute = static_cast<int>(strntoi64(p+1, tokenlen - (p-str) - 1));
  2096. if (direntry.date.hour < 0 || direntry.date.hour > 24)
  2097. return FALSE;
  2098. if (direntry.date.minute < 0 || direntry.date.minute > 59)
  2099. return FALSE;
  2100. direntry.date.hastime = TRUE;
  2101. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  2102. if (!str)
  2103. return FALSE;
  2104. if (tokenlen > 1 && (str[tokenlen-1] == '\\' || str[tokenlen-1] == '/'))
  2105. {
  2106. direntry.dir = TRUE;
  2107. tokenlen--;
  2108. }
  2109. else
  2110. direntry.dir = FALSE;
  2111. if (!tokenlen)
  2112. return FALSE;
  2113. copyStr(direntry.name, 0, str, tokenlen, true);
  2114. }
  2115. }
  2116. direntry.bUnsure = FALSE;
  2117. direntry.date.hasdate = TRUE;
  2118. return TRUE;
  2119. }
  2120. _int64 CFtpListResult::strntoi64(const char *str, int len) const
  2121. {
  2122. _int64 res = 0;
  2123. const char *p = str;
  2124. while ((p-str) < len)
  2125. {
  2126. if (*p < '0' || *p > '9')
  2127. break;
  2128. res *= 10;
  2129. res += *p++ - '0';
  2130. }
  2131. return res;
  2132. }
  2133. const char *CFtpListResult::GetNextToken(const char *line, const int linelen, int &len, int &pos, int type) const
  2134. {
  2135. const char *p = line + pos;
  2136. if ((p - line) >= linelen)
  2137. return NULL;
  2138. while ((p - line) < linelen && (!p || *p==' ' || *p=='\t'))
  2139. p++;
  2140. if ((p - line) >= linelen)
  2141. return NULL;
  2142. const char *res = p;
  2143. if (type)
  2144. {
  2145. pos = linelen;
  2146. len = linelen - (p - line);
  2147. }
  2148. else
  2149. {
  2150. while ((p - line) < linelen && *p && *p != ' ' && *p!='\t')
  2151. p++;
  2152. len = p - res;
  2153. pos = p - line;
  2154. }
  2155. return res;
  2156. }
  2157. const char * CFtpListResult::strnchr(const char *str, int len, char c) const
  2158. {
  2159. if (!str)
  2160. return NULL;
  2161. const char *p = str;
  2162. while (len > 0)
  2163. {
  2164. if (!*p)
  2165. return NULL;
  2166. if (*p == c)
  2167. return p;
  2168. p++;
  2169. len--;
  2170. }
  2171. return NULL;
  2172. }
  2173. const char * CFtpListResult::strnstr(const char *str, int len, const char *c) const
  2174. {
  2175. if (!str)
  2176. return NULL;
  2177. if (!c)
  2178. return NULL;
  2179. int clen = strlen(c);
  2180. const char *p = str;
  2181. while (len > 0)
  2182. {
  2183. if (!*p)
  2184. return NULL;
  2185. if (*p == *c)
  2186. {
  2187. if (clen == 1)
  2188. return p;
  2189. else if (len >= clen)
  2190. {
  2191. if (!memcmp(p + 1, c+1, clen-1))
  2192. return p;
  2193. }
  2194. else
  2195. return NULL;
  2196. }
  2197. p++;
  2198. len--;
  2199. }
  2200. return NULL;
  2201. }
  2202. void CFtpListResult::copyStr(CString &target, int pos, const char *source, int len, bool mayInvalidateUTF8 /*=false*/)
  2203. {
  2204. USES_CONVERSION;
  2205. char *p = new char[len + 1];
  2206. memcpy(p, source, len);
  2207. p[len] = '\0';
  2208. if (m_bUTF8 && *m_bUTF8)
  2209. {
  2210. // convert from UTF-8 to ANSI
  2211. if (!utf8_valid((const unsigned char*)p, len))
  2212. {
  2213. if (mayInvalidateUTF8 && m_server.nUTF8 != 1)
  2214. {
  2215. LogMessage(__FILE__, __LINE__, this, FZ_LOG_WARNING, _T("Server does not send proper UTF-8, falling back to local charset"));
  2216. *m_bUTF8 = false;
  2217. }
  2218. target = target.Left(pos) + A2CT(p);
  2219. }
  2220. else
  2221. {
  2222. // convert from UTF-8 to ANSI
  2223. int len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)p, -1, NULL, 0);
  2224. if (len != 0)
  2225. {
  2226. LPWSTR p1 = new WCHAR[len + 1];
  2227. MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)p, -1 , (LPWSTR)p1, len + 1);
  2228. target = target.Left(pos) + W2CT(p1);
  2229. delete [] p1;
  2230. }
  2231. else
  2232. target = target.Left(pos) + A2CT(p);
  2233. }
  2234. }
  2235. else
  2236. target = target.Left(pos) + A2CT(p);
  2237. delete [] p;
  2238. }
  2239. BOOL CFtpListResult::parseAsIBM(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2240. {
  2241. int pos = 0;
  2242. int tokenlen = 0;
  2243. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2244. if (!str)
  2245. return FALSE;
  2246. copyStr(direntry.ownergroup , 0, str, tokenlen);
  2247. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2248. if (!str)
  2249. return FALSE;
  2250. if (!IsNumeric(str, tokenlen))
  2251. return FALSE;
  2252. direntry.size = strntoi64(str, tokenlen);
  2253. //Date
  2254. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2255. if (!str)
  2256. return FALSE;
  2257. if (!ParseShortDate(str, tokenlen, direntry.date))
  2258. return FALSE;
  2259. //Time
  2260. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2261. if (!str)
  2262. return FALSE;
  2263. if (!parseTime(str, tokenlen, direntry.date))
  2264. return FALSE;
  2265. //Unused Token
  2266. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2267. if (!str)
  2268. return FALSE;
  2269. //Name
  2270. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  2271. if (!str)
  2272. return FALSE;
  2273. if (str[tokenlen-1] == '/')
  2274. {
  2275. direntry.dir = TRUE;
  2276. if (!--tokenlen)
  2277. return FALSE;
  2278. }
  2279. else
  2280. direntry.dir = FALSE;
  2281. copyStr(direntry.name, 0, str, tokenlen, true);
  2282. direntry.bUnsure = FALSE;
  2283. return true;
  2284. }
  2285. BOOL CFtpListResult::parseAsIBMMVS(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2286. {
  2287. int pos = 0;
  2288. int tokenlen = 0;
  2289. // volume
  2290. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2291. if (!str)
  2292. return FALSE;
  2293. // unit
  2294. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2295. if (!str)
  2296. return FALSE;
  2297. //Referred Date
  2298. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2299. if (!str)
  2300. return FALSE;
  2301. if (!ParseShortDate(str, tokenlen, direntry.date))
  2302. {
  2303. // Perhaps of the following type:
  2304. // TSO004 3390 VSAM FOO.BAR
  2305. if (tokenlen != 4 || strncmp(str, "VSAM", tokenlen))
  2306. return FALSE;
  2307. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  2308. if (!str)
  2309. return FALSE;
  2310. if (strnchr(str, tokenlen, ' '))
  2311. return FALSE;
  2312. copyStr(direntry.name, 0, str, tokenlen, true);
  2313. direntry.dir = false;
  2314. return true;
  2315. }
  2316. // ext
  2317. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2318. if (!str)
  2319. return FALSE;
  2320. if (!IsNumeric(str, tokenlen))
  2321. return FALSE;
  2322. int prevLen = tokenlen;
  2323. // used
  2324. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2325. if (!str)
  2326. return FALSE;
  2327. if (IsNumeric(str, tokenlen))
  2328. {
  2329. // recfm
  2330. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2331. if (!str)
  2332. return FALSE;
  2333. if (IsNumeric(str, tokenlen))
  2334. return false;
  2335. }
  2336. else
  2337. {
  2338. if (prevLen < 6)
  2339. return false;
  2340. }
  2341. // lrecl
  2342. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2343. if (!str)
  2344. return FALSE;
  2345. if (!IsNumeric(str, tokenlen))
  2346. return FALSE;
  2347. // blksize
  2348. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2349. if (!str)
  2350. return FALSE;
  2351. if (!IsNumeric(str, tokenlen))
  2352. return FALSE;
  2353. // dsorg
  2354. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2355. if (!str)
  2356. return FALSE;
  2357. if (tokenlen == 2 && !memcmp(str, "PO", 2))
  2358. {
  2359. direntry.dir = TRUE;
  2360. }
  2361. else
  2362. {
  2363. direntry.dir = FALSE;
  2364. direntry.size = 100;
  2365. }
  2366. // name of dataset or sequential name
  2367. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2368. if (!str)
  2369. return FALSE;
  2370. copyStr(direntry.name, 0, str, tokenlen, true);
  2371. direntry.bUnsure = FALSE;
  2372. return true;
  2373. }
  2374. BOOL CFtpListResult::parseAsIBMMVSPDS(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2375. {
  2376. int pos = 0;
  2377. int tokenlen = 0;
  2378. // pds member name
  2379. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2380. if (!str)
  2381. return FALSE;
  2382. copyStr(direntry.name, 0, str, tokenlen);
  2383. // vv.mm
  2384. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2385. if (!str)
  2386. return FALSE;
  2387. // creation date
  2388. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2389. if (!str)
  2390. return FALSE;
  2391. // change date
  2392. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2393. if (!str)
  2394. return FALSE;
  2395. if (!ParseShortDate(str, tokenlen, direntry.date))
  2396. return FALSE;
  2397. // change time
  2398. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2399. if (!str)
  2400. return FALSE;
  2401. if (!parseTime(str, tokenlen, direntry.date))
  2402. return FALSE;
  2403. // size
  2404. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2405. if (!str)
  2406. return FALSE;
  2407. if (!IsNumeric(str, tokenlen))
  2408. return FALSE;
  2409. direntry.size = strntoi64(str, tokenlen);
  2410. // init
  2411. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2412. if (!str)
  2413. return FALSE;
  2414. if (!IsNumeric(str, tokenlen))
  2415. return FALSE;
  2416. // mod
  2417. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2418. if (!str)
  2419. return FALSE;
  2420. if (!IsNumeric(str, tokenlen))
  2421. return FALSE;
  2422. // id
  2423. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2424. if (!str)
  2425. return FALSE;
  2426. direntry.dir = FALSE;
  2427. direntry.bUnsure = FALSE;
  2428. return true;
  2429. }
  2430. BOOL CFtpListResult::parseAsIBMMVSPDS2(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2431. {
  2432. // Use this one only on MVS servers, as it will cause problems on other servers
  2433. #ifndef LISTDEBUG
  2434. if (!(m_server.nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000)))
  2435. return false;
  2436. #endif
  2437. int pos = 0;
  2438. int tokenlen = 0;
  2439. direntry.bUnsure = FALSE;
  2440. direntry.dir = FALSE;
  2441. direntry.bLink = FALSE;
  2442. direntry.ownergroup = _MPT("");
  2443. direntry.permissionstr = _MPT("");
  2444. direntry.date.hasdate = direntry.date.hastime = FALSE;
  2445. // pds member name
  2446. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2447. if (!str)
  2448. return FALSE;
  2449. if (m_server.nServerType & FZ_SERVERTYPE_SUB_FTP_BS2000 &&
  2450. IsNumeric(str, tokenlen))
  2451. {
  2452. int prevlen = tokenlen;
  2453. const char* prev = str;
  2454. int oldpos = pos;
  2455. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2456. if (!str)
  2457. return FALSE;
  2458. if (str[0] != ':')
  2459. {
  2460. str = prev;
  2461. tokenlen = prevlen;
  2462. pos = oldpos;
  2463. }
  2464. }
  2465. copyStr(direntry.name, 0, str, tokenlen);
  2466. if (m_server.nServerType & FZ_SERVERTYPE_SUB_FTP_BS2000)
  2467. {
  2468. int pos = direntry.name.ReverseFind(_MPT(':'));
  2469. if (pos != -1)
  2470. direntry.name = direntry.name.Mid(pos + 1);
  2471. if (direntry.name[0] == _MPT('$'))
  2472. {
  2473. int pos = direntry.name.Find(_MPT('.'));
  2474. if (pos != -1)
  2475. direntry.name = direntry.name.Mid(pos + 1);
  2476. }
  2477. if (direntry.name == _T(""))
  2478. return FALSE;
  2479. }
  2480. // Hexadecimal filesize
  2481. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2482. if (!str)
  2483. // We can return true since sometimes MVS servers send pure filenames
  2484. return true;
  2485. direntry.size = 0;
  2486. const char *end = str + tokenlen;
  2487. const char *p = str;
  2488. while (p < end)
  2489. {
  2490. direntry.size *= 16;
  2491. if (*p >= '0' && *p <= '9')
  2492. direntry.size += *p - '0';
  2493. else if (*p >= 'A' && *p <= 'F')
  2494. direntry.size += *p - 'A' + 10;
  2495. else
  2496. return false;
  2497. p++;
  2498. }
  2499. // Unused hexadecimal token
  2500. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2501. if (!str)
  2502. return false;
  2503. end = str + tokenlen;
  2504. p = str;
  2505. while (p < end)
  2506. {
  2507. if ((*p < '0' || *p > '9') && (*p < 'A' || *p > 'F'))
  2508. return false;
  2509. p++;
  2510. }
  2511. // Unused token
  2512. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2513. if (!str)
  2514. return false;
  2515. if (!IsNumeric(str, tokenlen))
  2516. return false;
  2517. const char* prevprev = 0;
  2518. const char* prev = 0;
  2519. int prevprevlen = 0;
  2520. int prevlen = 0;
  2521. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2522. if (!str)
  2523. return false;
  2524. while (str)
  2525. {
  2526. if (prevprev)
  2527. {
  2528. for (int i = 0; i < prevprevlen; i++)
  2529. if (prevprev[i] < 'A' || prevprev[i] > 'Z')
  2530. return false;
  2531. }
  2532. prevprev = prev;
  2533. prevprevlen = prevlen;
  2534. prev = str;
  2535. prevlen = tokenlen;
  2536. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2537. }
  2538. if (!prev || !prevprev)
  2539. return false;
  2540. if (!IsNumeric(prev, prevlen) && (prevlen != 3 || memcmp(prev, "ANY", 3)))
  2541. return false;
  2542. if (!IsNumeric(prevprev, prevprevlen) && (prevprevlen != 3 || memcmp(prevprev, "ANY", 3)))
  2543. return false;
  2544. return true;
  2545. }
  2546. bool CFtpListResult::parseTime(const char *str, int len, t_directory::t_direntry::t_date &date) const
  2547. {
  2548. int i = 0;
  2549. //Extract the hour
  2550. date.hastime = TRUE;
  2551. date.hour = 0;
  2552. while (str[i] != ':')
  2553. {
  2554. if (str[i] < '0' || str[i] > '9')
  2555. return false;
  2556. date.hour *= 10;
  2557. date.hour += str[i] - '0';
  2558. if (date.hour > 24)
  2559. return false;
  2560. i++;
  2561. if (i == len)
  2562. return false;
  2563. }
  2564. // Make sure we did read at least one digit
  2565. if (!i)
  2566. return false;
  2567. i++;
  2568. if (i == len)
  2569. return false;
  2570. //Extract the minute
  2571. date.minute = 0;
  2572. while (str[i] >= '0' && str[i] <= '9')
  2573. {
  2574. date.minute *= 10;
  2575. date.minute += str[i] - '0';
  2576. if (date.minute > 59)
  2577. return false;
  2578. i++;
  2579. if (i == len)
  2580. break;
  2581. }
  2582. //Convert to 24h format
  2583. //I really wish we would have the following system:
  2584. //one year->ten months->ten days->ten hours->ten minutes->ten seconds and so on...
  2585. //I should modifiy the earth rotation to force everyone to use this system *g*
  2586. if (i != len)
  2587. {
  2588. if (str[i]=='P')
  2589. {
  2590. if (date.hour < 12)
  2591. date.hour += 12;
  2592. }
  2593. else
  2594. if (date.hour == 12)
  2595. date.hour = 0;
  2596. }
  2597. date.hastime = TRUE;
  2598. return true;
  2599. }
  2600. BOOL CFtpListResult::parseAsWfFtp(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2601. {
  2602. int pos = 0;
  2603. int tokenlen = 0;
  2604. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2605. if (!str)
  2606. return FALSE;
  2607. copyStr(direntry.name, 0, str, tokenlen);
  2608. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2609. if (!str)
  2610. return FALSE;
  2611. if (!IsNumeric(str, tokenlen))
  2612. return FALSE;
  2613. direntry.size = strntoi64(str, tokenlen);
  2614. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2615. if (!str)
  2616. return FALSE;
  2617. if (!ParseShortDate(str, tokenlen, direntry.date))
  2618. return FALSE;
  2619. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2620. if (!str)
  2621. return FALSE;
  2622. if (str[tokenlen - 1] != '.')
  2623. return FALSE;
  2624. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2625. if (!str)
  2626. return FALSE;
  2627. if (!parseTime(str, tokenlen, direntry.date))
  2628. return FALSE;
  2629. direntry.bUnsure = FALSE;
  2630. direntry.dir = false;
  2631. direntry.bLink = false;
  2632. direntry.permissionstr = _T("");
  2633. direntry.ownergroup = _T("");
  2634. return TRUE;
  2635. }
  2636. bool CFtpListResult::ParseSize(const char* str, int len, __int64 &size) const
  2637. {
  2638. if (len < 1)
  2639. return false;
  2640. if (IsNumeric(str, len))
  2641. {
  2642. size = strntoi64(str, len);
  2643. return true;
  2644. }
  2645. size = 0;
  2646. char last = str[--len];
  2647. int delimiter = -1;
  2648. for (int i = 0; i < len; i++)
  2649. {
  2650. char c = str[i];
  2651. if (c >= '0' && c <= '9')
  2652. {
  2653. size *= 10;
  2654. size += c - '0';
  2655. }
  2656. else if (c == '.')
  2657. delimiter = len - i;
  2658. else
  2659. return false;
  2660. }
  2661. // Check for digit before or after delimiterr
  2662. if (!delimiter || delimiter == len)
  2663. return false;
  2664. switch (last)
  2665. {
  2666. case 'k':
  2667. case 'K':
  2668. size *= 1 << 10;
  2669. break;
  2670. case 'm':
  2671. case 'M':
  2672. size *= 1 << 20;
  2673. break;
  2674. case 'g':
  2675. case 'G':
  2676. size *= 1 << 30;
  2677. break;
  2678. case 't':
  2679. case 'T':
  2680. size *= (__int64)1 << 40;
  2681. break;
  2682. default:
  2683. return false;
  2684. }
  2685. if (delimiter == -1)
  2686. return true;
  2687. while (delimiter--)
  2688. size /= 10;
  2689. return true;
  2690. }