FtpListResult.cpp 74 KB

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