-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiCoder.cs
More file actions
588 lines (496 loc) · 18.7 KB
/
MultiCoder.cs
File metadata and controls
588 lines (496 loc) · 18.7 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
using System;
using System.Diagnostics.CodeAnalysis;
using System.Text;
// ReSharper disable ForCanBeConvertedToForeach
// ReSharper disable LoopCanBeConvertedToQuery
namespace MultiCode;
/// <summary>
/// Encode and Decode multi-code strings
/// </summary>
public static class MultiCoder
{
/// <summary>
/// Encode binary data to a multi-code string
/// </summary>
/// <param name="data">original data to encode</param>
/// <param name="correctionSymbols">number of extra symbols to add for error recovery</param>
public static string Encode(byte[] data, int correctionSymbols)
{
// Convert from bytes to nybbles
var src = FlexArray.Fixed(data.Length * 2);
int j = 0;
for (int i = 0; i < data.Length; i++) {
var upper = (data[i] >> 4) & 0x0F;
var lower = data[i] & 0x0F;
src.Set(j++, upper);
src.Set(j++, lower);
}
var encoded = RsEncode(src, correctionSymbols);
var output = Display(encoded);
encoded.Release();
src.Release();
return output;
}
/// <summary>
/// Decode a multi-code string to binary data
/// </summary>
/// <param name="code">user input: encoded data to restore</param>
/// <param name="dataLength">expected length of data in bytes, excluding correction symbols</param>
/// <param name="correctionSymbols">number of extra bytes added to original data</param>
public static byte[] Decode(string code, int dataLength, int correctionSymbols)
{
var expectedCodeLength = (dataLength * 2) + correctionSymbols;
var cleanInput = DecodeDisplay(expectedCodeLength, code);
Console.WriteLine(cleanInput.ToString());
// Input too short
if (cleanInput.Length() < expectedCodeLength) {
cleanInput.Release();
return [];
}
// Input too long
if (cleanInput.Length() > expectedCodeLength) {
cleanInput.Release();
return [];
}
var decoded = TryHardDecode(cleanInput, correctionSymbols, cleanInput.Length());
if (decoded.Ok) {
// remove error correction symbols
for (var i = 0; i < correctionSymbols; i++) decoded.Result?.Pop();
// decoded data is nybbles, convert back to bytes
var final = new byte[(decoded.Result?.Length()??0) / 2];
for (int i = 0; i < final.Length; i++) {
var upper = (decoded.Result?.PopFirst()??0) << 4;
var lower = decoded.Result?.PopFirst()??0;
final[i] = (byte)(upper + lower);
}
cleanInput.Release();
if (decoded.Result != cleanInput) decoded.Release();
return final;
}
cleanInput.Release();
if (decoded.Result != cleanInput) decoded.Release();
return [];
}
#region Code parameters
// Note: '~' is for error.
// Q and S are lower cased to look less like 0 and 5.
/// <summary> Characters for odd-positioned output codes </summary>
private static readonly char[] OddSet = ['0', '1', '2', '3', '6', '7', '8', '9', 'b', 'G', 'J', 'N', 'q', 'X', 'Y', 'Z', '~'];
/// <summary> Characters for even-positioned output codes </summary>
private static readonly char[] EvenSet = ['4', '5', 'A', 'C', 'D', 'E', 'F', 'H', 'K', 'M', 'P', 'R', 's', 'T', 'V', 'W', '~'];
/// <summary>
/// Look up characters likely to be entered as spaces. These will be trimmed from input
/// </summary>
private static bool IsSpace(char c)
{
switch (c)
{
case ' ':
case '-':
case '.':
case '_':
case '+':
case '*':
case '#':
return true;
default: return false;
}
}
/// <summary> Likely mistakes. Mapped to characters we guess are correct </summary>
private static char Correction(char inp)
{
switch (inp)
{
case 'O': return '0';
case 'L': return '1';
case 'I': return '1';
case 'U': return 'V';
default: return inp;
}
}
/// <summary> Case changes to improve letter/number distinction </summary>
private static char CaseChanges(char inp)
{
switch (inp) {
case 'B': return 'b';
case 'Q': return 'q';
case 'S': return 's';
default: return inp;
}
}
#endregion Code parameters
#region Internal
/// <summary>
/// Find index in char array, or -1 if not found
/// </summary>
private static int IndexOf(char[] src, char target)
{
for (int i = 0; i < src.Length; i++) {
if (src[i] == target) return i;
}
return -1;
}
/// <summary>
/// Message value, and message output position to encoded character
/// </summary>
private static char EncodeDisplay(int symbol, int position)
{
if (symbol < 0 || symbol > 15) return '~';
if ((position & 1) == 0) return OddSet[symbol];
return EvenSet[symbol];
}
/// <summary>
/// Create an output string for message data
/// </summary>
private static string Display(FlexArray message)
{
var sb = new StringBuilder();
for (int i = 0; i < message.Length(); i++)
{
if (i > 0)
{
if (i % 4 == 0) sb.Append('-');
else if (i % 2 == 0) sb.Append(' ');
}
sb.Append(EncodeDisplay(message.Get(i), i));
}
return sb.ToString();
}
/// <summary>
/// Find first position where chirality is incorrect
/// </summary>
private static int FindFirstChiralityError(FlexArray chirality)
{
for (var position = 0; position < chirality.Length(); position++) {
var expected = position & 1;
if (chirality.Get(position) != expected) return position;
}
return -1;
}
/// <summary>
/// Try to repair errors in the input code that can be
/// detected with chirality
/// </summary>
private static bool RepairCodesAndChirality(int expectedCodeLength,
FlexArray codes, FlexArray chirality)
{
const bool tryAgain = false;
const bool completed = true;
if (codes.Length() != chirality.Length())
{
// ERROR in code/chirality code
return completed; // can't do much here
}
var currentLength = codes.Length();
var minLength = (2 * expectedCodeLength) / 3;
if (currentLength < minLength)
{
// Code is too short to recover accurately
return completed;
}
var firstErrPos = FindFirstChiralityError(chirality);
if (currentLength == expectedCodeLength && firstErrPos < 0)
{
// Input codes seem correct
return completed;
}
// If input is shorter than expected, guess where a deletion occurred, and insert a zero-value.
if (currentLength < expectedCodeLength)
{
// Insert a zero value at error position, and inject correct chirality
if (firstErrPos < 0)
{
// error is at the end
var chi = currentLength & 1;
var endChi = expectedCodeLength & 1;
var diff = expectedCodeLength - currentLength;
if (diff == 1 && chi == endChi)
{
// don't add a wrong chi at the end if we're off-by-one
codes.AddStart(0);
chirality.AddStart(0);
}
else
{
codes.Push(0);
chirality.Push(chi);
}
}
else
{
var chi = firstErrPos & 1;
var chiNext = (firstErrPos + 1) & 1;
var chiAfter = (firstErrPos + 2) & 1;
// First, check if this is a transpose and not the first delete
if (firstErrPos < currentLength - 3 // not near end
&& chirality.Get(firstErrPos) != chi // this position has wrong chirality
&& chirality.Get(firstErrPos+1) != chiNext // next position ALSO has wrong chirality
&& chirality.Get(firstErrPos+2) == chiAfter // but after that it's ok
) {
// Swap these character
codes.Swap(firstErrPos, firstErrPos + 1);
chirality.Swap(firstErrPos, firstErrPos + 1);
return tryAgain;
}
// looks like a delete
codes.InsertAt(firstErrPos, 0);
chirality.InsertAt(firstErrPos, chi);
}
return tryAgain;
}
// If input is longer than expected, guess where the problem is and delete
if (currentLength > expectedCodeLength)
{
// First, if the last code is bad chirality, delete that before anything else
var expectedLastChi = (1 + expectedCodeLength) & 1;
if (chirality.Get(currentLength - 1) != expectedLastChi)
{
codes.Pop();
chirality.Pop();
return tryAgain;
}
// value and chirality at error position
if (firstErrPos < 0) firstErrPos = currentLength - 1;
codes.DeleteAt(firstErrPos);
chirality.DeleteAt(firstErrPos);
return tryAgain;
}
// Input is correct length, but we have swapped characters.
// Try swapping at first error, unless it is at the end.
if (firstErrPos >= expectedCodeLength - 1)
{
return completed;
}
if (chirality.Get(firstErrPos) == chirality.Get(firstErrPos + 1))
{
// A simple swap won't fix this. Either a totally wrong code, or repeated insertions and deletions.
// For now, we will flip the chirality without changing anything so the checks can continue.
chirality.Set(firstErrPos, 1 - chirality.Get(firstErrPos));
return tryAgain;
}
// swapping characters might fix the problem
codes.Swap(firstErrPos, firstErrPos + 1);
chirality.Swap(firstErrPos, firstErrPos + 1);
return tryAgain;
}
/// <summary>
/// Try to decode a string input into a symbol array
/// </summary>
private static FlexArray DecodeDisplay(int expectedCodeLength, string input)
{
var validCharCount = 0;
// Run filters first, to get the number of 'correct' characters.
// We could extend this to store the location of unexpected chars to improve the next loop.
for (var i = 0; i < input.Length; i++) {
var src = char.ToUpperInvariant(input[i]);
if (IsSpace(src)) continue; // skip spaces
src = CaseChanges(src); // Q->q, S->s, B->b
src = Correction(src); // fix for anticipated transcription errors
var oddIdx = IndexOf(OddSet, src);
var evenIdx = IndexOf(EvenSet, src);
if (oddIdx >= 0 || evenIdx >= 0) validCharCount++;
}
// negative = too many chars. Positive = too few.
var charCountMismatch = expectedCodeLength - validCharCount;
var codes = FlexArray.EmptyWithStorage(validCharCount);
var chirality = FlexArray.EmptyWithStorage(validCharCount);
var nextChir = 0;
for (var i = 0; i < input.Length; i++) {
var src = char.ToUpperInvariant(input[i]); // make upper-case
if (IsSpace(src)) continue; // skip spaces
src = CaseChanges(src); // Q->q, S->s, B->b
src = Correction(src); // fix for anticipated transcription errors
var oddIdx = IndexOf(OddSet, src);
var evenIdx = IndexOf(EvenSet, src);
if (oddIdx < 0 && evenIdx < 0) {
// Broken character, maybe insert dummy.
if (charCountMismatch > 0) {
codes.Push(0);
chirality.Push(nextChir);
nextChir = 1 - nextChir;
charCountMismatch--;
} else {
charCountMismatch++;
}
} else if (oddIdx >= 0 && evenIdx >= 0) {
// Should never happen!
codes.Release();
chirality.Release();
return FlexArray.Fixed(0);
} else if (oddIdx >= 0) {
codes.Push(oddIdx);
chirality.Push(0);
nextChir = 1;
} else {
codes.Push(evenIdx);
chirality.Push(1);
nextChir = 0;
}
}
for (var tries = 0; tries < expectedCodeLength; tries++) {
if (RepairCodesAndChirality(expectedCodeLength, codes, chirality)) {
chirality.Release();
return codes;
}
}
chirality.Release();
return codes;
}
/// <summary>
/// Main RS encode.
/// </summary>
/// <param name="msg">array of ints in 0..15</param>
/// <param name="sym">number of additional symbols</param>
private static FlexArray RsEncode(FlexArray msg, int sym)
{
var gen = Galois16.IrreduciblePoly(sym);
var mix = FlexArray.BySize(msg.Length() + gen.Length() - 1);
for (var i = 0; i < msg.Length(); i++)
{
mix.Set(i, msg.Get(i));
}
for (var i = 0; i < msg.Length(); i++)
{
var coeff = mix.Get(i);
if (coeff == 0) continue;
for (var j = 1; j < gen.Length(); j++)
{
var next = mix.Get(i + j) ^ Galois16.Mul(gen.Get(j), coeff);
mix.Set(i + j, next);
}
}
var outp = FlexArray.BySize(0);
var len = msg.Length() + gen.Length() - 1;
for (var i = 0; i < len; i++) outp.Push(mix.Get(i));
for (var i = 0; i < msg.Length(); i++)
{
outp.Set(i, msg.Get(i));
}
gen.Release();
mix.Release();
return outp;
}
/// <summary>
/// Main decode and correct function
/// </summary>
/// <param name="msg">input symbols</param>
/// <param name="sym">count of additional check symbols in input</param>
/// <param name="expectedLength">expected length of input</param>
private static DecodeResult RsDecode(FlexArray msg, int sym, int expectedLength)
{
var result = new DecodeResult
{
Ok = false,
Errs = false,
Result = msg.Copy(),
Info = ""
};
var erases = expectedLength - msg.Length();
var synd = ReedSolomon.CalcSyndromes(msg, sym);
if (synd.AllZero()) {
//log('no errors found');
result.Ok = true;
synd.Release();
return result;
}
var errPoly = ReedSolomon.ErrorLocatorPoly(synd, sym, erases);
if (errPoly.Length() - 1 - erases > sym) {
result.Errs = true;
result.Info = "too many errors (A)";
errPoly.Release();
synd.Release();
return result;
}
errPoly.Reverse();
var errorPositions = ReedSolomon.FindErrors(errPoly, msg.Length());
if (errorPositions.Length() < 1) {
result.Errs = true;
result.Info = "too many errors (B)";
errorPositions.Release();
errPoly.Release();
synd.Release();
return result;
}
errorPositions.Reverse();
result.Release();
result.Result = ReedSolomon.CorrectErrors(msg, synd, errorPositions);
errorPositions.Release();
errPoly.Release();
synd.Release();
// recheck result
var synd2 = ReedSolomon.CalcSyndromes(result.Result, sym);
if (synd2.AllZero()) {
//log('all errors corrected');
synd2.Release();
result.Ok = true;
result.Errs = true;
result.Info = "all errors corrected";
return result;
}
synd2.Release();
result.Ok = false;
result.Errs = true;
result.Info = "too many errors (C)";
return result;
}
private static DecodeResult TryHardDecode(FlexArray msg, int sym,
int expectedLength)
{
var basicDecode = RsDecode(msg, sym, expectedLength);
if (basicDecode.Ok) return basicDecode;
// Normal decoding didn't work. Try rotations
var end = msg.Length();
var half = end / 2;
int i;
for (i = 0; i < half; i++) {
// rotate left until we run out of zeros
var r = msg.PopFirst(); // remove and return first element
if (r != 0) {
msg.AddStart(r);
break;
}
msg.Push(r);
basicDecode.Release();
basicDecode = RsDecode(msg, sym, expectedLength);
if (basicDecode.Ok) return basicDecode;
}
// undo
while (i > 0) {
i--;
var r = msg.Pop();
msg.AddStart(r);
}
for (i = 0; i < half; i++) {
// rotate right until we run out of zeros
var r = msg.Pop();
if (r != 0) {
msg.Push(r);
break;
}
msg.AddStart(r);
basicDecode.Release();
basicDecode = RsDecode(msg, sym, expectedLength);
if (basicDecode.Ok) return basicDecode;
}
return basicDecode;
}
/// <summary>
/// Result from RsDecode
/// </summary>
[SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Global")]
internal class DecodeResult
{
public bool Ok { get; set; }
public bool Errs { get; set; }
public FlexArray? Result { get; set; }
public string Info { get; set; } = "";
/// <summary>
/// Release result if there is one
/// </summary>
public void Release()
{
Result?.Release();
}
}
#endregion Internal
}