FtpListResult.cpp 79 KB

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