GZipFile.cc 4.3 KB

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