private static byte[] UrlEncodeNonAscii(byte[] bytes, int offset, int count) { int cNonAscii = 0; // count them first for (int i = 0; i < count; i++) { if (IsNonAsciiByte(bytes[offset + i])) { cNonAscii++; } } // nothing to expand? if (0u >= (uint)cNonAscii) { return(bytes); } // expand not 'safe' characters into %XX, spaces to +s byte[] expandedBytes = new byte[count + cNonAscii * 2]; int pos = 0; for (int i = 0; i < count; i++) { byte b = bytes[offset + i]; if (IsNonAsciiByte(b)) { expandedBytes[pos++] = (byte)'%'; expandedBytes[pos++] = (byte)HttpEncoderUtility.IntToHex((b >> 4) & 0xf); expandedBytes[pos++] = (byte)HttpEncoderUtility.IntToHex(b & 0x0f); } else { expandedBytes[pos++] = b; } } return(expandedBytes); }
private static byte[] UrlDecodeToBytesImpl(byte[] bytes, int offset, int count) { int decodedBytesCount = 0; byte[] decodedBytes = new byte[count]; for (int i = 0; i < count; i++) { int pos = offset + i; byte b = bytes[pos]; if (b == '+') { b = (byte)' '; } else if (b == '%' && i < count - 2) { int h1 = HttpEncoderUtility.HexToInt((char)bytes[pos + 1]); int h2 = HttpEncoderUtility.HexToInt((char)bytes[pos + 2]); if (h1 >= 0 && h2 >= 0) { // valid 2 hex chars b = (byte)((h1 << 4) | h2); i += 2; } } decodedBytes[decodedBytesCount++] = b; } if (decodedBytesCount < decodedBytes.Length) { byte[] newDecodedBytes = new byte[decodedBytesCount]; Array.Copy(decodedBytes, 0, newDecodedBytes, 0, decodedBytesCount); decodedBytes = newDecodedBytes; } return(decodedBytes); }
// This is the original UrlPathEncode(string) private static string UrlPathEncodeImpl(string value) { if (string.IsNullOrEmpty(value)) { return(value); } // recurse in case there is a query string int i = value.IndexOf('?'); if (i >= 0) { //#if NETCOREAPP_3_0_GREATER || NETSTANDARD_2_0_GREATER // return string.Concat(UrlPathEncodeImpl(value.Substring(0, i)), value.AsSpan(i)); //#else return(UrlPathEncodeImpl(value.Substring(0, i)) + value.Substring(i)); //#endif } // encode DBCS characters and spaces only return(HttpEncoderUtility.UrlEncodeSpaces(UrlEncodeNonAscii(value, Encoding.UTF8))); }
private static string UrlDecodeImpl(byte[] bytes, int offset, int count, Encoding encoding) { if (encoding is null) { encoding = Encoding.UTF8; } UrlDecoder helper = new UrlDecoder(count, encoding); // go through the bytes collapsing %XX and %uXXXX and appending // each byte as byte, with exception of %uXXXX constructs that // are appended as chars for (int i = 0; i < count; i++) { int pos = offset + i; byte b = bytes[pos]; // The code assumes that + and % cannot be in multibyte sequence if (b == '+') { b = (byte)' '; } else if (b == '%' && i < count - 2) { if (bytes[pos + 1] == 'u' && i < count - 5) { int h1 = HttpEncoderUtility.HexToInt((char)bytes[pos + 2]); int h2 = HttpEncoderUtility.HexToInt((char)bytes[pos + 3]); int h3 = HttpEncoderUtility.HexToInt((char)bytes[pos + 4]); int h4 = HttpEncoderUtility.HexToInt((char)bytes[pos + 5]); if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0) { // valid 4 hex chars char ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4); i += 5; // don't add as byte helper.AddChar(ch); continue; } } else { int h1 = HttpEncoderUtility.HexToInt((char)bytes[pos + 1]); int h2 = HttpEncoderUtility.HexToInt((char)bytes[pos + 2]); if (h1 >= 0 && h2 >= 0) { // valid 2 hex chars b = (byte)((h1 << 4) | h2); i += 2; } } } helper.AddByte(b); } return(Utf16StringValidator.ValidateString(helper.GetString())); }
private static string UrlDecodeImpl(string value, Encoding encoding) { if (value is null) { return(null); } int count = value.Length; UrlDecoder helper = new UrlDecoder(count, encoding); // go through the string's chars collapsing %XX and %uXXXX and // appending each char as char, with exception of %XX constructs // that are appended as bytes for (int pos = 0; pos < count; pos++) { char ch = value[pos]; if (ch == '+') { ch = ' '; } else if (ch == '%' && pos < count - 2) { if (value[pos + 1] == 'u' && pos < count - 5) { int h1 = HttpEncoderUtility.HexToInt(value[pos + 2]); int h2 = HttpEncoderUtility.HexToInt(value[pos + 3]); int h3 = HttpEncoderUtility.HexToInt(value[pos + 4]); int h4 = HttpEncoderUtility.HexToInt(value[pos + 5]); if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0) { // valid 4 hex chars ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4); pos += 5; // only add as char helper.AddChar(ch); continue; } } else { int h1 = HttpEncoderUtility.HexToInt(value[pos + 1]); int h2 = HttpEncoderUtility.HexToInt(value[pos + 2]); if (h1 >= 0 && h2 >= 0) { // valid 2 hex chars byte b = (byte)((h1 << 4) | h2); pos += 2; // don't add as char helper.AddByte(b); continue; } } } if (0u >= (uint)(ch & 0xFF80)) { helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode } else { helper.AddChar(ch); } } return(Utf16StringValidator.ValidateString(helper.GetString())); }
private static byte[] UrlEncodeToBytesImpl(byte[] bytes, int offset, int count) { 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 (0u >= (uint)cSpaces && 0u >= (uint)cUnsafe) { // DevDiv 912606: respect "offset" and "count" if (0 == offset && bytes.Length == count) { return(bytes); } else { byte[] 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); }