GZipFile.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. FILE* fp =
  45. #ifdef __MINGW32__
  46. strcmp(DEV_STDIN, filename) == 0
  47. ? stdin
  48. : 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. }
  65. else {
  66. ::close(fd);
  67. }
  68. }
  69. fclose(fp);
  70. }
  71. }
  72. GZipFile::~GZipFile()
  73. {
  74. close();
  75. free(buf_);
  76. }
  77. int GZipFile::onClose()
  78. {
  79. int rv = 0;
  80. if (fp_) {
  81. rv = gzclose(fp_);
  82. fp_ = nullptr;
  83. }
  84. return rv;
  85. }
  86. bool GZipFile::onSupportsColor() { return false; }
  87. bool GZipFile::isError() const
  88. {
  89. int rv = 0;
  90. const char* e = gzerror(fp_, &rv);
  91. return (e != nullptr && *e != 0) || rv != 0;
  92. }
  93. bool GZipFile::isEOF() const { return gzeof(fp_); }
  94. bool GZipFile::isOpen() const { return fp_; }
  95. size_t GZipFile::onRead(void* ptr, size_t count)
  96. {
  97. char* data = reinterpret_cast<char*>(ptr);
  98. size_t read = 0;
  99. while (count) {
  100. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  101. int rv = gzread(fp_, data, len);
  102. if (rv <= 0) {
  103. break;
  104. }
  105. count -= rv;
  106. read += rv;
  107. data += rv;
  108. }
  109. return read;
  110. }
  111. size_t GZipFile::onWrite(const void* ptr, size_t count)
  112. {
  113. const char* data = reinterpret_cast<const char*>(ptr);
  114. size_t written = 0;
  115. while (count) {
  116. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  117. int rv = gzwrite(fp_, data, len);
  118. if (rv <= 0) {
  119. break;
  120. }
  121. count -= rv;
  122. written += rv;
  123. data += rv;
  124. }
  125. return written;
  126. }
  127. char* GZipFile::onGets(char* s, int size) { return gzgets(fp_, s, size); }
  128. int GZipFile::onFlush() { return gzflush(fp_, 0); }
  129. int GZipFile::onVprintf(const char* format, va_list va)
  130. {
  131. ssize_t len;
  132. for (;;) {
  133. len = vsnprintf(buf_, buflen_, format, va);
  134. // len does not include terminating null
  135. if (len >= static_cast<ssize_t>(buflen_)) {
  136. // Include terminate null
  137. ++len;
  138. // truncated; reallocate buf and try again
  139. while (static_cast<ssize_t>(buflen_) < len) {
  140. buflen_ *= 2;
  141. }
  142. buf_ = reinterpret_cast<char*>(realloc(buf_, buflen_));
  143. }
  144. else if (len < 0) {
  145. return len;
  146. }
  147. else {
  148. break;
  149. }
  150. }
  151. return gzwrite(fp_, buf_, len);
  152. }
  153. } // namespace aria2