public int GetBytes( byte[] source, int sourceIndex, int sourceCount, byte[] dest, int destIndex, bool flush ) { if (source == null || source == null) { throw new ArgumentNullException(); } int bytes = 0; int newDestIndex = destIndex; for (int i = sourceIndex; i < sourceIndex + sourceCount; i++) { bool escape = false; bool newline = false; bool abort = false; byte b; try { b = source[i]; if (!escapeNextByte) { switch (b) { case escapeByte: i++; escape = true; if (i < sourceIndex + sourceCount) { b = source[i]; lineBytes++; } else { //what a pain, we cannot get the next character now, so //we set a flag to tell us to do it next time escapeNextByte = true; abort = true; } break; case 10: case 13: newline = true; break; } } } catch { throw new ArgumentOutOfRangeException(); } if ((!newline) && (!abort)) { b = DecodeByte(b, escape | escapeNextByte); escapeNextByte = false; try { dest[newDestIndex] = b; newDestIndex++; bytes++; } catch { throw new ArgumentException(); } } } if (flush) { crc32Hasher.TransformFinalBlock(dest, destIndex, bytes); storedHash = crc32Hasher.Hash; crc32Hasher = new CRC32(); } else { crc32Hasher.TransformBlock(dest, destIndex, bytes, dest, destIndex); } return(bytes); }
/// <summary> /// Core encoding algorithm. Encodes from the source bytes into the destination bytes. /// </summary> /// <param name="source"></param> /// <param name="sourceIndex">the offset in the source from where to start encoding</param> /// <param name="sourceCount">the number of bytes to encode</param> /// <param name="dest"></param> /// <param name="destIndex">the offset in the destination to start writing to</param> /// <param name="flush">set to true if this is the last block of data</param> /// <returns>the number of bytes output to the destination</returns> public int GetBytes(byte[] source, int sourceIndex, int sourceCount, byte[] dest, int destIndex, bool flush) { if (source == null || dest == null) { throw new ArgumentNullException(); } int byteCount = 0; int lineBytes = this.lineBytes; for (int i = sourceIndex; i < sourceCount + sourceIndex; i++) { byte c; try { c = source[i]; } catch { throw new ArgumentOutOfRangeException(); } bool escape = false; byte b = EncodeByte(c, out escape); try { if (escape) { dest[destIndex] = escapeByte; destIndex++; byteCount++; lineBytes++; } dest[destIndex] = b; destIndex++; lineBytes++; byteCount++; if (lineBytes >= this.lineLength) { //line termination dest[destIndex] = 13; destIndex++; byteCount++; dest[destIndex] = 10; destIndex++; byteCount++; lineBytes = 0; } } catch { throw new ArgumentException(); } } if (flush) { if (doLineFeedAfterFlush) { dest[destIndex] = 13; destIndex++; byteCount++; dest[destIndex] = 10; destIndex++; byteCount++; } crc32Hasher.TransformFinalBlock(source, sourceIndex, sourceCount); storedHash = crc32Hasher.Hash; crc32Hasher = new CRC32(); this.lineBytes = 0; } else { crc32Hasher.TransformBlock(source, sourceIndex, sourceCount, source, sourceIndex); this.lineBytes = lineBytes; } return(byteCount); }