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