Utils.cs 3.6 KB

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