Example #1
0
            private byte[] UrlEncode(byte[] bytes, int offset, int count)
            {
                if (!ValidateUrlEncodingParameters(bytes, offset, count))
                {
                    return(null);
                }

                var cSpaces = 0;
                var cUnsafe = 0;

                // count them first
                for (var i = 0; i < count; i++)
                {
                    var ch = (char)bytes[offset + i];

                    if (ch == ' ')
                    {
                        cSpaces++;
                    }
                    else if (!HttpEncoderUtility.IsUrlSafeChar(ch))
                    {
                        cUnsafe++;
                    }
                }

                // nothing to expand?
                if (cSpaces == 0 && cUnsafe == 0)
                {
                    return(bytes);
                }

                // expand not 'safe' characters into %XX, spaces to +s
                var expandedBytes = new byte[count + cUnsafe * 2];
                var pos           = 0;

                for (var i = 0; i < count; i++)
                {
                    var b  = bytes[offset + i];
                    var ch = (char)b;

                    if (HttpEncoderUtility.IsUrlSafeChar(ch))
                    {
                        expandedBytes[pos++] = b;
                    }
                    else if (ch == ' ')
                    {
                        expandedBytes[pos++] = (byte)'+';
                    }
                    else
                    {
                        expandedBytes[pos++] = (byte)'%';
                        expandedBytes[pos++] = (byte)HttpEncoderUtility.IntToHex((b >> 4) & 0xf);
                        expandedBytes[pos++] = (byte)HttpEncoderUtility.IntToHex(b & 0x0f);
                    }
                }

                return(expandedBytes);
            }
Example #2
0
            internal string UrlEncodeUnicode(string value, bool ignoreAscii)
            {
                if (value == null)
                {
                    return(null);
                }

                var l  = value.Length;
                var sb = new StringBuilder(l);

                for (var i = 0; i < l; i++)
                {
                    var ch = value[i];

                    if ((ch & 0xff80) == 0)
                    {
                        // 7 bit?
                        if (ignoreAscii || HttpEncoderUtility.IsUrlSafeChar(ch))
                        {
                            sb.Append(ch);
                        }
                        else if (ch == ' ')
                        {
                            sb.Append('+');
                        }
                        else
                        {
                            sb.Append('%');
                            sb.Append(HttpEncoderUtility.IntToHex((ch >> 4) & 0xf));
                            sb.Append(HttpEncoderUtility.IntToHex(ch & 0xf));
                        }
                    }
                    else
                    {
                        // arbitrary Unicode?
                        sb.Append("%u");
                        sb.Append(HttpEncoderUtility.IntToHex((ch >> 12) & 0xf));
                        sb.Append(HttpEncoderUtility.IntToHex((ch >> 8) & 0xf));
                        sb.Append(HttpEncoderUtility.IntToHex((ch >> 4) & 0xf));
                        sb.Append(HttpEncoderUtility.IntToHex(ch & 0xf));
                    }
                }

                return(sb.ToString());
            }
        internal static byte[] UrlEncode(byte[] bytes, int offset, int count)
        {
            if (!ValidateUrlEncodingParameters(bytes, offset, count))
            {
                return(null);
            }

            int cSpaces = 0;
            int cUnsafe = 0;

            // count them first
            for (int i = 0; i < count; i++)
            {
                char ch = (char)bytes[offset + i];

                if (ch == ' ')
                {
                    cSpaces++;
                }
                else if (!HttpEncoderUtility.IsUrlSafeChar(ch))
                {
                    cUnsafe++;
                }
            }

            // nothing to expand?
            if (cSpaces == 0 && cUnsafe == 0)
            {
                // DevDiv 912606: respect "offset" and "count"
                if (0 == offset && bytes.Length == count)
                {
                    return(bytes);
                }
                else
                {
                    var subarray = new byte[count];
                    Buffer.BlockCopy(bytes, offset, subarray, 0, count);
                    return(subarray);
                }
            }

            // expand not 'safe' characters into %XX, spaces to +s
            byte[] expandedBytes = new byte[count + cUnsafe * 2];
            int    pos           = 0;

            for (int i = 0; i < count; i++)
            {
                byte b  = bytes[offset + i];
                char ch = (char)b;

                if (HttpEncoderUtility.IsUrlSafeChar(ch))
                {
                    expandedBytes[pos++] = b;
                }
                else if (ch == ' ')
                {
                    expandedBytes[pos++] = (byte)'+';
                }
                else
                {
                    expandedBytes[pos++] = (byte)'%';
                    expandedBytes[pos++] = (byte)HttpEncoderUtility.IntToHex((b >> 4) & 0xf);
                    expandedBytes[pos++] = (byte)HttpEncoderUtility.IntToHex(b & 0x0f);
                }
            }

            return(expandedBytes);
        }