Util.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 "DlAbortEx.h"
  37. #include "File.h"
  38. #include "message.h"
  39. #include <ctype.h>
  40. #include <errno.h>
  41. #include <sys/types.h>
  42. #include <sys/stat.h>
  43. #include <fcntl.h>
  44. #include <netinet/in.h>
  45. #include <sys/socket.h>
  46. #include <netinet/in.h>
  47. #include <arpa/inet.h>
  48. #include <unistd.h>
  49. #include <signal.h>
  50. template<typename T>
  51. string uint2str(T value, bool comma) {
  52. string str;
  53. if(value == 0) {
  54. str = "0";
  55. return str;
  56. }
  57. int count = 0;
  58. while(value) {
  59. ++count;
  60. char digit = value%10+'0';
  61. str.insert(str.begin(), digit);
  62. value /= 10;
  63. if(comma && count > 3 && count%3 == 1) {
  64. str.insert(str.begin()+1, ',');
  65. }
  66. }
  67. return str;
  68. }
  69. template<typename T>
  70. string int2str(T value, bool comma) {
  71. bool flag = false;
  72. if(value < 0) {
  73. flag = true;
  74. value = -value;
  75. }
  76. string str = uint2str<T>(value, comma);
  77. if(flag) {
  78. str.insert(str.begin(), '-');
  79. }
  80. return str;
  81. }
  82. string Util::uitos(uint16_t value, bool comma) {
  83. return uint2str<uint16_t>(value, comma);
  84. }
  85. string Util::itos(int16_t value, bool comma) {
  86. return int2str<int16_t>(value, comma);
  87. }
  88. string Util::uitos(uint32_t value, bool comma) {
  89. return uint2str<uint32_t>(value, comma);
  90. }
  91. string Util::itos(int32_t value, bool comma) {
  92. return int2str<int32_t>(value, comma);
  93. }
  94. string Util::ullitos(uint64_t value, bool comma) {
  95. return uint2str<uint64_t>(value, comma);
  96. }
  97. string Util::llitos(int64_t value, bool comma)
  98. {
  99. return int2str<int64_t>(value, comma);
  100. }
  101. string Util::trim(const string& src) {
  102. string::size_type sp = src.find_first_not_of("\r\n\t ");
  103. string::size_type ep = src.find_last_not_of("\r\n\t ");
  104. if(sp == string::npos || ep == string::npos) {
  105. return "";
  106. } else {
  107. return src.substr(sp, ep-sp+1);
  108. }
  109. }
  110. void Util::split(pair<string, string>& hp, const string& src, char delim) {
  111. hp.first = "";
  112. hp.second = "";
  113. string::size_type p = src.find(delim);
  114. if(p == string::npos) {
  115. hp.first = src;
  116. hp.second = "";
  117. } else {
  118. hp.first = trim(src.substr(0, p));
  119. hp.second = trim(src.substr(p+1));
  120. }
  121. }
  122. long long int Util::difftv(struct timeval tv1, struct timeval tv2) {
  123. if(tv1.tv_sec < tv2.tv_sec || tv1.tv_sec == tv2.tv_sec && tv1.tv_usec < tv2.tv_usec) {
  124. return 0;
  125. }
  126. return ((long long int)(tv1.tv_sec-tv2.tv_sec)*1000000+
  127. tv1.tv_usec-tv2.tv_usec);
  128. }
  129. int Util::difftvsec(struct timeval tv1, struct timeval tv2) {
  130. if(tv1.tv_sec < tv2.tv_sec) {
  131. return 0;
  132. }
  133. return tv1.tv_sec-tv2.tv_sec;
  134. }
  135. void Util::slice(Strings& result, const string& src, char delim, bool doTrim) {
  136. string::size_type p = 0;
  137. while(1) {
  138. string::size_type np = src.find(delim, p);
  139. if(np == string::npos) {
  140. string term = src.substr(p);
  141. if(doTrim) {
  142. term = trim(term);
  143. }
  144. if(term.size()) {
  145. result.push_back(term);
  146. }
  147. break;
  148. }
  149. string term = src.substr(p, np-p);
  150. if(doTrim) {
  151. term = trim(term);
  152. }
  153. p = np+1;
  154. if(term.size()) {
  155. result.push_back(term);
  156. }
  157. }
  158. }
  159. bool Util::startsWith(const string& target, const string& part) {
  160. if(target.size() < part.size()) {
  161. return false;
  162. }
  163. if(part == "") {
  164. return true;
  165. }
  166. if(target.find(part) == 0) {
  167. return true;
  168. } else {
  169. return false;
  170. }
  171. }
  172. bool Util::endsWith(const string& target, const string& part) {
  173. if(target.size() < part.size()) {
  174. return false;
  175. }
  176. if(part == "") {
  177. return true;
  178. }
  179. if(target.rfind(part) == target.size()-part.size()) {
  180. return true;
  181. } else {
  182. return false;
  183. }
  184. }
  185. string Util::replace(const string& target, const string& oldstr, const string& newstr) {
  186. if(target == "" || oldstr == "" ) {
  187. return target;
  188. }
  189. string result;
  190. string::size_type p = 0;
  191. string::size_type np = target.find(oldstr);
  192. while(np != string::npos) {
  193. result += target.substr(p, np-p)+newstr;
  194. p = np+oldstr.size();
  195. np = target.find(oldstr, p);
  196. }
  197. result += target.substr(p);
  198. return result;
  199. }
  200. string Util::urlencode(const unsigned char* target, int len) {
  201. string dest;
  202. for(int i = 0; i < len; i++) {
  203. if(!('0' <= target[i] && target[i] <= '9' ||
  204. 'A' <= target[i] && target[i] <= 'Z' ||
  205. 'a' <= target[i] && target[i] <= 'z' ||
  206. '$' == target[i] || '-' == target[i] ||
  207. '_' == target[i] || '.' == target[i] ||
  208. '+' == target[i] || '!' == target[i] ||
  209. '*' == target[i] || '\'' == target[i] ||
  210. '(' == target[i] || ')' == target[i] ||
  211. ',' == target[i])) {
  212. char temp[4];
  213. sprintf(temp, "%%%02x", target[i]);
  214. temp[sizeof(temp)-1] = '\0';
  215. dest.append(temp);
  216. } else {
  217. dest += target[i];
  218. }
  219. }
  220. return dest;
  221. }
  222. string Util::torrentUrlencode(const unsigned char* target, int len) {
  223. string dest;
  224. for(int i = 0; i < len; i++) {
  225. if('0' <= target[i] && target[i] <= '9' ||
  226. 'A' <= target[i] && target[i] <= 'Z' ||
  227. 'a' <= target[i] && target[i] <= 'z') {
  228. dest += target[i];
  229. } else {
  230. char temp[4];
  231. sprintf(temp, "%%%02x", target[i]);
  232. temp[sizeof(temp)-1] = '\0';
  233. dest.append(temp);
  234. }
  235. }
  236. return dest;
  237. }
  238. string Util::urldecode(const string& target) {
  239. string result;
  240. for(string::const_iterator itr = target.begin();
  241. itr != target.end(); itr++) {
  242. if(*itr == '%') {
  243. if(itr+1 != target.end() && itr+2 != target.end() &&
  244. isxdigit(*(itr+1)) && isxdigit(*(itr+2))) {
  245. char temp[3];
  246. temp[0] = *(itr+1);
  247. temp[1] = *(itr+2);
  248. temp[2] = '\0';
  249. result += strtol(temp, 0, 16);
  250. itr += 2;
  251. } else {
  252. result += *itr;
  253. }
  254. } else {
  255. result += *itr;
  256. }
  257. }
  258. return result;
  259. }
  260. string Util::toHex(const unsigned char* src, int len) {
  261. char* temp = new char[len*2+1];
  262. for(int i = 0; i < len; i++) {
  263. sprintf(temp+i*2, "%02x", src[i]);
  264. }
  265. temp[len*2] = '\0';
  266. string hex = temp;
  267. delete [] temp;
  268. return hex;
  269. }
  270. FILE* Util::openFile(const string& filename, const string& mode) {
  271. FILE* file = fopen(filename.c_str(), mode.c_str());
  272. return file;
  273. }
  274. void Util::fileCopy(const string& dest, const string& src) {
  275. File file(src);
  276. rangedFileCopy(dest, src, 0, file.size());
  277. }
  278. void Util::rangedFileCopy(const string& dest, const string& src, long long int srcOffset, long long int length) {
  279. int bufSize = 4096;
  280. char buf[bufSize];
  281. int destFd = -1;
  282. int srcFd = -1;
  283. try {
  284. if((destFd = open(dest.c_str(), O_CREAT|O_WRONLY|O_TRUNC, S_IRUSR|S_IWUSR)) == -1) {
  285. throw new DlAbortEx(EX_FILE_OPEN, dest.c_str(), strerror(errno));
  286. }
  287. if((srcFd = open(src.c_str(), O_RDONLY, S_IRUSR|S_IWUSR)) == -1) {
  288. throw new DlAbortEx(EX_FILE_OPEN, src.c_str(), strerror(errno));
  289. }
  290. if(lseek(srcFd, srcOffset, SEEK_SET) != srcOffset) {
  291. throw new DlAbortEx(EX_FILE_SEEK, src.c_str(), strerror(errno));
  292. }
  293. int x = length/bufSize;
  294. int r = length%bufSize;
  295. for(int i = 0; i < x; i++) {
  296. int readLength;
  297. if((readLength = read(srcFd, buf, bufSize)) == -1 || readLength != bufSize) {
  298. throw new DlAbortEx(EX_FILE_READ, src.c_str(), strerror(errno));
  299. }
  300. if(write(destFd, buf, readLength) == -1) {
  301. throw new DlAbortEx(EX_FILE_WRITE, dest.c_str(), strerror(errno));
  302. }
  303. }
  304. if(r > 0) {
  305. int readLength;
  306. if((readLength = read(srcFd, buf, r)) == -1 || readLength != r) {
  307. throw new DlAbortEx(EX_FILE_READ, src.c_str(), strerror(errno));
  308. }
  309. if(write(destFd, buf, r) == -1) {
  310. throw new DlAbortEx(EX_FILE_WRITE, dest.c_str(), strerror(errno));
  311. }
  312. }
  313. close(srcFd);
  314. close(destFd);
  315. srcFd = -1;
  316. destFd = -1;
  317. } catch(RecoverableException* e) {
  318. if(srcFd != -1) {
  319. close(srcFd);
  320. }
  321. if(destFd != -1) {
  322. close(destFd);
  323. }
  324. throw;
  325. }
  326. }
  327. bool Util::isPowerOf(int num, int base) {
  328. if(base <= 0) { return false; }
  329. if(base == 1) { return true; }
  330. while(num%base == 0) {
  331. num /= base;
  332. if(num == 1) {
  333. return true;
  334. }
  335. }
  336. return false;
  337. }
  338. string Util::secfmt(int sec) {
  339. string str;
  340. if(sec >= 3600) {
  341. str = itos(sec/3600)+"h";
  342. sec %= 3600;
  343. }
  344. if(sec >= 60) {
  345. int min = sec/60;
  346. if(min < 10) {
  347. str += "0";
  348. }
  349. str += itos(min)+"m";
  350. sec %= 60;
  351. }
  352. if(sec < 10) {
  353. str += "0";
  354. }
  355. str += itos(sec)+"s";
  356. return str;
  357. }
  358. int Util::expandBuffer(char** pbuf, int curLength, int newLength) {
  359. char* newbuf = new char[newLength];
  360. memcpy(newbuf, *pbuf, curLength);
  361. delete [] *pbuf;
  362. *pbuf = newbuf;
  363. return newLength;
  364. }
  365. int getNum(const char* buf, int offset, int length) {
  366. char* temp = new char[length+1];
  367. memcpy(temp, buf+offset, length);
  368. temp[length] = '\0';
  369. int x = strtol(temp, NULL, 10);
  370. delete [] temp;
  371. return x;
  372. }
  373. void unfoldSubRange(const string& src, Integers& range) {
  374. if(src.empty()) {
  375. return;
  376. }
  377. string::size_type p = src.find_first_of(",-");
  378. if(p == 0) {
  379. return;
  380. } else if(p == string::npos) {
  381. range.push_back(atoi(src.c_str()));
  382. } else {
  383. if(src.at(p) == ',') {
  384. int num = getNum(src.c_str(), 0, p);
  385. range.push_back(num);
  386. unfoldSubRange(src.substr(p+1), range);
  387. } else if(src.at(p) == '-') {
  388. int rightNumBegin = p+1;
  389. string::size_type nextDelim = src.find_first_of(",", rightNumBegin);
  390. if(nextDelim == string::npos) {
  391. nextDelim = src.size();
  392. }
  393. int left = getNum(src.c_str(), 0, p);
  394. int right = getNum(src.c_str(), rightNumBegin, nextDelim-rightNumBegin);
  395. for(int i = left; i <= right; i++) {
  396. range.push_back(i);
  397. }
  398. if(src.size() > nextDelim) {
  399. unfoldSubRange(src.substr(nextDelim+1), range);
  400. }
  401. }
  402. }
  403. }
  404. void Util::unfoldRange(const string& src, Integers& range) {
  405. unfoldSubRange(src, range);
  406. sort(range.begin(), range.end());
  407. range.erase(unique(range.begin(), range.end()), range.end());
  408. }
  409. string Util::getContentDispositionFilename(const string& header) {
  410. string::size_type attributesp = header.find("filename=\"");
  411. if(attributesp == string::npos) {
  412. return "";
  413. }
  414. string::size_type filenamesp = attributesp+strlen("filename=\"");
  415. string::size_type filenameep = header.find("\"", filenamesp);
  416. if(filenameep == string::npos) {
  417. return "";
  418. }
  419. return trim(header.substr(filenamesp, filenameep-filenamesp));
  420. }
  421. #ifdef ENABLE_MESSAGE_DIGEST
  422. void Util::sha1Sum(unsigned char* digest, const void* data, int dataLength) {
  423. MessageDigestContext ctx(DIGEST_ALGO_SHA1);
  424. ctx.digestInit();
  425. ctx.digestUpdate(data, dataLength);
  426. ctx.digestFinal(digest);
  427. ctx.digestFree();
  428. }
  429. string Util::simpleMessageDigest(const string& data) {
  430. unsigned char checksum[20];
  431. sha1Sum(checksum, data.c_str(), data.size());
  432. return Util::toHex(checksum, sizeof(checksum));
  433. }
  434. #endif // ENABLE_MESSAGE_DIGEST
  435. #ifdef ENABLE_MESSAGE_DIGEST
  436. void Util::fileChecksum(const string& filename, unsigned char* digest,
  437. MessageDigestContext::DigestAlgo algo) {
  438. MessageDigestContext ctx(algo);
  439. ctx.digestInit();
  440. int BUFLEN = 4096;
  441. char buf[BUFLEN];
  442. int fd;
  443. if((fd = open(filename.c_str(), O_RDWR, S_IRUSR|S_IWUSR)) < 0) {
  444. throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
  445. }
  446. while(1) {
  447. int size = read(fd, buf, BUFLEN);
  448. if(size == -1) {
  449. if(errno == EINTR) {
  450. continue;
  451. } else {
  452. close(fd);
  453. throw new DlAbortEx(EX_FILE_READ, filename.c_str(), strerror(errno));
  454. }
  455. } else if(size > 0) {
  456. ctx.digestUpdate(buf, size);
  457. }
  458. if(size < BUFLEN) {
  459. break;
  460. }
  461. }
  462. ctx.digestFinal(digest);
  463. ctx.digestFree();
  464. }
  465. #endif // ENABLE_MESSAGE_DIGEST
  466. #ifdef ENABLE_BITTORRENT
  467. Integers Util::computeFastSet(string ipaddr, const unsigned char* infoHash,
  468. int pieces, int fastSetSize) {
  469. Integers fastSet;
  470. struct in_addr saddr;
  471. if(inet_aton(ipaddr.c_str(), &saddr) == 0) {
  472. abort();
  473. }
  474. unsigned char tx[24];
  475. memcpy(tx, (void*)&saddr.s_addr, 4);
  476. if((tx[0] & 0x80) == 0 || (tx[0] & 0x40) == 0) {
  477. tx[2] = 0x00;
  478. tx[3] = 0x00;
  479. } else {
  480. tx[3] = 0x00;
  481. }
  482. memcpy(tx+4, infoHash, 20);
  483. unsigned char x[20];
  484. sha1Sum(x, tx, 24);
  485. while((int)fastSet.size() < fastSetSize) {
  486. for(int i = 0; i < 5 && (int)fastSet.size() < fastSetSize; i++) {
  487. int j = i*4;
  488. unsigned int ny;
  489. memcpy(&ny, x+j, 4);
  490. unsigned int y = ntohl(ny);
  491. int index = y%pieces;
  492. if(find(fastSet.begin(), fastSet.end(), index) == fastSet.end()) {
  493. fastSet.push_back(index);
  494. }
  495. }
  496. unsigned char temp[20];
  497. sha1Sum(temp, x, 20);
  498. memcpy(x, temp, sizeof(x));
  499. }
  500. return fastSet;
  501. }
  502. #endif // ENABLE_BITTORRENT
  503. /*
  504. int Util::countBit(unsigned int n) {
  505. int count = 0;
  506. while(n > 0) {
  507. count++;
  508. n &= (n-1);
  509. }
  510. return count;
  511. }
  512. */
  513. static int nbits[] = {
  514. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  515. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  516. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  517. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  518. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  519. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  520. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  521. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  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. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  525. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  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. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  529. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
  530. };
  531. uint32_t Util::countBit(uint32_t n) {
  532. return
  533. nbits[n&0xffu]+
  534. nbits[(n >> 8)&0xffu]+
  535. nbits[(n >> 16)&0xffu]+
  536. nbits[(n >> 24)&0xffu];
  537. }
  538. string Util::randomAlpha(int length) {
  539. string str;
  540. for(int i = 0; i < length; i++) {
  541. int index = (int)(((double)52)*random()/(RAND_MAX+1.0));
  542. char ch;
  543. if(index < 26) {
  544. ch = (char)('A'+index);
  545. } else {
  546. ch = (char)('a'+index-26);
  547. }
  548. str += ch;
  549. }
  550. return str;
  551. }
  552. class UpperCase {
  553. public:
  554. void operator()(char& ch) {
  555. ch = toupper(ch);
  556. }
  557. };
  558. string Util::toUpper(const string& src) {
  559. string temp = src;
  560. for_each(temp.begin(), temp.end(), UpperCase());
  561. return temp;
  562. }
  563. class LowerCase {
  564. public:
  565. void operator()(char& ch) {
  566. ch = tolower(ch);
  567. }
  568. };
  569. string Util::toLower(const string& src) {
  570. string temp = src;
  571. for_each(temp.begin(), temp.end(), LowerCase());
  572. return temp;
  573. }
  574. bool Util::isNumbersAndDotsNotation(const string& name) {
  575. struct sockaddr_in sockaddr;
  576. if(inet_aton(name.c_str(), &sockaddr.sin_addr)) {
  577. return true;
  578. } else {
  579. return false;
  580. }
  581. }
  582. void Util::setGlobalSignalHandler(int signal, void (*handler)(int), int flags) {
  583. struct sigaction sigact;
  584. sigact.sa_handler = handler;
  585. sigact.sa_flags = flags;
  586. sigemptyset(&sigact.sa_mask);
  587. sigaction(signal, &sigact, NULL);
  588. }