]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.fastlz/native/fastlz_read.c
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.fastlz / native / fastlz_read.c
diff --git a/bundles/org.simantics.fastlz/native/fastlz_read.c b/bundles/org.simantics.fastlz/native/fastlz_read.c
new file mode 100644 (file)
index 0000000..50314f2
--- /dev/null
@@ -0,0 +1,82 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include "fastlz.h"\r
+\r
+char* uncompressed;\r
+int uncompressedCapacity;\r
+int position;\r
+int uncompressedSize;\r
+char* compressed;\r
+int compressedCapacity;\r
+\r
+FILE* handle;\r
+\r
+void initRead(FILE* _handle) {\r
+    uncompressed = 0;\r
+    uncompressedCapacity = 0;\r
+    position = 0;\r
+    uncompressedSize = 0;    \r
+    compressed = malloc(8);\r
+    compressedCapacity = 8;\r
+    handle = _handle;\r
+}\r
+\r
+void deinitRead() {\r
+    free(uncompressed);\r
+    uncompressed = 0;\r
+    free(compressed);\r
+    compressed = 0;\r
+    handle = 0;\r
+}\r
+\r
+void fillBuffer() {\r
+    int compressedSize;\r
+\r
+    fread(compressed, 8, 1, handle);\r
+    compressedSize = ((int*)compressed)[0];\r
+    uncompressedSize = ((int*)compressed)[1];\r
+    \r
+    if(compressedSize > compressedCapacity) {\r
+        free(compressed);\r
+        compressed = malloc(compressedSize);\r
+        compressedCapacity = compressedSize;\r
+    }\r
+    if(uncompressedSize > uncompressedCapacity) {\r
+        free(uncompressed);\r
+        uncompressed = malloc(uncompressedSize);\r
+        uncompressedCapacity = uncompressedSize;\r
+    }\r
+\r
+    fread(compressed, compressedSize, 1, handle);\r
+    printf("decompress %d %d %d %d\n", compressedSize, compressedCapacity, uncompressedSize, uncompressedCapacity); fflush(stdout);\r
+    fastlz_decompress(compressed, compressedSize, uncompressed, uncompressedSize); \r
+    position = 0;\r
+}\r
+\r
+void read(char* data, int offset, int length) {\r
+    int s;\r
+\r
+    while(length > uncompressedSize - position) {\r
+        s = uncompressedSize - position;\r
+        memcpy(data + offset, uncompressed + position, s);\r
+        offset += s;\r
+        length -= s;\r
+        fillBuffer();\r
+    }\r
+    memcpy(data + offset, uncompressed + position, length);\r
+    position += length;\r
+}\r
+\r
+\r