util.cc 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "util.h"
  36. #include <signal.h>
  37. #include <sys/types.h>
  38. #ifdef HAVE_PWD_H
  39. # include <pwd.h>
  40. #endif // HAVE_PWD_H
  41. #include <cerrno>
  42. #include <cassert>
  43. #include <cstring>
  44. #include <cstdio>
  45. #include <cstdlib>
  46. #include <sstream>
  47. #include <ostream>
  48. #include <algorithm>
  49. #include <fstream>
  50. #include <iomanip>
  51. #include "SimpleRandomizer.h"
  52. #include "File.h"
  53. #include "Randomizer.h"
  54. #include "a2netcompat.h"
  55. #include "BitfieldMan.h"
  56. #include "DefaultDiskWriter.h"
  57. #include "FatalException.h"
  58. #include "FileEntry.h"
  59. #include "A2STR.h"
  60. #include "array_fun.h"
  61. #include "bitfield.h"
  62. #include "DownloadHandlerConstants.h"
  63. #include "RequestGroup.h"
  64. #include "LogFactory.h"
  65. #include "Logger.h"
  66. #include "Option.h"
  67. #include "DownloadContext.h"
  68. #include "BufferedFile.h"
  69. #include "SocketCore.h"
  70. #include "prefs.h"
  71. #ifdef ENABLE_MESSAGE_DIGEST
  72. # include "MessageDigest.h"
  73. # include "message_digest_helper.h"
  74. #endif // ENABLE_MESSAGE_DIGEST
  75. // For libc6 which doesn't define ULLONG_MAX properly because of broken limits.h
  76. #ifndef ULLONG_MAX
  77. # define ULLONG_MAX 18446744073709551615ULL
  78. #endif // ULLONG_MAX
  79. namespace aria2 {
  80. #ifdef __MINGW32__
  81. namespace {
  82. int utf8ToWChar(wchar_t* out, size_t outLength, const char* src)
  83. {
  84. return MultiByteToWideChar(CP_UTF8, 0, src, -1, out, outLength);
  85. }
  86. } // namespace
  87. namespace {
  88. int ansiToWChar(wchar_t* out, size_t outLength, const char* src)
  89. {
  90. return MultiByteToWideChar(CP_ACP, 0, src, -1, out, outLength);
  91. }
  92. } // namespace
  93. namespace {
  94. int wCharToUtf8(char* out, size_t outLength, const wchar_t* src)
  95. {
  96. return WideCharToMultiByte(CP_UTF8, 0, src, -1, out, outLength, 0, 0);
  97. }
  98. } // namespace
  99. namespace {
  100. int wCharToAnsi(char* out, size_t outLength, const wchar_t* src)
  101. {
  102. return WideCharToMultiByte(CP_ACP, 0, src, -1, out, outLength, 0, 0);
  103. }
  104. } // namespace
  105. std::wstring utf8ToWChar(const char* src)
  106. {
  107. int len = utf8ToWChar(0, 0, src);
  108. if(len == 0) {
  109. abort();
  110. }
  111. array_ptr<wchar_t> buf(new wchar_t[len]);
  112. len = utf8ToWChar(buf, len, src);
  113. if(len == 0) {
  114. abort();
  115. } else {
  116. std::wstring dest(buf);
  117. return dest;
  118. }
  119. }
  120. std::wstring utf8ToWChar(const std::string& src)
  121. {
  122. return utf8ToWChar(src.c_str());
  123. }
  124. std::string utf8ToNative(const std::string& src)
  125. {
  126. std::wstring wsrc = utf8ToWChar(src);
  127. int len = wCharToAnsi(0, 0, wsrc.c_str());
  128. if(len == 0) {
  129. abort();
  130. }
  131. array_ptr<char> buf(new char[len]);
  132. len = wCharToAnsi(buf, len, wsrc.c_str());
  133. if(len == 0) {
  134. abort();
  135. } else {
  136. std::string dest(buf);
  137. return dest;
  138. }
  139. }
  140. std::string wCharToUtf8(const std::wstring& wsrc)
  141. {
  142. int len = wCharToUtf8(0, 0, wsrc.c_str());
  143. if(len == 0) {
  144. abort();
  145. }
  146. array_ptr<char> buf(new char[len]);
  147. len = wCharToUtf8(buf, len, wsrc.c_str());
  148. if(len == 0) {
  149. abort();
  150. } else {
  151. std::string dest(buf);
  152. return dest;
  153. }
  154. }
  155. std::string nativeToUtf8(const std::string& src)
  156. {
  157. int len = ansiToWChar(0, 0, src.c_str());
  158. if(len == 0) {
  159. abort();
  160. }
  161. array_ptr<wchar_t> buf(new wchar_t[len]);
  162. len = ansiToWChar(buf, len, src.c_str());
  163. if(len == 0) {
  164. abort();
  165. } else {
  166. return wCharToUtf8(std::wstring(buf));
  167. }
  168. }
  169. #endif // __MINGW32__
  170. namespace util {
  171. const std::string DEFAULT_STRIP_CHARSET("\r\n\t ");
  172. std::string strip(const std::string& str, const std::string& chars)
  173. {
  174. std::pair<std::string::const_iterator,
  175. std::string::const_iterator> p =
  176. stripIter(str.begin(), str.end(), chars);
  177. return std::string(p.first, p.second);
  178. }
  179. std::string itos(int64_t value, bool comma)
  180. {
  181. bool flag = false;
  182. std::string str;
  183. if(value < 0) {
  184. if(value == INT64_MIN) {
  185. if(comma) {
  186. str = "-9,223,372,036,854,775,808";
  187. } else {
  188. str = "-9223372036854775808";
  189. }
  190. return str;
  191. }
  192. flag = true;
  193. value = -value;
  194. }
  195. str = uitos(value, comma);
  196. if(flag) {
  197. str.insert(str.begin(), '-');
  198. }
  199. return str;
  200. }
  201. int64_t difftv(struct timeval tv1, struct timeval tv2) {
  202. if((tv1.tv_sec < tv2.tv_sec) ||
  203. ((tv1.tv_sec == tv2.tv_sec) && (tv1.tv_usec < tv2.tv_usec))) {
  204. return 0;
  205. }
  206. return ((int64_t)(tv1.tv_sec-tv2.tv_sec)*1000000+
  207. tv1.tv_usec-tv2.tv_usec);
  208. }
  209. int32_t difftvsec(struct timeval tv1, struct timeval tv2) {
  210. if(tv1.tv_sec < tv2.tv_sec) {
  211. return 0;
  212. }
  213. return tv1.tv_sec-tv2.tv_sec;
  214. }
  215. std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr) {
  216. if(target.empty() || oldstr.empty()) {
  217. return target;
  218. }
  219. std::string result;
  220. std::string::size_type p = 0;
  221. std::string::size_type np = target.find(oldstr);
  222. while(np != std::string::npos) {
  223. result.append(target.begin()+p, target.begin()+np);
  224. result += newstr;
  225. p = np+oldstr.size();
  226. np = target.find(oldstr, p);
  227. }
  228. result.append(target.begin()+p, target.end());
  229. return result;
  230. }
  231. bool isAlpha(const char c)
  232. {
  233. return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
  234. }
  235. bool isDigit(const char c)
  236. {
  237. return '0' <= c && c <= '9';
  238. }
  239. bool isHexDigit(const char c)
  240. {
  241. return isDigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
  242. }
  243. bool isHexDigit(const std::string& s)
  244. {
  245. for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
  246. if(!isHexDigit(*i)) {
  247. return false;
  248. }
  249. }
  250. return true;
  251. }
  252. bool inRFC3986ReservedChars(const char c)
  253. {
  254. static const char reserved[] = {
  255. ':' , '/' , '?' , '#' , '[' , ']' , '@',
  256. '!' , '$' , '&' , '\'' , '(' , ')',
  257. '*' , '+' , ',' , ';' , '=' };
  258. return std::find(vbegin(reserved), vend(reserved), c) != vend(reserved);
  259. }
  260. bool inRFC3986UnreservedChars(const char c)
  261. {
  262. static const char unreserved[] = { '-', '.', '_', '~' };
  263. return isAlpha(c) || isDigit(c) ||
  264. std::find(vbegin(unreserved), vend(unreserved), c) != vend(unreserved);
  265. }
  266. bool inRFC2978MIMECharset(const char c)
  267. {
  268. static const char chars[] = {
  269. '!', '#', '$', '%', '&',
  270. '\'', '+', '-', '^', '_',
  271. '`', '{', '}', '~'
  272. };
  273. return isAlpha(c) || isDigit(c) ||
  274. std::find(vbegin(chars), vend(chars), c) != vend(chars);
  275. }
  276. bool inRFC2616HttpToken(const char c)
  277. {
  278. static const char chars[] = {
  279. '!', '#', '$', '%', '&', '\'', '*', '+', '-', '.',
  280. '^', '_', '`', '|', '~'
  281. };
  282. return isAlpha(c) || isDigit(c) ||
  283. std::find(vbegin(chars), vend(chars), c) != vend(chars);
  284. }
  285. namespace {
  286. bool isUtf8Tail(unsigned char ch)
  287. {
  288. return in(ch, 0x80u, 0xbfu);
  289. }
  290. } // namespace
  291. bool isUtf8(const std::string& str)
  292. {
  293. for(std::string::const_iterator s = str.begin(), eos = str.end(); s != eos;
  294. ++s) {
  295. unsigned char firstChar = *s;
  296. // See ABNF in http://tools.ietf.org/search/rfc3629#section-4
  297. if(in(firstChar, 0x20u, 0x7eu) ||
  298. firstChar == 0x08u || // \b
  299. firstChar == 0x09u || // \t
  300. firstChar == 0x0au || // \n
  301. firstChar == 0x0cu || // \f
  302. firstChar == 0x0du // \r
  303. ) {
  304. // UTF8-1 (without ctrl chars)
  305. } else if(in(firstChar, 0xc2u, 0xdfu)) {
  306. // UTF8-2
  307. if(++s == eos || !isUtf8Tail(*s)) {
  308. return false;
  309. }
  310. } else if(0xe0u == firstChar) {
  311. // UTF8-3
  312. if(++s == eos || !in(static_cast<unsigned char>(*s), 0xa0u, 0xbfu) ||
  313. ++s == eos || !isUtf8Tail(*s)) {
  314. return false;
  315. }
  316. } else if(in(firstChar, 0xe1u, 0xecu) || in(firstChar, 0xeeu, 0xefu)) {
  317. // UTF8-3
  318. if(++s == eos || !isUtf8Tail(*s) ||
  319. ++s == eos || !isUtf8Tail(*s)) {
  320. return false;
  321. }
  322. } else if(0xedu == firstChar) {
  323. // UTF8-3
  324. if(++s == eos || !in(static_cast<unsigned char>(*s), 0x80u, 0x9fu) ||
  325. ++s == eos || !isUtf8Tail(*s)) {
  326. return false;
  327. }
  328. } else if(0xf0u == firstChar) {
  329. // UTF8-4
  330. if(++s == eos || !in(static_cast<unsigned char>(*s), 0x90u, 0xbfu) ||
  331. ++s == eos || !isUtf8Tail(*s) ||
  332. ++s == eos || !isUtf8Tail(*s)) {
  333. return false;
  334. }
  335. } else if(in(firstChar, 0xf1u, 0xf3u)) {
  336. // UTF8-4
  337. if(++s == eos || !isUtf8Tail(*s) ||
  338. ++s == eos || !isUtf8Tail(*s) ||
  339. ++s == eos || !isUtf8Tail(*s)) {
  340. return false;
  341. }
  342. } else if(0xf4u == firstChar) {
  343. // UTF8-4
  344. if(++s == eos || !in(static_cast<unsigned char>(*s), 0x80u, 0x8fu) ||
  345. ++s == eos || !isUtf8Tail(*s) ||
  346. ++s == eos || !isUtf8Tail(*s)) {
  347. return false;
  348. }
  349. } else {
  350. return false;
  351. }
  352. }
  353. return true;
  354. }
  355. std::string percentEncode(const unsigned char* target, size_t len)
  356. {
  357. std::string dest;
  358. for(size_t i = 0; i < len; ++i) {
  359. if(inRFC3986UnreservedChars(target[i])) {
  360. dest += target[i];
  361. } else {
  362. dest.append(fmt("%%%02X", target[i]));
  363. }
  364. }
  365. return dest;
  366. }
  367. std::string percentEncode(const std::string& target)
  368. {
  369. return percentEncode(reinterpret_cast<const unsigned char*>(target.c_str()),
  370. target.size());
  371. }
  372. std::string percentEncodeMini(const std::string& src)
  373. {
  374. std::string result;
  375. for(std::string::const_iterator i = src.begin(), eoi = src.end(); i != eoi;
  376. ++i) {
  377. // Non-Printable ASCII and non-ASCII chars + some ASCII chars.
  378. unsigned char c = *i;
  379. if(in(c, 0x00u, 0x20u) || c >= 0x7fu ||
  380. // Chromium escapes following characters. Firefox4 escapes
  381. // more.
  382. c == '"' || c == '<' || c == '>') {
  383. result += fmt("%%%02X", c);
  384. } else {
  385. result += c;
  386. }
  387. }
  388. return result;
  389. }
  390. std::string torrentPercentEncode(const unsigned char* target, size_t len) {
  391. std::string dest;
  392. for(size_t i = 0; i < len; ++i) {
  393. if(isAlpha(target[i]) || isDigit(target[i])) {
  394. dest += target[i];
  395. } else {
  396. dest.append(fmt("%%%02X", target[i]));
  397. }
  398. }
  399. return dest;
  400. }
  401. std::string torrentPercentEncode(const std::string& target)
  402. {
  403. return torrentPercentEncode
  404. (reinterpret_cast<const unsigned char*>(target.c_str()), target.size());
  405. }
  406. std::string percentDecode
  407. (std::string::const_iterator first, std::string::const_iterator last)
  408. {
  409. std::string result;
  410. for(; first != last; ++first) {
  411. if(*first == '%') {
  412. if(first+1 != last && first+2 != last &&
  413. isHexDigit(*(first+1)) && isHexDigit(*(first+2))) {
  414. result += parseInt(std::string(first+1, first+3), 16);
  415. first += 2;
  416. } else {
  417. result += *first;
  418. }
  419. } else {
  420. result += *first;
  421. }
  422. }
  423. return result;
  424. }
  425. std::string toHex(const unsigned char* src, size_t len) {
  426. std::string out(len*2, '\0');
  427. std::string::iterator o = out.begin();
  428. const unsigned char* last = src+len;
  429. for(const unsigned char* i = src; i != last; ++i) {
  430. *o = (*i >> 4);
  431. *(o+1) = (*i)&0x0fu;
  432. for(int j = 0; j < 2; ++j) {
  433. if(*o < 10) {
  434. *o += '0';
  435. } else {
  436. *o += 'a'-10;
  437. }
  438. ++o;
  439. }
  440. }
  441. return out;
  442. }
  443. std::string toHex(const char* src, size_t len)
  444. {
  445. return toHex(reinterpret_cast<const unsigned char*>(src), len);
  446. }
  447. std::string toHex(const std::string& src)
  448. {
  449. return toHex(reinterpret_cast<const unsigned char*>(src.c_str()), src.size());
  450. }
  451. unsigned int hexCharToUInt(unsigned char ch)
  452. {
  453. if('a' <= ch && ch <= 'f') {
  454. ch -= 'a';
  455. ch += 10;
  456. } else if('A' <= ch && ch <= 'F') {
  457. ch -= 'A';
  458. ch += 10;
  459. } else if('0' <= ch && ch <= '9') {
  460. ch -= '0';
  461. } else {
  462. ch = 255;
  463. }
  464. return ch;
  465. }
  466. FILE* openFile(const std::string& filename, const std::string& mode) {
  467. FILE* file = fopen(filename.c_str(), mode.c_str());
  468. return file;
  469. }
  470. bool isPowerOf(int num, int base) {
  471. if(base <= 0) { return false; }
  472. if(base == 1) { return true; }
  473. while(num%base == 0) {
  474. num /= base;
  475. if(num == 1) {
  476. return true;
  477. }
  478. }
  479. return false;
  480. }
  481. std::string secfmt(time_t sec) {
  482. time_t tsec = sec;
  483. std::string str;
  484. if(sec >= 3600) {
  485. str = fmt("%lldh", static_cast<long long int>(sec/3600));
  486. sec %= 3600;
  487. }
  488. if(sec >= 60) {
  489. str += fmt("%dm", static_cast<int>(sec/60));
  490. sec %= 60;
  491. }
  492. if(sec || tsec == 0) {
  493. str += fmt("%ds", static_cast<int>(sec));
  494. }
  495. return str;
  496. }
  497. int getNum(const char* buf, int offset, size_t length) {
  498. char* temp = new char[length+1];
  499. memcpy(temp, buf+offset, length);
  500. temp[length] = '\0';
  501. int x = strtol(temp, 0, 10);
  502. delete [] temp;
  503. return x;
  504. }
  505. namespace {
  506. template<typename T, typename F>
  507. bool parseLong(T& res, F f, const std::string& s, int base)
  508. {
  509. if(s.empty()) {
  510. return false;
  511. }
  512. char* endptr;
  513. errno = 0;
  514. res = f(s.c_str(), &endptr, base);
  515. if(errno == ERANGE) {
  516. return false;
  517. }
  518. if(*endptr != '\0') {
  519. for(const char* i = endptr, *eoi = s.c_str()+s.size(); i < eoi; ++i) {
  520. if(!isspace(*i)) {
  521. return false;
  522. }
  523. }
  524. }
  525. return true;
  526. }
  527. } // namespace
  528. bool parseIntNoThrow(int32_t& res, const std::string& s, int base)
  529. {
  530. long int t;
  531. if(parseLong(t, strtol, s, base) &&
  532. t >= std::numeric_limits<int32_t>::min() &&
  533. t <= std::numeric_limits<int32_t>::max()) {
  534. res = t;
  535. return true;
  536. } else {
  537. return false;
  538. }
  539. }
  540. int32_t parseInt(const std::string& s, int base)
  541. {
  542. int32_t res;
  543. if(parseIntNoThrow(res, s, base)) {
  544. return res;
  545. } else {
  546. throw DL_ABORT_EX
  547. (fmt("Failed to convert string into 32bit signed integer. '%s'",
  548. s.c_str()));
  549. }
  550. }
  551. bool parseUIntNoThrow(uint32_t& res, const std::string& s, int base)
  552. {
  553. long int t;
  554. if(parseLong(t, strtol, s, base) &&
  555. t >= 0 &&
  556. t <= std::numeric_limits<int32_t>::max()) {
  557. res = t;
  558. return true;
  559. } else {
  560. return false;
  561. }
  562. }
  563. uint32_t parseUInt(const std::string& s, int base)
  564. {
  565. uint32_t res;
  566. if(parseUIntNoThrow(res, s, base)) {
  567. return res;
  568. } else {
  569. throw DL_ABORT_EX
  570. (fmt("Failed to convert string into 32bit unsigned integer. '%s'",
  571. s.c_str()));
  572. }
  573. }
  574. bool parseLLIntNoThrow(int64_t& res, const std::string& s, int base)
  575. {
  576. long long int t;
  577. if(parseLong(t, strtoll, s, base) &&
  578. t >= std::numeric_limits<int64_t>::min() &&
  579. t <= std::numeric_limits<int64_t>::max()) {
  580. res = t;
  581. return true;
  582. } else {
  583. return false;
  584. }
  585. }
  586. int64_t parseLLInt(const std::string& s, int base)
  587. {
  588. int64_t res;
  589. if(parseLLIntNoThrow(res, s, base)) {
  590. return res;
  591. } else {
  592. throw DL_ABORT_EX
  593. (fmt("Failed to convert string into 64bit signed integer. '%s'",
  594. s.c_str()));
  595. }
  596. }
  597. void parseIntSegments(SegList<int>& sgl, const std::string& src)
  598. {
  599. for(std::string::const_iterator i = src.begin(), eoi = src.end(); i != eoi;) {
  600. std::string::const_iterator j = std::find(i, eoi, ',');
  601. if(j == i) {
  602. ++i;
  603. continue;
  604. }
  605. std::string::const_iterator p = std::find(i, j, '-');
  606. if(p == j) {
  607. int a = parseInt(std::string(i, j));
  608. sgl.add(a, a+1);
  609. } else if(p == i || p+1 == j) {
  610. throw DL_ABORT_EX(fmt(MSG_INCOMPLETE_RANGE, std::string(i, j).c_str()));
  611. } else {
  612. int a = parseInt(std::string(i, p));
  613. int b = parseInt(std::string(p+1, j));
  614. sgl.add(a, b+1);
  615. }
  616. if(j == eoi) {
  617. break;
  618. }
  619. i = j+1;
  620. }
  621. }
  622. namespace {
  623. void computeHeadPieces
  624. (std::vector<size_t>& indexes,
  625. const std::vector<SharedHandle<FileEntry> >& fileEntries,
  626. size_t pieceLength,
  627. off_t head)
  628. {
  629. if(head == 0) {
  630. return;
  631. }
  632. for(std::vector<SharedHandle<FileEntry> >::const_iterator fi =
  633. fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
  634. if((*fi)->getLength() == 0) {
  635. continue;
  636. }
  637. size_t lastIndex =
  638. ((*fi)->getOffset()+std::min(head, (*fi)->getLength())-1)/pieceLength;
  639. for(size_t index = (*fi)->getOffset()/pieceLength;
  640. index <= lastIndex; ++index) {
  641. indexes.push_back(index);
  642. }
  643. }
  644. }
  645. } // namespace
  646. namespace {
  647. void computeTailPieces
  648. (std::vector<size_t>& indexes,
  649. const std::vector<SharedHandle<FileEntry> >& fileEntries,
  650. size_t pieceLength,
  651. off_t tail)
  652. {
  653. if(tail == 0) {
  654. return;
  655. }
  656. for(std::vector<SharedHandle<FileEntry> >::const_iterator fi =
  657. fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
  658. if((*fi)->getLength() == 0) {
  659. continue;
  660. }
  661. int64_t endOffset = (*fi)->getLastOffset();
  662. size_t fromIndex =
  663. (endOffset-1-(std::min(tail, (*fi)->getLength())-1))/pieceLength;
  664. for(size_t index = fromIndex; index <= (endOffset-1)/pieceLength;
  665. ++index) {
  666. indexes.push_back(index);
  667. }
  668. }
  669. }
  670. } // namespace
  671. void parsePrioritizePieceRange
  672. (std::vector<size_t>& result, const std::string& src,
  673. const std::vector<SharedHandle<FileEntry> >& fileEntries,
  674. size_t pieceLength,
  675. off_t defaultSize)
  676. {
  677. std::vector<size_t> indexes;
  678. std::vector<Scip> parts;
  679. splitIter(src.begin(), src.end(), std::back_inserter(parts), ',', true);
  680. for(std::vector<Scip>::const_iterator i = parts.begin(),
  681. eoi = parts.end(); i != eoi; ++i) {
  682. if(util::streq((*i).first, (*i).second, "head")) {
  683. computeHeadPieces(indexes, fileEntries, pieceLength, defaultSize);
  684. } else if(util::startsWith((*i).first, (*i).second, "head=")) {
  685. std::string sizestr((*i).first+5, (*i).second);
  686. computeHeadPieces(indexes, fileEntries, pieceLength,
  687. std::max((int64_t)0, getRealSize(sizestr)));
  688. } else if(util::streq((*i).first, (*i).second, "tail")) {
  689. computeTailPieces(indexes, fileEntries, pieceLength, defaultSize);
  690. } else if(util::startsWith((*i).first, (*i).second, "tail=")) {
  691. std::string sizestr((*i).first+5, (*i).second);
  692. computeTailPieces(indexes, fileEntries, pieceLength,
  693. std::max((int64_t)0, getRealSize(sizestr)));
  694. } else {
  695. throw DL_ABORT_EX(fmt("Unrecognized token %s",
  696. std::string((*i).first, (*i).second).c_str()));
  697. }
  698. }
  699. std::sort(indexes.begin(), indexes.end());
  700. indexes.erase(std::unique(indexes.begin(), indexes.end()), indexes.end());
  701. result.insert(result.end(), indexes.begin(), indexes.end());
  702. }
  703. // Converts ISO/IEC 8859-1 string to UTF-8 string. If there is a
  704. // character not in ISO/IEC 8859-1, returns empty string.
  705. std::string iso8859ToUtf8(const std::string& src)
  706. {
  707. std::string dest;
  708. for(std::string::const_iterator itr = src.begin(), eoi = src.end();
  709. itr != eoi; ++itr) {
  710. unsigned char c = *itr;
  711. if(0xa0u <= c) {
  712. if(c <= 0xbfu) {
  713. dest += 0xc2u;
  714. } else {
  715. dest += 0xc3u;
  716. }
  717. dest += c&(~0x40u);
  718. } else if(0x80u <= c && c <= 0x9fu) {
  719. return A2STR::NIL;
  720. } else {
  721. dest += c;
  722. }
  723. }
  724. return dest;
  725. }
  726. namespace {
  727. template<typename OutputIterator>
  728. void parseParam(OutputIterator out, const std::string& header)
  729. {
  730. for(std::string::const_iterator i = header.begin(), eoi = header.end();
  731. i != eoi;) {
  732. std::string::const_iterator paramFirst = i;
  733. std::string::const_iterator paramLast = paramFirst;
  734. for(; paramLast != eoi && *paramLast != '=' && *paramLast != ';';
  735. ++paramLast);
  736. std::string param;
  737. if(paramLast == eoi || *paramLast == ';') {
  738. // No value, parmname only
  739. param.assign(paramFirst, paramLast);
  740. } else {
  741. for(; paramLast != eoi && *paramLast != '"' && *paramLast != ';';
  742. ++paramLast);
  743. if(paramLast != eoi && *paramLast == '"') {
  744. // quoted-string
  745. ++paramLast;
  746. for(; paramLast != eoi && *paramLast != '"'; ++paramLast);
  747. if(paramLast != eoi) {
  748. ++paramLast;
  749. }
  750. param.assign(paramFirst, paramLast);
  751. for(; paramLast != eoi && *paramLast != ';'; ++paramLast);
  752. } else {
  753. param.assign(paramFirst, paramLast);
  754. }
  755. }
  756. param = strip(param);
  757. *out++ = param;
  758. if(paramLast == eoi) {
  759. break;
  760. }
  761. i = paramLast;
  762. ++i;
  763. }
  764. }
  765. } // namespace
  766. std::string getContentDispositionFilename(const std::string& header)
  767. {
  768. static const char A2_KEYNAME[] = "filename";
  769. std::string filename;
  770. std::vector<std::string> params;
  771. parseParam(std::back_inserter(params), header);
  772. for(std::vector<std::string>::const_iterator i = params.begin(),
  773. eoi = params.end(); i != eoi; ++i) {
  774. const std::string& param = *i;
  775. if(!istartsWith(param, A2_KEYNAME) ||
  776. param.size() == sizeof(A2_KEYNAME)-1) {
  777. continue;
  778. }
  779. std::string::const_iterator markeritr = param.begin()+sizeof(A2_KEYNAME)-1;
  780. if(*markeritr == '*') {
  781. // See RFC2231 Section4 and draft-reschke-rfc2231-in-http.
  782. // Please note that this function doesn't do charset conversion
  783. // except that if iso-8859-1 is specified, it is converted to
  784. // utf-8.
  785. ++markeritr;
  786. for(; markeritr != param.end() && *markeritr == ' '; ++markeritr);
  787. if(markeritr == param.end() || *markeritr != '=') {
  788. continue;
  789. }
  790. std::vector<Scip> extValues;
  791. splitIter(markeritr+1, param.end(), std::back_inserter(extValues),
  792. '\'', true, true);
  793. if(extValues.size() != 3) {
  794. continue;
  795. }
  796. bool bad = false;
  797. for(std::string::const_iterator j = extValues[0].first,
  798. eoj = extValues[0].second; j != eoj; ++j) {
  799. // Since we first split parameter by ', we can safely assume
  800. // that ' is not included in charset.
  801. if(!inRFC2978MIMECharset(*j)) {
  802. bad = true;
  803. break;
  804. }
  805. }
  806. if(bad) {
  807. continue;
  808. }
  809. bad = false;
  810. for(std::string::const_iterator j = extValues[2].first,
  811. eoj = extValues[2].second; j != eoj; ++j){
  812. if(*j == '%') {
  813. if(j+1 != eoj && isHexDigit(*(j+1)) &&
  814. j+2 != eoj && isHexDigit(*(j+2))) {
  815. j += 2;
  816. } else {
  817. bad = true;
  818. break;
  819. }
  820. } else {
  821. if(*j == '*' || *j == '\'' || !inRFC2616HttpToken(*j)) {
  822. bad = true;
  823. break;
  824. }
  825. }
  826. }
  827. if(bad) {
  828. continue;
  829. }
  830. std::string value =
  831. percentDecode(extValues[2].first, extValues[2].second);
  832. if(util::strieq(extValues[0].first, extValues[0].second, "iso-8859-1")) {
  833. value = iso8859ToUtf8(value);
  834. }
  835. if(!detectDirTraversal(value) && value.find("/") == std::string::npos) {
  836. filename = value;
  837. }
  838. if(!filename.empty()) {
  839. break;
  840. }
  841. } else {
  842. for(; markeritr != param.end() && *markeritr == ' '; ++markeritr);
  843. if(markeritr == param.end() || markeritr+1 == param.end() ||
  844. *markeritr != '=') {
  845. continue;
  846. }
  847. Scip p = stripIter(markeritr+1, param.end());
  848. if(p.first == p.second) {
  849. continue;
  850. }
  851. std::string value(p.first, p.second);
  852. std::string::iterator filenameLast;
  853. if(value[0] == '\'' || value[0] == '"') {
  854. char qc = *value.begin();
  855. for(filenameLast = value.begin()+1;
  856. filenameLast != value.end() && *filenameLast != qc;
  857. ++filenameLast);
  858. } else {
  859. filenameLast = value.end();
  860. }
  861. static const std::string TRIMMED("\r\n\t '\"");
  862. value = percentDecode(value.begin(), filenameLast);
  863. value = strip(value, TRIMMED);
  864. value.erase(std::remove(value.begin(), value.end(), '\\'), value.end());
  865. if(!detectDirTraversal(value) && value.find("/") == std::string::npos) {
  866. filename = value;
  867. }
  868. // continue because there is a chance we can find filename*=...
  869. }
  870. }
  871. return filename;
  872. }
  873. std::string randomAlpha(size_t length, const RandomizerHandle& randomizer) {
  874. static const char randomChars[] =
  875. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  876. std::string str;
  877. for(size_t i = 0; i < length; ++i) {
  878. size_t index = randomizer->getRandomNumber(sizeof(randomChars)-1);
  879. str += randomChars[index];
  880. }
  881. return str;
  882. }
  883. std::string toUpper(const std::string& src) {
  884. std::string temp = src;
  885. std::transform(temp.begin(), temp.end(), temp.begin(), toUpperChar);
  886. return temp;
  887. }
  888. std::string toLower(const std::string& src) {
  889. std::string temp = src;
  890. std::transform(temp.begin(), temp.end(), temp.begin(), toLowerChar);
  891. return temp;
  892. }
  893. void uppercase(std::string& s)
  894. {
  895. std::transform(s.begin(), s.end(), s.begin(), toUpperChar);
  896. }
  897. void lowercase(std::string& s)
  898. {
  899. std::transform(s.begin(), s.end(), s.begin(), toLowerChar);
  900. }
  901. char toUpperChar(char c)
  902. {
  903. if('a' <= c && c <= 'z') {
  904. c += 'A'-'a';
  905. }
  906. return c;
  907. }
  908. char toLowerChar(char c)
  909. {
  910. if('A' <= c && c <= 'Z') {
  911. c += 'a'-'A';
  912. }
  913. return c;
  914. }
  915. bool isNumericHost(const std::string& name)
  916. {
  917. struct addrinfo hints;
  918. struct addrinfo* res;
  919. memset(&hints, 0, sizeof(hints));
  920. hints.ai_family = AF_UNSPEC;
  921. hints.ai_flags = AI_NUMERICHOST;
  922. if(getaddrinfo(name.c_str(), 0, &hints, &res)) {
  923. return false;
  924. }
  925. freeaddrinfo(res);
  926. return true;
  927. }
  928. void setGlobalSignalHandler(int sig, void (*handler)(int), int flags) {
  929. #ifdef HAVE_SIGACTION
  930. struct sigaction sigact;
  931. sigact.sa_handler = handler;
  932. sigact.sa_flags = flags;
  933. sigemptyset(&sigact.sa_mask);
  934. sigaction(sig, &sigact, NULL);
  935. #else
  936. signal(sig, handler);
  937. #endif // HAVE_SIGACTION
  938. }
  939. std::string getHomeDir()
  940. {
  941. const char* p = getenv("HOME");
  942. if(p) {
  943. return p;
  944. } else {
  945. #ifdef __MINGW32__
  946. p = getenv("USERPROFILE");
  947. if(p) {
  948. return p;
  949. } else {
  950. p = getenv("HOMEDRIVE");
  951. if(p) {
  952. std::string homeDir = p;
  953. p = getenv("HOMEPATH");
  954. if(p) {
  955. homeDir += p;
  956. return homeDir;
  957. }
  958. }
  959. }
  960. #elif HAVE_PWD_H
  961. passwd* pw = getpwuid(geteuid());
  962. if(pw && pw->pw_dir) {
  963. return pw->pw_dir;
  964. }
  965. #endif // HAVE_PWD_H
  966. return A2STR::NIL;
  967. }
  968. }
  969. int64_t getRealSize(const std::string& sizeWithUnit)
  970. {
  971. std::string::size_type p = sizeWithUnit.find_first_of("KM");
  972. std::string size;
  973. int32_t mult = 1;
  974. if(p == std::string::npos) {
  975. size = sizeWithUnit;
  976. } else {
  977. if(sizeWithUnit[p] == 'K') {
  978. mult = 1024;
  979. } else if(sizeWithUnit[p] == 'M') {
  980. mult = 1024*1024;
  981. }
  982. size.assign(sizeWithUnit.begin(), sizeWithUnit.begin()+p);
  983. }
  984. int64_t v = parseLLInt(size);
  985. if(v < 0) {
  986. throw DL_ABORT_EX(fmt("Negative value detected: %s", sizeWithUnit.c_str()));
  987. } else if(INT64_MAX/mult < v) {
  988. throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  989. "overflow/underflow"));
  990. }
  991. return v*mult;
  992. }
  993. std::string abbrevSize(int64_t size)
  994. {
  995. if(size < 1024) {
  996. return itos(size, true);
  997. }
  998. static const char units[] = { 'K', 'M' };
  999. size_t i = 0;
  1000. int r = size&0x3ffu;
  1001. size >>= 10;
  1002. for(; i < sizeof(units)-1 && size >= 1024; ++i) {
  1003. r = size&0x3ffu;
  1004. size >>= 10;
  1005. }
  1006. std::string result = itos(size, true);
  1007. result += fmt(".%d%ci", r*10/1024, units[i]);
  1008. return result;
  1009. }
  1010. void sleep(long seconds) {
  1011. #ifdef HAVE_SLEEP
  1012. ::sleep(seconds);
  1013. #elif defined(HAVE_USLEEP)
  1014. ::usleep(seconds * 1000000);
  1015. #elif defined(HAVE_WINSOCK2_H)
  1016. ::Sleep(seconds * 1000);
  1017. #else
  1018. #error no sleep function is available (nanosleep?)
  1019. #endif
  1020. }
  1021. void usleep(long microseconds) {
  1022. #ifdef HAVE_USLEEP
  1023. ::usleep(microseconds);
  1024. #elif defined(HAVE_WINSOCK2_H)
  1025. LARGE_INTEGER current, freq, end;
  1026. static enum {GET_FREQUENCY, GET_MICROSECONDS, SKIP_MICROSECONDS} state = GET_FREQUENCY;
  1027. if (state == GET_FREQUENCY) {
  1028. if (QueryPerformanceFrequency(&freq))
  1029. state = GET_MICROSECONDS;
  1030. else
  1031. state = SKIP_MICROSECONDS;
  1032. }
  1033. long msec = microseconds / 1000;
  1034. microseconds %= 1000;
  1035. if (state == GET_MICROSECONDS && microseconds) {
  1036. QueryPerformanceCounter(&end);
  1037. end.QuadPart += (freq.QuadPart * microseconds) / 1000000;
  1038. while (QueryPerformanceCounter(&current) && (current.QuadPart <= end.QuadPart))
  1039. /* noop */ ;
  1040. }
  1041. if (msec)
  1042. Sleep(msec);
  1043. #else
  1044. #error no usleep function is available (nanosleep?)
  1045. #endif
  1046. }
  1047. unsigned int alphaToNum(const std::string& alphabets)
  1048. {
  1049. if(alphabets.empty()) {
  1050. return 0;
  1051. }
  1052. char base;
  1053. if(islower(alphabets[0])) {
  1054. base = 'a';
  1055. } else {
  1056. base = 'A';
  1057. }
  1058. uint64_t num = 0;
  1059. for(size_t i = 0, eoi = alphabets.size(); i < eoi; ++i) {
  1060. unsigned int v = alphabets[i]-base;
  1061. num = num*26+v;
  1062. if(num > UINT32_MAX) {
  1063. return 0;
  1064. }
  1065. }
  1066. return num;
  1067. }
  1068. void mkdirs(const std::string& dirpath)
  1069. {
  1070. File dir(dirpath);
  1071. if(!dir.mkdirs()) {
  1072. int errNum = errno;
  1073. if(!dir.isDir()) {
  1074. throw DL_ABORT_EX3
  1075. (errNum,
  1076. fmt(EX_MAKE_DIR, dir.getPath().c_str(),
  1077. safeStrerror(errNum).c_str()),
  1078. error_code::DIR_CREATE_ERROR);
  1079. }
  1080. }
  1081. }
  1082. void convertBitfield(BitfieldMan* dest, const BitfieldMan* src)
  1083. {
  1084. size_t numBlock = dest->countBlock();
  1085. for(size_t index = 0; index < numBlock; ++index) {
  1086. if(src->isBitSetOffsetRange((off_t)index*dest->getBlockLength(),
  1087. dest->getBlockLength())) {
  1088. dest->setBit(index);
  1089. }
  1090. }
  1091. }
  1092. std::string toString(const BinaryStreamHandle& binaryStream)
  1093. {
  1094. std::stringstream strm;
  1095. char data[2048];
  1096. while(1) {
  1097. int32_t dataLength = binaryStream->readData
  1098. (reinterpret_cast<unsigned char*>(data), sizeof(data), strm.tellp());
  1099. strm.write(data, dataLength);
  1100. if(dataLength == 0) {
  1101. break;
  1102. }
  1103. }
  1104. return strm.str();
  1105. }
  1106. #ifdef HAVE_POSIX_MEMALIGN
  1107. /**
  1108. * In linux 2.6, alignment and size should be a multiple of 512.
  1109. */
  1110. void* allocateAlignedMemory(size_t alignment, size_t size)
  1111. {
  1112. void* buffer;
  1113. int res;
  1114. if((res = posix_memalign(&buffer, alignment, size)) != 0) {
  1115. throw FATAL_EXCEPTION
  1116. (fmt("Error in posix_memalign: %s",
  1117. util::safeStrerror(res).c_str()));
  1118. }
  1119. return buffer;
  1120. }
  1121. #endif // HAVE_POSIX_MEMALIGN
  1122. std::pair<std::string, uint16_t>
  1123. getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len)
  1124. {
  1125. char host[NI_MAXHOST];
  1126. char service[NI_MAXSERV];
  1127. int s = getnameinfo(sockaddr, len, host, NI_MAXHOST, service, NI_MAXSERV,
  1128. NI_NUMERICHOST|NI_NUMERICSERV);
  1129. if(s != 0) {
  1130. throw DL_ABORT_EX(fmt("Failed to get hostname and port. cause: %s",
  1131. gai_strerror(s)));
  1132. }
  1133. return std::pair<std::string, uint16_t>(host, atoi(service)); // TODO
  1134. }
  1135. std::string htmlEscape(const std::string& src)
  1136. {
  1137. std::string dest;
  1138. for(std::string::const_iterator i = src.begin(), eoi = src.end();
  1139. i != eoi; ++i) {
  1140. char ch = *i;
  1141. if(ch == '<') {
  1142. dest += "&lt;";
  1143. } else if(ch == '>') {
  1144. dest += "&gt;";
  1145. } else if(ch == '&') {
  1146. dest += "&amp;";
  1147. } else if(ch == '\'') {
  1148. dest += "&#39;";
  1149. } else if(ch == '"') {
  1150. dest += "&quot;";
  1151. } else {
  1152. dest += ch;
  1153. }
  1154. }
  1155. return dest;
  1156. }
  1157. std::pair<size_t, std::string>
  1158. parseIndexPath(const std::string& line)
  1159. {
  1160. std::pair<Scip, Scip> p;
  1161. divide(p, line.begin(), line.end(), '=');
  1162. size_t index = parseUInt(std::string(p.first.first, p.first.second));
  1163. if(p.second.first == p.second.second) {
  1164. throw DL_ABORT_EX(fmt("Path with index=%u is empty.",
  1165. static_cast<unsigned int>(index)));
  1166. }
  1167. return std::make_pair(index, std::string(p.second.first, p.second.second));
  1168. }
  1169. std::vector<std::pair<size_t, std::string> > createIndexPaths(std::istream& i)
  1170. {
  1171. std::vector<std::pair<size_t, std::string> > indexPaths;
  1172. std::string line;
  1173. while(getline(i, line)) {
  1174. indexPaths.push_back(parseIndexPath(line));
  1175. }
  1176. return indexPaths;
  1177. }
  1178. namespace {
  1179. void generateRandomDataRandom(unsigned char* data, size_t length)
  1180. {
  1181. const SharedHandle<SimpleRandomizer>& rd = SimpleRandomizer::getInstance();
  1182. for(size_t i = 0; i < length; ++i) {
  1183. data[i] = static_cast<unsigned long>(rd->getRandomNumber(256));
  1184. }
  1185. }
  1186. } // namespace
  1187. namespace {
  1188. void generateRandomDataUrandom
  1189. (unsigned char* data, size_t length, std::ifstream& devUrand)
  1190. {
  1191. devUrand.read(reinterpret_cast<char*>(data), length);
  1192. }
  1193. } // namespace
  1194. void generateRandomData(unsigned char* data, size_t length)
  1195. {
  1196. #ifdef __MINGW32__
  1197. generateRandomDataRandom(data, length);
  1198. #else // !__MINGW32__
  1199. static int method = -1;
  1200. static std::ifstream devUrand;
  1201. if(method == 0) {
  1202. generateRandomDataUrandom(data, length, devUrand);
  1203. } else if(method == 1) {
  1204. generateRandomDataRandom(data, length);
  1205. } else {
  1206. devUrand.open("/dev/urandom");
  1207. if(devUrand) {
  1208. method = 0;
  1209. } else {
  1210. method = 1;
  1211. }
  1212. generateRandomData(data, length);
  1213. }
  1214. #endif // !__MINGW32__
  1215. }
  1216. bool saveAs
  1217. (const std::string& filename, const std::string& data, bool overwrite)
  1218. {
  1219. if(!overwrite && File(filename).exists()) {
  1220. return false;
  1221. }
  1222. std::string tempFilename = filename;
  1223. tempFilename += "__temp";
  1224. {
  1225. BufferedFile fp(tempFilename, BufferedFile::WRITE);
  1226. if(!fp) {
  1227. return false;
  1228. }
  1229. if(fp.write(data.data(), data.size()) != data.size()) {
  1230. return false;
  1231. }
  1232. if(fp.close() == EOF) {
  1233. return false;
  1234. }
  1235. }
  1236. return File(tempFilename).renameTo(filename);
  1237. }
  1238. std::string applyDir(const std::string& dir, const std::string& relPath)
  1239. {
  1240. std::string s;
  1241. if(dir.empty()) {
  1242. s = "./";
  1243. s += relPath;
  1244. } else {
  1245. s = dir;
  1246. if(dir == "/") {
  1247. s += relPath;
  1248. } else {
  1249. s += "/";
  1250. s += relPath;
  1251. }
  1252. }
  1253. #ifdef __MINGW32__
  1254. for(std::string::iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
  1255. if(*i == '\\') {
  1256. *i = '/';
  1257. }
  1258. }
  1259. #endif // __MINGW32__
  1260. return s;
  1261. }
  1262. std::string fixTaintedBasename(const std::string& src)
  1263. {
  1264. static std::string SLASH_REP = "%2F";
  1265. return escapePath(replace(src, A2STR::SLASH_C, SLASH_REP));
  1266. }
  1267. void generateRandomKey(unsigned char* key)
  1268. {
  1269. #ifdef ENABLE_MESSAGE_DIGEST
  1270. unsigned char bytes[40];
  1271. generateRandomData(bytes, sizeof(bytes));
  1272. message_digest::digest(key, 20, MessageDigest::sha1(), bytes, sizeof(bytes));
  1273. #else // !ENABLE_MESSAGE_DIGEST
  1274. generateRandomData(key, 20);
  1275. #endif // !ENABLE_MESSAGE_DIGEST
  1276. }
  1277. // Returns true is given numeric ipv4addr is in Private Address Space.
  1278. //
  1279. // From Section.3 RFC1918
  1280. // 10.0.0.0 - 10.255.255.255 (10/8 prefix)
  1281. // 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
  1282. // 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
  1283. bool inPrivateAddress(const std::string& ipv4addr)
  1284. {
  1285. if(util::startsWith(ipv4addr, "10.") ||
  1286. util::startsWith(ipv4addr, "192.168.")) {
  1287. return true;
  1288. }
  1289. if(util::startsWith(ipv4addr, "172.")) {
  1290. for(int i = 16; i <= 31; ++i) {
  1291. std::string t(fmt("%d.", i));
  1292. if(util::startsWith(ipv4addr.begin()+4, ipv4addr.end(),
  1293. t.begin(), t.end())) {
  1294. return true;
  1295. }
  1296. }
  1297. }
  1298. return false;
  1299. }
  1300. bool detectDirTraversal(const std::string& s)
  1301. {
  1302. if(s.empty()) {
  1303. return false;
  1304. }
  1305. for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
  1306. unsigned char c = *i;
  1307. if(in(c, 0x00u, 0x1fu) || c == 0x7fu) {
  1308. return true;
  1309. }
  1310. }
  1311. return s == "." || s == ".." || s[0] == '/' ||
  1312. util::startsWith(s, "./") || util::startsWith(s, "../") ||
  1313. s.find("/../") != std::string::npos ||
  1314. s.find("/./") != std::string::npos ||
  1315. s[s.size()-1] == '/' ||
  1316. util::endsWith(s, "/.") || util::endsWith(s, "/..");
  1317. }
  1318. std::string escapePath(const std::string& s)
  1319. {
  1320. // We don't escape '/' because we use it as a path separator.
  1321. #ifdef __MINGW32__
  1322. static const char WIN_INVALID_PATH_CHARS[] =
  1323. { '"', '*', ':', '<', '>', '?', '\\', '|' };
  1324. #endif // __MINGW32__
  1325. std::string d;
  1326. for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
  1327. unsigned char c = *i;
  1328. if(in(c, 0x00u, 0x1fu) || c == 0x7fu
  1329. #ifdef __MINGW32__
  1330. || std::find(vbegin(WIN_INVALID_PATH_CHARS),
  1331. vend(WIN_INVALID_PATH_CHARS),
  1332. c) != vend(WIN_INVALID_PATH_CHARS)
  1333. #endif // __MINGW32__
  1334. ){
  1335. d += fmt("%%%02X", c);
  1336. } else {
  1337. d += *i;
  1338. }
  1339. }
  1340. return d;
  1341. }
  1342. bool inSameCidrBlock
  1343. (const std::string& ip1, const std::string& ip2, size_t bits)
  1344. {
  1345. unsigned char s1[16], s2[16];
  1346. size_t len1, len2;
  1347. if((len1 = net::getBinAddr(s1, ip1)) == 0 ||
  1348. (len2 = net::getBinAddr(s2, ip2)) == 0 ||
  1349. len1 != len2) {
  1350. return false;
  1351. }
  1352. if(bits == 0) {
  1353. return true;
  1354. }
  1355. if(bits > 8*len1) {
  1356. bits = 8*len1;
  1357. }
  1358. int last = (bits-1)/8;
  1359. for(int i = 0; i < last; ++i) {
  1360. if(s1[i] != s2[i]) {
  1361. return false;
  1362. }
  1363. }
  1364. unsigned char mask = bitfield::lastByteMask(bits);
  1365. return (s1[last] & mask) == (s2[last] & mask);
  1366. }
  1367. void removeMetalinkContentTypes(const SharedHandle<RequestGroup>& group)
  1368. {
  1369. removeMetalinkContentTypes(group.get());
  1370. }
  1371. void removeMetalinkContentTypes(RequestGroup* group)
  1372. {
  1373. for(std::vector<std::string>::const_iterator i =
  1374. DownloadHandlerConstants::getMetalinkContentTypes().begin(),
  1375. eoi = DownloadHandlerConstants::getMetalinkContentTypes().end();
  1376. i != eoi; ++i) {
  1377. group->removeAcceptType(*i);
  1378. }
  1379. }
  1380. namespace {
  1381. void executeHook
  1382. (const std::string& command,
  1383. a2_gid_t gid,
  1384. size_t numFiles,
  1385. const std::string& firstFilename)
  1386. {
  1387. const std::string gidStr = util::itos(gid);
  1388. const std::string numFilesStr = util::uitos(numFiles);
  1389. #ifndef __MINGW32__
  1390. A2_LOG_INFO(fmt("Executing user command: %s %s %s %s",
  1391. command.c_str(),
  1392. gidStr.c_str(),
  1393. numFilesStr.c_str(),
  1394. firstFilename.c_str()));
  1395. pid_t cpid = fork();
  1396. if(cpid == -1) {
  1397. A2_LOG_ERROR("fork() failed. Cannot execute user command.");
  1398. } else if(cpid == 0) {
  1399. execl(command.c_str(),
  1400. command.c_str(),
  1401. gidStr.c_str(),
  1402. numFilesStr.c_str(),
  1403. firstFilename.c_str(),
  1404. reinterpret_cast<char*>(0));
  1405. perror(("Could not execute user command: "+command).c_str());
  1406. exit(EXIT_FAILURE);
  1407. }
  1408. #else
  1409. PROCESS_INFORMATION pi;
  1410. STARTUPINFO si;
  1411. memset(&si, 0, sizeof (si));
  1412. si.cb = sizeof(STARTUPINFO);
  1413. memset(&pi, 0, sizeof (pi));
  1414. bool batch = util::iendsWith(command, ".bat");
  1415. std::string cmdline;
  1416. std::string cmdexe;
  1417. if(batch) {
  1418. const char* p = getenv("windir");
  1419. if(p) {
  1420. cmdexe = p;
  1421. cmdexe += "\\system32\\cmd.exe";
  1422. } else {
  1423. A2_LOG_INFO("Failed to get windir environment variable."
  1424. " Executing batch file will fail.");
  1425. // TODO Might be useless.
  1426. cmdexe = "cmd.exe";
  1427. }
  1428. cmdline += "/C \"";
  1429. }
  1430. cmdline += "\"";
  1431. cmdline += command;
  1432. cmdline += "\"";
  1433. cmdline += " ";
  1434. cmdline += gidStr;
  1435. cmdline += " ";
  1436. cmdline += numFilesStr;
  1437. cmdline += " \"";
  1438. cmdline += firstFilename;
  1439. cmdline += "\"";
  1440. if(batch) {
  1441. cmdline += "\"";
  1442. }
  1443. A2_LOG_INFO(fmt("Executing user command: %s", cmdline.c_str()));
  1444. DWORD rc = CreateProcess(
  1445. batch ? cmdexe.c_str() : NULL,
  1446. (LPSTR)cmdline.c_str(),
  1447. NULL,
  1448. NULL,
  1449. true,
  1450. NULL,
  1451. NULL,
  1452. 0,
  1453. &si,
  1454. &pi);
  1455. if(!rc) {
  1456. A2_LOG_ERROR("CreateProcess() failed. Cannot execute user command.");
  1457. }
  1458. #endif
  1459. }
  1460. } // namespace
  1461. void executeHookByOptName
  1462. (const SharedHandle<RequestGroup>& group, const Option* option,
  1463. const Pref* pref)
  1464. {
  1465. executeHookByOptName(group.get(), option, pref);
  1466. }
  1467. void executeHookByOptName
  1468. (const RequestGroup* group, const Option* option, const Pref* pref)
  1469. {
  1470. const std::string& cmd = option->get(pref);
  1471. if(!cmd.empty()) {
  1472. const SharedHandle<DownloadContext> dctx = group->getDownloadContext();
  1473. std::string firstFilename;
  1474. size_t numFiles = 0;
  1475. if(!group->inMemoryDownload()) {
  1476. SharedHandle<FileEntry> file = dctx->getFirstRequestedFileEntry();
  1477. if(file) {
  1478. firstFilename = file->getPath();
  1479. }
  1480. numFiles = dctx->countRequestedFileEntry();
  1481. }
  1482. executeHook(cmd, group->getGID(), numFiles, firstFilename);
  1483. }
  1484. }
  1485. std::string createSafePath
  1486. (const std::string& dir, const std::string& filename)
  1487. {
  1488. return util::applyDir
  1489. (dir,
  1490. util::isUtf8(filename)?
  1491. util::fixTaintedBasename(filename):
  1492. util::escapePath(util::percentEncode(filename)));
  1493. }
  1494. std::string encodeNonUtf8(const std::string& s)
  1495. {
  1496. return util::isUtf8(s)?s:util::percentEncode(s);
  1497. }
  1498. std::string makeString(const char* str)
  1499. {
  1500. if(str) {
  1501. return str;
  1502. } else {
  1503. return A2STR::NIL;
  1504. }
  1505. }
  1506. std::string safeStrerror(int errNum)
  1507. {
  1508. return makeString(strerror(errNum));
  1509. }
  1510. bool noProxyDomainMatch
  1511. (const std::string& hostname,
  1512. const std::string& domain)
  1513. {
  1514. if(!domain.empty() && domain[0] == '.' && !util::isNumericHost(hostname)) {
  1515. return util::endsWith(hostname, domain);
  1516. } else {
  1517. return hostname == domain;
  1518. }
  1519. }
  1520. bool tlsHostnameMatch(const std::string& pattern, const std::string& hostname)
  1521. {
  1522. std::string::const_iterator ptWildcard = std::find(pattern.begin(),
  1523. pattern.end(),
  1524. '*');
  1525. if(ptWildcard == pattern.end()) {
  1526. return strieq(pattern.begin(), pattern.end(),
  1527. hostname.begin(), hostname.end());
  1528. }
  1529. std::string::const_iterator ptLeftLabelEnd = std::find(pattern.begin(),
  1530. pattern.end(),
  1531. '.');
  1532. bool wildcardEnabled = true;
  1533. // Do case-insensitive match. At least 2 dots are required to enable
  1534. // wildcard match. Also wildcard must be in the left-most label.
  1535. // Don't attempt to match a presented identifier where the wildcard
  1536. // character is embedded within an A-label.
  1537. if(ptLeftLabelEnd == pattern.end() ||
  1538. std::find(ptLeftLabelEnd+1, pattern.end(), '.') == pattern.end() ||
  1539. ptLeftLabelEnd < ptWildcard ||
  1540. istartsWith(pattern, "xn--")) {
  1541. wildcardEnabled = false;
  1542. }
  1543. if(!wildcardEnabled) {
  1544. return strieq(pattern.begin(), pattern.end(),
  1545. hostname.begin(), hostname.end());
  1546. }
  1547. std::string::const_iterator hnLeftLabelEnd = std::find(hostname.begin(),
  1548. hostname.end(),
  1549. '.');
  1550. if(!strieq(ptLeftLabelEnd, pattern.end(), hnLeftLabelEnd, hostname.end())) {
  1551. return false;
  1552. }
  1553. // Perform wildcard match. Here '*' must match at least one
  1554. // character.
  1555. if(hnLeftLabelEnd - hostname.begin() < ptLeftLabelEnd - pattern.begin()) {
  1556. return false;
  1557. }
  1558. return
  1559. istartsWith(hostname.begin(), hnLeftLabelEnd,
  1560. pattern.begin(), ptWildcard) &&
  1561. iendsWith(hostname.begin(), hnLeftLabelEnd,
  1562. ptWildcard+1, ptLeftLabelEnd);
  1563. }
  1564. bool startsWith(const std::string& a, const char* b)
  1565. {
  1566. return startsWith(a.begin(), a.end(), b);
  1567. }
  1568. bool startsWith(const std::string& a, const std::string& b)
  1569. {
  1570. return startsWith(a.begin(), a.end(), b.begin(), b.end());
  1571. }
  1572. bool istartsWith(const std::string& a, const char* b)
  1573. {
  1574. return istartsWith(a.begin(), a.end(), b);
  1575. }
  1576. bool endsWith(const std::string& a, const char* b)
  1577. {
  1578. return endsWith(a.begin(), a.end(), b, b+strlen(b));
  1579. }
  1580. bool endsWith(const std::string& a, const std::string& b)
  1581. {
  1582. return endsWith(a.begin(), a.end(), b.begin(), b.end());
  1583. }
  1584. bool iendsWith(const std::string& a, const char* b)
  1585. {
  1586. return iendsWith(a.begin(), a.end(), b, b+strlen(b));
  1587. }
  1588. bool iendsWith(const std::string& a, const std::string& b)
  1589. {
  1590. return iendsWith(a.begin(), a.end(), b.begin(), b.end());
  1591. }
  1592. } // namespace util
  1593. } // namespace aria2