--- /dev/null
+/*******************************************************************************\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
+#define BLOCKSIZE (128*1024)\r
+\r
+char* uncompressed;\r
+int uncompressedCapacity;\r
+int position;\r
+char* compressed;\r
+int compressedCapacity;\r
+\r
+FILE* handle;\r
+\r
+void initWrite(FILE* _handle) {\r
+ uncompressedCapacity = BLOCKSIZE;\r
+ uncompressed = malloc(uncompressedCapacity);\r
+ position = 0;\r
+ compressedCapacity = 8 + BLOCKSIZE + (BLOCKSIZE/20);\r
+ compressed = malloc(compressedCapacity);\r
+ handle = _handle;\r
+}\r
+\r
+void flushBuffer() {\r
+ int uncompressedSize = position;\r
+ int compressedSize = fastlz_compress(uncompressed, uncompressedSize, compressed+8);\r
+\r
+ ((int*)compressed)[0] = compressedSize;\r
+ ((int*)compressed)[1] = uncompressedSize;\r
+ \r
+ fwrite(compressed, compressedSize+8, 1, handle);\r
+ position = 0;\r
+}\r
+\r
+void deinitWrite() {\r
+ flushBuffer();\r
+\r
+ ((int*)compressed)[0] = 0;\r
+ ((int*)compressed)[1] = 0; \r
+ fwrite(compressed, 8, 1, handle);\r
+\r
+ free(uncompressed);\r
+ uncompressed = 0;\r
+ free(compressed);\r
+ compressed = 0;\r
+ handle = 0;\r
+}\r
+\r
+void write(char* data, int offset, int length) {\r
+ int s;\r
+\r
+ while(length > uncompressedCapacity - position) {\r
+ s = uncompressedCapacity - position;\r
+ memcpy(uncompressed + position, data + offset, s);\r
+ offset += s;\r
+ length -= s;\r
+ position += s;\r
+ flushBuffer();\r
+ }\r
+ memcpy(uncompressed + position, data + offset, length);\r
+ position += length;\r
+}\r