streams.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /* Input and output classes for streaming brotli compression. */
  6. #ifndef BROTLI_ENC_STREAMS_H_
  7. #define BROTLI_ENC_STREAMS_H_
  8. #include <stdio.h>
  9. #include <string>
  10. #include "../common/types.h"
  11. namespace brotli {
  12. /* Input interface for the compression routines. */
  13. class BrotliIn {
  14. public:
  15. virtual ~BrotliIn(void) {}
  16. /* Return a pointer to the next block of input of at most n bytes.
  17. Return the actual length in *nread.
  18. At end of data, return NULL. Don't return NULL if there is more data
  19. to read, even if called with n == 0.
  20. Read will only be called if some of its bytes are needed. */
  21. virtual const void* Read(size_t n, size_t* nread) = 0;
  22. };
  23. /* Output interface for the compression routines. */
  24. class BrotliOut {
  25. public:
  26. virtual ~BrotliOut(void) {}
  27. /* Write n bytes of data from buf.
  28. Return true if all written, false otherwise. */
  29. virtual bool Write(const void *buf, size_t n) = 0;
  30. };
  31. /* Adapter class to make BrotliIn objects from raw memory. */
  32. class BrotliMemIn : public BrotliIn {
  33. public:
  34. BrotliMemIn(const void* buf, size_t len);
  35. void Reset(const void* buf, size_t len);
  36. /* returns the amount of data consumed */
  37. size_t position(void) const { return pos_; }
  38. const void* Read(size_t n, size_t* OUTPUT);
  39. private:
  40. const void* buf_; /* start of input buffer */
  41. size_t len_; /* length of input */
  42. size_t pos_; /* current read position within input */
  43. };
  44. /* Adapter class to make BrotliOut objects from raw memory. */
  45. class BrotliMemOut : public BrotliOut {
  46. public:
  47. BrotliMemOut(void* buf, size_t len);
  48. void Reset(void* buf, size_t len);
  49. /* returns the amount of data written */
  50. size_t position(void) const { return pos_; }
  51. bool Write(const void* buf, size_t n);
  52. private:
  53. void* buf_; /* start of output buffer */
  54. size_t len_; /* length of output */
  55. size_t pos_; /* current write position within output */
  56. };
  57. /* Adapter class to make BrotliOut objects from a string. */
  58. class BrotliStringOut : public BrotliOut {
  59. public:
  60. /* Create a writer that appends its data to buf.
  61. buf->size() will grow to at most max_size
  62. buf is expected to be empty when constructing BrotliStringOut. */
  63. BrotliStringOut(std::string* buf, size_t max_size);
  64. void Reset(std::string* buf, size_t max_len);
  65. bool Write(const void* buf, size_t n);
  66. private:
  67. std::string* buf_; /* start of output buffer */
  68. size_t max_size_; /* max length of output */
  69. };
  70. /* Adapter class to make BrotliIn object from a file. */
  71. class BrotliFileIn : public BrotliIn {
  72. public:
  73. BrotliFileIn(FILE* f, size_t max_read_size);
  74. ~BrotliFileIn(void);
  75. const void* Read(size_t n, size_t* bytes_read);
  76. private:
  77. FILE* f_;
  78. char* buf_;
  79. size_t buf_size_;
  80. };
  81. /* Adapter class to make BrotliOut object from a file. */
  82. class BrotliFileOut : public BrotliOut {
  83. public:
  84. explicit BrotliFileOut(FILE* f);
  85. bool Write(const void* buf, size_t n);
  86. private:
  87. FILE* f_;
  88. };
  89. } /* namespace brotli */
  90. #endif /* BROTLI_ENC_STREAMS_H_ */