我制作了一个 C# 控制台应用程序,它重定向程序的标准输出并将字节输出为 ascii85。因此,无需再担心 Windows shell 将字节输出到 C# 进程的能力。
我目前使用它从转换为 ffmpeg 的 MP3 动态重定向到 webresponse 字节,并且它可以工作。
如果将 Ascii85 与表示 100% 开销的十六进制字符串和表示 33% 数据大小开销的 base64 进行比较,则 Ascii85 表示相对于字节空间的 25% 开销。
我使用了 Jeff Atwood 在他的 Coding Horror 博客上的 Ascii85 课程:http: //www.codinghorror.com/blog/2005/10/c-implementation-of-ascii85.html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace stdshare {
class Program
{
static void Main(string[] args)
{
Int32 dur = 0;
try
{
dur = Convert.ToInt32(args[0]);
} catch { return; }
Console.InputEncoding = Encoding.UTF8;
Console.OutputEncoding = Encoding.UTF8;
using (Stream stdin = Console.OpenStandardInput())
using (Stream stdout = Console.OpenStandardOutput())
{
byte[] buffer = new byte[2048];
byte[] buffer2 = new byte[2048];
int bytes;
int pos = 0;
using (BinaryReader br = new BinaryReader(stdin))
while ((bytes = br.Read(buffer, 0, buffer.Length)) > 0)
{
buffer2 = new byte[bytes];
Buffer.BlockCopy(buffer, 0, buffer2, 0, bytes);
string s = System.Convert.ToBase64String(buffer2);
Console.WriteLine(s);
Console.Out.Flush();
stdout.Flush();
pos += 1;
if (pos > dur) break;
}
}
}
class Ascii85
{
/// <summary>
/// Prefix mark that identifies an encoded ASCII85 string, traditionally '<~'
/// </summary>
public string PrefixMark = "<~";
/// <summary>
/// Suffix mark that identifies an encoded ASCII85 string, traditionally '~>'
/// </summary>
public string SuffixMark = "~>";
/// <summary>
/// Maximum line length for encoded ASCII85 string;
/// set to zero for one unbroken line.
/// </summary>
public int LineLength = 0;
/// <summary>
/// Add the Prefix and Suffix marks when encoding, and enforce their presence for decoding
/// </summary>
public bool EnforceMarks = true;
private const int _asciiOffset = 33;
private byte[] _encodedBlock = new byte[5];
private byte[] _decodedBlock = new byte[4];
private uint _tuple = 0;
private int _linePos = 0;
private uint[] pow85 = { 85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1 };
/// <summary>
/// Decodes an ASCII85 encoded string into the original binary data
/// </summary>
/// <param name="s">ASCII85 encoded string</param>
/// <returns>byte array of decoded binary data</returns>
public byte[] Decode(string s)
{
if (EnforceMarks)
{
if (!s.StartsWith(PrefixMark) | !s.EndsWith(SuffixMark))
{
throw new Exception("ASCII85 encoded data should begin with '" + PrefixMark +
"' and end with '" + SuffixMark + "' Problematic string: " + s);
}
}
// strip prefix and suffix if present
if (s.StartsWith(PrefixMark))
{
s = s.Substring(PrefixMark.Length);
}
if (s.EndsWith(SuffixMark))
{
s = s.Substring(0, s.Length - SuffixMark.Length);
}
MemoryStream ms = new MemoryStream();
int count = 0;
bool processChar = false;
foreach (char c in s)
{
switch (c)
{
case 'z':
if (count != 0)
{
throw new Exception("The character 'z' is invalid inside an ASCII85 block.");
}
_decodedBlock[0] = 0;
_decodedBlock[1] = 0;
_decodedBlock[2] = 0;
_decodedBlock[3] = 0;
ms.Write(_decodedBlock, 0, _decodedBlock.Length);
processChar = false;
break;
case '\n':
case '\r':
case '\t':
case '\0':
case '\f':
case '\b':
processChar = false;
break;
default:
if (c < '!' || c > 'u')
{
throw new Exception("Bad character '" + c + "' found. ASCII85 only allows characters '!' to 'u'.");
}
processChar = true;
break;
}
if (processChar)
{
_tuple += ((uint)(c - _asciiOffset) * pow85[count]);
count++;
if (count == _encodedBlock.Length)
{
DecodeBlock();
ms.Write(_decodedBlock, 0, _decodedBlock.Length);
_tuple = 0;
count = 0;
}
}
}
// if we have some bytes left over at the end..
if (count != 0)
{
if (count == 1)
{
throw new Exception("The last block of ASCII85 data cannot be a single byte.");
}
count--;
_tuple += pow85[count];
DecodeBlock(count);
for (int i = 0; i < count; i++)
{
ms.WriteByte(_decodedBlock[i]);
}
}
return ms.ToArray();
}
/// <summary>
/// Encodes binary data into a plaintext ASCII85 format string
/// </summary>
/// <param name="ba">binary data to encode</param>
/// <returns>ASCII85 encoded string</returns>
public string Encode(byte[] ba)
{
StringBuilder sb = new StringBuilder((int)(ba.Length * (_encodedBlock.Length / _decodedBlock.Length)));
_linePos = 0;
if (EnforceMarks)
{
AppendString(sb, PrefixMark);
}
int count = 0;
_tuple = 0;
foreach (byte b in ba)
{
if (count >= _decodedBlock.Length - 1)
{
_tuple |= b;
if (_tuple == 0)
{
AppendChar(sb, 'z');
}
else
{
EncodeBlock(sb);
}
_tuple = 0;
count = 0;
}
else
{
_tuple |= (uint)(b << (24 - (count * 8)));
count++;
}
}
// if we have some bytes left over at the end..
if (count > 0)
{
EncodeBlock(count + 1, sb);
}
if (EnforceMarks)
{
AppendString(sb, SuffixMark);
}
return sb.ToString();
}
private void EncodeBlock(StringBuilder sb)
{
EncodeBlock(_encodedBlock.Length, sb);
}
private void EncodeBlock(int count, StringBuilder sb)
{
for (int i = _encodedBlock.Length - 1; i >= 0; i--)
{
_encodedBlock[i] = (byte)((_tuple % 85) + _asciiOffset);
_tuple /= 85;
}
for (int i = 0; i < count; i++)
{
char c = (char)_encodedBlock[i];
AppendChar(sb, c);
}
}
private void DecodeBlock()
{
DecodeBlock(_decodedBlock.Length);
}
private void DecodeBlock(int bytes)
{
for (int i = 0; i < bytes; i++)
{
_decodedBlock[i] = (byte)(_tuple >> 24 - (i * 8));
}
}
private void AppendString(StringBuilder sb, string s)
{
if (LineLength > 0 && (_linePos + s.Length > LineLength))
{
_linePos = 0;
sb.Append('\n');
}
else
{
_linePos += s.Length;
}
sb.Append(s);
}
private void AppendChar(StringBuilder sb, char c)
{
sb.Append(c);
_linePos++;
if (LineLength > 0 && (_linePos >= LineLength))
{
_linePos = 0;
sb.Append('\n');
}
}
}
}
}