~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to Filters/FilterInfo.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// FilterInfo.cs
 
3
//
 
4
// Copyright (C) 2008 D Bera<dbera.web@gmail.com>
 
5
//
 
6
// Permission is hereby granted, free of charge, to any person obtaining a
 
7
// copy of this software and associated documentation files (the "Software"),
 
8
// to deal in the Software without restriction, including without limitation
 
9
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
10
// and/or sell copies of the Software, and to permit persons to whom the
 
11
// Software is furnished to do so, subject to the following conditions:
 
12
//
 
13
// The above copyright notice and this permission notice shall be included in
 
14
// all copies or substantial portions of the Software.
 
15
//
 
16
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
17
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
18
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 
19
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
20
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
21
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
22
// DEALINGS IN THE SOFTWARE.
 
23
//
 
24
 
 
25
using System;
 
26
using System.IO;
 
27
using System.Text;
 
28
using System.Text.RegularExpressions;
 
29
 
 
30
using Beagle.Util;
 
31
using Beagle.Daemon;
 
32
 
 
33
using ICSharpCode.SharpZipLib.GZip;
 
34
using ICSharpCode.SharpZipLib.BZip2;
 
35
using Decoder = SevenZip.Compression.LZMA.Decoder;
 
36
 
 
37
namespace Beagle.Filters {
 
38
 
 
39
        public class FilterInfo : Beagle.Daemon.Filter {
 
40
 
 
41
                public FilterInfo ()
 
42
                {
 
43
                        SetFileType ("documentation");
 
44
                }
 
45
 
 
46
                protected override void RegisterSupportedTypes ()
 
47
                {
 
48
                        // common paths
 
49
                        AddSupportedFlavor (new FilterFlavor ("file:///usr/share/info/*", ".lzma", null, 1));
 
50
                        AddSupportedFlavor (new FilterFlavor ("file:///usr/share/info/*", ".gz", null, 1));
 
51
                        AddSupportedFlavor (new FilterFlavor ("file:///usr/share/info/*", ".bz2", null, 1));
 
52
                        AddSupportedFlavor (new FilterFlavor ("file:///usr/share/info/*", ".info", null, 1));
 
53
                }
 
54
 
 
55
                const char c = ''; // Info node separator
 
56
                private bool skipping = false; // use this to skip certain nodes
 
57
                static readonly char[] node_line_sep = new char[] {'*', ':', '.', ','};
 
58
 
 
59
                private TextReader reader;
 
60
 
 
61
                protected override void DoOpen (FileInfo info)
 
62
                {
 
63
                        if (Extension == ".gz" || Extension == ".bz2" || Extension == ".lzma")
 
64
                                GetCompressedInfoReader ();
 
65
                        else
 
66
                                reader = base.TextReader;
 
67
                }
 
68
 
 
69
                override protected void DoPullProperties ()
 
70
                {
 
71
                        // start reading lines till the first node is hit
 
72
                        string line;
 
73
                        while ((line = reader.ReadLine ()) != null) {
 
74
                                if (line.Length == 0)
 
75
                                        continue;
 
76
 
 
77
                                if (line [0] == c) {
 
78
                                        // no title node
 
79
                                        skipping = ReportNewNode ();
 
80
                                        return;
 
81
                                }
 
82
 
 
83
                                if (line [0] != '*')
 
84
                                        continue;
 
85
 
 
86
                                string[] words = line.Split (node_line_sep, StringSplitOptions.RemoveEmptyEntries);
 
87
                                AddProperty (Beagle.Property.New ("dc:title", words [0].Trim ()));
 
88
                                skipping = true;
 
89
                                return;
 
90
                        }
 
91
 
 
92
                        if (line == null) {
 
93
                                Finished ();
 
94
                                return;
 
95
                        }
 
96
                }
 
97
 
 
98
                override protected void DoPull ()
 
99
                {
 
100
                        string line;
 
101
                                                
 
102
                        line = reader.ReadLine ();
 
103
                        if (line == null) {
 
104
                                Finished ();
 
105
                                return;
 
106
                        }
 
107
 
 
108
                        if (line.Length == 0)
 
109
                                return;
 
110
 
 
111
                        if (skipping && line [0] != c)
 
112
                                return;
 
113
 
 
114
                        if (line [0] != c) {
 
115
                                if (! skipping && ! line.StartsWith ("*"))
 
116
                                        AppendLine (line);
 
117
                                return;
 
118
                        }
 
119
 
 
120
                        skipping = ReportNewNode ();
 
121
                }
 
122
 
 
123
                // Returns true if the node should be skipped
 
124
                private bool ReportNewNode ()
 
125
                {
 
126
                        // Starting a new node
 
127
                        // Read the next line
 
128
                        string line = reader.ReadLine ();
 
129
                        if (line == null) {
 
130
                                Finished ();
 
131
                                return true;
 
132
                        }
 
133
 
 
134
                        if (line.StartsWith ("Indirect:") ||
 
135
                            line.StartsWith ("Tag Table:") ||
 
136
                            line.Contains ("Node: Index") ||
 
137
                            line.Contains ("Node: Top")) {
 
138
                                return true;
 
139
                        }
 
140
 
 
141
                        return false;
 
142
                }
 
143
 
 
144
                private void GetCompressedInfoReader ()
 
145
                {
 
146
                        StreamReader compressed_reader = null;
 
147
 
 
148
                        try {
 
149
                                Stream stream = null;
 
150
                                if (Extension == ".gz")
 
151
                                        stream = new GZipInputStream (Stream);
 
152
                                else if (Extension == ".bz2")
 
153
                                        stream = new BZip2InputStream (Stream);
 
154
                                else if (Extension == ".lzma")
 
155
                                        stream = GetLzmaStream (Stream);
 
156
 
 
157
                                compressed_reader = new StreamReader (stream);
 
158
                        } catch (Exception e) {
 
159
                                Log.Error (e, "Error in opening compressed man page");
 
160
                                if (compressed_reader != null)
 
161
                                        compressed_reader.Close ();
 
162
                                Error ();
 
163
                                return;
 
164
                        }
 
165
 
 
166
                        reader = compressed_reader;
 
167
                }
 
168
 
 
169
                protected override void DoClose ()
 
170
                {
 
171
                        if (Extension == ".gz" || Extension == ".bz2" || Extension == ".lzma")
 
172
                                if (reader != null)
 
173
                                        reader.Close ();
 
174
                }
 
175
 
 
176
                private Stream GetLzmaStream (Stream in_stream)
 
177
                {
 
178
                        // From LzmaAlone.cs
 
179
                        byte[] properties = new byte [5];
 
180
                        if (in_stream.Read (properties, 0, 5) != 5)
 
181
                                throw new Exception ("input .lzma is too short");
 
182
 
 
183
                        Decoder decoder = new Decoder ();
 
184
                        decoder.SetDecoderProperties (properties);
 
185
 
 
186
                        long out_size = 0;
 
187
                        for (int i = 0; i < 8; i++)
 
188
                        {
 
189
                                int v = in_stream.ReadByte ();
 
190
                                if (v < 0)
 
191
                                        throw new Exception ("LZMA: Can't Read 1");
 
192
                                out_size |= ((long)(byte)v) << (8 * i);
 
193
                        }
 
194
                        long compressed_size = in_stream.Length - in_stream.Position;
 
195
 
 
196
                        // FIXME: Man pages are small enough to use a MemoryStream to store the
 
197
                        // entire uncompressed file.
 
198
                        // Still, a proper stream based approach would be good. Unfortunately,
 
199
                        // LZMA does not provide a streaming interface. Current hacks involve
 
200
                        // a separate synchronized thread.
 
201
                        MemoryStream out_stream = new MemoryStream ((int) out_size); // outsize is long but this constructor is resizable
 
202
                        decoder.Code (in_stream, out_stream, compressed_size, out_size, null);
 
203
                        //Log.Debug ("Decoded {0} bytes to {1} bytes", compressed_size, out_size);
 
204
                        out_stream.Position = 0;
 
205
                        return out_stream;
 
206
                }
 
207
        }
 
208
}
 
209