Example #1
0
        public LongDoubleInfo(ref ReadOnlySpan <byte> fileData, ConstantPoolTag tag) : base(tag)
        {
            HighBytes = (int)fileData.ReadFourBytes();
            LowBytes  = (int)fileData.ReadFourBytes();

            LongValue = (long)((((ulong)HighBytes) << 32) & (ulong)LowBytes);
        }
        public XElement Convert(BinaryFormat source)
        {
            DataReader reader = new DataReader(source.Stream)
            {
                Endianness      = EndiannessMode.BigEndian,
                DefaultEncoding = new EucJpEncoding()
            };

            // Read header
            var magicStamp = reader.ReadUInt32();

            if (magicStamp != 0xCAFEBABE)
            {
                throw new FormatException("Invalid MagicStamp CAFEBABE");
            }
            reader.ReadUInt16();    // Minor version
            reader.ReadUInt16();    // Major version

            // Read the constant pool
            var textConstants     = new Dictionary <int, string>();
            var stringsIdx        = new List <int>();
            var constantPoolCount = reader.ReadUInt16();

            for (int i = 0; i < constantPoolCount - 1; i++)
            {
                ConstantPoolTag tag = (ConstantPoolTag)reader.ReadByte();
                if (tag == ConstantPoolTag.Utf8)
                {
                    textConstants.Add(i, reader.ReadString(typeof(ushort)));
                }
                else if (tag == ConstantPoolTag.String)
                {
                    stringsIdx.Add(reader.ReadUInt16() - 1);
                }
                else
                {
                    reader.ReadBytes(GetConstantPoolItemInfoLength(tag));
                }
            }

            // Generate the XML element
            var xml = new XElement("StringConstants");

            foreach (var index in stringsIdx)
            {
                // We are skipping some text with encoding issues
                if (!textConstants.ContainsKey(index))
                {
                    continue;
                }

                var xmlString = new XElement("StringConstant");
                xmlString.SetAttributeValue("index", index);
                xmlString.SetIndentedValue(textConstants[index].Replace("\0", ""), 3);
                xml.Add(xmlString);
            }

            return(xml);
        }
Example #3
0
        public Po Convert(BinaryFormat source)
        {
            DataReader reader = new DataReader(source.Stream)
            {
                Endianness      = EndiannessMode.BigEndian,
                DefaultEncoding = new EucJpEncoding()
            };

            // Read header
            var magicStamp = reader.ReadUInt32();

            if (magicStamp != 0xCAFEBABE)
            {
                throw new FormatException("Invalid MagicStamp CAFEBABE");
            }
            reader.ReadUInt16();    // Minor version
            reader.ReadUInt16();    // Major version

            // Read the constant pool
            var textConstants     = new Dictionary <int, string>();
            var stringsIdx        = new List <int>();
            var constantPoolCount = reader.ReadUInt16();

            for (int i = 0; i < constantPoolCount - 1; i++)
            {
                ConstantPoolTag tag = (ConstantPoolTag)reader.ReadByte();
                if (tag == ConstantPoolTag.Utf8)
                {
                    textConstants.Add(i, reader.ReadString(typeof(ushort)));
                }
                else if (tag == ConstantPoolTag.String)
                {
                    stringsIdx.Add(reader.ReadUInt16() - 1);
                }
                else
                {
                    reader.ReadBytes(GetConstantPoolItemInfoLength(tag));
                }
            }

            // Generate the PO
            Po po = new Po {
                Header = new PoHeader("Xenosaga I", "*****@*****.**")
            };

            foreach (var index in stringsIdx)
            {
                po.Add(new PoEntry(textConstants[index].Replace("\0", ""))
                {
                    Context = index.ToString()
                });
            }

            return(po);
        }
Example #4
0
        static int GetConstantPoolItemInfoLength(ConstantPoolTag tag)
        {
            switch (tag)
            {
            case ConstantPoolTag.Class:
                return(2);

            case ConstantPoolTag.FieldRef:
                return(4);

            case ConstantPoolTag.MethodRef:
                return(4);

            case ConstantPoolTag.InterfaceMethodRef:
                return(4);

            case ConstantPoolTag.String:
                return(2);

            case ConstantPoolTag.Integer:
                return(4);

            case ConstantPoolTag.Float:
                return(4);

            case ConstantPoolTag.Long:
                return(8);

            case ConstantPoolTag.Double:
                return(8);

            case ConstantPoolTag.NameAndType:
                return(4);

            case ConstantPoolTag.MethodHandle:
                return(3);

            case ConstantPoolTag.MethodType:
                return(2);

            case ConstantPoolTag.InvokeDynamic:
                return(4);

            default:
                throw new FormatException("Unknown tag");
            }
        }
Example #5
0
 public InvokeDynamicInfo(ConstantPoolTag tag, ushort bootstrapMethodAttrIndex, ushort nameAndTypeIndex) :
     base(tag)
 {
     BootstrapMethodAttrIndex = bootstrapMethodAttrIndex;
     _nameAndTypeIndex        = nameAndTypeIndex;
 }
Example #6
0
 public MethodTypeInfo(ConstantPoolTag tag, ushort descriptorIndex) : base(tag)
 {
     _descriptorIndex = descriptorIndex;
 }
Example #7
0
 public MethodHandleInfo(ConstantPoolTag tag, MethodHandleType referenceKind, ushort referenceIndex) : base(tag)
 {
     ReferenceKind   = referenceKind;
     _referenceIndex = referenceIndex;
 }
Example #8
0
 public ConstantPoolInfo(ConstantPoolTag tag)
 {
     this.tag = tag;
 }
Example #9
0
 public StringInfo(ConstantPoolTag tag, ushort stringIndex) : base(tag)
 {
     _stringIndex = stringIndex;
 }
Example #10
0
 public ClassInfo(ConstantPoolTag tag, ushort nameIndex) : base(tag)
 {
     _nameIndex = nameIndex;
 }
Example #11
0
 public FloatInfo(ConstantPoolTag tag, float value) : base(tag)
 {
     Value = value;
 }
Example #12
0
 public IntegerInfo(ConstantPoolTag tag, int value) : base(tag)
 {
     Value = value;
 }
Example #13
0
 public RefInfo(ref ReadOnlySpan <byte> fileData, ConstantPoolTag tag) : base(tag)
 {
     classIndex       = fileData.ReadTwoBytes();
     nameAndTypeIndex = fileData.ReadTwoBytes();
 }
 public ConstantPoolDescription(ConstantPoolTag tag, ByteSerializable info)
 {
     Tag  = tag;
     Info = info;
 }
Example #15
0
 public ClassStringInfo(ref ReadOnlySpan <byte> fileData, ConstantPoolTag tag) : base(tag)
 {
     stringIndex = fileData.ReadTwoBytes();
 }
Example #16
0
 protected CpInfo(ConstantPoolTag tag)
 {
     Tag = tag;
 }
Example #17
0
 public ModuleOrPackageInfo(ConstantPoolTag tag, ushort nameIndex) : base(tag)
 {
     _nameIndex = nameIndex;
 }
Example #18
0
 public LongInfo(ConstantPoolTag tag, long value) : base(tag)
 {
     Value = value;
 }
Example #19
0
 public FieldOrMethodrefInfo(ConstantPoolTag tag, ushort classIndex, ushort nameAndTypeIndex) : base(tag)
 {
     _classIndex       = classIndex;
     _nameAndTypeIndex = nameAndTypeIndex;
 }
Example #20
0
 public DoubleInfo(ConstantPoolTag tag, double value) : base(tag)
 {
     Value = value;
 }
Example #21
0
 public NameAndTypeInfo(ConstantPoolTag tag, ushort nameIndex, ushort descriptorIndex) : base(tag)
 {
     _nameIndex       = nameIndex;
     _descriptorIndex = descriptorIndex;
 }
Example #22
0
 public Utf8Info(ConstantPoolTag tag, ushort length, string data) : base(tag)
 {
     Data = data;
 }
Example #23
0
        public BinaryFormat Convert(Po source)
        {
            if (OriginalStream == null)
            {
                throw new InvalidOperationException("Cannot convert from XML without original stream");
            }

            // Get the list of strings to change
            Dictionary <int, string> textConstants = new Dictionary <int, string>();

            foreach (var element in source.Entries)
            {
                int index = Int32.Parse(element.Context);
                textConstants[index] = element.Translated ?? element.Original;
            }

            OriginalStream.Position = 0;
            DataReader reader = new DataReader(OriginalStream)
            {
                Endianness      = EndiannessMode.BigEndian,
                DefaultEncoding = new EucJpEncoding()
            };

            BinaryFormat format = new BinaryFormat();
            DataWriter   writer = new DataWriter(format.Stream)
            {
                Endianness      = EndiannessMode.BigEndian,
                DefaultEncoding = new EucJpEncoding()
            };

            // Magic stamp + Version
            writer.Write(reader.ReadBytes(8));

            // Read pool and write into new stream changing texts
            ushort poolCount = reader.ReadUInt16();

            writer.Write(poolCount);
            for (int i = 0; i < poolCount - 1; i++)
            {
                ConstantPoolTag tag = (ConstantPoolTag)reader.ReadByte();
                writer.Write((byte)tag);

                if (tag == ConstantPoolTag.Utf8)
                {
                    var textData = reader.ReadBytes(reader.ReadUInt16());
                    if (textConstants.ContainsKey(i))
                    {
                        writer.Write(textConstants[i], typeof(ushort), textConstants[i].Length > 0);
                    }
                    else
                    {
                        writer.Write((ushort)textData.Length);
                        writer.Write(textData);
                    }
                }
                else
                {
                    writer.Write(reader.ReadBytes(GetConstantPoolItemInfoLength(tag)));
                }
            }

            // Write the rest of the file without changes
            writer.Write(reader.ReadBytes((int)(OriginalStream.Length - OriginalStream.Position)));

            // Clean to prevent errors
            OriginalStream = null;

            return(format);
        }
Example #24
0
 public IntegerFloatInfo(ref ReadOnlySpan <byte> fileData, ConstantPoolTag tag) : base(tag)
 {
     IntegerValue = (int)fileData.ReadFourBytes();
 }