Exemplo n.º 1
0
        private static int Math_Frexp(ILuaState lua)
        {
            double d = lua.L_CheckNumber(1);

            // Translate the double into sign, exponent and mantissa.
            long bits = BitConverter.DoubleToInt64Bits(d);
            // Note that the shift is sign-extended, hence the test against -1 not 1
            bool negative = (bits < 0);
            int  exponent = (int)((bits >> 52) & 0x7ffL);
            long mantissa = bits & 0xfffffffffffffL;

            // Subnormal numbers; exponent is effectively one higher,
            // but there's no extra normalisation bit in the mantissa
            if (exponent == 0)
            {
                exponent++;
            }
            // Normal numbers; leave exponent as it is but add extra
            // bit to the front of the mantissa
            else
            {
                mantissa = mantissa | (1L << 52);
            }

            // Bias the exponent. It's actually biased by 1023, but we're
            // treating the mantissa as m.0 rather than 0.m, so we need
            // to subtract another 52 from it.
            exponent -= 1075;

            if (mantissa == 0)
            {
                lua.PushNumber(0.0);
                lua.PushNumber(0.0);
                return(2);
            }

            /* Normalize */
            while ((mantissa & 1) == 0)
            {                /*  i.e., Mantissa is even */
                mantissa >>= 1;
                exponent++;
            }

            double m = (double)mantissa;
            double e = (double)exponent;

            while (m >= 1)
            {
                m /= 2.0;
                e += 1.0;
            }

            if (negative)
            {
                m = -m;
            }
            lua.PushNumber(m);
            lua.PushNumber(e);
            return(2);
        }
            public void Randomize(double num)
            {
                int num1 = _seed;
                int num2 = !BitConverter.IsLittleEndian ? BitConverter.ToInt32(BitConverter.GetBytes(num), 0) : BitConverter.ToInt32(BitConverter.GetBytes(num), 4);
                int num3 = (num2 & (int)ushort.MaxValue ^ num2 >> 16) << 8;
                int num4 = num1 & -16776961 | num3;

                _seed = num4;
            }
Exemplo n.º 3
0
 /// <summary>
 /// Writes a value of the specified type to this buffer.
 /// </summary>
 /// <param name="value">DOUBLE value to be written.</param>
 /// <exception cref="System.IndexOutOfRangeException"/>
 public void Write(double value)
 {
     byte[] bytes = BitConverter.GetBytes(value);
     for (int i = 0; i < bytes.Length; i++)
     {
         memory[iterator++] = bytes[i];
     }
     iterator = (iterator + fastAlign) & fastAlignNot;
 }
Exemplo n.º 4
0
        public void WriteRecord(Record record, long index)
        {
            Stream.Position = index * _pageSize;

            Stream.Write(BC.GetBytes(record.Radius), 0, sizeof(double));
            Stream.Write(BC.GetBytes(record.Angle), 0, sizeof(short));
            Stream.Flush();
            DiskOperations++;
        }
Exemplo n.º 5
0
        /// <summary>
        /// A representation of the Double bits formatted for easy readability
        /// </summary>
        /// <param name="bits"></param>
        /// <returns></returns>
        public string ToString(long bits)
        {
            double x      = BitConverter.Int64BitsToDouble(bits);
            string numStr = HexConverter.ConvertAny2Any(bits.ToString(), 10, 2);
            string padStr = "0000000000000000000000000000000000000000000000000000000000000000" + numStr;
            string bitStr = padStr.Substring(padStr.Length - 64);
            string str    = bitStr.Substring(0, 1) + "  " + bitStr.Substring(1, 12) + "(exp) "
                            + bitStr.Substring(12) + " [ " + x + " ]";

            return(str);
        }
            public void Randomize()
            {
                System.DateTime now   = System.DateTime.Now;
                float           timer = (float)checked ((60 * now.Hour + now.Minute) * 60 + now.Second) + (float)now.Millisecond / 1000f;
                int             num1  = _seed;
                int             num2  = BitConverter.ToInt32(BitConverter.GetBytes(timer), 0);
                int             num3  = (num2 & (int)ushort.MaxValue ^ num2 >> 16) << 8;
                int             num4  = num1 & -16776961 | num3;

                _seed = num4;
            }
Exemplo n.º 7
0
        public Record ReadRecord(long index)
        {
            var record = new byte[_pageSize];

            Stream.Position = index * _pageSize;

            Stream.Read(record, 0, _pageSize);

            DiskOperations++;
            return(new Record(BC.ToDouble(record, 0), BC.ToInt16(record, 8)));
        }
Exemplo n.º 8
0
        public static ushort ReadUInt16BE(byte[] bytes, int offset)
        {
            if (SysBitConverter.IsLittleEndian)
            {
                var v = ((ushort)bytes[offset] << 8) |
                        ((ushort)bytes[offset + 1]);
                return((ushort)v);
            }

            return(SysBitConverter.ToUInt16(bytes, offset));
        }
Exemplo n.º 9
0
        public static ulong ReadUInt32BE(byte[] bytes, int offset)
        {
            if (SysBitConverter.IsLittleEndian)
            {
                return(((uint)bytes[offset] << 24) |
                       ((uint)bytes[offset + 1] << 16) |
                       ((uint)bytes[offset + 2] << 8) |
                       (uint)bytes[offset + 3]);
            }

            return(SysBitConverter.ToUInt64(bytes, offset));
        }
            public float VBNext(float num)
            {
                int num1 = _seed;

                if ((double)num != 0.0)
                {
                    if ((double)num < 0.0)
                    {
                        long num2 = (long)BitConverter.ToInt32(BitConverter.GetBytes(num), 0) & (long)uint.MaxValue;
                        num1 = checked ((int)(num2 + (num2 >> 24) & 16777215L));
                    }
                    num1 = checked ((int)((long)num1 * 1140671485L + 12820163L & 16777215L));
                }
                _seed = num1;
                return((float)num1 / 1.677722E+07f);
            }
Exemplo n.º 11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="num"></param>
        public void Add(double num)
        {
            long numBits = BitConverter.DoubleToInt64Bits(num);

            if (isFirst)
            {
                commonBits    = numBits;
                commonSignExp = SignExpBits(commonBits);
                isFirst       = false;
                return;
            }

            long numSignExp = SignExpBits(numBits);

            if (numSignExp != commonSignExp)
            {
                commonBits = 0;
                return;
            }
            commonMantissaBitsCount = NumCommonMostSigMantissaBits(commonBits, numBits);
            commonBits = ZeroLowerBits(commonBits, 64 - (12 + commonMantissaBitsCount));
        }
Exemplo n.º 12
0
 /// <summary>
 /// Reads the value of the specified type from the buffer.
 /// </summary>
 /// <param name="value">The OUT variable to store the DOUBLE value in.</param>
 /// <exception cref="System.ArgumentException"/>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentOutOfRangeException"/>
 public void Read(out double value)
 {
     value    = BitConverter.ToUInt16(memory, iterator);
     iterator = (iterator + (int)BufferTypeSize.Double + fastAlign) & fastAlignNot;
 }
Exemplo n.º 13
0
 /// <summary>
 /// Reads the value of the specified type from the buffer.
 /// </summary>
 /// <param name="value">The OUT variable to store the FLOAT value in.</param>
 /// <exception cref="System.ArgumentException"/>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentOutOfRangeException"/>
 public void Read(out float value)
 {
     value    = BitConverter.ToSingle(memory, iterator);
     iterator = (iterator + (int)BufferTypeSize.Single + fastAlign) & fastAlignNot;
 }
Exemplo n.º 14
0
        public HttpStatusCode Upload(FileInfo mediaFile, string message, string keyword, string username, string twitter_id, ref string content)
        {
            // Message必須
            if (string.IsNullOrEmpty(message))
            {
                throw new ArgumentException("'Message' is required.");
            }

            // Check filetype and size
            if (Array.IndexOf(multimediaExt, mediaFile.Extension.ToLower()) > -1)
            {
                if (mediaFile.Length > TwitVideo.MaxMultiMediaFileSize)
                {
                    throw new ArgumentException("File is too large.");
                }
            }
            else if (Array.IndexOf(pictureExt, mediaFile.Extension.ToLower()) > -1)
            {
                if (mediaFile.Length > TwitVideo.MaxPictureFileSize)
                {
                    throw new ArgumentException("File is too large.");
                }
            }
            else
            {
                throw new ArgumentException("Service don't support this filetype.");
            }

            // Endpoint(URI+Token)
            const string URLBASE = "http://api.twitvideo.jp/oauth/upload/";

            byte[] data = Encoding.ASCII.GetBytes(ApplicationSettings.TwitVideoConsumerKey.Substring(0, 9) + username);

            byte[] bHash;
            using (MD5CryptoServiceProvider md5provider = new MD5CryptoServiceProvider())
            {
                bHash = md5provider.ComputeHash(data);
            }

            string url = URLBASE + BitConverter.ToString(bHash).ToLower().Replace("-", "");
            // Parameters
            Dictionary <string, string> param = new Dictionary <string, string>();

            param.Add("username", username);
            if (!string.IsNullOrEmpty(twitter_id))
            {
                param.Add("twitter_id", twitter_id);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                param.Add("keyword", keyword);
            }
            param.Add("type", "xml");
            param.Add("message", message);
            List <KeyValuePair <string, FileInfo> > binary = new List <KeyValuePair <string, FileInfo> >();

            binary.Add(new KeyValuePair <string, FileInfo>("media", mediaFile));
            this.InstanceTimeout = 60000;             // タイムアウト60秒

            HttpWebRequest req = this.CreateRequest(HttpConnection.PostMethod, new Uri(url), param, binary, false);

            return(this.GetResponse(req, out content, null, false));
        }
Exemplo n.º 15
0
 /// <summary>
 /// Reads the value of the specified type from the buffer.
 /// </summary>
 /// <param name="value">The OUT variable to store the INT value in.</param>
 /// <exception cref="System.ArgumentException"/>
 /// <exception cref="System.ArgumentNullException"/>
 /// <exception cref="System.ArgumentOutOfRangeException"/>
 public void Read(out int value)
 {
     value    = BitConverter.ToInt32(memory, iterator);
     iterator = (iterator + (int)BufferTypeSize.Int32 + fastAlign) & fastAlignNot;
 }
Exemplo n.º 16
0
 internal static int GetShort(byte[] memory, int index)
 {
     return(BitConverter.ToInt16(memory, index));
 }
Exemplo n.º 17
0
        public virtual ResStringPool ReadResStringPool(ResStringPool_header header)
        {
            var pool = new ResStringPool
            {
                Header     = header,
                StringData = new List <string>(),
                StyleData  = new List <List <ResStringPool_span> >()
            };

            // Offsets of the string data, relative to header.StringStart
            var stringIndices = new List <uint>();

            for (int i = 0; i < header.StringCount; i++)
            {
                stringIndices.Add(ReadUInt32());
            }

            // Offset of the style data, relative to header.StylesStart
            var styleIndices = new List <uint>();

            for (int i = 0; i < header.StyleCount; i++)
            {
                styleIndices.Add(ReadUInt32());
            }

            // Keep track of how many bytes are left, to prevent us
            // from reading invalid data.
            long bytesLeft = header.Header.Size;

            bytesLeft -= header.Header.HeaderSize;
            bytesLeft -= 4 * header.StringCount;
            bytesLeft -= 4 * header.StyleCount;

            // Fetch the block which contains the string. If a styles section is
            // present, the strings block ends there; otherwise, it runs to the end
            // of this entry.
            uint stringsEnd = header.StyleCount > 0 ? header.StylesStart : header.Header.Size;

            byte[] rawStringData = ReadBytes((int)stringsEnd - (int)header.StringStart);

            bytesLeft -= rawStringData.Length;

            bool isUtf8 = (header.Flags & StringPoolFlags.UTF8_FLAG) == StringPoolFlags.UTF8_FLAG;

            foreach (uint startingIndex in stringIndices)
            {
                // The starting index specifies where the string starts.
                // We can now read the string in either UTF8 or UTF16 format.
                uint pos = startingIndex;
                if (isUtf8)
                {
                    uint   charLen = Helper.DecodeLengthUtf8(rawStringData, ref pos);
                    uint   byteLen = Helper.DecodeLengthUtf8(rawStringData, ref pos);
                    string item    = Encoding.UTF8.GetString(rawStringData, (int)pos, (int)byteLen);
                    if (item.Length != charLen)
                    {
#if !CORECLR
                        Debug.WriteLine("Warning: UTF-8 string length ({0}) not matching specified length ({1}).",
                                        item.Length, charLen);
#endif
                    }
                    pool.StringData.Add(item);
                }
                else
                {
                    uint   charLen = Helper.DecodeLengthUtf16(rawStringData, ref pos);
                    uint   byteLen = charLen * 2;
                    string item    = Encoding.Unicode.GetString(rawStringData, (int)pos, (int)byteLen);
                    pool.StringData.Add(item);
                }
            }

            // If styles are present, we should read them, too.
            if (header.StyleCount > 0)
            {
                byte[] rawStyleData = ReadBytes((int)header.Header.Size - (int)header.StylesStart);

                foreach (uint startingIndex in styleIndices)
                {
                    // At startingIndex, there are N entries defining the individual tags (b, i,...)
                    // that style the string at index i
                    // They are terminated by a value with value END (0xFFFFFFFF)
                    List <ResStringPool_span> styleData = new List <ResStringPool_span>();

                    int pos = (int)startingIndex;

                    while (true)
                    {
                        var index     = BitConverter.ToUInt32(rawStyleData, pos);
                        var firstChar = BitConverter.ToUInt32(rawStyleData, pos + 4);
                        var lastChar  = BitConverter.ToUInt32(rawStringData, pos + 8);

                        var span = new ResStringPool_span
                        {
                            Name = new ResStringPool_ref()
                            {
                                Index = index == 0xFFFFFFFFu ? (uint?)null : index
                            },
                            FirstChar = firstChar,
                            LastChar  = lastChar,
                        };

                        styleData.Add(span);
                        if (span.IsEnd)
                        {
                            break;
                        }

                        pos += 12;
                    }

                    pool.StyleData.Add(styleData);
                }

                bytesLeft -= rawStyleData.Length;
            }

            // Make sure we didn't go out of bounds.
            if (bytesLeft < 0)
            {
                throw new InvalidDataException("The length of the content exceeds the ResStringPool block boundary.");
            }
            if (bytesLeft > 0)
            {
                // Padding: data is always aligned to 4 bytes.
#if !CORECLR
                Debug.WriteLine("Warning: Garbage at the end of the StringPool block. Padding?");
#endif
                ReadBytes((int)bytesLeft);
            }

            return(pool);
        }