Beispiel #1
0
        public static bool IsPrime_M(BigInteger value)
        {
            if (value <= 1)
            {
                return(false);
            }

            if (value <= 3)
            {
                return(true);
            }

            if (value.IsEven)
            {
                return(false);
            }

            if (value < 100)
            {
                return(Consts.PRIMES_NN.Any(v => v == value));
            }

            int        valueScale = BigIntegerUtils.GetByteArrayLength(value);
            BigInteger d          = value >> 1;
            int        r          = 0;

            while (d.IsEven)
            {
                d >>= 1;
                r++;
            }

            // if n < 3,317,044,064,679,887,385,961,981, it is enough to test a = 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, and 41. @ wiki

            if (value < Consts.BIPXX)
            {
                foreach (int ix in new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41 })
                {
                    BigInteger x = new BigInteger(new byte[] { (byte)ix, 0x00 });

                    if (IsPrime_X(x, d, r, value) == false)
                    {
                        return(false);
                    }
                }
            }
            else
            {
                for (int k = 0; k < Ground.MillerRabin_K; k++)
                {
                    BigInteger x = new BigInteger(BinTools.Join(new byte[][] { SecurityTools.CRandom.GetBytes(valueScale + 10), new byte[] { 0x00 } })) % (value - 3) + 2;                     // 2 ~ (value - 2)

                    if (IsPrime_X(x, d, r, value) == false)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Beispiel #2
0
        //
        //	copied the source file by https://github.com/stackprobe/Factory/blob/master/SubTools/CopyLib.c
        //
        public static void INIT()
        {
            ReleaseMode = File.Exists(DDConsts.ResourceFile_01);

            if (ReleaseMode)
            {
                foreach (string resFile in new string[] { DDConsts.ResourceFile_01, DDConsts.ResourceFile_02 })
                {
                    List <ResInfo> resInfos = new List <ResInfo>();

                    using (FileStream reader = new FileStream(resFile, FileMode.Open, FileAccess.Read))
                    {
                        while (reader.Position < reader.Length)
                        {
                            int size = BinTools.ToInt(FileTools.Read(reader, 4));

                            if (size < 0)
                            {
                                throw new DDError();
                            }

                            resInfos.Add(new ResInfo()
                            {
                                ResFile = resFile,
                                Offset  = reader.Position,
                                Size    = size,
                            });

                            reader.Seek((long)size, SeekOrigin.Current);
                        }
                    }
                    string[] files = FileTools.TextToLines(StringTools.ENCODING_SJIS.GetString(LoadFile(resInfos[0])));

                    if (files.Length != resInfos.Count)
                    {
                        throw new DDError(files.Length + ", " + resInfos.Count);
                    }

                    for (int index = 1; index < files.Length; index++)
                    {
                        string file = files[index];

                        if (File2ResInfo.ContainsKey(file))
                        {
                            throw new DDError(file);
                        }

                        File2ResInfo.Add(file, resInfos[index]);
                    }
                }
            }
        }
        // memo:
        // value の最小の素因数が p のとき、ランダムに選ばれた r が p の倍数である確率は 1/p また p 以外の約数も発見出来る可能性がある。
        // なので、たかだか平均 p 回のトライで約数を発見出来る。
        // -- 合成数のとき更に素因数分解する必要がある。処理速度に貢献していないじゃないか?
        // -- 愚直に 2, 3, 5, 7, 9, ... と割って試していく方法の方が速いんじゃないか? <-- トライ毎に FF_GCD() に比べ 1回の剰余で済む。

        private static BigInteger FindFactor(BigInteger value)
        {
            if (Ground.IsStopped())
            {
                throw new Cancelled();
            }

            if (value < 2)
            {
                throw null;             // bugged !!!
            }
            if (value <= 3)             // 2, 3 are prime
            {
                goto retired;
            }

            int valueScale = BigIntegerUtils.GetByteArrayLength(value);

            if (valueScale < 1)
            {
                throw null;                 // souteigai !!!
            }
            for (int c = 0; c < 1000; c++)
            {
                // ここからトライ

                BigInteger r;

                if (valueScale < 100)
                {
                    r = new BigInteger(BinTools.Join(new byte[][] { SecurityTools.CRandom.GetBytes(valueScale + 10), new byte[] { 0x00 } })) % (value - 2) + 2;                     // 2 ~ (value - 1)
                }
                else
                {
                    r = new BigInteger(BinTools.Join(new byte[][] { SecurityTools.CRandom.GetBytes(valueScale / 2 + 10), new byte[] { 0x00 } })) + 2;                     // 2 ~ (about_sqrt(value))
                }
                BigInteger f = FF_GCD(value, r);

                if (f != 1)
                {
                    return(f);                    // トライ成功
                }
                // トライ失敗
            }

retired:
            throw new FF_Retired();
        }
Beispiel #4
0
        //
        //	copied the source file by https://github.com/stackprobe/Factory/blob/master/SubTools/CopyLib.c
        //
        public static void Load()
        {
            if (File.Exists(DDConsts.SaveDataFile) == false)
            {
                return;
            }

            byte[][] blocks = BinTools.Split(DDJammer.Decode(File.ReadAllBytes(DDConsts.SaveDataFile)));
            int      bc     = 0;

            string[] lines = DDUtils.Split(blocks[bc++]);
            int      c     = 0;

            if (lines[c++] != Program.APP_IDENT)
            {
                throw new DDError();
            }

            if (lines[c++] != Program.APP_TITLE)
            {
                throw new DDError();
            }

            // アプリのアップデートによって項目の更新・増減があっても処理を続行するように try ~ catch しておく。

            try             // Donut3 のセーブデータ
            {
                // TODO int.Parse -> IntTools.ToInt

                DDGround.RealScreen_W = int.Parse(lines[c++]);
                DDGround.RealScreen_H = int.Parse(lines[c++]);

                DDGround.RealScreenDraw_L = int.Parse(lines[c++]);
                DDGround.RealScreenDraw_T = int.Parse(lines[c++]);
                DDGround.RealScreenDraw_W = int.Parse(lines[c++]);
                DDGround.RealScreenDraw_H = int.Parse(lines[c++]);

                DDGround.MusicVolume = long.Parse(lines[c++]) / (double)IntTools.IMAX;
                DDGround.SEVolume    = long.Parse(lines[c++]) / (double)IntTools.IMAX;

                DDInput.DIR_2.BtnId = int.Parse(lines[c++]);
                DDInput.DIR_4.BtnId = int.Parse(lines[c++]);
                DDInput.DIR_6.BtnId = int.Parse(lines[c++]);
                DDInput.DIR_8.BtnId = int.Parse(lines[c++]);
                DDInput.A.BtnId     = int.Parse(lines[c++]);
                DDInput.B.BtnId     = int.Parse(lines[c++]);
                DDInput.C.BtnId     = int.Parse(lines[c++]);
                DDInput.D.BtnId     = int.Parse(lines[c++]);
                DDInput.E.BtnId     = int.Parse(lines[c++]);
                DDInput.F.BtnId     = int.Parse(lines[c++]);
                DDInput.L.BtnId     = int.Parse(lines[c++]);
                DDInput.R.BtnId     = int.Parse(lines[c++]);
                DDInput.PAUSE.BtnId = int.Parse(lines[c++]);
                DDInput.START.BtnId = int.Parse(lines[c++]);

                DDInput.DIR_2.KeyId = int.Parse(lines[c++]);
                DDInput.DIR_4.KeyId = int.Parse(lines[c++]);
                DDInput.DIR_6.KeyId = int.Parse(lines[c++]);
                DDInput.DIR_8.KeyId = int.Parse(lines[c++]);
                DDInput.A.KeyId     = int.Parse(lines[c++]);
                DDInput.B.KeyId     = int.Parse(lines[c++]);
                DDInput.C.KeyId     = int.Parse(lines[c++]);
                DDInput.D.KeyId     = int.Parse(lines[c++]);
                DDInput.E.KeyId     = int.Parse(lines[c++]);
                DDInput.F.KeyId     = int.Parse(lines[c++]);
                DDInput.L.KeyId     = int.Parse(lines[c++]);
                DDInput.R.KeyId     = int.Parse(lines[c++]);
                DDInput.PAUSE.KeyId = int.Parse(lines[c++]);
                DDInput.START.KeyId = int.Parse(lines[c++]);

                DDGround.RO_MouseDispMode = int.Parse(lines[c++]) != 0;

                // 新しい項目をここへ追加...
            }
            catch (Exception e)
            {
                ProcMain.WriteLog(e);
            }

            Load_Delay = () =>
            {
                lines = DDUtils.Split(blocks[bc++]);
                c     = 0;

                try                 // アプリ固有のセーブデータ
                {
                    // app > @ Load

                    //DDUtils.Noop(lines[c++]); // Dummy

                    Ground.I.MessageSpeed = IntTools.ToInt(lines[c++],
                                                           Charlotte.Games.GameConsts.MESSAGE_SPEED_MIN,
                                                           Charlotte.Games.GameConsts.MESSAGE_SPEED_MAX,
                                                           Charlotte.Games.GameConsts.MESSAGE_SPEED_DEF
                                                           );

                    // 新しい項目をここへ追加...

                    // < app
                }
                catch (Exception e)
                {
                    ProcMain.WriteLog(e);
                }

                Load_Delay = () => { };                 // reset
            };
        }
Beispiel #5
0
        //
        //	copied the source file by https://github.com/stackprobe/Factory/blob/master/SubTools/CopyLib.c
        //
        public static void Save()
        {
            List <byte[]> blocks = new List <byte[]>();

            // Donut3 用のセーブデータ
            {
                List <string> lines = new List <string>();

                lines.Add(Program.APP_IDENT);
                lines.Add(Program.APP_TITLE);

                lines.Add("" + DDGround.RealScreen_W);
                lines.Add("" + DDGround.RealScreen_H);

                lines.Add("" + DDGround.RealScreenDraw_L);
                lines.Add("" + DDGround.RealScreenDraw_T);
                lines.Add("" + DDGround.RealScreenDraw_W);
                lines.Add("" + DDGround.RealScreenDraw_H);

                lines.Add("" + DoubleTools.ToLong(DDGround.MusicVolume * IntTools.IMAX));
                lines.Add("" + DoubleTools.ToLong(DDGround.SEVolume * IntTools.IMAX));

                lines.Add("" + DDInput.DIR_2.BtnId);
                lines.Add("" + DDInput.DIR_4.BtnId);
                lines.Add("" + DDInput.DIR_6.BtnId);
                lines.Add("" + DDInput.DIR_8.BtnId);
                lines.Add("" + DDInput.A.BtnId);
                lines.Add("" + DDInput.B.BtnId);
                lines.Add("" + DDInput.C.BtnId);
                lines.Add("" + DDInput.D.BtnId);
                lines.Add("" + DDInput.E.BtnId);
                lines.Add("" + DDInput.F.BtnId);
                lines.Add("" + DDInput.L.BtnId);
                lines.Add("" + DDInput.R.BtnId);
                lines.Add("" + DDInput.PAUSE.BtnId);
                lines.Add("" + DDInput.START.BtnId);

                lines.Add("" + DDInput.DIR_2.KeyId);
                lines.Add("" + DDInput.DIR_4.KeyId);
                lines.Add("" + DDInput.DIR_6.KeyId);
                lines.Add("" + DDInput.DIR_8.KeyId);
                lines.Add("" + DDInput.A.KeyId);
                lines.Add("" + DDInput.B.KeyId);
                lines.Add("" + DDInput.C.KeyId);
                lines.Add("" + DDInput.D.KeyId);
                lines.Add("" + DDInput.E.KeyId);
                lines.Add("" + DDInput.F.KeyId);
                lines.Add("" + DDInput.L.KeyId);
                lines.Add("" + DDInput.R.KeyId);
                lines.Add("" + DDInput.PAUSE.KeyId);
                lines.Add("" + DDInput.START.KeyId);

                lines.Add("" + (DDGround.RO_MouseDispMode ? 1 : 0));

                // 新しい項目をここへ追加...

                blocks.Add(DDUtils.SplitableJoin(lines.ToArray()));
            }

            // アプリ固有のセーブデータ
            {
                List <string> lines = new List <string>();

                // app > @ Save

                //lines.Add("G4NovelAdv-Dummy"); // Dummy

                lines.Add("" + Ground.I.MessageSpeed);

                // 新しい項目をここへ追加...

                // < app

                blocks.Add(DDUtils.SplitableJoin(lines.ToArray()));
            }

            File.WriteAllBytes(DDConsts.SaveDataFile, DDJammer.Encode(BinTools.SplittableJoin(blocks.ToArray())));
        }
Beispiel #6
0
        //
        //	copied the source file by https://github.com/stackprobe/Factory/blob/master/SubTools/CopyLib.c
        //
        public static void Load()
        {
            if (File.Exists(DDConsts.SaveDataFile) == false)
            {
                return;
            }

            byte[][] blocks = BinTools.Split(DDJammer.Decode(File.ReadAllBytes(DDConsts.SaveDataFile)));
            int      bc     = 0;

            string[] lines = DDUtils.Split(blocks[bc++]);
            int      c     = 0;

            if (lines[c++] != Program.APP_IDENT)
            {
                throw new DDError();
            }

            if (lines[c++] != Program.APP_TITLE)
            {
                throw new DDError();
            }

            // 項目が増えた場合を想定して try ~ catch しておく。

            try             // for Donut2
            {
                // TODO int.Parse -> IntTools.ToInt

                DDGround.RealScreen_W = int.Parse(lines[c++]);
                DDGround.RealScreen_H = int.Parse(lines[c++]);

                DDGround.RealScreenDraw_L = int.Parse(lines[c++]);
                DDGround.RealScreenDraw_T = int.Parse(lines[c++]);
                DDGround.RealScreenDraw_W = int.Parse(lines[c++]);
                DDGround.RealScreenDraw_H = int.Parse(lines[c++]);

                DDGround.MusicVolume = long.Parse(lines[c++]) / (double)IntTools.IMAX;
                DDGround.SEVolume    = long.Parse(lines[c++]) / (double)IntTools.IMAX;

                DDInput.DIR_2.BtnId = int.Parse(lines[c++]);
                DDInput.DIR_4.BtnId = int.Parse(lines[c++]);
                DDInput.DIR_6.BtnId = int.Parse(lines[c++]);
                DDInput.DIR_8.BtnId = int.Parse(lines[c++]);
                DDInput.A.BtnId     = int.Parse(lines[c++]);
                DDInput.B.BtnId     = int.Parse(lines[c++]);
                DDInput.C.BtnId     = int.Parse(lines[c++]);
                DDInput.D.BtnId     = int.Parse(lines[c++]);
                DDInput.E.BtnId     = int.Parse(lines[c++]);
                DDInput.F.BtnId     = int.Parse(lines[c++]);
                DDInput.L.BtnId     = int.Parse(lines[c++]);
                DDInput.R.BtnId     = int.Parse(lines[c++]);
                DDInput.PAUSE.BtnId = int.Parse(lines[c++]);
                DDInput.START.BtnId = int.Parse(lines[c++]);

                DDInput.DIR_2.KeyId = int.Parse(lines[c++]);
                DDInput.DIR_4.KeyId = int.Parse(lines[c++]);
                DDInput.DIR_6.KeyId = int.Parse(lines[c++]);
                DDInput.DIR_8.KeyId = int.Parse(lines[c++]);
                DDInput.A.KeyId     = int.Parse(lines[c++]);
                DDInput.B.KeyId     = int.Parse(lines[c++]);
                DDInput.C.KeyId     = int.Parse(lines[c++]);
                DDInput.D.KeyId     = int.Parse(lines[c++]);
                DDInput.E.KeyId     = int.Parse(lines[c++]);
                DDInput.F.KeyId     = int.Parse(lines[c++]);
                DDInput.L.KeyId     = int.Parse(lines[c++]);
                DDInput.R.KeyId     = int.Parse(lines[c++]);
                DDInput.PAUSE.KeyId = int.Parse(lines[c++]);
                DDInput.START.KeyId = int.Parse(lines[c++]);

                DDGround.RO_MouseDispMode = int.Parse(lines[c++]) != 0;

                // 新しい項目をここへ追加...
            }
            catch (Exception e)
            {
                ProcMain.WriteLog(e);
            }

            Load_Delay = () =>
            {
                try                 // for app
                {
                    lines = DDUtils.Split(blocks[bc++]);

                    DDAdditionalEvents.Load(lines);
                }
                catch (Exception e)
                {
                    ProcMain.WriteLog(e);
                }

                Load_Delay = () => { };                 // reset
            };
        }
Beispiel #7
0
        //
        //	copied the source file by https://github.com/stackprobe/Factory/blob/master/SubTools/CopyLib.c
        //
        public static void Save()
        {
            List <byte[]> blocks = new List <byte[]>();

            // for Donut2
            {
                List <string> lines = new List <string>();

                lines.Add(Program.APP_IDENT);
                lines.Add(Program.APP_TITLE);

                lines.Add("" + DDGround.RealScreen_W);
                lines.Add("" + DDGround.RealScreen_H);

                lines.Add("" + DDGround.RealScreenDraw_L);
                lines.Add("" + DDGround.RealScreenDraw_T);
                lines.Add("" + DDGround.RealScreenDraw_W);
                lines.Add("" + DDGround.RealScreenDraw_H);

                lines.Add("" + DoubleTools.ToLong(DDGround.MusicVolume * IntTools.IMAX));
                lines.Add("" + DoubleTools.ToLong(DDGround.SEVolume * IntTools.IMAX));

                lines.Add("" + DDInput.DIR_2.BtnId);
                lines.Add("" + DDInput.DIR_4.BtnId);
                lines.Add("" + DDInput.DIR_6.BtnId);
                lines.Add("" + DDInput.DIR_8.BtnId);
                lines.Add("" + DDInput.A.BtnId);
                lines.Add("" + DDInput.B.BtnId);
                lines.Add("" + DDInput.C.BtnId);
                lines.Add("" + DDInput.D.BtnId);
                lines.Add("" + DDInput.E.BtnId);
                lines.Add("" + DDInput.F.BtnId);
                lines.Add("" + DDInput.L.BtnId);
                lines.Add("" + DDInput.R.BtnId);
                lines.Add("" + DDInput.PAUSE.BtnId);
                lines.Add("" + DDInput.START.BtnId);

                lines.Add("" + DDInput.DIR_2.KeyId);
                lines.Add("" + DDInput.DIR_4.KeyId);
                lines.Add("" + DDInput.DIR_6.KeyId);
                lines.Add("" + DDInput.DIR_8.KeyId);
                lines.Add("" + DDInput.A.KeyId);
                lines.Add("" + DDInput.B.KeyId);
                lines.Add("" + DDInput.C.KeyId);
                lines.Add("" + DDInput.D.KeyId);
                lines.Add("" + DDInput.E.KeyId);
                lines.Add("" + DDInput.F.KeyId);
                lines.Add("" + DDInput.L.KeyId);
                lines.Add("" + DDInput.R.KeyId);
                lines.Add("" + DDInput.PAUSE.KeyId);
                lines.Add("" + DDInput.START.KeyId);

                lines.Add("" + (DDGround.RO_MouseDispMode ? 1 : 0));

                // 新しい項目をここへ追加...

                blocks.Add(DDUtils.SplitableJoin(lines.ToArray()));
            }

            // for app
            {
                List <string> lines = new List <string>();

                DDAdditionalEvents.Save(lines);

                blocks.Add(DDUtils.SplitableJoin(lines.ToArray()));
            }

            File.WriteAllBytes(DDConsts.SaveDataFile, DDJammer.Encode(BinTools.SplittableJoin(blocks.ToArray())));
        }
        public ResourceClusterFile(string file)
        {
            this.ClusterFile = file;

            long fileSize = new FileInfo(file).Length;

            using (FileStream reader = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                byte[] signature = FileTools.Read(reader, SIGNATURE.Length);

                if (BinTools.Comp(signature, SIGNATURE) != 0)
                {
                    throw new GameError("Bad signature");
                }

                this.ResCount = BinTools.ToInt(FileTools.Read(reader, 4));

                if (this.ResCount < 0 || IntTools.IMAX < this.ResCount)
                {
                    throw new GameError("Bad ResCount");
                }

                this.ResStartPositions = new long[this.ResCount];
                this.ResSizes          = new int[this.ResCount];

                long count = SIGNATURE.Length + 4 + this.ResCount * 4;

                for (int index = 0; index < this.ResCount; index++)
                {
                    int resSize = BinTools.ToInt(FileTools.Read(reader, 4));

                    if (resSize < 0 || IntTools.IMAX < resSize)
                    {
                        throw new GameError("Bad resSize " + resSize);
                    }

                    this.ResStartPositions[index] = count;
                    this.ResSizes[index]          = resSize;

                    if (LongTools.IMAX_64 - count < (long)resSize)
                    {
                        throw new GameError("Bad resSize " + resSize + ", " + count);
                    }

                    count += (long)resSize;
                }
                if (count + 64L != fileSize)
                {
                    throw new GameError("Bad fileSize " + fileSize + ", " + count);
                }

                reader.Seek(0L, SeekOrigin.Begin);

                byte[] hash  = SecurityTools.GetSHA512(FileTools.Iterate(new LimitedReader(count, reader.Read).Read));
                byte[] hash2 = FileTools.Read(reader, 64);

                if (BinTools.Comp(hash, hash2) != 0)
                {
                    throw new GameError("Bad hash");
                }
            }
        }
Beispiel #9
0
 //
 //	copied the source file by https://github.com/stackprobe/Factory/blob/master/SubTools/CopyLib.c
 //
 public static string[] Split(byte[] data)
 {
     return(BinTools.Split(data).Select(bLine => Encoding.UTF8.GetString(bLine)).ToArray());
 }
Beispiel #10
0
        // < DX.*

        //
        //	copied the source file by https://github.com/stackprobe/Factory/blob/master/SubTools/CopyLib.c
        //
        public static byte[] SplitableJoin(string[] lines)
        {
            return(BinTools.SplittableJoin(lines.Select(line => Encoding.UTF8.GetBytes(line)).ToArray()));
        }
Beispiel #11
0
        public static string DecodeURL(string url)
        {
            byte[] bUrl = Encoding.ASCII.GetBytes(url);

            using (MemoryStream dest = new MemoryStream())
            {
                for (int index = 0; index < bUrl.Length; index++)
                {
                    if (bUrl[index] == 0x25)                     // ? '%'
                    {
                        dest.WriteByte((byte)Convert.ToInt32(Encoding.ASCII.GetString(BinTools.GetSubBytes(bUrl, index + 1, 2)), 16));
                        index += 2;
                    }
                    else if (bUrl[index] == 0x2b)                     // ? '+'
                    {
                        dest.WriteByte(0x20);                         // ' '
                    }
                    else
                    {
                        dest.WriteByte(bUrl[index]);
                    }
                }
                return(Encoding.UTF8.GetString(dest.ToArray()));
            }
        }