src/java.base/share/native/libzip/zip_util.c

Print this page




  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  * Support for reading ZIP/JAR files.
  28  */
  29 
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <stddef.h>
  33 #include <string.h>
  34 #include <fcntl.h>
  35 #include <limits.h>
  36 #include <time.h>
  37 #include <ctype.h>
  38 #include <assert.h>
  39 

  40 #include "jni.h"
  41 #include "jni_util.h"
  42 #include "jlong.h"
  43 #include "jvm.h"
  44 #include "io_util.h"
  45 #include "io_util_md.h"
  46 #include "zip_util.h"
  47 #include <zlib.h>
  48 
  49 #ifdef _ALLBSD_SOURCE
  50 #define off64_t off_t
  51 #define mmap64 mmap
  52 #endif
  53 
  54 /* USE_MMAP means mmap the CEN & ENDHDR part of the zip file. */
  55 #ifdef USE_MMAP
  56 #include <sys/mman.h>
  57 #endif
  58 
  59 #define MAXREFS 0xFFFF  /* max number of open zip file references */


1421 ZIP_FindEntry(jzfile *zip, char *name, jint *sizeP, jint *nameLenP)
1422 {
1423     jzentry *entry = ZIP_GetEntry(zip, name, 0);
1424     if (entry) {
1425         *sizeP = (jint)entry->size;
1426         *nameLenP = strlen(entry->name);
1427     }
1428     return entry;
1429 }
1430 
1431 /*
1432  * Reads a zip file entry into the specified byte array
1433  * When the method completes, it releases the jzentry.
1434  * Note: this is called from the separately delivered VM (hotspot/classic)
1435  * so we have to be careful to maintain the expected behaviour.
1436  */
1437 jboolean JNICALL
1438 ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entryname)
1439 {
1440     char *msg;

1441 
1442     strcpy(entryname, entry->name);
1443     if (entry->csize == 0) {
1444         /* Entry is stored */
1445         jlong pos = 0;
1446         jlong size = entry->size;
1447         while (pos < size) {
1448             jint n;
1449             jlong limit = ((((jlong) 1) << 31) - 1);
1450             jint count = (size - pos < limit) ?
1451                 /* These casts suppress a VC++ Internal Compiler Error */
1452                 (jint) (size - pos) :
1453                 (jint) limit;
1454             ZIP_Lock(zip);
1455             n = ZIP_Read(zip, entry, pos, buf, count);
1456             msg = zip->msg;
1457             ZIP_Unlock(zip);
1458             if (n == -1) {
1459                 jio_fprintf(stderr, "%s: %s\n", zip->name,
1460                             msg != 0 ? msg : strerror(errno));



1461                 return JNI_FALSE;
1462             }
1463             buf += n;
1464             pos += n;
1465         }
1466     } else {
1467         /* Entry is compressed */
1468         int ok = InflateFully(zip, entry, buf, &msg);
1469         if (!ok) {
1470             if ((msg == NULL) || (*msg == 0)) {
1471                 msg = zip->msg;
1472             }
1473             jio_fprintf(stderr, "%s: %s\n", zip->name,
1474                         msg != 0 ? msg : strerror(errno));



1475             return JNI_FALSE;
1476         }
1477     }
1478 
1479     ZIP_FreeEntry(zip, entry);
1480 
1481     return JNI_TRUE;
1482 }
1483 
1484 jboolean JNICALL
1485 ZIP_InflateFully(void *inBuf, jlong inLen, void *outBuf, jlong outLen, char **pmsg)
1486 {
1487     z_stream strm;
1488     int i = 0;
1489     memset(&strm, 0, sizeof(z_stream));
1490 
1491     *pmsg = 0; /* Reset error message */
1492 
1493     if (inflateInit2(&strm, MAX_WBITS) != Z_OK) {
1494         *pmsg = strm.msg;




  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  * Support for reading ZIP/JAR files.
  28  */
  29 
  30 #include <stdio.h>
  31 #include <stdlib.h>
  32 #include <stddef.h>
  33 #include <string.h>
  34 #include <fcntl.h>
  35 #include <limits.h>
  36 #include <time.h>
  37 #include <ctype.h>
  38 #include <assert.h>
  39 
  40 #include "jdk_strerror.h"
  41 #include "jni.h"
  42 #include "jni_util.h"
  43 #include "jlong.h"
  44 #include "jvm.h"
  45 #include "io_util.h"
  46 #include "io_util_md.h"
  47 #include "zip_util.h"
  48 #include <zlib.h>
  49 
  50 #ifdef _ALLBSD_SOURCE
  51 #define off64_t off_t
  52 #define mmap64 mmap
  53 #endif
  54 
  55 /* USE_MMAP means mmap the CEN & ENDHDR part of the zip file. */
  56 #ifdef USE_MMAP
  57 #include <sys/mman.h>
  58 #endif
  59 
  60 #define MAXREFS 0xFFFF  /* max number of open zip file references */


1422 ZIP_FindEntry(jzfile *zip, char *name, jint *sizeP, jint *nameLenP)
1423 {
1424     jzentry *entry = ZIP_GetEntry(zip, name, 0);
1425     if (entry) {
1426         *sizeP = (jint)entry->size;
1427         *nameLenP = strlen(entry->name);
1428     }
1429     return entry;
1430 }
1431 
1432 /*
1433  * Reads a zip file entry into the specified byte array
1434  * When the method completes, it releases the jzentry.
1435  * Note: this is called from the separately delivered VM (hotspot/classic)
1436  * so we have to be careful to maintain the expected behaviour.
1437  */
1438 jboolean JNICALL
1439 ZIP_ReadEntry(jzfile *zip, jzentry *entry, unsigned char *buf, char *entryname)
1440 {
1441     char *msg;
1442     char tmpbuf[1024];
1443 
1444     strcpy(entryname, entry->name);
1445     if (entry->csize == 0) {
1446         /* Entry is stored */
1447         jlong pos = 0;
1448         jlong size = entry->size;
1449         while (pos < size) {
1450             jint n;
1451             jlong limit = ((((jlong) 1) << 31) - 1);
1452             jint count = (size - pos < limit) ?
1453                 /* These casts suppress a VC++ Internal Compiler Error */
1454                 (jint) (size - pos) :
1455                 (jint) limit;
1456             ZIP_Lock(zip);
1457             n = ZIP_Read(zip, entry, pos, buf, count);
1458             msg = zip->msg;
1459             ZIP_Unlock(zip);
1460             if (n == -1) {
1461                 if (msg == 0) {
1462                     jdk_strerror(errno, tmpbuf, (size_t) 1024);
1463                     msg = tmpbuf;
1464                 }
1465                 jio_fprintf(stderr, "%s: %s\n", zip->name, msg);
1466                 return JNI_FALSE;
1467             }
1468             buf += n;
1469             pos += n;
1470         }
1471     } else {
1472         /* Entry is compressed */
1473         int ok = InflateFully(zip, entry, buf, &msg);
1474         if (!ok) {
1475             if ((msg == NULL) || (*msg == 0)) {
1476                 msg = zip->msg;
1477             }
1478             if (msg == 0) {
1479                 jdk_strerror(errno, tmpbuf, (size_t) 1024);
1480                 msg = tmpbuf;
1481             }
1482             jio_fprintf(stderr, "%s: %s\n", zip->name, msg);
1483             return JNI_FALSE;
1484         }
1485     }
1486 
1487     ZIP_FreeEntry(zip, entry);
1488 
1489     return JNI_TRUE;
1490 }
1491 
1492 jboolean JNICALL
1493 ZIP_InflateFully(void *inBuf, jlong inLen, void *outBuf, jlong outLen, char **pmsg)
1494 {
1495     z_stream strm;
1496     int i = 0;
1497     memset(&strm, 0, sizeof(z_stream));
1498 
1499     *pmsg = 0; /* Reset error message */
1500 
1501     if (inflateInit2(&strm, MAX_WBITS) != Z_OK) {
1502         *pmsg = strm.msg;