scc  2022.4.0
SystemC components library
lz4_streambuf.h
1 /*******************************************************************************
2  * Copyright 2022 MINRES Technologies GmbH
3  * SPDX-License-Identifier: Apache-2.0
4  *******************************************************************************
5  *
6  * Created on: May 7, 2022
7  * Author: eyck
8  */
9 
10 #ifndef _UTIL_LZ4_STREAMBUF_H_
11 #define _UTIL_LZ4_STREAMBUF_H_
12 
13 #include <cassert>
14 #include <istream>
15 #include <lz4frame.h>
16 #include <ostream>
17 #include <streambuf>
18 #include <vector>
19 
20 namespace util {
21 class lz4c_steambuf : public std::streambuf {
22 public:
23  lz4c_steambuf(std::ostream& sink, size_t buf_size);
24 
25  ~lz4c_steambuf();
26 
27  void close();
28 
29  lz4c_steambuf(const lz4c_steambuf&) = delete;
30  lz4c_steambuf(lz4c_steambuf&&) = delete;
31  lz4c_steambuf& operator=(const lz4c_steambuf&) = delete;
32  lz4c_steambuf& operator=(lz4c_steambuf&&) = delete;
33 
34 private:
35  int_type overflow(int_type ch) override;
36 
37  int_type sync() override;
38 
39  void compress_and_write();
40 
41  std::ostream& sink;
42  std::vector<char> src_buf;
43  std::vector<char> dest_buf;
44  LZ4F_compressionContext_t ctx{nullptr};
45  bool closed{false};
46 };
47 
48 class lz4d_streambuf : public std::streambuf {
49 public:
50  lz4d_streambuf(std::istream& source, size_t buf_size);
51 
52  ~lz4d_streambuf();
53 
54  int_type underflow() override;
55 
56  lz4d_streambuf(const lz4d_streambuf&) = delete;
57  lz4d_streambuf(lz4d_streambuf&&) = delete;
58  lz4d_streambuf& operator=(const lz4d_streambuf&) = delete;
59  lz4d_streambuf& operator=(lz4d_streambuf&&) = delete;
60 
61 private:
62  std::istream& src_str;
63  std::vector<char> src_buf;
64  std::vector<char> dest_buf;
65  size_t offset{0};
66  size_t src_buf_size{0};
67  LZ4F_decompressionContext_t ctx{nullptr};
68 };
69 
70 } // namespace util
71 
72 #endif /* _UTIL_LZ4_STREAMBUF_H_ */
SCC common utilities.
Definition: bit_field.h:30