// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.jcajce.provider.asymmetric.x509;

import org.bouncycastle.util.encoders.Base64;
import org.bouncycastle.asn1.ASN1Sequence;
import java.io.IOException;
import java.io.InputStream;

class PEMUtil
{
    private final Boundaries[] _supportedBoundaries;
    
    PEMUtil(final String str) {
        this._supportedBoundaries = new Boundaries[] { new Boundaries(str), new Boundaries("X509 " + str), new Boundaries("PKCS7") };
    }
    
    private String readLine(final InputStream inputStream) throws IOException {
        final StringBuffer sb = new StringBuffer();
        int read;
        while (true) {
            if ((read = inputStream.read()) != 13 && read != 10 && read >= 0) {
                sb.append((char)read);
            }
            else {
                if (read < 0 || sb.length() != 0) {
                    break;
                }
                continue;
            }
        }
        if (read >= 0) {
            if (read == 13) {
                inputStream.mark(1);
                final int read2;
                if ((read2 = inputStream.read()) == 10) {
                    inputStream.mark(1);
                }
                if (read2 > 0) {
                    inputStream.reset();
                }
            }
            return sb.toString();
        }
        if (sb.length() == 0) {
            return null;
        }
        return sb.toString();
    }
    
    private Boundaries getBoundaries(final String s) {
        for (int i = 0; i != this._supportedBoundaries.length; ++i) {
            final Boundaries boundaries = this._supportedBoundaries[i];
            if (boundaries.isTheExpectedHeader(s) || boundaries.isTheExpectedFooter(s)) {
                return boundaries;
            }
        }
        return null;
    }
    
    ASN1Sequence readPEMObject(final InputStream inputStream, final boolean b) throws IOException {
        final StringBuffer sb = new StringBuffer();
        Boundaries boundaries = null;
        String line;
        while (boundaries == null && (line = this.readLine(inputStream)) != null) {
            boundaries = this.getBoundaries(line);
            if (boundaries != null && !boundaries.isTheExpectedHeader(line)) {
                throw new IOException("malformed PEM data: found footer where header was expected");
            }
        }
        if (boundaries == null) {
            if (!b) {
                return null;
            }
            throw new IOException("malformed PEM data: no header found");
        }
        else {
            Boundaries boundaries2 = null;
            String line2;
            while (boundaries2 == null && (line2 = this.readLine(inputStream)) != null) {
                boundaries2 = this.getBoundaries(line2);
                if (boundaries2 != null) {
                    if (!boundaries.isTheExpectedFooter(line2)) {
                        throw new IOException("malformed PEM data: header/footer mismatch");
                    }
                    continue;
                }
                else {
                    sb.append(line2);
                }
            }
            if (boundaries2 == null) {
                throw new IOException("malformed PEM data: no footer found");
            }
            if (sb.length() != 0) {
                try {
                    return ASN1Sequence.getInstance(Base64.decode(sb.toString()));
                }
                catch (final Exception ex) {
                    throw new IOException("malformed PEM data encountered");
                }
            }
            return null;
        }
    }
    
    private static class Boundaries
    {
        private final String _header;
        private final String _footer;
        
        private Boundaries(final String s) {
            this._header = "-----BEGIN " + s + "-----";
            this._footer = "-----END " + s + "-----";
        }
        
        public boolean isTheExpectedHeader(final String s) {
            return s.startsWith(this._header);
        }
        
        public boolean isTheExpectedFooter(final String s) {
            return s.startsWith(this._footer);
        }
    }
}
