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