FtpListResult.cpp 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846
  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. __int64 rawtime = strntoi64(fact+1, len-1);
  808. COleDateTime time((time_t)rawtime);
  809. dir.date.hasdate = TRUE;
  810. dir.date.hastime = TRUE;
  811. dir.date.year = time.GetYear();
  812. dir.date.month = time.GetMonth();
  813. dir.date.day = time.GetDay();
  814. dir.date.hour = time.GetHour();
  815. dir.date.minute = time.GetMinute();
  816. }
  817. else if (len == 5 && *fact=='u' && *(fact+1)=='p')
  818. {
  819. char buffer[4] = {0};
  820. memcpy(buffer, fact+2, len-2);
  821. direntry.permissionstr = buffer;
  822. }
  823. if (!nextfact || nextfact>=(str-2))
  824. break;
  825. fact = nextfact+1;
  826. nextfact = strstr(nextfact+1, ",");
  827. // if (nextfact && nextfact<str)
  828. // *nextfact=0;
  829. }
  830. direntry = dir;
  831. return TRUE;
  832. }
  833. return FALSE;
  834. }
  835. BOOL CFtpListResult::parseAsMlsd(const char *line, const int linelen, t_directory::t_direntry &direntry)
  836. {
  837. #ifdef _DEBUG
  838. USES_CONVERSION;
  839. #endif
  840. // MLSD format as described here: https://datatracker.ietf.org/doc/html/rfc3659
  841. // Parsing is done strict, abort on slightest error.
  842. // If we ever add some detection that entry is symlink,
  843. // make sure to add support for resolving the symlink
  844. // using MLST to TFTPFileSystem::ReadSymlink
  845. int pos = 0;
  846. int tokenlen = 0;
  847. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  848. if (!str)
  849. {
  850. return FALSE;
  851. }
  852. CString facts(str, tokenlen);
  853. if (facts.IsEmpty())
  854. {
  855. return FALSE;
  856. }
  857. direntry.name = L"";
  858. direntry.bUnsure = FALSE;
  859. direntry.dir = FALSE;
  860. direntry.bLink = FALSE;
  861. direntry.ownergroup = L"";
  862. direntry.owner = L"";
  863. direntry.group = L"";
  864. direntry.permissionstr = L"";
  865. CString owner, group, uid, gid, ownername, groupname;
  866. while (!facts.IsEmpty())
  867. {
  868. int delim = facts.Find(';');
  869. if (delim < 3)
  870. {
  871. if (delim != -1)
  872. {
  873. return 0;
  874. }
  875. else
  876. delim = facts.GetLength();
  877. }
  878. int pos = facts.Find('=');
  879. if (pos < 1 || pos > delim)
  880. {
  881. return FALSE;
  882. }
  883. CString factname = facts.Left(pos);
  884. factname.MakeLower();
  885. CString value = facts.Mid(pos + 1, delim - pos - 1);
  886. // When adding new facts, update filter in CFtpControlSocket::LogOnToServer
  887. // (CONNECT_FEAT state)
  888. if (factname == L"type")
  889. {
  890. if (!value.CompareNoCase(L"dir"))
  891. {
  892. direntry.dir = TRUE;
  893. }
  894. // This is syntax used by proftpd by default
  895. // http://www.proftpd.org/docs/modules/mod_facts.html
  896. // They claim it's the correct one.
  897. // See also
  898. // https://www.rfc-editor.org/errata_search.php?rfc=3659&eid=1500
  899. else if (!value.Left(15).CompareNoCase(L"OS.unix=symlink"))
  900. {
  901. direntry.dir = TRUE;
  902. direntry.bLink = TRUE;
  903. // actually symlink target should not be included in this syntax,
  904. // but just in case some servers do.
  905. if ((value.GetLength() > 16) && (value[15] == ':'))
  906. direntry.linkTarget = value.Mid(16);
  907. }
  908. // This is syntax shown in RFC 3659 section 7.7.4 "A More Complex Example"
  909. // Type=OS.unix=slink:/foobar;Perm=;Unique=keVO1+4G4; foobar
  910. // https://datatracker.ietf.org/doc/html/rfc3659
  911. else if (!value.Left(13).CompareNoCase(L"OS.unix=slink"))
  912. {
  913. direntry.dir = TRUE;
  914. direntry.bLink = TRUE;
  915. if ((value.GetLength() > 14) && (value[13] == ':'))
  916. direntry.linkTarget = value.Mid(14);
  917. }
  918. // For MLSD, these will be skipped in AddData.
  919. else if (!value.CompareNoCase(L"cdir"))
  920. {
  921. // ProFTPD up to 1.3.6rc1 and 1.3.5a incorrectly uses "cdir" for the current working directory.
  922. // So at least in MLST, where this would be the only entry, we treat it like "dir".
  923. if (!m_mlst)
  924. {
  925. direntry.name = L".";
  926. }
  927. direntry.dir = TRUE;
  928. }
  929. else if (!value.CompareNoCase(L"pdir"))
  930. {
  931. direntry.name = L"..";
  932. direntry.dir = TRUE;
  933. }
  934. }
  935. else if (factname == L"size")
  936. {
  937. direntry.size = 0;
  938. for (unsigned int i = 0; i < value.GetLength(); ++i)
  939. {
  940. if (value[i] < '0' || value[i] > '9')
  941. {
  942. return FALSE;
  943. }
  944. direntry.size *= 10;
  945. direntry.size += value[i] - '0';
  946. }
  947. }
  948. else if (factname == L"modify" ||
  949. (!direntry.date.hasdate && factname == L"create"))
  950. {
  951. if (!parseMlsdDateTime(value, direntry.date))
  952. {
  953. return FALSE;
  954. }
  955. }
  956. else if (factname == L"perm")
  957. {
  958. // there's no way we can convert Perm fact to unix-style permissions,
  959. // so we at least present Perm as-is to a user
  960. direntry.humanpermstr = value;
  961. }
  962. else if (factname == L"unix.mode")
  963. {
  964. direntry.permissionstr = value;
  965. }
  966. else if (factname == L"unix.owner" || factname == L"unix.user")
  967. {
  968. owner = value;
  969. }
  970. else if (factname == L"unix.group")
  971. {
  972. group = value;
  973. }
  974. else if (factname == L"unix.uid")
  975. {
  976. uid = value;
  977. }
  978. else if (factname == L"unix.gid")
  979. {
  980. gid = value;
  981. }
  982. else if (factname == L"unix.ownername")
  983. {
  984. ownername = value;
  985. }
  986. else if (factname == L"unix.groupname")
  987. {
  988. groupname = value;
  989. }
  990. facts = facts.Mid(delim + 1);
  991. }
  992. // The order of the facts is undefined
  993. if (!ownername.IsEmpty())
  994. direntry.owner = ownername;
  995. else if (!owner.IsEmpty())
  996. direntry.owner = owner;
  997. else if (!uid.IsEmpty())
  998. direntry.owner = uid;
  999. if (!groupname.IsEmpty())
  1000. direntry.group = groupname;
  1001. else if (!group.IsEmpty())
  1002. direntry.group = group;
  1003. else if (!gid.IsEmpty())
  1004. direntry.group = gid;
  1005. if (line[pos] != L' ')
  1006. {
  1007. return FALSE;
  1008. }
  1009. if (direntry.name.IsEmpty())
  1010. {
  1011. pos++;
  1012. CString fileName;
  1013. copyStr(fileName, 0, line + pos, linelen - pos, true);
  1014. if (m_mlst)
  1015. {
  1016. // do not try to detect path type, assume a standard *nix syntax + do not trim
  1017. CServerPath path(fileName, FZ_SERVERTYPE_FTP, false);
  1018. direntry.name = path.GetLastSegment();
  1019. if (direntry.name.IsEmpty())
  1020. {
  1021. direntry.name = fileName;
  1022. }
  1023. }
  1024. else
  1025. {
  1026. direntry.name = fileName;
  1027. }
  1028. }
  1029. return TRUE;
  1030. }
  1031. bool CFtpListResult::parseMlsdDateTime(const CString value, t_directory::t_direntry::t_date &date) const
  1032. {
  1033. if (value.IsEmpty())
  1034. {
  1035. return FALSE;
  1036. }
  1037. bool result = FALSE;
  1038. int Year, Month, Day, Hours, Minutes, Seconds;
  1039. Year=Month=Day=Hours=Minutes=Seconds=0;
  1040. // Time can include a fraction after a dot, this will ignore the fraction part.
  1041. if (swscanf((LPCWSTR)value, L"%4d%2d%2d%2d%2d%2d", &Year, &Month, &Day, &Hours, &Minutes, &Seconds) == 6)
  1042. {
  1043. date.hasdate = TRUE;
  1044. date.hastime = TRUE;
  1045. date.hasseconds = TRUE;
  1046. result = TRUE;
  1047. }
  1048. else if (swscanf((LPCWSTR)value, L"%4d%2d%2d", &Year, &Month, &Day) == 3)
  1049. {
  1050. date.hasdate = TRUE;
  1051. date.hastime = FALSE;
  1052. result = TRUE;
  1053. }
  1054. if (result)
  1055. {
  1056. date.year = Year;
  1057. date.month = Month;
  1058. date.day = Day;
  1059. date.hour = Hours;
  1060. date.minute = Minutes;
  1061. date.second = Seconds;
  1062. date.utc = TRUE;
  1063. }
  1064. return result;
  1065. }
  1066. BOOL CFtpListResult::parseAsUnix(const char *line, const int linelen, t_directory::t_direntry &direntry)
  1067. {
  1068. int pos = 0;
  1069. int tokenlen = 0;
  1070. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1071. if (!str)
  1072. {
  1073. return FALSE;
  1074. }
  1075. //Check the first token
  1076. if (str[0] != 'b' &&
  1077. str[0] != 'c' &&
  1078. str[0] != 'd' &&
  1079. str[0] != 'l' &&
  1080. str[0] != 'p' &&
  1081. str[0] != 's' &&
  1082. str[0] != '-')
  1083. {
  1084. return FALSE;
  1085. }
  1086. //First check if it is a netware server
  1087. bool bNetWare = false;
  1088. copyStr(direntry.permissionstr, 0, str, tokenlen);
  1089. if (tokenlen == 1)
  1090. {
  1091. //Yes, it's most likely a netware server
  1092. //Now get the full permission string
  1093. bNetWare = true;
  1094. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1095. if (!str)
  1096. {
  1097. return FALSE;
  1098. }
  1099. direntry.permissionstr += L" ";
  1100. copyStr(direntry.permissionstr, direntry.permissionstr.GetLength(), str, tokenlen);
  1101. }
  1102. //Set directory and link flags
  1103. //Always assume links point directories
  1104. //GUI frontend should try to figure out
  1105. //to where the link really points
  1106. if (direntry.permissionstr[0]==L'd' || direntry.permissionstr[0]==L'l')
  1107. direntry.dir = true;
  1108. else
  1109. direntry.dir = false;
  1110. if (direntry.permissionstr[0]==L'l')
  1111. direntry.bLink = true;
  1112. else
  1113. direntry.bLink = false;
  1114. bool bNetPresenz = false;
  1115. if (!bNetWare) //On non-netware servers, expect at least two unused tokens
  1116. {
  1117. bool groupid = false;
  1118. // Unused param
  1119. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1120. if (!str)
  1121. {
  1122. return FALSE;
  1123. }
  1124. if (tokenlen == 6 && !strncmp(str, "folder", tokenlen) && direntry.dir)
  1125. bNetPresenz = true; //Assume NetPresenz server
  1126. //However, it's possible that we mark a non-NetPresenz
  1127. //server if the fileowner is "folder"
  1128. else if (!IsNumeric(str, tokenlen))
  1129. {
  1130. // Check for Connect:Enterprise server
  1131. if (direntry.permissionstr.GetLength() > 3 &&
  1132. (direntry.permissionstr.Right(3) == L"FTP" ||
  1133. direntry.permissionstr.Right(3) == L"FTS" ||
  1134. direntry.permissionstr.Right(3) == L"TCP" ||
  1135. direntry.permissionstr.Right(3) == L"SSH"))
  1136. groupid = TRUE;
  1137. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), str, tokenlen);
  1138. }
  1139. else
  1140. groupid = TRUE;
  1141. if (!bNetPresenz && groupid)
  1142. {
  1143. //Unused param
  1144. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1145. if (!str)
  1146. {
  1147. return FALSE;
  1148. }
  1149. if (direntry.ownergroup != L"")
  1150. direntry.ownergroup += L" ";
  1151. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), str, tokenlen);
  1152. }
  1153. }
  1154. //Skip param, may be used for size
  1155. int skippedlen = 0;
  1156. const char *skipped = GetNextToken(line, linelen,skippedlen, pos, 0);
  1157. if (!skipped)
  1158. {
  1159. return FALSE;
  1160. }
  1161. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1162. if (!str)
  1163. {
  1164. return FALSE;
  1165. }
  1166. // Keep parsing the information until we get a numerical string,
  1167. // because some broken servers may send 3 tags: domain/network, group and user
  1168. const char *prevstr = 0;
  1169. int prevstrlen = 0;
  1170. __int64 tmp = 0;
  1171. std::map<CString, int>::const_iterator iter;
  1172. while (str && !ParseSize(str, tokenlen, tmp) && !IsNumeric(skipped, skippedlen))
  1173. {
  1174. //Maybe the server has left no space between the group and the size
  1175. //because of stupid alignment
  1176. char *tmpstr = new char[tokenlen + 1];
  1177. strncpy(tmpstr, str, tokenlen);
  1178. tmpstr[tokenlen] = 0;
  1179. strlwr(tmpstr);
  1180. USES_CONVERSION;
  1181. iter = m_MonthNamesMap.find(A2T(tmpstr));
  1182. delete [] tmpstr;
  1183. if (iter != m_MonthNamesMap.end())
  1184. {
  1185. BOOL bRightNumeric = true;
  1186. if (skipped[skippedlen-1]<'0' || skipped[skippedlen-1]>'9')
  1187. bRightNumeric = false;
  1188. if (bRightNumeric)
  1189. break;
  1190. bRightNumeric = true;
  1191. if (prevstr && prevstrlen)
  1192. {
  1193. if (prevstr[prevstrlen-1]<'0' || prevstr[prevstrlen-1]>'9')
  1194. bRightNumeric = false;
  1195. if (bRightNumeric)
  1196. {
  1197. if (direntry.ownergroup != L"")
  1198. direntry.ownergroup += L" ";
  1199. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), str, tokenlen);
  1200. skipped = prevstr;
  1201. skippedlen = prevstrlen;
  1202. break;
  1203. }
  1204. }
  1205. }
  1206. if (prevstr)
  1207. {
  1208. if (direntry.ownergroup != L"")
  1209. direntry.ownergroup += L" ";
  1210. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), prevstr, prevstrlen);
  1211. }
  1212. prevstr = str;
  1213. prevstrlen = tokenlen;
  1214. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1215. }
  1216. if (!str)
  1217. {
  1218. return FALSE;
  1219. }
  1220. const char *size = str;
  1221. int sizelen = tokenlen;
  1222. if (!ParseSize(str, tokenlen, direntry.size))
  1223. {
  1224. //Maybe we've skipped too much tokens
  1225. if (!ParseSize(skipped, skippedlen, direntry.size))
  1226. {
  1227. //Maybe the server has left no space between the group and the size
  1228. //because of stupid alignment
  1229. bool bRightNumeric = true;
  1230. const char *pos;
  1231. for (pos=(str+tokenlen-1); pos > str; pos--)
  1232. {
  1233. if (*pos<'0' || *pos>'9')
  1234. {
  1235. if (pos==(str+tokenlen-1))
  1236. bRightNumeric=false;
  1237. break;
  1238. }
  1239. }
  1240. if (bRightNumeric && pos>str)
  1241. {
  1242. size = pos + 1;
  1243. sizelen = pos - str;
  1244. direntry.ownergroup += L" ";
  1245. if (direntry.ownergroup != L"")
  1246. direntry.ownergroup += L" ";
  1247. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), str, pos-str);
  1248. }
  1249. else
  1250. {
  1251. for (pos=(skipped+skippedlen-1); pos > skipped; pos--)
  1252. {
  1253. if (*pos<'0' || *pos>'9')
  1254. {
  1255. if (pos==(skipped+skippedlen-1))
  1256. {
  1257. return false;
  1258. }
  1259. break;
  1260. }
  1261. }
  1262. size = pos + 1;
  1263. sizelen = skippedlen + skipped - size;
  1264. if (direntry.ownergroup != L"")
  1265. direntry.ownergroup += L" ";
  1266. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), skipped, skippedlen - sizelen);
  1267. }
  1268. direntry.size = strntoi64(size, sizelen);
  1269. }
  1270. else
  1271. {
  1272. //We should've not skipped the last token
  1273. //This also fixes the problem with the NetPresenz detection
  1274. if (bNetPresenz && direntry.dir && direntry.ownergroup != L"")
  1275. {
  1276. direntry.ownergroup = L"folder " + direntry.ownergroup;
  1277. }
  1278. }
  1279. }
  1280. else
  1281. {
  1282. if (direntry.ownergroup != L"")
  1283. direntry.ownergroup += L" ";
  1284. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), skipped, skippedlen);
  1285. if (prevstr)
  1286. {
  1287. direntry.ownergroup += L" ";
  1288. copyStr(direntry.ownergroup, direntry.ownergroup.GetLength(), prevstr, prevstrlen);
  1289. }
  1290. str = 0;
  1291. }
  1292. //Month
  1293. if (!str)
  1294. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1295. if (!str)
  1296. {
  1297. return FALSE;
  1298. }
  1299. const char *smonth = str;
  1300. int smonthlen = tokenlen;
  1301. direntry.date.year = 0;
  1302. //Day
  1303. const char *sday = 0;
  1304. int sdaylen = 0;
  1305. // Some VShell server send both the year and the time, try to detect them
  1306. BOOL bCouldBeVShell = FALSE;
  1307. //Some servers use the following date formats:
  1308. // 26-09 2002, 2002-10-14, 01-jun-99
  1309. const char *p = strnchr(smonth, smonthlen, '-');
  1310. if (p)
  1311. {
  1312. int plen = smonthlen - (p - smonth);
  1313. const char *pos2 = strnchr(p+1, plen - 1, '-');
  1314. if (!pos2) //26-09 2002
  1315. {
  1316. sday = p + 1;
  1317. sdaylen = plen - 1;
  1318. smonthlen = p-smonth;
  1319. }
  1320. else if (p-smonth == 4) //2002-10-14
  1321. {
  1322. direntry.date.year = static_cast<int>(strntoi64(smonth, p-smonth));
  1323. sday = pos2 + 1;
  1324. sdaylen = smonthlen - (pos2 - smonth) - 1;
  1325. smonthlen = pos2-smonth - (p-smonth) - 1;
  1326. smonth = p + 1;
  1327. /* Try to detect difference between yyyy/dd/mm and yyyy/mm/dd
  1328. * Unfortunately we have to guess which one is the right if
  1329. * the month is < 12
  1330. */
  1331. if (strntoi64(smonth, smonthlen) > 12)
  1332. {
  1333. const char *tmp = smonth;
  1334. smonth = sday;
  1335. sday = tmp;
  1336. int tmplen = smonthlen;
  1337. smonthlen = sdaylen;
  1338. sdaylen = tmplen;
  1339. }
  1340. }
  1341. else if (p-smonth) //14-10-2002 or 01-jun-99
  1342. {
  1343. direntry.date.year = static_cast<int>(strntoi64(pos2+1, tokenlen - (pos2-smonth) - 1));
  1344. sday = smonth;
  1345. sdaylen = p - smonth;
  1346. smonthlen = pos2-smonth - (p-smonth) - 1;
  1347. smonth = p + 1;
  1348. /* Try to detect difference between yyyy/dd/mm and yyyy/mm/dd
  1349. * Unfortunately we have to guess which one is the right if
  1350. * the month is < 12
  1351. */
  1352. if (strntoi64(smonth, smonthlen) > 12)
  1353. {
  1354. const char *tmp = smonth;
  1355. smonth = sday;
  1356. sday = tmp;
  1357. int tmplen = smonthlen;
  1358. smonthlen = sdaylen;
  1359. sdaylen = tmplen;
  1360. }
  1361. }
  1362. else
  1363. {
  1364. return FALSE;
  1365. }
  1366. }
  1367. /* Some servers use the following date formats:
  1368. * yyyy/dd/mm, yyyy/mm/dd, dd/mm/yyyy, mm/dd/yyyy
  1369. * try to detect them.
  1370. */
  1371. else if (strnchr(smonth, smonthlen, '/'))
  1372. {
  1373. const char *p = strnchr(smonth, smonthlen, '/');
  1374. int plen = smonthlen - (p - smonth);
  1375. const char *pos2 = strnchr(p+1, plen - 1, '/');
  1376. if (!pos2) //Assume 26/09 2002
  1377. {
  1378. sday = p + 1;
  1379. sdaylen = plen - 1;
  1380. smonthlen = p-smonth;
  1381. }
  1382. else if (p-smonth==4)
  1383. {
  1384. direntry.date.year = static_cast<int>(strntoi64(smonth, p-smonth));
  1385. sday = pos2 + 1;
  1386. sdaylen = smonthlen - (pos2 - smonth) - 1;
  1387. smonthlen = pos2-smonth - (p-smonth) - 1;
  1388. smonth = p + 1;
  1389. /* Try to detect difference between yyyy/dd/mm and yyyy/mm/dd
  1390. * Unfortunately we have to guess which one is the right if
  1391. * the month is < 12
  1392. */
  1393. if (strntoi64(smonth, smonthlen) > 12)
  1394. {
  1395. const char *tmp = smonth;
  1396. smonth = sday;
  1397. sday = tmp;
  1398. int tmplen = smonthlen;
  1399. smonthlen = sdaylen;
  1400. sdaylen = tmplen;
  1401. }
  1402. }
  1403. else if (p-smonth==2)
  1404. {
  1405. direntry.date.year = static_cast<int>(strntoi64(pos2+1, tokenlen - (pos2-smonth) - 1));
  1406. sday = smonth;
  1407. sdaylen = p - smonth;
  1408. smonthlen = pos2-smonth - (p-smonth) - 1;
  1409. smonth = p + 1;
  1410. /* Try to detect difference between yyyy/dd/mm and yyyy/mm/dd
  1411. * Unfortunately we have to guess which one is the right if
  1412. * the month is < 12
  1413. */
  1414. if (strntoi64(smonth, smonthlen) > 12)
  1415. {
  1416. const char *tmp = smonth;
  1417. smonth = sday;
  1418. sday = tmp;
  1419. int tmplen = smonthlen;
  1420. smonthlen = sdaylen;
  1421. sdaylen = tmplen;
  1422. }
  1423. }
  1424. else
  1425. {
  1426. return FALSE;
  1427. }
  1428. }
  1429. else
  1430. {
  1431. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1432. if (!str)
  1433. {
  1434. return FALSE;
  1435. }
  1436. sday = str;
  1437. sdaylen = tokenlen;
  1438. if (sdaylen && sday[sdaylen-1] == ',')
  1439. {
  1440. sdaylen--;
  1441. bCouldBeVShell = TRUE;
  1442. }
  1443. //Trim trailing characters
  1444. while (sdaylen && (sday[sdaylen-1]=='.' || sday[sdaylen-1]==','))
  1445. {
  1446. bCouldBeVShell = FALSE;
  1447. sdaylen--;
  1448. }
  1449. int i;
  1450. for (i = 0; i < sdaylen; i++)
  1451. if (sday[i] < '0' || sday[i] > '9')
  1452. break;
  1453. if (i && i < sdaylen)
  1454. {
  1455. if ((unsigned char)sday[i] > 127)
  1456. sdaylen = i;
  1457. }
  1458. if (!sdaylen)
  1459. {
  1460. return FALSE;
  1461. }
  1462. }
  1463. if (!strntoi64(sday, sdaylen)) //Day field invalid
  1464. { //Maybe the server is sending a directory listing with localized date format.
  1465. //Try to fix this really bad behavior
  1466. bCouldBeVShell = FALSE;
  1467. const char *tmp = smonth;
  1468. smonth = sday;
  1469. sday = tmp;
  1470. int tmplen = smonthlen;
  1471. smonthlen = sdaylen;
  1472. sdaylen = tmplen;
  1473. }
  1474. //Time/Year
  1475. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1476. if (!str)
  1477. {
  1478. return FALSE;
  1479. }
  1480. //Trim trailing characters
  1481. while (smonthlen && (smonth[smonthlen-1]=='.' || smonth[smonthlen-1]==','))
  1482. smonthlen--;
  1483. if (!smonthlen)
  1484. {
  1485. return FALSE;
  1486. }
  1487. char *lwr = new char[smonthlen + 1];
  1488. memcpy(lwr, smonth, smonthlen);
  1489. lwr[smonthlen] = 0;
  1490. _strlwr(lwr);
  1491. bool gotYear = false;
  1492. int year = (int)strntoi64(smonth, smonthlen);
  1493. if (year > 1000)
  1494. {
  1495. gotYear = true;
  1496. direntry.date.year = year;
  1497. }
  1498. else
  1499. {
  1500. //Try if we can recognize the month name
  1501. USES_CONVERSION;
  1502. iter = m_MonthNamesMap.find(A2T(lwr));
  1503. delete [] lwr;
  1504. if (iter == m_MonthNamesMap.end())
  1505. {
  1506. int i;
  1507. for (i = 0; i < smonthlen; i++)
  1508. if (smonth[i] < '0' || smonth[i] > '9')
  1509. break;
  1510. if (!i || i == smonthlen)
  1511. {
  1512. return false;
  1513. }
  1514. if ((unsigned char)smonth[i] < 128)
  1515. {
  1516. return false;
  1517. }
  1518. smonthlen = i;
  1519. direntry.date.month = (int)strntoi64(smonth, smonthlen);
  1520. if (!direntry.date.month || direntry.date.month > 12)
  1521. {
  1522. return false;
  1523. }
  1524. }
  1525. else
  1526. direntry.date.month = iter->second;
  1527. }
  1528. if (!gotYear)
  1529. {
  1530. direntry.date.day = static_cast<int>(strntoi64(sday, sdaylen));
  1531. if (direntry.date.day < 1 || direntry.date.day > 31)
  1532. {
  1533. return false;
  1534. }
  1535. }
  1536. else
  1537. {
  1538. direntry.date.month = static_cast<int>(strntoi64(sday, sdaylen));
  1539. if (direntry.date.month < 1 || direntry.date.month > 12)
  1540. {
  1541. return false;
  1542. }
  1543. }
  1544. const char *stimeyear = str;
  1545. int stimeyearlen = tokenlen;
  1546. //Parse the time/year token
  1547. const char *strpos = strnchr(stimeyear, stimeyearlen, ':');
  1548. if (!strpos)
  1549. strpos = strnchr(stimeyear, stimeyearlen, '.');
  1550. if (!strpos)
  1551. strpos = strnchr(stimeyear, stimeyearlen, '-');
  1552. if (strpos)
  1553. {
  1554. //stimeyear has delimiter, so it's a time
  1555. direntry.date.hour = static_cast<int>(strntoi64(stimeyear, strpos - stimeyear));
  1556. int stimeyearrem = stimeyearlen - (strpos - stimeyear) - 1;
  1557. const char *strpos2 = strnchr(strpos + 1, stimeyearrem, ':');
  1558. if (strpos2 == NULL)
  1559. {
  1560. direntry.date.minute = static_cast<int>(strntoi64(strpos + 1, stimeyearrem));
  1561. }
  1562. else
  1563. {
  1564. direntry.date.minute = static_cast<int>(strntoi64(strpos + 1, strpos2 - strpos - 1));
  1565. direntry.date.second = static_cast<int>(strntoi64(strpos2 + 1, stimeyearlen - (strpos2 - stimeyear) - 1));
  1566. direntry.date.hasseconds = TRUE;
  1567. }
  1568. direntry.date.hastime = TRUE;
  1569. //Problem: Some servers use times only for files newer than 6 months,
  1570. //others use one year as limit. IIS shows time for files from the current year (jan-dec).
  1571. //So there is no support for files with time
  1572. //dated in the near future. Under normal conditions there should not be such files.
  1573. if (!direntry.date.year)
  1574. {
  1575. CTime curtime = CTime::GetCurrentTime();
  1576. int curday = curtime.GetDay();
  1577. int curmonth = curtime.GetMonth();
  1578. int curyear = curtime.GetYear();
  1579. int now = curmonth*31+curday;
  1580. int file = direntry.date.month*31+direntry.date.day;
  1581. if ((now+1)>=file)
  1582. direntry.date.year = curyear;
  1583. else
  1584. direntry.date.year = curyear-1;
  1585. }
  1586. bCouldBeVShell = FALSE;
  1587. }
  1588. else
  1589. {
  1590. if (gotYear)
  1591. {
  1592. direntry.date.day = static_cast<int>(strntoi64(stimeyear, stimeyearlen));
  1593. if (direntry.date.day < 1 || direntry.date.day > 31)
  1594. {
  1595. return false;
  1596. }
  1597. }
  1598. else if (!direntry.date.year)
  1599. {
  1600. //No delimiters -> year
  1601. direntry.date.hastime = FALSE;
  1602. direntry.date.year = static_cast<int>(strntoi64(stimeyear, stimeyearlen));
  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!
  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. BOOL CFtpListResult::parseAsOther(const char *line, const int linelen, t_directory::t_direntry &direntry)
  1730. {
  1731. int pos = 0;
  1732. int tokenlen = 0;
  1733. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1734. if (!str)
  1735. return FALSE;
  1736. //Check the first token
  1737. if (str[0]=='b' ||
  1738. str[0]=='c' ||
  1739. str[0]=='d' ||
  1740. str[0]=='l' ||
  1741. str[0]=='p' ||
  1742. str[0]=='s' ||
  1743. str[0]=='-')
  1744. return FALSE;
  1745. if (!IsNumeric(str, tokenlen))
  1746. return FALSE;
  1747. //Could be numerical Unix style format or VShell format
  1748. //or even an OS2 server
  1749. const char *skipped = str;
  1750. int skippedtokenlen = tokenlen;
  1751. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1752. if (!str)
  1753. return FALSE;
  1754. //If next token is numerical, than it's the numerical Unix style format,
  1755. //else it's the VShell or OS/2 format
  1756. if (IsNumeric(str, tokenlen))
  1757. {
  1758. copyStr(direntry.permissionstr, 0, skipped, skippedtokenlen);
  1759. if (skippedtokenlen >= 2 && skipped[1] == '4')
  1760. direntry.dir = TRUE;
  1761. else
  1762. direntry.dir = FALSE;
  1763. //Unused token
  1764. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1765. if (!str)
  1766. return FALSE;
  1767. //Size
  1768. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1769. if (!str)
  1770. return FALSE;
  1771. direntry.size = strntoi64(str, tokenlen);
  1772. //Date/Time
  1773. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1774. if (!str)
  1775. return FALSE;
  1776. time_t secsSince1970 = static_cast<long>(strntoi64(str, tokenlen));
  1777. tm *sTime = gmtime(&secsSince1970);
  1778. direntry.date.year = sTime->tm_year + 1900;
  1779. direntry.date.month = sTime->tm_mon+1;
  1780. direntry.date.day = sTime->tm_mday;
  1781. direntry.date.hour = sTime->tm_hour;
  1782. direntry.date.minute = sTime->tm_min;
  1783. direntry.date.hasdate = direntry.date.hastime = TRUE;
  1784. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  1785. if (!str)
  1786. return FALSE;
  1787. copyStr(direntry.name, 0, str, tokenlen, true);
  1788. }
  1789. else
  1790. {
  1791. std::map<CString, int>::const_iterator iter;
  1792. //Get size
  1793. direntry.size = strntoi64(skipped, skippedtokenlen);
  1794. //Get date, month first
  1795. if (tokenlen >= 15)
  1796. return FALSE;
  1797. char buffer[15] = {0};
  1798. memcpy(buffer, str, tokenlen);
  1799. strlwr(buffer);
  1800. USES_CONVERSION;
  1801. iter = m_MonthNamesMap.find(A2T(buffer));
  1802. if (iter == m_MonthNamesMap.end())
  1803. {
  1804. direntry.dir = FALSE;
  1805. while (str)
  1806. {
  1807. //Could be an OS/2 server
  1808. if (tokenlen == 3 && !memcmp(str, "dir", 3))
  1809. direntry.dir = TRUE;
  1810. else if (tokenlen == 3 && !memcmp(str, "DIR", 3))
  1811. direntry.dir = TRUE;
  1812. else if (strnchr(str, tokenlen, '-'))
  1813. break;
  1814. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1815. }
  1816. if (!str)
  1817. return FALSE;
  1818. if (!ParseShortDate(str, tokenlen, direntry.date))
  1819. return FALSE;
  1820. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1821. if (!str)
  1822. return FALSE;
  1823. //Parse the time token
  1824. const char *strpos = strnchr(str, tokenlen, ':');
  1825. if (!strpos)
  1826. return FALSE;
  1827. if (strpos)
  1828. {
  1829. //stimeyear has delimiter, so it's a time
  1830. direntry.date.hour = static_cast<int>(strntoi64(str, strpos - str));
  1831. direntry.date.minute = static_cast<int>(strntoi64(strpos+1, tokenlen - (strpos - str) - 1));
  1832. direntry.date.hastime = TRUE;
  1833. //Problem: Some servers use times only for files newer than 6 months,
  1834. //others use one year as limit. So there is no support for files with time
  1835. //dated in the near future. Under normal conditions there should not be such files
  1836. if (!direntry.date.year)
  1837. {
  1838. CTime curtime = CTime::GetCurrentTime();
  1839. int curday = curtime.GetDay();
  1840. int curmonth = curtime.GetMonth();
  1841. int curyear = curtime.GetYear();
  1842. int now = curmonth*31+curday;
  1843. int file = direntry.date.month*31+direntry.date.day;
  1844. if ((now+1)>=file)
  1845. direntry.date.year = curyear;
  1846. else
  1847. direntry.date.year = curyear-1;
  1848. }
  1849. }
  1850. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  1851. if (!str)
  1852. return FALSE;
  1853. if (tokenlen > 6)
  1854. {
  1855. if (!strnicmp(str + tokenlen - 5, "<DIR>", 5))
  1856. {
  1857. direntry.dir = TRUE;
  1858. tokenlen -= 5;
  1859. while (tokenlen && str[tokenlen - 1] == ' ' || str[tokenlen - 1] == '\t')
  1860. tokenlen--;
  1861. }
  1862. if (!tokenlen)
  1863. return FALSE;
  1864. }
  1865. copyStr(direntry.name, 0, str, tokenlen, true);
  1866. }
  1867. else
  1868. {
  1869. direntry.date.month = iter->second;
  1870. //Day
  1871. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1872. if (!str)
  1873. return FALSE;
  1874. if (str[tokenlen-1]==',')
  1875. tokenlen--;
  1876. if (!tokenlen)
  1877. return FALSE;
  1878. if (!IsNumeric(str, tokenlen))
  1879. return FALSE;
  1880. direntry.date.day = static_cast<int>(strntoi64(str, tokenlen));
  1881. if (direntry.date.day < 1 || direntry.date.day > 31)
  1882. return FALSE;
  1883. //Year
  1884. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1885. if (!str)
  1886. return FALSE;
  1887. if (!IsNumeric(str, tokenlen))
  1888. return FALSE;
  1889. direntry.date.year = static_cast<int>(strntoi64(str, tokenlen));
  1890. if (direntry.date.year < 50)
  1891. direntry.date.year += 2000;
  1892. else if (direntry.date.year < 1000)
  1893. direntry.date.year += 1900;
  1894. direntry.date.hasdate = TRUE;
  1895. //Now get the time
  1896. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  1897. const char *p = strnchr(str, tokenlen, ':');
  1898. if (!p)
  1899. return FALSE;
  1900. if (p==str || !IsNumeric(str, p-str) || (p-str + 1) >= tokenlen || !IsNumeric(p+1, tokenlen - (p-str) - 1))
  1901. return FALSE;
  1902. direntry.date.hour = static_cast<int>(strntoi64(str, p-str));
  1903. direntry.date.minute = static_cast<int>(strntoi64(p+1, tokenlen - (p-str) - 1));
  1904. if (direntry.date.hour < 0 || direntry.date.hour > 24)
  1905. return FALSE;
  1906. if (direntry.date.minute < 0 || direntry.date.minute > 59)
  1907. return FALSE;
  1908. direntry.date.hastime = TRUE;
  1909. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  1910. if (!str)
  1911. return FALSE;
  1912. if (tokenlen > 1 && (str[tokenlen-1] == '\\' || str[tokenlen-1] == '/'))
  1913. {
  1914. direntry.dir = TRUE;
  1915. tokenlen--;
  1916. }
  1917. else
  1918. direntry.dir = FALSE;
  1919. if (!tokenlen)
  1920. return FALSE;
  1921. copyStr(direntry.name, 0, str, tokenlen, true);
  1922. }
  1923. }
  1924. direntry.bUnsure = FALSE;
  1925. direntry.date.hasdate = TRUE;
  1926. return TRUE;
  1927. }
  1928. _int64 CFtpListResult::strntoi64(const char *str, int len) const
  1929. {
  1930. _int64 res = 0;
  1931. const char *p = str;
  1932. while ((p-str) < len)
  1933. {
  1934. if (*p < '0' || *p > '9')
  1935. break;
  1936. res *= 10;
  1937. res += *p++ - '0';
  1938. }
  1939. return res;
  1940. }
  1941. const char *CFtpListResult::GetNextToken(const char *line, const int linelen, int &len, int &pos, int type) const
  1942. {
  1943. const char *p = line + pos;
  1944. if ((p - line) >= linelen)
  1945. return NULL;
  1946. while ((p - line) < linelen && (!p || *p==' ' || *p=='\t'))
  1947. p++;
  1948. if ((p - line) >= linelen)
  1949. return NULL;
  1950. const char *res = p;
  1951. if (type)
  1952. {
  1953. pos = linelen;
  1954. len = linelen - (p - line);
  1955. }
  1956. else
  1957. {
  1958. while ((p - line) < linelen && *p && *p != ' ' && *p!='\t')
  1959. p++;
  1960. len = p - res;
  1961. pos = p - line;
  1962. }
  1963. return res;
  1964. }
  1965. const char * CFtpListResult::strnchr(const char *str, int len, char c) const
  1966. {
  1967. if (!str)
  1968. return NULL;
  1969. const char *p = str;
  1970. while (len > 0)
  1971. {
  1972. if (!*p)
  1973. return NULL;
  1974. if (*p == c)
  1975. return p;
  1976. p++;
  1977. len--;
  1978. }
  1979. return NULL;
  1980. }
  1981. const char * CFtpListResult::strnstr(const char *str, int len, const char *c) const
  1982. {
  1983. if (!str)
  1984. return NULL;
  1985. if (!c)
  1986. return NULL;
  1987. int clen = strlen(c);
  1988. const char *p = str;
  1989. while (len > 0)
  1990. {
  1991. if (!*p)
  1992. return NULL;
  1993. if (*p == *c)
  1994. {
  1995. if (clen == 1)
  1996. return p;
  1997. else if (len >= clen)
  1998. {
  1999. if (!memcmp(p + 1, c+1, clen-1))
  2000. return p;
  2001. }
  2002. else
  2003. return NULL;
  2004. }
  2005. p++;
  2006. len--;
  2007. }
  2008. return NULL;
  2009. }
  2010. void CFtpListResult::copyStr(CString &target, int pos, const char *source, int len, bool mayInvalidateUTF8 /*=false*/)
  2011. {
  2012. USES_CONVERSION;
  2013. char *p = new char[len + 1];
  2014. memcpy(p, source, len);
  2015. p[len] = '\0';
  2016. if (m_bUTF8 && *m_bUTF8)
  2017. {
  2018. // convert from UTF-8 to ANSI
  2019. if (DetectUTF8Encoding(RawByteString(p, len)) == etANSI)
  2020. {
  2021. if (mayInvalidateUTF8 && m_server.nUTF8 != 1)
  2022. {
  2023. LogMessage(FZ_LOG_WARNING, L"Server does not send proper UTF-8, falling back to local charset");
  2024. *m_bUTF8 = false;
  2025. }
  2026. target = target.Left(pos) + A2CT(p);
  2027. }
  2028. else
  2029. {
  2030. // convert from UTF-8 to ANSI
  2031. int len = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)p, -1, NULL, 0);
  2032. if (len != 0)
  2033. {
  2034. LPWSTR p1 = new WCHAR[len + 1];
  2035. MultiByteToWideChar(CP_UTF8, 0, (LPCSTR)p, -1 , (LPWSTR)p1, len + 1);
  2036. target = target.Left(pos) + W2CT(p1);
  2037. delete [] p1;
  2038. }
  2039. else
  2040. target = target.Left(pos) + A2CT(p);
  2041. }
  2042. }
  2043. else
  2044. target = target.Left(pos) + A2CT(p);
  2045. delete [] p;
  2046. }
  2047. BOOL CFtpListResult::parseAsIBM(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2048. {
  2049. int pos = 0;
  2050. int tokenlen = 0;
  2051. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2052. if (!str)
  2053. return FALSE;
  2054. copyStr(direntry.ownergroup , 0, str, tokenlen);
  2055. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2056. if (!str)
  2057. return FALSE;
  2058. if (!IsNumeric(str, tokenlen))
  2059. return FALSE;
  2060. direntry.size = strntoi64(str, tokenlen);
  2061. //Date
  2062. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2063. if (!str)
  2064. return FALSE;
  2065. if (!ParseShortDate(str, tokenlen, direntry.date))
  2066. return FALSE;
  2067. //Time
  2068. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2069. if (!str)
  2070. return FALSE;
  2071. if (!parseTime(str, tokenlen, direntry.date))
  2072. return FALSE;
  2073. //Unused Token
  2074. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2075. if (!str)
  2076. return FALSE;
  2077. //Name
  2078. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  2079. if (!str)
  2080. return FALSE;
  2081. if (str[tokenlen-1] == '/')
  2082. {
  2083. direntry.dir = TRUE;
  2084. if (!--tokenlen)
  2085. return FALSE;
  2086. }
  2087. else
  2088. direntry.dir = FALSE;
  2089. copyStr(direntry.name, 0, str, tokenlen, true);
  2090. direntry.bUnsure = FALSE;
  2091. return true;
  2092. }
  2093. BOOL CFtpListResult::parseAsIBMMVS(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2094. {
  2095. int pos = 0;
  2096. int tokenlen = 0;
  2097. // volume
  2098. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2099. if (!str)
  2100. return FALSE;
  2101. if (strncmp(str, "Migrated", tokenlen) != 0)
  2102. {
  2103. // unit
  2104. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2105. if (!str)
  2106. return FALSE;
  2107. if (strncmp(str, "Tape", tokenlen) != 0)
  2108. {
  2109. //Referred Date
  2110. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2111. if (!str)
  2112. return FALSE;
  2113. if (!ParseShortDate(str, tokenlen, direntry.date))
  2114. {
  2115. // Perhaps of the following type:
  2116. // TSO004 3390 VSAM FOO.BAR
  2117. if (tokenlen != 4 || strncmp(str, "VSAM", tokenlen))
  2118. return FALSE;
  2119. str = GetNextToken(line, linelen, tokenlen, pos, 1);
  2120. if (!str)
  2121. return FALSE;
  2122. if (strnchr(str, tokenlen, ' '))
  2123. return FALSE;
  2124. copyStr(direntry.name, 0, str, tokenlen, true);
  2125. direntry.dir = false;
  2126. return true;
  2127. }
  2128. // ext
  2129. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2130. if (!str)
  2131. return FALSE;
  2132. if (!IsNumeric(str, tokenlen))
  2133. return FALSE;
  2134. int prevLen = tokenlen;
  2135. // used
  2136. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2137. if (!str)
  2138. return FALSE;
  2139. if (IsNumeric(str, tokenlen))
  2140. {
  2141. // recfm
  2142. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2143. if (!str)
  2144. return FALSE;
  2145. if (IsNumeric(str, tokenlen))
  2146. return false;
  2147. }
  2148. else
  2149. {
  2150. if (prevLen < 6)
  2151. return false;
  2152. }
  2153. // lrecl
  2154. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2155. if (!str)
  2156. return FALSE;
  2157. if (!IsNumeric(str, tokenlen))
  2158. return FALSE;
  2159. // blksize
  2160. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2161. if (!str)
  2162. return FALSE;
  2163. if (!IsNumeric(str, tokenlen))
  2164. return FALSE;
  2165. // dsorg
  2166. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2167. if (!str)
  2168. return FALSE;
  2169. if (tokenlen == 2 && !memcmp(str, "PO", 2))
  2170. {
  2171. direntry.dir = TRUE;
  2172. }
  2173. else
  2174. {
  2175. direntry.dir = FALSE;
  2176. direntry.size = 100;
  2177. }
  2178. }
  2179. }
  2180. // name of dataset or sequential name
  2181. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2182. if (!str)
  2183. return FALSE;
  2184. copyStr(direntry.name, 0, str, tokenlen, true);
  2185. direntry.bUnsure = FALSE;
  2186. return true;
  2187. }
  2188. BOOL CFtpListResult::parseAsIBMMVSPDS(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2189. {
  2190. int pos = 0;
  2191. int tokenlen = 0;
  2192. // pds member name
  2193. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2194. if (!str)
  2195. return FALSE;
  2196. copyStr(direntry.name, 0, str, tokenlen);
  2197. // vv.mm
  2198. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2199. if (!str)
  2200. return FALSE;
  2201. // creation date
  2202. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2203. if (!str)
  2204. return FALSE;
  2205. // change date
  2206. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2207. if (!str)
  2208. return FALSE;
  2209. if (!ParseShortDate(str, tokenlen, direntry.date))
  2210. return FALSE;
  2211. // change time
  2212. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2213. if (!str)
  2214. return FALSE;
  2215. if (!parseTime(str, tokenlen, direntry.date))
  2216. return FALSE;
  2217. // size
  2218. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2219. if (!str)
  2220. return FALSE;
  2221. if (!IsNumeric(str, tokenlen))
  2222. return FALSE;
  2223. direntry.size = strntoi64(str, tokenlen);
  2224. // init
  2225. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2226. if (!str)
  2227. return FALSE;
  2228. if (!IsNumeric(str, tokenlen))
  2229. return FALSE;
  2230. // mod
  2231. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2232. if (!str)
  2233. return FALSE;
  2234. if (!IsNumeric(str, tokenlen))
  2235. return FALSE;
  2236. // id
  2237. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2238. if (!str)
  2239. return FALSE;
  2240. direntry.dir = FALSE;
  2241. direntry.bUnsure = FALSE;
  2242. return true;
  2243. }
  2244. BOOL CFtpListResult::parseAsIBMMVSPDS2(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2245. {
  2246. // Use this one only on MVS servers, as it will cause problems on other servers
  2247. if (!(m_server.nServerType & (FZ_SERVERTYPE_SUB_FTP_MVS | FZ_SERVERTYPE_SUB_FTP_BS2000)))
  2248. return false;
  2249. int pos = 0;
  2250. int tokenlen = 0;
  2251. direntry.bUnsure = FALSE;
  2252. direntry.dir = FALSE;
  2253. direntry.bLink = FALSE;
  2254. direntry.ownergroup = L"";
  2255. direntry.permissionstr = L"";
  2256. direntry.date.hasdate = direntry.date.hastime = FALSE;
  2257. // pds member name
  2258. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2259. if (!str)
  2260. return FALSE;
  2261. if (m_server.nServerType & FZ_SERVERTYPE_SUB_FTP_BS2000 &&
  2262. IsNumeric(str, tokenlen))
  2263. {
  2264. int prevlen = tokenlen;
  2265. const char* prev = str;
  2266. int oldpos = pos;
  2267. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2268. if (!str)
  2269. return FALSE;
  2270. if (str[0] != ':')
  2271. {
  2272. str = prev;
  2273. tokenlen = prevlen;
  2274. pos = oldpos;
  2275. }
  2276. }
  2277. copyStr(direntry.name, 0, str, tokenlen);
  2278. if (m_server.nServerType & FZ_SERVERTYPE_SUB_FTP_BS2000)
  2279. {
  2280. int pos = direntry.name.ReverseFind(L':');
  2281. if (pos != -1)
  2282. direntry.name = direntry.name.Mid(pos + 1);
  2283. if (direntry.name[0] == L'$')
  2284. {
  2285. int pos = direntry.name.Find(L'.');
  2286. if (pos != -1)
  2287. direntry.name = direntry.name.Mid(pos + 1);
  2288. }
  2289. if (direntry.name == L"")
  2290. return FALSE;
  2291. }
  2292. // Hexadecimal filesize
  2293. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2294. if (!str)
  2295. // We can return true since sometimes MVS servers send pure filenames
  2296. return true;
  2297. direntry.size = 0;
  2298. const char *end = str + tokenlen;
  2299. const char *p = str;
  2300. while (p < end)
  2301. {
  2302. direntry.size *= 16;
  2303. if (*p >= '0' && *p <= '9')
  2304. direntry.size += *p - '0';
  2305. else if (*p >= 'A' && *p <= 'F')
  2306. direntry.size += *p - 'A' + 10;
  2307. else
  2308. return false;
  2309. p++;
  2310. }
  2311. // Unused hexadecimal token
  2312. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2313. if (!str)
  2314. return false;
  2315. end = str + tokenlen;
  2316. p = str;
  2317. while (p < end)
  2318. {
  2319. if ((*p < '0' || *p > '9') && (*p < 'A' || *p > 'F'))
  2320. return false;
  2321. p++;
  2322. }
  2323. // Unused token
  2324. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2325. if (!str)
  2326. return false;
  2327. if (!IsNumeric(str, tokenlen))
  2328. return false;
  2329. const char* prevprev = 0;
  2330. const char* prev = 0;
  2331. int prevprevlen = 0;
  2332. int prevlen = 0;
  2333. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2334. if (!str)
  2335. return false;
  2336. while (str)
  2337. {
  2338. if (prevprev)
  2339. {
  2340. for (int i = 0; i < prevprevlen; i++)
  2341. if (prevprev[i] < 'A' || prevprev[i] > 'Z')
  2342. return false;
  2343. }
  2344. prevprev = prev;
  2345. prevprevlen = prevlen;
  2346. prev = str;
  2347. prevlen = tokenlen;
  2348. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2349. }
  2350. if (!prev || !prevprev)
  2351. return false;
  2352. if (!IsNumeric(prev, prevlen) && (prevlen != 3 || memcmp(prev, "ANY", 3)))
  2353. return false;
  2354. if (!IsNumeric(prevprev, prevprevlen) && (prevprevlen != 3 || memcmp(prevprev, "ANY", 3)))
  2355. return false;
  2356. return true;
  2357. }
  2358. bool CFtpListResult::parseTime(const char *str, int len, t_directory::t_direntry::t_date &date) const
  2359. {
  2360. int i = 0;
  2361. //Extract the hour
  2362. date.hastime = TRUE;
  2363. date.hour = 0;
  2364. while (str[i] != ':')
  2365. {
  2366. if (str[i] < '0' || str[i] > '9')
  2367. return false;
  2368. date.hour *= 10;
  2369. date.hour += str[i] - '0';
  2370. if (date.hour > 24)
  2371. return false;
  2372. i++;
  2373. if (i == len)
  2374. return false;
  2375. }
  2376. // Make sure we did read at least one digit
  2377. if (!i)
  2378. return false;
  2379. i++;
  2380. if (i == len)
  2381. return false;
  2382. //Extract the minute
  2383. date.minute = 0;
  2384. while (str[i] >= '0' && str[i] <= '9')
  2385. {
  2386. date.minute *= 10;
  2387. date.minute += str[i] - '0';
  2388. if (date.minute > 59)
  2389. return false;
  2390. i++;
  2391. if (i == len)
  2392. break;
  2393. }
  2394. //Convert to 24h format
  2395. //I really wish we would have the following system:
  2396. //one year->ten months->ten days->ten hours->ten minutes->ten seconds and so on...
  2397. //I should modifiy the earth rotation to force everyone to use this system *g*
  2398. if (i != len)
  2399. {
  2400. if (str[i]=='P')
  2401. {
  2402. if (date.hour < 12)
  2403. date.hour += 12;
  2404. }
  2405. else
  2406. if (date.hour == 12)
  2407. date.hour = 0;
  2408. }
  2409. date.hastime = TRUE;
  2410. return true;
  2411. }
  2412. BOOL CFtpListResult::parseAsWfFtp(const char *line, const int linelen, t_directory::t_direntry &direntry)
  2413. {
  2414. int pos = 0;
  2415. int tokenlen = 0;
  2416. const char *str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2417. if (!str)
  2418. return FALSE;
  2419. copyStr(direntry.name, 0, str, tokenlen);
  2420. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2421. if (!str)
  2422. return FALSE;
  2423. if (!IsNumeric(str, tokenlen))
  2424. return FALSE;
  2425. direntry.size = strntoi64(str, tokenlen);
  2426. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2427. if (!str)
  2428. return FALSE;
  2429. if (!ParseShortDate(str, tokenlen, direntry.date))
  2430. return FALSE;
  2431. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2432. if (!str)
  2433. return FALSE;
  2434. if (str[tokenlen - 1] != '.')
  2435. return FALSE;
  2436. str = GetNextToken(line, linelen, tokenlen, pos, 0);
  2437. if (!str)
  2438. return FALSE;
  2439. if (!parseTime(str, tokenlen, direntry.date))
  2440. return FALSE;
  2441. direntry.bUnsure = FALSE;
  2442. direntry.dir = false;
  2443. direntry.bLink = false;
  2444. direntry.permissionstr = L"";
  2445. direntry.ownergroup = L"";
  2446. return TRUE;
  2447. }
  2448. bool CFtpListResult::ParseSize(const char* str, int len, __int64 &size) const
  2449. {
  2450. if (len < 1)
  2451. return false;
  2452. if (IsNumeric(str, len))
  2453. {
  2454. size = strntoi64(str, len);
  2455. return true;
  2456. }
  2457. size = 0;
  2458. char last = str[--len];
  2459. int delimiter = -1;
  2460. for (int i = 0; i < len; i++)
  2461. {
  2462. char c = str[i];
  2463. if (c >= '0' && c <= '9')
  2464. {
  2465. size *= 10;
  2466. size += c - '0';
  2467. }
  2468. else if (c == '.')
  2469. delimiter = len - i;
  2470. else
  2471. return false;
  2472. }
  2473. // Check for digit before or after delimiterr
  2474. if (!delimiter || delimiter == len)
  2475. return false;
  2476. switch (last)
  2477. {
  2478. case 'k':
  2479. case 'K':
  2480. size *= 1 << 10;
  2481. break;
  2482. case 'm':
  2483. case 'M':
  2484. size *= 1 << 20;
  2485. break;
  2486. case 'g':
  2487. case 'G':
  2488. size *= 1 << 30;
  2489. break;
  2490. case 't':
  2491. case 'T':
  2492. size *= (__int64)1 << 40;
  2493. break;
  2494. default:
  2495. return false;
  2496. }
  2497. if (delimiter == -1)
  2498. return true;
  2499. while (delimiter--)
  2500. size /= 10;
  2501. return true;
  2502. }