コード例 #1
0
        public static StringBuffer ConvertToUnicode(UCharacterIterator iter, IDNA2003Options options)
        {
            // the source contains all ascii codepoints
            bool srcIsASCII = true;

            int ch;
            int saveIndex = iter.Index;

            // step 1: find out if all the codepoints in src are ASCII
            while ((ch = iter.Next()) != UCharacterIterator.DONE)
            {
                if (ch > 0x7F)
                {
                    srcIsASCII = false;
                    break;
                }
            }

            // The RFC states that
            // <quote>
            // ToUnicode never fails. If any step fails, then the original input
            // is returned immediately in that step.
            // </quote>
            do
            {
                StringBuffer processOut;
                if (srcIsASCII == false)
                {
                    // step 2: process the string
                    iter.Index = (saveIndex);
                    try
                    {
                        processOut = transform.Prepare(iter, (StringPrepOptions)options);
                    }
                    catch (StringPrepParseException e)
                    {
                        break;
                    }
                }
                else
                {
                    // just point to source
                    processOut = new StringBuffer(iter.GetText());
                }

                // step 3: verify ACE Prefix
                if (StartsWithPrefix(processOut))
                {
                    // step 4: Remove the ACE Prefix
                    String temp = processOut.ToString(ACE_PREFIX_LENGTH, processOut.Length - ACE_PREFIX_LENGTH);

                    // step 5: Decode using punycode
                    StringBuffer decodeOut = null;
                    try
                    {
                        decodeOut = PunycodeReference.Decode(new StringBuffer(temp), null);
                    }
                    catch (StringPrepParseException e)
                    {
                        break;
                    }

                    // step 6:Apply toASCII
                    StringBuffer toASCIIOut = ConvertToASCII(decodeOut, options);

                    // step 7: verify
                    if (CompareCaseInsensitiveASCII(processOut, toASCIIOut) != 0)
                    {
                        break;
                    }
                    // step 8: return output of step 5
                    return(decodeOut);
                }
            } while (false);

            return(new StringBuffer(iter.GetText()));
        }
コード例 #2
0
        public static StringBuffer ConvertToASCII(UCharacterIterator srcIter, IDNA2003Options options)
        {
            char[]
            caseFlags = null;

            // the source contains all ascii codepoints
            bool srcIsASCII = true;
            // assume the source contains all LDH codepoints
            bool srcIsLDH = true;

            //get the options
            bool useSTD3ASCIIRules = ((options & USE_STD3_RULES) != 0);

            int ch;

            // step 1
            while ((ch = srcIter.Next()) != UCharacterIterator.DONE)
            {
                if (ch > 0x7f)
                {
                    srcIsASCII = false;
                }
            }
            int failPos = -1;

            srcIter.SetToStart();
            StringBuffer processOut = null;

            // step 2 is performed only if the source contains non ASCII
            if (!srcIsASCII)
            {
                // step 2
                processOut = transform.Prepare(srcIter, (StringPrepOptions)options);
            }
            else
            {
                processOut = new StringBuffer(srcIter.GetText());
            }
            int poLen = processOut.Length;

            if (poLen == 0)
            {
                throw new StringPrepParseException("Found zero length lable after NamePrep.", StringPrepErrorType.ZeroLengthLabel);
            }
            StringBuffer dest = new StringBuffer();

            // reset the variable to verify if output of prepare is ASCII or not
            srcIsASCII = true;

            // step 3 & 4
            for (int j = 0; j < poLen; j++)
            {
                ch = processOut[j];
                if (ch > 0x7F)
                {
                    srcIsASCII = false;
                }
                else if (IsLDHChar(ch) == false)
                {
                    // here we do not assemble surrogates
                    // since we know that LDH code points
                    // are in the ASCII range only
                    srcIsLDH = false;
                    failPos  = j;
                }
            }

            if (useSTD3ASCIIRules == true)
            {
                // verify 3a and 3b
                if (srcIsLDH == false || /* source contains some non-LDH characters */
                    processOut[0] == HYPHEN ||
                    processOut[processOut.Length - 1] == HYPHEN)
                {
                    /* populate the parseError struct */
                    if (srcIsLDH == false)
                    {
                        throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules",
                                                           StringPrepErrorType.STD3ASCIIRulesError,
                                                           processOut.ToString(),
                                                           (failPos > 0) ? (failPos - 1) : failPos);
                    }
                    else if (processOut[0] == HYPHEN)
                    {
                        throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules",
                                                           StringPrepErrorType.STD3ASCIIRulesError, processOut.ToString(), 0);
                    }
                    else
                    {
                        throw new StringPrepParseException("The input does not conform to the STD 3 ASCII rules",
                                                           StringPrepErrorType.STD3ASCIIRulesError,
                                                           processOut.ToString(),
                                                           (poLen > 0) ? poLen - 1 : poLen);
                    }
                }
            }
            if (srcIsASCII)
            {
                dest = processOut;
            }
            else
            {
                // step 5 : verify the sequence does not begin with ACE prefix
                if (!StartsWithPrefix(processOut))
                {
                    //step 6: encode the sequence with punycode
                    StringBuffer punyout = PunycodeReference.Encode(processOut, caseFlags);

                    // convert all codepoints to lower case ASCII
                    StringBuffer lowerOut = ToASCIILower(punyout);

                    //Step 7: prepend the ACE prefix
                    dest.Append(ACE_PREFIX, 0, ACE_PREFIX_LENGTH - 0); // ICU4N: Checked 3rd parameter
                                                                       //Step 6: copy the contents in b2 into dest
                    dest.Append(lowerOut);
                }
                else
                {
                    throw new StringPrepParseException("The input does not start with the ACE Prefix.",
                                                       StringPrepErrorType.AcePrefixError, processOut.ToString(), 0);
                }
            }
            if (dest.Length > MAX_LABEL_LENGTH)
            {
                throw new StringPrepParseException("The labels in the input are too long. Length > 64.",
                                                   StringPrepErrorType.LabelTooLongError, dest.ToString(), 0);
            }
            return(dest);
        }