util.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. // Parses |s| as floating point number, and stores the result into
  235. // |res|. This function returns true if it succeeds.
  236. bool parseDoubleNoThrow(double& res, const std::string& s);
  237. SegList<int> parseIntSegments(const std::string& src);
  238. // Parses string which specifies the range of piece index for higher
  239. // priority and appends those indexes into result. The input string
  240. // src can contain 2 keywords "head" and "tail". To include both
  241. // keywords, they must be separated by comma. "head" means the pieces
  242. // where the first byte of each file sits. "tail" means the pieces
  243. // where the last byte of each file sits. These keywords can take one
  244. // parameter, SIZE. For example, if "head=SIZE" is specified, pieces
  245. // in the range of first SIZE bytes of each file get higher
  246. // priority. SIZE can include K or M(1K = 1024, 1M = 1024K).
  247. // If SIZE is omitted, SIZE=defaultSize is used.
  248. //
  249. // sample: head=512K,tail=512K
  250. void parsePrioritizePieceRange(
  251. std::vector<size_t>& result, const std::string& src,
  252. const std::vector<std::shared_ptr<FileEntry>>& fileEntries,
  253. size_t pieceLength, int64_t defaultSize = 1048576 /* 1MiB */);
  254. // Converts ISO/IEC 8859-1 string src to utf-8.
  255. std::string iso8859p1ToUtf8(const char* src, size_t len);
  256. std::string iso8859p1ToUtf8(const std::string& src);
  257. // Parses Content-Disposition header field value |in| with its length
  258. // |len| in a manner conforming to RFC 6266 and extracts filename
  259. // value and copies it to the region pointed by |dest|. The |destlen|
  260. // specifies the capacity of the |dest|. This function does not store
  261. // NUL character after filename in |dest|. This function does not
  262. // support RFC 2231 Continuation. If the function sees RFC 2231/5987
  263. // encoding and charset, it stores its first pointer to |*charsetp|
  264. // and its length in |*charsetlenp|. Otherwise, they are NULL and 0
  265. // respectively. In RFC 2231/5987 encoding, percent-encoded string
  266. // will be decoded to original form and stored in |dest|.
  267. //
  268. // This function returns the number of written bytes in |dest| if it
  269. // succeeds, or -1. If there is enough room to store filename in
  270. // |dest|, this function returns -1. If this function returns -1, the
  271. // |dest|, |*charsetp| and |*charsetlenp| are undefined.
  272. //
  273. // It will handle quoted string(as in RFC 7230 section 3.2.6) as
  274. // ISO-8859-1 by default, or UTF-8 if defaultUTF8 == true.
  275. ssize_t parse_content_disposition(char* dest, size_t destlen,
  276. const char** charsetp, size_t* charsetlenp,
  277. const char* in, size_t len, bool defaultUTF8);
  278. std::string getContentDispositionFilename(const std::string& header,
  279. bool defaultUTF8);
  280. std::string toUpper(std::string src);
  281. std::string toLower(std::string src);
  282. void uppercase(std::string& s);
  283. void lowercase(std::string& s);
  284. char toUpperChar(char c);
  285. char toLowerChar(char c);
  286. bool isNumericHost(const std::string& name);
  287. typedef void (*signal_handler_t)(int);
  288. void setGlobalSignalHandler(int signal, sigset_t* mask,
  289. signal_handler_t handler, int flags);
  290. std::string getHomeDir();
  291. std::string getXDGDir(const std::string& environmentVariable,
  292. const std::string& fallbackDirectory);
  293. std::string getConfigFile();
  294. std::string getDHTFile(bool ipv6);
  295. int64_t getRealSize(const std::string& sizeWithUnit);
  296. std::string abbrevSize(int64_t size);
  297. template <typename InputIterator, typename Output>
  298. void toStream(InputIterator first, InputIterator last, Output& os)
  299. {
  300. os.printf("%s\n"
  301. "idx|path/length\n"
  302. "===+=============================================================="
  303. "=============\n",
  304. _("Files:"));
  305. int32_t count = 1;
  306. for (; first != last; ++first, ++count) {
  307. os.printf("%3d|%s\n"
  308. " |%sB (%s)\n"
  309. "---+------------------------------------------------------------"
  310. "---------------\n",
  311. count, (*first)->getPath().c_str(),
  312. util::abbrevSize((*first)->getLength()).c_str(),
  313. util::uitos((*first)->getLength(), true).c_str());
  314. }
  315. }
  316. void sleep(long seconds);
  317. void usleep(long microseconds);
  318. template <typename InputIterator>
  319. bool isNumber(InputIterator first, InputIterator last)
  320. {
  321. if (first == last) {
  322. return false;
  323. }
  324. for (; first != last; ++first) {
  325. if ('0' > *first || *first > '9') {
  326. return false;
  327. }
  328. }
  329. return true;
  330. }
  331. bool isAlpha(const char c);
  332. bool isDigit(const char c);
  333. bool isHexDigit(const char c);
  334. bool isHexDigit(const std::string& s);
  335. bool isLws(const char c);
  336. bool isCRLF(const char c);
  337. template <typename InputIterator>
  338. bool isLowercase(InputIterator first, InputIterator last)
  339. {
  340. if (first == last) {
  341. return false;
  342. }
  343. for (; first != last; ++first) {
  344. if ('a' > *first || *first > 'z') {
  345. return false;
  346. }
  347. }
  348. return true;
  349. }
  350. template <typename InputIterator>
  351. bool isUppercase(InputIterator first, InputIterator last)
  352. {
  353. if (first == last) {
  354. return false;
  355. }
  356. for (; first != last; ++first) {
  357. if ('A' > *first || *first > 'Z') {
  358. return false;
  359. }
  360. }
  361. return true;
  362. }
  363. void mkdirs(const std::string& dirpath);
  364. void convertBitfield(BitfieldMan* dest, const BitfieldMan* src);
  365. // binaryStream has to be opened before calling this function.
  366. std::string toString(const std::shared_ptr<BinaryStream>& binaryStream);
  367. #ifdef HAVE_POSIX_MEMALIGN
  368. void* allocateAlignedMemory(size_t alignment, size_t size);
  369. #endif // HAVE_POSIX_MEMALIGN
  370. Endpoint getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len);
  371. std::string htmlEscape(const std::string& src);
  372. // Joins path element specified in [first, last). If ".." is found,
  373. // it eats the previous element if it exists. If "." is found, it
  374. // is just ignored and it is not appeared in the result.
  375. template <typename InputIterator>
  376. std::string joinPath(InputIterator first, InputIterator last)
  377. {
  378. std::vector<std::string> elements;
  379. for (; first != last; ++first) {
  380. if (*first == "..") {
  381. if (!elements.empty()) {
  382. elements.pop_back();
  383. }
  384. }
  385. else if (*first == ".") {
  386. // do nothing
  387. }
  388. else {
  389. elements.push_back(*first);
  390. }
  391. }
  392. return strjoin(elements.begin(), elements.end(), "/");
  393. }
  394. // Parses INDEX=PATH format string. INDEX must be an unsigned
  395. // integer.
  396. std::pair<size_t, std::string> parseIndexPath(const std::string& line);
  397. std::vector<std::pair<size_t, std::string>> createIndexPaths(std::istream& i);
  398. /**
  399. * Take a string [first, last) which is a delimited list and add its
  400. * elements into result as iterator pair. result is stored in out.
  401. */
  402. template <typename InputIterator, typename OutputIterator>
  403. OutputIterator splitIter(InputIterator first, InputIterator last,
  404. OutputIterator out, char delim, bool doStrip = false,
  405. bool allowEmpty = false)
  406. {
  407. for (InputIterator i = first; i != last;) {
  408. InputIterator j = std::find(i, last, delim);
  409. std::pair<InputIterator, InputIterator> p(i, j);
  410. if (doStrip) {
  411. p = stripIter(i, j);
  412. }
  413. if (allowEmpty || p.first != p.second) {
  414. *out++ = p;
  415. }
  416. i = j;
  417. if (j != last) {
  418. ++i;
  419. }
  420. }
  421. if (allowEmpty && (first == last || *(last - 1) == delim)) {
  422. *out++ = std::make_pair(last, last);
  423. }
  424. return out;
  425. }
  426. template <typename InputIterator, typename OutputIterator>
  427. OutputIterator splitIterM(InputIterator first, InputIterator last,
  428. OutputIterator out, const char* delims,
  429. bool doStrip = false, bool allowEmpty = false)
  430. {
  431. size_t numDelims = strlen(delims);
  432. const char* dlast = delims + numDelims;
  433. for (InputIterator i = first; i != last;) {
  434. InputIterator j = i;
  435. for (; j != last && std::find(delims, dlast, *j) == dlast; ++j)
  436. ;
  437. std::pair<InputIterator, InputIterator> p(i, j);
  438. if (doStrip) {
  439. p = stripIter(i, j);
  440. }
  441. if (allowEmpty || p.first != p.second) {
  442. *out++ = p;
  443. }
  444. i = j;
  445. if (j != last) {
  446. ++i;
  447. }
  448. }
  449. if (allowEmpty &&
  450. (first == last || std::find(delims, dlast, *(last - 1)) != dlast)) {
  451. *out++ = std::make_pair(last, last);
  452. }
  453. return out;
  454. }
  455. template <typename InputIterator, typename OutputIterator>
  456. OutputIterator split(InputIterator first, InputIterator last,
  457. OutputIterator out, char delim, bool doStrip = false,
  458. bool allowEmpty = false)
  459. {
  460. for (InputIterator i = first; i != last;) {
  461. InputIterator j = std::find(i, last, delim);
  462. std::pair<InputIterator, InputIterator> p(i, j);
  463. if (doStrip) {
  464. p = stripIter(i, j);
  465. }
  466. if (allowEmpty || p.first != p.second) {
  467. *out++ = std::string(p.first, p.second);
  468. }
  469. i = j;
  470. if (j != last) {
  471. ++i;
  472. }
  473. }
  474. if (allowEmpty && (first == last || *(last - 1) == delim)) {
  475. *out++ = std::string(last, last);
  476. }
  477. return out;
  478. }
  479. template <typename InputIterator1, typename InputIterator2>
  480. bool streq(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
  481. InputIterator2 last2)
  482. {
  483. if (last1 - first1 != last2 - first2) {
  484. return false;
  485. }
  486. return std::equal(first1, last1, first2);
  487. }
  488. template <typename InputIterator>
  489. bool streq(InputIterator first, InputIterator last, const char* b)
  490. {
  491. for (; first != last && *b != '\0'; ++first, ++b) {
  492. if (*first != *b) {
  493. return false;
  494. }
  495. }
  496. return first == last && *b == '\0';
  497. }
  498. struct CaseCmp {
  499. bool operator()(char lhs, char rhs) const
  500. {
  501. if ('A' <= lhs && lhs <= 'Z') {
  502. lhs += 'a' - 'A';
  503. }
  504. if ('A' <= rhs && rhs <= 'Z') {
  505. rhs += 'a' - 'A';
  506. }
  507. return lhs == rhs;
  508. }
  509. };
  510. template <typename InputIterator1, typename InputIterator2>
  511. InputIterator1 strifind(InputIterator1 first1, InputIterator1 last1,
  512. InputIterator2 first2, InputIterator2 last2)
  513. {
  514. return std::search(first1, last1, first2, last2, CaseCmp());
  515. }
  516. template <typename InputIterator1, typename InputIterator2>
  517. bool strieq(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,
  518. InputIterator2 last2)
  519. {
  520. if (last1 - first1 != last2 - first2) {
  521. return false;
  522. }
  523. return std::equal(first1, last1, first2, CaseCmp());
  524. }
  525. template <typename InputIterator>
  526. bool strieq(InputIterator first, InputIterator last, const char* b)
  527. {
  528. CaseCmp cmp;
  529. for (; first != last && *b != '\0'; ++first, ++b) {
  530. if (!cmp(*first, *b)) {
  531. return false;
  532. }
  533. }
  534. return first == last && *b == '\0';
  535. }
  536. bool strieq(const std::string& a, const char* b);
  537. bool strieq(const std::string& a, const std::string& b);
  538. template <typename InputIterator1, typename InputIterator2>
  539. bool startsWith(InputIterator1 first1, InputIterator1 last1,
  540. InputIterator2 first2, InputIterator2 last2)
  541. {
  542. if (last1 - first1 < last2 - first2) {
  543. return false;
  544. }
  545. return std::equal(first2, last2, first1);
  546. }
  547. template <typename InputIterator>
  548. bool startsWith(InputIterator first, InputIterator last, const char* b)
  549. {
  550. for (; first != last && *b != '\0'; ++first, ++b) {
  551. if (*first != *b) {
  552. return false;
  553. }
  554. }
  555. return *b == '\0';
  556. }
  557. bool startsWith(const std::string& a, const char* b);
  558. bool startsWith(const std::string& a, const std::string& b);
  559. template <typename InputIterator1, typename InputIterator2>
  560. bool istartsWith(InputIterator1 first1, InputIterator1 last1,
  561. InputIterator2 first2, InputIterator2 last2)
  562. {
  563. if (last1 - first1 < last2 - first2) {
  564. return false;
  565. }
  566. return std::equal(first2, last2, first1, CaseCmp());
  567. }
  568. template <typename InputIterator>
  569. bool istartsWith(InputIterator first, InputIterator last, const char* b)
  570. {
  571. CaseCmp cmp;
  572. for (; first != last && *b != '\0'; ++first, ++b) {
  573. if (!cmp(*first, *b)) {
  574. return false;
  575. }
  576. }
  577. return *b == '\0';
  578. }
  579. bool istartsWith(const std::string& a, const char* b);
  580. bool istartsWith(const std::string& a, const std::string& b);
  581. template <typename InputIterator1, typename InputIterator2>
  582. bool endsWith(InputIterator1 first1, InputIterator1 last1,
  583. InputIterator2 first2, InputIterator2 last2)
  584. {
  585. if (last1 - first1 < last2 - first2) {
  586. return false;
  587. }
  588. return std::equal(first2, last2, last1 - (last2 - first2));
  589. }
  590. bool endsWith(const std::string& a, const char* b);
  591. bool endsWith(const std::string& a, const std::string& b);
  592. template <typename InputIterator1, typename InputIterator2>
  593. bool iendsWith(InputIterator1 first1, InputIterator1 last1,
  594. InputIterator2 first2, InputIterator2 last2)
  595. {
  596. if (last1 - first1 < last2 - first2) {
  597. return false;
  598. }
  599. return std::equal(first2, last2, last1 - (last2 - first2), CaseCmp());
  600. }
  601. bool iendsWith(const std::string& a, const char* b);
  602. bool iendsWith(const std::string& a, const std::string& b);
  603. // Returns true if strcmp(a, b) < 0
  604. bool strless(const char* a, const char* b);
  605. void generateRandomData(unsigned char* data, size_t length);
  606. // Saves data to file whose name is filename. If overwrite is true,
  607. // existing file is overwritten. Otherwise, this function doesn't do
  608. // nothing. If data is saved successfully, return true. Otherwise
  609. // returns false.
  610. bool saveAs(const std::string& filename, const std::string& data,
  611. bool overwrite = false);
  612. // Prepend dir to relPath. If dir is empty, it prepends "." to relPath.
  613. //
  614. // dir = "/dir", relPath = "foo" => "/dir/foo"
  615. // dir = "", relPath = "foo" => "./foo"
  616. // dir = "/", relPath = "foo" => "/foo"
  617. std::string applyDir(const std::string& dir, const std::string& relPath);
  618. // In HTTP/FTP, file name is file component in URI. In HTTP, filename
  619. // may be a value of Content-Disposition header. They are likely
  620. // percent encoded. If they contains, for example, %2F, when decoded,
  621. // basename contains dir component. This should be avoided. This
  622. // function is created to fix these issues. This function expects src
  623. // should be non-percent-encoded basename. Currently, this function
  624. // replaces '/' with '_' and result string is passed to escapePath()
  625. // function and its result is returned.
  626. std::string fixTaintedBasename(const std::string& src);
  627. // Generates 20 bytes random key and store it to the address pointed
  628. // by key. Caller must allocate at least 20 bytes for generated key.
  629. void generateRandomKey(unsigned char* key);
  630. // Returns true is given numeric ipv4addr is in Private Address Space.
  631. bool inPrivateAddress(const std::string& ipv4addr);
  632. // Returns true if s contains directory traversal path component such
  633. // as '..' or it contains null or control character which may fool
  634. // user.
  635. bool detectDirTraversal(const std::string& s);
  636. // Replaces null(0x00) and control character(0x01-0x1f) with '_'. If
  637. // __MINGW32__ is defined, following characters are also replaced with
  638. // '_': '"', '*', ':', '<', '>', '?', '\', '|'.
  639. std::string escapePath(const std::string& s);
  640. // Returns true if ip1 and ip2 are in the same CIDR block. ip1 and
  641. // ip2 must be numeric IPv4 or IPv6 address. If either of them or both
  642. // of them is not valid numeric address, then returns false. bits is
  643. // prefix bits. If bits is out of range, then bits is set to the
  644. // length of binary representation of the address*8.
  645. bool inSameCidrBlock(const std::string& ip1, const std::string& ip2,
  646. size_t bits);
  647. // No throw
  648. void executeHookByOptName(const std::shared_ptr<RequestGroup>& group,
  649. const Option* option, PrefPtr pref);
  650. // No throw
  651. void executeHookByOptName(const RequestGroup* group, const Option* option,
  652. PrefPtr pref);
  653. std::string createSafePath(const std::string& dir, const std::string& filename);
  654. std::string createSafePath(const std::string& filename);
  655. std::string encodeNonUtf8(const std::string& s);
  656. // Create string safely. If str is NULL, returns empty string.
  657. // Otherwise, returns std::string(str).
  658. std::string makeString(const char* str);
  659. // This function is basically the same with strerror(errNum) but when
  660. // strerror returns NULL, this function returns empty string.
  661. std::string safeStrerror(int errNum);
  662. // Parses sequence [first, last) and find name=value pair delimited by
  663. // delim character. If name(and optionally value) is found, returns
  664. // pair of iterator which can use as first parameter of next call of
  665. // this function, and true. If no name is found, returns the pair of
  666. // last and false.
  667. template <typename Iterator>
  668. std::pair<Iterator, bool> nextParam(std::string& name, std::string& value,
  669. Iterator first, Iterator last, char delim)
  670. {
  671. Iterator end = last;
  672. while (first != end) {
  673. last = first;
  674. Iterator parmnameFirst = first;
  675. Iterator parmnameLast = first;
  676. bool eqFound = false;
  677. for (; last != end; ++last) {
  678. if (*last == delim) {
  679. break;
  680. }
  681. else if (!eqFound && *last == '=') {
  682. eqFound = true;
  683. parmnameFirst = first;
  684. parmnameLast = last;
  685. }
  686. }
  687. std::pair<std::string::const_iterator, std::string::const_iterator> namep =
  688. std::make_pair(last, last);
  689. std::pair<std::string::const_iterator, std::string::const_iterator> valuep =
  690. std::make_pair(last, last);
  691. if (parmnameFirst == parmnameLast) {
  692. if (!eqFound) {
  693. parmnameFirst = first;
  694. parmnameLast = last;
  695. namep = stripIter(parmnameFirst, parmnameLast);
  696. }
  697. }
  698. else {
  699. first = parmnameLast + 1;
  700. namep = stripIter(parmnameFirst, parmnameLast);
  701. valuep = stripIter(first, last);
  702. }
  703. if (last != end) {
  704. ++last;
  705. }
  706. if (namep.first != namep.second) {
  707. name.assign(namep.first, namep.second);
  708. value.assign(valuep.first, valuep.second);
  709. return std::make_pair(last, true);
  710. }
  711. first = last;
  712. }
  713. return std::make_pair(end, false);
  714. }
  715. template <typename T> std::shared_ptr<T> copy(const std::shared_ptr<T>& a)
  716. {
  717. return std::make_shared<T>(*a.get());
  718. }
  719. // This is a bit different from cookie_helper::domainMatch(). If
  720. // hostname is numeric host, then returns true if domain == hostname.
  721. // That is if domain starts with ".", then returns true if domain is a
  722. // suffix of hostname. If domain does not start with ".", then
  723. // returns true if domain == hostname. Otherwise returns true.
  724. // For example,
  725. //
  726. // * noProxyDomainMatch("aria2.sf.net", ".sf.net") returns true.
  727. // * noProxyDomainMatch("sf.net", ".sf.net") returns false.
  728. bool noProxyDomainMatch(const std::string& hostname, const std::string& domain);
  729. // Checks hostname matches pattern as described in RFC 6125.
  730. bool tlsHostnameMatch(const std::string& pattern, const std::string& hostname);
  731. #ifdef ENABLE_SSL
  732. TLSVersion toTLSVersion(const std::string& ver);
  733. #endif // ENABLE_SSL
  734. #ifdef __MINGW32__
  735. // Formats error message for error code errNum, which is the return
  736. // value of GetLastError(). On error, this function returns empty
  737. // string.
  738. std::string formatLastError(int errNum);
  739. #endif // __MINGW32__
  740. // Sets file descriptor file FD_CLOEXEC to |fd|. This function is
  741. // noop for Mingw32 build, since we disable inheritance in
  742. // CreateProcess call.
  743. void make_fd_cloexec(int fd);
  744. #ifdef __MINGW32__
  745. bool gainPrivilege(LPCTSTR privName);
  746. #endif // __MINGW32__
  747. } // namespace util
  748. } // namespace aria2
  749. #endif // D_UTIL_H