GZipFile.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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_(nullptr),
  43. buflen_(1024), buf_(reinterpret_cast<char*>(malloc(buflen_)))
  44. {
  45. FILE* fp =
  46. #ifdef __MINGW32__
  47. strcmp(DEV_STDIN, filename) == 0 ?
  48. stdin : a2fopen(utf8ToWChar(filename).c_str(), utf8ToWChar(mode).c_str());
  49. #else // !__MINGW32__
  50. a2fopen(filename, mode);
  51. #endif // !__MINGW32__
  52. if (fp) {
  53. int fd = dup(fileno(fp));
  54. if (fd != -1) {
  55. fp_ = gzdopen(fd, mode);
  56. if (fp_) {
  57. // fp_ retains fd and gzclose() will close fd as well.
  58. #if HAVE_GZBUFFER
  59. gzbuffer(fp_, 1<<17);
  60. #endif
  61. #if HAVE_GZSETPARAMS
  62. gzsetparams(fp_, 2, Z_DEFAULT_STRATEGY);
  63. #endif
  64. } else {
  65. ::close(fd);
  66. }
  67. }
  68. fclose(fp);
  69. }
  70. }
  71. GZipFile::~GZipFile()
  72. {
  73. close();
  74. free(buf_);
  75. }
  76. int GZipFile::onClose()
  77. {
  78. int rv = 0;
  79. if (fp_) {
  80. rv = gzclose(fp_);
  81. fp_ = nullptr;
  82. }
  83. return rv;
  84. }
  85. bool GZipFile::onSupportsColor()
  86. {
  87. return false;
  88. }
  89. bool GZipFile::isError() const
  90. {
  91. int rv = 0;
  92. const char *e = gzerror(fp_, &rv);
  93. return (e != nullptr && *e != 0) || rv != 0;
  94. }
  95. bool GZipFile::isEOF() const
  96. {
  97. return gzeof(fp_);
  98. }
  99. bool GZipFile::isOpen() const
  100. {
  101. return fp_;
  102. }
  103. size_t GZipFile::onRead(void* ptr, size_t count)
  104. {
  105. char *data = reinterpret_cast<char*>(ptr);
  106. size_t read = 0;
  107. while (count) {
  108. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  109. int rv = gzread(fp_, data, len);
  110. if (rv <= 0) {
  111. break;
  112. }
  113. count -= rv;
  114. read += rv;
  115. data += rv;
  116. }
  117. return read;
  118. }
  119. size_t GZipFile::onWrite(const void* ptr, size_t count)
  120. {
  121. const char *data = reinterpret_cast<const char*>(ptr);
  122. size_t written = 0;
  123. while (count) {
  124. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  125. int rv = gzwrite(fp_, data, len);
  126. if (rv <= 0) {
  127. break;
  128. }
  129. count -= rv;
  130. written += rv;
  131. data += rv;
  132. }
  133. return written;
  134. }
  135. char* GZipFile::onGets(char* s, int size)
  136. {
  137. return gzgets(fp_, s, size);
  138. }
  139. int GZipFile::onFlush()
  140. {
  141. return gzflush(fp_, 0);
  142. }
  143. int GZipFile::onVprintf(const char* format, va_list va)
  144. {
  145. ssize_t len;
  146. for(;;) {
  147. len = vsnprintf(buf_, buflen_, format, va);
  148. // len does not include terminating null
  149. if(len >= static_cast<ssize_t>(buflen_)) {
  150. // Include terminate null
  151. ++len;
  152. // truncated; reallocate buf and try again
  153. while(static_cast<ssize_t>(buflen_) < len) {
  154. buflen_ *= 2;
  155. }
  156. buf_ = reinterpret_cast<char*>(realloc(buf_, buflen_));
  157. } else if(len < 0) {
  158. return len;
  159. } else {
  160. break;
  161. }
  162. }
  163. return gzwrite(fp_, buf_, len);
  164. }
  165. } // namespace aria2