package org.bouncycastle.asn1;

import java.io.IOException;

import org.bouncycastle.util.Arrays;

/**
 * DER BMPString object encodes BMP (<i>Basic Multilingual Plane</i>) subset
 * (aka UCS-2) of UNICODE (ISO 10646) characters in codepoints 0 to 65535.
 * <p>
 * At ISO-10646:2011 the term "BMP" has been withdrawn, and replaced by
 * term "UCS-2".
 * </p>
 */
public class DERBMPString
    extends ASN1Primitive
    implements ASN1String
{
    private final char[]  string;

    /**
     * Return a BMP String from the given object.
     *
     * @param obj the object we want converted.
     * @exception IllegalArgumentException if the object cannot be converted.
     * @return a DERBMPString instance, or null.
     */
    public static DERBMPString getInstance(
        Object  obj)
    {
        if (obj == null || obj instanceof DERBMPString)
        {
            return (DERBMPString)obj;
        }

        if (obj instanceof byte[])
        {
            try
            {
                return (DERBMPString)fromByteArray((byte[])obj);
            }
            catch (Exception e)
            {
                throw new IllegalArgumentException("encoding error in getInstance: " + e.toString());
            }
        }

        throw new IllegalArgumentException("illegal object in getInstance: " + obj.getClass().getName());
    }

    /**
     * Return a BMP String from a tagged object.
     *
     * @param obj the tagged object holding the object we want
     * @param explicit true if the object is meant to be explicitly
     *              tagged false otherwise.
     * @exception IllegalArgumentException if the tagged object cannot
     *              be converted.
     * @return a DERBMPString instance.
     */
    public static DERBMPString getInstance(
        ASN1TaggedObject obj,
        boolean          explicit)
    {
        ASN1Primitive o = obj.getObject();

        if (explicit || o instanceof DERBMPString)
        {
            return getInstance(o);
        }
        else
        {
            return new DERBMPString(ASN1OctetString.getInstance(o).getOctets());
        }
    }

    /**
     * Basic constructor - byte encoded string.
     * @param string the encoded BMP STRING to wrap.
     */
    DERBMPString(
        byte[]   string)
    {
        char[]  cs = new char[string.length / 2];

        for (int i = 0; i != cs.length; i++)
        {
            cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
        }

        this.string = cs;
    }

    DERBMPString(char[] string)
    {
        this.string = string;
    }

    /**
     * Basic constructor
     * @param string a String to wrap as a BMP STRING.
     */
    public DERBMPString(
        String   string)
    {
        this.string = string.toCharArray();
    }

    public String getString()
    {
        return new String(string);
    }

    public String toString()
    {
        return getString();
    }

    public int hashCode()
    {
        return Arrays.hashCode(string);
    }

    protected boolean asn1Equals(
        ASN1Primitive o)
    {
        if (!(o instanceof DERBMPString))
        {
            return false;
        }

        DERBMPString  s = (DERBMPString)o;

        return Arrays.areEqual(string, s.string);
    }

    boolean isConstructed()
    {
        return false;
    }

    int encodedLength()
    {
        return 1 + StreamUtil.calculateBodyLength(string.length * 2) + (string.length * 2);
    }

    void encode(
        ASN1OutputStream out)
        throws IOException
    {
        out.write(BERTags.BMP_STRING);
        out.writeLength(string.length * 2);

        for (int i = 0; i != string.length; i++)
        {
            char c = string[i];

            out.write((byte)(c >> 8));
            out.write((byte)c);
        }
    }
}

@@ -3546,24 +3546,59 @@
 ption%0A    %7B%0A
+        int count = string.length;%0A
         out.
@@ -3644,37 +3644,29 @@
 writeLength(
-string.length
+count
  * 2);%0A%0A    
@@ -3673,47 +3673,98 @@
     
-for (int i = 0; i != string.length; i++
+byte%5B%5D buf = new byte%5B8%5D;%0A%0A        int i = 0, limit = count & -4;%0A        while (i %3C limit
 )%0A  
@@ -3789,16 +3789,17 @@
   char c
+0
  = strin
@@ -3806,41 +3806,258 @@
 g%5Bi%5D
-;%0A%0A            out.write(
+, c1 = string%5Bi + 1%5D, c2 = string%5Bi + 2%5D, c3 = string%5Bi + 3%5D;%0A            i += 4;%0A%0A            buf%5B0%5D = (byte)(c0 %3E%3E 8);%0A            buf%5B1%5D = (byte)c0;%0A            buf%5B2%5D = (byte)(c1 %3E%3E 8);%0A            buf%5B3%5D = (byte)c1;%0A            buf%5B4%5D = 
 (byte)(c
  %3E%3E 
@@ -4056,46 +4056,487 @@
 e)(c
+2
  %3E%3E 8)
-);%0A            out.write((byte)c
+;%0A            buf%5B5%5D = (byte)c2;%0A            buf%5B6%5D = (byte)(c3 %3E%3E 8);%0A            buf%5B7%5D = (byte)c3;%0A%0A            out.write(buf, 0, 8);%0A        %7D%0A        if (i %3C count)%0A        %7B%0A            int bufPos = 0;%0A            do%0A            %7B%0A                char c0 = string%5Bi%5D;%0A                i += 1;%0A%0A                buf%5BbufPos++%5D = (byte)(c0 %3E%3E 8);%0A                buf%5BbufPos++%5D = (byte)c0;%0A            %7D%0A            while (i %3C count);%0A%0A            out.write(buf, 0, bufPos
 );%0A 

Use small buffer in DerBMPString.encode