package org.bouncycastle.crypto.digests;

import org.bouncycastle.crypto.ExtendedDigest;
import org.bouncycastle.util.Memoable;
import org.bouncycastle.util.Pack;

/**
 * base implementation of MD4 family style digest as outlined in
 * "Handbook of Applied Cryptography", pages 344 - 347.
 */
public abstract class GeneralDigest
    implements ExtendedDigest, Memoable
{
    private static final int BYTE_LENGTH = 64;

    private final byte[]  xBuf = new byte[4];
    private int           xBufOff;

    private long    byteCount;

    /**
     * Standard constructor
     */
    protected GeneralDigest()
    {
        xBufOff = 0;
    }

    /**
     * Copy constructor.  We are using copy constructors in place
     * of the Object.clone() interface as this interface is not
     * supported by J2ME.
     */
    protected GeneralDigest(GeneralDigest t)
    {
        copyIn(t);
    }

    protected GeneralDigest(byte[] encodedState)
    {
        System.arraycopy(encodedState, 0, xBuf, 0, xBuf.length);
        xBufOff = Pack.bigEndianToInt(encodedState, 4);
        byteCount = Pack.bigEndianToLong(encodedState, 8);
    }

    protected void copyIn(GeneralDigest t)
    {
        System.arraycopy(t.xBuf, 0, xBuf, 0, t.xBuf.length);

        xBufOff = t.xBufOff;
        byteCount = t.byteCount;
    }

    public void update(
        byte in)
    {
        xBuf[xBufOff++] = in;

        if (xBufOff == xBuf.length)
        {
            processWord(xBuf, 0);
            xBufOff = 0;
        }

        byteCount++;
    }

    public void update(
        byte[]  in,
        int     inOff,
        int     len)
    {
        //
        // fill the current word
        //
        while ((xBufOff != 0) && (len > 0))
        {
            update(in[inOff]);

            inOff++;
            len--;
        }

        //
        // process whole words.
        //
        while (len > xBuf.length)
        {
            processWord(in, inOff);

            inOff += xBuf.length;
            len -= xBuf.length;
            byteCount += xBuf.length;
        }

        //
        // load in the remainder.
        //
        while (len > 0)
        {
            update(in[inOff]);

            inOff++;
            len--;
        }
    }

    public void finish()
    {
        long    bitLength = (byteCount << 3);

        //
        // add the pad bytes.
        //
        update((byte)128);

        while (xBufOff != 0)
        {
            update((byte)0);
        }

        processLength(bitLength);

        processBlock();
    }

    public void reset()
    {
        byteCount = 0;

        xBufOff = 0;
        for (int i = 0; i < xBuf.length; i++)
        {
            xBuf[i] = 0;
        }
    }

    protected void populateState(byte[] state)
    {
        System.arraycopy(xBuf, 0, state, 0, xBufOff);
        Pack.intToBigEndian(xBufOff, state, 4);
        Pack.longToBigEndian(byteCount, state, 8);
    }

    public int getByteLength()
    {
        return BYTE_LENGTH;
    }
    
    protected abstract void processWord(byte[] in, int inOff);

    protected abstract void processLength(long bitLength);

    protected abstract void processBlock();
}

@@ -1613,32 +1613,66 @@
      len)%0A    %7B%0A
+        len = Math.max(0,  len);%0A%0A
         //%0A     
@@ -1722,109 +1722,182 @@
     
-while ((xBufOff != 0) && (len %3E 0))%0A        %7B%0A            update(in%5BinOff%5D);%0A%0A            inOff++;%0A  
+int i = 0;%0A        if (xBufOff != 0)%0A        %7B%0A            while (i %3C len)%0A            %7B%0A                xBuf%5BxBufOff++%5D = in%5BinOff + i++%5D;%0A                if (xBufOff == 4)%0A
     
@@ -1902,33 +1902,23 @@
         
-len--;%0A
       
-  %7D%0A
+%7B
 %0A       
@@ -1918,27 +1918,27 @@
         
-//%0A
+   
         
 // proce
@@ -1933,18 +1933,16 @@
     
-//
  process
  who
@@ -1941,21 +1941,22 @@
 cess
- whole words.
+Word(xBuf, 0);
 %0A   
@@ -1956,35 +1956,32 @@
 0);%0A        
-//%0A
         
 while (len %3E
@@ -1972,33 +1972,83 @@
     
-while (len %3E xBuf.length)
+    xBufOff = 0;%0A                    break;%0A                %7D%0A            %7D
 %0A   
@@ -2044,33 +2044,34 @@
       %7D%0A        
-%7B
+%7D%0A
 %0A            pro
@@ -2067,41 +2067,54 @@
     
+//%0A
     
-processWord(in, inOff);%0A%0A
+    // process whole words.%0A  
     
+  //%0A
     
@@ -2123,96 +2123,122 @@
   in
-Off += xBuf.length;%0A            len -= xBuf.length;%0A            byteCount += xBuf.length
+t limit = ((len - i) & ~3) + i;%0A        for (; i %3C limit; i += 4)%0A        %7B%0A            processWord(in, inOff + i)
 ;%0A  
@@ -2321,15 +2321,15 @@
 le (
-len %3E 0
+i %3C len
 )%0A  
@@ -2352,77 +2352,79 @@
     
-update(in%5BinOff%5D);%0A%0A            inOff++;%0A            len--;%0A        %7D
+xBuf%5BxBufOff++%5D = in%5BinOff + i++%5D;%0A        %7D%0A%0A        byteCount += len;
 %0A   

Rewrite (block) update for improved performance