/*
 * Copyright (c) 2008 Mozilla Foundation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a 
 * copy of this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation 
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in 
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

package org.whattf.io;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Conceptually decodes bytes into ASCII and then decodes Base64 
 * into bytes. In practice, converting into a <code>Reader</code>
 * in between is optimized away.
 * 
 * @version $Id$
 * @author hsivonen
 */
public class Base64InputStream extends InputStream {

    private final InputStream delegate;

    private int bytesLeftInBuffer = 0;
    
    private int buffer = 0;
    
    /**
     * @param delegate
     */
    public Base64InputStream(InputStream delegate) {
        this.delegate = delegate;
    }

    @Override
    public int read() throws IOException {
        if (bytesLeftInBuffer == 0) {
            assert buffer == 0;
            bytesLeftInBuffer = 3;
            for (int i = 0; i < 4; i++) {
                int c = delegate.read();
                buffer <<= 6;
                if (c < 0) {
                    if (i == 0) {
                        return -1;
                    } else {
                        throw new EOFException();
                    }
                } else if (c >= 'A' && c <= 'Z') {
                    buffer <<= 6;
                    buffer |= (c - 'A') & 0x3F;
                } else if (c >= 'a' && c <= 'z') {
                    buffer |= (c - 'a' + 26) & 0x3F;
                } else if (c >= '0' && c <= '9') {
                    buffer |= (c - '0' + 52) & 0x3F;
                } else if (c == '+') {
                    buffer |= 62;
                } else if (c == '/') {
                    buffer |= 63;
                } else if (c == '=') {
                    if (i == 0 || i == 1) {
                        throw new IOException("Base 64 padding in a bad position.");
                    }
                    bytesLeftInBuffer--;
                } else {
                    throw new IOException("Non-Base64 input: \u201C0x" + Integer.toHexString(c) + "\u201D.");
                }
            }
            if (bytesLeftInBuffer == 1) {
                buffer >>= 16;
            } else if (bytesLeftInBuffer == 2) {
                buffer >>= 8;
            }
        }
        int rv = buffer & 0xFF;
        buffer >>= 8;
        bytesLeftInBuffer--;
        return rv;
    }

    /**
     * @throws IOException
     * @see java.io.InputStream#close()
     */
    public void close() throws IOException {
        delegate.close();
    }

}

@@ -1869,26 +1869,29 @@
     
-assert b
+bytesLeftInB
 uffer =
-= 0
+ 3
 ;%0A  
@@ -1901,36 +1901,42 @@
        b
-ytesLeftInBuffer = 3
+oolean paddingSeen = false
 ;%0A      
@@ -2097,32 +2097,79 @@
   if (i == 0) %7B%0A
+                        bytesLeftInBuffer = 0;%0A
                 
@@ -2319,27 +2319,54 @@
 if (
-c %3E= 'A' && c %3C= 'Z
+paddingSeen) %7B%0A                    if (c == '=
 ') %7B
@@ -2382,37 +2382,256 @@
             
-buffer %3C%3C= 6;
+    bytesLeftInBuffer--;%0A                    %7D else %7B%0A                        throw new IOException(%22Non-padding in Base64 stream after padding had started.%22);%0A                    %7D%0A                %7D else if (c %3E= 'A' && c %3C= 'Z') %7B
 %0A           
@@ -2658,23 +2658,16 @@
 c - 'A')
- & 0x3F
 ;%0A      
@@ -2755,23 +2755,16 @@
 a' + 26)
- & 0x3F
 ;%0A      
@@ -2856,15 +2856,8 @@
  52)
- & 0x3F
 ;%0A  
@@ -3182,32 +3182,72 @@
               %7D%0A
+                    paddingSeen = true;%0A
                 
@@ -3442,174 +3442,8 @@
   %7D%0A
-            if (bytesLeftInBuffer == 1) %7B%0A                buffer %3E%3E= 16;%0A            %7D else if (bytesLeftInBuffer == 2) %7B%0A                buffer %3E%3E= 8;%0A            %7D%0A
     
@@ -3465,16 +3465,17 @@
 nt rv = 
+(
 buffer &
@@ -3479,16 +3479,27 @@
 r & 0xFF
+0000) %3E%3E 16
 ;%0A      
@@ -3507,18 +3507,18 @@
  buffer 
-%3E%3E
+%3C%3C
 = 8;%0A   

Make the base64 decoder not bogus