< prev index next >

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

Print this page
rev 58388 : 8237354: Add option to jcmd to write a gzipped heap dump
Reviewed-by:


1579                 }
1580                 break;
1581             case Z_DATA_ERROR:
1582                 *pmsg = "INFLATER_inflateFully: Compressed data corrupted";
1583                 inflateEnd(&strm);
1584                 return JNI_FALSE;
1585             case Z_MEM_ERROR:
1586                 *pmsg = "INFLATER_inflateFully: out of memory";
1587                 inflateEnd(&strm);
1588                 return JNI_FALSE;
1589             default:
1590                 *pmsg = "INFLATER_inflateFully: internal error";
1591                 inflateEnd(&strm);
1592                 return JNI_FALSE;
1593         }
1594     } while (strm.avail_in > 0);
1595 
1596     inflateEnd(&strm);
1597     return JNI_TRUE;
1598 }













































































































1579                 }
1580                 break;
1581             case Z_DATA_ERROR:
1582                 *pmsg = "INFLATER_inflateFully: Compressed data corrupted";
1583                 inflateEnd(&strm);
1584                 return JNI_FALSE;
1585             case Z_MEM_ERROR:
1586                 *pmsg = "INFLATER_inflateFully: out of memory";
1587                 inflateEnd(&strm);
1588                 return JNI_FALSE;
1589             default:
1590                 *pmsg = "INFLATER_inflateFully: internal error";
1591                 inflateEnd(&strm);
1592                 return JNI_FALSE;
1593         }
1594     } while (strm.avail_in > 0);
1595 
1596     inflateEnd(&strm);
1597     return JNI_TRUE;
1598 }
1599 
1600 static voidpf tracking_zlib_alloc(voidpf opaque, uInt items, uInt size) {
1601   size_t* needed = (size_t*) opaque;
1602   *needed += (size_t) items * (size_t) size;
1603   return (voidpf) calloc((size_t) items, (size_t) size);
1604 }
1605 
1606 static void tracking_zlib_free(voidpf opaque, voidpf address) {
1607   free((void*) address);
1608 }
1609 
1610 static voidpf zlib_block_alloc(voidpf opaque, uInt items, uInt size) {
1611   char** range = (char**) opaque;
1612   voidpf result = NULL;
1613   size_t needed = (size_t) items * (size_t) size;
1614 
1615   if (range[1] - range[0] >= (ptrdiff_t) needed) {
1616     result = (voidpf) range[0];
1617     range[0] += needed;
1618   }
1619 
1620   return result;
1621 }
1622 
1623 static void zlib_block_free(voidpf opaque, voidpf address) {
1624   /* Nothing to do. */
1625 }
1626 
1627 static char const* deflateInit2Wrapper(z_stream* strm, int level) {
1628   int err = deflateInit2(strm, level >= 0 && level <= 9 ? level : Z_DEFAULT_COMPRESSION,
1629                          Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
1630   if (err == Z_MEM_ERROR) {
1631     return "Out of memory in deflateInit2";
1632   }
1633 
1634   if (err != Z_OK) {
1635     return "Internal error in deflateInit2";
1636   }
1637 
1638   return NULL;
1639 }
1640 
1641 JNIEXPORT char const*
1642 ZIP_GZip_InitParams(size_t inLen, size_t* outLen, size_t* tmpLen, int level) {
1643   z_stream strm;
1644   *tmpLen = 0;
1645   char const* errorMsg;
1646 
1647   memset(&strm, 0, sizeof(z_stream));
1648   strm.zalloc = tracking_zlib_alloc;
1649   strm.zfree = tracking_zlib_free;
1650   strm.opaque = (voidpf) tmpLen;
1651 
1652   errorMsg = deflateInit2Wrapper(&strm, level);
1653 
1654   if (errorMsg == NULL) {
1655     *outLen = (size_t) deflateBound(&strm, (uLong) inLen);
1656   }
1657 
1658   deflateEnd(&strm);
1659 
1660   return errorMsg;
1661 }
1662 
1663 JNIEXPORT size_t
1664 ZIP_GZip_Fully(char* inBuf, size_t inLen, char* outBuf, size_t outLen, char* tmp, size_t tmpLen,
1665                int level, char* comment, char const** pmsg) {
1666   z_stream strm;
1667   gz_header hdr;
1668   int err;
1669   char* block[] = {tmp, tmpLen + tmp};
1670   size_t result = 0;
1671 
1672   memset(&strm, 0, sizeof(z_stream));
1673   strm.zalloc = zlib_block_alloc;
1674   strm.zfree = zlib_block_free;
1675   strm.opaque = (voidpf) block;
1676 
1677   *pmsg = deflateInit2Wrapper(&strm, level);
1678 
1679   if (*pmsg == NULL) {
1680     strm.next_out = (Bytef *) outBuf;
1681     strm.avail_out = (uInt) outLen;
1682     strm.next_in = (Bytef *) inBuf;
1683     strm.avail_in = (uInt) inLen;
1684 
1685     if (comment != NULL) {
1686       memset(&hdr, 0, sizeof(hdr));
1687       hdr.comment = (Bytef*) comment;
1688       deflateSetHeader(&strm, &hdr);
1689     }
1690 
1691     err = deflate(&strm, Z_FINISH);
1692 
1693     if (err == Z_OK || err == Z_BUF_ERROR) {
1694       *pmsg = "Buffer too small";
1695     } else if (err != Z_STREAM_END) {
1696       *pmsg = "Intern deflate error";
1697     } else {
1698       result = (size_t) strm.total_out;
1699     }
1700 
1701     deflateEnd(&strm);
1702   }
1703 
1704   return result;
1705 }
< prev index next >