xoreos  0.0.5
lzma.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 // We need to include our types.h before lzma.h to stop it redefining macros
26 #include "src/common/types.h"
27 #include <lzma.h>
28 
29 #include <boost/scope_exit.hpp>
30 
31 #include "src/common/lzma.h"
32 #include "src/common/scopedptr.h"
33 #include "src/common/error.h"
35 
36 namespace Common {
37 
38 /* Custom allocator to work around an issue when linking with liblzma that was
39  * built with a different libc than the one we are build with. This happens
40  * on our AppVeyor builds, for example.
41  *
42  * The underlying issue is that liblzma expects us to free() memory that was
43  * allocated within liblzma in lzma_properties_decode(). liblzma (at the time
44  * of writing) provides no wrapper function for free either. So if our free()
45  * uses different structures from the malloc() in liblzma, this will crash
46  * and burn.
47  *
48  * So instead, we provide lzma_properties_decode() with a custom allocator,
49  * which uses our malloc(), so we can also use our free().
50  */
51 
52 static void * LZMA_API_CALL lzmaAlloc(void *UNUSED(opaque), size_t nmemb, size_t size) {
53  return malloc(nmemb * size);
54 }
55 
56 static void LZMA_API_CALL lzmaFree(void *UNUSED(opaque), void *ptr) {
57  free(ptr);
58 }
59 
60 // Allocators in liblzma < 5.1.3alpha were not const. We allow liblzma >= 5.0.3.
61 static lzma_allocator kLZMAAllocator = {
62  &lzmaAlloc, &lzmaFree, 0
63 };
64 
65 byte *decompressLZMA1(const byte *data, size_t inputSize, size_t outputSize, bool noEndMarker) {
66  lzma_filter filters[2] = {
67  { LZMA_FILTER_LZMA1, 0 },
68  { LZMA_VLI_UNKNOWN , 0 }
69  };
70 
71  if (!lzma_filter_decoder_is_supported(filters[0].id))
72  throw Exception("LZMA1 compression not supported");
73 
74  uint32 propsSize;
75  if (lzma_properties_size(&propsSize, &filters[0]) != LZMA_OK)
76  throw Exception("Can't get LZMA1 properties size");
77 
78  if (propsSize > inputSize)
79  throw Exception("LZMA1 properties size larger than input data");
80 
81  if (lzma_properties_decode(&filters[0], &kLZMAAllocator, data, propsSize) != LZMA_OK)
82  throw Exception("Failed to decode LZMA1 properties");
83 
84  data += propsSize;
85  inputSize -= propsSize;
86 
87  lzma_stream strm = LZMA_STREAM_INIT;
88  BOOST_SCOPE_EXIT( (&strm) (&filters) ) {
89  kLZMAAllocator.free(0, filters[0].options);
90  lzma_end(&strm);
91  } BOOST_SCOPE_EXIT_END
92 
93  lzma_ret lzmaRet = LZMA_OK;
94 
95  if ((lzmaRet = lzma_raw_decoder(&strm, filters)) != LZMA_OK)
96  throw Exception("Failed to create raw LZMA1 decoder: %d", (int) lzmaRet);
97 
98  ScopedArray<byte> outputData(new byte[outputSize]);
99 
100  strm.next_in = data;
101  strm.avail_in = inputSize;
102  strm.next_out = outputData.get();
103  strm.avail_out = outputSize;
104 
105  lzmaRet = lzma_code(&strm, LZMA_FINISH);
106 
107  if (noEndMarker && (lzmaRet == LZMA_OK) && (strm.avail_in == 0) && (strm.avail_out == 0))
108  return outputData.release();
109 
110  if ((lzmaRet != LZMA_STREAM_END) || (strm.avail_out != 0)) {
111  if (lzmaRet == LZMA_OK)
112  throw Exception("Failed to uncompress LZMA1 data: premature end of output buffer");
113 
114  if (strm.avail_out != 0)
115  throw Exception("Failed to uncompress LZMA1 data: output buffer not completely filled");
116 
117  throw Exception("Failed to uncompress LZMA1 data: %d", (int) lzmaRet);
118  }
119 
120  return outputData.release();
121 }
122 
123 SeekableReadStream *decompressLZMA1(ReadStream &input, size_t inputSize, size_t outputSize, bool noEndMarker) {
124  ScopedArray<byte> inputData(new byte[inputSize]);
125  if (input.read(inputData.get(), inputSize) != inputSize)
126  throw Exception(kReadError);
127 
128  const byte *outputData = decompressLZMA1(inputData.get(), inputSize, outputSize, noEndMarker);
129 
130  return new MemoryReadStream(outputData, outputSize, true);
131 }
132 
133 } // End of namespace Common
Generic interface for a readable data stream.
Definition: readstream.h:64
Definition: 2dafile.h:39
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
Implementing the reading stream interfaces for plain memory blocks.
Decompress LZMA, using liblzma.
static void *LZMA_API_CALL lzmaAlloc(void *opaque, size_t nmemb, size_t size)
Definition: lzma.cpp:52
static void LZMA_API_CALL lzmaFree(void *opaque, void *ptr)
Definition: lzma.cpp:56
A simple scoped smart pointer template.
Basic exceptions to throw.
#define UNUSED(x)
Definition: system.h:170
virtual size_t read(void *dataPtr, size_t dataSize)=0
Read data from the stream.
Simple memory based &#39;stream&#39;, which implements the ReadStream interface for a plain memory block...
Definition: memreadstream.h:66
Low-level type definitions to handle fixed width types portably.
StackException Exception
Definition: error.h:59
const Exception kReadError("Read error")
Exception when reading from a stream failed.
Definition: error.h:62
byte * decompressLZMA1(const byte *data, size_t inputSize, size_t outputSize, bool noEndMarker)
Decompress using the LZMA1 algorithm.
Definition: lzma.cpp:65
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
uint32_t uint32
Definition: types.h:204
static lzma_allocator kLZMAAllocator
Definition: lzma.cpp:61
Interface for a seekable & readable data stream.
Definition: readstream.h:265
uint8 byte
Definition: types.h:209