Example #1
0
        public byte[] GetDataToWrite()
        {
            if (IsMagicBlock)
            {
                var length = BlockMagic.Length + // block magic
                             4 +                 // plugin name length
                             PluginName.Length + // plugin name itself
                             BlockData.Length;

                using (var ms = new MemoryStream())
                {
                    ms.Write(LittleEndian.GetBytes(length), 0, 4);
                    ms.Write(BlockMagic, 0, BlockMagic.Length);
                    ms.Write(BigEndian.GetBytes(PluginName.Length), 0, 4);
                    ms.Write(PluginName, 0, PluginName.Length);
                    ms.Write(BlockData, 0, BlockData.Length);

                    return(ms.ToByteArray());
                }
            }

            using (var ms = new MemoryStream())
            {
                var length2 = BlockData.Length;
                ms.Write(LittleEndian.GetBytes(length2), 0, 4);
                ms.Write(BlockData, 0, BlockData.Length);
                ms.Write(new byte[] { 0xDE, 0xAD, 0xBE, 0xEF }, 0, 4);

                return(ms.ToByteArray());
            }
        }
Example #2
0
        /// <summary>
        /// Generates binary image of the <see cref="MetadataRecordComposedFields"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public virtual int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes(m_highAlarm), 0, buffer, startIndex, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_lowAlarm), 0, buffer, startIndex + 4, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_highRange), 0, buffer, startIndex + 8, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_lowRange), 0, buffer, startIndex + 12, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_lowWarning), 0, buffer, startIndex + 16, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_highWarning), 0, buffer, startIndex + 20, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_displayDigits), 0, buffer, startIndex + 24, 4);

            for (int i = 0; i < m_inputPointers.Count; i++)
            {
                Buffer.BlockCopy(LittleEndian.GetBytes(m_inputPointers[i]), 0, buffer, startIndex + 28 + (i * 4), 4);
            }

            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_engineeringUnits.PadRight(24).TruncateRight(24)), 0, buffer, startIndex + 76, 24);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_equation.PadRight(256).TruncateRight(256)), 0, buffer, startIndex + 100, 256);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_compressionLimit), 0, buffer, startIndex + 356, 4);

            return(length);
        }
Example #3
0
        /// <summary>
        /// Computes the checksum.
        /// </summary>
        /// <returns></returns>
        public byte[] ComputeChecksum()
        {
            var checksum = _bytes.GetChecksum32(0, 106);

            checksum = _bytes.GetChecksum32(108, 4, checksum);
            checksum = _bytes.GetChecksum32(113, (int)(BytesPerSector.Value * 11 - 113), checksum);
            return(LittleEndian.GetBytes(checksum));
        }
Example #4
0
        public virtual byte[] GetDataToWrite()
        {
            var parameterBytes = GetParameters();
            var blockData      = GetBlockDataToWrite();

            byte[] midiData = new byte[0];
            byte[] endChunk;

            using (var endChunkStream = new MemoryStream())
            {
                var endChunkData = GetEndChunk();
                endChunkStream.Write(endChunkData, 0, endChunkData.Length);

                if (IncludePatchNameAtEnd)
                {
                    var patchEndName = GetCompiledEndPatchName();
                    endChunkStream.Write(patchEndName, 0, patchEndName.Length);
                }

                endChunk = endChunkStream.ToByteArray();
            }

            var totalLength = parameterBytes.Length +
                              blockData.Length +
                              endChunk.Length; // end chunk

            if (IncludeMidi)
            {
                midiData     = MidiBlock.GetDataToWrite();
                totalLength += midiData.Length;
            }

            var ms = new MemoryStream();

            // total length
            ms.Write(LittleEndian.GetBytes(totalLength), 0, 4);

            // Fill up
            for (var i = 0; i < 12; i++)
            {
                ms.WriteByte(0);
            }

            ms.Write(parameterBytes, 0, parameterBytes.Length);
            ms.Write(blockData, 0, blockData.Length);

            if (IncludeMidi)
            {
                ms.Write(midiData, 0, midiData.Length);
            }

            ms.Write(endChunk, 0, endChunk.Length);
            var data = ms.ToByteArray();

            ms.Close();

            return(data);
        }
Example #5
0
        /// <summary>
        /// Generates a binary representation of this <see cref="RiffChunk"/> and copies it into the given buffer.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="ISupportBinaryImage.BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="ISupportBinaryImage.BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public virtual int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            buffer.ValidateParameters(startIndex, FixedLength);

            Buffer.BlockCopy(Encoding.ASCII.GetBytes(TypeID), 0, buffer, 0, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(ChunkSize), 0, buffer, 4, 4);

            return(FixedLength);
        }
Example #6
0
        public static void Main111(string[] args)
        {
            const string drive = "X:";

            // label
            var driveInfo = new DriveInfo(drive);

            driveInfo.VolumeLabel = DiskContent.VolumeLabel;

            // long contiguous file
            using (var fc = File.Create(Path.Combine(drive, DiskContent.LongContiguousFileName)))
            {
                for (ulong offset = 0; offset < DiskContent.LongFileSize; offset += sizeof(ulong))
                {
                    var b = LittleEndian.GetBytes(DiskContent.GetLongContiguousFileNameOffsetValue(offset));
                    fc.Write(b, 0, b.Length);
                }
            }

            // long sparse files
            const uint chunks = 1u << 10;

            for (ulong offsetBase = 0; offsetBase < DiskContent.LongFileSize; offsetBase += chunks)
            {
                using (var fs1 = File.OpenWrite(Path.Combine(drive, DiskContent.LongSparseFile1Name)))
                    using (var fs2 = File.OpenWrite(Path.Combine(drive, DiskContent.LongSparseFile2Name)))
                    {
                        fs1.Seek(0, SeekOrigin.End);
                        fs2.Seek(0, SeekOrigin.End);
                        for (ulong subOffset = 0; subOffset < chunks; subOffset += sizeof(ulong))
                        {
                            var offset = offsetBase + subOffset;
                            var b1     = LittleEndian.GetBytes(DiskContent.GetLongSparseFile1NameOffsetValue(offset));
                            fs1.Write(b1, 0, b1.Length);
                            var b2 = LittleEndian.GetBytes(DiskContent.GetLongSparseFile2NameOffsetValue(offset));
                            fs2.Write(b2, 0, b2.Length);
                        }
                    }
            }

            // An empty folder
            Directory.CreateDirectory(Path.Combine(drive, DiskContent.EmptyRootFolderFileName));

            // A folder full of garbage
            var longDirectoryPath = Path.Combine(drive, DiskContent.LongFolderFileName);

            Directory.CreateDirectory(longDirectoryPath);
            for (int subFileIndex = 0; subFileIndex < DiskContent.LongFolderEntriesCount; subFileIndex++)
            {
                var path = Path.Combine(longDirectoryPath, Guid.NewGuid().ToString("N"));
                using (var t = File.CreateText(path))
                {
                    t.WriteLine(subFileIndex);
                }
            }
        }
Example #7
0
        /// <summary>
        /// Generates binary image of the <see cref="PacketType5"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public override int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes(TypeID), 0, buffer, startIndex, 2);

            return(length);
        }
        /// <summary>
        /// Creates the first node and set it to root.
        /// </summary>
        TreeNode <K, V> CreateFirstRoot()
        {
            // Create the first block that refers to the next block which is id(2).
            // Due to the design of RecordStorage, creating the very first block will result in creating two,
            // one for the special #0 (deletion marker) and the other which was asked to create.
            recordStorage.Create(LittleEndian.GetBytes(2U));

            // Creating a new node after above should make this block's id 2.
            return(Create(null, null));
        }
Example #9
0
 private byte[] GetCompiledEndPatchName()
 {
     using (var ms = new MemoryStream())
     {
         ms.Write(LittleEndian.GetBytes(PatchName.Length + 1), 0, 4);
         ms.Write(PatchName, 0, PatchName.Length);
         ms.WriteByte(0);
         return(ms.ToByteArray());
     }
 }
Example #10
0
        /// <summary>
        /// Generates binary image of the <see cref="ArchiveDataBlockPointer"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes(m_historianID), 0, buffer, startIndex, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes((double)m_startTime.Value), 0, buffer, startIndex + 4, 8);

            return(length);
        }
Example #11
0
        /// <summary>
        /// Generates binary image of the <see cref="MetadataRecordConstantFields"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public virtual int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes(m_value), 0, buffer, startIndex, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_displayDigits), 0, buffer, startIndex + 4, 4);

            return(length);
        }
Example #12
0
        /// <summary>
        /// Generates binary image of the <see cref="StateRecordSummary"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes(m_historianID), 0, buffer, startIndex, 4);
            m_currentData.GenerateBinaryImage(buffer, startIndex + 4);

            return(length);
        }
Example #13
0
        public override void WriteData(Stream target)
        {
            byte[] buffer = new byte[m_headerLength];

            Buffer.BlockCopy(Encoding.ASCII.GetBytes(TypeID), 0, buffer, 0, 4);

            kontaktSound.WriteChunk();

            Buffer.BlockCopy(LittleEndian.GetBytes(kontaktSound.Chunk.Length + 4), 0, buffer, 4, 4);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(FileFormat), 0, buffer, 8, 4);
            target.Write(buffer, 0, 12);
        }
Example #14
0
        /// <summary>
        /// Builds a compressed payload from the current content's data.
        /// </summary>
        /// <param name="opcode">The packet's opcode.</param>
        /// <returns>A new compressed payload.</returns>
        public byte[] GetCompressedPayload(short opcode)
        {
            byte[] compressedData = ZLib.CompressData(_contentData);

            byte[] packetId         = BigEndian.GetBytes(opcode);
            byte[] size             = BigEndian.GetBytes(compressedData.Length + sizeof(int));
            byte[] compressionFlag  = { 1 }; // true
            byte[] decompressedSize = LittleEndian.GetBytes(_contentData.Length);
            byte[] padding          = { 0, 0, 0, 0 };

            return(Sequence.Concat(packetId, size, compressionFlag, decompressedSize, compressedData, padding));
        }
Example #15
0
        /// <summary>
        /// Generates binary image of the <see cref="ArchiveDataPoint"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public virtual int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes((int)m_time.Value), 0, buffer, startIndex, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes((short)m_flags), 0, buffer, startIndex + 4, 2);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_value), 0, buffer, startIndex + 6, 4);

            return(length);
        }
Example #16
0
        public override void WriteChunk()
        {
            MemoryStream ms = new MemoryStream();

            byte[] tmpChunk = PresetData;

            Chunk = new byte[tmpChunk.Length + 4];
            ms.Write(LittleEndian.GetBytes(Version), 0, 4);
            ms.Write(tmpChunk, 0, tmpChunk.Length);
            Chunk     = ms.ToArray();
            ChunkSize = Chunk.Length;
        }
        public static int DecodeValueAsInt(byte[] data, int length, int bitSize)
        {
            uint tmp = 0;

            for (var i = 0; i < length; i++)
            {
                tmp = (tmp << bitSize) ^ data[i];
            }

            var bTmp = LittleEndian.GetBytes(tmp);

            return(LittleEndian.ToInt32(bTmp, 0));
        }
        /// <summary>
        /// Generates binary image of the <see cref="MetadataRecordDigitalFields"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public virtual int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_setDescription.PadRight(24).TruncateRight(24)), 0, buffer, startIndex, 24);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_clearDescription.PadRight(24).TruncateRight(24)), 0, buffer, startIndex + 24, 24);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_alarmState), 0, buffer, startIndex + 48, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_alarmDelay), 0, buffer, startIndex + 52, 4);

            return(length);
        }
        public static MemoryStream WriteVC2(string pluginData)
        {
            var ms = new MemoryStream();

            ms.Write(new byte[] { 0x56, 0x43, 0x32, 0x21 }, 0, 4);
            var data = Encoding.UTF8.GetBytes(pluginData);

            ms.Write(LittleEndian.GetBytes(data.Length), 0, 4);
            ms.Write(data, 0, data.Length);
            ms.WriteByte(0);

            return(ms);
        }
Example #20
0
        /// <summary>
        /// Generates binary image of the <see cref="PacketType101DataPoint"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public override int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes(HistorianID), 0, buffer, startIndex, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes((int)Time.Value), 0, buffer, startIndex + 4, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes((short)Flags), 0, buffer, startIndex + 8, 2);
            Buffer.BlockCopy(LittleEndian.GetBytes(Value), 0, buffer, startIndex + 10, 4);

            return(length);
        }
Example #21
0
        /// <summary>
        /// Generates binary image of the <see cref="MetadataRecord"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public virtual int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            // Construct the binary IP buffer for this event
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_remarks.PadRight(512).TruncateRight(512)), 0, buffer, startIndex, 512);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_hardwareInfo.PadRight(512).TruncateRight(512)), 0, buffer, startIndex + 512, 512);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_emailAddresses.PadRight(512).TruncateRight(512)), 0, buffer, startIndex + 1024, 512);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_description.PadRight(80).TruncateRight(80)), 0, buffer, startIndex + 1536, 80);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_currentData.PadRight(80).TruncateRight(80)), 0, buffer, startIndex + 1616, 80);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_name.PadRight(40).TruncateRight(40)), 0, buffer, startIndex + 1696, 40);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_synonym1.PadRight(40).TruncateRight(40)), 0, buffer, startIndex + 1736, 40);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_synonym2.PadRight(40).TruncateRight(40)), 0, buffer, startIndex + 1776, 40);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_synonym3.PadRight(40).TruncateRight(40)), 0, buffer, startIndex + 1816, 40);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_pagerNumbers.PadRight(40).TruncateRight(40)), 0, buffer, startIndex + 1856, 40);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_phoneNumbers.PadRight(40).TruncateRight(40)), 0, buffer, startIndex + 1896, 40);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_plantCode.PadRight(24).TruncateRight(24)), 0, buffer, startIndex + 1936, 24);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_system.PadRight(24).TruncateRight(24)), 0, buffer, startIndex + 1960, 24);
            Buffer.BlockCopy(Encoding.ASCII.GetBytes(m_emailTime.PadRight(40).TruncateRight(40)), 0, buffer, startIndex + 1984, 40);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_scanRate), 0, buffer, startIndex + 2104, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_unitNumber), 0, buffer, startIndex + 2108, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_securityFlags.Value), 0, buffer, startIndex + 2112, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_generalFlags.Value), 0, buffer, startIndex + 2116, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_alarmFlags.Value), 0, buffer, startIndex + 2120, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_compressionMinTime), 0, buffer, startIndex + 2124, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_compressionMaxTime), 0, buffer, startIndex + 2128, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_sourceID), 0, buffer, startIndex + 2132, 4);

            switch (m_generalFlags.DataType)
            {
            case DataType.Analog:
                m_analogFields.GenerateBinaryImage(buffer, startIndex + 2152);
                break;

            case DataType.Digital:
                m_digitalFields.GenerateBinaryImage(buffer, startIndex + 2152);
                break;

            case DataType.Composed:
                m_composedFields.GenerateBinaryImage(buffer, startIndex + 2152);
                break;

            case DataType.Constant:
                m_constantFields.GenerateBinaryImage(buffer, startIndex + 2152);
                break;
            }

            return(length);
        }
Example #22
0
        /// <summary>
        /// Initializes a new instance of OutPacket from the given payload data, crypto handler, auth handler, prefix and count.
        /// </summary>
        /// <param name="payload">The ready payload data.</param>
        /// <param name="crypto">The crypto handler to be used.</param>
        /// <param name="auth">The auth handler to be used.</param>
        /// <param name="prefix">The packet's prefix.</param>
        /// <param name="count">The packet's count.</param>
        public OutPacket(byte[] payload, CryptoHandler crypto, AuthHandler auth, short prefix, int count)
        {
            byte[] packetPrefix  = LittleEndian.GetBytes(prefix);
            byte[] packetCount   = LittleEndian.GetBytes(count);
            byte[] iv            = Generate.IV();
            byte[] encryptedData = crypto.EncryptPacket(payload, iv);

            byte[] size = LittleEndian.GetBytes(Convert.ToInt16(16 + encryptedData.Length + 10));

            byte[] authData = Sequence.Concat(packetPrefix, packetCount, iv, encryptedData);
            byte[] hmac     = auth.GetHmac(authData);

            PacketData = Sequence.Concat(size, authData, hmac);
        }
Example #23
0
        /// <summary>
        /// Builds a compressed payload from the current content's data.
        /// </summary>
        /// <param name="id">The packet's id.</param>
        /// <returns>A new compressed payload.</returns>
        public byte[] GetCompressedPayload(CenterOpcodes oId)
        {
            short id = (short)oId;

            byte[] compressedData = ZLib.CompressData(Data);

            byte[] packetId         = BigEndian.GetBytes(id);
            byte[] size             = BigEndian.GetBytes(compressedData.Length + 4);
            byte[] compressionFlag  = { 1 }; // true
            byte[] decompressedSize = LittleEndian.GetBytes(Data.Length);
            byte[] padding          = { 0, 0, 0 };

            return(Sequence.Concat(packetId, size, compressionFlag, decompressedSize, compressedData, padding));
        }
Example #24
0
        /// <summary>
        /// Generates binary image of the <see cref="PacketType1"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public override int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes(TypeID), 0, buffer, startIndex, 2);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_historianID), 0, buffer, startIndex + 2, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_time.Value), 0, buffer, startIndex + 6, 8);
            Buffer.BlockCopy(LittleEndian.GetBytes((int)m_quality), 0, buffer, startIndex + 14, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_value), 0, buffer, startIndex + 18, 4);

            return(length);
        }
Example #25
0
            /// <summary>
            /// Generates binary image of the <see cref="FixedTableRegion"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
            /// </summary>
            /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
            /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
            /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
            /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
            /// <exception cref="ArgumentOutOfRangeException">
            /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
            /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
            /// </exception>
            public int GenerateBinaryImage(byte[] buffer, int startIndex)
            {
                int length = BinaryLength;

                buffer.ValidateParameters(startIndex, length);

                Buffer.BlockCopy(LittleEndian.GetBytes(m_parent.m_fileStartTime.Value), 0, buffer, startIndex, 8);
                Buffer.BlockCopy(LittleEndian.GetBytes(m_parent.m_fileEndTime.Value), 0, buffer, startIndex + 8, 8);
                Buffer.BlockCopy(LittleEndian.GetBytes(m_parent.m_dataPointsReceived), 0, buffer, startIndex + 16, 4);
                Buffer.BlockCopy(LittleEndian.GetBytes(m_parent.m_dataPointsArchived), 0, buffer, startIndex + 20, 4);
                Buffer.BlockCopy(LittleEndian.GetBytes(m_parent.m_dataBlockSize), 0, buffer, startIndex + 24, 4);
                Buffer.BlockCopy(LittleEndian.GetBytes(m_parent.m_dataBlockCount), 0, buffer, startIndex + 28, 4);

                return(length);
            }
Example #26
0
        /// <summary>
        /// Generates binary image of the <see cref="PacketType101"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public override int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes(TypeID), 0, buffer, startIndex, 2);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_data.Count), 0, buffer, startIndex + 2, 4);
            for (int i = 0; i < m_data.Count; i++)
            {
                m_data[i].GenerateBinaryImage(buffer, startIndex + 6 + (i * PacketType101DataPoint.FixedLength));
            }

            return(length);
        }
Example #27
0
        /// <summary>
        /// Generates binary image of the <see cref="QueryPacketBase"/> and copies it into the given buffer, for <see cref="BinaryLength"/> bytes.
        /// </summary>
        /// <param name="buffer">Buffer used to hold generated binary image of the source object.</param>
        /// <param name="startIndex">0-based starting index in the <paramref name="buffer"/> to start writing.</param>
        /// <returns>The number of bytes written to the <paramref name="buffer"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="buffer"/> is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <see cref="BinaryLength"/> is less than 0 -or-
        /// <paramref name="startIndex"/> and <see cref="BinaryLength"/> will exceed <paramref name="buffer"/> length.
        /// </exception>
        public override int GenerateBinaryImage(byte[] buffer, int startIndex)
        {
            int length = BinaryLength;

            buffer.ValidateParameters(startIndex, length);

            Buffer.BlockCopy(LittleEndian.GetBytes(TypeID), 0, buffer, startIndex, 2);
            Buffer.BlockCopy(LittleEndian.GetBytes(m_requestIDs.Count), 0, buffer, startIndex + 2, 4);
            for (int i = 0; i < m_requestIDs.Count; i++)
            {
                Buffer.BlockCopy(LittleEndian.GetBytes(m_requestIDs[i]), 0, buffer, startIndex + 6 + (i * 4), 4);
            }

            return(length);
        }
Example #28
0
        public virtual void WriteData(Stream target)
        {
            byte[] buffer = new byte[8];

            Buffer.BlockCopy(Encoding.ASCII.GetBytes(TypeID), 0, buffer, 0, 4);
            Buffer.BlockCopy(LittleEndian.GetBytes(ChunkSize), 0, buffer, 4, 4);
            target.Write(buffer, 0, 8);

            target.Write(Chunk, 0, Chunk.Length);
            buffer[0] = 0x0;

            if (ChunkSize % 2 != 0)
            {
                target.Write(buffer, 0, 1);
            }
        }
Example #29
0
        public byte[] GetParameters()
        {
            using (var ms = new MemoryStream())
            {
                // parameter count
                ms.Write(LittleEndian.GetBytes(Parameters.Count), 0, 4);

                // parameters
                foreach (var paramValue in Parameters)
                {
                    ms.Write(LittleEndian.GetBytes((float)paramValue), 0, 4);
                }

                return(ms.ToByteArray());
            }
        }
Example #30
0
        /// <summary>
        /// Writes next COMTRADE record in float32 format.
        /// </summary>
        /// <param name="output">Destination stream.</param>
        /// <param name="schema">Source schema.</param>
        /// <param name="timestamp">Record timestamp (implicitly castable as <see cref="DateTime"/>).</param>
        /// <param name="values">Values to write - 16-bit digitals should exist as a word in an individual double value.</param>
        /// <param name="sample">User incremented sample index.</param>
        /// <param name="injectFracSecValue">Determines if FRACSEC value should be automatically injected into stream as first digital - defaults to <c>true</c>.</param>
        /// <param name="fracSecValue">FRACSEC value to inject into output stream - defaults to 0x0000.</param>
        /// <remarks>
        /// This function is primarily intended to write COMTRADE float32 data records based on synchrophasor data
        /// (see Annex H: Schema for Phasor Data 2150 Using the COMTRADE File Standard in IEEE C37.111-2010),
        /// it may be necessary to manually write records for other COMTRADE needs (e.g., non 16-bit digitals).
        /// </remarks>
        public static void WriteNextRecordFloat32(Stream output, Schema schema, Ticks timestamp, double[] values, uint sample, bool injectFracSecValue = true, ushort fracSecValue = 0x0000)
        {
            // Make timestamp relative to beginning of file
            timestamp -= schema.StartTime.Value;

            uint microseconds   = (uint)(timestamp.ToMicroseconds() / schema.TimeFactor);
            bool isFirstDigital = true;

            output.Write(LittleEndian.GetBytes(sample), 0, 4);
            output.Write(LittleEndian.GetBytes(microseconds), 0, 4);

            for (int i = 0; i < values.Length; i++)
            {
                double value = values[i];

                if (i < schema.AnalogChannels.Length)
                {
                    value -= schema.AnalogChannels[i].Adder;
                    value /= schema.AnalogChannels[i].Multiplier;

                    output.Write(LittleEndian.GetBytes((float)value), 0, 4);
                }
                else
                {
                    if (isFirstDigital)
                    {
                        // Handle automatic injection of IEEE C37.118 FRACSEC digital value if requested
                        isFirstDigital = false;

                        if (injectFracSecValue)
                        {
                            output.Write(LittleEndian.GetBytes(fracSecValue), 0, 2);
                        }
                    }

                    output.Write(LittleEndian.GetBytes((ushort)value), 0, 2);
                }
            }

            // Make sure FRACSEC values are injected
            if (isFirstDigital && injectFracSecValue)
            {
                output.Write(LittleEndian.GetBytes(fracSecValue), 0, 2);
            }
        }