/// <summary> /// Internal private hashing method. /// /// This is the new hashing algorithm from other clients. /// Found to be fast and have very good distribution. /// /// UPDATE: this is dog slow under java. Maybe under .NET? /// </summary> /// <param name="key">string to hash</param> /// <returns>hashcode for this string using memcached's hashing algorithm</returns> private static int NewHashingAlgorithm(string key) { CRCTool checksum = new CRCTool(); checksum.Init(CRCTool.CRCCode.CRC32); int crc = (int)checksum.crctablefast(UTF8Encoding.UTF8.GetBytes(key)); return((crc >> 16) & 0x7fff); }
/// <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); }
protected override Variant fixupImpl() { var elem = elements["ref"]; byte[] data = elem.Value.Value; CRCTool crcTool = new CRCTool(); crcTool.Init(type); return(new Variant((uint)crcTool.crctablefast(data))); }
protected override Variant fixupImpl() { var elem = elements["ref"]; var data = elem.Value; data.Seek(0, System.IO.SeekOrigin.Begin); CRCTool crcTool = new CRCTool(); crcTool.Init(type); return(new Variant((uint)crcTool.crctablefast(data))); }
protected override Variant fixupImpl() { var ref1 = elements["ref1"]; var ref2 = elements["ref2"]; byte[] data1 = ref1.Value.Value; byte[] data2 = ref2.Value.Value; byte[] data3 = new byte[data1.Length + data2.Length]; Buffer.BlockCopy(data1, 0, data3, 0, data1.Length); Buffer.BlockCopy(data2, 0, data3, data1.Length, data2.Length); CRCTool crcTool = new CRCTool(); crcTool.Init(type); return(new Variant((uint)crcTool.crctablefast(data3))); }
protected override Variant fixupImpl() { var ref1 = elements["ref1"]; var ref2 = elements["ref2"]; var data = new BitStreamList(); data.Add(ref1.Value); data.Add(ref2.Value); data.Seek(0, System.IO.SeekOrigin.Begin); CRCTool crcTool = new CRCTool(); crcTool.Init(type); return(new Variant((uint)crcTool.crctablefast(data))); }
public string EncryptLine(string strLine) { if (string.IsNullOrEmpty(strLine)) { return(string.Empty); } if (String.Compare("unknown", strLine, true) == 0) { return(string.Empty); } CRCTool crc = new CRCTool(); crc.Init(CRCTool.CRCCode.CRC32); ulong dwcrc = crc.calc(strLine); string strRet = String.Format("{0}", dwcrc); return(strRet); }
/// <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); }
/// <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); }
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); }
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); }
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); }
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); }
public void TestFixupBefore() { // Verify that in a DOM with Fixups before Relations, the fixup runs // after the relation has. // In this case the data model is: // CRC, 4 byte number whose value is the CRC of the data model // Len, 4 byte number whose value is the size of the data model // Data, 5 byte string. // The CRC should include the computed size relation. string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" + "<Peach>" + " <DataModel name=\"TheDataModel\">" + " <Block>" + " <Number name=\"CRC\" size=\"32\" signed=\"false\">" + " <Fixup class=\"Crc32Fixup\">" + " <Param name=\"ref\" value=\"TheDataModel\"/>" + " </Fixup>" + " </Number>" + " </Block>" + " <Number name=\"len\" size=\"32\" signed=\"false\">" + " <Relation type=\"size\" of=\"TheDataModel\" />" + " </Number>" + " <Blob name=\"Data\" value=\"Hello\">" + " <Hint name=\"BlobMutator-How\" value=\"ExpandAllRandom\"/>" + " </Blob>" + " </DataModel>" + " <StateModel name=\"TheState\" initialState=\"Initial\">" + " <State name=\"Initial\">" + " <Action type=\"output\">" + " <DataModel ref=\"TheDataModel\"/>" + " </Action>" + " </State>" + " </StateModel>" + " <Test name=\"Default\">" + " <StateModel ref=\"TheState\"/>" + " <Publisher class=\"Null\"/>" + " <Strategy class=\"RandomDeterministic\"/>" + " </Test>" + "</Peach>"; PitParser parser = new PitParser(); Dom.Dom dom = parser.asParser(null, new MemoryStream(ASCIIEncoding.ASCII.GetBytes(xml))); dom.tests[0].includedMutators = new List <string>(); dom.tests[0].includedMutators.Add("BlobMutator"); RunConfiguration config = new RunConfiguration(); Engine e = new Engine(null); e.startFuzzing(dom, config); Assert.AreEqual(2, dataModels.Count); byte[] dm1 = dataModels[0].Value.ToArray(); byte[] dm2 = dataModels[1].Value.ToArray(); Assert.AreEqual(4 + 4 + 5, dm1.Length); Assert.Greater(dm2.Length, dm1.Length); var data = Bits.Fmt("{0:L32}{1:L32}{2}", 0, 13, "Hello"); var crc = new CRCTool(); crc.Init(CRCTool.CRCCode.CRC32); data.Seek(0, SeekOrigin.Begin); data.WriteBits(Endian.Little.GetBits((uint)crc.crctablefast(data.ToArray()), 32), 32); byte[] final = data.ToArray(); Assert.AreEqual(final, dm1); }
/// <summary> /// Internal private hashing method. /// /// This is the new hashing algorithm from other clients. /// Found to be fast and have very good distribution. /// /// UPDATE: this is dog slow under java. Maybe under .NET? /// </summary> /// <param name="key">string to hash</param> /// <returns>hashcode for this string using memcached's hashing algorithm</returns> private static int NewHashingAlgorithm(string key) { CRCTool checksum = new CRCTool(); checksum.Init(CRCTool.CRCCode.CRC32); int crc = (int)checksum.crctablefast(UTF8Encoding.UTF8.GetBytes(key)); return (crc >> 16) & 0x7fff; }