FtpListResult.cpp 80 KB

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