Esempio n. 1
0
        /// <summary>
        ///     Preps a name according to the Stringprep profile defined in
        ///     RFC3491.
        ///     *
        /// </summary>
        /// <param name="input">
        ///     the name to prep.
        /// </param>
        /// <param name="allowUnassigned">
        ///     true if the name may contain unassigned
        ///     code points.
        /// </param>
        /// <returns>
        ///     the prepped name.
        ///     @throws StringprepException If the name cannot be prepped with
        ///     this profile.
        ///     @throws NullPointerException If the name is null.
        /// </returns>
        public static string NamePrep(string input, bool allowUnassigned)
        {
            if (input == null)
            {
                throw new NullReferenceException();
            }

            var s = new StringBuilder(input);

            if (!allowUnassigned && Contains(s, RFC3454.A1))
            {
                throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED);
            }

            Filter(s, RFC3454.B1);
            Map(s, RFC3454.B2search, RFC3454.B2replace);

            s = new StringBuilder(NFKC.NormalizeNFKC(s.ToString()));
            // B.3 is only needed if NFKC is not used, right?
            // map(s, RFC3454.B3search, RFC3454.B3replace);

            if (Contains(s, RFC3454.C12) || Contains(s, RFC3454.C22) || Contains(s, RFC3454.C3) ||
                Contains(s, RFC3454.C4) || Contains(s, RFC3454.C5) || Contains(s, RFC3454.C6) || Contains(s, RFC3454.C7) ||
                Contains(s, RFC3454.C8))
            {
                // Table C.9 only contains code points > 0xFFFF which Java
                // doesn't handle
                throw new StringprepException(StringprepException.CONTAINS_PROHIBITED);
            }

            // Bidi handling
            var r = Contains(s, RFC3454.D1);
            var l = Contains(s, RFC3454.D2);

            // RFC 3454, section 6, requirement 1: already handled above (table C.8)

            // RFC 3454, section 6, requirement 2
            if (r && l)
            {
                throw new StringprepException(StringprepException.BIDI_BOTHRAL);
            }

            // RFC 3454, section 6, requirement 3
            if (r)
            {
                if (!Contains(s[0], RFC3454.D1) || !Contains(s[s.Length - 1], RFC3454.D1))
                {
                    throw new StringprepException(StringprepException.BIDI_LTRAL);
                }
            }

            return(s.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Preps a resource name according to the Stringprep profile defined
        /// in RFC3920.
        /// </summary>
        /// <param name="input">the resource name to prep.
        /// </param>
        /// <param name="allowUnassigned">true if the resource name may contain
        /// unassigned code points.
        /// </param>
        /// <returns>
        /// the prepped node name.
        /// @throws StringprepException If the resource name cannot be prepped
        /// with this profile.
        /// @throws NullPointerException If the resource name is null.
        ///
        /// </returns>
        public static string ResourcePrep(string input, bool allowUnassigned)
        {
            if (input == null)
            {
                throw new System.NullReferenceException();
            }

            StringBuilder s = new StringBuilder(input);

            if (!allowUnassigned && Contains(s, RFC3454.A1))
            {
                throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED);
            }

            Filter(s, RFC3454.B1);

            s = new StringBuilder(NFKC.NormalizeNFKC(s.ToString()));

            if (Contains(s, RFC3454.C12) || Contains(s, RFC3454.C21) || Contains(s, RFC3454.C22) || Contains(s, RFC3454.C3) || Contains(s, RFC3454.C4) || Contains(s, RFC3454.C5) || Contains(s, RFC3454.C6) || Contains(s, RFC3454.C7) || Contains(s, RFC3454.C8))
            {
                // Table C.9 only contains code points > 0xFFFF which Java
                // doesn't handle
                throw new StringprepException(StringprepException.CONTAINS_PROHIBITED);
            }

            // Bidi handling
            bool r = Contains(s, RFC3454.D1);
            bool l = Contains(s, RFC3454.D2);

            // RFC 3454, section 6, requirement 1: already handled above (table C.8)

            // RFC 3454, section 6, requirement 2
            if (r && l)
            {
                throw new StringprepException(StringprepException.BIDI_BOTHRAL);
            }

            // RFC 3454, section 6, requirement 3
            if (r)
            {
                if (!Contains(s[0], RFC3454.D1) || !Contains(s[s.Length - 1], RFC3454.D1))
                {
                    throw new StringprepException(StringprepException.BIDI_LTRAL);
                }
            }

            return(s.ToString());
        }