jdk/src/share/classes/com/sun/java/util/jar/pack/BandStructure.java

Print this page
rev 5678 : 7186957: Improve Pack200 data validation
Reviewed-by: jrose, jjh, mschoene


 986                 setBandIndex(this, cpTag);
 987         }
 988         public CPRefBand(String name, Coding regularCoding, byte cpTag) {
 989             this(name, regularCoding, cpTag, false);
 990         }
 991         public CPRefBand(String name, Coding regularCoding, Object undef) {
 992             this(name, regularCoding, CONSTANT_None, false);
 993         }
 994 
 995         public void setIndex(Index index) {
 996             this.index = index;
 997         }
 998 
 999         protected void readDataFrom(InputStream in) throws IOException {
1000             super.readDataFrom(in);
1001             assert(assertValidCPRefs(this));
1002         }
1003 
1004         /** Write a constant pool reference. */
1005         public void putRef(Entry e) {
1006             assert(index != null);
1007             addValue(encodeRefOrNull(e, index));
1008         }
1009         public void putRef(Entry e, Index index) {
1010             assert(this.index == null);
1011             addValue(encodeRefOrNull(e, index));
1012         }
1013         public void putRef(Entry e, byte cptag) {
1014             putRef(e, getCPIndex(cptag));
1015         }
1016 
1017         public Entry getRef() {
1018             if (index == null)  Utils.log.warning("No index for "+this);
1019             assert(index != null);
1020             return decodeRefOrNull(getValue(), index);
1021         }
1022         public Entry getRef(Index index) {
1023             assert(this.index == null);
1024             return decodeRefOrNull(getValue(), index);
1025         }
1026         public Entry getRef(byte cptag) {


1038             return (nullOK ? 1 : 0) + nonNullCode;
1039         }
1040         private Entry decodeRefOrNull(int code, Index index) {
1041             // Inverse to encodeRefOrNull...
1042             int nonNullCode = code - (nullOK ? 1 : 0);
1043             if (nonNullCode == -1) {
1044                 return null;
1045             } else {
1046                 return decodeRef(nonNullCode, index);
1047             }
1048         }
1049     }
1050 
1051     // Bootstrap support for CPRefBands.  These are needed to record
1052     // intended CP indexes, before the CP has been created.
1053     private final List<CPRefBand> allKQBands = new ArrayList<>();
1054     private List<Object[]> needPredefIndex = new ArrayList<>();
1055 
1056 
1057     int encodeRef(Entry e, Index ix) {


1058         int coding = ix.indexOf(e);
1059         if (verbose > 2)
1060             Utils.log.fine("putRef "+coding+" => "+e);
1061         return coding;
1062     }
1063 
1064     Entry decodeRef(int n, Index ix) {
1065         if (n < 0 || n >= ix.size())
1066             Utils.log.warning("decoding bad ref "+n+" in "+ix);
1067         Entry e = ix.getEntry(n);
1068         if (verbose > 2)
1069             Utils.log.fine("getRef "+n+" => "+e);
1070         return e;
1071     }
1072 
1073     private CodingChooser codingChooser;
1074     protected CodingChooser getCodingChooser() {
1075         if (codingChooser == null) {
1076             codingChooser = new CodingChooser(effort, basicCodings);
1077             if (codingChooser.stress != null




 986                 setBandIndex(this, cpTag);
 987         }
 988         public CPRefBand(String name, Coding regularCoding, byte cpTag) {
 989             this(name, regularCoding, cpTag, false);
 990         }
 991         public CPRefBand(String name, Coding regularCoding, Object undef) {
 992             this(name, regularCoding, CONSTANT_None, false);
 993         }
 994 
 995         public void setIndex(Index index) {
 996             this.index = index;
 997         }
 998 
 999         protected void readDataFrom(InputStream in) throws IOException {
1000             super.readDataFrom(in);
1001             assert(assertValidCPRefs(this));
1002         }
1003 
1004         /** Write a constant pool reference. */
1005         public void putRef(Entry e) {

1006             addValue(encodeRefOrNull(e, index));
1007         }
1008         public void putRef(Entry e, Index index) {
1009             assert(this.index == null);
1010             addValue(encodeRefOrNull(e, index));
1011         }
1012         public void putRef(Entry e, byte cptag) {
1013             putRef(e, getCPIndex(cptag));
1014         }
1015 
1016         public Entry getRef() {
1017             if (index == null)  Utils.log.warning("No index for "+this);
1018             assert(index != null);
1019             return decodeRefOrNull(getValue(), index);
1020         }
1021         public Entry getRef(Index index) {
1022             assert(this.index == null);
1023             return decodeRefOrNull(getValue(), index);
1024         }
1025         public Entry getRef(byte cptag) {


1037             return (nullOK ? 1 : 0) + nonNullCode;
1038         }
1039         private Entry decodeRefOrNull(int code, Index index) {
1040             // Inverse to encodeRefOrNull...
1041             int nonNullCode = code - (nullOK ? 1 : 0);
1042             if (nonNullCode == -1) {
1043                 return null;
1044             } else {
1045                 return decodeRef(nonNullCode, index);
1046             }
1047         }
1048     }
1049 
1050     // Bootstrap support for CPRefBands.  These are needed to record
1051     // intended CP indexes, before the CP has been created.
1052     private final List<CPRefBand> allKQBands = new ArrayList<>();
1053     private List<Object[]> needPredefIndex = new ArrayList<>();
1054 
1055 
1056     int encodeRef(Entry e, Index ix) {
1057         if (ix == null)
1058             throw new RuntimeException("null index for " + e.stringValue());
1059         int coding = ix.indexOf(e);
1060         if (verbose > 2)
1061             Utils.log.fine("putRef "+coding+" => "+e);
1062         return coding;
1063     }
1064 
1065     Entry decodeRef(int n, Index ix) {
1066         if (n < 0 || n >= ix.size())
1067             Utils.log.warning("decoding bad ref "+n+" in "+ix);
1068         Entry e = ix.getEntry(n);
1069         if (verbose > 2)
1070             Utils.log.fine("getRef "+n+" => "+e);
1071         return e;
1072     }
1073 
1074     private CodingChooser codingChooser;
1075     protected CodingChooser getCodingChooser() {
1076         if (codingChooser == null) {
1077             codingChooser = new CodingChooser(effort, basicCodings);
1078             if (codingChooser.stress != null