GZipFile.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. : BufferedFile(0), 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::close()
  77. {
  78. if (open_) {
  79. open_ = false;
  80. return gzclose(fp_);
  81. }
  82. return 0;
  83. }
  84. bool GZipFile::isError() const
  85. {
  86. int rv = 0;
  87. const char *e = gzerror(fp_, &rv);
  88. return (e != 0 && *e != 0) || rv != 0;
  89. }
  90. size_t GZipFile::read(void* ptr, size_t count)
  91. {
  92. char *data = reinterpret_cast<char*>(ptr);
  93. size_t read = 0;
  94. while (count) {
  95. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  96. int rv = gzread(fp_, data, len);
  97. if (rv <= 0) {
  98. break;
  99. }
  100. count -= rv;
  101. read += rv;
  102. data += rv;
  103. }
  104. return read;
  105. }
  106. size_t GZipFile::write(const void* ptr, size_t count)
  107. {
  108. const char *data = reinterpret_cast<const char*>(ptr);
  109. size_t written = 0;
  110. while (count) {
  111. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  112. int rv = gzwrite(fp_, data, len);
  113. if (rv <= 0) {
  114. break;
  115. }
  116. count -= rv;
  117. written += rv;
  118. data += rv;
  119. }
  120. return written;
  121. }
  122. char* GZipFile::gets(char* s, int size)
  123. {
  124. return gzgets(fp_, s, size);
  125. }
  126. int GZipFile::flush()
  127. {
  128. return gzflush(fp_, 0);
  129. }
  130. int GZipFile::vprintf(const char* format, va_list va)
  131. {
  132. ssize_t len;
  133. #ifdef __MINGW32__
  134. // Windows vsnprintf returns -1 when output is truncated, so we
  135. // cannot use same logic in non-MINGW32 code.
  136. len = _vscprintf(format, va);
  137. if(len == 0) {
  138. return 0;
  139. }
  140. // Include terminate null
  141. ++len;
  142. if(len > static_cast<ssize_t>(buflen_)) {
  143. while(static_cast<ssize_t>(buflen_) < len) {
  144. buflen_ *= 2;
  145. }
  146. buf_ = reinterpret_cast<char*>(realloc(buf_, buflen_));
  147. }
  148. len = vsnprintf(buf_, buflen_, format, va);
  149. if(len < 0) {
  150. return len;
  151. }
  152. #else // !__MINGW32__
  153. for(;;) {
  154. len = vsnprintf(buf_, buflen_, format, va);
  155. // len does not include terminating null
  156. if(len >= static_cast<ssize_t>(buflen_)) {
  157. // Include terminate null
  158. ++len;
  159. // truncated; reallocate buf and try again
  160. while(static_cast<ssize_t>(buflen_) < len) {
  161. buflen_ *= 2;
  162. }
  163. buf_ = reinterpret_cast<char*>(realloc(buf_, buflen_));
  164. } else if(len < 0) {
  165. return len;
  166. } else {
  167. break;
  168. }
  169. }
  170. #endif // !__MINGW32__
  171. return gzwrite(fp_, buf_, len);
  172. }
  173. } // namespace aria2