util.h 25 KB

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