* Removed all Id tags.
[cacao.git] / src / native / vm / cldc1.1 / com_sun_cldc_io_ResourceInputStream.c
1 /* src/native/vm/cldc1.1/com_sun_cldc_io_ResourceInputStream.c
2
3    Copyright (C) 2007 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27 #include <sys/stat.h>
28 #include <sys/mman.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <zlib.h>
32
33 #include "config.h"
34
35 #include "mm/memory.h"
36
37 #include "native/jni.h"
38 #include "native/llni.h"
39 #include "native/native.h"
40
41 #include "native/include/java_lang_Object.h"
42 #include "native/include/java_lang_String.h"
43 #include "native/include/com_sun_cldc_io_ResourceInputStream.h"
44 #include "native/include/com_sun_cldchi_jvm_FileDescriptor.h"
45
46 #include "vm/types.h"
47 #include "vm/builtin.h"
48 #include "vm/vm.h" /* REMOVE ME: temporarily */
49 #include "vm/exceptions.h"
50 #include "vm/stringlocal.h"
51
52 #include "vmcore/zip.h"
53
54 #include "threads/lock-common.h"
55
56 /* native methods implemented by this file ************************************/
57  
58 static JNINativeMethod methods[] = {
59         { "open", "(Ljava/lang/String;)Ljava/lang/Object;", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_open },
60         { "bytesRemain", "(Ljava/lang/Object;)I", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_bytesRemain },
61         { "readByte", "(Ljava/lang/Object;)I", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_readByte },
62         { "readBytes", "(Ljava/lang/Object;[BII)I", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_readBytes },
63         { "clone", "(Ljava/lang/Object;)Ljava/lang/Object;", (void *) (ptrint) &Java_com_sun_cldc_io_ResourceInputStream_clone },
64 };
65  
66 /* _Jv_com_sun_cldc_io_ResourceInputStream_init ********************************
67  
68    Register native functions.
69  
70 *******************************************************************************/
71  
72 void _Jv_com_sun_cldc_io_ResourceInputStream_init(void)
73 {
74         utf *u;
75  
76         u = utf_new_char("com/sun/cldc/io/ResourceInputStream");
77  
78         native_method_register(u, methods, NATIVE_METHODS_COUNT);
79 }
80
81 static struct com_sun_cldchi_jvm_FileDescriptor* zip_read_resource(list_classpath_entry *lce, utf *name)
82 {
83         hashtable_zipfile_entry *htzfe;
84         lfh                      lfh;
85         u1                      *indata;
86         u1                      *outdata;
87         z_stream                 zs;
88         int                      err;
89         
90         classinfo *ci;
91         com_sun_cldchi_jvm_FileDescriptor *fileDescriptor = NULL;
92
93         /* try to find the class in the current archive */
94
95         htzfe = zip_find(lce, name);
96
97         if (htzfe == NULL)
98                 return NULL;
99
100         /* read stuff from local file header */
101
102         lfh.filenamelength   = SUCK_LE_U2(htzfe->data + LFH_FILE_NAME_LENGTH);
103         lfh.extrafieldlength = SUCK_LE_U2(htzfe->data + LFH_EXTRA_FIELD_LENGTH);
104
105         indata = htzfe->data +
106                 LFH_HEADER_SIZE +
107                 lfh.filenamelength +
108                 lfh.extrafieldlength;
109
110         /* allocate buffer for uncompressed data */
111
112         outdata = MNEW(u1, htzfe->uncompressedsize);
113
114         /* how is the file stored? */
115
116         switch (htzfe->compressionmethod) {
117         case Z_DEFLATED:
118                 /* fill z_stream structure */
119
120                 zs.next_in   = indata;
121                 zs.avail_in  = htzfe->compressedsize;
122                 zs.next_out  = outdata;
123                 zs.avail_out = htzfe->uncompressedsize;
124
125                 zs.zalloc = Z_NULL;
126                 zs.zfree  = Z_NULL;
127                 zs.opaque = Z_NULL;
128
129                 /* initialize this inflate run */
130
131                 if (inflateInit2(&zs, -MAX_WBITS) != Z_OK)
132                         vm_abort("zip_get: inflateInit2 failed: %s", strerror(errno));
133
134                 /* decompress the file into buffer */
135
136                 err = inflate(&zs, Z_SYNC_FLUSH);
137
138                 if ((err != Z_STREAM_END) && (err != Z_OK))
139                         vm_abort("zip_get: inflate failed: %s", strerror(errno));
140
141                 /* finish this inflate run */
142
143                 if (inflateEnd(&zs) != Z_OK)
144                         vm_abort("zip_get: inflateEnd failed: %s", strerror(errno));
145                 break;
146
147         case 0:
148                 /* uncompressed file, just copy the data */
149                 MCOPY(outdata, indata, u1, htzfe->compressedsize);
150                 break;
151
152         default:
153                 vm_abort("zip_get: unknown compression method %d",
154                                  htzfe->compressionmethod);
155         }
156                 
157         /* Create a file descriptor object */
158         ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
159         fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
160         LLNI_field_set_val(fileDescriptor, pointer, (int)outdata);
161         LLNI_field_set_val(fileDescriptor, length, htzfe->uncompressedsize);
162         LLNI_field_set_val(fileDescriptor, position, 0);
163         return fileDescriptor;
164         
165 }
166
167 static struct com_sun_cldchi_jvm_FileDescriptor* file_read_resource(char *path) 
168 {
169         int len;
170         struct stat statBuffer;
171         u1 *filep;
172         com_sun_cldchi_jvm_FileDescriptor *fileDescriptor = NULL; 
173         classinfo *ci;
174         int fd;
175         
176         fd = open(path, O_RDONLY);
177         
178         if (fd > 0) {
179                 
180                 if (fstat(fd, &statBuffer) != -1) {
181                         len = statBuffer.st_size;
182                 } else {  
183                         return NULL;
184                 }
185                 
186                 /* Map file into the memory */
187                 filep = mmap(0, len, PROT_READ, MAP_PRIVATE, fd, 0);
188                 
189                 /* Create a file descriptor object */
190                 ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
191                 fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
192                 LLNI_field_set_val(fileDescriptor, pointer, (int)filep);
193                 LLNI_field_set_val(fileDescriptor, length, len);
194                 LLNI_field_set_val(fileDescriptor, position, 0);
195                 
196                 return fileDescriptor;  
197                 
198         } else {
199                 return NULL;
200         }
201         
202 }
203
204
205 /*
206  * Class:     com/sun/cldc/io/ResourceInputStream
207  * Method:    open
208  * Signature: (Ljava/lang/String;)Ljava/lang/Object;
209  */
210 JNIEXPORT struct java_lang_Object* JNICALL Java_com_sun_cldc_io_ResourceInputStream_open(JNIEnv *env, jclass clazz, java_lang_String *name)
211 {
212         
213         list_classpath_entry *lce;
214         char *filename;
215         s4 filenamelen;
216         char *path;
217         utf *uname;
218         com_sun_cldchi_jvm_FileDescriptor* descriptor;
219         
220         /* get the classname as char string (do it here for the warning at
221        the end of the function) */
222
223         uname = javastring_toutf((java_handle_t *)name, false);
224         filenamelen = utf_bytes(uname) + strlen("0");
225         filename = MNEW(char, filenamelen);
226         utf_copy(filename, uname);
227         
228         /* walk through all classpath entries */
229
230         for (lce = list_first(list_classpath_entries); lce != NULL;
231                  lce = list_next(list_classpath_entries, lce)) {
232                         
233 #if defined(ENABLE_ZLIB)
234                 if (lce->type == CLASSPATH_ARCHIVE) {
235
236                         /* enter a monitor on zip/jar archives */
237                         LOCK_MONITOR_ENTER(lce);
238
239                         /* try to get the file in current archive */
240                         descriptor = zip_read_resource(lce, uname);
241
242                         /* leave the monitor */
243                         LOCK_MONITOR_EXIT(lce);
244                         
245                         if (descriptor != NULL) { /* file exists */
246                                 break;
247                         }
248
249                 } else {
250 #endif
251                         
252                         path = MNEW(char, lce->pathlen + filenamelen);
253                         strcpy(path, lce->path);
254                         strcat(path, filename);
255
256                         descriptor = file_read_resource(path);
257                         
258                         MFREE(path, char, lce->pathlen + filenamelen);
259
260                         if (descriptor != NULL) { /* file exists */
261                                 break;
262                         }
263                         
264 #if defined(ENABLE_ZLIB)
265                 }
266 #endif  
267                         
268         }
269
270         MFREE(filename, char, filenamelen);
271
272         return (java_lang_Object*) descriptor;
273         
274 }
275
276
277 /*
278  * Class:     com_sun_cldc_io_ResourceInputStream
279  * Method:    bytesRemain
280  * Signature: (Ljava/lang/Object;)I
281  */
282 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_bytesRemain(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
283         
284         com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
285         int32_t length;
286         int32_t position;
287
288         fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
289         LLNI_field_get_val(fileDescriptor, position, position);
290         LLNI_field_get_val(fileDescriptor, length, length);
291         
292         return length - position;
293
294 }
295
296 /*
297  * Class:     com_sun_cldc_io_ResourceInputStream
298  * Method:    readByte
299  * Signature: (Ljava/lang/Object;)I
300  */
301 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_readByte(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
302         
303         com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
304         u1 byte;
305         int32_t length;
306         int32_t position;
307         int64_t filep;
308         
309         fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
310         LLNI_field_get_val(fileDescriptor, position, position);
311         LLNI_field_get_val(fileDescriptor, length, length);
312         LLNI_field_get_val(fileDescriptor, pointer, filep);
313         
314         if (position < length) {
315                 byte = ((u1*)(int)filep)[position];
316                 position++;
317         } else {
318                 return -1; /* EOF */
319         }
320
321         /* Update access position */
322         LLNI_field_set_val(fileDescriptor, position, position);
323         
324         return (byte & 0xFF);
325
326 }
327
328 /*
329  * Class:     com_sun_cldc_io_ResourceInputStream
330  * Method:    readBytes
331  * Signature: (Ljava/lang/Object;[BII)I
332  */
333 JNIEXPORT s4 JNICALL Java_com_sun_cldc_io_ResourceInputStream_readBytes(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj, java_handle_bytearray_t* byteArray, s4 off, s4 len) {
334         
335         com_sun_cldchi_jvm_FileDescriptor *fileDescriptor;
336         s4 readBytes = -1;
337         int32_t fileLength;
338         int32_t position;
339         s4 available;
340         int64_t filep;
341         void *buf;
342
343         /* get pointer to the buffer */
344         buf = &(LLNI_array_direct(byteArray, off));
345         
346         fileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
347         LLNI_field_get_val(fileDescriptor, position, position);
348         LLNI_field_get_val(fileDescriptor, length, fileLength);
349         LLNI_field_get_val(fileDescriptor, pointer, filep);
350         
351         if (position < fileLength) {
352                 available = fileLength - position;
353                 if (available < len) {
354                         readBytes = available;
355                 } else {
356                         readBytes = len;
357                 }
358                 memcpy(buf, ((u1*)(int)filep) + position, readBytes * sizeof(u1));
359                 position += readBytes;
360         } else {
361                 return -1; /* EOF */
362         }
363
364         /* Update access position */
365         LLNI_field_set_val(fileDescriptor, position, position);
366         
367         return readBytes;
368 }
369
370 /*
371  * Class:     com_sun_cldc_io_ResourceInputStream
372  * Method:    clone
373  * Signature: (Ljava/lang/Object;)Ljava/lang/Object;
374  */
375 JNIEXPORT struct java_lang_Object* JNICALL Java_com_sun_cldc_io_ResourceInputStream_clone(JNIEnv *env, jclass clazz, struct java_lang_Object* jobj) {
376         
377         classinfo *ci;
378         com_sun_cldchi_jvm_FileDescriptor *srcFileDescriptor;
379         com_sun_cldchi_jvm_FileDescriptor *dstFileDescriptor;
380         int32_t srcLength;
381         int32_t srcPosition;
382         int64_t srcFilePointer;
383         
384         srcFileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) jobj;
385         LLNI_field_get_val(srcFileDescriptor, position, srcPosition);
386         LLNI_field_get_val(srcFileDescriptor, length, srcLength);
387         LLNI_field_get_val(srcFileDescriptor, pointer, srcFilePointer);
388         
389         ci = load_class_bootstrap(utf_new_char("com/sun/cldchi/jvm/FileDescriptor"));
390         dstFileDescriptor = (com_sun_cldchi_jvm_FileDescriptor *) native_new_and_init(ci);
391         LLNI_field_set_val(dstFileDescriptor, position, srcPosition);
392         LLNI_field_set_val(dstFileDescriptor, length, srcLength);
393         LLNI_field_set_val(dstFileDescriptor, pointer, srcFilePointer);
394         
395         return (java_lang_Object*) dstFileDescriptor;
396
397 }
398
399
400 /*
401  * These are local overrides for various environment variables in Emacs.
402  * Please do not remove this and leave it at the end of the file, where
403  * Emacs will automagically detect them.
404  * ---------------------------------------------------------------------
405  * Local variables:
406  * mode: c
407  * indent-tabs-mode: t
408  * c-basic-offset: 4
409  * tab-width: 4
410  * End:
411  * vim:noexpandtab:sw=4:ts=4:
412  */