Parses an ASN.1 length field. @return the parsed length value
()
| 158 | * @return the parsed length value |
| 159 | */ |
| 160 | public int parseLength() { |
| 161 | int len = next(); |
| 162 | if (len > 127) { |
| 163 | int bytes = len - 128; |
| 164 | if (bytes > 4) { |
| 165 | throw new IllegalArgumentException(sm.getString("asn1Parser.lengthInvalid", Integer.valueOf(-1), |
| 166 | Integer.valueOf(source.length - pos))); |
| 167 | } |
| 168 | len = 0; |
| 169 | for (int i = 0; i < bytes; i++) { |
| 170 | len = len << 8; |
| 171 | len = len + next(); |
| 172 | } |
| 173 | if (len < 0) { |
| 174 | throw new IllegalArgumentException(sm.getString("asn1Parser.lengthInvalid", Integer.valueOf(-1), |
| 175 | Integer.valueOf(source.length - pos))); |
| 176 | } |
| 177 | } |
| 178 | /* |
| 179 | * If this is the first length parsed after a sequence has been added to the sequence nesting tracking mechanism |
| 180 | * it must be the length of the sequence so update the entry to record the end position of the sequence. Note |
| 181 | * that position recorded is actually the start of the first element after the sequence ends. |
| 182 | */ |
| 183 | if (nestedSequenceEndPositions.peekLast() != null && nestedSequenceEndPositions.peekLast().intValue() == -1) { |
| 184 | nestedSequenceEndPositions.pollLast(); |
| 185 | if (source.length - pos < len) { |
| 186 | throw new IllegalArgumentException(sm.getString("asn1Parser.lengthInvalid", Integer.valueOf(-1), |
| 187 | Integer.valueOf(source.length - pos))); |
| 188 | } |
| 189 | nestedSequenceEndPositions.addLast(Integer.valueOf(pos + len)); |
| 190 | } |
| 191 | return len; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | /** |
no test coverage detected