FtpListResult.cpp 79 KB

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