• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // API for Brotli compression
16 
17 #ifndef BROTLI_ENC_ENCODE_H_
18 #define BROTLI_ENC_ENCODE_H_
19 
20 #include <stddef.h>
21 #include <stdint.h>
22 #include <string>
23 #include <vector>
24 #include "./hash.h"
25 #include "./ringbuffer.h"
26 #include "./static_dict.h"
27 
28 namespace brotli {
29 
30 struct BrotliParams {
31   enum Mode {
32     MODE_TEXT = 0,
33     MODE_FONT = 1,
34   };
35   Mode mode;
36 
BrotliParamsBrotliParams37   BrotliParams() : mode(MODE_TEXT) {}
38 };
39 
40 class BrotliCompressor {
41  public:
42   explicit BrotliCompressor(BrotliParams params);
43   ~BrotliCompressor();
44 
45   // Writes the stream header into the internal output buffer.
46   void WriteStreamHeader();
47 
48   // Encodes the data in input_buffer as a meta-block and writes it to
49   // encoded_buffer and sets *encoded_size to the number of bytes that was
50   // written.
51   void WriteMetaBlock(const size_t input_size,
52                       const uint8_t* input_buffer,
53                       const bool is_last,
54                       size_t* encoded_size,
55                       uint8_t* encoded_buffer);
56 
57   // Writes a zero-length meta-block with end-of-input bit set to the
58   // internal output buffer and copies the output buffer to encoded_buffer and
59   // sets *encoded_size to the number of bytes written.
60   void FinishStream(size_t* encoded_size, uint8_t* encoded_buffer);
61 
62 
63  private:
64   // Initializes the hasher with the hashes of dictionary words.
65   void StoreDictionaryWordHashes();
66 
67   BrotliParams params_;
68   int window_bits_;
69   std::unique_ptr<Hashers> hashers_;
70   Hashers::Type hash_type_;
71   int dist_ringbuffer_[4];
72   size_t dist_ringbuffer_idx_;
73   size_t input_pos_;
74   RingBuffer ringbuffer_;
75   std::vector<float> literal_cost_;
76   int storage_ix_;
77   uint8_t* storage_;
78   static StaticDictionary *static_dictionary_;
79 };
80 
81 // Compresses the data in input_buffer into encoded_buffer, and sets
82 // *encoded_size to the compressed length.
83 // Returns 0 if there was an error and 1 otherwise.
84 int BrotliCompressBuffer(BrotliParams params,
85                          size_t input_size,
86                          const uint8_t* input_buffer,
87                          size_t* encoded_size,
88                          uint8_t* encoded_buffer);
89 
90 
91 }  // namespace brotli
92 
93 #endif  // BROTLI_ENC_ENCODE_H_
94