summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormatthijs <matthijs@openttd.org>2014-10-23 10:49:19 +0000
committermatthijs <matthijs@openttd.org>2014-10-23 10:49:19 +0000
commit0c0f660059f76624b4bdf2beef784d2a28d6c3b8 (patch)
tree2d30619820cfdca07dff58547399b452c28b793a
parent983d286ebfc3d49789cc2e33edfd18e5034d2e67 (diff)
downloadopenttd-0c0f660059f76624b4bdf2beef784d2a28d6c3b8.tar.xz
(svn r27035) -Feature: Support .txt.xz changelog, readme and license files in basesets, newgrfs, etc
-rw-r--r--src/textfile_gui.cpp71
1 files changed, 70 insertions, 1 deletions
diff --git a/src/textfile_gui.cpp b/src/textfile_gui.cpp
index 0b9814f67..3efd98497 100644
--- a/src/textfile_gui.cpp
+++ b/src/textfile_gui.cpp
@@ -25,6 +25,10 @@
#include <zlib.h>
#endif
+#if defined(WITH_LZMA)
+#include <lzma.h>
+#endif
+
#include "safeguards.h"
/** Widgets for the textfile window. */
@@ -253,6 +257,61 @@ static void Gunzip(byte **bufp, size_t *sizep)
}
#endif
+#if defined(WITH_LZMA)
+
+/**
+ * Do an in-memory xunzip operation. This works on a .xz or (legacy)
+ * .lzma file.
+ * @param bufp A pointer to a buffer containing the input data. This
+ * buffer will be freed and replaced by a buffer containing
+ * the uncompressed data.
+ * @param sizep A pointer to the buffer size. Before the call, the value
+ * pointed to should contain the size of the input buffer.
+ * After the call, it contains the size of the uncompressed
+ * data.
+ *
+ * When decompressing fails, *bufp is set to NULL and *sizep to 0. The
+ * compressed buffer passed in is still freed in this case.
+ */
+static void Xunzip(byte **bufp, size_t *sizep)
+{
+ static const int BLOCKSIZE = 8192;
+ byte *buf = NULL;
+ size_t alloc_size = 0;
+ lzma_stream z = LZMA_STREAM_INIT;
+ int res;
+
+ z.next_in = *bufp;
+ z.avail_in = *sizep;
+
+ res = lzma_auto_decoder(&z, UINT64_MAX, LZMA_CONCATENATED);
+ /* Z_BUF_ERROR just means we need more space */
+ while (res == LZMA_OK || (res == LZMA_BUF_ERROR && z.avail_out == 0)) {
+ /* When we get here, we're either just starting, or
+ * inflate is out of output space - allocate more */
+ alloc_size += BLOCKSIZE;
+ z.avail_out += BLOCKSIZE;
+ buf = ReallocT(buf, alloc_size);
+ z.next_out = buf + alloc_size - z.avail_out;
+ res = lzma_code(&z, LZMA_FINISH);
+ }
+
+ free(*bufp);
+ lzma_end(&z);
+
+ if (res == LZMA_STREAM_END) {
+ *bufp = buf;
+ *sizep = alloc_size - z.avail_out;
+ } else {
+ /* Something went wrong */
+ *bufp = NULL;
+ *sizep = 0;
+ free(buf);
+ }
+}
+#endif
+
+
/**
* Loads the textfile text from file and setup #lines.
*/
@@ -273,14 +332,21 @@ static void Gunzip(byte **bufp, size_t *sizep)
if (read != filesize) return;
-#if defined(WITH_ZLIB)
+#if defined(WITH_ZLIB) || defined(WITH_LZMA)
const char *suffix = strrchr(textfile, '.');
if (suffix == NULL) return;
+#endif
+#if defined(WITH_ZLIB)
/* In-place gunzip */
if (strcmp(suffix, ".gz") == 0) Gunzip((byte**)&this->text, &filesize);
#endif
+#if defined(WITH_LZMA)
+ /* In-place xunzip */
+ if (strcmp(suffix, ".xz") == 0) Xunzip((byte**)&this->text, &filesize);
+#endif
+
if (!this->text) return;
/* Add space for trailing \0 */
@@ -341,6 +407,9 @@ const char *GetTextfile(TextfileType type, Subdirectory dir, const char *filenam
#if defined(WITH_ZLIB)
"txt.gz",
#endif
+#if defined(WITH_LZMA)
+ "txt.xz",
+#endif
};
for (size_t i = 0; i < lengthof(exts); i++) {