GZipFile.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2013 Nils Maier
  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. #include "GZipFile.h"
  36. #include <algorithm>
  37. #include <limits>
  38. #include "a2io.h"
  39. #include "util.h"
  40. namespace aria2 {
  41. GZipFile::GZipFile(const char* filename, const char* mode)
  42. : fp_(0), open_(false),
  43. buflen_(1024), buf_(reinterpret_cast<char*>(malloc(buflen_)))
  44. {
  45. FILE* fp =
  46. #ifdef __MINGW32__
  47. a2fopen(utf8ToWChar(filename).c_str(), utf8ToWChar(mode).c_str());
  48. #else // !__MINGW32__
  49. a2fopen(filename, mode);
  50. #endif // !__MINGW32__
  51. open_ = fp;
  52. if (open_) {
  53. int fd = dup(fileno(fp));
  54. if ((open_ = (fd >= 0))) {
  55. open_ = (fp_ = gzdopen(fd, mode));
  56. if (!open_) {
  57. ::close(fd);
  58. }
  59. }
  60. if (open_) {
  61. #if HAVE_GZBUFFER
  62. gzbuffer(fp_, 1<<17);
  63. #endif
  64. #if HAVE_GZSETPARAMS
  65. gzsetparams(fp_, 2, Z_DEFAULT_STRATEGY);
  66. #endif
  67. }
  68. fclose(fp);
  69. }
  70. }
  71. GZipFile::~GZipFile()
  72. {
  73. close();
  74. free(buf_);
  75. }
  76. int GZipFile::onClose()
  77. {
  78. if (open_) {
  79. open_ = false;
  80. return gzclose(fp_);
  81. }
  82. return 0;
  83. }
  84. bool GZipFile::onSupportsColor()
  85. {
  86. return false;
  87. }
  88. bool GZipFile::isError() const
  89. {
  90. int rv = 0;
  91. const char *e = gzerror(fp_, &rv);
  92. return (e != 0 && *e != 0) || rv != 0;
  93. }
  94. bool GZipFile::isEOF() const
  95. {
  96. return gzeof(fp_);
  97. }
  98. bool GZipFile::isOpen() const
  99. {
  100. return open_;
  101. }
  102. size_t GZipFile::onRead(void* ptr, size_t count)
  103. {
  104. char *data = reinterpret_cast<char*>(ptr);
  105. size_t read = 0;
  106. while (count) {
  107. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  108. int rv = gzread(fp_, data, len);
  109. if (rv <= 0) {
  110. break;
  111. }
  112. count -= rv;
  113. read += rv;
  114. data += rv;
  115. }
  116. return read;
  117. }
  118. size_t GZipFile::onWrite(const void* ptr, size_t count)
  119. {
  120. const char *data = reinterpret_cast<const char*>(ptr);
  121. size_t written = 0;
  122. while (count) {
  123. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  124. int rv = gzwrite(fp_, data, len);
  125. if (rv <= 0) {
  126. break;
  127. }
  128. count -= rv;
  129. written += rv;
  130. data += rv;
  131. }
  132. return written;
  133. }
  134. char* GZipFile::onGets(char* s, int size)
  135. {
  136. return gzgets(fp_, s, size);
  137. }
  138. int GZipFile::onFlush()
  139. {
  140. return gzflush(fp_, 0);
  141. }
  142. int GZipFile::onVprintf(const char* format, va_list va)
  143. {
  144. ssize_t len;
  145. #ifdef __MINGW32__
  146. // Windows vsnprintf returns -1 when output is truncated, so we
  147. // cannot use same logic in non-MINGW32 code.
  148. len = _vscprintf(format, va);
  149. if(len == 0) {
  150. return 0;
  151. }
  152. // Include terminate null
  153. ++len;
  154. if(len > static_cast<ssize_t>(buflen_)) {
  155. while(static_cast<ssize_t>(buflen_) < len) {
  156. buflen_ *= 2;
  157. }
  158. buf_ = reinterpret_cast<char*>(realloc(buf_, buflen_));
  159. }
  160. len = vsnprintf(buf_, buflen_, format, va);
  161. if(len < 0) {
  162. return len;
  163. }
  164. #else // !__MINGW32__
  165. for(;;) {
  166. len = vsnprintf(buf_, buflen_, format, va);
  167. // len does not include terminating null
  168. if(len >= static_cast<ssize_t>(buflen_)) {
  169. // Include terminate null
  170. ++len;
  171. // truncated; reallocate buf and try again
  172. while(static_cast<ssize_t>(buflen_) < len) {
  173. buflen_ *= 2;
  174. }
  175. buf_ = reinterpret_cast<char*>(realloc(buf_, buflen_));
  176. } else if(len < 0) {
  177. return len;
  178. } else {
  179. break;
  180. }
  181. }
  182. #endif // !__MINGW32__
  183. return gzwrite(fp_, buf_, len);
  184. }
  185. } // namespace aria2