resolve darcs stupidity
[org.ibex.core.git] / src / org / bouncycastle / asn1 / x509 / X509NameTokenizer.java
index 2474027..f91c7be 100644 (file)
@@ -1,48 +1,82 @@
 package org.bouncycastle.asn1.x509;
 
 /**
- * class for breaking up an X509 Name into it's component tokens, ala
+ * class for breaking up an X500 Name into it's component tokens, ala
  * java.util.StringTokenizer. We need this class as some of the
  * lightweight Java environment don't support classes like
  * StringTokenizer.
  */
 public class X509NameTokenizer
 {
-    private String  oid;
-    private int     index;
+    private String          oid;
+    private int             index;
+    private StringBuffer    buf = new StringBuffer();
 
     public X509NameTokenizer(
         String oid)
     {
         this.oid = oid;
-        this.index = 0;
+        this.index = -1;
     }
 
     public boolean hasMoreTokens()
     {
-        return (index != -1);
+        return (index != oid.length());
     }
 
     public String nextToken()
     {
-        if (index == -1)
+        if (index == oid.length())
         {
             return null;
         }
 
-        String  token;
-        int     end = oid.indexOf(',', index);
+        int     end = index + 1;
+        boolean quoted = false;
+        boolean escaped = false;
 
-        if (end == -1)
+        buf.setLength(0);
+
+        while (end != oid.length())
         {
-            token = oid.substring(index);
-            index = -1;
-            return token.trim();
-        }
+            char    c = oid.charAt(end);
 
-        token = oid.substring(index, end);
+            if (c == '"')
+            {
+                if (!escaped)
+                {
+                    quoted = !quoted;
+                }
+                else
+                {
+                    buf.append(c);
+                }
+                escaped = false;
+            }
+            else
+            {
+                if (escaped || quoted)
+                {
+                    buf.append(c);
+                    escaped = false;
+                }
+                else if (c == '\\')
+                {
+                    escaped = true;
+                }
+                else if (c == ',')
+                {
+                    break;
+                }
+                else
+                {
+                    buf.append(c);
+                }
+            }
+            end++;
+        }
 
-        index = end + 1;
-        return token.trim();
+        index = end;
+        return buf.toString().trim();
     }
 }