Ejemplo n.º 1
0
        /// <summary>
        /// Function to check SCP.
        /// </summary>
        /// <param name="buffer">byte array to do check in</param>
        /// <param name="offset">position to start checking</param>
        /// <param name="crc">value crc should be</param>
        /// <param name="length">length of section</param>
        /// <returns>0 on success</returns>
        public bool CheckSCP(byte[] buffer, int offset, ushort crc, int length)
        {
            CRCTool crctool = new CRCTool();

            crctool.Init(CRCTool.CRCCode.CRC_CCITT);
            if (crctool.CalcCRCITT(buffer, offset + Marshal.SizeOf(_CRC), length - Marshal.SizeOf(_CRC)) == crc)
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Function to write an SCP Section.
        /// </summary>
        /// <param name="buffer">buffer to write to</param>
        /// <param name="offset">position on buffer to start writing</param>
        /// <returns>error:
        /// 0x00) succes
        /// 0x01) section incorrect
        /// 0x02) no buffer provided or buffer to small for header
        /// rest) Section specific error </returns>
        public int Write(byte[] buffer, int offset)
        {
            Length = _getLength() + Size;

            if (Length == Size)
            {
                return(0);
            }

            if (Works())
            {
                if ((buffer != null) &&
                    ((offset + Length) <= buffer.Length))
                {
                    int crcoffset = offset;
                    offset   += Marshal.SizeOf(CRC);
                    SectionID = getSectionID();
                    BytesTool.writeBytes(SectionID, buffer, offset, Marshal.SizeOf(SectionID), true);
                    offset += Marshal.SizeOf(SectionID);
                    BytesTool.writeBytes(Length, buffer, offset, Marshal.SizeOf(Length), true);
                    offset += Marshal.SizeOf(Length);
                    BytesTool.writeBytes(SectionVersionNr, buffer, offset, Marshal.SizeOf(SectionVersionNr), true);
                    offset += Marshal.SizeOf(SectionVersionNr);
                    BytesTool.writeBytes(ProtocolVersionNr, buffer, offset, Marshal.SizeOf(ProtocolVersionNr), true);
                    offset += Marshal.SizeOf(ProtocolVersionNr);
                    offset += BytesTool.copy(buffer, offset, Reserved, 0, _ReservedLength);

                    int err = _Write(buffer, offset);
                    if (err == 0)
                    {
                        CRCTool crc = new CRCTool();
                        crc.Init(CRCTool.CRCCode.CRC_CCITT);
                        CRC = crc.CalcCRCITT(buffer, crcoffset + Marshal.SizeOf(CRC), Length - Marshal.SizeOf(CRC));
                        BytesTool.writeBytes(CRC, buffer, crcoffset, Marshal.SizeOf(CRC), true);
                    }
                    return(err << 2);
                }
                return(0x2);
            }
            return(0x1);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Function to read an SCP Section.
        /// </summary>
        /// <param name="buffer">buffer to read from</param>
        /// <param name="offset">position on buffer to start reading</param>
        /// <returns>error:
        /// 0x00) succes
        /// 0x01) no buffer provided or buffer to small for header
        /// 0x02) Section ID doesn't seem to be right
        /// 0x04) buffer not big enough for entire section
        /// 0x08) CRC Check Failed
        /// rest) Section specific error </returns>
        public int Read(byte[] buffer, int offset, int length)
        {
            Empty();
            if ((buffer != null) &&
                (offset + Size) <= buffer.Length)
            {
                int crcoffset = offset;
                CRC       = (ushort)BytesTool.readBytes(buffer, offset, Marshal.SizeOf(CRC), true);
                offset   += Marshal.SizeOf(CRC);
                SectionID = (ushort)BytesTool.readBytes(buffer, offset, Marshal.SizeOf(SectionID), true);
                if (SectionID != getSectionID())
                {
                    return(0x2);
                }
                offset += Marshal.SizeOf(SectionID);
                Length  = (int)BytesTool.readBytes(buffer, offset, Marshal.SizeOf(Length), true);
                if (((length != Length) &&
                     (SectionID != 0)) ||
                    (crcoffset + Length) > buffer.Length)
                {
                    return(0x4);
                }
                offset += Marshal.SizeOf(Length);
                CRCTool crc = new CRCTool();
                crc.Init(CRCTool.CRCCode.CRC_CCITT);
                if (CRC != crc.CalcCRCITT(buffer, crcoffset + Marshal.SizeOf(CRC), Length - Marshal.SizeOf(CRC)))
                {
                    return(0x8);
                }
                SectionVersionNr  = (byte)BytesTool.readBytes(buffer, offset, Marshal.SizeOf(SectionVersionNr), true);
                offset           += Marshal.SizeOf(SectionVersionNr);
                ProtocolVersionNr = (byte)BytesTool.readBytes(buffer, offset, Marshal.SizeOf(ProtocolVersionNr), true);
                offset           += Marshal.SizeOf(ProtocolVersionNr);
                offset           += BytesTool.copy(Reserved, 0, buffer, offset, _ReservedLength);

                return(_Read(buffer, offset) << 4);
            }
            return(0x1);
        }
Ejemplo n.º 4
0
 public override int Write(byte[] buffer, int offset)
 {
     // Check if format works.
     if (Works())
     {
         _Length = getFileSize();
         if ((buffer != null) &&
             ((offset + _Length) <= buffer.Length) &&
             (_Default[0] is SCPSection0))
         {
             // Write length of file.
             BytesTool.writeBytes(_Length, buffer, offset + Marshal.SizeOf(_CRC), Marshal.SizeOf(_Length), true);
             SCPSection0 pointers = (SCPSection0)_Default[0];
             // Write all sections in format.
             for (int loper = 0; loper < pointers.getNrPointers(); loper++)
             {
                 if (loper < _MinNrSections)
                 {
                     _Default[loper].Write(buffer, offset + pointers.getIndex(loper) - 1);
                 }
                 else if ((pointers.getLength(loper) > SCPSection.Size) &&
                          (_Manufactor[loper - _MinNrSections] != null))
                 {
                     _Manufactor[loper - _MinNrSections].Write(buffer, offset + pointers.getIndex(loper) - 1);
                 }
             }
             // Calculate CRC of byte array.
             CRCTool crctool = new CRCTool();
             crctool.Init(CRCTool.CRCCode.CRC_CCITT);
             _CRC = crctool.CalcCRCITT(buffer, offset + Marshal.SizeOf(_CRC), _Length - Marshal.SizeOf(_CRC));
             BytesTool.writeBytes(_CRC, buffer, offset, Marshal.SizeOf(_CRC), true);
             return(0x0);
         }
         return(0x2);
     }
     return(0x1);
 }
Ejemplo n.º 5
0
        public override bool CheckFormat(Stream input, int offset)
        {
            if ((input != null) &&
                input.CanRead &&
                input.CanSeek &&
                (offset >= 0))
            {
                byte[] buff = new byte[_Header.Size() + BYTES_BEFORE_HEADER];

                long origin = input.Position;

                input.Position += offset;

                if (BytesTool.readStream(input, buff, 0, buff.Length) == buff.Length)
                {
                    string magicNumber = BytesTool.readString(buff, 0, MAGIC_NUMBER.Length);

                    if (string.Compare(magicNumber, MAGIC_NUMBER) == 0)
                    {
                        ISHNEHeader head = new ISHNEHeader();

                        if ((head.Read(buff, BYTES_BEFORE_HEADER, buff.Length - BYTES_BEFORE_HEADER) == 0) &&
                            head.Works())
                        {
                            if (head.VarBlockSize > 0)
                            {
                                byte[] buffTwo = new byte[buff.Length + head.VarBlockSize];

                                BytesTool.copy(buffTwo, 0, buff, 0, buff.Length);

                                if (BytesTool.readStream(input, buffTwo, buff.Length, head.VarBlockSize) != head.VarBlockSize)
                                {
                                    input.Position = origin;

                                    return(false);
                                }

                                buff = buffTwo;
                            }

                            input.Position = origin;

                            if (_CRCValidation)
                            {
                                CRCTool tool = new CRCTool();
                                tool.Init(CRCTool.CRCCode.CRC_CCITT);

                                ushort crc = (ushort)BytesTool.readBytes(buff, MAGIC_NUMBER.Length, SHORT_SIZE, true);

                                return(crc == tool.CalcCRCITT(buff, BYTES_BEFORE_HEADER, buff.Length - BYTES_BEFORE_HEADER));
                            }

                            return(true);
                        }
                    }
                }

                input.Position = origin;
            }

            return(false);
        }
Ejemplo n.º 6
0
        public override int Write(Stream output)
        {
            try
            {
                if ((output != null) &&
                    output.CanWrite)
                {
                    if (Works())
                    {
                        _Header.VarBlockOffset = _Header.Size() + BYTES_BEFORE_HEADER;
                        _Header.VarBlockSize   = (_HeaderAndVarBlock != null) && (_HeaderAndVarBlock.Length < _Header.VarBlockOffset) ? _HeaderAndVarBlock.Length - _Header.VarBlockOffset : 0;
                        _Header.ECGOffset      = _Header.VarBlockOffset + _Header.VarBlockSize;

                        if ((_HeaderAndVarBlock == null) ||
                            (_HeaderAndVarBlock.Length < _Header.ECGOffset))
                        {
                            _HeaderAndVarBlock = new byte[_Header.ECGOffset];
                        }

                        // Begin: code that handles overriding of AVM
                        double   avmOverride = _AVMOverride;
                        int      nrLeads     = _Signals.NrLeads;
                        double[] avm         = new double[nrLeads];

                        if (avmOverride <= 0.0)
                        {
                            for (int i = 0; i < nrLeads; i++)
                            {
                                avm[i] = _Header.GetLeadAmplitude(i);
                            }
                        }
                        else
                        {
                            for (int i = 0; i < nrLeads; i++)
                            {
                                _Header.ECGLeadResolution[i] = (Int16)(avmOverride * ISHNEHeader.UV_TO_AMPLITUDE);
                                avm[i] = avmOverride;
                            }
                        }
                        // End: code that handles overriding of AVM

                        BytesTool.writeString(MAGIC_NUMBER, _HeaderAndVarBlock, 0, MAGIC_NUMBER.Length);

                        if (_Header.Write(_HeaderAndVarBlock, BYTES_BEFORE_HEADER, _Header.Size()) != 0)
                        {
                            return(0x4);
                        }

                        CRCTool tool = new CRCTool();
                        tool.Init(CRCTool.CRCCode.CRC_CCITT);

                        ushort crc = tool.CalcCRCITT(_HeaderAndVarBlock, BYTES_BEFORE_HEADER, _HeaderAndVarBlock.Length - BYTES_BEFORE_HEADER);

                        BytesTool.writeBytes(crc, _HeaderAndVarBlock, MAGIC_NUMBER.Length, SHORT_SIZE, true);

                        output.Write(_HeaderAndVarBlock, 0, _Header.ECGOffset);

                        if (_Signals.IsBuffered)
                        {
                            BufferedSignals bs = _Signals.AsBufferedSignals;

                            int rhythmCur = bs.RealRhythmStart,
                                rhythmEnd = bs.RealRhythmEnd,
                                stepSize  = 60 * bs.RhythmSamplesPerSecond;

                            byte[] data = null;

                            for (; rhythmCur < rhythmEnd; rhythmCur += stepSize)
                            {
                                if (!bs.LoadSignal(rhythmCur, rhythmCur + stepSize))
                                {
                                    return(0x8);
                                }

                                int buff_size = _WriteSignal(ref data, _Signals, avm);

                                if (buff_size < 0)
                                {
                                    return(0x10);
                                }

                                output.Write(data, 0, buff_size);
                            }
                        }
                        else
                        {
                            int rhythmStart, rhythmEnd;

                            _Signals.CalculateStartAndEnd(out rhythmStart, out rhythmEnd);

                            byte[] data = null;

                            int buff_size = _WriteSignal(ref data, _Signals, avm);

                            if (buff_size < 0)
                            {
                                return(0x10);
                            }

                            output.Write(data, 0, buff_size);
                        }

                        return(0x0);
                    }

                    return(0x2);
                }
            }
            catch
            {
                return(0x10);
            }

            return(0x1);
        }
Ejemplo n.º 7
0
        public override int Read(Stream input, int offset)
        {
            _Signals = null;

            if ((input != null) &&
                input.CanRead &&
                input.CanSeek &&
                (offset >= 0))
            {
                _InputStreamOffset = input.Position + offset;

                _HeaderAndVarBlock = new byte[_Header.Size() + BYTES_BEFORE_HEADER];

                input.Position += offset;

                if (BytesTool.readStream(input, _HeaderAndVarBlock, 0, _HeaderAndVarBlock.Length) == _HeaderAndVarBlock.Length)
                {
                    string magicNumber = BytesTool.readString(_HeaderAndVarBlock, 0, MAGIC_NUMBER.Length);

                    if (string.Compare(magicNumber, MAGIC_NUMBER) == 0)
                    {
                        if ((_Header.Read(_HeaderAndVarBlock, BYTES_BEFORE_HEADER, _HeaderAndVarBlock.Length - BYTES_BEFORE_HEADER) == 0) &&
                            _Header.Works())
                        {
                            if (_Header.VarBlockSize > 0)
                            {
                                byte[] buff = new byte[_HeaderAndVarBlock.Length + _Header.VarBlockSize];

                                BytesTool.copy(buff, 0, _HeaderAndVarBlock, 0, _HeaderAndVarBlock.Length);

                                if (BytesTool.readStream(input, buff, _HeaderAndVarBlock.Length, _Header.VarBlockSize) != _Header.VarBlockSize)
                                {
                                    return(0x10);
                                }

                                _HeaderAndVarBlock = buff;
                            }

                            CRCTool tool = new CRCTool();
                            tool.Init(CRCTool.CRCCode.CRC_CCITT);

                            ushort crc = (ushort)BytesTool.readBytes(_HeaderAndVarBlock, MAGIC_NUMBER.Length, SHORT_SIZE, true);

                            if (!_CRCValidation ||
                                (crc == tool.CalcCRCITT(_HeaderAndVarBlock, BYTES_BEFORE_HEADER, _HeaderAndVarBlock.Length - BYTES_BEFORE_HEADER)))
                            {
                                if (input.CanSeek)
                                {
                                    long nrSamples = (input.Length - _Header.ECGOffset) / 2;

                                    if ((nrSamples > _Header.ECGNrSamples) &&
                                        (nrSamples <= int.MaxValue))
                                    {
                                        _Header.ECGNrSamples = (int)nrSamples;
                                    }
                                }

                                BufferedSignals sigs = new BufferedSignals(this, (byte)_Header.ECGNrLeads);

                                // Begin: code that handles overriding of AVM
                                double avm = _AVMOverride;

                                if (avm <= 0.0)
                                {
                                    avm = double.MaxValue;

                                    for (int i = 0; i < _Header.ECGNrLeads; i++)
                                    {
                                        sigs[i]      = new Signal();
                                        sigs[i].Type = _Header.GetLeadType(i);

                                        double val = _Header.GetLeadAmplitude(i);

                                        if (val < avm)
                                        {
                                            avm = val;
                                        }
                                    }
                                }
                                else
                                {
                                    for (int i = 0; i < _Header.ECGNrLeads; i++)
                                    {
                                        sigs[i]      = new Signal();
                                        sigs[i].Type = _Header.GetLeadType(i);
                                    }
                                }
                                // End: code that handles overriding of AVM

                                sigs.RhythmAVM = avm;
                                sigs.RhythmSamplesPerSecond     = _Header.ECGSampleRate;
                                sigs.RealRhythmSamplesPerSecond = _Header.ECGSampleRate;
                                sigs.RealRhythmStart            = 0;
                                sigs.RealRhythmEnd = _Header.ECGNrSamples / _Header.ECGNrLeads;

                                _InputStream = input;
                                _Signals     = sigs;

                                sigs.Init();

                                return(0x0);
                            }

                            return(0x20);
                        }

                        return(0x8);
                    }

                    return(0x4);
                }

                return(0x2);
            }

            return(0x1);
        }