• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 5395967798

28 Jun 2023 12:28AM UTC coverage: 91.737% (+0.01%) from 91.726%
5395967798

push

github

randombit
Merge GH #3606 Add example of custom entropy source

78190 of 85233 relevant lines covered (91.74%)

12513919.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/lib/compression/lzma/lzma.cpp
1
/*
2
* Lzma Compressor
3
* (C) 2001 Peter J Jones
4
*     2001-2007,2014 Jack Lloyd
5
*     2006 Matt Johnston
6
*     2012 Vojtech Kral
7
*
8
* Botan is released under the Simplified BSD License (see license.txt)
9
*/
10

11
#include <botan/lzma.h>
12

13
#include <botan/exceptn.h>
14
#include <botan/internal/compress_utils.h>
15
#include <lzma.h>
16

17
namespace Botan {
18

19
namespace {
20

21
class LZMA_Stream : public Zlib_Style_Stream<lzma_stream, uint8_t> {
22
   public:
23
      LZMA_Stream() {
48✔
24
         m_allocator.opaque = alloc();
48✔
25
         m_allocator.alloc = Compression_Alloc_Info::malloc<size_t>;
48✔
26
         m_allocator.free = Compression_Alloc_Info::free;
48✔
27
         streamp()->allocator = &m_allocator;
48✔
28
      }
29

30
      ~LZMA_Stream() override { ::lzma_end(streamp()); }
×
31

32
      LZMA_Stream(const LZMA_Stream& other) = delete;
33
      LZMA_Stream(LZMA_Stream&& other) = delete;
34
      LZMA_Stream& operator=(const LZMA_Stream& other) = delete;
35
      LZMA_Stream& operator=(LZMA_Stream&& other) = delete;
36

37
      bool run(uint32_t flags) override {
122✔
38
         lzma_ret rc = ::lzma_code(streamp(), static_cast<lzma_action>(flags));
122✔
39

40
         if(rc != LZMA_OK && rc != LZMA_STREAM_END) {
122✔
41
            throw Compression_Error("lzma_code", ErrorType::LzmaError, rc);
×
42
         }
43

44
         return (rc == LZMA_STREAM_END);
122✔
45
      }
46

47
      uint32_t run_flag() const override { return LZMA_RUN; }
74✔
48

49
      uint32_t flush_flag() const override { return LZMA_FULL_FLUSH; }
10✔
50

51
      uint32_t finish_flag() const override { return LZMA_FINISH; }
28✔
52

53
   private:
54
      ::lzma_allocator m_allocator;
55
};
56

57
class LZMA_Compression_Stream final : public LZMA_Stream {
58
   public:
59
      explicit LZMA_Compression_Stream(size_t level) {
28✔
60
         if(level == 0) {
28✔
61
            level = 6;  // default
62
         } else if(level > 9) {
24✔
63
            level = 9;  // clamp to maximum allowed value
64
         }
65

66
         lzma_ret rc = ::lzma_easy_encoder(streamp(), static_cast<uint32_t>(level), LZMA_CHECK_CRC64);
28✔
67

68
         if(rc != LZMA_OK) {
28✔
69
            throw Compression_Error("lzam_easy_encoder", ErrorType::LzmaError, rc);
×
70
         }
71
      }
28✔
72
};
73

74
class LZMA_Decompression_Stream final : public LZMA_Stream {
75
   public:
76
      LZMA_Decompression_Stream() {
20✔
77
         lzma_ret rc = ::lzma_stream_decoder(streamp(), UINT64_MAX, LZMA_TELL_UNSUPPORTED_CHECK);
20✔
78

79
         if(rc != LZMA_OK) {
20✔
80
            throw Compression_Error("lzma_stream_decoder", ErrorType::LzmaError, rc);
×
81
         }
82
      }
20✔
83
};
84

85
}  // namespace
86

87
std::unique_ptr<Compression_Stream> LZMA_Compression::make_stream(size_t level) const {
28✔
88
   return std::make_unique<LZMA_Compression_Stream>(level);
28✔
89
}
90

91
std::unique_ptr<Compression_Stream> LZMA_Decompression::make_stream() const {
20✔
92
   return std::make_unique<LZMA_Decompression_Stream>();
20✔
93
}
94

95
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc