41 switch (type) { 42 case MODULE_INFO: 43 case SIGNATURE: 44 return "."; 45 case CLASSES: 46 case RESOURCES: 47 return "classes"; 48 case NATIVE_LIBS: 49 return "lib"; 50 case NATIVE_CMDS: 51 return "bin"; 52 case CONFIG: 53 return "etc"; 54 default: 55 throw new AssertionError(type); 56 } 57 } 58 59 public final static class Reader implements Closeable { 60 61 private DataInputStream stream; 62 private File destination; 63 private boolean deflate; 64 private HashType hashtype; 65 66 private static class CountingInputStream extends FilterInputStream { 67 int count; 68 public CountingInputStream(InputStream stream, int count) { 69 super(stream); 70 this.count = count; 71 } 72 73 public int available() throws IOException { 74 return count; 75 } 76 77 public boolean markSupported() { 78 return false; 79 } 80 106 return -1; 107 n = Math.min(n, count); 108 long skipped = super.skip(n); 109 if (n > 0) 110 count-=skipped; 111 return skipped; 112 } 113 } 114 115 public Reader(DataInputStream stream) { 116 hashtype = HashType.SHA256; 117 // Ensure that mark/reset is supported 118 if (stream.markSupported()) { 119 this.stream = stream; 120 } else { 121 this.stream = 122 new DataInputStream(new BufferedInputStream(stream)); 123 } 124 } 125 126 private void checkHashMatch(byte[] expected, byte[] computed) 127 throws IOException 128 { 129 if (!MessageDigest.isEqual(expected, computed)) 130 throw new IOException("Expected hash " 131 + hashHexString(expected) 132 + " instead of " 133 + hashHexString(computed)); 134 } 135 136 private ModuleFileHeader fileHeader = null; 137 private MessageDigest fileDigest = null; 138 private MessageDigest sectionDigest = null; 139 private DataInputStream fileIn = null; 140 private byte[] moduleInfoBytes = null; 141 private Integer moduleSignatureType = null; 142 private byte[] moduleSignatureBytes = null; 143 private final int MAX_SECTION_HEADER_LENGTH = 128; 144 private List<byte[]> calculatedHashes = new ArrayList<>(); 145 private boolean extract = true; 452 } 453 454 public byte[] readModuleInfo(DataInputStream in, int csize) 455 throws IOException 456 { 457 CountingInputStream cin = new CountingInputStream(in, csize); 458 ByteArrayOutputStream out = new ByteArrayOutputStream(); 459 byte[] buf = new byte[8192]; 460 int n; 461 while ((n = cin.read(buf)) >= 0) 462 out.write(buf, 0, n); 463 return out.toByteArray(); 464 } 465 466 public byte[] readModuleSignature(DataInputStream in, int csize) 467 throws IOException 468 { 469 return readModuleInfo(in, csize); // signature has the same format 470 } 471 472 private File computeRealPath(String storedpath) throws IOException { 473 474 String convertedpath = storedpath.replace('/', File.separatorChar); 475 File path = new File(convertedpath); 476 477 // Absolute path names are not permitted. 478 ensureNonAbsolute(path); 479 path = resolveAndNormalize(destination, convertedpath); 480 // Create the parent directories if necessary 481 File parent = path.getParentFile(); 482 if (!parent.exists()) 483 Files.mkdirs(parent, path.getName()); 484 485 return path; 486 } 487 488 private File computeRealPath(SectionType type, 489 String storedpath) 490 throws IOException 491 { 492 String dir = getSubdirOfSection(type); 493 return computeRealPath(dir + File.separatorChar + storedpath); 494 } 495 496 private static void markNativeCodeExecutable(SectionType type, 497 File file) 498 { 499 if (type == SectionType.NATIVE_CMDS 500 || (type == SectionType.NATIVE_LIBS 501 && System.getProperty("os.name").startsWith("Windows"))) 502 { 503 file.setExecutable(true); 504 } 505 } 506 507 private void unpack200gzip(DataInputStream in) throws IOException { 508 GZIPInputStream gis = new GZIPInputStream(in) { 509 public void close() throws IOException {} 510 }; 511 Pack200.Unpacker unpacker = Pack200.newUnpacker(); | 41 switch (type) { 42 case MODULE_INFO: 43 case SIGNATURE: 44 return "."; 45 case CLASSES: 46 case RESOURCES: 47 return "classes"; 48 case NATIVE_LIBS: 49 return "lib"; 50 case NATIVE_CMDS: 51 return "bin"; 52 case CONFIG: 53 return "etc"; 54 default: 55 throw new AssertionError(type); 56 } 57 } 58 59 public final static class Reader implements Closeable { 60 61 // The library where this module is to be installed, or null if 62 // simply extracting ( jmod Extract, or jsign ) 63 private SimpleLibrary lib; 64 private DataInputStream stream; 65 private File destination; 66 private boolean deflate; 67 private HashType hashtype; 68 69 private static class CountingInputStream extends FilterInputStream { 70 int count; 71 public CountingInputStream(InputStream stream, int count) { 72 super(stream); 73 this.count = count; 74 } 75 76 public int available() throws IOException { 77 return count; 78 } 79 80 public boolean markSupported() { 81 return false; 82 } 83 109 return -1; 110 n = Math.min(n, count); 111 long skipped = super.skip(n); 112 if (n > 0) 113 count-=skipped; 114 return skipped; 115 } 116 } 117 118 public Reader(DataInputStream stream) { 119 hashtype = HashType.SHA256; 120 // Ensure that mark/reset is supported 121 if (stream.markSupported()) { 122 this.stream = stream; 123 } else { 124 this.stream = 125 new DataInputStream(new BufferedInputStream(stream)); 126 } 127 } 128 129 public Reader(DataInputStream stream, SimpleLibrary lib) { 130 this(stream); 131 this.lib = lib; 132 } 133 134 private void checkHashMatch(byte[] expected, byte[] computed) 135 throws IOException 136 { 137 if (!MessageDigest.isEqual(expected, computed)) 138 throw new IOException("Expected hash " 139 + hashHexString(expected) 140 + " instead of " 141 + hashHexString(computed)); 142 } 143 144 private ModuleFileHeader fileHeader = null; 145 private MessageDigest fileDigest = null; 146 private MessageDigest sectionDigest = null; 147 private DataInputStream fileIn = null; 148 private byte[] moduleInfoBytes = null; 149 private Integer moduleSignatureType = null; 150 private byte[] moduleSignatureBytes = null; 151 private final int MAX_SECTION_HEADER_LENGTH = 128; 152 private List<byte[]> calculatedHashes = new ArrayList<>(); 153 private boolean extract = true; 460 } 461 462 public byte[] readModuleInfo(DataInputStream in, int csize) 463 throws IOException 464 { 465 CountingInputStream cin = new CountingInputStream(in, csize); 466 ByteArrayOutputStream out = new ByteArrayOutputStream(); 467 byte[] buf = new byte[8192]; 468 int n; 469 while ((n = cin.read(buf)) >= 0) 470 out.write(buf, 0, n); 471 return out.toByteArray(); 472 } 473 474 public byte[] readModuleSignature(DataInputStream in, int csize) 475 throws IOException 476 { 477 return readModuleInfo(in, csize); // signature has the same format 478 } 479 480 // Returns the path for the section type, if the library 481 // has one configured. 482 private File librarySectionPath(SectionType type) { 483 if (lib != null && type == SectionType.NATIVE_LIBS 484 && lib.natlibs() != null) 485 return lib.natlibs(); 486 if (lib != null && type == SectionType.NATIVE_CMDS 487 && lib.natcmds() != null) 488 return lib.natcmds(); 489 490 return null; 491 } 492 493 private File computeRealPath(String storedpath) throws IOException { 494 495 String convertedpath = storedpath.replace('/', File.separatorChar); 496 File path = new File(convertedpath); 497 498 // Absolute path names are not permitted. 499 ensureNonAbsolute(path); 500 path = resolveAndNormalize(destination, convertedpath); 501 // Create the parent directories if necessary 502 File parent = path.getParentFile(); 503 if (!parent.exists()) 504 Files.mkdirs(parent, path.getName()); 505 506 return path; 507 } 508 509 private File computeRealPath(SectionType type, 510 String storedpath) 511 throws IOException 512 { 513 File lsp = librarySectionPath(type); 514 if (lsp != null) { 515 // The library has a configured path for this section 516 File realpath = new File(lsp, storedpath); 517 // Create the parent directories if necessary 518 File parent = realpath.getParentFile(); 519 if (!parent.exists()) 520 Files.mkdirs(parent, realpath.getName()); 521 return realpath; 522 } 523 String dir = getSubdirOfSection(type); 524 return computeRealPath(dir + File.separatorChar + storedpath); 525 } 526 527 private static void markNativeCodeExecutable(SectionType type, 528 File file) 529 { 530 if (type == SectionType.NATIVE_CMDS 531 || (type == SectionType.NATIVE_LIBS 532 && System.getProperty("os.name").startsWith("Windows"))) 533 { 534 file.setExecutable(true); 535 } 536 } 537 538 private void unpack200gzip(DataInputStream in) throws IOException { 539 GZIPInputStream gis = new GZIPInputStream(in) { 540 public void close() throws IOException {} 541 }; 542 Pack200.Unpacker unpacker = Pack200.newUnpacker(); |