FtpListResult.cpp 80 KB

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