Quellcodebibliothek Statistik Leitseite products/Sources/formale Sprachen/Java/Tomcat/java/org/apache/tomcat/util/buf/   (Apache Software Stiftung Version 2.4.65©)  Datei vom 10.10.2023 mit Größe 3 kB image not shown  

Quelle  Asn1Parser.java   Sprache: JAVA

 
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.tomcat.util.buf;

import java.math.BigInteger;

import org.apache.tomcat.util.res.StringManager;

/**
 * This is a very basic ASN.1 parser that provides the limited functionality required by Tomcat. It is a long way from a
 * complete parser.
 * <p>
 * TODO: Consider extending this parser and refactoring the SpnegoTokenFixer to use it.
 */

public class Asn1Parser {

    private static final StringManager sm = StringManager.getManager(Asn1Parser.class);

    public static final int TAG_INTEGER = 0x02;
    public static final int TAG_OCTET_STRING = 0x04;
    public static final int TAG_NULL = 0x05;
    public static final int TAG_OID = 0x06;
    public static final int TAG_SEQUENCE = 0x30;
    public static final int TAG_ATTRIBUTE_BASE = 0xA0;

    private final byte[] source;

    private int pos = 0;


    public Asn1Parser(byte[] source) {
        this.source = source;
    }


    public boolean eof() {
        return pos == source.length;
    }


    public int peekTag() {
        return source[pos] & 0xFF;
    }


    public void parseTagSequence() {
        parseTag(TAG_SEQUENCE);
    }


    public void parseTag(int tag) {
        int value = next();
        if (value != tag) {
            throw new IllegalArgumentException(
                    sm.getString("asn1Parser.tagMismatch", Integer.valueOf(tag), Integer.valueOf(value)));
        }
    }


    public void parseFullLength() {
        int len = parseLength();
        if (len + pos != source.length) {
            throw new IllegalArgumentException(sm.getString("asn1Parser.lengthInvalid", Integer.valueOf(len),
                    Integer.valueOf(source.length - pos)));
        }
    }


    public int parseLength() {
        int len = next();
        if (len > 127) {
            int bytes = len - 128;
            len = 0;
            for (int i = 0; i < bytes; i++) {
                len = len << 8;
                len = len + next();
            }
        }
        return len;
    }


    public BigInteger parseInt() {
        byte[] val = parseBytes(TAG_INTEGER);
        return new BigInteger(val);
    }


    public byte[] parseOctetString() {
        return parseBytes(TAG_OCTET_STRING);
    }


    public void parseNull() {
        parseBytes(TAG_NULL);
    }


    public byte[] parseOIDAsBytes() {
        return parseBytes(TAG_OID);
    }


    public byte[] parseAttributeAsBytes(int index) {
        return parseBytes(TAG_ATTRIBUTE_BASE + index);
    }


    private byte[] parseBytes(int tag) {
        parseTag(tag);
        int len = parseLength();
        byte[] result = new byte[len];
        System.arraycopy(source, pos, result, 0, result.length);
        pos += result.length;
        return result;
    }


    public void parseBytes(byte[] dest) {
        System.arraycopy(source, pos, dest, 0, dest.length);
        pos += dest.length;
    }


    private int next() {
        return source[pos++] & 0xFF;
    }
}

Messung V0.5
C=96 H=92 G=93

¤ Dauer der Verarbeitung: 0.1 Sekunden  (vorverarbeitet)  ¤

*© Formatika GbR, Deutschland






Wurzel

Suchen

Beweissystem der NASA

Beweissystem Isabelle

NIST Cobol Testsuite

Cephes Mathematical Library

Wiener Entwicklungsmethode

Haftungshinweis

Die Informationen auf dieser Webseite wurden nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit, noch Qualität der bereit gestellten Informationen zugesichert.

Bemerkung:

Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.