FtpListResult.cpp 80 KB

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