1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * 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     if (s != NULL) {
  75         x = JNU_NewObjectByName(env,
  76                             "java/util/zip/ZipException",
  77                             "(Ljava/lang/String;)V", s);
  78         if (x != NULL) {
  79             (*env)->Throw(env, x);
  80         }
  81     }
  82 }
  83 
  84 JNIEXPORT jlong JNICALL
  85 Java_java_util_zip_ZipFile_open(JNIEnv *env, jclass cls, jstring name,
  86                                         jint mode, jlong lastModified,
  87                                         jboolean usemmap)
  88 {
  89     const char *path = JNU_GetStringPlatformChars(env, name, 0);
  90     char *msg = 0;
  91     jlong result = 0;
  92     int flag = 0;
  93     jzfile *zip = 0;
  94 
  95     if (mode & OPEN_READ) flag |= O_RDONLY;
  96     if (mode & OPEN_DELETE) flag |= JVM_O_DELETE;
  97 
  98     if (path != 0) {
  99         zip = ZIP_Get_From_Cache(path, &msg, lastModified);
 100         if (zip == 0 && msg == 0) {
 101             ZFILE zfd = 0;
 102 #ifdef WIN32
 103             zfd = winFileHandleOpen(env, name, flag);
 104             if (zfd == -1) {
 105                 /* Exception already pending. */
 106                 goto finally;
 107             }
 108 #else
 109             zfd = JVM_Open(path, flag, 0);
 110             if (zfd < 0) {
 111                 throwFileNotFoundException(env, name);
 112                 goto finally;
 113             }
 114 #endif
 115             zip = ZIP_Put_In_Cache0(path, zfd, &msg, lastModified, usemmap);
 116         }
 117 
 118         if (zip != 0) {
 119             result = ptr_to_jlong(zip);
 120         } else if (msg != 0) {
 121             ThrowZipException(env, msg);
 122             free(msg);
 123         } else if (errno == ENOMEM) {
 124             JNU_ThrowOutOfMemoryError(env, 0);
 125         } else {
 126             ThrowZipException(env, "error in opening zip file");
 127         }
 128 finally:
 129         JNU_ReleaseStringPlatformChars(env, name, path);
 130     }
 131     return result;
 132 }
 133 
 134 JNIEXPORT jint JNICALL
 135 Java_java_util_zip_ZipFile_getTotal(JNIEnv *env, jclass cls, jlong zfile)
 136 {
 137     jzfile *zip = jlong_to_ptr(zfile);
 138 
 139     return zip->total;
 140 }
 141 
 142 JNIEXPORT jboolean JNICALL
 143 Java_java_util_zip_ZipFile_startsWithLOC(JNIEnv *env, jclass cls, jlong zfile)
 144 {
 145     jzfile *zip = jlong_to_ptr(zfile);
 146 
 147     return zip->locsig;
 148 }
 149 
 150 JNIEXPORT void JNICALL
 151 Java_java_util_zip_ZipFile_close(JNIEnv *env, jclass cls, jlong zfile)
 152 {
 153     ZIP_Close(jlong_to_ptr(zfile));
 154 }
 155 
 156 JNIEXPORT jlong JNICALL
 157 Java_java_util_zip_ZipFile_getEntry(JNIEnv *env, jclass cls, jlong zfile,
 158                                     jbyteArray name, jboolean addSlash)
 159 {
 160 #define MAXNAME 1024
 161     jzfile *zip = jlong_to_ptr(zfile);
 162     jsize ulen = (*env)->GetArrayLength(env, name);
 163     char buf[MAXNAME+2], *path;
 164     jzentry *ze;
 165 
 166     if (ulen > MAXNAME) {
 167         path = malloc(ulen + 2);
 168         if (path == 0) {
 169             JNU_ThrowOutOfMemoryError(env, 0);
 170             return 0;
 171         }
 172     } else {
 173         path = buf;
 174     }
 175     (*env)->GetByteArrayRegion(env, name, 0, ulen, (jbyte *)path);
 176     path[ulen] = '\0';
 177     if (addSlash == JNI_FALSE) {
 178         ze = ZIP_GetEntry(zip, path, 0);
 179     } else {
 180         ze = ZIP_GetEntry(zip, path, (jint)ulen);
 181     }
 182     if (path != buf) {
 183         free(path);
 184     }
 185     return ptr_to_jlong(ze);
 186 }
 187 
 188 JNIEXPORT void JNICALL
 189 Java_java_util_zip_ZipFile_freeEntry(JNIEnv *env, jclass cls, jlong zfile,
 190                                     jlong zentry)
 191 {
 192     jzfile *zip = jlong_to_ptr(zfile);
 193     jzentry *ze = jlong_to_ptr(zentry);
 194     ZIP_FreeEntry(zip, ze);
 195 }
 196 
 197 JNIEXPORT jlong JNICALL
 198 Java_java_util_zip_ZipFile_getNextEntry(JNIEnv *env, jclass cls, jlong zfile,
 199                                         jint n)
 200 {
 201     jzentry *ze = ZIP_GetNextEntry(jlong_to_ptr(zfile), n);
 202     return ptr_to_jlong(ze);
 203 }
 204 
 205 JNIEXPORT jint JNICALL
 206 Java_java_util_zip_ZipFile_getEntryMethod(JNIEnv *env, jclass cls, jlong zentry)
 207 {
 208     jzentry *ze = jlong_to_ptr(zentry);
 209     return ze->csize != 0 ? DEFLATED : STORED;
 210 }
 211 
 212 JNIEXPORT jint JNICALL
 213 Java_java_util_zip_ZipFile_getEntryFlag(JNIEnv *env, jclass cls, jlong zentry)
 214 {
 215     jzentry *ze = jlong_to_ptr(zentry);
 216     return ze->flag;
 217 }
 218 
 219 JNIEXPORT jlong JNICALL
 220 Java_java_util_zip_ZipFile_getEntryCSize(JNIEnv *env, jclass cls, jlong zentry)
 221 {
 222     jzentry *ze = jlong_to_ptr(zentry);
 223     return ze->csize != 0 ? ze->csize : ze->size;
 224 }
 225 
 226 JNIEXPORT jlong JNICALL
 227 Java_java_util_zip_ZipFile_getEntrySize(JNIEnv *env, jclass cls, jlong zentry)
 228 {
 229     jzentry *ze = jlong_to_ptr(zentry);
 230     return ze->size;
 231 }
 232 
 233 JNIEXPORT jlong JNICALL
 234 Java_java_util_zip_ZipFile_getEntryTime(JNIEnv *env, jclass cls, jlong zentry)
 235 {
 236     jzentry *ze = jlong_to_ptr(zentry);
 237     return (jlong)ze->time & 0xffffffffUL;
 238 }
 239 
 240 JNIEXPORT jlong JNICALL
 241 Java_java_util_zip_ZipFile_getEntryCrc(JNIEnv *env, jclass cls, jlong zentry)
 242 {
 243     jzentry *ze = jlong_to_ptr(zentry);
 244     return (jlong)ze->crc & 0xffffffffUL;
 245 }
 246 
 247 JNIEXPORT jbyteArray JNICALL
 248 Java_java_util_zip_ZipFile_getCommentBytes(JNIEnv *env,
 249                                            jclass cls,
 250                                            jlong zfile)
 251 {
 252     jzfile *zip = jlong_to_ptr(zfile);
 253     jbyteArray jba = NULL;
 254 
 255     if (zip->comment != NULL) {
 256         if ((jba = (*env)->NewByteArray(env, zip->clen)) == NULL)
 257             return NULL;
 258         (*env)->SetByteArrayRegion(env, jba, 0, zip->clen, (jbyte*)zip->comment);
 259     }
 260     return jba;
 261 }
 262 
 263 JNIEXPORT jbyteArray JNICALL
 264 Java_java_util_zip_ZipFile_getEntryBytes(JNIEnv *env,
 265                                          jclass cls,
 266                                          jlong zentry, jint type)
 267 {
 268     jzentry *ze = jlong_to_ptr(zentry);
 269     int len = 0;
 270     jbyteArray jba = NULL;
 271     switch (type) {
 272     case java_util_zip_ZipFile_JZENTRY_NAME:
 273         if (ze->name != 0) {
 274             len = (int)strlen(ze->name);
 275             if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
 276                 break;
 277             (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte *)ze->name);
 278         }
 279         break;
 280     case java_util_zip_ZipFile_JZENTRY_EXTRA:
 281         if (ze->extra != 0) {
 282             unsigned char *bp = (unsigned char *)&ze->extra[0];
 283             len = (bp[0] | (bp[1] << 8));
 284             if (len <= 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
 285                 break;
 286             (*env)->SetByteArrayRegion(env, jba, 0, len, &ze->extra[2]);
 287         }
 288         break;
 289     case java_util_zip_ZipFile_JZENTRY_COMMENT:
 290         if (ze->comment != 0) {
 291             len = (int)strlen(ze->comment);
 292             if (len == 0 || (jba = (*env)->NewByteArray(env, len)) == NULL)
 293                 break;
 294             (*env)->SetByteArrayRegion(env, jba, 0, len, (jbyte*)ze->comment);
 295         }
 296         break;
 297     }
 298     return jba;
 299 }
 300 
 301 JNIEXPORT jint JNICALL
 302 Java_java_util_zip_ZipFile_read(JNIEnv *env, jclass cls, jlong zfile,
 303                                 jlong zentry, jlong pos, jbyteArray bytes,
 304                                 jint off, jint len)
 305 {
 306     jzfile *zip = jlong_to_ptr(zfile);
 307     char *msg;
 308 
 309 #define BUFSIZE 8192
 310     /* copy via tmp stack buffer: */
 311     jbyte buf[BUFSIZE];
 312 
 313     if (len > BUFSIZE) {
 314         len = BUFSIZE;
 315     }
 316 
 317     ZIP_Lock(zip);
 318     len = ZIP_Read(zip, jlong_to_ptr(zentry), pos, buf, len);
 319     msg = zip->msg;
 320     ZIP_Unlock(zip);
 321     if (len != -1) {
 322         (*env)->SetByteArrayRegion(env, bytes, off, len, buf);
 323     }
 324 
 325     if (len == -1) {
 326         if (msg != 0) {
 327             ThrowZipException(env, msg);
 328         } else {
 329             char errmsg[128];
 330             sprintf(errmsg, "errno: %d, error: %s\n",
 331                     errno, "Error reading ZIP file");
 332             JNU_ThrowIOExceptionWithLastError(env, errmsg);
 333         }
 334     }
 335 
 336     return len;
 337 }
 338 
 339 /*
 340  * Returns an array of strings representing the names of all entries
 341  * that begin with "META-INF/" (case ignored). This native method is
 342  * used in JarFile as an optimization when looking up manifest and
 343  * signature file entries. Returns null if no entries were found.
 344  */
 345 JNIEXPORT jobjectArray JNICALL
 346 Java_java_util_jar_JarFile_getMetaInfEntryNames(JNIEnv *env, jobject obj)
 347 {
 348     jlong zfile = (*env)->GetLongField(env, obj, jzfileID);
 349     jzfile *zip;
 350     int i, count;
 351     jobjectArray result = 0;
 352 
 353     if (zfile == 0) {
 354         JNU_ThrowByName(env,
 355                         "java/lang/IllegalStateException", "zip file closed");
 356         return NULL;
 357     }
 358     zip = jlong_to_ptr(zfile);
 359 
 360     /* count the number of valid ZIP metanames */
 361     count = 0;
 362     if (zip->metanames != 0) {
 363         for (i = 0; i < zip->metacount; i++) {
 364             if (zip->metanames[i] != 0) {
 365                 count++;
 366             }
 367         }
 368     }
 369 
 370     /* If some names were found then build array of java strings */
 371     if (count > 0) {
 372         jclass cls = (*env)->FindClass(env, "java/lang/String");
 373         CHECK_NULL_RETURN(cls, NULL);
 374         result = (*env)->NewObjectArray(env, count, cls, 0);
 375         CHECK_NULL_RETURN(result, NULL);
 376         if (result != 0) {
 377             for (i = 0; i < count; i++) {
 378                 jstring str = (*env)->NewStringUTF(env, zip->metanames[i]);
 379                 if (str == 0) {
 380                     break;
 381                 }
 382                 (*env)->SetObjectArrayElement(env, result, i, str);
 383                 (*env)->DeleteLocalRef(env, str);
 384             }
 385         }
 386     }
 387     return result;
 388 }
 389 
 390 JNIEXPORT jstring JNICALL
 391 Java_java_util_zip_ZipFile_getZipMessage(JNIEnv *env, jclass cls, jlong zfile)
 392 {
 393     jzfile *zip = jlong_to_ptr(zfile);
 394     char *msg = zip->msg;
 395     if (msg == NULL) {
 396         return NULL;
 397     }
 398     return JNU_NewStringPlatform(env, msg);
 399 }