1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
17 #define BLOCKSIZE (128*1024)
\r
20 int uncompressedCapacity;
\r
23 int compressedCapacity;
\r
27 void initWrite(FILE* _handle) {
\r
28 uncompressedCapacity = BLOCKSIZE;
\r
29 uncompressed = malloc(uncompressedCapacity);
\r
31 compressedCapacity = 8 + BLOCKSIZE + (BLOCKSIZE/20);
\r
32 compressed = malloc(compressedCapacity);
\r
36 void flushBuffer() {
\r
37 int uncompressedSize = position;
\r
38 int compressedSize = fastlz_compress(uncompressed, uncompressedSize, compressed+8);
\r
40 ((int*)compressed)[0] = compressedSize;
\r
41 ((int*)compressed)[1] = uncompressedSize;
\r
43 fwrite(compressed, compressedSize+8, 1, handle);
\r
47 void deinitWrite() {
\r
50 ((int*)compressed)[0] = 0;
\r
51 ((int*)compressed)[1] = 0;
\r
52 fwrite(compressed, 8, 1, handle);
\r
61 void write(char* data, int offset, int length) {
\r
64 while(length > uncompressedCapacity - position) {
\r
65 s = uncompressedCapacity - position;
\r
66 memcpy(uncompressed + position, data + offset, s);
\r
72 memcpy(uncompressed + position, data + offset, length);
\r