blob: 49bd5b138e2c3530cd2b4f93a40cd83d74c2e1f3 [file] [log] [blame]
Zoltan Szabadka79e99af2013-10-23 13:06:13 +02001// 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>
Zoltan Szabadka1571db32013-11-15 19:02:17 +010023#include <vector>
24#include "./hash.h"
25#include "./ringbuffer.h"
Zoltan Szabadka79e99af2013-10-23 13:06:13 +020026
27namespace brotli {
28
Zoltan Szabadka1571db32013-11-15 19:02:17 +010029class BrotliCompressor {
30 public:
31 BrotliCompressor();
32 ~BrotliCompressor();
33
34 // Writes the stream header into the internal output buffer.
35 void WriteStreamHeader();
36
37 // Encodes the data in input_buffer as a meta-block and writes it to
38 // encoded_buffer and sets *encoded_size to the number of bytes that was
39 // written.
40 void WriteMetaBlock(const size_t input_size,
41 const uint8_t* input_buffer,
Zoltan Szabadkae7094912013-12-12 13:18:04 +010042 const bool is_last,
Zoltan Szabadka1571db32013-11-15 19:02:17 +010043 size_t* encoded_size,
44 uint8_t* encoded_buffer);
45
46 // Writes a zero-length meta-block with end-of-input bit set to the
47 // internal output buffer and copies the output buffer to encoded_buffer and
48 // sets *encoded_size to the number of bytes written.
49 void FinishStream(size_t* encoded_size, uint8_t* encoded_buffer);
50
51
52 private:
Zoltan Szabadka2733d6c2014-02-17 14:25:36 +010053 // Initializes the hasher with the hashes of dictionary words.
54 void StoreDictionaryWordHashes();
55
Roderick Sheeter437bbad2013-11-19 14:32:56 -080056 int window_bits_;
Zoltan Szabadka1571db32013-11-15 19:02:17 +010057 Hasher* hasher_;
58 int dist_ringbuffer_[4];
59 size_t dist_ringbuffer_idx_;
60 size_t input_pos_;
61 RingBuffer ringbuffer_;
62 std::vector<float> literal_cost_;
63 int storage_ix_;
64 uint8_t* storage_;
65};
66
Zoltan Szabadka79e99af2013-10-23 13:06:13 +020067// Compresses the data in input_buffer into encoded_buffer, and sets
68// *encoded_size to the compressed length.
69// Returns 0 if there was an error and 1 otherwise.
70int BrotliCompressBuffer(size_t input_size,
71 const uint8_t* input_buffer,
72 size_t* encoded_size,
73 uint8_t* encoded_buffer);
74
75
76} // namespace brotli
77
78#endif // BROTLI_ENC_ENCODE_H_