GZipFile.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. {
  44. FILE* fp =
  45. #ifdef __MINGW32__
  46. a2fopen(utf8ToWChar(filename).c_str(), utf8ToWChar(mode).c_str());
  47. #else // !__MINGW32__
  48. a2fopen(filename, mode);
  49. #endif // !__MINGW32__
  50. open_ = fp;
  51. if (open_) {
  52. int fd = dup(fileno(fp));
  53. if ((open_ = fd) >= 0) {
  54. open_ = (fp_ = gzdopen(fd, mode));
  55. if (!open_) {
  56. ::close(fd);
  57. }
  58. }
  59. if (open_) {
  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. fclose(fp);
  68. }
  69. }
  70. int GZipFile::close()
  71. {
  72. if (open_) {
  73. open_ = false;
  74. return gzclose(fp_);
  75. }
  76. return 0;
  77. }
  78. bool GZipFile::isError() const
  79. {
  80. int rv = 0;
  81. const char *e = gzerror(fp_, &rv);
  82. return (e != 0 && *e != 0) || rv != 0;
  83. }
  84. size_t GZipFile::read(void* ptr, size_t count)
  85. {
  86. char *data = reinterpret_cast<char*>(ptr);
  87. size_t read = 0;
  88. while (count) {
  89. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  90. int rv = gzread(fp_, data, len);
  91. if (rv <= 0) {
  92. break;
  93. }
  94. count -= rv;
  95. read += rv;
  96. data += rv;
  97. }
  98. return read;
  99. }
  100. size_t GZipFile::write(const void* ptr, size_t count)
  101. {
  102. const char *data = reinterpret_cast<const char*>(ptr);
  103. size_t written = 0;
  104. while (count) {
  105. size_t len = std::min(count, (size_t)std::numeric_limits<unsigned>::max());
  106. int rv = gzwrite(fp_, data, len);
  107. if (rv <= 0) {
  108. break;
  109. }
  110. count -= rv;
  111. written += rv;
  112. data += rv;
  113. }
  114. return written;
  115. }
  116. char* GZipFile::gets(char* s, int size)
  117. {
  118. return gzgets(fp_, s, size);
  119. }
  120. int GZipFile::flush()
  121. {
  122. return gzflush(fp_, 0);
  123. }
  124. int GZipFile::vprintf(const char* format, va_list va)
  125. {
  126. char *buf = 0;
  127. size_t len;
  128. int rv = ::vasprintf(&buf, format, va);
  129. if (rv <= 0) {
  130. // buf is undefined at this point
  131. // do not attempt to free it
  132. return rv;
  133. }
  134. len = strlen(buf);
  135. if (len) {
  136. rv = gzwrite(fp_, buf, len);
  137. }
  138. if (buf) {
  139. free(buf);
  140. }
  141. return rv;
  142. }
  143. } // namespace aria2