FtpListResult.cpp 79 KB

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