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