llsdXml: do direct base64 encode

master
UbitUmarov 2020-05-15 14:41:38 +01:00
parent 5848d3a6bb
commit 214cf5d60b
1 changed files with 129 additions and 3 deletions

View File

@ -148,7 +148,22 @@ namespace OpenSim.Framework
else else
{ {
sb.Append("<binary>"); // encode64 is default sb.Append("<binary>"); // encode64 is default
sb.Append(Convert.ToBase64String(e,Base64FormattingOptions.None)); base64Encode(e, sb);
sb.Append("</binary>");
}
}
public static void AddElem(byte[] e, int start, int lenght, StringBuilder sb)
{
if (start + lenght >= e.Length)
lenght = e.Length - start;
if (e == null || e.Length == 0 || lenght <= 0)
sb.Append("binary />");
else
{
sb.Append("<binary>"); // encode64 is default
base64Encode(e, start, lenght, sb);
sb.Append("</binary>"); sb.Append("</binary>");
} }
} }
@ -451,12 +466,31 @@ namespace OpenSim.Framework
sb.Append(name); sb.Append(name);
sb.Append("</key>"); sb.Append("</key>");
if(e == null || e.Length == 0) if (e == null || e.Length == 0)
sb.Append("binary />"); sb.Append("binary />");
else else
{ {
sb.Append("<binary>"); // encode64 is default sb.Append("<binary>"); // encode64 is default
sb.Append(Convert.ToBase64String(e,Base64FormattingOptions.None)); base64Encode(e, sb);
sb.Append("</binary>");
}
}
public static void AddElem(string name, byte[] e, int start, int lenght, StringBuilder sb)
{
sb.Append("<key>");
sb.Append(name);
sb.Append("</key>");
if (start + lenght >= e.Length)
lenght = e.Length - start;
if (e == null || e.Length == 0 || lenght <= 0)
sb.Append("binary />");
else
{
sb.Append("<binary>"); // encode64 is default
base64Encode(e, start, lenght, sb);
sb.Append("</binary>"); sb.Append("</binary>");
} }
} }
@ -786,5 +820,97 @@ namespace OpenSim.Framework
(byte)value (byte)value
}; };
} }
static readonly char[] base64Chars = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O',
'P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d',
'e','f','g','h','i','j','k','l','m','n','o','p','q','r','s',
't','u','v','w','x','y','z','0','1','2','3','4','5','6','7',
'8','9','+','/'};
public static unsafe void base64Encode(byte[] data, StringBuilder sb)
{
int lenMod3 = data.Length % 3;
int len = data.Length - lenMod3;
fixed (byte* d = data)
{
fixed (char* b64 = base64Chars)
{
int i = 0;
while(i < len)
{
sb.Append(b64[d[i] >> 2]);
sb.Append(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
sb.Append(b64[((d[i + 1] & 0x0f) << 2) | ((d[i + 2] & 0xc0) >> 6)]);
sb.Append(b64[d[i + 2] & 0x3f]);
i += 3;
}
switch (lenMod3)
{
case 2:
{
i = len;
sb.Append(b64[d[i] >> 2]);
sb.Append(b64[((d[i] & 0x03) << 4) | ((d[i + 1] & 0xf0) >> 4)]);
sb.Append(b64[((d[i + 1] & 0x0f) << 2)]);
sb.Append('=');
break;
}
case 1:
{
i = len;
sb.Append(b64[d[i] >> 2]);
sb.Append(b64[(d[i] & 0x03) << 4]);
sb.Append("==");
break;
}
}
}
}
}
public static unsafe void base64Encode(byte[] data, int start, int lenght, StringBuilder sb)
{
int lenMod3 = lenght % 3;
int len = start + (lenght - lenMod3);
fixed (byte* d = data)
{
fixed (char* b64 = base64Chars)
{
int i = start;
while(i < len)
{
sb.Append(b64[(d[i] & 0xfc) >> 2]);
sb.Append(b64[((d[i] & 0x03) << 4) | (d[i + 1] >> 4)]);
sb.Append(b64[((d[i + 1] & 0x0f) << 2) | (d[i + 2] >> 6)]);
sb.Append(b64[d[i + 2] & 0x3f]);
i += 3;
}
switch (lenMod3)
{
case 2:
{
i = len;
sb.Append(b64[d[i] >> 2]);
sb.Append(b64[((d[i] & 0x03) << 4) | (d[i + 1] >> 4)]);
sb.Append(b64[((d[i + 1] & 0x0f) << 2)]);
sb.Append('=');
break;
}
case 1:
{
i = len;
sb.Append(b64[d[i] >> 2]);
sb.Append(b64[(d[i] & 0x03) << 4]);
sb.Append("==");
break;
}
}
}
}
}
} }
} }