FtpListResult.cpp 70 KB

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