util.h 26 KB

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