FtpListResult.cpp 80 KB

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