public CSharpStringsTableSerializer(StringsTable stringsTable, string @namespace, string className, string preprocessorExpr)
 {
     this.stringsTable     = stringsTable;
     this.@namespace       = @namespace;
     this.className        = className;
     this.preprocessorExpr = preprocessorExpr;
 }
Ejemplo n.º 2
0
        public string this[string key]
        {
            get
            {
                try
                {
                    var temp = StringsTable[key];
                    if (temp.AliasedKey)
                    {
                        return(this[temp.CloneOf]);
                    }
                    if (temp.DeriveFromParent)
                    {
                        return(null);
                    }

                    return(temp.Value);
                }
                catch (KeyNotFoundException)
                {
                    throw new StringNotFoundException(key);
                }
            }
            set
            {
                if (StringsTable.ContainsKey(key))
                {
                    StringsTable[key] = new StringTranslation(key, value);
                }
                else
                {
                    StringsTable[key].Value = value;
                }
            }
        }
 public void Serialize(FileWriter writer, StringsTable stringsTable)
 {
     writer.WriteFileHeader();
     writer.WriteLine($"#if {define}");
     writer.WriteLine($"namespace {@namespace} {{");
     using (writer.Indent()) {
         writer.WriteLine("static partial class InstrInfos {");
         using (writer.Indent()) {
             writer.WriteLineNoIndent($"#if {CSharpConstants.HasSpanDefine}");
             writer.WriteLine("static System.ReadOnlySpan<byte> GetSerializedInstrInfos() =>");
             writer.WriteLineNoIndent("#else");
             writer.WriteLine("static byte[] GetSerializedInstrInfos() =>");
             writer.WriteLineNoIndent("#endif");
             using (writer.Indent()) {
                 writer.WriteLine("new byte[] {");
                 using (writer.Indent())
                     SerializeTable(writer, stringsTable);
                 writer.WriteLine("};");
             }
         }
         writer.WriteLine("}");
     }
     writer.WriteLine("}");
     writer.WriteLine("#endif");
 }
Ejemplo n.º 4
0
        public void Test_ReadSharedStrings()
        {
            var t = new StringsTable <SharedString>
            {
                String1 = "string",
                String2 = "foo",
                String3 = "string",
            };

            byte[] destination = new byte[1024];
            var    serializer  = FlatBufferSerializer.Default.Compile <StringsTable <SharedString> >()
                                 .WithSettings(new SerializerSettings
            {
                SharedStringWriterFactory = () => new SharedStringWriter(100),
                SharedStringReaderFactory = () => SharedStringReader.Create(100),
            });

            int bytesWritten = serializer.Write(SpanWriter.Instance, destination, t);

            var table = serializer.Parse(destination);

            Assert.AreEqual("string", (string)table.String1);
            Assert.AreEqual("foo", (string)table.String2);
            Assert.AreEqual("string", (string)table.String3);

            Assert.IsTrue(object.ReferenceEquals(table.String1.String, table.String3.String));
        }
Ejemplo n.º 5
0
        public void Test_TableSharedStringsWithoutEviction()
        {
            var t = new StringsTable <SharedString>
            {
                String1 = "string",
                String2 = "foo",
                String3 = "string",
            };

            byte[] destination = new byte[1024];
            var    serializer  = FlatBufferSerializer.Default.Compile <StringsTable <SharedString> >()
                                 .WithSettings(new SerializerSettings
            {
                SharedStringWriterFactory = () => new SharedStringWriter(100)
            });

            int bytesWritten = serializer.Write(SpanWriter.Instance, destination, t);

            byte[] stringBytes = Encoding.UTF8.GetBytes("string");

            // We can't predict ordering since there is hashing under the hood.
            int firstIndex  = destination.AsSpan().IndexOf(stringBytes);
            int secondIndex = destination.AsSpan().Slice(0, firstIndex + 1).IndexOf(stringBytes);

            Assert.AreEqual(-1, secondIndex);
        }
Ejemplo n.º 6
0
        public void Test_TableSharedStringsWithNull()
        {
            var t = new StringsTable <SharedString>
            {
                String1 = null,
                String2 = "string",
                String3 = (string)null,
            };

            byte[] destination = new byte[1024];

            var serializer = FlatBufferSerializer.Default.Compile <StringsTable <SharedString> >()
                             .WithSettings(new SerializerSettings
            {
                SharedStringWriterFactory = () => new SharedStringWriter(5)
            });

            int bytesWritten = serializer.Write(SpanWriter.Instance, destination, t);

            byte[] expectedBytes = new byte[]
            {
                4, 0, 0, 0,             // offset to table start
                248, 255, 255, 255,     // soffset to vtable.
                12, 0, 0, 0,            // uoffset to string
                8, 0, 8, 0,             // vtable length, table length
                0, 0, 4, 0,             // vtable(0), vtable(1)
                6, 0, 0, 0,             // string length
                (byte)'s', (byte)'t', (byte)'r', (byte)'i',
                (byte)'n', (byte)'g', 0 // null terminator.
            };

            Assert.IsTrue(expectedBytes.AsSpan().SequenceEqual(destination.AsSpan().Slice(0, bytesWritten)));
        }
Ejemplo n.º 7
0
 public void Serialize(FileWriter writer, StringsTable stringsTable)
 {
     writer.WriteFileHeader();
     writer.WriteLine(RustConstants.AttributeNoRustFmt);
     writer.WriteLine($"pub(super) static FORMATTER_TBL_DATA: &[u8] = &[");
     using (writer.Indent())
         SerializeTable(writer, stringsTable);
     writer.WriteLine("];");
 }
Ejemplo n.º 8
0
        public void Test_TableSharedStringsWithEviction()
        {
            var t = new StringsTable <SharedString>
            {
                String1 = "string",
                String2 = "foo",
                String3 = "string",
            };

            byte[] destination = new byte[1024];
            var    serializer  = FlatBufferSerializer.Default.Compile <StringsTable <SharedString> >()
                                 .WithSettings(new SerializerSettings
            {
                SharedStringWriterFactory = () => new SharedStringWriter(1)
            });

            int bytesWritten = serializer.Write(SpanWriter.Instance, destination, t);

            byte[] expectedBytes = new byte[]
            {
                4, 0, 0, 0,                         // offset to table start
                240, 255, 255, 255,                 // soffset to vtable.
                24, 0, 0, 0,                        // uoffset to string 1
                32, 0, 0, 0,                        // uoffset to string 2
                36, 0, 0, 0,                        // uoffset to string 3
                10, 0, 16, 0,                       // vtable length, table length
                4, 0, 8, 0,                         // vtable(0), vtable(1)
                12, 0, 0, 0,                        // vtable(2), padding
                6, 0, 0, 0,                         // string0 length
                (byte)'s', (byte)'t', (byte)'r', (byte)'i',
                (byte)'n', (byte)'g', 0, 0,         // null terminator + 1 byte padding
                3, 0, 0, 0,                         // string1 length
                (byte)'f', (byte)'o', (byte)'o', 0, // string1 + null terminator
                6, 0, 0, 0,
                (byte)'s', (byte)'t', (byte)'r', (byte)'i',
                (byte)'n', (byte)'g', 0 // null terminator
            };

            Assert.IsTrue(expectedBytes.AsSpan().SequenceEqual(destination.AsSpan().Slice(0, bytesWritten)));
        }
Ejemplo n.º 9
0
        public void Test_ReadSharedStrings_NoFactories()
        {
            var t = new StringsTable <SharedString>
            {
                String1 = "string",
                String2 = "foo",
                String3 = "string",
            };

            byte[] destination = new byte[1024];
            var    serializer  = FlatBufferSerializer.Default.Compile <StringsTable <SharedString> >();

            int bytesWritten = serializer.Write(SpanWriter.Instance, destination, t);

            var table = serializer.Parse(destination);

            Assert.AreEqual("string", (string)table.String1);
            Assert.AreEqual("foo", (string)table.String2);
            Assert.AreEqual("string", (string)table.String3);

            Assert.IsFalse(object.ReferenceEquals(table.String1.String, table.String3.String));
        }
Ejemplo n.º 10
0
 public RustStringsTableSerializer(StringsTable stringsTable) =>
 this.stringsTable = stringsTable;
Ejemplo n.º 11
0
        public override void Serialize(FileWriter writer, StringsTable stringsTable)
        {
            writer.WriteHeader();
            writer.WriteLine("#if !NO_MASM_FORMATTER && !NO_FORMATTER");
            writer.WriteLine("namespace Iced.Intel.MasmFormatterInternal {");
            writer.Indent();
            writer.WriteLine("static partial class InstrInfos {");
            writer.Indent();
            writer.WriteLine("static byte[] GetSerializedInstrInfos() =>");
            writer.Indent();
            writer.WriteLine("new byte[] {");
            writer.Indent();

            int index = -1;
            var sb    = new StringBuilder();
            var infos = CtorInfos.Infos;

            for (int i = 0; i < infos.Length; i++)
            {
                var info = infos[i];
                index++;
                var ctorKind = (CtorKind)info[0];
                var code     = (Code)info[1];
                if (code != (Code)index)
                {
                    throw new InvalidOperationException();
                }

                if (index != 0)
                {
                    writer.WriteLine();
                }
                writer.WriteCommentLine(code.ToString());

                bool isSame = i > 0 && IsSame(infos[i - 1], info);
                if (isSame)
                {
                    ctorKind = CtorKind.Previous;
                }

                if ((uint)ctorKind > 0x7F)
                {
                    throw new InvalidOperationException();
                }
                uint firstStringIndex = GetFirstStringIndex(stringsTable, info, out bool hasVPrefix);
                writer.WriteByte((byte)((uint)ctorKind | (hasVPrefix ? 0x80U : 0)));
                if (hasVPrefix)
                {
                    writer.WriteCommentLine($"'v', {ctorKind}");
                }
                else
                {
                    writer.WriteCommentLine($"{ctorKind}");
                }
                if (isSame)
                {
                    continue;
                }
                uint si;
                for (int j = 2; j < info.Length; j++)
                {
                    switch (info[j])
                    {
                    case string s:
                        if (firstStringIndex != uint.MaxValue)
                        {
                            si = firstStringIndex;
                            firstStringIndex = uint.MaxValue;
                        }
                        else
                        {
                            si = stringsTable.GetIndex(s, ignoreVPrefix: true, out hasVPrefix);
                            if (hasVPrefix)
                            {
                                throw new InvalidOperationException();
                            }
                        }
                        writer.WriteCompressedUInt32(si);
                        writer.WriteCommentLine($"{si} = \"{s}\"");
                        break;

                    case char c:
                        if ((ushort)c > byte.MaxValue)
                        {
                            throw new InvalidOperationException();
                        }
                        writer.WriteByte((byte)c);
                        if (c == '\0')
                        {
                            writer.WriteCommentLine(@"'\0'");
                        }
                        else
                        {
                            writer.WriteCommentLine($"'{c}'");
                        }
                        break;

                    case InstrOpInfoFlags flags:
                        writer.WriteCompressedUInt32((uint)flags);
                        writer.WriteCommentLine($"0x{(uint)flags:X} = {ToString(sb, flags)}");
                        break;

                    case int ival:
                        writer.WriteCompressedUInt32((uint)ival);
                        writer.WriteCommentLine($"0x{ival:X}");
                        break;

                    case PseudoOpsKind pseudoOpsKind:
                        if ((uint)pseudoOpsKind > byte.MaxValue)
                        {
                            throw new InvalidOperationException();
                        }
                        writer.WriteByte((byte)pseudoOpsKind);
                        writer.WriteCommentLine(pseudoOpsKind.ToString());
                        break;

                    case CodeSize codeSize:
                        if ((uint)codeSize > byte.MaxValue)
                        {
                            throw new InvalidOperationException();
                        }
                        writer.WriteByte((byte)codeSize);
                        writer.WriteCommentLine(codeSize.ToString());
                        break;

                    case Register register:
                        if ((uint)register > byte.MaxValue)
                        {
                            throw new InvalidOperationException();
                        }
                        writer.WriteByte((byte)register);
                        writer.WriteCommentLine(register.ToString());
                        break;

                    case bool b:
                        writer.WriteByte((byte)(b ? 1 : 0));
                        writer.WriteCommentLine(b.ToString());
                        break;

                    default:
                        throw new InvalidOperationException();
                    }
                }
            }

            writer.Unindent();
            writer.WriteLine("};");
            writer.Unindent();
            writer.Unindent();
            writer.WriteLine("}");
            writer.Unindent();
            writer.WriteLine("}");
            writer.WriteLine("#endif");
        }
Ejemplo n.º 12
0
 public override void Initialize(StringsTable stringsTable) =>
 Initialize(stringsTable, CtorInfos.Infos);
Ejemplo n.º 13
0
        /// <summary>
        /// Gets an object from the data.
        /// </summary>
        /// <returns></returns>
        SType GetObject()
        {
            SType sObject   = null;
            var   type      = m_reader.ReadByte();
            var   checkType = (StreamType)(type & ~(byte)StreamType.SharedFlag);
            var   shared    = Convert.ToBoolean(type & (byte)StreamType.SharedFlag);

            switch (checkType)
            {
            case 0:
            case StreamType.StreamStart:
                break;

            case StreamType.Marker:
                break;

            case StreamType.None:
                sObject = new SNoneType();
                break;

            case StreamType.Utf8:
                sObject = new SStringType(m_reader.ReadUtf8String(m_reader.ReadLength()));
                break;

            case StreamType.String:
            case StreamType.StringLong:
                sObject = new SStringType(m_reader.ReadString(m_reader.ReadLength()));
                break;

            case StreamType.StringGlobal:
                sObject = new SStringType(m_reader.ReadString(m_reader.ReadLength()));
                CheckShared(shared, sObject);
                break;

            case StreamType.StringUnicode:
                sObject = new SStringType(m_reader.ReadUnicodeString(m_reader.ReadLength() * 2));
                break;

            case StreamType.Long:
                sObject = new SLongType(m_reader.ReadLong());
                break;

            case StreamType.Int:
                sObject = new SIntType(m_reader.ReadInt());
                break;

            case StreamType.Short:
                sObject = new SShortType(m_reader.ReadShort());
                break;

            case StreamType.Byte:
                sObject = new SByteType(m_reader.ReadByte());
                break;

            case StreamType.IntNegOne:
                sObject = new SIntType(-1);
                break;

            case StreamType.IntZero:
                sObject = new SIntType(0);
                break;

            case StreamType.IntOne:
                sObject = new SIntType(1);
                break;

            case StreamType.Double:
                sObject = new SDoubleType(m_reader.ReadDouble());
                break;

            case StreamType.DoubleZero:
                sObject = new SDoubleType(0);
                break;

            case StreamType.StringEmpty:
                sObject = new SStringType(null);
                break;

            case StreamType.StringOne:
                sObject = new SStringType(m_reader.ReadString(1));
                break;

            case StreamType.StringRef:
                var id = m_reader.ReadByte();
                sObject = new SReferenceType(id, StringsTable.GetStringByID(id));
                break;

            case StreamType.StringIdent:
                sObject = new SIdentType(m_reader.ReadString(m_reader.ReadLength()));
                CheckShared(shared, sObject);
                break;

            case StreamType.Tuple:
            {
                var length = m_reader.ReadLength();
                sObject = new STupleType((uint)length);
                CheckShared(shared, sObject);
                Parse(sObject, length);
                break;
            }

            case StreamType.List:
            {
                var length = m_reader.ReadLength();
                sObject = new SListType((uint)length);
                CheckShared(shared, sObject);
                Parse(sObject, length);
                break;
            }

            case StreamType.Dict:
            {
                var length = (m_reader.ReadLength() * 2);
                sObject = new SDictType((uint)length);
                CheckShared(shared, sObject);
                Parse(sObject, length);
                break;
            }

            case StreamType.SharedObj:
                sObject = m_reader.GetSharedObj(m_reader.ReadLength() - 1);
                break;

            case StreamType.Checksum:
                sObject = new SStringType("checksum");
                m_reader.ReadInt();
                break;

            case StreamType.BoolTrue:
                sObject = new SBooleanType(1);
                break;

            case StreamType.BoolFalse:
                sObject = new SBooleanType(0);
                break;

            case StreamType.ClassObject:
            case StreamType.NewObj:
            case StreamType.Object:
            {
                var storedId = m_reader.ReserveSlot(shared);
                sObject = ParseObject();
                m_reader.UpdateSlot(storedId, sObject);
            }
            break;

            case StreamType.TupleEmpty:
                sObject = new STupleType(0);
                break;

            case StreamType.TupleOne:
                sObject = new STupleType(1);
                CheckShared(shared, sObject);
                Parse(sObject);
                break;

            case StreamType.ListEmpty:
                sObject = new SListType(0);
                CheckShared(shared, sObject);
                break;

            case StreamType.ListOne:
                sObject = new SListType(1);
                CheckShared(shared, sObject);
                Parse(sObject);
                break;

            case StreamType.StringUnicodeEmpty:
                sObject = new SStringType(String.Empty);
                break;

            case StreamType.StringUnicodeOne:
                sObject = new SStringType(m_reader.ReadString(2));
                break;

            case StreamType.CompressedDBRow:
                sObject = ParseDBRow();
                break;

            case StreamType.SubStream:
                sObject = ParseSubStream();
                CheckShared(shared, sObject);
                break;

            case StreamType.TupleTwo:
                sObject = new STupleType(2);
                CheckShared(shared, sObject);
                Parse(sObject, 2);
                break;

            case StreamType.BigInt:
                sObject = new SLongType(m_reader.ReadBigInt());
                CheckShared(shared, sObject);
                break;

            default:
                throw new ParserException(
                          String.Format(CultureInfo.InvariantCulture,
                                        "Can't identify type {0:x2} at position {1:x2} [{1}] and length {2}",
                                        type, m_reader.Position, m_reader.Length));
            }

            if (sObject == null && type != (byte)StreamType.Marker)
            {
                throw new ParserException("An object could not be created");
            }

            return(sObject);
        }