WebHeaderEncoding.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. namespace SocketHttpListener.Net
  6. {
  7. // we use this static class as a helper class to encode/decode HTTP headers.
  8. // what we need is a 1-1 correspondence between a char in the range U+0000-U+00FF
  9. // and a byte in the range 0x00-0xFF (which is the range that can hit the network).
  10. // The Latin-1 encoding (ISO-88591-1) (GetEncoding(28591)) works for byte[] to string, but is a little slow.
  11. // It doesn't work for string -> byte[] because of best-fit-mapping problems.
  12. internal static class WebHeaderEncoding
  13. {
  14. // We don't want '?' replacement characters, just fail.
  15. private static readonly Encoding s_utf8Decoder = Encoding.GetEncoding("utf-8", EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
  16. internal static unsafe string GetString(byte[] bytes, int byteIndex, int byteCount)
  17. {
  18. fixed (byte* pBytes = bytes)
  19. return GetString(pBytes + byteIndex, byteCount);
  20. }
  21. internal static unsafe string GetString(byte* pBytes, int byteCount)
  22. {
  23. if (byteCount < 1)
  24. return "";
  25. string s = new string('\0', byteCount);
  26. fixed (char* pStr = s)
  27. {
  28. char* pString = pStr;
  29. while (byteCount >= 8)
  30. {
  31. pString[0] = (char)pBytes[0];
  32. pString[1] = (char)pBytes[1];
  33. pString[2] = (char)pBytes[2];
  34. pString[3] = (char)pBytes[3];
  35. pString[4] = (char)pBytes[4];
  36. pString[5] = (char)pBytes[5];
  37. pString[6] = (char)pBytes[6];
  38. pString[7] = (char)pBytes[7];
  39. pString += 8;
  40. pBytes += 8;
  41. byteCount -= 8;
  42. }
  43. for (int i = 0; i < byteCount; i++)
  44. {
  45. pString[i] = (char)pBytes[i];
  46. }
  47. }
  48. return s;
  49. }
  50. internal static int GetByteCount(string myString)
  51. {
  52. return myString.Length;
  53. }
  54. internal static unsafe void GetBytes(string myString, int charIndex, int charCount, byte[] bytes, int byteIndex)
  55. {
  56. if (myString.Length == 0)
  57. {
  58. return;
  59. }
  60. fixed (byte* bufferPointer = bytes)
  61. {
  62. byte* newBufferPointer = bufferPointer + byteIndex;
  63. int finalIndex = charIndex + charCount;
  64. while (charIndex < finalIndex)
  65. {
  66. *newBufferPointer++ = (byte)myString[charIndex++];
  67. }
  68. }
  69. }
  70. internal static unsafe byte[] GetBytes(string myString)
  71. {
  72. byte[] bytes = new byte[myString.Length];
  73. if (myString.Length != 0)
  74. {
  75. GetBytes(myString, 0, myString.Length, bytes, 0);
  76. }
  77. return bytes;
  78. }
  79. }
  80. }