Util.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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. pair<string, string> Util::split(const string& src, const string& delims)
  123. {
  124. pair<string, string> hp;
  125. hp.first = "";
  126. hp.second = "";
  127. string::size_type p = src.find_first_of(delims);
  128. if(p == string::npos) {
  129. hp.first = src;
  130. hp.second = "";
  131. } else {
  132. hp.first = trim(src.substr(0, p));
  133. hp.second = trim(src.substr(p+1));
  134. }
  135. return hp;
  136. }
  137. long long int Util::difftv(struct timeval tv1, struct timeval tv2) {
  138. if(tv1.tv_sec < tv2.tv_sec || tv1.tv_sec == tv2.tv_sec && tv1.tv_usec < tv2.tv_usec) {
  139. return 0;
  140. }
  141. return ((long long int)(tv1.tv_sec-tv2.tv_sec)*1000000+
  142. tv1.tv_usec-tv2.tv_usec);
  143. }
  144. int Util::difftvsec(struct timeval tv1, struct timeval tv2) {
  145. if(tv1.tv_sec < tv2.tv_sec) {
  146. return 0;
  147. }
  148. return tv1.tv_sec-tv2.tv_sec;
  149. }
  150. void Util::slice(Strings& result, const string& src, char delim, bool doTrim) {
  151. string::size_type p = 0;
  152. while(1) {
  153. string::size_type np = src.find(delim, p);
  154. if(np == string::npos) {
  155. string term = src.substr(p);
  156. if(doTrim) {
  157. term = trim(term);
  158. }
  159. if(term.size()) {
  160. result.push_back(term);
  161. }
  162. break;
  163. }
  164. string term = src.substr(p, np-p);
  165. if(doTrim) {
  166. term = trim(term);
  167. }
  168. p = np+1;
  169. if(term.size()) {
  170. result.push_back(term);
  171. }
  172. }
  173. }
  174. bool Util::startsWith(const string& target, const string& part) {
  175. if(target.size() < part.size()) {
  176. return false;
  177. }
  178. if(part == "") {
  179. return true;
  180. }
  181. if(target.find(part) == 0) {
  182. return true;
  183. } else {
  184. return false;
  185. }
  186. }
  187. bool Util::endsWith(const string& target, const string& part) {
  188. if(target.size() < part.size()) {
  189. return false;
  190. }
  191. if(part == "") {
  192. return true;
  193. }
  194. if(target.rfind(part) == target.size()-part.size()) {
  195. return true;
  196. } else {
  197. return false;
  198. }
  199. }
  200. string Util::replace(const string& target, const string& oldstr, const string& newstr) {
  201. if(target == "" || oldstr == "" ) {
  202. return target;
  203. }
  204. string result;
  205. string::size_type p = 0;
  206. string::size_type np = target.find(oldstr);
  207. while(np != string::npos) {
  208. result += target.substr(p, np-p)+newstr;
  209. p = np+oldstr.size();
  210. np = target.find(oldstr, p);
  211. }
  212. result += target.substr(p);
  213. return result;
  214. }
  215. string Util::urlencode(const unsigned char* target, int len) {
  216. string dest;
  217. for(int i = 0; i < len; i++) {
  218. if(!('0' <= target[i] && target[i] <= '9' ||
  219. 'A' <= target[i] && target[i] <= 'Z' ||
  220. 'a' <= target[i] && target[i] <= 'z' ||
  221. '$' == target[i] || '-' == target[i] ||
  222. '_' == target[i] || '.' == target[i] ||
  223. '+' == target[i] || '!' == target[i] ||
  224. '*' == target[i] || '\'' == target[i] ||
  225. '(' == target[i] || ')' == target[i] ||
  226. ',' == target[i])) {
  227. char temp[4];
  228. sprintf(temp, "%%%02x", target[i]);
  229. temp[sizeof(temp)-1] = '\0';
  230. dest.append(temp);
  231. } else {
  232. dest += target[i];
  233. }
  234. }
  235. return dest;
  236. }
  237. string Util::torrentUrlencode(const unsigned char* target, int len) {
  238. string dest;
  239. for(int i = 0; i < len; i++) {
  240. if('0' <= target[i] && target[i] <= '9' ||
  241. 'A' <= target[i] && target[i] <= 'Z' ||
  242. 'a' <= target[i] && target[i] <= 'z') {
  243. dest += target[i];
  244. } else {
  245. char temp[4];
  246. sprintf(temp, "%%%02x", target[i]);
  247. temp[sizeof(temp)-1] = '\0';
  248. dest.append(temp);
  249. }
  250. }
  251. return dest;
  252. }
  253. string Util::urldecode(const string& target) {
  254. string result;
  255. for(string::const_iterator itr = target.begin();
  256. itr != target.end(); itr++) {
  257. if(*itr == '%') {
  258. if(itr+1 != target.end() && itr+2 != target.end() &&
  259. isxdigit(*(itr+1)) && isxdigit(*(itr+2))) {
  260. char temp[3];
  261. temp[0] = *(itr+1);
  262. temp[1] = *(itr+2);
  263. temp[2] = '\0';
  264. result += strtol(temp, 0, 16);
  265. itr += 2;
  266. } else {
  267. result += *itr;
  268. }
  269. } else {
  270. result += *itr;
  271. }
  272. }
  273. return result;
  274. }
  275. string Util::toHex(const unsigned char* src, int len) {
  276. char* temp = new char[len*2+1];
  277. for(int i = 0; i < len; i++) {
  278. sprintf(temp+i*2, "%02x", src[i]);
  279. }
  280. temp[len*2] = '\0';
  281. string hex = temp;
  282. delete [] temp;
  283. return hex;
  284. }
  285. FILE* Util::openFile(const string& filename, const string& mode) {
  286. FILE* file = fopen(filename.c_str(), mode.c_str());
  287. return file;
  288. }
  289. void Util::fileCopy(const string& dest, const string& src) {
  290. File file(src);
  291. rangedFileCopy(dest, src, 0, file.size());
  292. }
  293. void Util::rangedFileCopy(const string& dest, const string& src, long long int srcOffset, long long int length) {
  294. int bufSize = 4096;
  295. char buf[bufSize];
  296. int destFd = -1;
  297. int srcFd = -1;
  298. try {
  299. if((destFd = open(dest.c_str(), O_CREAT|O_WRONLY|O_TRUNC, OPEN_MODE)) == -1) {
  300. throw new DlAbortEx(EX_FILE_OPEN, dest.c_str(), strerror(errno));
  301. }
  302. if((srcFd = open(src.c_str(), O_RDONLY, OPEN_MODE)) == -1) {
  303. throw new DlAbortEx(EX_FILE_OPEN, src.c_str(), strerror(errno));
  304. }
  305. if(lseek(srcFd, srcOffset, SEEK_SET) != srcOffset) {
  306. throw new DlAbortEx(EX_FILE_SEEK, src.c_str(), strerror(errno));
  307. }
  308. int x = length/bufSize;
  309. int r = length%bufSize;
  310. for(int i = 0; i < x; i++) {
  311. int readLength;
  312. if((readLength = read(srcFd, buf, bufSize)) == -1 || readLength != bufSize) {
  313. throw new DlAbortEx(EX_FILE_READ, src.c_str(), strerror(errno));
  314. }
  315. if(write(destFd, buf, readLength) == -1) {
  316. throw new DlAbortEx(EX_FILE_WRITE, dest.c_str(), strerror(errno));
  317. }
  318. }
  319. if(r > 0) {
  320. int readLength;
  321. if((readLength = read(srcFd, buf, r)) == -1 || readLength != r) {
  322. throw new DlAbortEx(EX_FILE_READ, src.c_str(), strerror(errno));
  323. }
  324. if(write(destFd, buf, r) == -1) {
  325. throw new DlAbortEx(EX_FILE_WRITE, dest.c_str(), strerror(errno));
  326. }
  327. }
  328. close(srcFd);
  329. close(destFd);
  330. srcFd = -1;
  331. destFd = -1;
  332. } catch(RecoverableException* e) {
  333. if(srcFd != -1) {
  334. close(srcFd);
  335. }
  336. if(destFd != -1) {
  337. close(destFd);
  338. }
  339. throw;
  340. }
  341. }
  342. bool Util::isPowerOf(int num, int base) {
  343. if(base <= 0) { return false; }
  344. if(base == 1) { return true; }
  345. while(num%base == 0) {
  346. num /= base;
  347. if(num == 1) {
  348. return true;
  349. }
  350. }
  351. return false;
  352. }
  353. string Util::secfmt(int sec) {
  354. string str;
  355. if(sec >= 3600) {
  356. str = itos(sec/3600)+"h";
  357. sec %= 3600;
  358. }
  359. if(sec >= 60) {
  360. int min = sec/60;
  361. if(min < 10) {
  362. str += "0";
  363. }
  364. str += itos(min)+"m";
  365. sec %= 60;
  366. }
  367. if(sec < 10) {
  368. str += "0";
  369. }
  370. str += itos(sec)+"s";
  371. return str;
  372. }
  373. int Util::expandBuffer(char** pbuf, int curLength, int newLength) {
  374. char* newbuf = new char[newLength];
  375. memcpy(newbuf, *pbuf, curLength);
  376. delete [] *pbuf;
  377. *pbuf = newbuf;
  378. return newLength;
  379. }
  380. int getNum(const char* buf, int offset, int length) {
  381. char* temp = new char[length+1];
  382. memcpy(temp, buf+offset, length);
  383. temp[length] = '\0';
  384. int x = strtol(temp, NULL, 10);
  385. delete [] temp;
  386. return x;
  387. }
  388. void unfoldSubRange(const string& src, Integers& range) {
  389. if(src.empty()) {
  390. return;
  391. }
  392. string::size_type p = src.find_first_of(",-");
  393. if(p == 0) {
  394. return;
  395. } else if(p == string::npos) {
  396. range.push_back(atoi(src.c_str()));
  397. } else {
  398. if(src.at(p) == ',') {
  399. int num = getNum(src.c_str(), 0, p);
  400. range.push_back(num);
  401. unfoldSubRange(src.substr(p+1), range);
  402. } else if(src.at(p) == '-') {
  403. int rightNumBegin = p+1;
  404. string::size_type nextDelim = src.find_first_of(",", rightNumBegin);
  405. if(nextDelim == string::npos) {
  406. nextDelim = src.size();
  407. }
  408. int left = getNum(src.c_str(), 0, p);
  409. int right = getNum(src.c_str(), rightNumBegin, nextDelim-rightNumBegin);
  410. for(int i = left; i <= right; i++) {
  411. range.push_back(i);
  412. }
  413. if(src.size() > nextDelim) {
  414. unfoldSubRange(src.substr(nextDelim+1), range);
  415. }
  416. }
  417. }
  418. }
  419. void Util::unfoldRange(const string& src, Integers& range) {
  420. unfoldSubRange(src, range);
  421. sort(range.begin(), range.end());
  422. range.erase(unique(range.begin(), range.end()), range.end());
  423. }
  424. string Util::getContentDispositionFilename(const string& header) {
  425. string::size_type attributesp = header.find("filename=\"");
  426. if(attributesp == string::npos) {
  427. return "";
  428. }
  429. string::size_type filenamesp = attributesp+strlen("filename=\"");
  430. string::size_type filenameep = header.find("\"", filenamesp);
  431. if(filenameep == string::npos) {
  432. return "";
  433. }
  434. return trim(header.substr(filenamesp, filenameep-filenamesp));
  435. }
  436. #ifdef ENABLE_MESSAGE_DIGEST
  437. void Util::sha1Sum(unsigned char* digest, const void* data, int dataLength) {
  438. MessageDigestContext ctx(DIGEST_ALGO_SHA1);
  439. ctx.digestInit();
  440. ctx.digestUpdate(data, dataLength);
  441. ctx.digestFinal(digest);
  442. }
  443. string Util::simpleMessageDigest(const string& data) {
  444. unsigned char checksum[20];
  445. sha1Sum(checksum, data.c_str(), data.size());
  446. return Util::toHex(checksum, sizeof(checksum));
  447. }
  448. #endif // ENABLE_MESSAGE_DIGEST
  449. #ifdef ENABLE_MESSAGE_DIGEST
  450. void Util::fileChecksum(const string& filename, unsigned char* digest,
  451. MessageDigestContext::DigestAlgo algo) {
  452. MessageDigestContext ctx(algo);
  453. ctx.digestInit();
  454. int BUFLEN = 4096;
  455. char buf[BUFLEN];
  456. int fd;
  457. if((fd = open(filename.c_str(), O_RDWR, OPEN_MODE)) < 0) {
  458. throw new DlAbortEx(EX_FILE_OPEN, filename.c_str(), strerror(errno));
  459. }
  460. while(1) {
  461. int size = read(fd, buf, BUFLEN);
  462. if(size == -1) {
  463. if(errno == EINTR) {
  464. continue;
  465. } else {
  466. close(fd);
  467. throw new DlAbortEx(EX_FILE_READ, filename.c_str(), strerror(errno));
  468. }
  469. } else if(size > 0) {
  470. ctx.digestUpdate(buf, size);
  471. }
  472. if(size < BUFLEN) {
  473. break;
  474. }
  475. }
  476. ctx.digestFinal(digest);
  477. }
  478. #endif // ENABLE_MESSAGE_DIGEST
  479. #ifdef ENABLE_BITTORRENT
  480. Integers Util::computeFastSet(string ipaddr, const unsigned char* infoHash,
  481. int pieces, int fastSetSize) {
  482. Integers fastSet;
  483. struct in_addr saddr;
  484. if(inet_aton(ipaddr.c_str(), &saddr) == 0) {
  485. abort();
  486. }
  487. unsigned char tx[24];
  488. memcpy(tx, (void*)&saddr.s_addr, 4);
  489. if((tx[0] & 0x80) == 0 || (tx[0] & 0x40) == 0) {
  490. tx[2] = 0x00;
  491. tx[3] = 0x00;
  492. } else {
  493. tx[3] = 0x00;
  494. }
  495. memcpy(tx+4, infoHash, 20);
  496. unsigned char x[20];
  497. sha1Sum(x, tx, 24);
  498. while((int)fastSet.size() < fastSetSize) {
  499. for(int i = 0; i < 5 && (int)fastSet.size() < fastSetSize; i++) {
  500. int j = i*4;
  501. unsigned int ny;
  502. memcpy(&ny, x+j, 4);
  503. unsigned int y = ntohl(ny);
  504. int index = y%pieces;
  505. if(find(fastSet.begin(), fastSet.end(), index) == fastSet.end()) {
  506. fastSet.push_back(index);
  507. }
  508. }
  509. unsigned char temp[20];
  510. sha1Sum(temp, x, 20);
  511. memcpy(x, temp, sizeof(x));
  512. }
  513. return fastSet;
  514. }
  515. #endif // ENABLE_BITTORRENT
  516. /*
  517. int Util::countBit(unsigned int n) {
  518. int count = 0;
  519. while(n > 0) {
  520. count++;
  521. n &= (n-1);
  522. }
  523. return count;
  524. }
  525. */
  526. static int nbits[] = {
  527. 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
  528. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  529. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  530. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  531. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  532. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  533. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  534. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  535. 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
  536. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  537. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  538. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  539. 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
  540. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  541. 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
  542. 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
  543. };
  544. int32_t Util::countBit(uint32_t n) {
  545. return
  546. nbits[n&0xffu]+
  547. nbits[(n >> 8)&0xffu]+
  548. nbits[(n >> 16)&0xffu]+
  549. nbits[(n >> 24)&0xffu];
  550. }
  551. string Util::randomAlpha(int length) {
  552. string str;
  553. for(int i = 0; i < length; i++) {
  554. int index = (int)(((double)52)*random()/(RAND_MAX+1.0));
  555. char ch;
  556. if(index < 26) {
  557. ch = (char)('A'+index);
  558. } else {
  559. ch = (char)('a'+index-26);
  560. }
  561. str += ch;
  562. }
  563. return str;
  564. }
  565. class UpperCase {
  566. public:
  567. void operator()(char& ch) {
  568. ch = toupper(ch);
  569. }
  570. };
  571. string Util::toUpper(const string& src) {
  572. string temp = src;
  573. for_each(temp.begin(), temp.end(), UpperCase());
  574. return temp;
  575. }
  576. class LowerCase {
  577. public:
  578. void operator()(char& ch) {
  579. ch = tolower(ch);
  580. }
  581. };
  582. string Util::toLower(const string& src) {
  583. string temp = src;
  584. for_each(temp.begin(), temp.end(), LowerCase());
  585. return temp;
  586. }
  587. bool Util::isNumbersAndDotsNotation(const string& name) {
  588. struct sockaddr_in sockaddr;
  589. if(inet_aton(name.c_str(), &sockaddr.sin_addr)) {
  590. return true;
  591. } else {
  592. return false;
  593. }
  594. }
  595. void Util::setGlobalSignalHandler(int signal, void (*handler)(int), int flags) {
  596. struct sigaction sigact;
  597. sigact.sa_handler = handler;
  598. sigact.sa_flags = flags;
  599. sigemptyset(&sigact.sa_mask);
  600. sigaction(signal, &sigact, NULL);
  601. }
  602. void Util::indexRange(int32_t& startIndex, int32_t& endIndex,
  603. int64_t offset, int32_t srcLength, int32_t destLength)
  604. {
  605. int64_t _startIndex = offset/destLength;
  606. int64_t _endIndex = (offset+srcLength-1)/destLength;
  607. assert(_startIndex <= INT32_MAX);
  608. assert(_endIndex <= INT32_MAX);
  609. startIndex = _startIndex;
  610. endIndex = _endIndex;
  611. }
  612. string Util::getHomeDir()
  613. {
  614. const char* p = getenv("HOME");
  615. if(p) {
  616. return p;
  617. } else {
  618. return "";
  619. }
  620. }
  621. int64_t Util::getRealSize(const string& sizeWithUnit)
  622. {
  623. string::size_type p = sizeWithUnit.find_first_of("KM");
  624. string size;
  625. int mult = 1;
  626. if(p == string::npos) {
  627. size = sizeWithUnit;
  628. } else {
  629. if(sizeWithUnit[p] == 'K') {
  630. mult = 1024;
  631. } else if(sizeWithUnit[p] == 'M') {
  632. mult = 1024*1024;
  633. }
  634. size = sizeWithUnit.substr(0, p);
  635. }
  636. return strtoll(size.c_str(), 0, 10)*mult;
  637. }
  638. string Util::abbrevSize(int64_t size)
  639. {
  640. if(size < 1024) {
  641. return Util::llitos(size, true);
  642. }
  643. size >>= 10;
  644. char units[] = { 'K', 'M' };
  645. int32_t numUnit = sizeof(units)/sizeof(char);
  646. int32_t i = 0;
  647. for(; i < numUnit-1 && size >= 1024; ++i) {
  648. size >>= 10;
  649. }
  650. return Util::llitos(size, true)+units[i];
  651. }
  652. time_t Util::httpGMT(const string& httpStdTime)
  653. {
  654. struct tm tm;
  655. memset(&tm, 0, sizeof(tm));
  656. strptime(httpStdTime.c_str(), "%a, %Y-%m-%d %H:%M:%S GMT", &tm);
  657. time_t thetime = timegm(&tm);
  658. return thetime;
  659. }