FtpListResult.cpp 70 KB

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