diff --git a/maker/csharp/DbMaker/HeaderBlock.cs b/maker/csharp/DbMaker/HeaderBlock.cs new file mode 100644 index 0000000..b561e0f --- /dev/null +++ b/maker/csharp/DbMaker/HeaderBlock.cs @@ -0,0 +1,57 @@ +namespace DbMaker +{ + /// + /// header block class + /// + public class HeaderBlock + { + public HeaderBlock(long indexStartIp, int indexPtr) + { + IndexStartIp = indexStartIp; + IndexPtr = indexPtr; + } + + /// + /// index block start ip address + /// + public long IndexStartIp { get; set; } + + /// + /// ip address + /// + public int IndexPtr { get; set; } + + public HeaderBlock SetIndexStartIp(long indexStartIp) + { + IndexStartIp = indexStartIp; + return this; + } + + public HeaderBlock SetIndexPtr(int indexPtr) + { + IndexPtr = indexPtr; + return this; + } + + /// + /// get the bytes for db storage + /// + /// + public byte[] GetBytes() + { + /* + * +------------+-----------+ + * | 4bytes | 4bytes | + * +------------+-----------+ + * start ip index ptr + */ + + var b = new byte[8]; + + Util.writeIntLong(b, 0, IndexStartIp); + Util.writeIntLong(b, 4, IndexPtr); + + return b; + } + } +} \ No newline at end of file diff --git a/maker/csharp/DbMaker/IndexBlock.cs b/maker/csharp/DbMaker/IndexBlock.cs new file mode 100644 index 0000000..b456126 --- /dev/null +++ b/maker/csharp/DbMaker/IndexBlock.cs @@ -0,0 +1,92 @@ +namespace DbMaker +{ + /// + /// item index class + /// + public class IndexBlock + { + private static int LENGTH = 12; + + public IndexBlock(long startIp, long endIp, int dataPtr, int dataLen) + { + StartIp = startIp; + EndIp = endIp; + DataPtr = dataPtr; + DataLen = dataLen; + } + + /** + * start ip address + */ + public long StartIp { get; set; } + + /** + * end ip address + */ + public long EndIp { get; set; } + + /** + * data ptr and data length + */ + public int DataPtr { get; set; } + + /** + * data length + */ + public int DataLen { get; set; } + + public IndexBlock SetStartIp(long startIp) + { + StartIp = startIp; + return this; + } + + public IndexBlock SetEndIp(long endIp) + { + EndIp = endIp; + return this; + } + + public IndexBlock SetDataPtr(int dataPtr) + { + DataPtr = dataPtr; + return this; + } + + public IndexBlock SetDataLen(int dataLen) + { + DataLen = dataLen; + return this; + } + + public static int GetIndexBlockLength() + { + return LENGTH; + } + + /** + * get the bytes for storage + * + * @return byte[] + */ + public byte[] GetBytes() + { + /* + * +------------+-----------+-----------+ + * | 4bytes | 4bytes | 4bytes | + * +------------+-----------+-----------+ + * start ip end ip data ptr + len + */ + var b = new byte[12]; + + Util.writeIntLong(b, 0, StartIp); //start ip + Util.writeIntLong(b, 4, EndIp); //end ip + + //write the data ptr and the length + var mix = DataPtr | DataLen << 24 & 0xFF000000L; + Util.writeIntLong(b, 8, mix); + + return b; + } + } +} \ No newline at end of file diff --git a/maker/csharp/DbMaker/Util.cs b/maker/csharp/DbMaker/Util.cs new file mode 100644 index 0000000..60e493e --- /dev/null +++ b/maker/csharp/DbMaker/Util.cs @@ -0,0 +1,143 @@ +using System; +using System.Collections.Generic; +using System.Net; +using System.Text; + +namespace DbMaker +{ + /// + /// util class + /// + public static class Util + { + /** + * write specfield bytes to a byte array start from offset + * + * @param b + * @param offset + * @param v + * @param bytes + */ + public static void write(byte[] b, int offset, long v, int bytes) + { + for (int i = 0; i < bytes; i++) + { + b[offset++] = (byte)((v >> (8 * i)) & 0xFF); + } + } + + /** + * write a int to a byte array + * + * @param b + * @param offet + * @param v + */ + public static void writeIntLong(byte[] b, int offset, long v) + { + b[offset++] = (byte)((v >> 0) & 0xFF); + b[offset++] = (byte)((v >> 8) & 0xFF); + b[offset++] = (byte)((v >> 16) & 0xFF); + b[offset] = (byte)((v >> 24) & 0xFF); + } + + /** + * get a int from a byte array start from the specifiled offset + * + * @param b + * @param offset + */ + public static long getIntLong(byte[] b, int offset) + { + return ( + ((b[offset++] & 0x000000FFL)) | + ((b[offset++] << 8) & 0x0000FF00L) | + ((b[offset++] << 16) & 0x00FF0000L) | + ((b[offset] << 24) & 0xFF000000L) + ); + } + + /** + * get a int from a byte array start from the specifield offset + * + * @param b + * @param offset + */ + public static int getInt3(byte[] b, int offset) + { + return ( + (b[offset++] & 0x000000FF) | + (b[offset++] & 0x0000FF00) | + (b[offset] & 0x00FF0000) + ); + } + + public static int getInt2(byte[] b, int offset) + { + return ( + (b[offset++] & 0x000000FF) | + (b[offset] & 0x0000FF00) + ); + } + + public static int getInt1(byte[] b, int offset) + { + return ( + (b[offset] & 0x000000FF) + ); + } + + /** + * string ip to long ip + * + * @param ip + * @return long + */ + public static long ip2long(String ip) + { + IPAddress ipaddress = IPAddress.Parse(ip); + byte[] addbuffer = ipaddress.GetAddressBytes(); + Array.Reverse(addbuffer); + return BitConverter.ToInt64(addbuffer, 0); + } + + /** + * int to ip string + * + * @param ip + * @return string + */ + public static String long2ip(long ip) + { + StringBuilder sb = new StringBuilder(); + + sb + .Append((ip >> 24) & 0xFF).Append('.') + .Append((ip >> 16) & 0xFF).Append('.') + .Append((ip >> 8) & 0xFF).Append('.') + .Append((ip >> 0) & 0xFF); + + return sb.ToString(); + } + + /** + * check the validate of the specifeld ip address + * + * @param ip + * @return boolean + */ + public static Boolean isIpAddress(String ip) + { + String[] p = ip.Split("\\."); + if (p.Length != 4) return false; + + foreach (var pp in p) + { + if (pp.Length > 3) return false; + int val = Convert.ToInt32(pp); //Integer.valueOf(pp); + if (val > 255) return false; + } + return true; + } + } +}