Util.cc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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 "File.h"
  37. #include "message.h"
  38. #include "Randomizer.h"
  39. #include "a2netcompat.h"
  40. #include "DlAbortEx.h"
  41. #include "BitfieldMan.h"
  42. #include "DefaultDiskWriter.h"
  43. #include "FatalException.h"
  44. #include "FileEntry.h"
  45. #include "StringFormat.h"
  46. #include "A2STR.h"
  47. #include <signal.h>
  48. #include <limits.h>
  49. #include <stdint.h>
  50. #include <cerrno>
  51. #include <cassert>
  52. #include <cstring>
  53. #include <cstdlib>
  54. #include <iomanip>
  55. #include <sstream>
  56. #include <ostream>
  57. #include <algorithm>
  58. #ifndef HAVE_SLEEP
  59. # ifdef HAVE_WINSOCK_H
  60. # define WIN32_LEAN_AND_MEAN
  61. # include <windows.h>
  62. # endif // HAVE_WINSOCK_H
  63. #endif // HAVE_SLEEP
  64. // For libc6 which doesn't define ULLONG_MAX properly because of broken limits.h
  65. #ifndef ULLONG_MAX
  66. # define ULLONG_MAX 18446744073709551615ULL
  67. #endif // ULLONG_MAX
  68. namespace aria2 {
  69. const std::string Util::DEFAULT_TRIM_CHARSET("\r\n\t ");
  70. std::string Util::trim(const std::string& src, const std::string& trimCharset)
  71. {
  72. std::string temp(src);
  73. trimSelf(temp, trimCharset);
  74. return temp;
  75. }
  76. void Util::trimSelf(std::string& str, const std::string& trimCharset)
  77. {
  78. std::string::size_type first = str.find_first_not_of(trimCharset);
  79. if(first == std::string::npos) {
  80. str.clear();
  81. } else {
  82. std::string::size_type last = str.find_last_not_of(trimCharset)+1;
  83. str.erase(last);
  84. str.erase(0, first);
  85. }
  86. }
  87. void Util::split(std::pair<std::string, std::string>& hp, const std::string& src, char delim)
  88. {
  89. hp.first = A2STR::NIL;
  90. hp.second = A2STR::NIL;
  91. std::string::size_type p = src.find(delim);
  92. if(p == std::string::npos) {
  93. hp.first = src;
  94. hp.second = A2STR::NIL;
  95. } else {
  96. hp.first = trim(src.substr(0, p));
  97. hp.second = trim(src.substr(p+1));
  98. }
  99. }
  100. std::pair<std::string, std::string> Util::split(const std::string& src, const std::string& delims)
  101. {
  102. std::pair<std::string, std::string> hp;
  103. hp.first = A2STR::NIL;
  104. hp.second = A2STR::NIL;
  105. std::string::size_type p = src.find_first_of(delims);
  106. if(p == std::string::npos) {
  107. hp.first = src;
  108. hp.second = A2STR::NIL;
  109. } else {
  110. hp.first = trim(src.substr(0, p));
  111. hp.second = trim(src.substr(p+1));
  112. }
  113. return hp;
  114. }
  115. int64_t Util::difftv(struct timeval tv1, struct timeval tv2) {
  116. if((tv1.tv_sec < tv2.tv_sec) ||
  117. ((tv1.tv_sec == tv2.tv_sec) && (tv1.tv_usec < tv2.tv_usec))) {
  118. return 0;
  119. }
  120. return ((int64_t)(tv1.tv_sec-tv2.tv_sec)*1000000+
  121. tv1.tv_usec-tv2.tv_usec);
  122. }
  123. int32_t Util::difftvsec(struct timeval tv1, struct timeval tv2) {
  124. if(tv1.tv_sec < tv2.tv_sec) {
  125. return 0;
  126. }
  127. return tv1.tv_sec-tv2.tv_sec;
  128. }
  129. void Util::slice(std::deque<std::string>& result, const std::string& src, char delim, bool doTrim) {
  130. std::string::size_type p = 0;
  131. while(1) {
  132. std::string::size_type np = src.find(delim, p);
  133. if(np == std::string::npos) {
  134. std::string term = src.substr(p);
  135. if(doTrim) {
  136. term = trim(term);
  137. }
  138. if(term.size()) {
  139. result.push_back(term);
  140. }
  141. break;
  142. }
  143. std::string term = src.substr(p, np-p);
  144. if(doTrim) {
  145. term = trim(term);
  146. }
  147. p = np+1;
  148. if(term.size()) {
  149. result.push_back(term);
  150. }
  151. }
  152. }
  153. bool Util::startsWith(const std::string& target, const std::string& part) {
  154. if(target.size() < part.size()) {
  155. return false;
  156. }
  157. if(part.empty()) {
  158. return true;
  159. }
  160. if(target.find(part) == 0) {
  161. return true;
  162. } else {
  163. return false;
  164. }
  165. }
  166. bool Util::endsWith(const std::string& target, const std::string& part) {
  167. if(target.size() < part.size()) {
  168. return false;
  169. }
  170. if(part.empty()) {
  171. return true;
  172. }
  173. if(target.rfind(part) == target.size()-part.size()) {
  174. return true;
  175. } else {
  176. return false;
  177. }
  178. }
  179. std::string Util::replace(const std::string& target, const std::string& oldstr, const std::string& newstr) {
  180. if(target.empty() || oldstr.empty()) {
  181. return target;
  182. }
  183. std::string result;
  184. std::string::size_type p = 0;
  185. std::string::size_type np = target.find(oldstr);
  186. while(np != std::string::npos) {
  187. result += target.substr(p, np-p)+newstr;
  188. p = np+oldstr.size();
  189. np = target.find(oldstr, p);
  190. }
  191. result += target.substr(p);
  192. return result;
  193. }
  194. bool Util::shouldUrlencode(const char c)
  195. {
  196. return !(// ALPHA
  197. ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') ||
  198. // DIGIT
  199. ('0' <= c && c <= '9') ||
  200. // safe
  201. '$' == c || '-' == c || '_' == c || '.' == c ||
  202. // extra
  203. '!' == c || '*' == c || '\'' == c ||'(' == c ||
  204. ')' == c || ',' == c ||
  205. // reserved
  206. ';' == c || '/' == c || '?' == c || ':' == c ||
  207. '@' == c || '&' == c || '=' == c || '+' == c);
  208. }
  209. std::string Util::urlencode(const unsigned char* target, size_t len) {
  210. std::string dest;
  211. for(size_t i = 0; i < len; i++) {
  212. if(shouldUrlencode(target[i])) {
  213. dest.append(StringFormat("%%%02x", target[i]).str());
  214. } else {
  215. dest += target[i];
  216. }
  217. }
  218. return dest;
  219. }
  220. std::string Util::torrentUrlencode(const unsigned char* target, size_t len) {
  221. std::string dest;
  222. for(size_t i = 0; i < len; i++) {
  223. if(('0' <= target[i] && target[i] <= '9') ||
  224. ('A' <= target[i] && target[i] <= 'Z') ||
  225. ('a' <= target[i] && target[i] <= 'z')) {
  226. dest += target[i];
  227. } else {
  228. dest.append(StringFormat("%%%02x", target[i]).str());
  229. }
  230. }
  231. return dest;
  232. }
  233. std::string Util::urldecode(const std::string& target) {
  234. std::string result;
  235. for(std::string::const_iterator itr = target.begin();
  236. itr != target.end(); itr++) {
  237. if(*itr == '%') {
  238. if(itr+1 != target.end() && itr+2 != target.end() &&
  239. isxdigit(*(itr+1)) && isxdigit(*(itr+2))) {
  240. result += Util::parseInt(std::string(itr+1, itr+3), 16);
  241. itr += 2;
  242. } else {
  243. result += *itr;
  244. }
  245. } else {
  246. result += *itr;
  247. }
  248. }
  249. return result;
  250. }
  251. std::string Util::toHex(const unsigned char* src, size_t len) {
  252. char* temp = new char[len*2+1];
  253. for(size_t i = 0; i < len; i++) {
  254. sprintf(temp+i*2, "%02x", src[i]);
  255. }
  256. temp[len*2] = '\0';
  257. std::string hex = temp;
  258. delete [] temp;
  259. return hex;
  260. }
  261. FILE* Util::openFile(const std::string& filename, const std::string& mode) {
  262. FILE* file = fopen(filename.c_str(), mode.c_str());
  263. return file;
  264. }
  265. void Util::fileCopy(const std::string& dest, const std::string& src) {
  266. File file(src);
  267. rangedFileCopy(dest, src, 0, file.size());
  268. }
  269. void Util::rangedFileCopy(const std::string& dest, const std::string& src, off_t srcOffset, uint64_t length)
  270. {
  271. size_t bufSize = 4096;
  272. unsigned char buf[bufSize];
  273. DefaultDiskWriter srcdw;
  274. DefaultDiskWriter destdw;
  275. srcdw.openExistingFile(src);
  276. destdw.initAndOpenFile(dest);
  277. lldiv_t res = lldiv(length, bufSize);
  278. unsigned int x = res.quot;
  279. unsigned int r = res.rem;
  280. off_t initialOffset = srcOffset;
  281. for(unsigned int i = 0; i < x; ++i) {
  282. size_t readLength = 0;
  283. while(readLength < bufSize) {
  284. ssize_t ret = srcdw.readData(buf, bufSize-readLength, srcOffset);
  285. destdw.writeData(buf, ret, srcOffset-initialOffset);
  286. srcOffset += ret;
  287. readLength += ret;
  288. }
  289. }
  290. if(r > 0) {
  291. size_t readLength = 0;
  292. while(readLength < r) {
  293. ssize_t ret = srcdw.readData(buf, r-readLength, srcOffset);
  294. destdw.writeData(buf, ret, srcOffset-initialOffset);
  295. srcOffset += ret;
  296. readLength += ret;
  297. }
  298. }
  299. }
  300. bool Util::isPowerOf(int num, int base) {
  301. if(base <= 0) { return false; }
  302. if(base == 1) { return true; }
  303. while(num%base == 0) {
  304. num /= base;
  305. if(num == 1) {
  306. return true;
  307. }
  308. }
  309. return false;
  310. }
  311. std::string Util::secfmt(time_t sec) {
  312. std::string str;
  313. if(sec >= 3600) {
  314. str = itos(sec/3600)+"h";
  315. sec %= 3600;
  316. }
  317. if(sec >= 60) {
  318. int min = sec/60;
  319. if(min < 10) {
  320. str += "0";
  321. }
  322. str += itos(min)+"m";
  323. sec %= 60;
  324. }
  325. if(sec < 10) {
  326. str += "0";
  327. }
  328. str += itos(sec)+"s";
  329. return str;
  330. }
  331. size_t Util::expandBuffer(char** pbuf, size_t curLength, size_t newLength) {
  332. char* newbuf = new char[newLength];
  333. memcpy(newbuf, *pbuf, curLength);
  334. delete [] *pbuf;
  335. *pbuf = newbuf;
  336. return newLength;
  337. }
  338. int getNum(const char* buf, int offset, size_t length) {
  339. char* temp = new char[length+1];
  340. memcpy(temp, buf+offset, length);
  341. temp[length] = '\0';
  342. int x = strtol(temp, 0, 10);
  343. delete [] temp;
  344. return x;
  345. }
  346. void unfoldSubRange(const std::string& src, std::deque<int>& range) {
  347. if(src.empty()) {
  348. return;
  349. }
  350. std::string::size_type p = src.find_first_of(",-");
  351. if(p == 0) {
  352. return;
  353. } else if(p == std::string::npos) {
  354. range.push_back(atoi(src.c_str()));
  355. } else {
  356. if(src.at(p) == ',') {
  357. int num = getNum(src.c_str(), 0, p);
  358. range.push_back(num);
  359. unfoldSubRange(src.substr(p+1), range);
  360. } else if(src.at(p) == '-') {
  361. std::string::size_type rightNumBegin = p+1;
  362. std::string::size_type nextDelim = src.find_first_of(",", rightNumBegin);
  363. if(nextDelim == std::string::npos) {
  364. nextDelim = src.size();
  365. }
  366. int left = getNum(src.c_str(), 0, p);
  367. int right = getNum(src.c_str(), rightNumBegin, nextDelim-rightNumBegin);
  368. for(int i = left; i <= right; i++) {
  369. range.push_back(i);
  370. }
  371. if(src.size() > nextDelim) {
  372. unfoldSubRange(src.substr(nextDelim+1), range);
  373. }
  374. }
  375. }
  376. }
  377. void Util::unfoldRange(const std::string& src, std::deque<int>& range) {
  378. unfoldSubRange(src, range);
  379. std::sort(range.begin(), range.end());
  380. range.erase(std::unique(range.begin(), range.end()), range.end());
  381. }
  382. int32_t Util::parseInt(const std::string& s, int32_t base)
  383. {
  384. std::string trimed = Util::trim(s);
  385. if(trimed.empty()) {
  386. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  387. "empty string").str());
  388. }
  389. char* stop;
  390. errno = 0;
  391. long int v = strtol(trimed.c_str(), &stop, base);
  392. if(*stop != '\0') {
  393. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  394. trimed.c_str()).str());
  395. } else if((((v == LONG_MIN) || (v == LONG_MAX)) && (errno == ERANGE)) ||
  396. (v > INT32_MAX) ||
  397. (v < INT32_MIN)) {
  398. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  399. trimed.c_str()).str());
  400. }
  401. return v;
  402. }
  403. uint32_t Util::parseUInt(const std::string& s, int base)
  404. {
  405. std::string trimed = Util::trim(s);
  406. if(trimed.empty()) {
  407. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  408. "empty string").str());
  409. }
  410. // We don't allow negative number.
  411. if(trimed[0] == '-') {
  412. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  413. trimed.c_str()).str());
  414. }
  415. char* stop;
  416. errno = 0;
  417. unsigned long int v = strtoul(trimed.c_str(), &stop, base);
  418. if(*stop != '\0') {
  419. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  420. trimed.c_str()).str());
  421. } else if(((v == ULONG_MAX) && (errno == ERANGE)) || (v > UINT32_MAX)) {
  422. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  423. trimed.c_str()).str());
  424. }
  425. return v;
  426. }
  427. int64_t Util::parseLLInt(const std::string& s, int32_t base)
  428. {
  429. std::string trimed = Util::trim(s);
  430. if(trimed.empty()) {
  431. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  432. "empty string").str());
  433. }
  434. char* stop;
  435. errno = 0;
  436. int64_t v = strtoll(trimed.c_str(), &stop, base);
  437. if(*stop != '\0') {
  438. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  439. trimed.c_str()).str());
  440. } else if(((v == INT64_MIN) || (v == INT64_MAX)) && (errno == ERANGE)) {
  441. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  442. trimed.c_str()).str());
  443. }
  444. return v;
  445. }
  446. uint64_t Util::parseULLInt(const std::string& s, int base)
  447. {
  448. std::string trimed = Util::trim(s);
  449. if(trimed.empty()) {
  450. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  451. "empty string").str());
  452. }
  453. // We don't allow negative number.
  454. if(trimed[0] == '-') {
  455. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  456. trimed.c_str()).str());
  457. }
  458. char* stop;
  459. errno = 0;
  460. uint64_t v = strtoull(trimed.c_str(), &stop, base);
  461. if(*stop != '\0') {
  462. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  463. trimed.c_str()).str());
  464. } else if((v == ULLONG_MAX) && (errno == ERANGE)) {
  465. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  466. trimed.c_str()).str());
  467. }
  468. return v;
  469. }
  470. IntSequence Util::parseIntRange(const std::string& src)
  471. {
  472. IntSequence::Values values;
  473. std::string temp = src;
  474. while(temp.size()) {
  475. std::pair<std::string, std::string> p = Util::split(temp, ",");
  476. temp = p.second;
  477. if(p.first.empty()) {
  478. continue;
  479. }
  480. if(p.first.find("-") == std::string::npos) {
  481. int32_t v = Util::parseInt(p.first.c_str());
  482. values.push_back(IntSequence::Value(v, v+1));
  483. } else {
  484. std::pair<std::string, std::string> vp = Util::split(p.first.c_str(), "-");
  485. if(vp.first.empty() || vp.second.empty()) {
  486. throw DlAbortEx
  487. (StringFormat(MSG_INCOMPLETE_RANGE, p.first.c_str()).str());
  488. }
  489. int32_t v1 = Util::parseInt(vp.first.c_str());
  490. int32_t v2 = Util::parseInt(vp.second.c_str());
  491. values.push_back(IntSequence::Value(v1, v2+1));
  492. }
  493. }
  494. return values;
  495. }
  496. std::string Util::getContentDispositionFilename(const std::string& header) {
  497. static const std::string keyName = "filename=";
  498. std::string::size_type attributesp = header.find(keyName);
  499. if(attributesp == std::string::npos) {
  500. return A2STR::NIL;
  501. }
  502. std::string::size_type filenamesp = attributesp+keyName.size();
  503. std::string::size_type filenameep;
  504. if(filenamesp == header.size()) {
  505. return A2STR::NIL;
  506. }
  507. if(header[filenamesp] == '\'' || header[filenamesp] == '"') {
  508. char quoteChar = header[filenamesp];
  509. filenameep = header.find(quoteChar, filenamesp+1);
  510. } else {
  511. filenameep = header.find(';', filenamesp);
  512. }
  513. if(filenameep == std::string::npos) {
  514. filenameep = header.size();
  515. }
  516. static const std::string TRIMMED("\r\n '\"");
  517. return trim(header.substr(filenamesp, filenameep-filenamesp), TRIMMED);
  518. }
  519. static int nbits[] = {
  520. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  521. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  522. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  523. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  524. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  525. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  526. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  527. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  528. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  529. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  530. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  531. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  532. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  533. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  534. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  535. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
  536. };
  537. unsigned int Util::countBit(uint32_t n) {
  538. return
  539. nbits[n&0xffu]+
  540. nbits[(n >> 8)&0xffu]+
  541. nbits[(n >> 16)&0xffu]+
  542. nbits[(n >> 24)&0xffu];
  543. }
  544. std::string Util::randomAlpha(size_t length, const RandomizerHandle& randomizer) {
  545. static const char *random_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  546. std::string str;
  547. for(size_t i = 0; i < length; i++) {
  548. size_t index = randomizer->getRandomNumber(strlen(random_chars));
  549. str += random_chars[index];
  550. }
  551. return str;
  552. }
  553. std::string Util::toUpper(const std::string& src) {
  554. std::string temp = src;
  555. std::transform(temp.begin(), temp.end(), temp.begin(), ::toupper);
  556. return temp;
  557. }
  558. std::string Util::toLower(const std::string& src) {
  559. std::string temp = src;
  560. std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
  561. return temp;
  562. }
  563. bool Util::isNumbersAndDotsNotation(const std::string& name) {
  564. struct sockaddr_in sockaddr;
  565. if(inet_aton(name.c_str(), &sockaddr.sin_addr)) {
  566. return true;
  567. } else {
  568. return false;
  569. }
  570. }
  571. void Util::setGlobalSignalHandler(int sig, void (*handler)(int), int flags) {
  572. #ifdef HAVE_SIGACTION
  573. struct sigaction sigact;
  574. sigact.sa_handler = handler;
  575. sigact.sa_flags = flags;
  576. sigemptyset(&sigact.sa_mask);
  577. sigaction(sig, &sigact, NULL);
  578. #else
  579. signal(sig, handler);
  580. #endif // HAVE_SIGACTION
  581. }
  582. void Util::indexRange(size_t& startIndex, size_t& endIndex,
  583. off_t offset, size_t srcLength, size_t destLength)
  584. {
  585. int64_t _startIndex = offset/destLength;
  586. int64_t _endIndex = (offset+srcLength-1)/destLength;
  587. assert(_startIndex <= INT32_MAX);
  588. assert(_endIndex <= INT32_MAX);
  589. startIndex = _startIndex;
  590. endIndex = _endIndex;
  591. }
  592. std::string Util::getHomeDir()
  593. {
  594. const char* p = getenv("HOME");
  595. if(p) {
  596. return p;
  597. } else {
  598. return A2STR::NIL;
  599. }
  600. }
  601. int64_t Util::getRealSize(const std::string& sizeWithUnit)
  602. {
  603. std::string::size_type p = sizeWithUnit.find_first_of("KM");
  604. std::string size;
  605. int32_t mult = 1;
  606. if(p == std::string::npos) {
  607. size = sizeWithUnit;
  608. } else {
  609. if(sizeWithUnit[p] == 'K') {
  610. mult = 1024;
  611. } else if(sizeWithUnit[p] == 'M') {
  612. mult = 1024*1024;
  613. }
  614. size = sizeWithUnit.substr(0, p);
  615. }
  616. int64_t v = Util::parseLLInt(size);
  617. if(v < 0) {
  618. throw DlAbortEx
  619. (StringFormat("Negative value detected: %s", sizeWithUnit.c_str()).str());
  620. } else if(INT64_MAX/mult < v) {
  621. throw DlAbortEx(StringFormat(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  622. "overflow/underflow").str());
  623. }
  624. return v*mult;
  625. }
  626. std::string Util::abbrevSize(int64_t size)
  627. {
  628. if(size < 1024) {
  629. return Util::itos(size, true);
  630. }
  631. char units[] = { 'K', 'M' };
  632. size_t numUnit = sizeof(units)/sizeof(char);
  633. size_t i = 0;
  634. int r = size&0x3ff;
  635. size >>= 10;
  636. for(; i < numUnit-1 && size >= 1024; ++i) {
  637. r = size&0x3ff;
  638. size >>= 10;
  639. }
  640. return Util::itos(size, true)+"."+Util::itos(r*10/1024)+units[i]+"i";
  641. }
  642. time_t Util::httpGMT(const std::string& httpStdTime)
  643. {
  644. struct tm tm;
  645. memset(&tm, 0, sizeof(tm));
  646. strptime(httpStdTime.c_str(), "%a, %Y-%m-%d %H:%M:%S GMT", &tm);
  647. time_t thetime = timegm(&tm);
  648. return thetime;
  649. }
  650. void Util::toStream(std::ostream& os, const FileEntries& fileEntries)
  651. {
  652. os << _("Files:") << "\n";
  653. os << "idx|path/length" << "\n";
  654. os << "===+===========================================================================" << "\n";
  655. int32_t count = 1;
  656. for(FileEntries::const_iterator itr = fileEntries.begin();
  657. itr != fileEntries.end(); count++, itr++) {
  658. os << std::setw(3) << count << "|" << (*itr)->getPath() << "\n";
  659. os << " |" << Util::abbrevSize((*itr)->getLength()) << "B" << "\n";
  660. os << "---+---------------------------------------------------------------------------" << "\n";
  661. }
  662. }
  663. void Util::sleep(long seconds) {
  664. #ifdef HAVE_SLEEP
  665. ::sleep(seconds);
  666. #elif defined(HAVE_USLEEP)
  667. ::usleep(seconds * 1000000);
  668. #elif defined(HAVE_WINSOCK2_H)
  669. ::Sleep(seconds * 1000);
  670. #else
  671. #error no sleep function is available (nanosleep?)
  672. #endif
  673. }
  674. void Util::usleep(long microseconds) {
  675. #ifdef HAVE_USLEEP
  676. ::usleep(microseconds);
  677. #elif defined(HAVE_WINSOCK2_H)
  678. LARGE_INTEGER current, freq, end;
  679. static enum {GET_FREQUENCY, GET_MICROSECONDS, SKIP_MICROSECONDS} state = GET_FREQUENCY;
  680. if (state == GET_FREQUENCY) {
  681. if (QueryPerformanceFrequency(&freq))
  682. state = GET_MICROSECONDS;
  683. else
  684. state = SKIP_MICROSECONDS;
  685. }
  686. long msec = microseconds / 1000;
  687. microseconds %= 1000;
  688. if (state == GET_MICROSECONDS && microseconds) {
  689. QueryPerformanceCounter(&end);
  690. end.QuadPart += (freq.QuadPart * microseconds) / 1000000;
  691. while (QueryPerformanceCounter(&current) && (current.QuadPart <= end.QuadPart))
  692. /* noop */ ;
  693. }
  694. if (msec)
  695. Sleep(msec);
  696. #else
  697. #error no usleep function is available (nanosleep?)
  698. #endif
  699. }
  700. bool Util::isNumber(const std::string& what)
  701. {
  702. if(what.empty()) {
  703. return false;
  704. }
  705. for(uint32_t i = 0; i < what.size(); ++i) {
  706. if(!isdigit(what[i])) {
  707. return false;
  708. }
  709. }
  710. return true;
  711. }
  712. bool Util::isLowercase(const std::string& what)
  713. {
  714. if(what.empty()) {
  715. return false;
  716. }
  717. for(uint32_t i = 0; i < what.size(); ++i) {
  718. if(!('a' <= what[i] && what[i] <= 'z')) {
  719. return false;
  720. }
  721. }
  722. return true;
  723. }
  724. bool Util::isUppercase(const std::string& what)
  725. {
  726. if(what.empty()) {
  727. return false;
  728. }
  729. for(uint32_t i = 0; i < what.size(); ++i) {
  730. if(!('A' <= what[i] && what[i] <= 'Z')) {
  731. return false;
  732. }
  733. }
  734. return true;
  735. }
  736. unsigned int Util::alphaToNum(const std::string& alphabets)
  737. {
  738. if(alphabets.empty()) {
  739. return 0;
  740. }
  741. char base;
  742. if(islower(alphabets[0])) {
  743. base = 'a';
  744. } else {
  745. base = 'A';
  746. }
  747. uint64_t num = 0;
  748. for(size_t i = 0; i < alphabets.size(); ++i) {
  749. unsigned int v = alphabets[i]-base;
  750. num = num*26+v;
  751. if(num > UINT32_MAX) {
  752. return 0;
  753. }
  754. }
  755. return num;
  756. }
  757. void Util::mkdirs(const std::string& dirpath)
  758. {
  759. File dir(dirpath);
  760. if(dir.isDir()) {
  761. // do nothing
  762. } else if(dir.exists()) {
  763. throw DlAbortEx
  764. (StringFormat(EX_MAKE_DIR, dir.getPath().c_str(),
  765. "File already exists.").str());
  766. } else if(!dir.mkdirs()) {
  767. throw DlAbortEx
  768. (StringFormat(EX_MAKE_DIR, dir.getPath().c_str(),
  769. strerror(errno)).str());
  770. }
  771. }
  772. void Util::convertBitfield(BitfieldMan* dest, const BitfieldMan* src)
  773. {
  774. size_t numBlock = dest->countBlock();
  775. for(size_t index = 0; index < numBlock; ++index) {
  776. if(src->isBitSetOffsetRange((uint64_t)index*dest->getBlockLength(),
  777. dest->getBlockLength())) {
  778. dest->setBit(index);
  779. }
  780. }
  781. }
  782. std::string Util::toString(const BinaryStreamHandle& binaryStream)
  783. {
  784. std::stringstream strm;
  785. char data[2048];
  786. while(1) {
  787. int32_t dataLength = binaryStream->readData((unsigned char*)data, sizeof(data), strm.tellp());
  788. strm.write(data, dataLength);
  789. if(dataLength == 0) {
  790. break;
  791. }
  792. }
  793. return strm.str();
  794. }
  795. #ifdef HAVE_POSIX_MEMALIGN
  796. /**
  797. * In linux 2.6, alignment and size should be a multiple of 512.
  798. */
  799. void* Util::allocateAlignedMemory(size_t alignment, size_t size)
  800. {
  801. void* buffer;
  802. int res;
  803. if((res = posix_memalign(&buffer, alignment, size)) != 0) {
  804. throw FatalException
  805. (StringFormat("Error in posix_memalign: %s", strerror(res)).str());
  806. }
  807. return buffer;
  808. }
  809. #endif // HAVE_POSIX_MEMALIGN
  810. std::pair<std::string, uint16_t>
  811. Util::getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len)
  812. {
  813. char host[NI_MAXHOST];
  814. char service[NI_MAXSERV];
  815. int s = getnameinfo(sockaddr, len, host, NI_MAXHOST, service, NI_MAXSERV,
  816. NI_NUMERICHOST|NI_NUMERICSERV);
  817. if(s != 0) {
  818. throw DlAbortEx(StringFormat("Failed to get hostname and port. cause: %s",
  819. gai_strerror(s)).str());
  820. }
  821. return std::pair<std::string, uint16_t>(host, atoi(service)); // TODO
  822. }
  823. } // namespace aria2