X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.fastlz%2Fnative%2Ffastlz_read.c;fp=bundles%2Forg.simantics.fastlz%2Fnative%2Ffastlz_read.c;h=50314f2590e195b1df372e3aa4390dc0a02df402;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.fastlz/native/fastlz_read.c b/bundles/org.simantics.fastlz/native/fastlz_read.c new file mode 100644 index 000000000..50314f259 --- /dev/null +++ b/bundles/org.simantics.fastlz/native/fastlz_read.c @@ -0,0 +1,82 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +#include +#include +#include +#include "fastlz.h" + +char* uncompressed; +int uncompressedCapacity; +int position; +int uncompressedSize; +char* compressed; +int compressedCapacity; + +FILE* handle; + +void initRead(FILE* _handle) { + uncompressed = 0; + uncompressedCapacity = 0; + position = 0; + uncompressedSize = 0; + compressed = malloc(8); + compressedCapacity = 8; + handle = _handle; +} + +void deinitRead() { + free(uncompressed); + uncompressed = 0; + free(compressed); + compressed = 0; + handle = 0; +} + +void fillBuffer() { + int compressedSize; + + fread(compressed, 8, 1, handle); + compressedSize = ((int*)compressed)[0]; + uncompressedSize = ((int*)compressed)[1]; + + if(compressedSize > compressedCapacity) { + free(compressed); + compressed = malloc(compressedSize); + compressedCapacity = compressedSize; + } + if(uncompressedSize > uncompressedCapacity) { + free(uncompressed); + uncompressed = malloc(uncompressedSize); + uncompressedCapacity = uncompressedSize; + } + + fread(compressed, compressedSize, 1, handle); + printf("decompress %d %d %d %d\n", compressedSize, compressedCapacity, uncompressedSize, uncompressedCapacity); fflush(stdout); + fastlz_decompress(compressed, compressedSize, uncompressed, uncompressedSize); + position = 0; +} + +void read(char* data, int offset, int length) { + int s; + + while(length > uncompressedSize - position) { + s = uncompressedSize - position; + memcpy(data + offset, uncompressed + position, s); + offset += s; + length -= s; + fillBuffer(); + } + memcpy(data + offset, uncompressed + position, length); + position += length; +} + +