OLD | NEW |
| (Empty) |
1 /* Copyright 2009 Google Inc. All Rights Reserved. | |
2 | |
3 Distributed under MIT license. | |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | |
5 */ | |
6 | |
7 // Input and output classes for streaming brotli compression. | |
8 | |
9 #ifndef BROTLI_ENC_STREAMS_H_ | |
10 #define BROTLI_ENC_STREAMS_H_ | |
11 | |
12 #include <stdio.h> | |
13 #include <string> | |
14 #include "./port.h" | |
15 #include "./types.h" | |
16 | |
17 namespace brotli { | |
18 | |
19 // Input interface for the compression routines. | |
20 class BrotliIn { | |
21 public: | |
22 virtual ~BrotliIn(void) {} | |
23 | |
24 // Return a pointer to the next block of input of at most n bytes. | |
25 // Return the actual length in *nread. | |
26 // At end of data, return NULL. Don't return NULL if there is more data | |
27 // to read, even if called with n == 0. | |
28 // Read will only be called if some of its bytes are needed. | |
29 virtual const void* Read(size_t n, size_t* nread) = 0; | |
30 }; | |
31 | |
32 // Output interface for the compression routines. | |
33 class BrotliOut { | |
34 public: | |
35 virtual ~BrotliOut(void) {} | |
36 | |
37 // Write n bytes of data from buf. | |
38 // Return true if all written, false otherwise. | |
39 virtual bool Write(const void *buf, size_t n) = 0; | |
40 }; | |
41 | |
42 // Adapter class to make BrotliIn objects from raw memory. | |
43 class BrotliMemIn : public BrotliIn { | |
44 public: | |
45 BrotliMemIn(const void* buf, size_t len); | |
46 | |
47 void Reset(const void* buf, size_t len); | |
48 | |
49 // returns the amount of data consumed | |
50 size_t position(void) const { return pos_; } | |
51 | |
52 const void* Read(size_t n, size_t* OUTPUT); | |
53 | |
54 private: | |
55 const void* buf_; // start of input buffer | |
56 size_t len_; // length of input | |
57 size_t pos_; // current read position within input | |
58 }; | |
59 | |
60 // Adapter class to make BrotliOut objects from raw memory. | |
61 class BrotliMemOut : public BrotliOut { | |
62 public: | |
63 BrotliMemOut(void* buf, size_t len); | |
64 | |
65 void Reset(void* buf, size_t len); | |
66 | |
67 // returns the amount of data written | |
68 size_t position(void) const { return pos_; } | |
69 | |
70 bool Write(const void* buf, size_t n); | |
71 | |
72 private: | |
73 void* buf_; // start of output buffer | |
74 size_t len_; // length of output | |
75 size_t pos_; // current write position within output | |
76 }; | |
77 | |
78 // Adapter class to make BrotliOut objects from a string. | |
79 class BrotliStringOut : public BrotliOut { | |
80 public: | |
81 // Create a writer that appends its data to buf. | |
82 // buf->size() will grow to at most max_size | |
83 // buf is expected to be empty when constructing BrotliStringOut. | |
84 BrotliStringOut(std::string* buf, size_t max_size); | |
85 | |
86 void Reset(std::string* buf, size_t max_len); | |
87 | |
88 bool Write(const void* buf, size_t n); | |
89 | |
90 private: | |
91 std::string* buf_; // start of output buffer | |
92 size_t max_size_; // max length of output | |
93 }; | |
94 | |
95 // Adapter class to make BrotliIn object from a file. | |
96 class BrotliFileIn : public BrotliIn { | |
97 public: | |
98 BrotliFileIn(FILE* f, size_t max_read_size); | |
99 ~BrotliFileIn(void); | |
100 | |
101 const void* Read(size_t n, size_t* bytes_read); | |
102 | |
103 private: | |
104 FILE* f_; | |
105 char* buf_; | |
106 size_t buf_size_; | |
107 }; | |
108 | |
109 // Adapter class to make BrotliOut object from a file. | |
110 class BrotliFileOut : public BrotliOut { | |
111 public: | |
112 explicit BrotliFileOut(FILE* f); | |
113 | |
114 bool Write(const void* buf, size_t n); | |
115 private: | |
116 FILE* f_; | |
117 }; | |
118 | |
119 } // namespace brotli | |
120 | |
121 #endif // BROTLI_ENC_STREAMS_H_ | |
OLD | NEW |