FtpListResult.cpp 70 KB

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