179e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// Copyright 2013 Google Inc. All Rights Reserved.
279e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka//
379e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// Licensed under the Apache License, Version 2.0 (the "License");
479e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// you may not use this file except in compliance with the License.
579e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// You may obtain a copy of the License at
679e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka//
779e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// http://www.apache.org/licenses/LICENSE-2.0
879e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka//
979e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// Unless required by applicable law or agreed to in writing, software
1079e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// distributed under the License is distributed on an "AS IS" BASIS,
1179e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1279e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// See the License for the specific language governing permissions and
1379e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// limitations under the License.
1479e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka//
1579e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka// Block split point selection utilities.
1679e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
1779e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka#ifndef BROTLI_ENC_BLOCK_SPLITTER_H_
1879e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka#define BROTLI_ENC_BLOCK_SPLITTER_H_
1979e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
2079e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka#include <stddef.h>
2179e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka#include <stdint.h>
2279e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka#include <string.h>
2379e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka#include <vector>
2479e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka#include <utility>
2579e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
2679e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka#include "./command.h"
2779e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
2879e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadkanamespace brotli {
2979e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
3079e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadkastruct BlockSplit {
3179e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  int num_types_;
3279e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  std::vector<uint8_t> types_;
33e70949119a5540e62ef3b9aae797f797a8d8b44bZoltan Szabadka  std::vector<int> type_codes_;
3479e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  std::vector<int> lengths_;
3579e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka};
3679e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
3779e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadkastruct BlockSplitIterator {
3879e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  explicit BlockSplitIterator(const BlockSplit& split)
3979e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka      : split_(split), idx_(0), type_(0), length_(0) {
4079e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka    if (!split.lengths_.empty()) {
4179e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka      length_ = split.lengths_[0];
4279e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka    }
4379e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  }
4479e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
4579e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  void Next() {
4679e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka    if (length_ == 0) {
4779e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka      ++idx_;
4879e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka      type_ = split_.types_[idx_];
4979e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka      length_ = split_.lengths_[idx_];
5079e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka    }
5179e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka    --length_;
5279e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  }
5379e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
5479e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  const BlockSplit& split_;
5579e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  int idx_;
5679e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  int type_;
5779e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka  int length_;
5879e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka};
5979e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
6079e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadkavoid CopyLiteralsToByteArray(const std::vector<Command>& cmds,
6179e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka                             const uint8_t* data,
6279e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka                             std::vector<uint8_t>* literals);
6379e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
6479e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadkavoid SplitBlock(const std::vector<Command>& cmds,
6579e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka                const uint8_t* data,
6679e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka                BlockSplit* literal_split,
6779e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka                BlockSplit* insert_and_copy_split,
6879e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka                BlockSplit* dist_split);
6979e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
7079e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadkavoid SplitBlockByTotalLength(const std::vector<Command>& all_commands,
7179e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka                             int input_size,
7279e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka                             int target_length,
7379e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka                             std::vector<std::vector<Command> >* blocks);
7479e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
7579e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka}  // namespace brotli
7679e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka
7779e99afe468407e9ff9f0820df7190cb069eabebZoltan Szabadka#endif  // BROTLI_ENC_BLOCK_SPLITTER_H_
78