FtpListResult.cpp 73 KB

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