Пример #1
0
        public void Disposed_can_be_called_more_than_once()
        {
            var subject = new ByteArrayChunk(1);

            subject.Dispose();
            subject.Dispose();
        }
Пример #2
0
 public PDV(byte pcid, byte[] value, bool command, bool last)
 {
     _pcid  = pcid;
     _value = new ByteArrayChunk {
         Array = value, Count = value.Length, Index = 0
     };
     _command = command;
     _last    = last;
 }
Пример #3
0
        public void Bytes_get_should_throw_when_subject_is_disposed()
        {
            var subject = new ByteArrayChunk(1);

            subject.Dispose();

            Action action = () => { var _ = subject.Bytes; };

            action.ShouldThrow <ObjectDisposedException>().And.ObjectName.Should().Be("ByteArrayChunk");
        }
Пример #4
0
        public void Dispose_should_dispose_subject()
        {
            var subject = new ByteArrayChunk(1);

            subject.Dispose();

            var reflector = new Reflector(subject);

            reflector._disposed.Should().BeTrue();
        }
Пример #5
0
        public ByteArrayChunk ReadBytesToArraySegment(String name, int count)
        {
            CheckOffset(count, name);
            var segment = new ByteArrayChunk {
                Array = _ms.GetBuffer(), Count = count, Index = (int)_ms.Position
            };

            _ms.Seek(count, SeekOrigin.Current);
            return(segment);
        }
Пример #6
0
        public void Dispose_should_not_dispose_forked_handle()
        {
            var subject = new ByteArrayChunk(1);
            var forked  = subject.Fork();

            subject.Dispose();

            var reflector = new Reflector((ByteArrayChunk)forked);

            reflector._disposed.Should().BeFalse();
        }
Пример #7
0
        public void constructor_with_size_should_initialize_subject(
            [Values(1, 2, 16)]
            int size)
        {
            var subject = new ByteArrayChunk(size);

            var segment = subject.Bytes;

            segment.Array.Should().NotBeNull();
            segment.Offset.Should().Be(0);
            segment.Count.Should().Be(size);
        }
Пример #8
0
        public void Bytes_get_should_return_expected_result()
        {
            var size    = 1;
            var bytes   = new byte[size];
            var subject = new ByteArrayChunk(bytes);

            var result = subject.Bytes;

            result.Array.Should().BeSameAs(bytes);
            result.Offset.Should().Be(0);
            result.Count.Should().Be(size);
        }
Пример #9
0
        public uint Read(RawPDU raw)
        {
            uint len = raw.ReadUInt32("PDV-Length");

            _pcid = raw.ReadByte("Presentation Context ID");
            byte mch = raw.ReadByte("Message Control Header");

            _value   = raw.ReadBytesToArraySegment("PDV Value", (int)len - 2);
            _command = (mch & 0x01) != 0;
            _last    = (mch & 0x02) != 0;
            return(len + 4);
        }
Пример #10
0
        public void constructor_with_bytes_should_initialize_subject(
            [Values(1, 2, 16)]
            int size)
        {
            var bytes = new byte[size];

            var subject = new ByteArrayChunk(bytes);

            var segment = subject.Bytes;

            segment.Array.Should().BeSameAs(bytes);
            segment.Offset.Should().Be(0);
            segment.Count.Should().Be(size);
        }
Пример #11
0
        public void Fork_should_return_a_new_handle()
        {
            var subject = new ByteArrayChunk(1);

            var result = subject.Fork();

            result.Should().NotBeSameAs(subject);
            var subjectSegment = subject.Bytes;
            var resultSegment  = result.Bytes;

            resultSegment.Array.Should().BeSameAs(subjectSegment.Array);
            resultSegment.Offset.Should().Be(subjectSegment.Offset);
            resultSegment.Count.Should().Be(subjectSegment.Count);
        }
Пример #12
0
 public Reflector(ByteArrayChunk instance)
 {
     _instance = instance;
 }
Пример #13
0
        void OnWriterEvent(object sender, ModuleWriterListenerEventArgs e)
        {
            var writer = (ModuleWriterBase)sender;

            if (e.WriterEvent == ModuleWriterEvent.MDBeginAddResources)
            {
                ctx.Context.CheckCancellation();
                ctx.Context.Logger.Debug("Encrypting resources...");
                bool hasPacker = ctx.Context.Packer != null;

                List <EmbeddedResource> resources = ctx.Module.Resources.OfType <EmbeddedResource>().ToList();
                if (!hasPacker)
                {
                    ctx.Module.Resources.RemoveWhere(res => res is EmbeddedResource);
                }

                // move resources
                string    asmName = ctx.Name.RandomName(RenameMode.Letters);
                PublicKey pubKey  = null;
                if (writer.TheOptions.StrongNameKey != null)
                {
                    pubKey = PublicKeyBase.CreatePublicKey(writer.TheOptions.StrongNameKey.PublicKey);
                }
                var assembly = new AssemblyDefUser(asmName, new Version(0, 0), pubKey);
                assembly.Modules.Add(new ModuleDefUser(asmName + ".dll"));
                ModuleDef module = assembly.ManifestModule;
                assembly.ManifestModule.Kind = ModuleKind.Dll;
                var asmRef = new AssemblyRefUser(module.Assembly);
                if (!hasPacker)
                {
                    foreach (EmbeddedResource res in resources)
                    {
                        res.Attributes = ManifestResourceAttributes.Public;
                        module.Resources.Add(res);
                        ctx.Module.Resources.Add(new AssemblyLinkedResource(res.Name, asmRef, res.Attributes));
                    }
                }
                byte[] moduleBuff;
                using (var ms = new MemoryStream()) {
                    module.Write(ms, new ModuleWriterOptions {
                        StrongNameKey = writer.TheOptions.StrongNameKey
                    });
                    moduleBuff = ms.ToArray();
                }

                // compress
                moduleBuff = ctx.Context.Registry.GetService <ICompressionService>().Compress(
                    moduleBuff,
                    progress => ctx.Context.Logger.Progress((int)(progress * 10000), 10000));
                ctx.Context.Logger.EndProgress();
                ctx.Context.CheckCancellation();

                uint compressedLen = (uint)(moduleBuff.Length + 3) / 4;
                compressedLen = (compressedLen + 0xfu) & ~0xfu;
                var compressedBuff = new uint[compressedLen];
                Buffer.BlockCopy(moduleBuff, 0, compressedBuff, 0, moduleBuff.Length);
                Debug.Assert(compressedLen % 0x10 == 0);

                // encrypt
                uint keySeed = ctx.Random.NextUInt32() | 0x10;
                var  key     = new uint[0x10];
                uint state   = keySeed;
                for (int i = 0; i < 0x10; i++)
                {
                    state ^= state >> 13;
                    state ^= state << 25;
                    state ^= state >> 27;
                    key[i] = state;
                }

                var encryptedBuffer = new byte[compressedBuff.Length * 4];
                int buffIndex       = 0;
                while (buffIndex < compressedBuff.Length)
                {
                    uint[] enc = ctx.ModeHandler.Encrypt(compressedBuff, buffIndex, key);
                    for (int j = 0; j < 0x10; j++)
                    {
                        key[j] ^= compressedBuff[buffIndex + j];
                    }
                    Buffer.BlockCopy(enc, 0, encryptedBuffer, buffIndex * 4, 0x40);
                    buffIndex += 0x10;
                }
                Debug.Assert(buffIndex == compressedBuff.Length);
                var size = (uint)encryptedBuffer.Length;

                TablesHeap tblHeap = writer.MetaData.TablesHeap;
                tblHeap.ClassLayoutTable[writer.MetaData.GetClassLayoutRid(ctx.DataType)].ClassSize = size;
                tblHeap.FieldTable[writer.MetaData.GetRid(ctx.DataField)].Flags |= (ushort)FieldAttributes.HasFieldRVA;
                encryptedResource = writer.Constants.Add(new ByteArrayChunk(encryptedBuffer), 8);

                // inject key values
                MutationHelper.InjectKeys(ctx.InitMethod,
                                          new[] { 0, 1 },
                                          new[] { (int)(size / 4), (int)(keySeed) });
            }
            else if (e.WriterEvent == ModuleWriterEvent.EndCalculateRvasAndFileOffsets)
            {
                TablesHeap tblHeap = writer.MetaData.TablesHeap;
                tblHeap.FieldRVATable[writer.MetaData.GetFieldRVARid(ctx.DataField)].RVA = (uint)encryptedResource.RVA;
            }
        }
Пример #14
0
        private MultiChunkBuffer CreateSubject(int size = 0, int?length = null, bool isReadOnly = false)
        {
            var chunk = new ByteArrayChunk(size);

            return(new MultiChunkBuffer(new[] { chunk }, length ?? size, isReadOnly));
        }
Пример #15
0
        private SingleChunkBuffer CreateSubject(int size = 0, int?length = null, bool isReadOnly = false)
        {
            var chunk = new ByteArrayChunk(size);

            return(new SingleChunkBuffer(chunk, length ?? size, isReadOnly));
        }
Пример #16
0
        private SingleChunkBuffer CreateSubject(byte[] bytes, int?length = null, bool isReadOnly = false)
        {
            var chunk = new ByteArrayChunk(bytes);

            return(new SingleChunkBuffer(chunk, length ?? bytes.Length, isReadOnly));
        }