Ascii85 Algorithm

Ascii85 use five ASCII characters to represent four bytes of binary data (encoded size 25% larger), it is more efficient than Base64, which use four characters to represent three bytes of data (33% increase). This encode method is suitable for posting small blocks of binary data to BBS or forum as plain text. It is also used in Portable Document Format. I implemented this algoritm in C# when doing my PDF processor project. You can find more description about ASCII85 in Wikipedia.

 

复制代码
The code
    /// <summary>
    
/// Adobe ASCII85 for encoding binary data in ASCII base-85
    
/// </summary>
    public class ASCII85
    {
        
/// <summary>
        
/// Maximum line length for encoded ASCII85 string; 
        
/// set to zero for one unbroken line.
        
/// </summary>
        public static int LineLength = 75;

        
static uint[] pow85 = { 85 * 85 * 85 * 8585 * 85 * 8585 * 85851 };

        
/// <summary>
        
/// Encodes binary data into a plaintext ASCII85 format string
        
/// </summary>
        
/// <param name="data">binary data to encode</param>
        
/// <returns>ASCII85 encoded string</returns>
        public static string Encode(byte[] data)
        {
            MemoryStream input 
= new MemoryStream(data);
            MemoryStream output 
= new MemoryStream();
            Encode(input, output);
            output.Position 
= 0;
            StreamReader reader 
= new StreamReader(output, Encoding.ASCII);
            
return reader.ReadToEnd();
        }

        
/// <summary>
        
/// Decodes an ASCII85 encoded string into the original binary data
        
/// </summary>
        
/// <param name="code">ASCII85 encoded string</param>
        
/// <returns>byte array of decoded binary data</returns>
        public static byte[] Decode(string code)
        {
            MemoryStream input 
= new MemoryStream(Encoding.ASCII.GetBytes(code));
            MemoryStream output 
= new MemoryStream();
            Decode(input, output);
            
return output.ToArray();
        }

        
/// <summary>
        
/// Encodes the specified input.
        
/// </summary>
        
/// <param name="input">The input.</param>
        
/// <param name="output">The output.</param>
        public static void Encode(Stream input, Stream output)
        {
            StreamWriter writer 
= new StreamWriter(output, Encoding.ASCII);
            
uint word = 0;
            
int count = 0;
            
int linepos = 0;
            
int code = input.ReadByte();
            
while (code != -1)
            {
                word 
|= (uint)(code << (24 - (count * 8)));
                count
++;
                
if (count == 4)
                {
                    
if (word == 0)
                    {
                        writer.Write(
'z');
                        linepos
++;
                    }
                    
else
                    {
                        writer.Write(Encode(word));
                        linepos 
+= 5;
                    }
                    word 
= 0;
                    count 
= 0;
                }
                
if (linepos >= LineLength)
                {
                    writer.WriteLine();
                    linepos 
= 0;
                }
                code 
= input.ReadByte();
            }
            
if (count > 0)
            {
                writer.Write(Encode(word), 
0, count + 1);
            }
            writer.Write(
"~>");
            writer.Flush();
        }

        
private static char[] Encode(uint word)
        {
            
char[] group = new char[5];
            
for (int i = group.Length - 1; i >= 0; i--)
            {
                group[i] 
= (char)(word % 85 + 33);
                word 
/= 85;
            }
            
return group;
        }

        
/// <summary>
        
/// Decodes the specified input.
        
/// </summary>
        
/// <param name="input">The input.</param>
        
/// <param name="output">The output.</param>
        public static void Decode(Stream input, Stream output)
        {
            BinaryWriter writer 
= new BinaryWriter(output);
            
uint word = 0;
            
int count = 0;
            
int code = input.ReadByte();
            
while (code != -1)
            {
                
if (code == 122// 'z'
                {
                    
if (count == 0)
                    {
                        writer.Write((
uint)0);
                    }
                    
else
                    {
                        
throw new Exception("A z character occurs in the middle of a group.");
                    }
                }
                
else if (code >= 33 && code <= 117)
                {
                    word 
+= (uint)((code - 33* pow85[count]);
                    count
++;
                    
if (count == 5)
                    {
                        writer.Write(Word2Bytes(word));
                        word 
= 0;
                        count 
= 0;
                    }
                }
                
else
                {
                    
switch (code)
                    {
                        
case 0:
                        
case 9:  // HT
                        case 10// LF
                        case 11// VT
                        case 12// FF
                        case 13// CR
                        case 32// SP
                            break;
                        
case 126// ~>
                            goto end;
                        
default:
                            
throw new Exception("Invalid character in ASCII85Decode:" + code);
                    }
                }
                code 
= input.ReadByte();
            }
        end:
            
if (count > 0)
            {
                count
--;
                word 
+= pow85[count]; // add maximum remained value
                writer.Write(Word2Bytes(word), 0, count);
            }
            writer.Flush();
        }

        
/// <summary>
        
/// split uint32 into bytes by big-endian order
        
/// </summary>
        
/// <param name="word"></param>
        
/// <returns></returns>
        static byte[] Word2Bytes(uint word)
        {
            
byte[] bytes = new byte[4];
            bytes[
0= (byte)((word & 0xFF000000>> 24);
            bytes[
1= (byte)((word & 0x00FF0000>> 16);
            bytes[
2= (byte)((word & 0x0000FF00>> 8);
            bytes[
3= (byte)(word & 0x000000FF);
            
return bytes;
        }
    }
复制代码

 

 

 

posted @   刘俊峰  阅读(1526)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示