FtpListResult.cpp 73 KB

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