util.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  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. #ifndef D_UTIL_H
  36. #define D_UTIL_H
  37. #include "common.h"
  38. #include <sys/time.h>
  39. #include <limits.h>
  40. #include <stdint.h>
  41. #include <cstdio>
  42. #include <cstring>
  43. #include <string>
  44. #include <utility>
  45. #include <iosfwd>
  46. #include <ostream>
  47. #include <numeric>
  48. #include <map>
  49. #include <iomanip>
  50. #include <algorithm>
  51. #include <vector>
  52. #include <memory>
  53. #include "a2time.h"
  54. #include "a2netcompat.h"
  55. #include "a2functional.h"
  56. #include "SegList.h"
  57. #include "a2iterator.h"
  58. #include "message.h"
  59. #include "DlAbortEx.h"
  60. #include "fmt.h"
  61. #ifndef HAVE_SIGACTION
  62. # define sigset_t int
  63. #endif // HAVE_SIGACTION
  64. namespace aria2 {
  65. class Randomizer;
  66. class BitfieldMan;
  67. class BinaryStream;
  68. class FileEntry;
  69. class RequestGroup;
  70. class Option;
  71. struct Pref;
  72. #define STRTOLL(X) strtoll(X, reinterpret_cast<char**>(0), 10)
  73. #define STRTOULL(X) strtoull(X, reinterpret_cast<char**>(0), 10)
  74. #define START_INDEX(OFFSET, PIECE_LENGTH) ((OFFSET)/(PIECE_LENGTH))
  75. #define END_INDEX(OFFSET, LENGTH, PIECE_LENGTH) (((OFFSET)+(LENGTH)-1)/(PIECE_LENGTH))
  76. #define DIV_FLOOR(X,Y) ((X)/(Y)+((X)%(Y)? 1:0))
  77. #ifdef WORDS_BIGENDIAN
  78. inline uint64_t ntoh64(uint64_t x) { return x; }
  79. inline uint64_t hton64(uint64_t x) { return x; }
  80. #else // !WORDS_BIGENDIAN
  81. inline uint64_t byteswap64(uint64_t x) {
  82. uint64_t v1 = ntohl(x & 0x00000000ffffffffllu);
  83. uint64_t v2 = ntohl(x >> 32);
  84. return (v1 << 32)|v2;
  85. }
  86. inline uint64_t ntoh64(uint64_t x) { return byteswap64(x); }
  87. inline uint64_t hton64(uint64_t x) { return byteswap64(x); }
  88. #endif // !WORDS_BIGENDIAN
  89. #ifdef __MINGW32__
  90. std::wstring utf8ToWChar(const std::string& src);
  91. std::wstring utf8ToWChar(const char* str);
  92. std::string utf8ToNative(const std::string& src);
  93. std::string wCharToUtf8(const std::wstring& wsrc);
  94. std::string nativeToUtf8(const std::string& src);
  95. #else // !__MINGW32__
  96. # define utf8ToWChar(src) src
  97. # define utf8ToNative(src) src
  98. #endif // !__MINGW32__
  99. namespace util {
  100. extern const char DEFAULT_STRIP_CHARSET[];
  101. template<typename InputIterator>
  102. std::pair<InputIterator, InputIterator> stripIter
  103. (InputIterator first, InputIterator last,
  104. const char* chars = DEFAULT_STRIP_CHARSET)
  105. {
  106. for(; first != last && strchr(chars, *first) != 0; ++first);
  107. if(first == last) {
  108. return std::make_pair(first, last);
  109. }
  110. InputIterator left = last-1;
  111. for(; left != first && strchr(chars, *left) != 0; --left);
  112. return std::make_pair(first, left+1);
  113. }
  114. template<typename InputIterator>
  115. InputIterator lstripIter
  116. (InputIterator first, InputIterator last, char ch)
  117. {
  118. for(; first != last && *first == ch; ++first);
  119. return first;
  120. }
  121. template<typename InputIterator>
  122. InputIterator lstripIter
  123. (InputIterator first, InputIterator last, const char* chars)
  124. {
  125. for(; first != last && strchr(chars, *first) != 0; ++first);
  126. return first;
  127. }
  128. template<typename InputIterator>
  129. InputIterator lstripIter
  130. (InputIterator first, InputIterator last)
  131. {
  132. return lstripIter(first, last, DEFAULT_STRIP_CHARSET);
  133. }
  134. std::string strip
  135. (const std::string& str, const char* chars = DEFAULT_STRIP_CHARSET);
  136. template<typename InputIterator>
  137. std::pair<std::pair<InputIterator, InputIterator>,
  138. std::pair<InputIterator, InputIterator>>
  139. divide(InputIterator first, InputIterator last, char delim)
  140. {
  141. auto dpos = std::find(first, last, delim);
  142. if(dpos == last) {
  143. return {stripIter(first, last), {last, last}};
  144. } else {
  145. return {stripIter(first, dpos), stripIter(dpos+1, last)};
  146. }
  147. }
  148. template<typename T>
  149. std::string uitos(T n, bool comma = false)
  150. {
  151. std::string res;
  152. if(n == 0) {
  153. res = "0";
  154. return res;
  155. }
  156. int i = 0;
  157. T t = n;
  158. for(; t; t /= 10, ++i);
  159. if(comma) {
  160. i += (i-1)/3;
  161. }
  162. res.resize(i);
  163. --i;
  164. for(int j = 0; n; --i, ++j, n /= 10) {
  165. res[i] = (n%10) + '0';
  166. if(comma && i > 1 && (j+1)%3 == 0) {
  167. res[--i] = ',';
  168. }
  169. }
  170. return res;
  171. }
  172. std::string itos(int64_t value, bool comma = false);
  173. /**
  174. * Computes difference in micro-seconds between tv1 and tv2,
  175. * assuming tv1 is newer than tv2.
  176. * If tv1 is older than tv2, then this method returns 0.
  177. */
  178. int64_t difftv(struct timeval tv1, struct timeval tv2);
  179. int32_t difftvsec(struct timeval tv1, struct timeval tv2);
  180. std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr);
  181. std::string percentEncode(const unsigned char* target, size_t len);
  182. std::string percentEncode(const std::string& target);
  183. std::string percentEncodeMini(const std::string& target);
  184. bool inRFC3986ReservedChars(const char c);
  185. bool inRFC3986UnreservedChars(const char c);
  186. bool inRFC2978MIMECharset(const char c);
  187. bool inRFC2616HttpToken(const char c);
  188. bool inRFC5987AttrChar(const char c);
  189. // Returns true if |c| is in ISO/IEC 8859-1 character set.
  190. bool isIso8859p1(unsigned char c);
  191. bool isUtf8(const std::string& str);
  192. std::string percentDecode
  193. (std::string::const_iterator first, std::string::const_iterator last);
  194. std::string torrentPercentEncode(const unsigned char* target, size_t len);
  195. std::string torrentPercentEncode(const std::string& target);
  196. std::string toHex(const unsigned char* src, size_t len);
  197. std::string toHex(const char* src, size_t len);
  198. std::string toHex(const std::string& src);
  199. unsigned int hexCharToUInt(unsigned char ch);
  200. // Converts hexadecimal ascii characters in [first, last) into packed
  201. // binary form and return the result. If characters in given range is
  202. // not well formed, then empty string is returned.
  203. template<typename InputIterator>
  204. std::string fromHex(InputIterator first, InputIterator last)
  205. {
  206. std::string dest;
  207. size_t len = last-first;
  208. if(len%2) {
  209. return dest;
  210. }
  211. for(; first != last; first += 2) {
  212. unsigned char high = hexCharToUInt(*first);
  213. unsigned char low = hexCharToUInt(*(first+1));
  214. if(high == 255 || low == 255) {
  215. dest.clear();
  216. return dest;
  217. }
  218. dest += (high*16+low);
  219. }
  220. return dest;
  221. }
  222. std::string secfmt(time_t sec);
  223. bool parseIntNoThrow(int32_t& res, const std::string& s, int base = 10);
  224. // Valid range: [0, INT32_MAX]
  225. bool parseUIntNoThrow(uint32_t& res, const std::string& s, int base = 10);
  226. bool parseLLIntNoThrow(int64_t& res, const std::string& s, int base = 10);
  227. void parseIntSegments(SegList<int>& sgl, const std::string& src);
  228. // Parses string which specifies the range of piece index for higher
  229. // priority and appends those indexes into result. The input string
  230. // src can contain 2 keywords "head" and "tail". To include both
  231. // keywords, they must be separated by comma. "head" means the pieces
  232. // where the first byte of each file sits. "tail" means the pieces
  233. // where the last byte of each file sits. These keywords can take one
  234. // parameter, SIZE. For example, if "head=SIZE" is specified, pieces
  235. // in the range of first SIZE bytes of each file get higher
  236. // priority. SIZE can include K or M(1K = 1024, 1M = 1024K).
  237. // If SIZE is omitted, SIZE=defaultSize is used.
  238. //
  239. // sample: head=512K,tail=512K
  240. void parsePrioritizePieceRange
  241. (std::vector<size_t>& result, const std::string& src,
  242. const std::vector<std::shared_ptr<FileEntry> >& fileEntries,
  243. size_t pieceLength,
  244. int64_t defaultSize = 1048576 /* 1MiB */);
  245. // Converts ISO/IEC 8859-1 string src to utf-8.
  246. std::string iso8859p1ToUtf8(const char* src, size_t len);
  247. std::string iso8859p1ToUtf8(const std::string& src);
  248. // Parses Content-Disposition header field value |in| with its length
  249. // |len| in a manner conforming to RFC 6266 and extracts filename
  250. // value and copies it to the region pointed by |dest|. The |destlen|
  251. // specifies the capacity of the |dest|. This function does not store
  252. // NUL character after filename in |dest|. This function does not
  253. // support RFC 2231 Continuation. If the function sees RFC 2231/5987
  254. // encoding and charset, it stores its first pointer to |*charsetp|
  255. // and its length in |*charsetlenp|. Otherwise, they are NULL and 0
  256. // respectively. In RFC 2231/5987 encoding, percent-encoded string
  257. // will be decoded to original form and stored in |dest|.
  258. //
  259. // This function returns the number of written bytes in |dest| if it
  260. // succeeds, or -1. If there is enough room to store filename in
  261. // |dest|, this function returns -1. If this function returns -1, the
  262. // |dest|, |*charsetp| and |*charsetlenp| are undefined.
  263. ssize_t parse_content_disposition(char *dest, size_t destlen,
  264. const char **charsetp, size_t *charsetlenp,
  265. const char *in, size_t len);
  266. std::string getContentDispositionFilename(const std::string& header);
  267. std::string toUpper(std::string src);
  268. std::string toLower(std::string src);
  269. void uppercase(std::string& s);
  270. void lowercase(std::string& s);
  271. char toUpperChar(char c);
  272. char toLowerChar(char c);
  273. bool isNumericHost(const std::string& name);
  274. typedef void(*signal_handler_t)(int);
  275. void setGlobalSignalHandler(int signal, sigset_t* mask,
  276. signal_handler_t handler, int flags);
  277. std::string getHomeDir();
  278. int64_t getRealSize(const std::string& sizeWithUnit);
  279. std::string abbrevSize(int64_t size);
  280. template<typename InputIterator, typename Output>
  281. void toStream
  282. (InputIterator first, InputIterator last, Output& os)
  283. {
  284. os.printf("%s\n"
  285. "idx|path/length\n"
  286. "===+===========================================================================\n", _("Files:"));
  287. int32_t count = 1;
  288. for(; first != last; ++first, ++count) {
  289. os.printf("%3d|%s\n"
  290. " |%sB (%s)\n"
  291. "---+---------------------------------------------------------------------------\n",
  292. count,
  293. (*first)->getPath().c_str(),
  294. util::abbrevSize((*first)->getLength()).c_str(),
  295. util::uitos((*first)->getLength(), true).c_str());
  296. }
  297. }
  298. void sleep(long seconds);
  299. void usleep(long microseconds);
  300. template<typename InputIterator>
  301. bool isNumber(InputIterator first, InputIterator last)
  302. {
  303. if(first == last) {
  304. return false;
  305. }
  306. for(; first != last; ++first) {
  307. if('0' > *first || *first > '9') {
  308. return false;
  309. }
  310. }
  311. return true;
  312. }
  313. bool isAlpha(const char c);
  314. bool isDigit(const char c);
  315. bool isHexDigit(const char c);
  316. bool isHexDigit(const std::string& s);
  317. bool isLws(const char c);
  318. bool isCRLF(const char c);
  319. template<typename InputIterator>
  320. bool isLowercase(InputIterator first, InputIterator last)
  321. {
  322. if(first == last) {
  323. return false;
  324. }
  325. for(; first != last; ++first) {
  326. if('a' > *first || *first > 'z') {
  327. return false;
  328. }
  329. }
  330. return true;
  331. }
  332. template<typename InputIterator>
  333. bool isUppercase(InputIterator first, InputIterator last)
  334. {
  335. if(first == last) {
  336. return false;
  337. }
  338. for(; first != last; ++first) {
  339. if('A' > *first || *first > 'Z') {
  340. return false;
  341. }
  342. }
  343. return true;
  344. }
  345. void mkdirs(const std::string& dirpath);
  346. void convertBitfield(BitfieldMan* dest, const BitfieldMan* src);
  347. // binaryStream has to be opened before calling this function.
  348. std::string toString(const std::shared_ptr<BinaryStream>& binaryStream);
  349. #ifdef HAVE_POSIX_MEMALIGN
  350. void* allocateAlignedMemory(size_t alignment, size_t size);
  351. #endif // HAVE_POSIX_MEMALIGN
  352. std::pair<std::string, uint16_t>
  353. getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len);
  354. std::string htmlEscape(const std::string& src);
  355. // Joins path element specified in [first, last). If ".." is found,
  356. // it eats the previous element if it exists. If "." is found, it
  357. // is just ignored and it is not appeared in the result.
  358. template<typename InputIterator>
  359. std::string joinPath(InputIterator first, InputIterator last)
  360. {
  361. std::vector<std::string> elements;
  362. for(;first != last; ++first) {
  363. if(*first == "..") {
  364. if(!elements.empty()) {
  365. elements.pop_back();
  366. }
  367. } else if(*first == ".") {
  368. // do nothing
  369. } else {
  370. elements.push_back(*first);
  371. }
  372. }
  373. return strjoin(elements.begin(), elements.end(), "/");
  374. }
  375. // Parses INDEX=PATH format string. INDEX must be an unsigned
  376. // integer.
  377. std::pair<size_t, std::string>
  378. parseIndexPath(const std::string& line);
  379. std::vector<std::pair<size_t, std::string> > createIndexPaths(std::istream& i);
  380. /**
  381. * Take a string [first, last) which is a delimited list and add its
  382. * elements into result as iterator pair. result is stored in out.
  383. */
  384. template<typename InputIterator, typename OutputIterator>
  385. OutputIterator splitIter
  386. (InputIterator first,
  387. InputIterator last,
  388. OutputIterator out,
  389. char delim,
  390. bool doStrip = false,
  391. bool allowEmpty = false)
  392. {
  393. for(InputIterator i = first; i != last;) {
  394. InputIterator j = std::find(i, last, delim);
  395. std::pair<InputIterator, InputIterator> p(i, j);
  396. if(doStrip) {
  397. p = stripIter(i, j);
  398. }
  399. if(allowEmpty || p.first != p.second) {
  400. *out++ = p;
  401. }
  402. i = j;
  403. if(j != last) {
  404. ++i;
  405. }
  406. }
  407. if(allowEmpty &&
  408. (first == last || *(last-1) == delim)) {
  409. *out++ = std::make_pair(last, last);
  410. }
  411. return out;
  412. }
  413. template<typename InputIterator, typename OutputIterator>
  414. OutputIterator splitIterM
  415. (InputIterator first,
  416. InputIterator last,
  417. OutputIterator out,
  418. const char* delims,
  419. bool doStrip = false,
  420. bool allowEmpty = false)
  421. {
  422. size_t numDelims = strlen(delims);
  423. const char* dlast = delims+numDelims;
  424. for(InputIterator i = first; i != last;) {
  425. InputIterator j = i;
  426. for(; j != last && std::find(delims, dlast, *j) == dlast; ++j);
  427. std::pair<InputIterator, InputIterator> p(i, j);
  428. if(doStrip) {
  429. p = stripIter(i, j);
  430. }
  431. if(allowEmpty || p.first != p.second) {
  432. *out++ = p;
  433. }
  434. i = j;
  435. if(j != last) {
  436. ++i;
  437. }
  438. }
  439. if(allowEmpty &&
  440. (first == last ||
  441. std::find(delims, dlast, *(last-1)) != dlast)) {
  442. *out++ = std::make_pair(last, last);
  443. }
  444. return out;
  445. }
  446. template<typename InputIterator, typename OutputIterator>
  447. OutputIterator split
  448. (InputIterator first,
  449. InputIterator last,
  450. OutputIterator out,
  451. char delim,
  452. bool doStrip = false,
  453. bool allowEmpty = false)
  454. {
  455. for(InputIterator i = first; i != last;) {
  456. InputIterator j = std::find(i, last, delim);
  457. std::pair<InputIterator, InputIterator> p(i, j);
  458. if(doStrip) {
  459. p = stripIter(i, j);
  460. }
  461. if(allowEmpty || p.first != p.second) {
  462. *out++ = std::string(p.first, p.second);
  463. }
  464. i = j;
  465. if(j != last) {
  466. ++i;
  467. }
  468. }
  469. if(allowEmpty &&
  470. (first == last || *(last-1) == delim)) {
  471. *out++ = std::string(last, last);
  472. }
  473. return out;
  474. }
  475. template<typename InputIterator1, typename InputIterator2>
  476. bool streq
  477. (InputIterator1 first1,
  478. InputIterator1 last1,
  479. InputIterator2 first2,
  480. InputIterator2 last2)
  481. {
  482. if(last1-first1 != last2-first2) {
  483. return false;
  484. }
  485. return std::equal(first1, last1, first2);
  486. }
  487. template<typename InputIterator>
  488. bool streq(InputIterator first, InputIterator last, const char* b)
  489. {
  490. for(; first != last && *b != '\0'; ++first, ++b) {
  491. if(*first != *b) {
  492. return false;
  493. }
  494. }
  495. return first == last && *b == '\0';
  496. }
  497. struct CaseCmp {
  498. bool operator()(char lhs, char rhs) const
  499. {
  500. if('A' <= lhs && lhs <= 'Z') {
  501. lhs += 'a'-'A';
  502. }
  503. if('A' <= rhs && rhs <= 'Z') {
  504. rhs += 'a'-'A';
  505. }
  506. return lhs == rhs;
  507. }
  508. };
  509. template<typename InputIterator1, typename InputIterator2>
  510. InputIterator1 strifind
  511. (InputIterator1 first1,
  512. InputIterator1 last1,
  513. InputIterator2 first2,
  514. InputIterator2 last2)
  515. {
  516. return std::search(first1, last1, first2, last2, CaseCmp());
  517. }
  518. template<typename InputIterator1, typename InputIterator2>
  519. bool strieq
  520. (InputIterator1 first1,
  521. InputIterator1 last1,
  522. InputIterator2 first2,
  523. InputIterator2 last2)
  524. {
  525. if(last1-first1 != last2-first2) {
  526. return false;
  527. }
  528. return std::equal(first1, last1, first2, CaseCmp());
  529. }
  530. template<typename InputIterator>
  531. bool strieq(InputIterator first, InputIterator last, const char* b)
  532. {
  533. CaseCmp cmp;
  534. for(; first != last && *b != '\0'; ++first, ++b) {
  535. if(!cmp(*first, *b)) {
  536. return false;
  537. }
  538. }
  539. return first == last && *b == '\0';
  540. }
  541. bool strieq(const std::string& a, const char* b);
  542. bool strieq(const std::string& a, const std::string& b);
  543. template<typename InputIterator1, typename InputIterator2>
  544. bool startsWith
  545. (InputIterator1 first1,
  546. InputIterator1 last1,
  547. InputIterator2 first2,
  548. InputIterator2 last2)
  549. {
  550. if(last1-first1 < last2-first2) {
  551. return false;
  552. }
  553. return std::equal(first2, last2, first1);
  554. }
  555. template<typename InputIterator>
  556. bool startsWith(InputIterator first, InputIterator last, const char* b)
  557. {
  558. for(; first != last && *b != '\0'; ++first, ++b) {
  559. if(*first != *b) {
  560. return false;
  561. }
  562. }
  563. return *b == '\0';
  564. }
  565. bool startsWith(const std::string& a, const char* b);
  566. bool startsWith(const std::string& a, const std::string& b);
  567. template<typename InputIterator1, typename InputIterator2>
  568. bool istartsWith
  569. (InputIterator1 first1,
  570. InputIterator1 last1,
  571. InputIterator2 first2,
  572. InputIterator2 last2)
  573. {
  574. if(last1-first1 < last2-first2) {
  575. return false;
  576. }
  577. return std::equal(first2, last2, first1, CaseCmp());
  578. }
  579. template<typename InputIterator>
  580. bool istartsWith(InputIterator first, InputIterator last, const char* b)
  581. {
  582. CaseCmp cmp;
  583. for(; first != last && *b != '\0'; ++first, ++b) {
  584. if(!cmp(*first, *b)) {
  585. return false;
  586. }
  587. }
  588. return *b == '\0';
  589. }
  590. bool istartsWith(const std::string& a, const char* b);
  591. template<typename InputIterator1, typename InputIterator2>
  592. bool endsWith
  593. (InputIterator1 first1,
  594. InputIterator1 last1,
  595. InputIterator2 first2,
  596. InputIterator2 last2)
  597. {
  598. if(last1-first1 < last2-first2) {
  599. return false;
  600. }
  601. return std::equal(first2, last2, last1-(last2-first2));
  602. }
  603. bool endsWith(const std::string& a, const char* b);
  604. bool endsWith(const std::string& a, const std::string& b);
  605. template<typename InputIterator1, typename InputIterator2>
  606. bool iendsWith
  607. (InputIterator1 first1,
  608. InputIterator1 last1,
  609. InputIterator2 first2,
  610. InputIterator2 last2)
  611. {
  612. if(last1-first1 < last2-first2) {
  613. return false;
  614. }
  615. return std::equal(first2, last2, last1-(last2-first2), CaseCmp());
  616. }
  617. bool iendsWith(const std::string& a, const char* b);
  618. bool iendsWith(const std::string& a, const std::string& b);
  619. // Returns true if strcmp(a, b) < 0
  620. bool strless(const char* a, const char *b);
  621. void generateRandomData(unsigned char* data, size_t length);
  622. // Saves data to file whose name is filename. If overwrite is true,
  623. // existing file is overwritten. Otherwise, this function doesn't do
  624. // nothing. If data is saved successfully, return true. Otherwise
  625. // returns false.
  626. bool saveAs
  627. (const std::string& filename, const std::string& data, bool overwrite=false);
  628. // Prepend dir to relPath. If dir is empty, it prepends "." to relPath.
  629. //
  630. // dir = "/dir", relPath = "foo" => "/dir/foo"
  631. // dir = "", relPath = "foo" => "./foo"
  632. // dir = "/", relPath = "foo" => "/foo"
  633. std::string applyDir(const std::string& dir, const std::string& relPath);
  634. // In HTTP/FTP, file name is file component in URI. In HTTP, filename
  635. // may be a value of Content-Disposition header. They are likely
  636. // percent encoded. If they contains, for example, %2F, when decoded,
  637. // basename contains dir component. This should be avoided. This
  638. // function is created to fix these issues. This function expects src
  639. // should be non-percent-encoded basename. Currently, this function
  640. // replaces '/' with '_' and result string is passed to escapePath()
  641. // function and its result is returned.
  642. std::string fixTaintedBasename(const std::string& src);
  643. // Generates 20 bytes random key and store it to the address pointed
  644. // by key. Caller must allocate at least 20 bytes for generated key.
  645. void generateRandomKey(unsigned char* key);
  646. // Returns true is given numeric ipv4addr is in Private Address Space.
  647. bool inPrivateAddress(const std::string& ipv4addr);
  648. // Returns true if s contains directory traversal path component such
  649. // as '..' or it contains null or control character which may fool
  650. // user.
  651. bool detectDirTraversal(const std::string& s);
  652. // Replaces null(0x00) and control character(0x01-0x1f) with '_'. If
  653. // __MINGW32__ is defined, following characters are also replaced with
  654. // '_': '"', '*', ':', '<', '>', '?', '\', '|'.
  655. std::string escapePath(const std::string& s);
  656. // Returns true if ip1 and ip2 are in the same CIDR block. ip1 and
  657. // ip2 must be numeric IPv4 or IPv6 address. If either of them or both
  658. // of them is not valid numeric address, then returns false. bits is
  659. // prefix bits. If bits is out of range, then bits is set to the
  660. // length of binary representation of the address*8.
  661. bool inSameCidrBlock
  662. (const std::string& ip1, const std::string& ip2, size_t bits);
  663. // No throw
  664. void executeHookByOptName
  665. (const std::shared_ptr<RequestGroup>& group, const Option* option,
  666. const Pref* pref);
  667. // No throw
  668. void executeHookByOptName
  669. (const RequestGroup* group, const Option* option, const Pref* pref);
  670. std::string createSafePath(const std::string& dir, const std::string& filename);
  671. std::string encodeNonUtf8(const std::string& s);
  672. // Create string safely. If str is NULL, returns empty string.
  673. // Otherwise, returns std::string(str).
  674. std::string makeString(const char* str);
  675. // This function is basically the same with strerror(errNum) but when
  676. // strerror returns NULL, this function returns empty string.
  677. std::string safeStrerror(int errNum);
  678. // Parses sequence [first, last) and find name=value pair delimited by
  679. // delim character. If name(and optionally value) is found, returns
  680. // pair of iterator which can use as first parameter of next call of
  681. // this function, and true. If no name is found, returns the pair of
  682. // last and false.
  683. template<typename Iterator>
  684. std::pair<Iterator, bool>
  685. nextParam
  686. (std::string& name,
  687. std::string& value,
  688. Iterator first,
  689. Iterator last,
  690. char delim)
  691. {
  692. Iterator end = last;
  693. while(first != end) {
  694. last = first;
  695. Iterator parmnameFirst = first;
  696. Iterator parmnameLast = first;
  697. bool eqFound = false;
  698. for(; last != end; ++last) {
  699. if(*last == delim) {
  700. break;
  701. } else if(!eqFound && *last == '=') {
  702. eqFound = true;
  703. parmnameFirst = first;
  704. parmnameLast = last;
  705. }
  706. }
  707. std::pair<std::string::const_iterator,
  708. std::string::const_iterator> namep = std::make_pair(last, last);
  709. std::pair<std::string::const_iterator,
  710. std::string::const_iterator> valuep = std::make_pair(last, last);
  711. if(parmnameFirst == parmnameLast) {
  712. if(!eqFound) {
  713. parmnameFirst = first;
  714. parmnameLast = last;
  715. namep = stripIter(parmnameFirst, parmnameLast);
  716. }
  717. } else {
  718. first = parmnameLast+1;
  719. namep = stripIter(parmnameFirst, parmnameLast);
  720. valuep = stripIter(first, last);
  721. }
  722. if(last != end) {
  723. ++last;
  724. }
  725. if(namep.first != namep.second) {
  726. name.assign(namep.first, namep.second);
  727. value.assign(valuep.first, valuep.second);
  728. return std::make_pair(last, true);
  729. }
  730. first = last;
  731. }
  732. return std::make_pair(end, false);
  733. }
  734. template<typename T>
  735. std::shared_ptr<T> copy(const std::shared_ptr<T>& a)
  736. {
  737. return std::shared_ptr<T>(new T(*a.get()));
  738. }
  739. // This is a bit different from cookie_helper::domainMatch(). If
  740. // hostname is numeric host, then returns true if domain == hostname.
  741. // That is if domain starts with ".", then returns true if domain is a
  742. // suffix of hostname. If domain does not start with ".", then
  743. // returns true if domain == hostname. Otherwise returns true.
  744. // For example,
  745. //
  746. // * noProxyDomainMatch("aria2.sf.net", ".sf.net") returns true.
  747. // * noProxyDomainMatch("sf.net", ".sf.net") returns false.
  748. bool noProxyDomainMatch(const std::string& hostname, const std::string& domain);
  749. // Checks hostname matches pattern as described in RFC 6125.
  750. bool tlsHostnameMatch(const std::string& pattern, const std::string& hostname);
  751. } // namespace util
  752. } // namespace aria2
  753. #endif // D_UTIL_H