| 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | * This file is part of the libcdr project. |
| 4 | * |
| 5 | * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | */ |
| 9 | |
| 10 | #include <memory> |
| 11 | #include <string> |
| 12 | |
| 13 | #include <libcdr/libcdr.h> |
| 14 | #include "CDRParser.h" |
| 15 | #include "CDRContentCollector.h" |
| 16 | #include "CDRStylesCollector.h" |
| 17 | #include "libcdr_utils.h" |
| 18 | #include "CDRDocumentStructure.h" |
| 19 | |
| 20 | using namespace libcdr; |
| 21 | |
| 22 | namespace |
| 23 | { |
| 24 | |
| 25 | static unsigned getCDRVersion(librevenge::RVNGInputStream *input) |
| 26 | { |
| 27 | unsigned riff = readU32(input); |
| 28 | if ((riff & 0xffff) == 0x4c57) // "WL<micro>\0" |
| 29 | return 200; |
| 30 | if (riff != CDR_FOURCC_RIFF) |
| 31 | return 0; |
| 32 | input->seek(4, librevenge::RVNG_SEEK_CUR); |
| 33 | auto signature_c = (char)readU8(input); |
| 34 | if (signature_c != 'C' && signature_c != 'c') |
| 35 | return 0; |
| 36 | auto signature_d = (char)readU8(input); |
| 37 | if (signature_d != 'D' && signature_d != 'd') |
| 38 | return 0; |
| 39 | auto signature_r = (char)readU8(input); |
| 40 | if (signature_r != 'R' && signature_r != 'r') |
| 41 | return 0; |
| 42 | unsigned char c = readU8(input); |
| 43 | if (c == 0x20) |
| 44 | return 300; |
| 45 | else if (c < 0x31) |
| 46 | return 0; |
| 47 | else if (c < 0x3a) |
| 48 | return 100 * (c - 0x30); |
| 49 | else if (c < 0x41) |
| 50 | return 0; |
| 51 | return 100 * (c - 0x37); |
| 52 | } |
| 53 | |
| 54 | } // anonymous namespace |
| 55 | |
| 56 | /** |
| 57 | Analyzes the content of an input stream to see if it can be parsed |
| 58 | \param input_ The input stream |
| 59 | \return A value that indicates whether the content from the input |
| 60 | stream is a Corel Draw Document that libcdr is able to parse |
| 61 | */ |
| 62 | CDRAPI bool libcdr::CDRDocument::isSupported(librevenge::RVNGInputStream *input_) try |
| 63 | { |
| 64 | if (!input_) |
| 65 | return false; |
| 66 | |
| 67 | librevenge::RVNGInputStream *tmpInput = input_; |
| 68 | std::shared_ptr<librevenge::RVNGInputStream> input(input_, CDRDummyDeleter()); |
| 69 | |
| 70 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 71 | unsigned version = getCDRVersion(input.get()); |
| 72 | if (version) |
| 73 | return true; |
| 74 | if (tmpInput->isStructured()) |
| 75 | { |
| 76 | input.reset(tmpInput->getSubStreamByName("content/riffData.cdr")); |
| 77 | if (!input) |
| 78 | input.reset(tmpInput->getSubStreamByName("content/root.dat")); |
| 79 | } |
| 80 | tmpInput->seek(0, librevenge::RVNG_SEEK_SET); |
| 81 | if (!input) |
| 82 | return false; |
| 83 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 84 | version = getCDRVersion(input.get()); |
| 85 | if (!version) |
| 86 | return false; |
| 87 | return true; |
| 88 | } |
| 89 | catch (...) |
| 90 | { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | Parses the input stream content. It will make callbacks to the functions provided by a |
| 96 | CDRPaintInterface class implementation when needed. This is often commonly called the |
| 97 | 'main parsing routine'. |
| 98 | \param input_ The input stream |
| 99 | \param painter A CDRPainterInterface implementation |
| 100 | \return A value that indicates whether the parsing was successful |
| 101 | */ |
| 102 | CDRAPI bool libcdr::CDRDocument::parse(librevenge::RVNGInputStream *input_, librevenge::RVNGDrawingInterface *painter) |
| 103 | { |
| 104 | if (!input_ || !painter) |
| 105 | return false; |
| 106 | |
| 107 | std::shared_ptr<librevenge::RVNGInputStream> input(input_, CDRDummyDeleter()); |
| 108 | |
| 109 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 110 | bool retVal = false; |
| 111 | unsigned version = 0; |
| 112 | try |
| 113 | { |
| 114 | version = getCDRVersion(input.get()); |
| 115 | if (version) |
| 116 | { |
| 117 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 118 | CDRParserState ps; |
| 119 | std::vector<std::unique_ptr<librevenge::RVNGInputStream>> dummyDataStreams; |
| 120 | CDRStylesCollector stylesCollector(ps); |
| 121 | CDRParser stylesParser(dummyDataStreams, &stylesCollector); |
| 122 | if (version >= 300) |
| 123 | retVal = stylesParser.parseRecords(input.get()); |
| 124 | else |
| 125 | retVal = stylesParser.parseWaldo(input.get()); |
| 126 | if (ps.m_pages.empty()) |
| 127 | retVal = false; |
| 128 | if (retVal) |
| 129 | { |
| 130 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 131 | CDRContentCollector contentCollector(ps, painter); |
| 132 | CDRParser contentParser(dummyDataStreams, &contentCollector); |
| 133 | if (version >= 300) |
| 134 | retVal = contentParser.parseRecords(input.get()); |
| 135 | else |
| 136 | retVal = contentParser.parseWaldo(input.get()); |
| 137 | } |
| 138 | return retVal; |
| 139 | } |
| 140 | } |
| 141 | catch (libcdr::EndOfStreamException const &) |
| 142 | { |
| 143 | // This can only happen if isSupported() has not been called before |
| 144 | return false; |
| 145 | } |
| 146 | |
| 147 | librevenge::RVNGInputStream *tmpInput = input_; |
| 148 | try |
| 149 | { |
| 150 | std::vector<std::string> dataFiles; |
| 151 | if (tmpInput->isStructured()) |
| 152 | { |
| 153 | tmpInput->seek(0, librevenge::RVNG_SEEK_SET); |
| 154 | input.reset(tmpInput->getSubStreamByName("content/riffData.cdr")); |
| 155 | if (!input) |
| 156 | { |
| 157 | tmpInput->seek(0, librevenge::RVNG_SEEK_SET); |
| 158 | input.reset(tmpInput->getSubStreamByName("content/root.dat")); |
| 159 | if (input) |
| 160 | { |
| 161 | std::unique_ptr<librevenge::RVNGInputStream> tmpStream(tmpInput->getSubStreamByName("content/dataFileList.dat")); |
| 162 | if (bool(tmpStream)) |
| 163 | { |
| 164 | std::string dataFileName; |
| 165 | while (!tmpStream->isEnd()) |
| 166 | { |
| 167 | unsigned char character = readU8(tmpStream.get()); |
| 168 | if (character == 0x0a) |
| 169 | { |
| 170 | dataFiles.push_back(dataFileName); |
| 171 | dataFileName.clear(); |
| 172 | } |
| 173 | else |
| 174 | dataFileName += (char)character; |
| 175 | } |
| 176 | if (!dataFileName.empty()) |
| 177 | dataFiles.push_back(dataFileName); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | std::vector<std::unique_ptr<librevenge::RVNGInputStream>> dataStreams; |
| 183 | dataStreams.reserve(dataFiles.size()); |
| 184 | for (const auto &dataFile : dataFiles) |
| 185 | { |
| 186 | std::string streamName("content/data/"); |
| 187 | streamName += dataFile; |
| 188 | CDR_DEBUG_MSG(("Extracting stream: %s\n", streamName.c_str())); |
| 189 | tmpInput->seek(0, librevenge::RVNG_SEEK_SET); |
| 190 | std::unique_ptr<librevenge::RVNGInputStream> strm(tmpInput->getSubStreamByName(streamName.c_str())); |
| 191 | dataStreams.push_back(std::move(strm)); |
| 192 | } |
| 193 | if (!input) |
| 194 | input.reset(tmpInput, CDRDummyDeleter()); |
| 195 | CDRParserState ps; |
| 196 | { |
| 197 | // libcdr extension to the getSubStreamByName. Will extract the first stream in the |
| 198 | // given directory |
| 199 | tmpInput->seek(0, librevenge::RVNG_SEEK_SET); |
| 200 | std::unique_ptr<librevenge::RVNGInputStream> cmykProfile(tmpInput->getSubStreamByName("color/profiles/cmyk/")); |
| 201 | if (cmykProfile) |
| 202 | ps.setColorTransform(cmykProfile.get()); |
| 203 | } |
| 204 | { |
| 205 | tmpInput->seek(0, librevenge::RVNG_SEEK_SET); |
| 206 | std::unique_ptr<librevenge::RVNGInputStream> rgbProfile(tmpInput->getSubStreamByName("color/profiles/rgb/")); |
| 207 | if (rgbProfile) |
| 208 | ps.setColorTransform(rgbProfile.get()); |
| 209 | } |
| 210 | CDRStylesCollector stylesCollector(ps); |
| 211 | CDRParser stylesParser(dataStreams, &stylesCollector); |
| 212 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 213 | retVal = stylesParser.parseRecords(input.get()); |
| 214 | if (ps.m_pages.empty()) |
| 215 | retVal = false; |
| 216 | if (retVal) |
| 217 | { |
| 218 | input->seek(0, librevenge::RVNG_SEEK_SET); |
| 219 | CDRContentCollector contentCollector(ps, painter); |
| 220 | CDRParser contentParser(dataStreams, &contentCollector); |
| 221 | retVal = contentParser.parseRecords(input.get()); |
| 222 | } |
| 223 | } |
| 224 | catch (libcdr::EndOfStreamException const &) |
| 225 | { |
| 226 | retVal = false; |
| 227 | } |
| 228 | return retVal; |
| 229 | } |
| 230 | |
| 231 | /* vim:set shiftwidth=2 softtabstop=2 expandtab: */ |