-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extensions.cs
94 lines (90 loc) · 2.86 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Collections.Generic;
using System.Reflection;
using AdiIRCAPI;
using Chaos.NaCl;
namespace System.Runtime.CompilerServices {
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class
| AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute { }
}
namespace AdiIRC_Encrypt {
public static class Extensions {
private const byte escapeChar = 0x21;
private static Assembly adiIRC = null;
// Extending the server API
public static bool SendFakePrivMessage(this IServer server, IUser source, string msg) {
string line = string.Format(":{0}!{1}@{2} PRIVMSG {3} :{4}", source.Nick, source.Ident, source.Host, server.UserNick, msg);
return server.SendFakeRaw(line);
}
// Extending byte[]
public static byte[] SubArray(this byte[] data, int startIdx, int length = 0) {
if (length <= 0) {
length = (data.Length - startIdx) + length;
}
byte[] result = new byte[length];
Array.Copy(data, startIdx, result, 0, length);
return result;
}
public static bool StartsWith(this byte[] data, byte[] start) {
if ((start == null) || (data.Length < start.Length))
return false;
for (int i = 0; i < start.Length; i++) {
if (data[i] != start[i])
return false;
}
return true;
}
public static byte[] ReadBase64(this string data) {
return CryptoBytes.FromBase64String(data);
}
public static byte[] IrcEscape(this byte[] data) {
List<byte> result = new List<byte>();
for (int i = 0; i < data.Length; i++) {
if ((data[i] == 0x00) || (data[i] == 0x0A) || (data[i] == 0x0D) || (data[i] == escapeChar)) {
result.Add(escapeChar);
result.Add((byte)(data[i] + escapeChar));
continue;
}
result.Add(data[i]);
}
return result.ToArray();
}
public static byte[] IrcUnescape(this byte[] data) {
List<byte> result = new List<byte>();
for (int i = 0; i < data.Length; i++) {
if (data[i] == escapeChar) {
result.Add((byte)(data[++i] - escapeChar));
continue;
}
result.Add(data[i]);
}
return result.ToArray();
}
public static byte[] Concatenate(this byte[] data, params byte[][] args) {
int totalLength = data.Length;
foreach (byte[] arg in args) {
totalLength += arg.Length;
}
byte[] result = new byte[totalLength];
data.CopyTo(result, 0);
int offset = data.Length;
foreach (byte[] arg in args) {
arg.CopyTo(result, offset);
offset += arg.Length;
}
return result;
}
// Extending string
public static int IndexOf(this byte[] data, byte needle, int skipCount = 0) {
for (int i = 0; i < data.Length; i++) {
if ((data[i] == needle) && (skipCount-- == 0))
return i;
}
return -1;
}
public static string ToBase64String(this byte[] data) {
return CryptoBytes.ToBase64String(data);
}
}
}