FtpListResult.cpp 70 KB

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