예제 #1
0
        public BHeader(Stream stream)
        {
            // 4-byte Lua signature
            if (stream.ReadInt32() != m_signature)
            {
                throw new InvalidOperationException("The input file does not have the signature of a valid Lua file.");
            }

            // 1-byte Lua version
            var version = 0xFF & stream.ReadByte();

            switch (version)
            {
            case 0x51:
                Version = Lua_Version.LUA51;
                break;

            case 0x52:
                Version = Lua_Version.LUA52;
                break;

            default:
            {
                var major = version >> 4;
                var minor = version & 0x0F;

                var error = System.String.Format("The input chunk's Lua version is {0}.{1}; unluac can only handle Lua 5.1 and Lua 5.2.",
                                                 major, minor);

                throw new InvalidOperationException(error);
            }
            }

            if (Debug)
            {
                Console.WriteLine("-- version: 0x{0:X}", version);
            }

            // 1-byte Lua "format"
            var format = 0xFF & stream.ReadByte();

            if (format != 0)
            {
                throw new InvalidOperationException("The input chunk reports a non-standard lua format: " + format);
            }

            if (Debug)
            {
                Console.WriteLine("-- format: {0}", format);
            }

            // 1-byte endianness
            var endianness = 0xFF & stream.ReadByte();

            // TODO: Handle Endianness
            if (endianness != 1)
            {
                if (endianness == 0)
                {
                    throw new InvalidOperationException("The input chunk reports a Big-Endian endianness; unluac can only handle Little-Endian");
                }
                else
                {
                    throw new InvalidOperationException("The input chunk reports an invalid endianness: " + endianness);
                }
            }

            if (Debug)
            {
                Console.WriteLine("-- endianness: 1 (little)");
            }

            // 1-byte int size
            var intSize = 0xFF & stream.ReadByte();

            if (Debug)
            {
                Console.WriteLine("-- int size: {0}", intSize);
            }

            Integer = new BIntegerType(intSize);

            // 1-byte sizeT size
            var sizeTSize = 0xFF & stream.ReadByte();

            if (Debug)
            {
                Console.WriteLine("-- size_t size: {0}", sizeTSize);
            }

            SizeT = new BSizeTType(sizeTSize);

            // 1-byte instruction size
            var instructionSize = 0xFF & stream.ReadByte();

            if (Debug)
            {
                Console.WriteLine("-- instruction size: {0}", instructionSize);
            }

            if (instructionSize != 4)
            {
                throw new InvalidOperationException("The input chunk reports an unsupported instruction size: " + instructionSize + " bytes");
            }

            var lNumberSize = 0xFF & stream.ReadByte();

            if (Debug)
            {
                Console.WriteLine("-- Lua number size: {0}", lNumberSize);
            }

            var lNumberIntegralCode = 0xFF & stream.ReadByte();

            if (Debug)
            {
                Console.WriteLine("-- Lua number integral code: {0}", lNumberIntegralCode);
            }

            if (lNumberIntegralCode > 1)
            {
                throw new InvalidOperationException("The input chunk reports an invalid code for lua number integralness: " + lNumberIntegralCode);
            }

            var lNumberIntegral = (lNumberIntegralCode == 1);

            Number   = new LNumberType(lNumberSize, lNumberIntegral);
            Bool     = new LBooleanType();
            String   = new LStringType();
            Constant = new LConstantType();
            Local    = new LLocalType();
            UpValue  = new LUpvalueType();

            Function = Version.GetLFunctionType();

            if (Version.HasHeaderTail)
            {
                for (int i = 0; i < m_luacTail.Length; i++)
                {
                    if (stream.ReadByte() != m_luacTail[i])
                    {
                        throw new InvalidOperationException("The input chunk does not have the header tail of a valid Lua file.");
                    }
                }
            }
        }
예제 #2
0
 public BSizeTType(int sizeTSize)
 {
     SizeTSize     = sizeTSize;
     m_integerType = new BIntegerType(sizeTSize);
 }