-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTextDecode.cs
129 lines (107 loc) · 3.29 KB
/
TextDecode.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
namespace spb2xml
{
static partial class TextDecode
{
static readonly byte[][] S = new byte[256][];
static readonly char[,] K = new char[256, 250];
static void Transform()
{
for (int i = 0; i < K.GetLength(0); i++)
{
for (int j = 0; j < K.GetLength(1); j++)
{
K[i, j] = (char)0xff;
}
}
for (int i = 0; i < S.Length; i++)
{
var row = S[i];
if (row != null)
{
for (int j = 0; j < row.Length; j++)
{
K[row[j], j] = (char)i;
}
}
}
}
public static byte[] Encode(string str)
{
var bytes = new byte[str.Length + 1];
int i = 0;
for (; i < str.Length; i++)
{
var row = S[str[i]];
bytes[i] = row?[i % 250] ?? 0xff;
}
bytes[i] = S[0][i % 250];
return bytes;
}
public static string Decode(byte[] encoded)
{
var chars = new char[encoded.Length - 1];
int i = 0;
for (; i < chars.Length; i++)
{
chars[i] = K[encoded[i], i % 250];
}
var last = K[encoded[i], i % 250];
if (last != 0)
{
throw new Exception("Unexpected");
}
return new string(chars);
}
[Conditional("DEBUG")]
private static void CheckUnique()
{
// vertical
for (int j = 0; j < 250; j++)
{
var bytes = new List<byte>();
for (int i = 0; i < S.Length; i++)
{
var row = S[i];
if (row != null)
{
bytes.Add(row[j]);
}
}
var u = bytes.Distinct().ToArray();
var e = Enumerable.Range(0, 256).Select(x => (byte)x).Except(bytes).ToArray();
Debug.Assert(u.Length == bytes.Count);
Debug.Assert(u.Length == 195);
Debug.Assert(e.Length == 61);
}
// horizontal
for (int i = 0; i < S.Length; i++)
{
var row = S[i];
if (row != null)
{
var bytes = new List<byte>();
for (int j = 0; j < 250; j++)
{
bytes.Add(row[j]);
}
var u = bytes.Distinct().ToArray();
var e = Enumerable.Range(0, 256).Select(x => (byte)x).Except(bytes).ToArray();
Debug.Assert(u.Length == bytes.Count);
Debug.Assert(u.Length == 250);
Debug.Assert(e.Length == 6);
Debug.Assert(e.Contains((byte)i));
}
}
}
static TextDecode()
{
InitData();
Transform();
CheckUnique();
}
}
}