• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef _BSDIFF_COMPRESSOR_BUFFER_H_
6 #define _BSDIFF_COMPRESSOR_BUFFER_H_
7 
8 #include <stddef.h>
9 #include <stdint.h>
10 
11 #include <vector>
12 
13 namespace bsdiff {
14 
15 // An utility class to store the output from a compressor. It has a small
16 // temporary buffer that holds the compressed data every time the compressed
17 // stream has some output. That buffer is then appended to a list of vectors
18 // stored in memory. This approach is more efficient than a simple vector
19 // because it saves some resize / reallocation of memory. And the compressed
20 // data is copied once no matter how many times we fetch output from the
21 // compressor.
22 class CompressorBuffer {
23  public:
CompressorBuffer(size_t buffer_size)24   explicit CompressorBuffer(size_t buffer_size) {
25     comp_buffer_.resize(buffer_size);
26   }
27 
buffer_size()28   size_t buffer_size() { return comp_buffer_.size(); }
buffer_data()29   uint8_t* buffer_data() { return comp_buffer_.data(); }
30 
31   // Concatenate the data in |comp_chunks_| and return the data in the form of
32   // a single vector.
33   const std::vector<uint8_t>& GetCompressedData();
34 
35   // Add the data in |comp_buffer_| to the end of |comp_chunks_|.
36   void AddDataToChunks(size_t data_size);
37 
38  private:
39   // A list of chunks of compressed data. The final compressed representation is
40   // the concatenation of all the compressed data.
41   std::vector<std::vector<uint8_t>> comp_chunks_;
42 
43   // A concatenated version of the |comp_chunks_|, used to store the compressed
44   // memory after Finish() is called.
45   std::vector<uint8_t> comp_data_;
46 
47   // A temporary compression buffer for multiple calls to Write().
48   std::vector<uint8_t> comp_buffer_;
49 };
50 
51 }  // namespace bsdiff
52 
53 #endif  // _BSDIFF_COMPRESSOR_BUFFER_H_
54