[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] Add C# controller lib port from Oliver Rau.
Update of /home/or/cvsroot/control/cs/control/HelperClasses
In directory moria:/tmp/cvs-serv5623/control/HelperClasses
Added Files:
BigEndianReader.cs BigEndianWriter.cs Bytes.cs Cmd.cs
ConfigEntry.cs ReplyLine.cs Waiter.cs
Log Message:
Add C# controller lib port from Oliver Rau.
--- NEW FILE: BigEndianReader.cs ---
/*
* File BigEndianStream.cs
*
* Copyright (C) 2005 Oliver Rau (olra0001@xxxxxxxxxxxxxxxxxxx)
*
* See LICENSE file for copying information
*
* Created on 19.09.2005 22:52
*
* $Id: BigEndianReader.cs,v 1.1 2005/11/09 21:47:04 nickm Exp $
*/
using System;
using System.IO;
namespace Tor.Control
{
/// <summary>
/// Implements a <see cref="BinaryReader">BinaryReader</see> which reads in big-endian order
/// instead of little-endian.
/// </summary>
/// <remarks>
/// This class is not fully implemented, it's just implemented so far, that it will handle
/// the operations needed by tor.<br />
/// All the other operations are still the one's implemented by the <b>BinaryReader</b>.
/// </remarks>
public class BigEndianReader : BinaryReader
{
public BigEndianReader(Stream s) : base(s) {}
new public ushort ReadUInt16()
{
byte high = base.ReadByte();
byte low = base.ReadByte();
ushort result = Convert.ToUInt16(((high & 0xff) << 8) | (low & 0xffu));
return result;
}
}
}
--- NEW FILE: BigEndianWriter.cs ---
/*
* File BigEndianWriter.cs
*
* Copyright (C) 2005 Oliver Rau (olra0001@xxxxxxxxxxxxxxxxxxx)
*
* See LICENSE file for copying information
*
* Created on 19.09.2005 23:12
*
* $Id: BigEndianWriter.cs,v 1.1 2005/11/09 21:47:04 nickm Exp $
*/
using System;
using System.IO;
namespace Tor.Control
{
/// <summary>
/// Implements a <see cref="BinaryWriter">BinaryWriter</see> which writes in big-endian order
/// instead of little-endian.
/// </summary>
/// <remarks>
/// This class is not fully implemented, it's just implemented so far, that it will handle
/// the operations needed by tor.<br />
/// All the other operations are still the one's implemented by the <b>BinaryWriter</b>.
/// </remarks>
public class BigEndianWriter : BinaryWriter
{
public BigEndianWriter(Stream s) : base(s) {}
public void WriteShort(int value)
{
byte a = (byte) (0xff & (value >> 8));
byte b = (byte) (0xff & value);
base.Write(a);
base.Write(b);
}
public void WriteInt(int value)
{
// first we need to swap all bytes
value = Convert.ToInt32(((value & 0xFF000000) >> 24) | (( value & 0x000000FF) << 24) |
((value & 0x00FF0000) >> 16) | (( value & 0x0000FF00) << 16));
base.Write(value);
}
}
}
--- NEW FILE: Bytes.cs ---
/*
* File Bytes.cs
*
* Copyright (C) 2005 Oliver Rau (olra0001@xxxxxxxxxxxxxxxxxxx)
*
* See LICENSE file for copying information
*
* Created on 08.08.2005 20:37
*
* $Id: Bytes.cs,v 1.1 2005/11/09 21:47:04 nickm Exp $
*/
using System;
using System.Collections;
using System.Text;
namespace Tor.Control
{
/// <summary>
/// Description of Bytes.
/// </summary>
public class Bytes
{
private static char[] NYBBLES = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
Bytes() {}
public static void SetU16(byte[] ba, int pos, short s)
{
ba[pos] = (byte)((s >> 8) & 0xff);
ba[pos+1] = (byte)((s ) & 0xff);
}
public static void SetU32(byte[] ba, int pos, int i)
{
ba[pos] = (byte)((i >> 24) & 0xff);
ba[pos+1] = (byte)((i >> 16) & 0xff);
ba[pos+2] = (byte)((i >> 8) & 0xff);
ba[pos+3] = (byte)((i ) & 0xff);
}
public static int GetU32(byte[] ba, int pos)
{
return
((ba[pos ] & 0xff) << 24) |
((ba[pos+1] & 0xff) << 16) |
((ba[pos+2] & 0xff) << 8) |
((ba[pos+3] & 0xff));
}
public static string GetU32S(byte[] ba, int pos)
{
return (((long)GetU32(ba,pos))&0xffffffffL ).ToString();
}
public static int GetU16(byte[] ba, int pos)
{
return
((ba[pos ] & 0xff) << 8) |
((ba[pos+1] & 0xff));
}
public static string GetNulTerminatedStr(byte[] ba, int pos)
{
int len, maxlen = ba.Length - pos;
for (len=0; len < maxlen; ++len) {
if (ba[pos+len] == 0)
break;
}
return ByteArrayToString(ba, pos, len);
}
/**
* Read bytes from 'ba' starting at 'pos', dividing them into strings
* along the character in 'split' and writing them into 'lst'
*/
public static void SplitStr(IList lst, byte[] ba, int pos, byte split)
{
while (pos < ba.Length && ba[pos] != 0) {
int len;
for (len=0; pos+len < ba.Length; ++len) {
if (ba[pos+len] == 0 || ba[pos+len] == split)
break;
}
if (len > 0)
lst.Add(ByteArrayToString(ba, pos, len));
pos += len;
if (ba[pos] == split)
++pos;
}
}
/**
* Read bytes from 'ba' starting at 'pos', dividing them into strings
* along the character in 'split' and writing them into 'lst'
*/
public static IList SplitStr(IList lst, string str)
{
if (lst == null)
lst = new ArrayList();
foreach (string s in str.Split(' ')) {
lst.Add(s);
}
return lst;
}
public static string Hex(byte[] ba)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ba.Length; ++i) {
int b = ((int)ba[i]) & 0xff;
sb.Append(NYBBLES[b >> 4]);
sb.Append(NYBBLES[b & 0x0f]);
}
return sb.ToString();
}
public static string ByteArrayToString(byte[] ba, int offset, int len)
{
char[] chars = new char[len];
for (int i = 0; i < len; ++i) {
chars[i] = Convert.ToChar(ba[offset+i]);
}
return new string(chars);
}
}
}
--- NEW FILE: Cmd.cs ---
/*
* File Cmd.cs
*
* Copyright (C) 2005 Oliver Rau (olra0001@xxxxxxxxxxxxxxxxxxx)
*
* See LICENSE file for copying information
*
* Created on 16.09.2005 20:57
*
* $Id: Cmd.cs,v 1.1 2005/11/09 21:47:04 nickm Exp $
*/
using System;
namespace Tor.Control
{
/// <summary>
/// Description of Cmd.
/// </summary>
public class Cmd
{
TorControl.Commands type;
byte[] body;
#region Getter
public byte[] Body {
get {
return body;
}
}
public TorControl.Commands Type {
get {
return type;
}
}
#endregion Getter
public Cmd(TorControl.Commands t, byte[] b) { type = t; body = b; }
public Cmd(TorControl.Commands t, int l) { type = t; body = new byte[l]; }
}
}
--- NEW FILE: ConfigEntry.cs ---
/*
* File ConfigEntry.cs
*
* Copyright (C) 2005 Oliver Rau (olra0001@xxxxxxxxxxxxxxxxxxx)
*
* See LICENSE file for copying information
*
* Created on 08.08.2005 20:37
*
* $Id: ConfigEntry.cs,v 1.1 2005/11/09 21:47:04 nickm Exp $
*/
using System;
namespace Tor.Control
{
/// <summary>
/// Description of ConfigEntry.
/// </summary>
public class ConfigEntry
{
string key;
string value;
#region Getter Methods
public string Key {
get {
return key;
}
}
public string Value {
get {
return value;
}
}
#endregion Getter Methods
public ConfigEntry(string k, string v)
{
key = k;
value = v;
}
}
}
--- NEW FILE: ReplyLine.cs ---
/*
* File ReplyLine.cs
*
* Copyright (C) 2005 Oliver Rau (olra0001@xxxxxxxxxxxxxxxxxxx)
*
* See LICENSE file for copying information
*
* Created on 08.08.2005 20:37
*
* $Id: ReplyLine.cs,v 1.1 2005/11/09 21:47:04 nickm Exp $
*/
using System;
namespace Tor.Control
{
/// <summary>
/// This class handles the default reply data of tor
/// </summary>
public class ReplyLine
{
string status;
string msg;
string rest;
#region Getter Methods
public string Msg {
get {
return msg;
}
}
public string Rest {
get {
return rest;
}
}
public string Status {
get {
return status;
}
}
#endregion Getter Methods
public ReplyLine(string status, string msg, string rest)
{
this.status = status;
this.msg = msg;
this.rest = rest;
}
}
}
--- NEW FILE: Waiter.cs ---
/*
* File Waiter.cs
*
* Copyright (C) 2005 Oliver Rau (olra0001@xxxxxxxxxxxxxxxxxxx)
*
* See LICENSE file for copying information
*
* Created on 08.08.2005 20:37
*
* $Id: Waiter.cs,v 1.1 2005/11/09 21:47:04 nickm Exp $
*/
using System;
using System.Threading;
namespace Tor.Control
{
/// <summary>
/// Description of Waiter.
/// </summary>
public class Waiter
{
Guid id;
public Guid Id {
get {
return id;
}
}
object response;
public Waiter()
{
id = Guid.NewGuid();
}
public object Response {
get {
try {
while (response == null)
Thread.Sleep(1);
} catch (Exception ex) {
return null;
}
return response;
}
set {
response = value;
}
}
}
}