Exemple #1
0
    private static FieldsData ParseFields(ref SpanReader reader)
    {
        int departureFieldsIndex = 0;

        int[] departureFields = new int[6];

        var fieldList = new List <Field>();

        int maxFieldVal = int.MinValue;

        while (reader.Peek() != '\n')
        {
            ReadOnlySpan <byte> fieldName = reader.ReadUntil(':');
            reader.SkipLength(1);
            int l1 = reader.ReadPosIntUntil('-');
            int r1 = reader.ReadPosIntUntil(' ');
            reader.SkipLength("or ".Length);
            int l2 = reader.ReadPosIntUntil('-');
            int r2 = reader.ReadPosIntUntil('\n');

            fieldList.Add(new Field(l1, r1, l2, r2));
            if (fieldName.StartsWith(new byte[] { (byte)'d', (byte)'e' }))
            {
                departureFields[departureFieldsIndex++] = fieldList.Count - 1;
            }

            if (r2 > maxFieldVal)
            {
                maxFieldVal = r2;
            }
        }

        return(new FieldsData(fieldList, departureFields, maxFieldVal));
    }
Exemple #2
0
        public void TraverseAndUnpack(EntryUnpacker unpacker)
        {
            SpanReader sr = new SpanReader(_buffer, Endian.Big);

            sr.Position += _offsetStart;

            uint offsetAndCount = sr.ReadUInt32();

            uint nodeCount = sr.ReadUInt16();

            for (int i = 0; i < nodeCount; i++)
            {
                uint high       = CryptoUtils.GetBitsAt(ref sr, 0) & 0x7FFu;
                uint nextOffset = CryptoUtils.GetBitsAt(ref sr, high + 1);

                for (uint j = 0; j < high; j++) // high is pretty much entry count
                {
                    uint offset = CryptoUtils.GetBitsAt(ref sr, j + 1);
                    var  data   = sr.GetReaderAtOffset((int)offset);

                    FileEntryKey key = new FileEntryKey();
                    key.OffsetFromTree = data.Position;

                    key.Deserialize(ref data);
                    unpacker.UnpackFromKey(key);
                }

                sr.Position += (int)nextOffset;
            }
        }
Exemple #3
0
        public override int LessThanKeyCompareOp(FileEntryKey key, ref SpanReader sr)
        {
            uint nameIndex = sr.ReadUInt32();

            if (key.NameIndex < nameIndex)
            {
                return(-1);
            }
            else if (key.NameIndex > nameIndex)
            {
                return(1);
            }

            uint extIndex = sr.ReadUInt32();

            if (key.FileExtensionIndex < extIndex)
            {
                return(-1);
            }
            else if (key.FileExtensionIndex > extIndex)
            {
                return(1);
            }
            else
            {
                throw new Exception("?????");
            }
        }
Exemple #4
0
        public static void DumpLeaksCode(this IObjectDB db)
        {
            var leakedObjects      = new Dictionary <ulong, bool>();
            var leakedDictionaries = new Dictionary <ulong, bool>();

            using var tr      = db.StartReadOnlyTransaction();
            using var visitor = new FindUnusedKeysVisitor();
            visitor.ImportAllKeys(tr);
            visitor.Iterate(tr);
            foreach (var unseenKey in visitor.UnseenKeys())
            {
                var isDict   = unseenKey.Key[0] == 2;
                var isObject = unseenKey.Key[0] == 1;

                var r = new SpanReader(unseenKey.Key);
                r.SkipUInt8();
                var oid = r.ReadVUInt64();

                if (isDict)
                {
                    leakedDictionaries.TryAdd(oid, false);
                }
                else if (isObject)
                {
                    leakedObjects.TryAdd(oid, false);
                }
            }

            WriteSplitIdList(leakedDictionaries.Keys, "dicts", 1000);
            WriteSplitIdList(leakedObjects.Keys, "objs", 1000);
        }
Exemple #5
0
        private Tensor <T> LoadVarData <T>(string name)
            where T : unmanaged
        {
            var v      = GetVar(name);
            var reader = new SpanReader(File.ReadAllBytes(Path.Combine(_modelPath, name)));

            var version  = reader.Read <uint>();
            var lodLevel = reader.Read <ulong>();

            for (uint i = 0; i < lodLevel; i++)
            {
                var len = reader.Read <ulong>();
                reader.Skip((int)len);
            }

            version = reader.Read <uint>();
            if (version != 0)
            {
                throw new NotSupportedException();
            }
            {
                var descSize = reader.Read <int>();
                reader.Skip(descSize);
            }

            var rest = reader.ReadAsSpan();
            var data = MemoryMarshal.Cast <byte, T>(rest);

            return(new DenseTensor <T>(data.ToArray(), GetVarShape(name)));
        }
Exemple #6
0
            public EnumConfiguration(byte[] configuration)
            {
                var reader = new SpanReader(configuration);
                var header = reader.ReadVUInt32();

                _signed = (header & 1) != 0;
                _flags  = (header & 2) != 0;
                var count = header >> 2;

                _names  = new string[count];
                _values = new ulong[count];
                for (var i = 0; i < count; i++)
                {
                    Names[i] = reader.ReadString() !;
                }
                if (_signed)
                {
                    for (var i = 0; i < count; i++)
                    {
                        Values[i] = (ulong)reader.ReadVInt64();
                    }
                }
                else
                {
                    for (var i = 0; i < count; i++)
                    {
                        Values[i] = reader.ReadVUInt64();
                    }
                }
            }
Exemple #7
0
    public void Solve(ReadOnlySpan <byte> input, Solution solution)
    {
        int lines = input.Count((byte)'\n');

        long[] nums = new long[lines];
        int    line = 0;

        var reader = new SpanReader(input);

        while (line < 25)
        {
            nums[line++] = reader.ReadPosLongUntil('\n');
        }

        while (!reader.Done)
        {
            long num = reader.ReadPosLongUntil('\n');
            nums[line] = num;
            if (!IsSumInPrevious25(nums, line, num))
            {
                break;
            }

            line++;
        }

        long part1 = nums[line];
        long part2 = SolvePart2(nums, part1);

        solution.SubmitPart1(part1);
        solution.SubmitPart2(part2);
    }
Exemple #8
0
 public static TeleportConfirm Deserialize(ref SpanReader br)
 {
     return(new TeleportConfirm
     {
         TeleportId = br.ReadAsVarInt(out _)
     });
 }
Exemple #9
0
        ITypeDescriptor NestedDescriptorReader(ref SpanReader reader)
        {
            var typeId = reader.ReadVInt32();

            if (typeId < 0 && -typeId - 1 < _id2InfoNew.Count)
            {
                var infoForType = _id2InfoNew[-typeId - 1];
                if (infoForType != null)
                {
                    return(infoForType.Descriptor !);
                }
            }
            else if (typeId > 0)
            {
                if (typeId >= _id2Info.Count)
                {
                    throw new BTDBException("Metadata corrupted");
                }
                var infoForType = _id2Info[typeId];
                if (infoForType == null)
                {
                    throw new BTDBException("Metadata corrupted");
                }
                return(infoForType.Descriptor !);
            }

            return(new PlaceHolderDescriptor(typeId));
        }
Exemple #10
0
        ulong SkipByteAndReadVUInt64(ReadOnlySpan <byte> key)
        {
            var reader = new SpanReader(key);

            reader.SkipBlock(1);
            return(reader.ReadVUInt64());
        }
Exemple #11
0
 public static Ping Deserialize(ref SpanReader br)
 {
     return(new Ping
     {
         Payload = br.ReadAsLong(),
     });
 }
Exemple #12
0
        void SkipUntilNextTransaction(ref SpanReader reader)
        {
            int depth = 0;

            while (!reader.Eof)
            {
                var b = reader.ReadUInt8();
                switch (depth)
                {
                case 0:
                    if (b == 3)
                    {
                        depth = 1;
                    }
                    break;

                case 1:
                    depth = b == (byte)'t' ? 2 : 0;
                    break;

                case 2:
                    if (b == (byte)'R')
                    {
                        return;
                    }
                    else
                    {
                        depth = 0;
                    }
                    break;
                }
            }
        }
        private object DeserializeLoginPacket(UncompressedPacket packet)
        {
            var    br = new SpanReader(packet.Data);
            object innerPacket;

            switch (packet.PacketId)
            {
            // Disconnect
            case 0x00:
                innerPacket = LoginDisconnect.Deserialize(ref br);
                break;

            // Login Success
            case 0x02:
                innerPacket = LoginSuccess.Deserialize(ref br);
                break;

            default:
                throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
            }

            if (!br.IsCosumed)
            {
                throw new InvalidDataException($"Packet data is not fully consumed.");
            }
            return(innerPacket);
        }
Exemple #14
0
 public static Response Deserialize(ref SpanReader br)
 {
     return(new Response
     {
         JsonResponse = br.ReadAsString()
     });
 }
        private static void RewriteMessageLocalized(ReadOnlySpan <byte> input, CircularBuffer <byte> output, out int length)
        {
            var isAffix = input[0] == 0xCC;

            var reader = new SpanReader(input);

            reader.Seek(3, SeekOrigin.Current);
            var serial  = (Serial)reader.ReadUInt32();
            var graphic = reader.ReadInt16();
            var type    = (MessageType)reader.ReadByte();
            var hue     = reader.ReadInt16();
            var font    = reader.ReadInt16();
            var label   = reader.ReadInt32();
            var flags   = isAffix ? (AffixType)reader.ReadByte() : AffixType.System;
            var name    = reader.ReadAscii(30);
            var affix   = isAffix ? reader.ReadAscii() : string.Empty;
            var args    = isAffix ? reader.ReadBigUni() : reader.ReadLittleUni();

            if (!ZhConfig.Messaging.Cliloc.ContainsKey(label))
            {
                length = NetworkCompression.Compress(input, output);
                return;
            }

            var text = ClilocList.Translate(label, args);

            if (isAffix)
            {
                text = flags switch
                {
                    AffixType.Append => $"{text}{affix}",
                    AffixType.Prepend => $"{affix}{text}",
                    _ => $"{text}{affix}"
                };

                if ((flags & AffixType.System) != 0)
                {
                    type = MessageType.System;
                }
            }

            var buffer  = stackalloc byte[GetMaxMessageLength(text)].InitializePacket();
            var pLength = CreateMessage(
                buffer,
                serial,
                graphic,
                type,
                hue,
                font,
                true,
                null,
                name,
                text
                );

            buffer = buffer.SliceToLength(pLength);

            length = NetworkCompression.Compress(buffer, output);
        }
    }
Exemple #16
0
        internal static void SkipHeader(ref SpanReader reader)
        {
            FileCollectionWithFileInfos.SkipHeader(ref reader);
            var type            = (KVFileType)reader.ReadUInt8();
            var withCommitUlong = type == KVFileType.KeyIndexWithCommitUlong || type == KVFileType.ModernKeyIndex || type == KVFileType.ModernKeyIndexWithUlongs;

            reader.SkipVInt64();  // generation
            reader.SkipVUInt32(); // trLogFileId
            reader.SkipVUInt32(); // trLogOffset
            reader.SkipVUInt64(); // keyValueCount
            if (withCommitUlong)
            {
                reader.SkipVUInt64();                  // commitUlong
            }
            if (type == KVFileType.ModernKeyIndex || type == KVFileType.ModernKeyIndexWithUlongs)
            {
                reader.SkipUInt8();
            }
            if (type == KVFileType.ModernKeyIndexWithUlongs)
            {
                var ulongCount = reader.ReadVUInt32();
                while (ulongCount-- > 0)
                {
                    reader.SkipVUInt64();
                }
            }
        }
Exemple #17
0
 public static ServerboundKeepAlive Deserialize(ref SpanReader br)
 {
     return(new ServerboundKeepAlive
     {
         KeepAliveId = br.ReadAsVarInt(out _)
     });
 }
Exemple #18
0
    public void Solve(ReadOnlySpan <byte> input, Solution solution)
    {
        int target = new SpanReader(input).ReadPosIntUntil('\n');

        solution.SubmitPart1(SolvePart1(target));
        solution.SubmitPart2(SolvePart2(target));
    }
Exemple #19
0
 public static ServerboundCloseWindow Deserialize(ref SpanReader br)
 {
     return(new ServerboundCloseWindow
     {
         WindowId = br.ReadAsByte()
     });
 }
Exemple #20
0
        internal static int ParseLogEntryPrologue(ReadOnlySpan <byte> input, out LogEntryMetadata metadata)
        {
            var reader = new SpanReader <byte>(input);

            metadata = new LogEntryMetadata(ref reader);
            return(LogEntryMetadata.Size);
        }
Exemple #21
0
 public static ServerboundHeldItemChange Deserialize(ref SpanReader br)
 {
     return(new ServerboundHeldItemChange
     {
         Slot = br.ReadAsShort()
     });
 }
        public static CharacterCreateRequest Unmarshal(MessageEventArgs args)
        {
            var reader = new SpanReader(args.Message.Payload.Span);
            var slot   = reader.ReadByte();
            var c      = new Character();

            c.Level = 1;
            c.Name  = reader.ReadDaocString();
            reader.Skip(4);             // 0x18 0x00 0x00 0x00 - DoL notes say this is an array length
            c.Customization = reader.Read <Customizations>();
            reader.Skip(9);
            var operation = reader.ReadByte();                  // 1
            var custType  = reader.ReadByte();                  // 0

            reader.Skip(2);                                     // ???
            c.LocationDescription = reader.ReadDaocString();    // empty
            reader.ReadDaocString();                            // class description; redundant and empty
            reader.ReadDaocString();                            // race description; redundant and empty
            reader.Skip(1);                                     // ???
            c.Classification = reader.Read <Classification>();
            c.Model          = reader.ReadUInt16LittleEndian(); // 12352
            c.Region         = reader.ReadUInt16LittleEndian(); // byte? 27
            reader.Skip(4);
            c.Stats     = reader.Read <Stats>();
            c.Equipment = reader.Read <Equipment>(); // empty
            ;                                        // 4 bytes left
            return(new CharacterCreateRequest(slot, c));
        }
Exemple #23
0
 public static ServerboundAnimation Deserialize(ref SpanReader br)
 {
     return(new ServerboundAnimation
     {
         Hand = (Hand)br.ReadAsVarInt(out _)
     });
 }
Exemple #24
0
 public static PlayerOnGround Deserialize(ref SpanReader br)
 {
     return(new PlayerOnGround
     {
         OnGround = br.ReadAsBoolean(),
     });
 }
Exemple #25
0
        private Task DispatchLoginPackets(UncompressedPacket packet)
        {
            var  br = new SpanReader(packet.Data);
            Task task;

            switch (packet.PacketId)
            {
            // Login Start
            case 0x00:
                task = DispatchPacket(PacketDeserializer.Deserialize <LoginStart>(ref br));
                break;

            // Encryption Response
            case 0x01:
                task = DispatchPacket(PacketDeserializer.Deserialize <EncryptionResponse>(ref br));
                break;

            default:
                throw new InvalidDataException($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
            }

            if (!br.IsCosumed)
            {
                throw new InvalidDataException($"Packet data is not fully consumed.");
            }
            return(task);
        }
Exemple #26
0
 internal static void SkipHeader(ref SpanReader reader)
 {
     reader.SkipBlock(DiskChunkCache.MagicStartOfFile.Length + 1); // magic + type of file
     reader.SkipVInt64();                                          // generation
     reader.SkipVUInt32();                                         // keySize
     reader.SkipVUInt64();                                         // keyValueCount
 }
Exemple #27
0
        public override int EqualToKeyCompareOp(FileEntryKey key, ref SpanReader sr)
        {
            uint nameIndex = sr.ReadUInt32();

            if (key.NameIndex < nameIndex)
            {
                return(-1);
            }
            else if (key.NameIndex > nameIndex)
            {
                return(1);
            }

            uint extIndex = sr.ReadUInt32();

            if (key.FileExtensionIndex < extIndex)
            {
                return(-1);
            }
            else if (key.FileExtensionIndex > extIndex)
            {
                return(1);
            }

            return(0);
        }
        private static void RewriteUnicodeMessage(ReadOnlySpan <byte> input, CircularBuffer <byte> output, out int length)
        {
            var reader = new SpanReader(input);

            reader.Seek(3, SeekOrigin.Current);

            var serial  = reader.ReadUInt32();
            var graphic = reader.ReadInt16();
            var type    = (MessageType)reader.ReadByte();
            var hue     = reader.ReadInt16();
            var font    = reader.ReadInt16();
            var lang    = reader.ReadAscii(4);
            var name    = reader.ReadAscii(30);
            var text    = reader.ReadBigUni();

            var buffer  = stackalloc byte[GetMaxMessageLength(text)].InitializePacket();
            var pLength = CreateMessage(
                buffer,
                serial,
                graphic,
                type,
                hue,
                font,
                true,
                null,
                name,
                text
                );

            buffer = buffer.SliceToLength(pLength);
            length = NetworkCompression.Compress(buffer, output);
        }
Exemple #29
0
 public static ServerboundChatMessage Deserialize(ref SpanReader br)
 {
     return(new ServerboundChatMessage
     {
         Message = br.ReadAsString()
     });
 }
Exemple #30
0
        public int CompareIDString(string label, int index, int labelLength)
        {
            SpanReader sr = new SpanReader(Buffer, Endian);

            //  iVar2 = (uint)*(ushort *)(buf + *(int *)(buf + index * 8 + 0x10) + *(int *)(buf + 4) * 8 + 0x10) - 1;

            // *(int *)(buf + 4) * 8 + 0x10)
            int keyCount     = KeyCount;
            int keyMapOffset = HeaderSize + (keyCount * EntrySize);

            // *(int *)(buf + index * 8 + 0x10)
            sr.Position = HeaderSize + (index * EntrySize);
            int keyNameOffset = sr.ReadInt32();

            sr.Position = keyMapOffset + keyNameOffset;

            short stringLength = sr.ReadInt16();

            labelLength++; // Null terminated
            if (stringLength == labelLength)
            {
                string labelEntry = sr.ReadString0();
                return(label.CompareTo(labelEntry));
            }

            return(labelLength - stringLength);
        }