/// <summary> /// Reads an ASCII-string of the specified length from this instance. /// </summary> /// <param name="reader">Extension method for the BinaryReader class.</param> /// <param name="count">The number of bytes to read from the underlying /// stream.</param> /// <returns>A string decoded from the bytes read from the underlying /// stream using the ASCII character set.</returns> public static string ReadASCIIString(this BinaryReader reader, int count) { ByteBuilder builder = new ByteBuilder(); int read = 0; while (true) { if (read++ >= count) break; byte b = reader.ReadByte(); builder.Append(b); } return Encoding.ASCII.GetString(builder.ToArray()).TrimEnd('\0'); }
/// <summary> /// Creates a new "multi-precision integer" from the specified array /// of bytes. /// </summary> /// <param name="data">A big-endian sequence of bytes forming the /// integer value of the multi-precision integer.</param> public Mpi(byte[] data) { byte[] b = new byte[data.Length]; Array.Copy(data.Reverse().ToArray(), b, data.Length); ByteBuilder builder = new ByteBuilder().Append(b); // We append a null byte to the buffer which ensures the most // significant bit will never be set and the big integer value // always be positive. if (b.Last() != 0) builder.Append(0); Value = new BigInteger(builder.ToArray()); }
/// <summary> /// Sends the accumulated payload data to the server. /// </summary> /// <returns>true if the client is done sending data, otherwise /// false.</returns> bool SendPayload() { // IMAP (as well as Pop3 and Smtp) can't deal with binary data and // expect the data as Base64-encoded ASCII string terminated with a // CRLF. string base64 = Convert.ToBase64String(payloadData.ToArray()); payloadData.Clear(); // Send it off to the IMAP server. byte[] data = Encoding.ASCII.GetBytes(base64 + "\r\n"); innerStream.Write(data, 0, data.Length); // If the latest client handshake is of type HandshakeDone, then the // client wont be sending any further handshake messages. return(handshake.MessageId == HandshakeType.HandshakeDone); }
/// <summary> /// Reads the client's handshake from the specified buffer. /// </summary> /// <param name="buffer">A byte array from which the handshake data /// will be read.</param> /// <param name="offset">The zero-based index in the buffer at which to /// begin reading bytes.</param> /// <param name="count">The number of bytes to read from buffer.</param> /// <returns>True if the handshake has been read completely, otherwise /// false.</returns> bool ReadHandshake(byte[] buffer, int offset, int count) { // Accumulate data into buffer until 5 bytes have been read. int read = Math.Min(count, 5 - handshakeData.Length); handshakeData.Append(buffer, offset, read); if (handshakeData.Length == 5) { // We're now expecting the payload data. state = FilterStreamState.ReadingPayload; handshake = Handshake.Deserialize(handshakeData.ToArray()); handshakeData.Clear(); // Append rest of buffer to payloadData. payloadData.Append(buffer, offset + read, count - read); return(true); } // We haven't read 5 bytes yet. return(false); }