]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.fastlz/native/jniWrapper.c
Merge commit '8e4e41fa135641b23f68e205832e0696951c5f63'
[simantics/platform.git] / bundles / org.simantics.fastlz / native / jniWrapper.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 "fastlz.h"\r
13 #include "lz4.h"\r
14 #include <stdio.h>\r
15 #include "jni.h"\r
16 #include <string.h>\r
17 #include <stdlib.h>\r
18 \r
19 JNIEXPORT jint JNICALL Java_org_simantics_fastlz_FastLZ_compress(JNIEnv* env, jclass clazz, \r
20                                                                  jobject input, jint inputOffset, jint length, \r
21                                                                  jobject output, jint outputOffset) {\r
22     void* inputAddress = (char*)(*env)->GetDirectBufferAddress(env, input) + inputOffset;\r
23     void* outputAddress = (char*)(*env)->GetDirectBufferAddress(env, output) + outputOffset;\r
24     return fastlz_compress(inputAddress, length, outputAddress);\r
25 }\r
26 \r
27 JNIEXPORT jint JNICALL Java_org_simantics_fastlz_FastLZ_decompress(JNIEnv* env, jclass clazz, \r
28                                                                    jobject input, jint inputOffset, jint length, \r
29                                                                    jobject output, jint outputOffset, jint maxout) {\r
30     void* inputAddress = (char*)(*env)->GetDirectBufferAddress(env, input) + inputOffset;\r
31     void* outputAddress = (char*)(*env)->GetDirectBufferAddress(env, output) + outputOffset;\r
32     return fastlz_decompress(inputAddress, length, outputAddress, maxout);\r
33 }\r
34 \r
35 #define INITIAL_SIZE 2000000\r
36 \r
37 JNIEXPORT jint JNICALL Java_org_simantics_fastlz_FastLZ_decompressCluster(JNIEnv* env, jclass clazz, jobject deflated, jint deflatedSize, jint inflatedSize, jobjectArray arrays) {\r
38 \r
39         static char *inflateBuffer = 0;\r
40         static int inflateBufferSize = 0;\r
41 \r
42         int ll, il, bl;\r
43 \r
44         jlongArray longs;\r
45         jintArray ints;\r
46         jbyteArray bytes;\r
47 \r
48         char *input = (char*)(*env)->GetDirectBufferAddress(env, deflated);\r
49         char *address;\r
50 \r
51         if(inflateBufferSize < inflatedSize) {\r
52                 if(!inflateBuffer) {\r
53                         if(inflatedSize < INITIAL_SIZE) inflatedSize = INITIAL_SIZE;\r
54                         inflateBuffer = malloc(inflatedSize);\r
55                         inflateBufferSize = inflatedSize;\r
56                 } else {\r
57                         if(inflateBuffer) free(inflateBuffer);\r
58                         inflateBuffer = malloc(inflatedSize);\r
59                         inflateBufferSize = inflatedSize;\r
60                 }\r
61         }\r
62 \r
63         address = inflateBuffer;\r
64 \r
65         fastlz_decompress(input, deflatedSize, inflateBuffer, inflateBufferSize);\r
66 \r
67         ll = *(int *)address;\r
68         longs = (*env)->NewLongArray(env, ll);\r
69         (*env)->SetLongArrayRegion(env, longs, 0, ll, (const jlong *)(address + 4));\r
70         (*env)->SetObjectArrayElement(env, arrays, 0, longs);\r
71 \r
72         address += 4 + 8 * ll;\r
73 \r
74         il = *(int *)address;\r
75         ints = (*env)->NewIntArray(env, il);\r
76         (*env)->SetIntArrayRegion(env, ints, 0, il, (const jint *)(address + 4));\r
77         (*env)->SetObjectArrayElement(env, arrays, 1, ints);\r
78 \r
79         address += 4 * il + 4;\r
80 \r
81         bl = *(int *)address;\r
82         bytes = (*env)->NewByteArray(env, bl);\r
83         (*env)->SetByteArrayRegion(env, bytes, 0, bl, (const jbyte *)(address + 4));\r
84         (*env)->SetObjectArrayElement(env, arrays, 2, bytes);\r
85 \r
86         return 0;\r
87 \r
88 }\r
89 \r