Utils.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. //*******************************
  2. // Created By Rocher Kong
  3. // Github https://github.com/RocherKong
  4. // Date 2018.02.09
  5. //*******************************
  6. using System;
  7. namespace IP2Region
  8. {
  9. public class IPInValidException : Exception
  10. {
  11. const string ERROR_MSG = "IP Illigel. Please input a valid IP.";
  12. public IPInValidException() : base(ERROR_MSG) { }
  13. }
  14. internal static class Utils
  15. {
  16. /// <summary>
  17. /// Write specfield bytes to a byte array start from offset.
  18. /// </summary>
  19. public static void Write(byte[] b, int offset, ulong v, int bytes)
  20. {
  21. for (int i = 0; i < bytes; i++)
  22. {
  23. b[offset++] = (byte)((v >> (8 * i)) & 0xFF);
  24. }
  25. }
  26. /// <summary>
  27. /// Write a int to a byte array.
  28. /// </summary>
  29. public static void WriteIntLong(byte[] b, int offset, long v)
  30. {
  31. b[offset++] = (byte)((v >> 0) & 0xFF);
  32. b[offset++] = (byte)((v >> 8) & 0xFF);
  33. b[offset++] = (byte)((v >> 16) & 0xFF);
  34. b[offset] = (byte)((v >> 24) & 0xFF);
  35. }
  36. /// <summary>
  37. /// Get a int from a byte array start from the specifiled offset.
  38. /// </summary>
  39. public static long GetIntLong(byte[] b, int offset)
  40. {
  41. return (
  42. ((b[offset++] & 0x000000FFL)) |
  43. ((b[offset++] << 8) & 0x0000FF00L) |
  44. ((b[offset++] << 16) & 0x00FF0000L) |
  45. ((b[offset] << 24) & 0xFF000000L)
  46. );
  47. }
  48. /// <summary>
  49. /// Get a int from a byte array start from the specifield offset.
  50. /// </summary>
  51. public static int GetInt3(byte[] b, int offset)
  52. {
  53. return (
  54. (b[offset++] & 0x000000FF) |
  55. (b[offset++] & 0x0000FF00) |
  56. (b[offset] & 0x00FF0000)
  57. );
  58. }
  59. public static int GetInt2(byte[] b, int offset)
  60. {
  61. return (
  62. (b[offset++] & 0x000000FF) |
  63. (b[offset] & 0x0000FF00)
  64. );
  65. }
  66. public static int GetInt1(byte[] b, int offset)
  67. {
  68. return (
  69. (b[offset] & 0x000000FF)
  70. );
  71. }
  72. /// <summary>
  73. /// String ip to long ip.
  74. /// </summary>
  75. public static long Ip2long(string ip)
  76. {
  77. string[] p = ip.Split('.');
  78. if (p.Length != 4) throw new IPInValidException();
  79. foreach (string pp in p)
  80. {
  81. if (pp.Length > 3) throw new IPInValidException();
  82. if (!int.TryParse(pp, out int value) || value > 255)
  83. {
  84. throw new IPInValidException();
  85. }
  86. }
  87. var bip1 = long.TryParse(p[0], out long ip1);
  88. var bip2 = long.TryParse(p[1], out long ip2);
  89. var bip3 = long.TryParse(p[2], out long ip3);
  90. var bip4 = long.TryParse(p[3], out long ip4);
  91. if (!bip1 || !bip2 || !bip3 || !bip4
  92. || ip4 > 255 || ip1 > 255 || ip2 > 255 || ip3 > 255
  93. || ip4 < 0 || ip1 < 0 || ip2 < 0 || ip3 < 0)
  94. {
  95. throw new IPInValidException();
  96. }
  97. long p1 = ((ip1 << 24) & 0xFF000000);
  98. long p2 = ((ip2 << 16) & 0x00FF0000);
  99. long p3 = ((ip3 << 8) & 0x0000FF00);
  100. long p4 = ((ip4 << 0) & 0x000000FF);
  101. return ((p1 | p2 | p3 | p4) & 0xFFFFFFFFL);
  102. }
  103. /// <summary>
  104. /// Int to ip string.
  105. /// </summary>
  106. public static string Long2ip(long ip)
  107. {
  108. return $"{(ip >> 24) & 0xFF}.{(ip >> 16) & 0xFF}.{(ip >> 8) & 0xFF}.{ip & 0xFF}";
  109. }
  110. }
  111. }