]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fastlz/native/fastlz_read.c
Merge commit '876ede6'
[simantics/platform.git] / bundles / org.simantics.fastlz / native / fastlz_read.c
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 #include <stdio.h>\r
13 #include <stdlib.h>\r
14 #include <string.h>\r
15 #include "fastlz.h"\r
16 \r
17 char* uncompressed;\r
18 int uncompressedCapacity;\r
19 int position;\r
20 int uncompressedSize;\r
21 char* compressed;\r
22 int compressedCapacity;\r
23 \r
24 FILE* handle;\r
25 \r
26 void initRead(FILE* _handle) {\r
27     uncompressed = 0;\r
28     uncompressedCapacity = 0;\r
29     position = 0;\r
30     uncompressedSize = 0;    \r
31     compressed = malloc(8);\r
32     compressedCapacity = 8;\r
33     handle = _handle;\r
34 }\r
35 \r
36 void deinitRead() {\r
37     free(uncompressed);\r
38     uncompressed = 0;\r
39     free(compressed);\r
40     compressed = 0;\r
41     handle = 0;\r
42 }\r
43 \r
44 void fillBuffer() {\r
45     int compressedSize;\r
46 \r
47     fread(compressed, 8, 1, handle);\r
48     compressedSize = ((int*)compressed)[0];\r
49     uncompressedSize = ((int*)compressed)[1];\r
50     \r
51     if(compressedSize > compressedCapacity) {\r
52         free(compressed);\r
53         compressed = malloc(compressedSize);\r
54         compressedCapacity = compressedSize;\r
55     }\r
56     if(uncompressedSize > uncompressedCapacity) {\r
57         free(uncompressed);\r
58         uncompressed = malloc(uncompressedSize);\r
59         uncompressedCapacity = uncompressedSize;\r
60     }\r
61 \r
62     fread(compressed, compressedSize, 1, handle);\r
63     printf("decompress %d %d %d %d\n", compressedSize, compressedCapacity, uncompressedSize, uncompressedCapacity); fflush(stdout);\r
64     fastlz_decompress(compressed, compressedSize, uncompressed, uncompressedSize); \r
65     position = 0;\r
66 }\r
67 \r
68 void read(char* data, int offset, int length) {\r
69     int s;\r
70 \r
71     while(length > uncompressedSize - position) {\r
72         s = uncompressedSize - position;\r
73         memcpy(data + offset, uncompressed + position, s);\r
74         offset += s;\r
75         length -= s;\r
76         fillBuffer();\r
77     }\r
78     memcpy(data + offset, uncompressed + position, length);\r
79     position += length;\r
80 }\r
81 \r
82 \r