public static String GetHexdump(IoBuffer buf, Int32 lengthLimit) { if (lengthLimit <= 0) throw new ArgumentException("lengthLimit: " + lengthLimit + " (expected: 1+)"); Boolean truncate = buf.Remaining > lengthLimit; Int32 size = truncate ? lengthLimit : buf.Remaining; if (size == 0) return "empty"; StringBuilder sb = new StringBuilder(size * 3 + 3); Int32 oldPos = buf.Position; // fill the first Int32 byteValue = buf.Get() & 0xFF; sb.Append((char)highDigits[byteValue]); sb.Append((char)lowDigits[byteValue]); size--; // and the others, too for (; size > 0; size--) { sb.Append(' '); byteValue = buf.Get() & 0xFF; sb.Append((char)highDigits[byteValue]); sb.Append((char)lowDigits[byteValue]); } buf.Position = oldPos; if (truncate) sb.Append("..."); return sb.ToString(); }
public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output) { if (input.HasRemaining) return FinishDecode(input.Get(), output); return this; }
public static void ReadAndCheckMagic(IoBuffer buf) { var magic = new byte[Magic.Length]; buf.Get(magic, 0, magic.Length); if (!magic.SequenceEqual(MAGIC)) throw new ArgumentException("Magic number mismatch"); }
private MessageCodecVersion GetVersion (IoBuffer buf) { var versionByte = buf.Get (); var version = MessageCodecVersion.ValueOf (versionByte); if (version == null) throw new ArgumentException (string.Format ("Unknown version {0}", versionByte)); return version; }
public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output) { while (input.HasRemaining) { switch (_counter) { case 0: _highByte = input.Get() & 0xff; break; case 1: _counter = 0; return FinishDecode((Int16)((_highByte << 8) | (input.Get() & 0xff)), output); } _counter++; } return this; }
public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output) { Boolean found = false; Boolean finished = false; while (input.HasRemaining) { Byte b = input.Get(); if (!_hasCR) { if (b == CR) { _hasCR = true; } else { if (b == LF) { found = true; } else { input.Position = input.Position - 1; found = false; } finished = true; break; } } else { if (b == LF) { found = true; finished = true; break; } throw new ProtocolDecoderException("Expected LF after CR but was: " + (b & 0xff)); } } if (finished) { _hasCR = false; return FinishDecode(found, output); } return this; }
public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output) { while (input.HasRemaining) { switch (_counter) { case 0: _firstByte = input.Get() & 0xff; break; case 1: _secondByte = input.Get() & 0xff; break; case 2: _thirdByte = input.Get() & 0xff; break; case 3: _counter = 0; return FinishDecode((_firstByte << 24) | (_secondByte << 16) | (_thirdByte << 8) | (input.Get() & 0xff), output); } _counter++; } return this; }
public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output) { Int32 beginPos = input.Position; Int32 limit = input.Limit; for (Int32 i = beginPos; i < limit; i++) { Byte b = input.Get(i); if (!CanSkip(b)) { input.Position = i; Int32 answer = _skippedBytes; _skippedBytes = 0; return FinishDecode(answer); } _skippedBytes++; } input.Position = limit; return this; }
public IDecodingState Decode(IoBuffer input, IProtocolDecoderOutput output) { Int32 beginPos = input.Position; Int32 limit = input.Limit; Int32 terminatorPos = -1; for (Int32 i = beginPos; i < limit; i++) { Byte b = input.Get(i); if (b == CR) { _lastIsCR = true; } else { if (b == LF && _lastIsCR) { terminatorPos = i; break; } _lastIsCR = false; } } if (terminatorPos >= 0) { IoBuffer product; Int32 endPos = terminatorPos - 1; if (beginPos < endPos) { input.Limit = endPos; if (_buffer == null) { product = input.Slice(); } else { _buffer.Put(input); product = _buffer.Flip(); _buffer = null; } input.Limit = limit; } else { // When input contained only CR or LF rather than actual data... if (_buffer == null) { product = IoBuffer.Allocate(0); } else { product = _buffer.Flip(); _buffer = null; } } input.Position = terminatorPos + 1; return FinishDecode(product, output); } input.Position = beginPos; if (_buffer == null) { _buffer = IoBuffer.Allocate(input.Remaining); _buffer.AutoExpand = true; } _buffer.Put(input); if (_lastIsCR) { _buffer.Position = _buffer.Position - 1; } return this; }
public static long Crc32(IoBuffer buf) { var array = new byte[buf.Remaining]; buf.Get(array, 0, array.Length); return Convert.ToInt64(CRC32.Compute(array)); }
private void DecodeNormal(Context ctx, IoSession session, IoBuffer input, IProtocolDecoderOutput output) { Int32 matchCount = ctx.MatchCount; // Try to find a match Int32 oldPos = input.Position, oldLimit = input.Limit; while (input.HasRemaining) { Byte b = input.Get(); if (_delimBuf[matchCount] == b) { matchCount++; if (matchCount == _delimBuf.Length) { // Found a match. Int32 pos = input.Position; input.Limit = pos; input.Position = oldPos; ctx.Append(input); input.Limit = oldLimit; input.Position = pos; if (ctx.OverflowPosition == 0) { IoBuffer buf = ctx.Buffer; buf.Flip(); buf.Limit -= matchCount; ArraySegment<Byte> bytes = buf.GetRemaining(); try { String str = _encoding.GetString(bytes.Array, bytes.Offset, bytes.Count); WriteText(session, str, output); } finally { buf.Clear(); } } else { Int32 overflowPosition = ctx.OverflowPosition; ctx.Reset(); throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition); } oldPos = pos; matchCount = 0; } } else { input.Position = Math.Max(0, input.Position - matchCount); matchCount = 0; } } // Put remainder to buf. input.Position = oldPos; ctx.Append(input); ctx.MatchCount = matchCount; }
private void DecodeAuto(Context ctx, IoSession session, IoBuffer input, IProtocolDecoderOutput output) { Int32 matchCount = ctx.MatchCount; // Try to find a match Int32 oldPos = input.Position, oldLimit = input.Limit; while (input.HasRemaining) { Byte b = input.Get(); Boolean matched = false; switch (b) { case 0x0d: // \r // Might be Mac, but we don't auto-detect Mac EOL // to avoid confusion. matchCount++; break; case 0x0a: // \n // UNIX matchCount++; matched = true; break; default: matchCount = 0; break; } if (matched) { // Found a match. Int32 pos = input.Position; input.Limit = pos; input.Position = oldPos; ctx.Append(input); input.Limit = oldLimit; input.Position = pos; if (ctx.OverflowPosition == 0) { IoBuffer buf = ctx.Buffer; buf.Flip(); buf.Limit -= matchCount; ArraySegment<Byte> bytes = buf.GetRemaining(); try { String str = _encoding.GetString(bytes.Array, bytes.Offset, bytes.Count); WriteText(session, str, output); } finally { buf.Clear(); } } else { Int32 overflowPosition = ctx.OverflowPosition; ctx.Reset(); throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition); } oldPos = pos; matchCount = 0; } } // Put remainder to buf. input.Position = oldPos; ctx.Append(input); ctx.MatchCount = matchCount; }
protected virtual void PutInternal(IoBuffer src) { Int32 n = src.Remaining; if (n > Remaining) throw new OverflowException(); for (Int32 i = 0; i < n; i++) { Put(src.Get()); } }
private IoBuffer InsertBytesToNewIoBuffer(IoSession session, IoBuffer buffer) { if (_insertByteProbability > _rng.Next(1000)) { Debug.WriteLine(buffer.GetHexDump()); // where to insert bytes ? int pos = _rng.Next(buffer.Remaining) - 1; // how many byte to insert ? int count = _rng.Next(_maxInsertByte - 1) + 1; IoBuffer newBuff = IoBuffer.Allocate(buffer.Remaining + count); for (int i = 0; i < pos; i++) newBuff.Put(buffer.Get()); for (int i = 0; i < count; i++) { newBuff.Put((byte)(_rng.Next(256))); } while (buffer.Remaining > 0) { newBuff.Put(buffer.Get()); } newBuff.Flip(); Debug.WriteLine("Inserted " + count + " bytes."); Debug.WriteLine(newBuff.GetHexDump()); return newBuff; } return null; }
private void ManipulateIoBuffer(IoSession session, IoBuffer buffer) { if ((buffer.Remaining > 0) && (_removeByteProbability > _rng.Next(1000))) { Debug.WriteLine(buffer.GetHexDump()); // where to remove bytes ? int pos = _rng.Next(buffer.Remaining); // how many byte to remove ? int count = _rng.Next(buffer.Remaining - pos) + 1; if (count == buffer.Remaining) count = buffer.Remaining - 1; IoBuffer newBuff = IoBuffer.Allocate(buffer.Remaining - count); for (int i = 0; i < pos; i++) newBuff.Put(buffer.Get()); buffer.Skip(count); // hole while (newBuff.Remaining > 0) newBuff.Put(buffer.Get()); newBuff.Flip(); // copy the new buffer in the old one buffer.Rewind(); buffer.Put(newBuff); buffer.Flip(); Debug.WriteLine("Removed " + count + " bytes at position " + pos + "."); Debug.WriteLine(buffer.GetHexDump()); } if ((buffer.Remaining > 0) && (_changeByteProbability > _rng.Next(1000))) { Debug.WriteLine(buffer.GetHexDump()); // how many byte to change ? int count = _rng.Next(buffer.Remaining - 1) + 1; byte[] values = new byte[count]; _rng.NextBytes(values); for (int i = 0; i < values.Length; i++) { int pos = _rng.Next(buffer.Remaining); buffer.Put(pos, values[i]); } Debug.WriteLine("Modified " + count + " bytes."); Debug.WriteLine(buffer.GetHexDump()); } }