~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Pdf.Filters/LzwDecode.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region PDFsharp - A .NET library for processing PDF
 
2
//
 
3
// Authors:
 
4
//   David Stephensen (mailto:David.Stephensen@pdfsharp.com)
 
5
//
 
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
 
7
//
 
8
// http://www.pdfsharp.com
 
9
// http://sourceforge.net/projects/pdfsharp
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining a
 
12
// copy of this software and associated documentation files (the "Software"),
 
13
// to deal in the Software without restriction, including without limitation
 
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
15
// and/or sell copies of the Software, and to permit persons to whom the
 
16
// Software is furnished to do so, subject to the following conditions:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included
 
19
// in all copies or substantial portions of the Software.
 
20
//
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
27
// DEALINGS IN THE SOFTWARE.
 
28
#endregion
 
29
 
 
30
using System;
 
31
using System.Diagnostics;
 
32
using System.Text;
 
33
using System.IO;
 
34
 
 
35
namespace PdfSharp.Pdf.Filters
 
36
{
 
37
  /// <summary>
 
38
  /// Implements the LzwDecode filter.
 
39
  /// </summary>
 
40
  public class LzwDecode : Filter
 
41
  {
 
42
    /// <summary>
 
43
    /// Throws a NotImplementedException because the obsolete LZW encoding is not supported by PDFsharp.
 
44
    /// </summary>
 
45
    public override byte[] Encode(byte[] data)
 
46
    {
 
47
      throw new NotImplementedException("PDFsharp does not support LZW encoding.");
 
48
    }
 
49
 
 
50
    /// <summary>
 
51
    /// Decodes the specified data.
 
52
    /// </summary>
 
53
    public override byte[] Decode(byte[] data, FilterParms parms)
 
54
    {
 
55
      if (data[0] == (byte)0x00 && data[1] == (byte)0x01)
 
56
        throw new Exception("LZW flavour not supported.");
 
57
 
 
58
      MemoryStream outputStream = new MemoryStream();
 
59
 
 
60
      InitializeDictionary();
 
61
 
 
62
      this.data = data;
 
63
 
 
64
      bytePointer = 0;
 
65
 
 
66
      nextData = 0;
 
67
      nextBits = 0;
 
68
 
 
69
      int code, oldCode = 0;
 
70
      byte[] str;
 
71
 
 
72
      while ((code = this.NextCode) != 257)
 
73
      {
 
74
        if (code == 256)
 
75
        {
 
76
          InitializeDictionary();
 
77
          code = NextCode;
 
78
          if (code == 257)
 
79
          {
 
80
            break;
 
81
          }
 
82
          outputStream.Write(stringTable[code], 0, stringTable[code].Length);
 
83
          oldCode = code;
 
84
 
 
85
        }
 
86
        else
 
87
        {
 
88
          if (code < tableIndex)
 
89
          {
 
90
            str = stringTable[code];
 
91
            outputStream.Write(str, 0, str.Length);
 
92
            AddEntry(stringTable[oldCode], str[0]);
 
93
            oldCode = code;
 
94
          }
 
95
          else
 
96
          {
 
97
            str = stringTable[oldCode];
 
98
            outputStream.Write(str, 0, str.Length);
 
99
            AddEntry(str, str[0]);
 
100
            oldCode = code;
 
101
          }
 
102
        }
 
103
      }
 
104
 
 
105
      if (outputStream.Length >= 0)
 
106
      {
 
107
        outputStream.Capacity = (int)outputStream.Length;
 
108
        return outputStream.GetBuffer();
 
109
      }
 
110
      return null;
 
111
    }
 
112
 
 
113
    /// <summary>
 
114
    /// Initialize the dictionary.
 
115
    /// </summary>
 
116
    void InitializeDictionary()
 
117
    {
 
118
      stringTable = new byte[8192][];
 
119
 
 
120
      for (int i = 0; i < 256; i++)
 
121
      {
 
122
        stringTable[i] = new byte[1];
 
123
        stringTable[i][0] = (byte)i;
 
124
      }
 
125
 
 
126
      tableIndex = 258;
 
127
      bitsToGet = 9;
 
128
    }
 
129
 
 
130
    /// <summary>
 
131
    /// Add a new entry to the Dictionary.
 
132
    /// </summary>
 
133
    void AddEntry(byte[] oldstring, byte newstring)
 
134
    {
 
135
      int length = oldstring.Length;
 
136
      byte[] str = new byte[length + 1];
 
137
      Array.Copy(oldstring, 0, str, 0, length);
 
138
      str[length] = newstring;
 
139
 
 
140
      stringTable[tableIndex++] = str;
 
141
 
 
142
      if (tableIndex == 511)
 
143
        bitsToGet = 10;
 
144
      else if (tableIndex == 1023)
 
145
        bitsToGet = 11;
 
146
      else if (tableIndex == 2047)
 
147
        bitsToGet = 12;
 
148
    }
 
149
 
 
150
    /// <summary>
 
151
    /// Returns the next set of bits.
 
152
    /// </summary>
 
153
    int NextCode
 
154
    {
 
155
      get
 
156
      {
 
157
        try
 
158
        {
 
159
          nextData = (nextData << 8) | (data[bytePointer++] & 0xff);
 
160
          nextBits += 8;
 
161
 
 
162
          if (nextBits < bitsToGet)
 
163
          {
 
164
            nextData = (nextData << 8) | (data[bytePointer++] & 0xff);
 
165
            nextBits += 8;
 
166
          }
 
167
 
 
168
          int code = (nextData >> (nextBits - bitsToGet)) & andTable[bitsToGet - 9];
 
169
          nextBits -= bitsToGet;
 
170
 
 
171
          return code;
 
172
        }
 
173
        catch
 
174
        {
 
175
          return 257;
 
176
        }
 
177
      }
 
178
    }
 
179
 
 
180
    int[] andTable = { 511, 1023, 2047, 4095 };
 
181
    byte[][] stringTable;
 
182
    byte[] data = null;
 
183
    int tableIndex, bitsToGet = 9;
 
184
    int bytePointer;
 
185
    int nextData = 0;
 
186
    int nextBits = 0;
 
187
  }
 
188
}