📄 iec16022ecc200.cs
字号:
}
private void ecc200(byte[] binary, int bytes, int datablock, int rsblock)
{
ReedSol rsObj = new ReedSol();
int blocks = (bytes + 2) / datablock, b;
rsObj.rs_init_gf(0x12d);
rsObj.rs_init_code(rsblock, 1);
for (b = 0; b < blocks; b++)
{
byte[] buf = new byte[256];
byte[] ecc = new byte[256];
int n, p = 0;
for (n = b; n < bytes; n += blocks)
buf[p++] = binary[n];
rsObj.rs_encode(p, buf, out ecc);
p = rsblock - 1; // comes back reversed
for (n = b; n < rsblock * blocks; n += blocks)
binary[bytes + n] = ecc[p--];
}
}
private char ecc200encode(byte[] t, int tl, byte[] s, int sl, byte[] encoding, ref int lenp)
{
char enc = 'a'; // start in ASCII encoding mode
int tp = 0, sp = 0;
if (encoding.Length < sl)
{
_errorMessage = "Encoding string too short";
return (char)0;
}
// do the encoding
while (sp < sl && tp < tl)
{
char newenc = enc; // suggest new encoding
if (tl - tp <= 1 && (enc == 'c' || enc == 't') || tl - tp <= 2 && enc == 'x')
enc = 'a'; // auto revert to ASCII
newenc = tolower(encoding[sp]); // in C was: newenc = tolower(encoding[sp]);
switch (newenc)
{ // encode character
case 'c': // C40
case 't': // Text
case 'x': // X12
{
char[] Out = new char[6];
char p = (char)0;
string s2 = "!\"#$%&'()*+,-./:;<=>?@[\\]_";
string s3 = "";
string e = "";
if (newenc == 'c')
{
e = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
s3 = "`abcdefghijklmnopqrstuvwxyz{|}~\0177";
}
if (newenc == 't')
{
e = " 0123456789abcdefghijklmnopqrstuvwxyz";
s3 = "`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~\0177";
}
if (newenc == 'x')
e = " 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\r*>";
do
{
char c = (char)s[sp++];
//char* w;
if ((c & 0x80) != 0)
{
if (newenc == 'x')
{
_errorMessage = "Cannot encode char 0x" + c + "X in X12";
return (char)0;
}
c &= (char)0x7f;
Out[p++] = (char)1;
Out[p++] = (char)30;
}
//w = strchr(e, c);
int w_idx = e.IndexOf(c);
if (w_idx >= 0)
Out[p++] = (char)((w_idx + 3) % 40); //Out[p++] = ((w - e) + 3) % 40;
else
{
if (newenc == 'x')
{
_errorMessage = "Cannot encode char 0x" + c + " in X12";
return (char)0;
}
if (c < 32)
{
// shift 1
Out[p++] = (char)0;
Out[p++] = c;
}
else
{
w_idx = s2.IndexOf(c); // strchr(s2, c);
if (w_idx >= 0)
{
// shift 2
Out[p++] = (char)1;
Out[p++] = (char)w_idx; // (w - s2);
}
else
{
w_idx = s3.IndexOf(c); //w = strchr(s3, c);
if (w_idx >= 0)
{
Out[p++] = (char)2;
Out[p++] = (char)w_idx;
}
else
{
_errorMessage = "Could not encode 0x" + c + "X, should not happen";
return (char)0;
}
}
}
}
if (p == 2 && tp + 2 == tl && sp == sl)
Out[p++] = (char)0; // shift 1 pad at end
while (p >= 3)
{
int v =
Out[0] * 1600 +
Out[1] * 40 + Out[2] + 1;
if (enc != newenc)
{
if (enc == 'c' || enc == 't' || enc == 'x')
t[tp++] = 254; // escape C40/text/X12
else if (enc == 'x')
t[tp++] = 0x7C; // escape EDIFACT
if (newenc == 'c')
t[tp++] = 230;
if (newenc == 't')
t[tp++] = 239;
if (newenc == 'x')
t[tp++] = 238;
enc = newenc;
}
t[tp++] = (byte)(v >> 8);
t[tp++] = (byte)(v & 0xFF);
p -= (char)3;
Out[0] = Out[3];
Out[1] = Out[4];
Out[2] = Out[5];
}
}
while ((p != 0) && (sp < sl));
break;
}
case 'e':
{
// EDIFACT
byte[] Out = new byte[4];
byte p = 0;
if (enc != newenc)
{ // can only be from C40/Text/X12
t[tp++] = 254;
enc = 'a';
}
while (sp < sl && tolower(encoding[sp]) == 'e'
&& p < 4)
Out[p++] = s[sp++];
if (p < 4)
{
Out[p++] = 0x1F;
enc = 'a';
} // termination
t[tp] = (byte)((s[0] & 0x3F) << 2);
t[tp++] |= (byte)((s[1] & 0x30) >> 4);
t[tp] = (byte)((s[1] & 0x0F) << 4);
if (p == 2)
tp++;
else
{
t[tp++] |= (byte)((s[2] & 0x3C) >> 2);
t[tp] = (byte)((s[2] & 0x03) << 6);
t[tp++] |= (byte)(s[3] & 0x3F);
}
break;
}
case 'a': // ASCII
{
if (enc != newenc)
{
if (enc == 'c' || enc == 't' || enc == 'x')
t[tp++] = 254; // escape C40/text/X12
else
t[tp++] = 0x7C; // escape EDIFACT
}
enc = 'a';
if (sl - sp >= 2 && isdigit(s[sp]) && isdigit(s[sp + 1]))
{
t[tp++] = (byte)((s[sp] - '0') * 10 + s[sp + 1] - '0' + 130);
sp += 2;
}
else if (s[sp] > 127)
{
t[tp++] = 235;
t[tp++] = (byte)(s[sp++] - 127);
}
else
t[tp++] = (byte)(s[sp++] + 1);
break;
}
case 'b': // Binary
{
int l = 0; // how much to encode
if (encoding != null)
{
int p;
for (p = sp; p < sl && tolower(encoding[p]) == 'b'; p++)
l++;
}
t[tp++] = 231; // base256
if (l < 250)
t[tp++] = (byte)l;
else
{
t[tp++] = (byte)(249 + (l / 250));
t[tp++] = (byte)(l % 250);
}
while ((l--) != 0 && tp < tl)
{
t[tp] = (byte)(s[sp++] + (((tp + 1) * 149) % 255) + 1); // see annex H
tp++;
}
enc = 'a'; // reverse to ASCII at end
break;
}
default:
_errorMessage = "Unknown encoding: " + newenc;
return (char)0; // failed
}
}
if (lenp != 0)
lenp = tp;
if (tp < tl && enc != 'a')
{
if (enc == 'c' || enc == 'x' || enc == 't')
t[tp++] = 254; // escape X12/C40/Text
else
t[tp++] = 0x7C; // escape EDIFACT
}
if (tp < tl)
t[tp++] = 129; // pad
while (tp < tl)
{ // more padding
int v = 129 + (((tp + 1) * 149) % 253) + 1; // see Annex H
if (v > 254)
v -= 254;
t[tp++] = (byte)v;
}
if (tp > tl || sp < sl)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -