1 /*
   2  * Copyright 1998-2006 Sun Microsystems, Inc.  All Rights Reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 /*
  27  * Native method support for java.util.zip.ZipFile
  28  */
  29 
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <string.h>
  33 #include <errno.h>
  34 #include <ctype.h>
  35 #include <assert.h>
  36 #include "jlong.h"
  37 #include "jvm.h"
  38 #include "jni.h"
  39 #include "jni_util.h"
  40 #include "zip_util.h"
  41 #ifdef WIN32
  42 #include "io_util_md.h"
  43 #else
  44 #include "io_util.h"
  45 #endif
  46 
  47 #include "java_util_zip_ZipFile.h"
  48 #include "java_util_jar_JarFile.h"
  49 
  50 #define DEFLATED 8
  51 #define STORED 0
  52 
  53 static jfieldID jzfileID;
  54 
  55 static int OPEN_READ = java_util_zip_ZipFile_OPEN_READ;
  56 static int OPEN_DELETE = java_util_zip_ZipFile_OPEN_DELETE;
  57 
  58 JNIEXPORT void JNICALL
  59 Java_java_util_zip_ZipFile_initIDs(JNIEnv *env, jclass cls)
  60 {
  61     jzfileID = (*env)->GetFieldID(env, cls, "jzfile", "J");
  62     assert(jzfileID != 0);
  63 }
  64 
  65 static void
  66 ThrowZipException(JNIEnv *env, const char *msg)
  67 {
  68     jstring s = NULL;
  69     jobject x;
  70 
  71     if (msg != NULL) {
  72         s = JNU_NewStringPlatform(env, msg);
  73     }
  74     x = JNU_NewObjectByName(env,
  75                             "java/util/zip/ZipException",
  76                             "(Ljava/lang/String;)V", s);
  77     if (x != NULL) {
  78         (*env)->Throw(env, x);
  79     }
  80 }
  81 
  82 JNIEXPORT jlong JNICALL
  83 Java_java_util_zip_ZipFile_open(JNIEnv *env, jclass cls, jstring name,
  84                                         jint mode, jlong lastModified)
  85 {
  86     const char *path = JNU_GetStringPlatformChars(env, name, 0);
  87     char *msg = 0;
  88     jlong result = 0;
  89     int flag = 0;
  90     jzfile *zip = 0;
  91 
  92     if (mode & OPEN_READ) flag |= O_RDONLY;
  93     if (mode & OPEN_DELETE) flag |= JVM_O_DELETE;
  94 
  95     if (path != 0) {
  96         zip = ZIP_Get_From_Cache(path, &msg, lastModified);
  97         if (zip == 0 && msg == 0) {
  98             ZFILE zfd = 0;
  99 #ifdef WIN32
 100             zfd = winFileHandleOpen(env, name, flag);
 101             if (zfd == -1) {
 102                 /* Exception already pending. */
 103                 goto finally;
 104             }
 105 #else
 106             zfd = JVM_Open(path, flag, 0);
 107             if (zfd < 0) {
 108                 throwFileNotFoundException(env, name);
 109                 goto finally;
 110             }
 111 #endif
 112             zip = ZIP_Put_In_Cache(path, zfd, &msg, lastModified);
 113         }
 114 
 115         if (zip != 0) {
 116             result = ptr_to_jlong(zip);
 117         } else if (msg != 0) {
 118             ThrowZipException(env, msg);
 119         } else if (errno == ENOMEM) {
 120             JNU_ThrowOutOfMemoryError(env, 0);
 121         } else {
 122             ThrowZipException(env, "error in opening zip file");
 123         }
 124 finally:
 125         JNU_ReleaseStringPlatformChars(env, name, path);
 126     }
 127     return result;
 128 }
 129 
 130 JNIEXPORT jint JNICALL
 131 Java_java_util_zip_ZipFile_getTotal(JNIEnv *env, jclass cls, jlong zfile)
 132 {
 133     jzfile *zip = jlong_to_ptr(zfile);
 134 
 135     return zip->total;
 136 }
 137 
 138 JNIEXPORT void JNICALL
 139 Java_java_util_zip_ZipFile_close(JNIEnv *env, jclass cls, jlong zfile)
 140 {
 141     ZIP_Close(jlong_to_ptr(zfile));
 142 }
 143 
 144 JNIEXPORT jlong JNICALL
 145 Java_java_util_zip_ZipFile_getEntry(JNIEnv *env, jclass cls, jlong zfile,
 146                                     jbyteArray name, jboolean addSlash)
 147 {
 148 #define MAXNAME 1024
 149     jzfile *zip = jlong_to_ptr(zfile);
 150     jsize ulen = (*env)->GetArrayLength(env, name);
 151     char buf[MAXNAME+2], *path;
 152     jzentry *ze;
 153 
 154     if (ulen > MAXNAME) {
 155         path = malloc(ulen + 2);
 156         if (path == 0) {
 157             JNU_ThrowOutOfMemoryError(env, 0);
 158             return 0;
 159         }
 160     } else {
 161         path = buf;
 162     }
 163     (*env)->GetByteArrayRegion(env, name, 0, ulen, (jbyte *)path);
 164     path[ulen] = '\0';
 165     if (addSlash == JNI_FALSE) {
 166         ze = ZIP_GetEntry(zip, path, 0);
 167     } else {
 168         ze = ZIP_GetEntry(zip, path, (jint)ulen);
 169     }
 170     if (path != buf) {
 171         free(path);
 172     }
 173     return ptr_to_jlong(ze);
 174 }
 175 
 176 JNIEXPORT void JNICALL
 177 Java_java_util_zip_ZipFile_freeEntry(JNIEnv *env, jclass cls, jlong zfile,
 178                                     jlong zentry)
 179 {
 180     jzfile *zip = jlong_to_ptr(zfile);
 181     jzentry *ze = jlong_to_ptr(zentry);
 182     ZIP_FreeEntry(zip, ze);
 183 }
 184 
 185 JNIEXPORT jlong JNICALL
 186 Java_java_util_zip_ZipFile_getNextEntry(JNIEnv *env, jclass cls, jlong zfile,
 187                                         jint n)
 188 {
 189     jzentry *ze = ZIP_GetNextEntry(jlong_to_ptr(zfile), n);
 190     return ptr_to_jlong(ze);
 191 }
 192 
 193 JNIEXPORT jint JNICALL
 194 Java_java_util_zip_ZipFile_getEntryMethod(JNIEnv *env, jclass cls, jlong zentry)
 195 {
 196     jzentry *ze = jlong_to_ptr(zentry);
 197     return ze->csize != 0 ? DEFLATED : STORED;
 198 }
 199 
 200 JNIEXPORT jint JNICALL
 201 Java_java_util_zip_ZipFile_getEntryFlag(JNIEnv *env, jclass cls, jlong zentry)
 202 {
 203     jzentry *ze = jlong_to_ptr(zentry);
 204     return ze->flag;
 205 }
 206 
 207 JNIEXPORT jlong JNICALL
 208 Java_java_util_zip_ZipFile_getEntryCSize(JNIEnv *env, jclass cls, jlong zentry)
 209 {
 210     jzentry *ze = jlong_to_ptr(zentry);
 211     return ze->csize != 0 ? ze->csize : ze->size;
 212 }
 213 
 214 JNIEXPORT jlong JNICALL
 215 Java_java_util_zip_ZipFile_getEntrySize(JNIEnv *env, jclass cls, jlong zentry)
 216 {
 217     jzentry *ze = jlong_to_ptr(zentry);
 218     return ze->size;
 219 }
 220 
 221 JNIEXPORT jlong JNICALL
 222 Java_java_util_zip_ZipFile_getEntryTime(JNIEnv *env, jclass cls, jlong zentry)
 223 {
 224     jzentry *ze = jlong_to_ptr(zentry);
 225     return (jlong)ze->time & 0xffffffffUL;
 226 }
 227 
 228 JNIEXPORT jlong JNICALL
 229 Java_java_util_zip_ZipFile_getEntryCrc(JNIEnv *env, jclass cls, jlong zentry)
 230 {
 231     jzentry *ze = jlong_to_ptr(zentry);
 232     return (jlong)ze->crc & 0xffffffffUL;
 233 }
 234 
 235 JNIEXPORT jbyteArray JNICALL
 236 Java_java_util_zip_ZipFile_getCommentBytes(JNIEnv *env,
 237                                            jclass cls,
 238                                            jlong zfile)
 239 {
 240     jzfile *zip = jlong_to_ptr(zfile);
 241     jbyteArray jba = NULL;
 242 
 243     if (zip->comment != NULL) {
 244         if ((jba = (*env)->NewByteArray(env, zip->clen)) == NULL)
 245             return NULL;
 246         (*env)->SetByteArrayRegion(env, jba, 0, zip->clen, (jbyte*)zip->comment);
 247     }
 248     return jba;
 249 }
 250 
 251 JNIEXPORT jbyteArray JNICALL
 252 Java_java_util_zip_ZipFile_getEntryBytes(JNIEnv *env,
 253                                          jclass cls,
 254                                          jlong zentry, jint type)
 255 {
 256     jzentry *ze = jlong_to_ptr(zentry);
 257     int len = 0;
 258     jbyteArray jba = NULL;
 259     switch (type) {
 260     case java_util_zip_ZipFile_JZENTRY_NAME:
 261         if (ze->name != 0) {
 262             len = (int)strlen(ze->name);
 263             if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
 264                 break;
 265             (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte *)ze->name);
 266         }
 267         break;
 268     case java_util_zip_ZipFile_JZENTRY_EXTRA:
 269         if (ze->extra != 0) {
 270             unsigned char *bp = (unsigned char *)&ze->extra[0];
 271             len = (bp[0] | (bp[1] << 8));
 272             if (len <= 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
 273                 break;
 274             (*env)->SetByteArrayRegion(env, jba, 0, len, &ze->extra[2]);
 275         }
 276         break;
 277     case java_util_zip_ZipFile_JZENTRY_COMMENT:
 278         if (ze->comment != 0) {
 279             len = (int)strlen(ze->comment);
 280             if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
 281                 break;
 282             (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte*)ze->comment);
 283         }
 284         break;
 285     }
 286     return jba;
 287 }
 288 
 289 JNIEXPORT jint JNICALL
 290 Java_java_util_zip_ZipFile_read(JNIEnv *env, jclass cls, jlong zfile,
 291                                 jlong zentry, jlong pos, jbyteArray bytes,
 292                                 jint off, jint len)
 293 {
 294     jzfile *zip = jlong_to_ptr(zfile);
 295     char *msg;
 296 
 297 #define BUFSIZE 8192
 298     /* copy via tmp stack buffer: */
 299     jbyte buf[BUFSIZE];
 300 
 301     if (len > BUFSIZE) {
 302         len = BUFSIZE;
 303     }
 304 
 305     ZIP_Lock(zip);
 306     len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf, len);
 307     msg = zip->msg;
 308     ZIP_Unlock(zip);
 309     if (len != -1) {
 310         (*env)->SetByteArrayRegion(env, bytes, off, len, buf);
 311     }
 312 
 313     if (len == -1) {
 314         if (msg != 0) {
 315             ThrowZipException(env, msg);
 316         } else {
 317             char errmsg[128];
 318             sprintf(errmsg, "errno: %d, error: %s\n",
 319                     errno, "Error reading ZIP file");
 320             JNU_ThrowIOExceptionWithLastError(env, errmsg);
 321         }
 322     }
 323 
 324     return len;
 325 }
 326 
 327 /*
 328  * Returns an array of strings representing the names of all entries
 329  * that begin with "META-INF/" (case ignored). This native method is
 330  * used in JarFile as an optimization when looking up manifest and
 331  * signature file entries. Returns null if no entries were found.
 332  */
 333 JNIEXPORT jobjectArray JNICALL
 334 Java_java_util_jar_JarFile_getMetaInfEntryNames(JNIEnv *env, jobject obj)
 335 {
 336     jlong zfile = (*env)->GetLongField(env, obj, jzfileID);
 337     jzfile *zip;
 338     int i, count;
 339     jobjectArray result = 0;
 340 
 341     if (zfile == 0) {
 342         JNU_ThrowByName(env,
 343                         "java/lang/IllegalStateException", "zip file closed");
 344         return NULL;
 345     }
 346     zip = jlong_to_ptr(zfile);
 347 
 348     /* count the number of valid ZIP metanames */
 349     count = 0;
 350     if (zip->metanames != 0) {
 351         for (i = 0; i < zip->metacount; i++) {
 352             if (zip->metanames[i] != 0) {
 353                 count++;
 354             }
 355         }
 356     }
 357 
 358     /* If some names were found then build array of java strings */
 359     if (count > 0) {
 360         jclass cls = (*env)->FindClass(env, "java/lang/String");
 361         result = (*env)->NewObjectArray(env, count, cls, 0);
 362         if (result != 0) {
 363             for (i = 0; i < count; i++) {
 364                 jstring str = (*env)->NewStringUTF(env, zip->metanames[i]);
 365                 if (str == 0) {
 366                     break;
 367                 }
 368                 (*env)->SetObjectArrayElement(env, result, i, str);
 369                 (*env)->DeleteLocalRef(env, str);
 370             }
 371         }
 372     }
 373     return result;
 374 }
 375 
 376 JNIEXPORT jstring JNICALL
 377 Java_java_util_zip_ZipFile_getZipMessage(JNIEnv *env, jclass cls, jlong zfile)
 378 {
 379     jzfile *zip = jlong_to_ptr(zfile);
 380     char *msg = zip->msg;
 381     if (msg == NULL) {
 382         return NULL;
 383     }
 384     return JNU_NewStringPlatform(env, msg);
 385 }