Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* libmspub
3 : * Version: MPL 1.1 / GPLv2+ / LGPLv2+
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License or as specified alternatively below. You may obtain a copy of
8 : * the License at http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * Major Contributor(s):
16 : * Copyright (C) 2012 Brennan Vincent <brennanv@email.arizona.edu>
17 : * Copyright (C) 2012 Fridrich Strba <fridrich.strba@bluewin.ch>
18 : *
19 : * All Rights Reserved.
20 : *
21 : * For minor contributions see the git repository.
22 : *
23 : * Alternatively, the contents of this file may be used under the terms of
24 : * either the GNU General Public License Version 2 or later (the "GPLv2+"), or
25 : * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
26 : * in which case the provisions of the GPLv2+ or the LGPLv2+ are applicable
27 : * instead of those above.
28 : */
29 :
30 : #include <sstream>
31 : #include <string>
32 : #include <string.h>
33 : #include <boost/scoped_ptr.hpp>
34 : #include "MSPUBDocument.h"
35 : #include "MSPUBSVGGenerator.h"
36 : #include "MSPUBCollector.h"
37 : #include "MSPUBParser.h"
38 : #include "MSPUBParser2k.h"
39 : #include "MSPUBParser97.h"
40 : #include "libmspub_utils.h"
41 :
42 : namespace
43 : {
44 :
45 : enum MSPUBVersion
46 : {
47 : MSPUB_UNKNOWN_VERSION = 0,
48 : MSPUB_2K,
49 : MSPUB_2K2
50 : };
51 :
52 0 : MSPUBVersion getVersion(WPXInputStream *input)
53 : {
54 0 : MSPUBVersion version = MSPUB_UNKNOWN_VERSION;
55 : try
56 : {
57 0 : if (!input->isOLEStream())
58 0 : return MSPUB_UNKNOWN_VERSION;
59 :
60 0 : boost::scoped_ptr<WPXInputStream> contentsStream(input->getDocumentOLEStream("Contents"));
61 0 : if (!contentsStream)
62 0 : return MSPUB_UNKNOWN_VERSION;
63 :
64 0 : if (0xe8 != libmspub::readU8(contentsStream.get()) || 0xac != libmspub::readU8(contentsStream.get()))
65 0 : return MSPUB_UNKNOWN_VERSION;
66 :
67 0 : unsigned char magicVersionByte = libmspub::readU8(contentsStream.get());
68 :
69 0 : if (0x00 != libmspub::readU8(contentsStream.get()))
70 0 : return MSPUB_UNKNOWN_VERSION;
71 0 : switch(magicVersionByte)
72 : {
73 : case 0x2C:
74 0 : version = MSPUB_2K2;
75 0 : break;
76 : case 0x22:
77 0 : version = MSPUB_2K;
78 0 : break;
79 : default:
80 0 : break;
81 : }
82 0 : return version;
83 : }
84 0 : catch (...)
85 : {
86 0 : return MSPUB_UNKNOWN_VERSION;
87 : }
88 :
89 : }
90 :
91 : } // anonymous namespace
92 :
93 :
94 :
95 : /**
96 : Analyzes the content of an input stream to see if it can be parsed
97 : \param input The input stream
98 : \return A value that indicates whether the content from the input
99 : stream is a Microsoft Publisher Document that libmspub is able to parse
100 : */
101 0 : bool libmspub::MSPUBDocument::isSupported(WPXInputStream *input)
102 : {
103 : try
104 : {
105 0 : MSPUBVersion version = getVersion(input);
106 0 : if (version == MSPUB_UNKNOWN_VERSION)
107 0 : return false;
108 :
109 0 : if (version == MSPUB_2K2)
110 : {
111 0 : boost::scoped_ptr<WPXInputStream> escherStream(input->getDocumentOLEStream("Escher/EscherStm"));
112 0 : if (!escherStream)
113 0 : return false;
114 0 : boost::scoped_ptr<WPXInputStream> quillStream(input->getDocumentOLEStream("Quill/QuillSub/CONTENTS"));
115 0 : if (!quillStream)
116 0 : return false;
117 : }
118 0 : return true;
119 : }
120 0 : catch (...)
121 : {
122 0 : return false;
123 : }
124 : }
125 :
126 : /**
127 : Parses the input stream content. It will make callbacks to the functions provided by a
128 : WPGPaintInterface class implementation when needed. This is often commonly called the
129 : 'main parsing routine'.
130 : \param input The input stream
131 : \param painter A MSPUBPainterInterface implementation
132 : \return A value that indicates whether the parsing was successful
133 : */
134 0 : bool libmspub::MSPUBDocument::parse(::WPXInputStream *input, libwpg::WPGPaintInterface *painter)
135 : {
136 0 : MSPUBCollector collector(painter);
137 0 : input->seek(0, WPX_SEEK_SET);
138 0 : boost::scoped_ptr<MSPUBParser> parser;
139 0 : switch (getVersion(input))
140 : {
141 : case MSPUB_2K:
142 : {
143 0 : boost::scoped_ptr<WPXInputStream> quillStream(input->getDocumentOLEStream("Quill/QuillSub/CONTENTS"));
144 0 : if (!quillStream)
145 : {
146 0 : boost::scoped_ptr<MSPUBParser> tmp(new MSPUBParser97(input, &collector));
147 0 : parser.swap(tmp);
148 : }
149 : else
150 : {
151 0 : boost::scoped_ptr<MSPUBParser> tmp(new MSPUBParser2k(input, &collector));
152 0 : parser.swap(tmp);
153 : }
154 0 : break;
155 : }
156 : case MSPUB_2K2:
157 : {
158 0 : boost::scoped_ptr<MSPUBParser> tmp(new MSPUBParser(input, &collector));
159 0 : parser.swap(tmp);
160 0 : break;
161 : }
162 : default:
163 0 : return false;
164 : }
165 0 : if (parser)
166 : {
167 0 : return parser->parse();
168 : }
169 0 : return false;
170 : }
171 :
172 : /**
173 : Parses the input stream content and generates a valid Scalable Vector Graphics
174 : Provided as a convenience function for applications that support SVG internally.
175 : \param input The input stream
176 : \param output The output string whose content is the resulting SVG
177 : \return A value that indicates whether the SVG generation was successful.
178 : */
179 0 : bool libmspub::MSPUBDocument::generateSVG(::WPXInputStream *input, libmspub::MSPUBStringVector &output)
180 : {
181 0 : libmspub::MSPUBSVGGenerator generator(output);
182 0 : bool result = libmspub::MSPUBDocument::parse(input, &generator);
183 0 : return result;
184 0 : }
185 :
186 : /* vim:set shiftwidth=2 softtabstop=2 expandtab: */
|