Beispiel #1
0
        public void append(object data, Int32 nbytes)
        {
            Ptr <byte> p      = new Ptr <byte>((byte[])data, 0);
            int        left   = nbytes;
            int        offset = (Int32)((count[0] >> 3) & 63);
            UInt32     nbits  = (UInt32)(nbytes << 3);

            if (nbytes <= 0)
            {
                return;
            }

            /* Update the message length. */
            count[1] += (UInt32)(nbytes >> 29);
            count[0] += nbits;
            if (count[0] < nbits)
            {
                count[1]++;
            }

            /* Process an initial partial block. */
            Ptr <byte> pBuf = new Ptr <byte>(buf, 0);

            if (offset != 0)
            {
                int copy = (offset + nbytes > 64) ? (64 - offset) : nbytes;
                pBuf.AddPtr(offset);
                mem.memcpy(ref pBuf, p, copy);
                pBuf.AddPtr(-offset);
                if (offset + copy < 64)
                {
                    return;
                }
                p.AddPtr(copy);
                left -= copy;
                process(pBuf);
            }

            /* Process full blocks. */
            for (; left >= 64; p.AddPtr(64), left -= 64)
            {
                process(p);
            }

            /* Process a final partial block. */
            if (left != 0)
            {
                mem.memcpy(ref pBuf, p, left);
            }
        }
Beispiel #2
0
        protected string petsciiToAscii(ref Ptr <byte> spPet)
        {
            List <byte> buffer = new List <byte>();

            do
            {
                byte petsciiChar = spPet.buf[spPet.ptr];
                spPet.AddPtr(1);

                if ((petsciiChar == 0x00) || (petsciiChar == 0x0d))
                {
                    break;
                }

                // If character is 0x9d (left arrow key) then move back.
                if ((petsciiChar == 0x9d) && buffer.Count != 0)
                {
                    buffer.RemoveAt(buffer.Count - 1);
                }
                else
                {
                    // ASCII CHR$ conversion
                    byte asciiChar = CHR_tab[petsciiChar];
                    if ((asciiChar >= 0x20) && (buffer.Count <= 31))
                    {
                        buffer.Add(asciiChar);
                    }
                }
            }while (spPet.buf.Length > spPet.ptr);

            return(Encoding.ASCII.GetString(buffer.ToArray()));
        }
Beispiel #3
0
        private Ptr <byte> reloc_globals(Ptr <byte> buf)
        {
            int n = getWord(buf, 0);

            buf.AddPtr(2);

            while (n != 0)
            {
                while (buf[buf.ptr] != 0)
                {
                    buf.AddPtr(1);
                }
                byte  seg    = buf[buf.ptr];
                Int32 oldVal = getWord(buf, 1);
                Int32 newVal = oldVal + reldiff(seg);
                setWord(buf, 1, newVal);
                buf.AddPtr(3);
                n--;
            }

            return(buf);
        }
Beispiel #4
0
        // ----------------------------------------------------------------------------
        // Evaluation of complete interpolating function.
        // Note that since each curve segment is controlled by four points, the
        // end points will not be interpolated. If extra control points are not
        // desirable, the end points can simply be repeated to ensure interpolation.
        // Note also that points of non-differentiability and discontinuity can be
        // introduced by repeating points.
        // ----------------------------------------------------------------------------
        //template<class PointIter, class PointPlotter>
        //public void interpolate(PointIter p0, PointIter pn, PointPlotter plot, double res)
        //{
        //    double k1, k2;

        //    // Set up points for first curve segment.
        //    PointIter p1 = p0; ++p1;
        //    PointIter p2 = p1; ++p2;
        //    PointIter p3 = p2; ++p3;

        //    // Draw each curve segment.
        //    for (; p2 != pn; ++p0, ++p1, ++p2, ++p3)
        //    {
        //        // p1 and p2 equal; single point.
        //        if (x(p1) == x(p2))
        //        {
        //            continue;
        //        }
        //        // Both end points repeated; straight line.
        //        if (x(p0) == x(p1) && x(p2) == x(p3))
        //        {
        //            k1 = k2 = (y(p2) - y(p1)) / (x(p2) - x(p1));
        //        }
        //        // p0 and p1 equal; use f''(x1) = 0.
        //        else if (x(p0) == x(p1))
        //        {
        //            k2 = (y(p3) - y(p1)) / (x(p3) - x(p1));
        //            k1 = (3 * (y(p2) - y(p1)) / (x(p2) - x(p1)) - k2) / 2;
        //        }
        //        // p2 and p3 equal; use f''(x2) = 0.
        //        else if (x(p2) == x(p3))
        //        {
        //            k1 = (y(p2) - y(p0)) / (x(p2) - x(p0));
        //            k2 = (3 * (y(p2) - y(p1)) / (x(p2) - x(p1)) - k1) / 2;
        //        }
        //        // Normal curve.
        //        else
        //        {
        //            k1 = (y(p2) - y(p0)) / (x(p2) - x(p0));
        //            k2 = (y(p3) - y(p1)) / (x(p3) - x(p1));
        //        }

        //        interpolate_segment(x(p1), y(p1), x(p2), y(p2), k1, k2, plot, res);
        //    }
        //}

        public void interpolate(double[][] sp0, int sptrPn, int[] splot, double res)
        {
            double         k1, k2;
            Ptr <double[]> p0 = new Ptr <double[]>(sp0, 0);
            Ptr <double[]> pn = new Ptr <double[]>(sp0, sptrPn);
            Ptr <double[]> p1 = new Ptr <double[]>(sp0, 1);
            Ptr <double[]> p2 = new Ptr <double[]>(sp0, 2);
            Ptr <double[]> p3 = new Ptr <double[]>(sp0, 3);

            // Set up points for first curve segment.
            //PointIter p1 = p0; ++p1;
            //PointIter p2 = p1; ++p2;
            //PointIter p3 = p2; ++p3;

            // Draw each curve segment.
            for (; p2.ptr != pn.ptr; p0.AddPtr(1), p1.AddPtr(1), p2.AddPtr(1), p3.AddPtr(1))
            {
                // p1 and p2 equal; single point.
                if (x(p1) == x(p2))
                {
                    continue;
                }
                // Both end points repeated; straight line.
                if (x(p0) == x(p1) && x(p2) == x(p3))
                {
                    k1 = k2 = (y(p2) - y(p1)) / (x(p2) - x(p1));
                }
                // p0 and p1 equal; use f''(x1) = 0.
                else if (x(p0) == x(p1))
                {
                    k2 = (y(p3) - y(p1)) / (x(p3) - x(p1));
                    k1 = (3 * (y(p2) - y(p1)) / (x(p2) - x(p1)) - k2) / 2;
                }
                // p2 and p3 equal; use f''(x2) = 0.
                else if (x(p2) == x(p3))
                {
                    k1 = (y(p2) - y(p0)) / (x(p2) - x(p0));
                    k2 = (3 * (y(p2) - y(p1)) / (x(p2) - x(p1)) - k1) / 2;
                }
                // Normal curve.
                else
                {
                    k1 = (y(p2) - y(p0)) / (x(p2) - x(p0));
                    k2 = (y(p3) - y(p1)) / (x(p3) - x(p1));
                }

                //interpolate_segment(x(p1), y(p1), x(p2), y(p2), k1, k2, splot, res);
                interpolate_forward_difference(x(p1), y(p1), x(p2), y(p2), k1, k2, splot, res);
            }
        }
Beispiel #5
0
        private void process(Ptr <byte> data)//[64])
        {
            UInt32 a = abcd[0], b = abcd[1], c = abcd[2], d = abcd[3];

#if MD5_WORDS_BIG_ENDIAN
            /*
             * On big-endian machines, we must arrange the bytes in the right
             * order.  (This also works on machines of unknown byte order.)
             */
            Ptr <byte> xp = new Ptr <byte>(data, 0);
            for (int i = 0; i < 16; ++i, xp.AddPtr(4))
            {
                tmpBuf[i] = (UInt32)((xp[0] & 0xFF) + ((xp[1] & 0xFF) << 8) +
                                     ((xp[2] & 0xFF) << 16) + ((xp[3] & 0xFF) << 24));
            }
            X = tmpBuf;
#else
            /* !MD5_IS_BIG_ENDIAN */

            /*
             * On little-endian machines, we can process properly aligned data
             * without copying it.
             */
            //if (((data - (byte[])0) & 3) == 0)
            if ((data[0] & 3) == 0)

            {
                /* data are properly aligned */
                X = new uint[data.buf.Length / 4];
                for (int i = 0; i < 64 / 4; i++)
                {
                    X[i] = (UInt32)((data[3 + i * 4] & 0xFF) + ((data[2 + i * 4] & 0xFF) << 8) +
                                    ((data[1 + i * 4] & 0xFF) << 16) + ((data[0 + i * 4] & 0xFF) << 24));
                }
            }
            else
            {
                /* not aligned */
                //mem.memcpy(ref tmpBuf, data, 64);
                //tmpBuf = new uint[data.buf.Length / 4];
                for (int i = 0; i < 64 / 4; i++)
                {
                    tmpBuf[i] = (UInt32)((data[3 + i * 4] & 0xFF) + ((data[2 + i * 4] & 0xFF) << 8) +
                                         ((data[1 + i * 4] & 0xFF) << 16) + ((data[0 + i * 4] & 0xFF) << 24));
                }
                X = tmpBuf;
            }
#endif

            /* Round 1. */

            /* Let [abcd k s i] denote the operation
             * a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */
            /* Do the following 16 operations. */
            SET(F, ref a, ref b, ref c, ref d, 0, 7, T1);
            SET(F, ref d, ref a, ref b, ref c, 1, 12, T2);
            SET(F, ref c, ref d, ref a, ref b, 2, 17, T3);
            SET(F, ref b, ref c, ref d, ref a, 3, 22, T4);
            SET(F, ref a, ref b, ref c, ref d, 4, 7, T5);
            SET(F, ref d, ref a, ref b, ref c, 5, 12, T6);
            SET(F, ref c, ref d, ref a, ref b, 6, 17, T7);
            SET(F, ref b, ref c, ref d, ref a, 7, 22, T8);
            SET(F, ref a, ref b, ref c, ref d, 8, 7, T9);
            SET(F, ref d, ref a, ref b, ref c, 9, 12, T10);
            SET(F, ref c, ref d, ref a, ref b, 10, 17, T11);
            SET(F, ref b, ref c, ref d, ref a, 11, 22, T12);
            SET(F, ref a, ref b, ref c, ref d, 12, 7, T13);
            SET(F, ref d, ref a, ref b, ref c, 13, 12, T14);
            SET(F, ref c, ref d, ref a, ref b, 14, 17, T15);
            SET(F, ref b, ref c, ref d, ref a, 15, 22, T16);

            /* Round 2. */

            /* Let [abcd k s i] denote the operation
             *   a = b + ((a + G(b,c,d) + X[k] + T[i]) <<< s). */
            /* Do the following 16 operations. */
            SET(G, ref a, ref b, ref c, ref d, 1, 5, T17);
            SET(G, ref d, ref a, ref b, ref c, 6, 9, T18);
            SET(G, ref c, ref d, ref a, ref b, 11, 14, T19);
            SET(G, ref b, ref c, ref d, ref a, 0, 20, T20);
            SET(G, ref a, ref b, ref c, ref d, 5, 5, T21);
            SET(G, ref d, ref a, ref b, ref c, 10, 9, T22);
            SET(G, ref c, ref d, ref a, ref b, 15, 14, T23);
            SET(G, ref b, ref c, ref d, ref a, 4, 20, T24);
            SET(G, ref a, ref b, ref c, ref d, 9, 5, T25);
            SET(G, ref d, ref a, ref b, ref c, 14, 9, T26);
            SET(G, ref c, ref d, ref a, ref b, 3, 14, T27);
            SET(G, ref b, ref c, ref d, ref a, 8, 20, T28);
            SET(G, ref a, ref b, ref c, ref d, 13, 5, T29);
            SET(G, ref d, ref a, ref b, ref c, 2, 9, T30);
            SET(G, ref c, ref d, ref a, ref b, 7, 14, T31);
            SET(G, ref b, ref c, ref d, ref a, 12, 20, T32);

            /* Round 3. */

            /* Let [abcd k s t] denote the operation
             *   a = b + ((a + H(b,c,d) + X[k] + T[i]) <<< s). */
            /* Do the following 16 operations. */
            SET(H, ref a, ref b, ref c, ref d, 5, 4, T33);
            SET(H, ref d, ref a, ref b, ref c, 8, 11, T34);
            SET(H, ref c, ref d, ref a, ref b, 11, 16, T35);
            SET(H, ref b, ref c, ref d, ref a, 14, 23, T36);
            SET(H, ref a, ref b, ref c, ref d, 1, 4, T37);
            SET(H, ref d, ref a, ref b, ref c, 4, 11, T38);
            SET(H, ref c, ref d, ref a, ref b, 7, 16, T39);
            SET(H, ref b, ref c, ref d, ref a, 10, 23, T40);
            SET(H, ref a, ref b, ref c, ref d, 13, 4, T41);
            SET(H, ref d, ref a, ref b, ref c, 0, 11, T42);
            SET(H, ref c, ref d, ref a, ref b, 3, 16, T43);
            SET(H, ref b, ref c, ref d, ref a, 6, 23, T44);
            SET(H, ref a, ref b, ref c, ref d, 9, 4, T45);
            SET(H, ref d, ref a, ref b, ref c, 12, 11, T46);
            SET(H, ref c, ref d, ref a, ref b, 15, 16, T47);
            SET(H, ref b, ref c, ref d, ref a, 2, 23, T48);

            /* Round 4. */

            /* Let [abcd k s t] denote the operation
             *   a = b + ((a + I(b,c,d) + X[k] + T[i]) <<< s). */
            /* Do the following 16 operations. */
            SET(I, ref a, ref b, ref c, ref d, 0, 6, T49);
            SET(I, ref d, ref a, ref b, ref c, 7, 10, T50);
            SET(I, ref c, ref d, ref a, ref b, 14, 15, T51);
            SET(I, ref b, ref c, ref d, ref a, 5, 21, T52);
            SET(I, ref a, ref b, ref c, ref d, 12, 6, T53);
            SET(I, ref d, ref a, ref b, ref c, 3, 10, T54);
            SET(I, ref c, ref d, ref a, ref b, 10, 15, T55);
            SET(I, ref b, ref c, ref d, ref a, 1, 21, T56);
            SET(I, ref a, ref b, ref c, ref d, 8, 6, T57);
            SET(I, ref d, ref a, ref b, ref c, 15, 10, T58);
            SET(I, ref c, ref d, ref a, ref b, 6, 15, T59);
            SET(I, ref b, ref c, ref d, ref a, 13, 21, T60);
            SET(I, ref a, ref b, ref c, ref d, 4, 6, T61);
            SET(I, ref d, ref a, ref b, ref c, 11, 10, T62);
            SET(I, ref c, ref d, ref a, ref b, 2, 15, T63);
            SET(I, ref b, ref c, ref d, ref a, 9, 21, T64);

            /* Then perform the following additions. (That is increment each
             * of the four registers by the value it had before this block
             * was started.) */
            abcd[0] += a;
            abcd[1] += b;
            abcd[2] += c;
            abcd[3] += d;
        }
Beispiel #6
0
        private Ptr <byte> reloc_seg(Ptr <byte> buf, int len, Ptr <byte> rtab)
        {
            Int32 adr = -1;

            while (rtab[0] != 0)//(rtab.ptr < rtab.Count)
            {
                if ((rtab[0] & 255) == 255)
                {
                    adr += 254;
                    rtab.AddPtr(1);
                }
                else
                {
                    adr += rtab[0] & 255;
                    rtab.AddPtr(1);
                    byte type = (byte)(rtab[0] & 0xe0);
                    byte seg  = (byte)(rtab[0] & 0x07);
                    rtab.AddPtr(1);
                    switch (type)
                    {
                    case 0x80:
                    {
                        Int32 oldVal = getWord(buf, adr);
                        Int32 newVal = oldVal + reldiff(seg);
                        setWord(buf, adr, newVal);
                        break;
                    }

                    case 0x40:
                    {
                        Int32 oldVal = buf[adr] * 256 + rtab[0];
                        Int32 newVal = oldVal + reldiff(seg);
                        buf[adr] = (byte)((newVal >> 8) & 255);
                        rtab[0]  = (byte)(newVal & 255);
                        rtab.AddPtr(1);
                        break;
                    }

                    case 0x20:
                    {
                        Int32 oldVal = buf[adr];
                        Int32 newVal = oldVal + reldiff(seg);
                        buf[adr] = (byte)(newVal & 255);
                        break;
                    }
                    }
                    if (seg == 0)
                    {
                        rtab.AddPtr(2);
                    }
                }
                //Console.WriteLine("buf[{0}]={1}",adr,buf[adr]);
                if (adr > len)
                {
                    // Warning: relocation table entries past segment end!
                }
            }

            rtab.AddPtr(1);
            return(rtab);
        }