streams.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* Copyright 2009 Google Inc. All Rights Reserved.
  2. Distributed under MIT license.
  3. See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
  4. */
  5. /* Convience routines to make Brotli I/O classes from some memory containers and
  6. files. */
  7. #include "./streams.h"
  8. #include <assert.h>
  9. #include <stdlib.h>
  10. #include <string.h> /* memcpy */
  11. namespace brotli {
  12. BrotliMemOut::BrotliMemOut(void* buf, size_t len)
  13. : buf_(buf),
  14. len_(len),
  15. pos_(0) {}
  16. void BrotliMemOut::Reset(void* buf, size_t len) {
  17. buf_ = buf;
  18. len_ = len;
  19. pos_ = 0;
  20. }
  21. /* Brotli output routine: copy n bytes to the output buffer. */
  22. bool BrotliMemOut::Write(const void *buf, size_t n) {
  23. if (n + pos_ > len_)
  24. return false;
  25. char* p = reinterpret_cast<char*>(buf_) + pos_;
  26. memcpy(p, buf, n);
  27. pos_ += n;
  28. return true;
  29. }
  30. BrotliStringOut::BrotliStringOut(std::string* buf, size_t max_size)
  31. : buf_(buf),
  32. max_size_(max_size) {
  33. assert(buf->empty());
  34. }
  35. void BrotliStringOut::Reset(std::string* buf, size_t max_size) {
  36. buf_ = buf;
  37. max_size_ = max_size;
  38. }
  39. /* Brotli output routine: add n bytes to a string. */
  40. bool BrotliStringOut::Write(const void *buf, size_t n) {
  41. if (buf_->size() + n > max_size_)
  42. return false;
  43. buf_->append(static_cast<const char*>(buf), n);
  44. return true;
  45. }
  46. BrotliMemIn::BrotliMemIn(const void* buf, size_t len)
  47. : buf_(buf),
  48. len_(len),
  49. pos_(0) {}
  50. void BrotliMemIn::Reset(const void* buf, size_t len) {
  51. buf_ = buf;
  52. len_ = len;
  53. pos_ = 0;
  54. }
  55. /* Brotli input routine: read the next chunk of memory. */
  56. const void* BrotliMemIn::Read(size_t n, size_t* output) {
  57. if (pos_ == len_) {
  58. return NULL;
  59. }
  60. if (n > len_ - pos_)
  61. n = len_ - pos_;
  62. const char* p = reinterpret_cast<const char*>(buf_) + pos_;
  63. pos_ += n;
  64. *output = n;
  65. return p;
  66. }
  67. BrotliFileIn::BrotliFileIn(FILE* f, size_t max_read_size)
  68. : f_(f),
  69. buf_(new char[max_read_size]),
  70. buf_size_(max_read_size) { }
  71. BrotliFileIn::~BrotliFileIn(void) {
  72. delete[] buf_;
  73. }
  74. const void* BrotliFileIn::Read(size_t n, size_t* bytes_read) {
  75. if (n > buf_size_) {
  76. n = buf_size_;
  77. } else if (n == 0) {
  78. return feof(f_) ? NULL : buf_;
  79. }
  80. *bytes_read = fread(buf_, 1, n, f_);
  81. if (*bytes_read == 0) {
  82. return NULL;
  83. } else {
  84. return buf_;
  85. }
  86. }
  87. BrotliFileOut::BrotliFileOut(FILE* f) : f_(f) {}
  88. bool BrotliFileOut::Write(const void* buf, size_t n) {
  89. if (fwrite(buf, n, 1, f_) != 1) {
  90. return false;
  91. }
  92. return true;
  93. }
  94. } /* namespace brotli */