1 /******************************************************************************
2 * Copyright (c) 2014, Howard Butler (howard@hobu.co)
3 *
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following
8 * conditions are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided
15 * with the distribution.
16 * * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
17 * names of its contributors may be used to endorse or promote
18 * products derived from this software without specific prior
19 * written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 * OF SUCH DAMAGE.
33 ****************************************************************************/
34
35 #include "LzmaCompression.hpp"
36
37 #include <lzma.h>
38
39 namespace pdal
40 {
41
42 class Lzma
43 {
44 protected:
Lzma(BlockCb cb)45 Lzma(BlockCb cb) : m_cb(cb)
46 {
47 m_strm = LZMA_STREAM_INIT;
48 }
49
~Lzma()50 ~Lzma()
51 {
52 lzma_end(&m_strm);
53 }
54
run(const char * buf,size_t bufsize,lzma_action mode)55 void run(const char *buf, size_t bufsize, lzma_action mode)
56 {
57 m_strm.avail_in = bufsize;
58 m_strm.next_in = reinterpret_cast<unsigned char *>(
59 const_cast<char *>(buf));
60 int ret = LZMA_OK;
61 do
62 {
63 m_strm.avail_out = CHUNKSIZE;
64 m_strm.next_out = m_tmpbuf;
65 ret = lzma_code(&m_strm, mode);
66 size_t written = CHUNKSIZE - m_strm.avail_out;
67 if (written)
68 m_cb(reinterpret_cast<char *>(m_tmpbuf), written);
69 } while (ret == LZMA_OK);
70 if (ret == LZMA_STREAM_END)
71 return;
72
73 switch (ret)
74 {
75 case LZMA_MEM_ERROR:
76 throw compression_error("Memory allocation failure.");
77 case LZMA_DATA_ERROR:
78 throw compression_error("LZMA data error.");
79 case LZMA_OPTIONS_ERROR:
80 throw compression_error("Unsupported option.");
81 case LZMA_UNSUPPORTED_CHECK:
82 throw compression_error("Unsupported integrity check.");
83 }
84 }
85
86 protected:
87 lzma_stream m_strm;
88
89 private:
90 unsigned char m_tmpbuf[CHUNKSIZE];
91 BlockCb m_cb;
92 };
93
94
95 class LzmaCompressorImpl : public Lzma
96 {
97 public:
LzmaCompressorImpl(BlockCb cb)98 LzmaCompressorImpl(BlockCb cb) : Lzma(cb)
99 {
100 if (lzma_easy_encoder(&m_strm, 2, LZMA_CHECK_CRC64) != LZMA_OK)
101 throw compression_error("Can't create compressor");
102 }
103
compress(const char * buf,size_t bufsize)104 void compress(const char *buf, size_t bufsize)
105 {
106 run(buf, bufsize, LZMA_RUN);
107 }
108
done()109 void done()
110 {
111 run(nullptr, 0, LZMA_FINISH);
112 }
113 };
114
115
LzmaCompressor(BlockCb cb)116 LzmaCompressor::LzmaCompressor(BlockCb cb) :
117 m_impl(new LzmaCompressorImpl(cb))
118 {}
119
120
~LzmaCompressor()121 LzmaCompressor::~LzmaCompressor()
122 {}
123
124
compress(const char * buf,size_t bufsize)125 void LzmaCompressor::compress(const char *buf, size_t bufsize)
126 {
127 m_impl->compress(buf, bufsize);
128 }
129
130
done()131 void LzmaCompressor::done()
132 {
133 m_impl->done();
134 }
135
136
137 class LzmaDecompressorImpl : public Lzma
138 {
139 public:
LzmaDecompressorImpl(BlockCb cb)140 LzmaDecompressorImpl(BlockCb cb) : Lzma(cb)
141 {
142 if (lzma_auto_decoder(&m_strm, (std::numeric_limits<uint32_t>::max)(),
143 LZMA_TELL_UNSUPPORTED_CHECK))
144 throw compression_error("Can't create decompressor");
145 }
146
decompress(const char * buf,size_t bufsize)147 void decompress(const char *buf, size_t bufsize)
148 {
149 run(buf, bufsize, LZMA_RUN);
150 }
151
done()152 void done()
153 {
154 run(nullptr, 0, LZMA_FINISH);
155 }
156 };
157
LzmaDecompressor(BlockCb cb)158 LzmaDecompressor::LzmaDecompressor(BlockCb cb) :
159 m_impl(new LzmaDecompressorImpl(cb))
160 {}
161
162
~LzmaDecompressor()163 LzmaDecompressor::~LzmaDecompressor()
164 {}
165
166
decompress(const char * buf,size_t bufsize)167 void LzmaDecompressor::decompress(const char *buf, size_t bufsize)
168 {
169 m_impl->decompress(buf, bufsize);
170 }
171
172
done()173 void LzmaDecompressor::done()
174 {
175 m_impl->done();
176 }
177
178 } // namespace pdal
179