summaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorChuck Miller <shadowmil@gmail.com>2010-08-15 15:57:21 -0400
committerChuck Miller <shadowmil@gmail.com>2010-08-15 15:57:21 -0400
commit8f573b6c425dc82be42ad80eae565b0641a90a3b (patch)
tree149c7889d384e10fd9dc95f1aa90478e69b314c8 /src/utils
parentfee9fbce12e620f25aecdc0690285cc47fa8791d (diff)
parent2b676bbf6fcf7dfd45cb33f06dacd2ca5bce19fe (diff)
downloadmana-8f573b6c425dc82be42ad80eae565b0641a90a3b.tar.gz
mana-8f573b6c425dc82be42ad80eae565b0641a90a3b.tar.bz2
mana-8f573b6c425dc82be42ad80eae565b0641a90a3b.tar.xz
mana-8f573b6c425dc82be42ad80eae565b0641a90a3b.zip
Merge branch '1.0'
Conflicts: mana.cbp mana.files src/CMakeLists.txt src/Makefile.am src/utils/xml.cpp
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/xml.cpp77
-rw-r--r--src/utils/xml.h14
-rw-r--r--src/utils/zlib.cpp184
-rw-r--r--src/utils/zlib.h48
4 files changed, 288 insertions, 35 deletions
diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp
index 94e0e38d..bb386f51 100644
--- a/src/utils/xml.cpp
+++ b/src/utils/xml.cpp
@@ -21,19 +21,37 @@
#include "utils/xml.h"
+#include <iostream>
+#include <fstream>
+#include <cstring>
+
+#include <libxml/parser.h>
+#include <libxml/xmlerror.h>
+
#include "log.h"
#include "resources/resourcemanager.h"
-#include <iostream>
-#include <fstream>
-#include <cstring>
+#include "utils/zlib.h"
namespace XML
{
+ static void xmlLogger(void *ctx, xmlErrorPtr error);
+
+ struct XMLContext
+ {
+ std::string file;
+ bool resman;
+ };
+
Document::Document(const std::string &filename, bool useResman):
mDoc(0)
{
+ XMLContext *ctx = new XMLContext();
+ ctx->file = filename;
+ ctx->resman = useResman;
+ xmlSetStructuredErrorFunc(ctx, xmlLogger);
+
int size;
char *data = NULL;
if (useResman)
@@ -43,25 +61,7 @@ namespace XML
}
else
{
- std::ifstream file;
- file.open(filename.c_str(), std::ios::in);
-
- if (file.is_open())
- {
- // Get length of file
- file.seekg (0, std::ios::end);
- size = file.tellg();
- file.seekg(0, std::ios::beg);
-
- data = (char*) malloc(size);
-
- file.read(data, size);
- file.close();
- }
- else
- {
- logger->log("Error loading XML file %s", filename.c_str());
- }
+ data = (char *) loadCompressedFile(filename, size);
}
if (data)
@@ -76,11 +76,8 @@ namespace XML
{
logger->log("Error loading %s", filename.c_str());
}
- }
- Document::Document(const char *data, int size)
- {
- mDoc = xmlParseMemory(data, size);
+ xmlSetStructuredErrorFunc(NULL, xmlLogger);
}
Document::~Document()
@@ -154,4 +151,32 @@ namespace XML
return NULL;
}
+ static void xmlLogger(void *ctx, xmlErrorPtr error)
+ {
+ XMLContext *context = static_cast<XMLContext*>(ctx);
+
+ if (context)
+ logger->log("Error in XML file '%s' on line %d",
+ context->file.c_str(), error->line);
+ else
+ logger->log("Error in unknown xml file on line %d",
+ error->line);
+
+ logger->log(error->message);
+
+ // No need to keep errors around
+ xmlCtxtResetLastError(error->ctxt);
+ }
+
+ void init()
+ {
+ // Initialize libxml2 and check for potential ABI mismatches between
+ // compiled version and the shared library actually used.
+ xmlInitParser();
+ LIBXML_TEST_VERSION;
+
+ // Handle libxml2 error messages
+ xmlSetStructuredErrorFunc(NULL, xmlLogger);
+ }
+
} // namespace XML
diff --git a/src/utils/xml.h b/src/utils/xml.h
index 253c4e09..48e66787 100644
--- a/src/utils/xml.h
+++ b/src/utils/xml.h
@@ -45,15 +45,6 @@ namespace XML
Document(const std::string &filename, bool useResman = true);
/**
- * Constructor that attempts to load an XML document from memory.
- * Does not log errors.
- *
- * @param data the string to parse as XML
- * @param size the length of the string in bytes
- */
- Document(const char *data, int size);
-
- /**
* Destructor. Frees the loaded XML file.
*/
~Document();
@@ -93,6 +84,11 @@ namespace XML
* Finds the first child node with the given name
*/
xmlNodePtr findFirstChildByName(xmlNodePtr parent, const char *name);
+
+ /**
+ * Initializes the XML engine.
+ */
+ void init();
}
#define for_each_xml_child_node(var, parent) \
diff --git a/src/utils/zlib.cpp b/src/utils/zlib.cpp
new file mode 100644
index 00000000..8e2a1795
--- /dev/null
+++ b/src/utils/zlib.cpp
@@ -0,0 +1,184 @@
+/*
+ * The Mana Client
+ * Copyright (C) 2004-2009 The Mana World Development Team
+ * Copyright (C) 2009-2010 The Mana Developers
+ *
+ * This file is part of The Mana Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "utils/zlib.h"
+
+#include "log.h"
+
+#include <cassert>
+#include <cstdlib>
+#include <fstream>
+#include <iostream>
+#include <zlib.h>
+
+/**
+ * Inflates either zlib or gzip deflated memory. The inflated memory is
+ * expected to be freed by the caller.
+ */
+int inflateMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out, unsigned int &outLength)
+{
+ int bufferSize = 256 * 1024;
+ int ret;
+ z_stream strm;
+
+ out = (unsigned char*) malloc(bufferSize);
+
+ strm.zalloc = Z_NULL;
+ strm.zfree = Z_NULL;
+ strm.opaque = Z_NULL;
+ strm.next_in = in;
+ strm.avail_in = inLength;
+ strm.next_out = out;
+ strm.avail_out = bufferSize;
+
+ ret = inflateInit2(&strm, 15 + 32);
+
+ if (ret != Z_OK)
+ return ret;
+
+ do
+ {
+ if (strm.next_out == NULL)
+ {
+ inflateEnd(&strm);
+ return Z_MEM_ERROR;
+ }
+
+ ret = inflate(&strm, Z_NO_FLUSH);
+ assert(ret != Z_STREAM_ERROR);
+
+ switch (ret)
+ {
+ case Z_NEED_DICT:
+ ret = Z_DATA_ERROR;
+ case Z_DATA_ERROR:
+ case Z_MEM_ERROR:
+ (void) inflateEnd(&strm);
+ return ret;
+ }
+
+ if (ret != Z_STREAM_END)
+ {
+ out = (unsigned char*) realloc(out, bufferSize * 2);
+
+ if (out == NULL)
+ {
+ inflateEnd(&strm);
+ return Z_MEM_ERROR;
+ }
+
+ strm.next_out = out + bufferSize;
+ strm.avail_out = bufferSize;
+ bufferSize *= 2;
+ }
+ }
+ while (ret != Z_STREAM_END);
+ assert(strm.avail_in == 0);
+
+ outLength = bufferSize - strm.avail_out;
+ (void) inflateEnd(&strm);
+ return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
+}
+
+int inflateMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out)
+{
+ unsigned int outLength = 0;
+ int ret = inflateMemory(in, inLength, out, outLength);
+
+ if (ret != Z_OK || out == NULL)
+ {
+ if (ret == Z_MEM_ERROR)
+ {
+ logger->log("Error: Out of memory while decompressing data!");
+ }
+ else if (ret == Z_VERSION_ERROR)
+ {
+ logger->log("Error: Incompatible zlib version!");
+ }
+ else if (ret == Z_DATA_ERROR)
+ {
+ logger->log("Error: Incorrect zlib compressed data!");
+ }
+ else
+ {
+ logger->log("Error: Unknown error while decompressing data!");
+ }
+
+ free(out);
+ out = NULL;
+ outLength = 0;
+ }
+
+ return outLength;
+}
+
+void *loadCompressedFile(const std::string &filename, int &filesize)
+{
+ std::ifstream file;
+ file.open(filename.c_str(), std::ios::in);
+
+ if (file.is_open())
+ {
+ // Get length of file
+ file.seekg (0, std::ios::end);
+ filesize = file.tellg();
+ file.seekg(0, std::ios::beg);
+
+ char *buffer = (char *) malloc(filesize);
+
+ file.read(buffer, filesize);
+ file.close();
+
+ unsigned char *inflated;
+ unsigned int inflatedSize;
+
+ if (inflated && filename.find(".gz", filename.length() - 3)
+ != std::string::npos)
+ {
+ // Inflate the gzipped map data
+ inflatedSize =
+ inflateMemory((unsigned char*) buffer, filesize, inflated);
+ free(buffer);
+
+ if (inflated == NULL)
+ {
+ logger->log("Could not decompress file: %s",
+ filename.c_str());
+ return NULL;
+ }
+
+ filesize = inflatedSize;
+ return inflated;
+ }
+ else
+ {
+ return buffer;
+ }
+ }
+ else
+ {
+ logger->log("Error loading file from drive: %s", filename.c_str());
+ }
+
+ return NULL;
+}
diff --git a/src/utils/zlib.h b/src/utils/zlib.h
new file mode 100644
index 00000000..003e64b6
--- /dev/null
+++ b/src/utils/zlib.h
@@ -0,0 +1,48 @@
+/*
+ * The Mana Client
+ * Copyright (C) 2007-2009 The Mana World Development Team
+ * Copyright (C) 2009-2010 The Mana Developers
+ *
+ * This file is part of The Mana Client.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef UTILS_ZLIB_H
+#define UTILS_ZLIB_H
+
+#include <string>
+
+/**
+ * Inflates either zlib or gzip deflated memory. The inflated memory is
+ * expected to be freed by the caller.
+ */
+int inflateMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out, unsigned int &outLength);
+
+int inflateMemory(unsigned char *in, unsigned int inLength,
+ unsigned char *&out);
+
+/**
+ * Loads the given file from the filesystem, uncompressing if it ends in ".gz".
+ *
+ * @param filename The name of the file to be loaded and uncompressed
+ * @param filesize The size of the file that was loaded and uncompressed.
+ *
+ * @return An allocated byte array containing the data that was loaded and
+ * uncompressed, or <code>NULL</code> on fail.
+ */
+void *loadCompressedFile(const std::string &filename, int &filesize);
+
+#endif // UTILS_ZLIB_H