]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fastlz/native/fastlz_read.c
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.fastlz / native / fastlz_read.c
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "fastlz.h"
16
17 char* uncompressed;
18 int uncompressedCapacity;
19 int position;
20 int uncompressedSize;
21 char* compressed;
22 int compressedCapacity;
23
24 FILE* handle;
25
26 void initRead(FILE* _handle) {
27     uncompressed = 0;
28     uncompressedCapacity = 0;
29     position = 0;
30     uncompressedSize = 0;    
31     compressed = malloc(8);
32     compressedCapacity = 8;
33     handle = _handle;
34 }
35
36 void deinitRead() {
37     free(uncompressed);
38     uncompressed = 0;
39     free(compressed);
40     compressed = 0;
41     handle = 0;
42 }
43
44 void fillBuffer() {
45     int compressedSize;
46
47     fread(compressed, 8, 1, handle);
48     compressedSize = ((int*)compressed)[0];
49     uncompressedSize = ((int*)compressed)[1];
50     
51     if(compressedSize > compressedCapacity) {
52         free(compressed);
53         compressed = malloc(compressedSize);
54         compressedCapacity = compressedSize;
55     }
56     if(uncompressedSize > uncompressedCapacity) {
57         free(uncompressed);
58         uncompressed = malloc(uncompressedSize);
59         uncompressedCapacity = uncompressedSize;
60     }
61
62     fread(compressed, compressedSize, 1, handle);
63     printf("decompress %d %d %d %d\n", compressedSize, compressedCapacity, uncompressedSize, uncompressedCapacity); fflush(stdout);
64     fastlz_decompress(compressed, compressedSize, uncompressed, uncompressedSize); 
65     position = 0;
66 }
67
68 void read(char* data, int offset, int length) {
69     int s;
70
71     while(length > uncompressedSize - position) {
72         s = uncompressedSize - position;
73         memcpy(data + offset, uncompressed + position, s);
74         offset += s;
75         length -= s;
76         fillBuffer();
77     }
78     memcpy(data + offset, uncompressed + position, length);
79     position += length;
80 }
81
82