util.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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 <cstdio>
  40. #include <string>
  41. #include <utility>
  42. #include <iosfwd>
  43. #include <ostream>
  44. #include <numeric>
  45. #include <map>
  46. #include <iomanip>
  47. #include <algorithm>
  48. #include <vector>
  49. #include "SharedHandle.h"
  50. #include "IntSequence.h"
  51. #include "a2time.h"
  52. #include "a2netcompat.h"
  53. #include "a2functional.h"
  54. namespace aria2 {
  55. class Randomizer;
  56. class BitfieldMan;
  57. class BinaryStream;
  58. class FileEntry;
  59. class RequestGroup;
  60. class Option;
  61. #define STRTOLL(X) strtoll(X, reinterpret_cast<char**>(0), 10)
  62. #define STRTOULL(X) strtoull(X, reinterpret_cast<char**>(0), 10)
  63. #define START_INDEX(OFFSET, PIECE_LENGTH) ((OFFSET)/(PIECE_LENGTH))
  64. #define END_INDEX(OFFSET, LENGTH, PIECE_LENGTH) (((OFFSET)+(LENGTH)-1)/(PIECE_LENGTH))
  65. #define DIV_FLOOR(X,Y) ((X)/(Y)+((X)%(Y)? 1:0))
  66. #ifdef WORDS_BIGENDIAN
  67. inline uint64_t ntoh64(uint64_t x) { return x; }
  68. inline uint64_t hton64(uint64_t x) { return x; }
  69. #else // !WORDS_BIGENDIAN
  70. inline uint64_t byteswap64(uint64_t x) {
  71. uint64_t v1 = ntohl(x & 0x00000000ffffffff);
  72. uint64_t v2 = ntohl(x >> 32);
  73. return (v1 << 32)|v2;
  74. }
  75. inline uint64_t ntoh64(uint64_t x) { return byteswap64(x); }
  76. inline uint64_t hton64(uint64_t x) { return byteswap64(x); }
  77. #endif // !WORDS_BIGENDIAN
  78. namespace util {
  79. void split(std::pair<std::string, std::string>& hp,
  80. const std::string& src, char delim);
  81. std::pair<std::string, std::string>
  82. split(const std::string& src, const std::string& delims);
  83. template<typename T>
  84. std::string uitos(T value, bool comma = false)
  85. {
  86. std::string str;
  87. if(value == 0) {
  88. str = "0";
  89. return str;
  90. }
  91. unsigned int count = 0;
  92. while(value) {
  93. ++count;
  94. char digit = value%10+'0';
  95. if(comma && count > 3 && count%3 == 1) {
  96. str += ',';
  97. }
  98. str += digit;
  99. value /= 10;
  100. }
  101. std::reverse(str.begin(), str.end());
  102. return str;
  103. }
  104. std::string itos(int64_t value, bool comma = false);
  105. /**
  106. * Computes difference in micro-seconds between tv1 and tv2,
  107. * assuming tv1 is newer than tv2.
  108. * If tv1 is older than tv2, then this method returns 0.
  109. */
  110. int64_t difftv(struct timeval tv1, struct timeval tv2);
  111. int32_t difftvsec(struct timeval tv1, struct timeval tv2);
  112. extern const std::string DEFAULT_TRIM_CHARSET;
  113. std::string trim(const std::string& src,
  114. const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
  115. void trimSelf(std::string& str,
  116. const std::string& trimCharset = DEFAULT_TRIM_CHARSET);
  117. bool startsWith(const std::string& target, const std::string& part);
  118. bool endsWith(const std::string& target, const std::string& part);
  119. std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr);
  120. std::string percentEncode(const unsigned char* target, size_t len);
  121. std::string percentEncode(const std::string& target);
  122. bool inRFC3986ReservedChars(const char c);
  123. bool inRFC3986UnreservedChars(const char c);
  124. std::string percentDecode(const std::string& target);
  125. std::string torrentPercentEncode(const unsigned char* target, size_t len);
  126. std::string torrentPercentEncode(const std::string& target);
  127. std::string toHex(const unsigned char* src, size_t len);
  128. std::string toHex(const char* src, size_t len);
  129. std::string toHex(const std::string& src);
  130. // Converts hexadecimal ascii string 'src' into packed binary form and
  131. // return the result. If src is not well formed, then empty string is
  132. // returned.
  133. std::string fromHex(const std::string& src);
  134. FILE* openFile(const std::string& filename, const std::string& mode);
  135. bool isPowerOf(int num, int base);
  136. std::string secfmt(time_t sec);
  137. int32_t parseInt(const std::string& s, int32_t base = 10);
  138. uint32_t parseUInt(const std::string& s, int base = 10);
  139. bool parseUIntNoThrow(uint32_t& result, const std::string& s, int base = 10);
  140. int64_t parseLLInt(const std::string& s, int32_t base = 10);
  141. uint64_t parseULLInt(const std::string& s, int base = 10);
  142. IntSequence parseIntRange(const std::string& src);
  143. // Parses string which specifies the range of piece index for higher
  144. // priority and appends those indexes into result. The input string
  145. // src can contain 2 keywords "head" and "tail". To include both
  146. // keywords, they must be separated by comma. "head" means the pieces
  147. // where the first byte of each file sits. "tail" means the pieces
  148. // where the last byte of each file sits. These keywords can take one
  149. // parameter, SIZE. For example, if "head=SIZE" is specified, pieces
  150. // in the range of first SIZE bytes of each file get higher
  151. // priority. SIZE can include K or M(1K = 1024, 1M = 1024K).
  152. // If SIZE is omitted, SIZE=defaultSize is used.
  153. //
  154. // sample: head=512K,tail=512K
  155. void parsePrioritizePieceRange
  156. (std::vector<size_t>& result, const std::string& src,
  157. const std::vector<SharedHandle<FileEntry> >& fileEntries,
  158. size_t pieceLength,
  159. uint64_t defaultSize = 1048576 /* 1MiB */);
  160. // Converts ISO/IEC 8859-1 string src to utf-8.
  161. std::string iso8859ToUtf8(const std::string& src);
  162. std::string getContentDispositionFilename(const std::string& header);
  163. std::string randomAlpha(size_t length,
  164. const SharedHandle<Randomizer>& randomizer);
  165. std::string toUpper(const std::string& src);
  166. std::string toLower(const std::string& src);
  167. bool isNumericHost(const std::string& name);
  168. void setGlobalSignalHandler(int signal, void (*handler)(int), int flags);
  169. std::string getHomeDir();
  170. int64_t getRealSize(const std::string& sizeWithUnit);
  171. std::string abbrevSize(int64_t size);
  172. template<typename InputIterator>
  173. void toStream
  174. (InputIterator first, InputIterator last, std::ostream& os)
  175. {
  176. os << _("Files:") << "\n";
  177. os << "idx|path/length" << "\n";
  178. os << "===+===========================================================================" << "\n";
  179. int32_t count = 1;
  180. for(; first != last; ++first, ++count) {
  181. os << std::setw(3) << count << "|" << (*first)->getPath() << "\n";
  182. os << " |" << util::abbrevSize((*first)->getLength()) << "B ("
  183. << util::uitos((*first)->getLength(), true) << ")\n";
  184. os << "---+---------------------------------------------------------------------------" << "\n";
  185. }
  186. }
  187. void sleep(long seconds);
  188. void usleep(long microseconds);
  189. bool isNumber(const std::string& what);
  190. bool isDigit(const char c);
  191. bool isHexDigit(const char c);
  192. bool isHexDigit(const std::string& s);
  193. bool isLowercase(const std::string& what);
  194. bool isUppercase(const std::string& what);
  195. /**
  196. * Converts alphabets to unsigned int, assuming alphabets as a base 26
  197. * integer and 'a' or 'A' is 0.
  198. * This function assumes alphabets includes only a-z.
  199. * Upper case are allowed but all letters must be upper case.
  200. * If overflow occurs, returns 0.
  201. */
  202. unsigned int alphaToNum(const std::string& alphabets);
  203. void mkdirs(const std::string& dirpath);
  204. void convertBitfield(BitfieldMan* dest, const BitfieldMan* src);
  205. // binaryStream has to be opened before calling this function.
  206. std::string toString(const SharedHandle<BinaryStream>& binaryStream);
  207. #ifdef HAVE_POSIX_MEMALIGN
  208. void* allocateAlignedMemory(size_t alignment, size_t size);
  209. #endif // HAVE_POSIX_MEMALIGN
  210. std::pair<std::string, uint16_t>
  211. getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len);
  212. std::string htmlEscape(const std::string& src);
  213. // Joins path element specified in [first, last). If ".." is found,
  214. // it eats the previous element if it exists. If "." is found, it
  215. // is just ignored and it is not appeared in the result.
  216. template<typename InputIterator>
  217. std::string joinPath(InputIterator first, InputIterator last)
  218. {
  219. std::vector<std::string> elements;
  220. for(;first != last; ++first) {
  221. if(*first == "..") {
  222. if(!elements.empty()) {
  223. elements.pop_back();
  224. }
  225. } else if(*first == ".") {
  226. // do nothing
  227. } else {
  228. elements.push_back(*first);
  229. }
  230. }
  231. return strjoin(elements.begin(), elements.end(), "/");
  232. }
  233. // Parses INDEX=PATH format string. INDEX must be an unsigned
  234. // integer.
  235. std::map<size_t, std::string>::value_type
  236. parseIndexPath(const std::string& line);
  237. std::map<size_t, std::string> createIndexPathMap(std::istream& i);
  238. /**
  239. * Take a string src which is a delimited list and add its elements
  240. * into result. result is stored in out.
  241. */
  242. template<typename OutputIterator>
  243. OutputIterator split(const std::string& src, OutputIterator out,
  244. const std::string& delims, bool doTrim = false,
  245. bool allowEmpty = false)
  246. {
  247. std::string::size_type p = 0;
  248. while(1) {
  249. std::string::size_type np = src.find_first_of(delims, p);
  250. if(np == std::string::npos) {
  251. std::string term = src.substr(p);
  252. if(doTrim) {
  253. term = util::trim(term);
  254. }
  255. if(allowEmpty || !term.empty()) {
  256. *out = term;
  257. ++out;
  258. }
  259. break;
  260. }
  261. std::string term = src.substr(p, np-p);
  262. if(doTrim) {
  263. term = util::trim(term);
  264. }
  265. p = np+1;
  266. if(allowEmpty || !term.empty()) {
  267. *out = term;
  268. ++out;
  269. }
  270. }
  271. return out;
  272. }
  273. void generateRandomData(unsigned char* data, size_t length);
  274. // Saves data to file whose name is filename. If overwrite is true,
  275. // existing file is overwritten. Otherwise, this function doesn't do
  276. // nothing. If data is saved successfully, return true. Otherwise
  277. // returns false.
  278. bool saveAs
  279. (const std::string& filename, const std::string& data, bool overwrite=false);
  280. // Prepend dir to relPath. If dir is empty, it prepends "." to relPath.
  281. //
  282. // dir = "/dir", relPath = "foo" => "/dir/foo"
  283. // dir = "", relPath = "foo" => "./foo"
  284. // dir = "/", relPath = "foo" => "/foo"
  285. std::string applyDir(const std::string& dir, const std::string& relPath);
  286. // In HTTP/FTP, file name is file component in URI. In HTTP, filename
  287. // may be a value of Content-Disposition header. They are likely
  288. // percent encoded. If they contains, for example, %2F, when decoded,
  289. // basename contains dir component. This should be avoided. This
  290. // function is created to fix these issues. This function expects src
  291. // should be non-percent-encoded basename. Currently, this function
  292. // replaces '/' with '_' and result string is passed to escapePath()
  293. // function and its result is returned.
  294. std::string fixTaintedBasename(const std::string& src);
  295. // Generates 20 bytes random key and store it to the address pointed
  296. // by key. Caller must allocate at least 20 bytes for generated key.
  297. void generateRandomKey(unsigned char* key);
  298. // Returns true is given numeric ipv4addr is in Private Address Space.
  299. bool inPrivateAddress(const std::string& ipv4addr);
  300. // Returns true if s contains directory traversal path component such
  301. // as '..' or it contains null or control character which may fool
  302. // user.
  303. bool detectDirTraversal(const std::string& s);
  304. // Replaces null(0x00) and control character(0x01-0x1f) with '_'. If
  305. // __MINGW32__ is defined, following characters are also replaced with
  306. // '_': '"', '*', ':', '<', '>', '?', '\', '|'.
  307. std::string escapePath(const std::string& s);
  308. // Stores network address of numeric IPv4 address ip using CIDR bits
  309. // into in. On success, returns true. Otherwise returns false.
  310. bool getCidrPrefix(struct in_addr& in, const std::string& ip, int bits);
  311. // Returns true if ip1 and ip2 are in the same CIDR block.
  312. bool inSameCidrBlock(const std::string& ip1, const std::string& ip2, int bits);
  313. void removeMetalinkContentTypes(const SharedHandle<RequestGroup>& group);
  314. void executeHook(const std::string& command, const std::string& arg);
  315. void executeHookByOptName
  316. (const SharedHandle<RequestGroup>& group, const Option* option,
  317. const std::string& opt);
  318. void executeHookByOptName
  319. (const RequestGroup* group, const Option* option, const std::string& opt);
  320. } // namespace util
  321. } // namespace aria2
  322. #endif // _D_UTIL_H_