util.cc 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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. bool isLws(const char c)
  286. {
  287. return c == ' ' || c == '\t';
  288. }
  289. bool isCRLF(const char c)
  290. {
  291. return c == '\r' || c == '\n';
  292. }
  293. namespace {
  294. bool isUtf8Tail(unsigned char ch)
  295. {
  296. return in(ch, 0x80u, 0xbfu);
  297. }
  298. } // namespace
  299. bool isUtf8(const std::string& str)
  300. {
  301. for(std::string::const_iterator s = str.begin(), eos = str.end(); s != eos;
  302. ++s) {
  303. unsigned char firstChar = *s;
  304. // See ABNF in http://tools.ietf.org/search/rfc3629#section-4
  305. if(in(firstChar, 0x20u, 0x7eu) ||
  306. firstChar == 0x08u || // \b
  307. firstChar == 0x09u || // \t
  308. firstChar == 0x0au || // \n
  309. firstChar == 0x0cu || // \f
  310. firstChar == 0x0du // \r
  311. ) {
  312. // UTF8-1 (without ctrl chars)
  313. } else if(in(firstChar, 0xc2u, 0xdfu)) {
  314. // UTF8-2
  315. if(++s == eos || !isUtf8Tail(*s)) {
  316. return false;
  317. }
  318. } else if(0xe0u == firstChar) {
  319. // UTF8-3
  320. if(++s == eos || !in(static_cast<unsigned char>(*s), 0xa0u, 0xbfu) ||
  321. ++s == eos || !isUtf8Tail(*s)) {
  322. return false;
  323. }
  324. } else if(in(firstChar, 0xe1u, 0xecu) || in(firstChar, 0xeeu, 0xefu)) {
  325. // UTF8-3
  326. if(++s == eos || !isUtf8Tail(*s) ||
  327. ++s == eos || !isUtf8Tail(*s)) {
  328. return false;
  329. }
  330. } else if(0xedu == firstChar) {
  331. // UTF8-3
  332. if(++s == eos || !in(static_cast<unsigned char>(*s), 0x80u, 0x9fu) ||
  333. ++s == eos || !isUtf8Tail(*s)) {
  334. return false;
  335. }
  336. } else if(0xf0u == firstChar) {
  337. // UTF8-4
  338. if(++s == eos || !in(static_cast<unsigned char>(*s), 0x90u, 0xbfu) ||
  339. ++s == eos || !isUtf8Tail(*s) ||
  340. ++s == eos || !isUtf8Tail(*s)) {
  341. return false;
  342. }
  343. } else if(in(firstChar, 0xf1u, 0xf3u)) {
  344. // UTF8-4
  345. if(++s == eos || !isUtf8Tail(*s) ||
  346. ++s == eos || !isUtf8Tail(*s) ||
  347. ++s == eos || !isUtf8Tail(*s)) {
  348. return false;
  349. }
  350. } else if(0xf4u == firstChar) {
  351. // UTF8-4
  352. if(++s == eos || !in(static_cast<unsigned char>(*s), 0x80u, 0x8fu) ||
  353. ++s == eos || !isUtf8Tail(*s) ||
  354. ++s == eos || !isUtf8Tail(*s)) {
  355. return false;
  356. }
  357. } else {
  358. return false;
  359. }
  360. }
  361. return true;
  362. }
  363. std::string percentEncode(const unsigned char* target, size_t len)
  364. {
  365. std::string dest;
  366. for(size_t i = 0; i < len; ++i) {
  367. if(inRFC3986UnreservedChars(target[i])) {
  368. dest += target[i];
  369. } else {
  370. dest.append(fmt("%%%02X", target[i]));
  371. }
  372. }
  373. return dest;
  374. }
  375. std::string percentEncode(const std::string& target)
  376. {
  377. return percentEncode(reinterpret_cast<const unsigned char*>(target.c_str()),
  378. target.size());
  379. }
  380. std::string percentEncodeMini(const std::string& src)
  381. {
  382. std::string result;
  383. for(std::string::const_iterator i = src.begin(), eoi = src.end(); i != eoi;
  384. ++i) {
  385. // Non-Printable ASCII and non-ASCII chars + some ASCII chars.
  386. unsigned char c = *i;
  387. if(in(c, 0x00u, 0x20u) || c >= 0x7fu ||
  388. // Chromium escapes following characters. Firefox4 escapes
  389. // more.
  390. c == '"' || c == '<' || c == '>') {
  391. result += fmt("%%%02X", c);
  392. } else {
  393. result += c;
  394. }
  395. }
  396. return result;
  397. }
  398. std::string torrentPercentEncode(const unsigned char* target, size_t len) {
  399. std::string dest;
  400. for(size_t i = 0; i < len; ++i) {
  401. if(isAlpha(target[i]) || isDigit(target[i])) {
  402. dest += target[i];
  403. } else {
  404. dest.append(fmt("%%%02X", target[i]));
  405. }
  406. }
  407. return dest;
  408. }
  409. std::string torrentPercentEncode(const std::string& target)
  410. {
  411. return torrentPercentEncode
  412. (reinterpret_cast<const unsigned char*>(target.c_str()), target.size());
  413. }
  414. std::string percentDecode
  415. (std::string::const_iterator first, std::string::const_iterator last)
  416. {
  417. std::string result;
  418. for(; first != last; ++first) {
  419. if(*first == '%') {
  420. if(first+1 != last && first+2 != last &&
  421. isHexDigit(*(first+1)) && isHexDigit(*(first+2))) {
  422. result += parseInt(std::string(first+1, first+3), 16);
  423. first += 2;
  424. } else {
  425. result += *first;
  426. }
  427. } else {
  428. result += *first;
  429. }
  430. }
  431. return result;
  432. }
  433. std::string toHex(const unsigned char* src, size_t len) {
  434. std::string out(len*2, '\0');
  435. std::string::iterator o = out.begin();
  436. const unsigned char* last = src+len;
  437. for(const unsigned char* i = src; i != last; ++i) {
  438. *o = (*i >> 4);
  439. *(o+1) = (*i)&0x0fu;
  440. for(int j = 0; j < 2; ++j) {
  441. if(*o < 10) {
  442. *o += '0';
  443. } else {
  444. *o += 'a'-10;
  445. }
  446. ++o;
  447. }
  448. }
  449. return out;
  450. }
  451. std::string toHex(const char* src, size_t len)
  452. {
  453. return toHex(reinterpret_cast<const unsigned char*>(src), len);
  454. }
  455. std::string toHex(const std::string& src)
  456. {
  457. return toHex(reinterpret_cast<const unsigned char*>(src.c_str()), src.size());
  458. }
  459. unsigned int hexCharToUInt(unsigned char ch)
  460. {
  461. if('a' <= ch && ch <= 'f') {
  462. ch -= 'a';
  463. ch += 10;
  464. } else if('A' <= ch && ch <= 'F') {
  465. ch -= 'A';
  466. ch += 10;
  467. } else if('0' <= ch && ch <= '9') {
  468. ch -= '0';
  469. } else {
  470. ch = 255;
  471. }
  472. return ch;
  473. }
  474. FILE* openFile(const std::string& filename, const std::string& mode) {
  475. FILE* file = fopen(filename.c_str(), mode.c_str());
  476. return file;
  477. }
  478. bool isPowerOf(int num, int base) {
  479. if(base <= 0) { return false; }
  480. if(base == 1) { return true; }
  481. while(num%base == 0) {
  482. num /= base;
  483. if(num == 1) {
  484. return true;
  485. }
  486. }
  487. return false;
  488. }
  489. std::string secfmt(time_t sec) {
  490. time_t tsec = sec;
  491. std::string str;
  492. if(sec >= 3600) {
  493. str = fmt("%" PRId64 "h", static_cast<int64_t>(sec/3600));
  494. sec %= 3600;
  495. }
  496. if(sec >= 60) {
  497. str += fmt("%dm", static_cast<int>(sec/60));
  498. sec %= 60;
  499. }
  500. if(sec || tsec == 0) {
  501. str += fmt("%ds", static_cast<int>(sec));
  502. }
  503. return str;
  504. }
  505. int getNum(const char* buf, int offset, size_t length) {
  506. char* temp = new char[length+1];
  507. memcpy(temp, buf+offset, length);
  508. temp[length] = '\0';
  509. int x = strtol(temp, 0, 10);
  510. delete [] temp;
  511. return x;
  512. }
  513. namespace {
  514. template<typename T, typename F>
  515. bool parseLong(T& res, F f, const std::string& s, int base)
  516. {
  517. if(s.empty()) {
  518. return false;
  519. }
  520. char* endptr;
  521. errno = 0;
  522. res = f(s.c_str(), &endptr, base);
  523. if(errno == ERANGE) {
  524. return false;
  525. }
  526. if(*endptr != '\0') {
  527. for(const char* i = endptr, *eoi = s.c_str()+s.size(); i < eoi; ++i) {
  528. if(!isspace(*i)) {
  529. return false;
  530. }
  531. }
  532. }
  533. return true;
  534. }
  535. } // namespace
  536. bool parseIntNoThrow(int32_t& res, const std::string& s, int base)
  537. {
  538. long int t;
  539. if(parseLong(t, strtol, s, base) &&
  540. t >= std::numeric_limits<int32_t>::min() &&
  541. t <= std::numeric_limits<int32_t>::max()) {
  542. res = t;
  543. return true;
  544. } else {
  545. return false;
  546. }
  547. }
  548. int32_t parseInt(const std::string& s, int base)
  549. {
  550. int32_t res;
  551. if(parseIntNoThrow(res, s, base)) {
  552. return res;
  553. } else {
  554. throw DL_ABORT_EX
  555. (fmt("Failed to convert string into 32bit signed integer. '%s'",
  556. s.c_str()));
  557. }
  558. }
  559. bool parseUIntNoThrow(uint32_t& res, const std::string& s, int base)
  560. {
  561. long int t;
  562. if(parseLong(t, strtol, s, base) &&
  563. t >= 0 &&
  564. t <= std::numeric_limits<int32_t>::max()) {
  565. res = t;
  566. return true;
  567. } else {
  568. return false;
  569. }
  570. }
  571. uint32_t parseUInt(const std::string& s, int base)
  572. {
  573. uint32_t res;
  574. if(parseUIntNoThrow(res, s, base)) {
  575. return res;
  576. } else {
  577. throw DL_ABORT_EX
  578. (fmt("Failed to convert string into 32bit unsigned integer. '%s'",
  579. s.c_str()));
  580. }
  581. }
  582. bool parseLLIntNoThrow(int64_t& res, const std::string& s, int base)
  583. {
  584. long long int t;
  585. if(parseLong(t, strtoll, s, base) &&
  586. t >= std::numeric_limits<int64_t>::min() &&
  587. t <= std::numeric_limits<int64_t>::max()) {
  588. res = t;
  589. return true;
  590. } else {
  591. return false;
  592. }
  593. }
  594. int64_t parseLLInt(const std::string& s, int base)
  595. {
  596. int64_t res;
  597. if(parseLLIntNoThrow(res, s, base)) {
  598. return res;
  599. } else {
  600. throw DL_ABORT_EX
  601. (fmt("Failed to convert string into 64bit signed integer. '%s'",
  602. s.c_str()));
  603. }
  604. }
  605. void parseIntSegments(SegList<int>& sgl, const std::string& src)
  606. {
  607. for(std::string::const_iterator i = src.begin(), eoi = src.end(); i != eoi;) {
  608. std::string::const_iterator j = std::find(i, eoi, ',');
  609. if(j == i) {
  610. ++i;
  611. continue;
  612. }
  613. std::string::const_iterator p = std::find(i, j, '-');
  614. if(p == j) {
  615. int a = parseInt(std::string(i, j));
  616. sgl.add(a, a+1);
  617. } else if(p == i || p+1 == j) {
  618. throw DL_ABORT_EX(fmt(MSG_INCOMPLETE_RANGE, std::string(i, j).c_str()));
  619. } else {
  620. int a = parseInt(std::string(i, p));
  621. int b = parseInt(std::string(p+1, j));
  622. sgl.add(a, b+1);
  623. }
  624. if(j == eoi) {
  625. break;
  626. }
  627. i = j+1;
  628. }
  629. }
  630. namespace {
  631. void computeHeadPieces
  632. (std::vector<size_t>& indexes,
  633. const std::vector<SharedHandle<FileEntry> >& fileEntries,
  634. size_t pieceLength,
  635. int64_t head)
  636. {
  637. if(head == 0) {
  638. return;
  639. }
  640. for(std::vector<SharedHandle<FileEntry> >::const_iterator fi =
  641. fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
  642. if((*fi)->getLength() == 0) {
  643. continue;
  644. }
  645. size_t lastIndex =
  646. ((*fi)->getOffset()+std::min(head, (*fi)->getLength())-1)/pieceLength;
  647. for(size_t index = (*fi)->getOffset()/pieceLength;
  648. index <= lastIndex; ++index) {
  649. indexes.push_back(index);
  650. }
  651. }
  652. }
  653. } // namespace
  654. namespace {
  655. void computeTailPieces
  656. (std::vector<size_t>& indexes,
  657. const std::vector<SharedHandle<FileEntry> >& fileEntries,
  658. size_t pieceLength,
  659. int64_t tail)
  660. {
  661. if(tail == 0) {
  662. return;
  663. }
  664. for(std::vector<SharedHandle<FileEntry> >::const_iterator fi =
  665. fileEntries.begin(), eoi = fileEntries.end(); fi != eoi; ++fi) {
  666. if((*fi)->getLength() == 0) {
  667. continue;
  668. }
  669. int64_t endOffset = (*fi)->getLastOffset();
  670. size_t fromIndex =
  671. (endOffset-1-(std::min(tail, (*fi)->getLength())-1))/pieceLength;
  672. for(size_t index = fromIndex; index <= (endOffset-1)/pieceLength;
  673. ++index) {
  674. indexes.push_back(index);
  675. }
  676. }
  677. }
  678. } // namespace
  679. void parsePrioritizePieceRange
  680. (std::vector<size_t>& result, const std::string& src,
  681. const std::vector<SharedHandle<FileEntry> >& fileEntries,
  682. size_t pieceLength,
  683. int64_t defaultSize)
  684. {
  685. std::vector<size_t> indexes;
  686. std::vector<Scip> parts;
  687. splitIter(src.begin(), src.end(), std::back_inserter(parts), ',', true);
  688. for(std::vector<Scip>::const_iterator i = parts.begin(),
  689. eoi = parts.end(); i != eoi; ++i) {
  690. if(util::streq((*i).first, (*i).second, "head")) {
  691. computeHeadPieces(indexes, fileEntries, pieceLength, defaultSize);
  692. } else if(util::startsWith((*i).first, (*i).second, "head=")) {
  693. std::string sizestr((*i).first+5, (*i).second);
  694. computeHeadPieces(indexes, fileEntries, pieceLength,
  695. std::max((int64_t)0, getRealSize(sizestr)));
  696. } else if(util::streq((*i).first, (*i).second, "tail")) {
  697. computeTailPieces(indexes, fileEntries, pieceLength, defaultSize);
  698. } else if(util::startsWith((*i).first, (*i).second, "tail=")) {
  699. std::string sizestr((*i).first+5, (*i).second);
  700. computeTailPieces(indexes, fileEntries, pieceLength,
  701. std::max((int64_t)0, getRealSize(sizestr)));
  702. } else {
  703. throw DL_ABORT_EX(fmt("Unrecognized token %s",
  704. std::string((*i).first, (*i).second).c_str()));
  705. }
  706. }
  707. std::sort(indexes.begin(), indexes.end());
  708. indexes.erase(std::unique(indexes.begin(), indexes.end()), indexes.end());
  709. result.insert(result.end(), indexes.begin(), indexes.end());
  710. }
  711. // Converts ISO/IEC 8859-1 string to UTF-8 string. If there is a
  712. // character not in ISO/IEC 8859-1, returns empty string.
  713. std::string iso8859ToUtf8(const std::string& src)
  714. {
  715. std::string dest;
  716. for(std::string::const_iterator itr = src.begin(), eoi = src.end();
  717. itr != eoi; ++itr) {
  718. unsigned char c = *itr;
  719. if(0xa0u <= c) {
  720. if(c <= 0xbfu) {
  721. dest += 0xc2u;
  722. } else {
  723. dest += 0xc3u;
  724. }
  725. dest += c&(~0x40u);
  726. } else if(0x80u <= c && c <= 0x9fu) {
  727. return A2STR::NIL;
  728. } else {
  729. dest += c;
  730. }
  731. }
  732. return dest;
  733. }
  734. namespace {
  735. template<typename OutputIterator>
  736. void parseParam(OutputIterator out, const std::string& header)
  737. {
  738. for(std::string::const_iterator i = header.begin(), eoi = header.end();
  739. i != eoi;) {
  740. std::string::const_iterator paramFirst = i;
  741. std::string::const_iterator paramLast = paramFirst;
  742. for(; paramLast != eoi && *paramLast != '=' && *paramLast != ';';
  743. ++paramLast);
  744. std::string param;
  745. if(paramLast == eoi || *paramLast == ';') {
  746. // No value, parmname only
  747. param.assign(paramFirst, paramLast);
  748. } else {
  749. for(; paramLast != eoi && *paramLast != '"' && *paramLast != ';';
  750. ++paramLast);
  751. if(paramLast != eoi && *paramLast == '"') {
  752. // quoted-string
  753. ++paramLast;
  754. for(; paramLast != eoi && *paramLast != '"'; ++paramLast);
  755. if(paramLast != eoi) {
  756. ++paramLast;
  757. }
  758. param.assign(paramFirst, paramLast);
  759. for(; paramLast != eoi && *paramLast != ';'; ++paramLast);
  760. } else {
  761. param.assign(paramFirst, paramLast);
  762. }
  763. }
  764. param = strip(param);
  765. *out++ = param;
  766. if(paramLast == eoi) {
  767. break;
  768. }
  769. i = paramLast;
  770. ++i;
  771. }
  772. }
  773. } // namespace
  774. std::string getContentDispositionFilename(const std::string& header)
  775. {
  776. static const char A2_KEYNAME[] = "filename";
  777. std::string filename;
  778. std::vector<std::string> params;
  779. parseParam(std::back_inserter(params), header);
  780. for(std::vector<std::string>::const_iterator i = params.begin(),
  781. eoi = params.end(); i != eoi; ++i) {
  782. const std::string& param = *i;
  783. if(!istartsWith(param, A2_KEYNAME) ||
  784. param.size() == sizeof(A2_KEYNAME)-1) {
  785. continue;
  786. }
  787. std::string::const_iterator markeritr = param.begin()+sizeof(A2_KEYNAME)-1;
  788. if(*markeritr == '*') {
  789. // See RFC2231 Section4 and draft-reschke-rfc2231-in-http.
  790. // Please note that this function doesn't do charset conversion
  791. // except that if iso-8859-1 is specified, it is converted to
  792. // utf-8.
  793. ++markeritr;
  794. for(; markeritr != param.end() && *markeritr == ' '; ++markeritr);
  795. if(markeritr == param.end() || *markeritr != '=') {
  796. continue;
  797. }
  798. std::vector<Scip> extValues;
  799. splitIter(markeritr+1, param.end(), std::back_inserter(extValues),
  800. '\'', true, true);
  801. if(extValues.size() != 3) {
  802. continue;
  803. }
  804. bool bad = false;
  805. for(std::string::const_iterator j = extValues[0].first,
  806. eoj = extValues[0].second; j != eoj; ++j) {
  807. // Since we first split parameter by ', we can safely assume
  808. // that ' is not included in charset.
  809. if(!inRFC2978MIMECharset(*j)) {
  810. bad = true;
  811. break;
  812. }
  813. }
  814. if(bad) {
  815. continue;
  816. }
  817. bad = false;
  818. for(std::string::const_iterator j = extValues[2].first,
  819. eoj = extValues[2].second; j != eoj; ++j){
  820. if(*j == '%') {
  821. if(j+1 != eoj && isHexDigit(*(j+1)) &&
  822. j+2 != eoj && isHexDigit(*(j+2))) {
  823. j += 2;
  824. } else {
  825. bad = true;
  826. break;
  827. }
  828. } else {
  829. if(*j == '*' || *j == '\'' || !inRFC2616HttpToken(*j)) {
  830. bad = true;
  831. break;
  832. }
  833. }
  834. }
  835. if(bad) {
  836. continue;
  837. }
  838. std::string value =
  839. percentDecode(extValues[2].first, extValues[2].second);
  840. if(util::strieq(extValues[0].first, extValues[0].second, "iso-8859-1")) {
  841. value = iso8859ToUtf8(value);
  842. }
  843. if(!detectDirTraversal(value) && value.find("/") == std::string::npos) {
  844. filename = value;
  845. }
  846. if(!filename.empty()) {
  847. break;
  848. }
  849. } else {
  850. for(; markeritr != param.end() && *markeritr == ' '; ++markeritr);
  851. if(markeritr == param.end() || markeritr+1 == param.end() ||
  852. *markeritr != '=') {
  853. continue;
  854. }
  855. Scip p = stripIter(markeritr+1, param.end());
  856. if(p.first == p.second) {
  857. continue;
  858. }
  859. std::string value(p.first, p.second);
  860. std::string::iterator filenameLast;
  861. if(value[0] == '\'' || value[0] == '"') {
  862. char qc = *value.begin();
  863. for(filenameLast = value.begin()+1;
  864. filenameLast != value.end() && *filenameLast != qc;
  865. ++filenameLast);
  866. } else {
  867. filenameLast = value.end();
  868. }
  869. static const std::string TRIMMED("\r\n\t '\"");
  870. std::pair<std::string::iterator, std::string::iterator> vi =
  871. util::stripIter(value.begin(), filenameLast, TRIMMED);
  872. value.assign(vi.first, vi.second);
  873. value.erase(std::remove(value.begin(), value.end(), '\\'), value.end());
  874. if(!detectDirTraversal(value) && value.find("/") == std::string::npos) {
  875. filename = value;
  876. }
  877. // continue because there is a chance we can find filename*=...
  878. }
  879. }
  880. return filename;
  881. }
  882. std::string randomAlpha(size_t length, const RandomizerHandle& randomizer) {
  883. static const char randomChars[] =
  884. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  885. std::string str;
  886. for(size_t i = 0; i < length; ++i) {
  887. size_t index = randomizer->getRandomNumber(sizeof(randomChars)-1);
  888. str += randomChars[index];
  889. }
  890. return str;
  891. }
  892. std::string toUpper(const std::string& src) {
  893. std::string temp = src;
  894. std::transform(temp.begin(), temp.end(), temp.begin(), toUpperChar);
  895. return temp;
  896. }
  897. std::string toLower(const std::string& src) {
  898. std::string temp = src;
  899. std::transform(temp.begin(), temp.end(), temp.begin(), toLowerChar);
  900. return temp;
  901. }
  902. void uppercase(std::string& s)
  903. {
  904. std::transform(s.begin(), s.end(), s.begin(), toUpperChar);
  905. }
  906. void lowercase(std::string& s)
  907. {
  908. std::transform(s.begin(), s.end(), s.begin(), toLowerChar);
  909. }
  910. char toUpperChar(char c)
  911. {
  912. if('a' <= c && c <= 'z') {
  913. c += 'A'-'a';
  914. }
  915. return c;
  916. }
  917. char toLowerChar(char c)
  918. {
  919. if('A' <= c && c <= 'Z') {
  920. c += 'a'-'A';
  921. }
  922. return c;
  923. }
  924. bool isNumericHost(const std::string& name)
  925. {
  926. struct addrinfo hints;
  927. struct addrinfo* res;
  928. memset(&hints, 0, sizeof(hints));
  929. hints.ai_family = AF_UNSPEC;
  930. hints.ai_flags = AI_NUMERICHOST;
  931. if(getaddrinfo(name.c_str(), 0, &hints, &res)) {
  932. return false;
  933. }
  934. freeaddrinfo(res);
  935. return true;
  936. }
  937. void setGlobalSignalHandler(int sig, void (*handler)(int), int flags) {
  938. #ifdef HAVE_SIGACTION
  939. struct sigaction sigact;
  940. sigact.sa_handler = handler;
  941. sigact.sa_flags = flags;
  942. sigemptyset(&sigact.sa_mask);
  943. sigaction(sig, &sigact, NULL);
  944. #else
  945. signal(sig, handler);
  946. #endif // HAVE_SIGACTION
  947. }
  948. std::string getHomeDir()
  949. {
  950. const char* p = getenv("HOME");
  951. if(p) {
  952. return p;
  953. } else {
  954. #ifdef __MINGW32__
  955. p = getenv("USERPROFILE");
  956. if(p) {
  957. return p;
  958. } else {
  959. p = getenv("HOMEDRIVE");
  960. if(p) {
  961. std::string homeDir = p;
  962. p = getenv("HOMEPATH");
  963. if(p) {
  964. homeDir += p;
  965. return homeDir;
  966. }
  967. }
  968. }
  969. #elif HAVE_PWD_H
  970. passwd* pw = getpwuid(geteuid());
  971. if(pw && pw->pw_dir) {
  972. return pw->pw_dir;
  973. }
  974. #endif // HAVE_PWD_H
  975. return A2STR::NIL;
  976. }
  977. }
  978. int64_t getRealSize(const std::string& sizeWithUnit)
  979. {
  980. std::string::size_type p = sizeWithUnit.find_first_of("KM");
  981. std::string size;
  982. int32_t mult = 1;
  983. if(p == std::string::npos) {
  984. size = sizeWithUnit;
  985. } else {
  986. if(sizeWithUnit[p] == 'K') {
  987. mult = 1024;
  988. } else if(sizeWithUnit[p] == 'M') {
  989. mult = 1024*1024;
  990. }
  991. size.assign(sizeWithUnit.begin(), sizeWithUnit.begin()+p);
  992. }
  993. int64_t v = parseLLInt(size);
  994. if(v < 0) {
  995. throw DL_ABORT_EX(fmt("Negative value detected: %s", sizeWithUnit.c_str()));
  996. } else if(INT64_MAX/mult < v) {
  997. throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  998. "overflow/underflow"));
  999. }
  1000. return v*mult;
  1001. }
  1002. std::string abbrevSize(int64_t size)
  1003. {
  1004. if(size < 1024) {
  1005. return itos(size, true);
  1006. }
  1007. static const char units[] = { 'K', 'M' };
  1008. size_t i = 0;
  1009. int r = size&0x3ffu;
  1010. size >>= 10;
  1011. for(; i < sizeof(units)-1 && size >= 1024; ++i) {
  1012. r = size&0x3ffu;
  1013. size >>= 10;
  1014. }
  1015. std::string result = itos(size, true);
  1016. result += fmt(".%d%ci", r*10/1024, units[i]);
  1017. return result;
  1018. }
  1019. void sleep(long seconds) {
  1020. #if defined(HAVE_WINSOCK2_H)
  1021. ::Sleep(seconds * 1000);
  1022. #elif HAVE_SLEEP
  1023. ::sleep(seconds);
  1024. #elif defined(HAVE_USLEEP)
  1025. ::usleep(seconds * 1000000);
  1026. #else
  1027. # error no sleep function is available (nanosleep?)
  1028. #endif
  1029. }
  1030. void usleep(long microseconds) {
  1031. #ifdef HAVE_USLEEP
  1032. ::usleep(microseconds);
  1033. #elif defined(HAVE_WINSOCK2_H)
  1034. LARGE_INTEGER current, freq, end;
  1035. static enum {GET_FREQUENCY, GET_MICROSECONDS, SKIP_MICROSECONDS} state = GET_FREQUENCY;
  1036. if (state == GET_FREQUENCY) {
  1037. if (QueryPerformanceFrequency(&freq))
  1038. state = GET_MICROSECONDS;
  1039. else
  1040. state = SKIP_MICROSECONDS;
  1041. }
  1042. long msec = microseconds / 1000;
  1043. microseconds %= 1000;
  1044. if (state == GET_MICROSECONDS && microseconds) {
  1045. QueryPerformanceCounter(&end);
  1046. end.QuadPart += (freq.QuadPart * microseconds) / 1000000;
  1047. while (QueryPerformanceCounter(&current) && (current.QuadPart <= end.QuadPart))
  1048. /* noop */ ;
  1049. }
  1050. if (msec)
  1051. Sleep(msec);
  1052. #else
  1053. #error no usleep function is available (nanosleep?)
  1054. #endif
  1055. }
  1056. unsigned int alphaToNum(const std::string& alphabets)
  1057. {
  1058. if(alphabets.empty()) {
  1059. return 0;
  1060. }
  1061. char base;
  1062. if(islower(alphabets[0])) {
  1063. base = 'a';
  1064. } else {
  1065. base = 'A';
  1066. }
  1067. uint64_t num = 0;
  1068. for(size_t i = 0, eoi = alphabets.size(); i < eoi; ++i) {
  1069. unsigned int v = alphabets[i]-base;
  1070. num = num*26+v;
  1071. if(num > UINT32_MAX) {
  1072. return 0;
  1073. }
  1074. }
  1075. return num;
  1076. }
  1077. void mkdirs(const std::string& dirpath)
  1078. {
  1079. File dir(dirpath);
  1080. if(!dir.mkdirs()) {
  1081. int errNum = errno;
  1082. if(!dir.isDir()) {
  1083. throw DL_ABORT_EX3
  1084. (errNum,
  1085. fmt(EX_MAKE_DIR, dir.getPath().c_str(),
  1086. safeStrerror(errNum).c_str()),
  1087. error_code::DIR_CREATE_ERROR);
  1088. }
  1089. }
  1090. }
  1091. void convertBitfield(BitfieldMan* dest, const BitfieldMan* src)
  1092. {
  1093. size_t numBlock = dest->countBlock();
  1094. for(size_t index = 0; index < numBlock; ++index) {
  1095. if(src->isBitSetOffsetRange((int64_t)index*dest->getBlockLength(),
  1096. dest->getBlockLength())) {
  1097. dest->setBit(index);
  1098. }
  1099. }
  1100. }
  1101. std::string toString(const BinaryStreamHandle& binaryStream)
  1102. {
  1103. std::stringstream strm;
  1104. char data[2048];
  1105. while(1) {
  1106. int32_t dataLength = binaryStream->readData
  1107. (reinterpret_cast<unsigned char*>(data), sizeof(data), strm.tellp());
  1108. strm.write(data, dataLength);
  1109. if(dataLength == 0) {
  1110. break;
  1111. }
  1112. }
  1113. return strm.str();
  1114. }
  1115. #ifdef HAVE_POSIX_MEMALIGN
  1116. /**
  1117. * In linux 2.6, alignment and size should be a multiple of 512.
  1118. */
  1119. void* allocateAlignedMemory(size_t alignment, size_t size)
  1120. {
  1121. void* buffer;
  1122. int res;
  1123. if((res = posix_memalign(&buffer, alignment, size)) != 0) {
  1124. throw FATAL_EXCEPTION
  1125. (fmt("Error in posix_memalign: %s",
  1126. util::safeStrerror(res).c_str()));
  1127. }
  1128. return buffer;
  1129. }
  1130. #endif // HAVE_POSIX_MEMALIGN
  1131. std::pair<std::string, uint16_t>
  1132. getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len)
  1133. {
  1134. char host[NI_MAXHOST];
  1135. char service[NI_MAXSERV];
  1136. int s = getnameinfo(sockaddr, len, host, NI_MAXHOST, service, NI_MAXSERV,
  1137. NI_NUMERICHOST|NI_NUMERICSERV);
  1138. if(s != 0) {
  1139. throw DL_ABORT_EX(fmt("Failed to get hostname and port. cause: %s",
  1140. gai_strerror(s)));
  1141. }
  1142. return std::pair<std::string, uint16_t>(host, atoi(service)); // TODO
  1143. }
  1144. std::string htmlEscape(const std::string& src)
  1145. {
  1146. std::string dest;
  1147. for(std::string::const_iterator i = src.begin(), eoi = src.end();
  1148. i != eoi; ++i) {
  1149. char ch = *i;
  1150. if(ch == '<') {
  1151. dest += "&lt;";
  1152. } else if(ch == '>') {
  1153. dest += "&gt;";
  1154. } else if(ch == '&') {
  1155. dest += "&amp;";
  1156. } else if(ch == '\'') {
  1157. dest += "&#39;";
  1158. } else if(ch == '"') {
  1159. dest += "&quot;";
  1160. } else {
  1161. dest += ch;
  1162. }
  1163. }
  1164. return dest;
  1165. }
  1166. std::pair<size_t, std::string>
  1167. parseIndexPath(const std::string& line)
  1168. {
  1169. std::pair<Scip, Scip> p;
  1170. divide(p, line.begin(), line.end(), '=');
  1171. size_t index = parseUInt(std::string(p.first.first, p.first.second));
  1172. if(p.second.first == p.second.second) {
  1173. throw DL_ABORT_EX(fmt("Path with index=%u is empty.",
  1174. static_cast<unsigned int>(index)));
  1175. }
  1176. return std::make_pair(index, std::string(p.second.first, p.second.second));
  1177. }
  1178. std::vector<std::pair<size_t, std::string> > createIndexPaths(std::istream& i)
  1179. {
  1180. std::vector<std::pair<size_t, std::string> > indexPaths;
  1181. std::string line;
  1182. while(getline(i, line)) {
  1183. indexPaths.push_back(parseIndexPath(line));
  1184. }
  1185. return indexPaths;
  1186. }
  1187. namespace {
  1188. void generateRandomDataRandom(unsigned char* data, size_t length)
  1189. {
  1190. const SharedHandle<SimpleRandomizer>& rd = SimpleRandomizer::getInstance();
  1191. for(size_t i = 0; i < length; ++i) {
  1192. data[i] = static_cast<unsigned long>(rd->getRandomNumber(256));
  1193. }
  1194. }
  1195. } // namespace
  1196. namespace {
  1197. void generateRandomDataUrandom
  1198. (unsigned char* data, size_t length, std::ifstream& devUrand)
  1199. {
  1200. devUrand.read(reinterpret_cast<char*>(data), length);
  1201. }
  1202. } // namespace
  1203. void generateRandomData(unsigned char* data, size_t length)
  1204. {
  1205. #ifdef __MINGW32__
  1206. generateRandomDataRandom(data, length);
  1207. #else // !__MINGW32__
  1208. static int method = -1;
  1209. static std::ifstream devUrand;
  1210. if(method == 0) {
  1211. generateRandomDataUrandom(data, length, devUrand);
  1212. } else if(method == 1) {
  1213. generateRandomDataRandom(data, length);
  1214. } else {
  1215. devUrand.open("/dev/urandom");
  1216. if(devUrand) {
  1217. method = 0;
  1218. } else {
  1219. method = 1;
  1220. }
  1221. generateRandomData(data, length);
  1222. }
  1223. #endif // !__MINGW32__
  1224. }
  1225. bool saveAs
  1226. (const std::string& filename, const std::string& data, bool overwrite)
  1227. {
  1228. if(!overwrite && File(filename).exists()) {
  1229. return false;
  1230. }
  1231. std::string tempFilename = filename;
  1232. tempFilename += "__temp";
  1233. {
  1234. BufferedFile fp(tempFilename, BufferedFile::WRITE);
  1235. if(!fp) {
  1236. return false;
  1237. }
  1238. if(fp.write(data.data(), data.size()) != data.size()) {
  1239. return false;
  1240. }
  1241. if(fp.close() == EOF) {
  1242. return false;
  1243. }
  1244. }
  1245. return File(tempFilename).renameTo(filename);
  1246. }
  1247. std::string applyDir(const std::string& dir, const std::string& relPath)
  1248. {
  1249. std::string s;
  1250. if(dir.empty()) {
  1251. s = "./";
  1252. s += relPath;
  1253. } else {
  1254. s = dir;
  1255. if(dir == "/") {
  1256. s += relPath;
  1257. } else {
  1258. s += "/";
  1259. s += relPath;
  1260. }
  1261. }
  1262. #ifdef __MINGW32__
  1263. for(std::string::iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
  1264. if(*i == '\\') {
  1265. *i = '/';
  1266. }
  1267. }
  1268. #endif // __MINGW32__
  1269. return s;
  1270. }
  1271. std::string fixTaintedBasename(const std::string& src)
  1272. {
  1273. static std::string SLASH_REP = "%2F";
  1274. return escapePath(replace(src, "/", SLASH_REP));
  1275. }
  1276. void generateRandomKey(unsigned char* key)
  1277. {
  1278. #ifdef ENABLE_MESSAGE_DIGEST
  1279. unsigned char bytes[40];
  1280. generateRandomData(bytes, sizeof(bytes));
  1281. message_digest::digest(key, 20, MessageDigest::sha1(), bytes, sizeof(bytes));
  1282. #else // !ENABLE_MESSAGE_DIGEST
  1283. generateRandomData(key, 20);
  1284. #endif // !ENABLE_MESSAGE_DIGEST
  1285. }
  1286. // Returns true is given numeric ipv4addr is in Private Address Space.
  1287. //
  1288. // From Section.3 RFC1918
  1289. // 10.0.0.0 - 10.255.255.255 (10/8 prefix)
  1290. // 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
  1291. // 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
  1292. bool inPrivateAddress(const std::string& ipv4addr)
  1293. {
  1294. if(util::startsWith(ipv4addr, "10.") ||
  1295. util::startsWith(ipv4addr, "192.168.")) {
  1296. return true;
  1297. }
  1298. if(util::startsWith(ipv4addr, "172.")) {
  1299. for(int i = 16; i <= 31; ++i) {
  1300. std::string t(fmt("%d.", i));
  1301. if(util::startsWith(ipv4addr.begin()+4, ipv4addr.end(),
  1302. t.begin(), t.end())) {
  1303. return true;
  1304. }
  1305. }
  1306. }
  1307. return false;
  1308. }
  1309. bool detectDirTraversal(const std::string& s)
  1310. {
  1311. if(s.empty()) {
  1312. return false;
  1313. }
  1314. for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
  1315. unsigned char c = *i;
  1316. if(in(c, 0x00u, 0x1fu) || c == 0x7fu) {
  1317. return true;
  1318. }
  1319. }
  1320. return s == "." || s == ".." || s[0] == '/' ||
  1321. util::startsWith(s, "./") || util::startsWith(s, "../") ||
  1322. s.find("/../") != std::string::npos ||
  1323. s.find("/./") != std::string::npos ||
  1324. s[s.size()-1] == '/' ||
  1325. util::endsWith(s, "/.") || util::endsWith(s, "/..");
  1326. }
  1327. std::string escapePath(const std::string& s)
  1328. {
  1329. // We don't escape '/' because we use it as a path separator.
  1330. #ifdef __MINGW32__
  1331. static const char WIN_INVALID_PATH_CHARS[] =
  1332. { '"', '*', ':', '<', '>', '?', '\\', '|' };
  1333. #endif // __MINGW32__
  1334. std::string d;
  1335. for(std::string::const_iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
  1336. unsigned char c = *i;
  1337. if(in(c, 0x00u, 0x1fu) || c == 0x7fu
  1338. #ifdef __MINGW32__
  1339. || std::find(vbegin(WIN_INVALID_PATH_CHARS),
  1340. vend(WIN_INVALID_PATH_CHARS),
  1341. c) != vend(WIN_INVALID_PATH_CHARS)
  1342. #endif // __MINGW32__
  1343. ){
  1344. d += fmt("%%%02X", c);
  1345. } else {
  1346. d += *i;
  1347. }
  1348. }
  1349. return d;
  1350. }
  1351. bool inSameCidrBlock
  1352. (const std::string& ip1, const std::string& ip2, size_t bits)
  1353. {
  1354. unsigned char s1[16], s2[16];
  1355. size_t len1, len2;
  1356. if((len1 = net::getBinAddr(s1, ip1)) == 0 ||
  1357. (len2 = net::getBinAddr(s2, ip2)) == 0 ||
  1358. len1 != len2) {
  1359. return false;
  1360. }
  1361. if(bits == 0) {
  1362. return true;
  1363. }
  1364. if(bits > 8*len1) {
  1365. bits = 8*len1;
  1366. }
  1367. int last = (bits-1)/8;
  1368. for(int i = 0; i < last; ++i) {
  1369. if(s1[i] != s2[i]) {
  1370. return false;
  1371. }
  1372. }
  1373. unsigned char mask = bitfield::lastByteMask(bits);
  1374. return (s1[last] & mask) == (s2[last] & mask);
  1375. }
  1376. namespace {
  1377. void executeHook
  1378. (const std::string& command,
  1379. a2_gid_t gid,
  1380. size_t numFiles,
  1381. const std::string& firstFilename)
  1382. {
  1383. const std::string gidStr = util::itos(gid);
  1384. const std::string numFilesStr = util::uitos(numFiles);
  1385. #ifndef __MINGW32__
  1386. A2_LOG_INFO(fmt("Executing user command: %s %s %s %s",
  1387. command.c_str(),
  1388. gidStr.c_str(),
  1389. numFilesStr.c_str(),
  1390. firstFilename.c_str()));
  1391. pid_t cpid = fork();
  1392. if(cpid == -1) {
  1393. A2_LOG_ERROR("fork() failed. Cannot execute user command.");
  1394. } else if(cpid == 0) {
  1395. execlp(command.c_str(),
  1396. command.c_str(),
  1397. gidStr.c_str(),
  1398. numFilesStr.c_str(),
  1399. firstFilename.c_str(),
  1400. reinterpret_cast<char*>(0));
  1401. perror(("Could not execute user command: "+command).c_str());
  1402. exit(EXIT_FAILURE);
  1403. }
  1404. #else
  1405. PROCESS_INFORMATION pi;
  1406. STARTUPINFOW si;
  1407. memset(&si, 0, sizeof (si));
  1408. si.cb = sizeof(STARTUPINFO);
  1409. memset(&pi, 0, sizeof (pi));
  1410. bool batch = util::iendsWith(command, ".bat");
  1411. std::string cmdline;
  1412. std::string cmdexe;
  1413. if(batch) {
  1414. const char* p = getenv("windir");
  1415. if(p) {
  1416. cmdexe = p;
  1417. cmdexe += "\\system32\\cmd.exe";
  1418. } else {
  1419. A2_LOG_INFO("Failed to get windir environment variable."
  1420. " Executing batch file will fail.");
  1421. // TODO Might be useless.
  1422. cmdexe = "cmd.exe";
  1423. }
  1424. cmdline += "/C \"";
  1425. }
  1426. cmdline += "\"";
  1427. cmdline += command;
  1428. cmdline += "\"";
  1429. cmdline += " ";
  1430. cmdline += gidStr;
  1431. cmdline += " ";
  1432. cmdline += numFilesStr;
  1433. cmdline += " \"";
  1434. cmdline += firstFilename;
  1435. cmdline += "\"";
  1436. if(batch) {
  1437. cmdline += "\"";
  1438. }
  1439. int cmdlineLen = utf8ToWChar(0, 0, cmdline.c_str());
  1440. assert(cmdlineLen > 0);
  1441. array_ptr<wchar_t> wcharCmdline(new wchar_t[cmdlineLen]);
  1442. cmdlineLen = utf8ToWChar(wcharCmdline, cmdlineLen, cmdline.c_str());
  1443. assert(cmdlineLen > 0);
  1444. A2_LOG_INFO(fmt("Executing user command: %s", cmdline.c_str()));
  1445. DWORD rc = CreateProcessW(batch ? utf8ToWChar(cmdexe).c_str() : NULL,
  1446. wcharCmdline,
  1447. NULL,
  1448. NULL,
  1449. true,
  1450. 0,
  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 strieq(const std::string& a, const char* b)
  1565. {
  1566. return strieq(a.begin(), a.end(), b);
  1567. }
  1568. bool strieq(const std::string& a, const std::string& b)
  1569. {
  1570. return strieq(a.begin(), a.end(), b.begin(), b.end());
  1571. }
  1572. bool startsWith(const std::string& a, const char* b)
  1573. {
  1574. return startsWith(a.begin(), a.end(), b);
  1575. }
  1576. bool startsWith(const std::string& a, const std::string& b)
  1577. {
  1578. return startsWith(a.begin(), a.end(), b.begin(), b.end());
  1579. }
  1580. bool istartsWith(const std::string& a, const char* b)
  1581. {
  1582. return istartsWith(a.begin(), a.end(), b);
  1583. }
  1584. bool endsWith(const std::string& a, const char* b)
  1585. {
  1586. return endsWith(a.begin(), a.end(), b, b+strlen(b));
  1587. }
  1588. bool endsWith(const std::string& a, const std::string& b)
  1589. {
  1590. return endsWith(a.begin(), a.end(), b.begin(), b.end());
  1591. }
  1592. bool iendsWith(const std::string& a, const char* b)
  1593. {
  1594. return iendsWith(a.begin(), a.end(), b, b+strlen(b));
  1595. }
  1596. bool iendsWith(const std::string& a, const std::string& b)
  1597. {
  1598. return iendsWith(a.begin(), a.end(), b.begin(), b.end());
  1599. }
  1600. bool strless(const char* a, const char* b)
  1601. {
  1602. return strcmp(a, b) < 0;
  1603. }
  1604. } // namespace util
  1605. } // namespace aria2