public static FullRouteIPInfoCache LoadFromFile(string filename)
        {
            Buf b = Buf.ReadFromFile(filename);

            b.SeekToBegin();

            return(LoadFromBuf(b));
        }
        // バッファに変換
        public Buf WriteToBuf()
        {
            Buf b = new Buf();

            // 要素数
            b.WriteInt((uint)elements.Count);

            // 要素
            foreach (Element e in elements)
            {
                e.WriteToBuf(b);
            }

            b.SeekToBegin();

            return(b);
        }
Example #3
0
        public Buf ToBuf()
        {
            string s = ToString();

            Buf b = new Buf();

            byte[] bom = Str.GetBOM(this.Encoding);

            if (bom != null)
            {
                b.Write(bom);
            }

            b.Write(encoding.GetBytes(s));

            b.SeekToBegin();

            return(b);
        }
        public static FullRouteIPInfoCache LoadFromBuf(Buf b)
        {
            b.Seek(b.Size - 20, SeekOrigin.Begin);
            byte[] hash = b.Read(20);
            b.SeekToBegin();
            byte[] hash2 = Secure.HashSHA1(Util.CopyByte(b.ByteData, 0, (int)b.Size - 20));

            if (Util.CompareByte(hash, hash2) == false)
            {
                throw new ApplicationException("Invalid Hash");
            }

            FullRouteIPInfoCache ret = new FullRouteIPInfoCache();

            ret.TimeStamp = new DateTime((long)b.ReadInt64());
            int num = (int)b.ReadInt();

            int i;

            for (i = 0; i < num; i++)
            {
                FullRouteIPInfoEntry e = new FullRouteIPInfoEntry();
                e.From        = b.ReadInt();
                e.To          = b.ReadInt();
                e.Registry    = b.ReadStr();
                e.Assigned    = b.ReadInt();
                e.Country2    = b.ReadStr();
                e.Country3    = b.ReadStr();
                e.CountryFull = DeleteSemi(b.ReadStr());
                ret.EntryList.Add(e);
            }

            ret.EntryList.Sort();

            ret.build_country_code_to_name_db();

            return(ret);
        }
        // ストリームから読み込み
        public static Buf ReadFromStream(Stream st)
        {
            Buf ret  = new Buf();
            int size = 32767;

            while (true)
            {
                byte[] tmp = new byte[size];
                int    i   = st.Read(tmp, 0, tmp.Length);

                if (i <= 0)
                {
                    break;
                }

                Array.Resize <byte>(ref tmp, i);

                ret.Write(tmp);
            }

            ret.SeekToBegin();

            return(ret);
        }
        public Buf SaveToBuf()
        {
            Buf b = new Buf();

            b.WriteInt64((ulong)this.TimeStamp.Ticks);
            b.WriteInt((uint)this.EntryList.Count);

            foreach (FullRouteIPInfoEntry e in this.EntryList)
            {
                b.WriteInt(e.From);
                b.WriteInt(e.To);
                b.WriteStr(e.Registry);
                b.WriteInt(e.Assigned);
                b.WriteStr(e.Country2);
                b.WriteStr(e.Country3);
                b.WriteStr(e.CountryFull);
            }

            b.Write(Secure.HashSHA1(b.ByteData));

            b.SeekToBegin();

            return(b);
        }